Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

Snd.cpp

Go to the documentation of this file.
00001 // Snd.cpp: implementation of the jack client's callback
00002 // maintains a list of DSP routines called orch
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);      // will delete all instruments in orchestra list
00020         Stk::setSampleRate ((double) _srate);
00021 //      Stk::setRawwavePath ("/home/cc/220a/system/stk-4.2.1/rawwaves/");
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 /* called in sample loop by jackClient when jack server needs to transfer buffers */
00040 {
00041         bool overflow = false;
00042         if (running)
00043         {
00044                 /* ------ this is where the real work happens ------ */
00045                 ctr++;
00046                 out = tickOrch (in);    // process orchestra objects
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 //  out[0] = out[1] = sin(6.28*0.001*(double)ctr);
00052         }
00053         else // muted
00054         {
00055         for (int ch = 0; ch < _chans; ch++)
00056                 out[ch] = 0.0;
00057         }
00058         if (running)
00059         {
00060                 /* ------ wake any per buffer processes ------ */
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 /* per sample -- tick each orchestra object in orch list */
00095 {
00096         for (int i = 0; i < _chans; i++)
00097                 tmp[i] = 0.0;
00098         Jukebox *i = orch.first ();
00099         while (i)
00100         {                       
00101                 // assign outputs by mod if more objects than channels
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 /* append object to orch list, assign sequential ID number */
00114 {
00115         orch.append (i);
00116         int tmp = orch.count ();
00117         // cout << tmp << endl;
00118 }
00119 
00120 void
00121 Snd::orchUpdate (double val)
00122 /* asynchronous -- update each orchestra object in orch list */
00123 {
00124         Jukebox *i = orch.first ();
00125         while (i)
00126         {
00127 //              i->update (val);
00128                 i = orch.next ();
00129         }
00130 }

Generated on Thu Aug 3 16:14:47 2006 by  doxygen 1.4.4