00001
00002
00003
00004 #include "Snd.h"
00005 #include "Stk.h"
00006 #include "ThreadCommEvent.h"
00007 #include "qmessagebox.h"
00008 #include "math.h"
00009 #include "MainDialog.h"
00010 #include <iostream>
00011 using namespace std;
00012 #undef RT_BUFFER_SIZE
00013
00014 Snd::Snd (int chans, int srate, int buffsize):
00015 _chans(chans),
00016 _srate(srate),
00017 _buffsize(buffsize)
00018 {
00019 orch.setAutoDelete (true);
00020 Stk::setSampleRate ((double) _srate);
00021
00022 out = new double[_chans];
00023 in = new double[_chans];
00024 running = false;
00025 ctr = 0;
00026 timeInc = ((double) (_buffsize)) / Stk::sampleRate ();
00027 tick_ctr = 0;
00028 }
00029
00030 Snd::~Snd ()
00031 {
00032 delete[]out;
00033 delete[]in;
00034 }
00035 #define GAIN 0.25
00036
00037 double *
00038 Snd::tickFrame (double *in)
00039
00040 {
00041 bool overflow = false;
00042 if (running)
00043 {
00044
00045 ctr++;
00046 out = tickOrch (in);
00047 for (int ch = 0; ch < _chans; ch++)
00048 out[ch] *= GAIN;
00049 if ((fabs (out[0]) > 1.0) || (fabs (out[0]) > 1.0))
00050 overflow = true;
00051
00052 }
00053 else
00054 {
00055 for (int ch = 0; ch < _chans; ch++)
00056 out[ch] = 0.0;
00057 }
00058 if (running)
00059 {
00060
00061 tick_ctr++;
00062 if (tick_ctr == _buffsize)
00063 {
00064 bufTime += timeInc;
00065 nexttick.wakeAll ();
00066 tick_ctr = 0;
00067 }
00068 }
00069 if (overflow)
00070 {
00071 printf ("overflow\n");
00072 }
00073 return out;
00074 }
00075
00076
00077 void
00078 Snd::go ()
00079 {
00080 ctr = 0;
00081 bufTime = 0.0;
00082 running = true;
00083 tick_ctr = 0;
00084 }
00085
00086 void
00087 Snd::stop ()
00088 {
00089 running = false;
00090 }
00091
00092 double *
00093 Snd::tickOrch (double *tmp)
00094
00095 {
00096 for (int i = 0; i < _chans; i++)
00097 tmp[i] = 0.0;
00098 Jukebox *i = orch.first ();
00099 while (i)
00100 {
00101
00102 i->tick();
00103 for (int ch = 0; ch < _chans; ch++)
00104 tmp[ch] += i->getOut (ch);
00105 i = orch.next ();
00106 }
00107 tmp[1] = tmp[0];
00108 return tmp;
00109 }
00110
00111 void
00112 Snd::addInstrument (Jukebox * i)
00113
00114 {
00115 orch.append (i);
00116 int tmp = orch.count ();
00117
00118 }
00119
00120 void
00121 Snd::orchUpdate (double val)
00122
00123 {
00124 Jukebox *i = orch.first ();
00125 while (i)
00126 {
00127
00128 i = orch.next ();
00129 }
00130 }