logo
Main Page | Namespaces | Classes | Compounds | Files | Compound Members | Related

OscDialog.cpp

00001 //
00002 //  SYNTOPIA. See http://Syntopia.sourceforge.net for details and documentation.        
00003 //
00004 //      Author of this file: Mikael Hvidtfeldt Christensen (mikaelc@users.sourceforge.net)
00005 //
00006 
00007 #include "OscDialog.h"
00008 #include "MainDialog.h"
00009 #include <complex>
00010 #include "windows.h"
00011 
00012 namespace SynthGUI {
00013 
00014 Sampler * OscDialog::getSampler(int i)
00015 {
00016         // Unsafe typecast
00017         return (Sampler *) myMod->mv[i];
00018 }
00019 
00020 OscDialog::OscDialog(mod * mmod) {
00021         myWindow = new WinForm(640,650, WinForm::standard, "Syntopia - Oscillator setup");
00022         myFreqTable= new FreqTable();
00023         
00024         myMod = mmod;
00025 
00026         // getSampler(0) is the sampler we will draw.
00027         mySampler = getSampler(0);
00028 
00029         modCount = myMod->mv.size();
00030 
00031         for (int j = 0; j< modCount; j++)
00032         {
00033                 std::cout << j << " : " << myMod->mv[j]->getName() << " : " << myMod->mv[j]->ModID << std::endl;
00034         }
00035 
00036         mySampler->hostVoice->setMidiNote(67);
00037         mySampler->hostVoice->setMidiFreq(myFreqTable->freqTab[67]);
00038         mySampler->newMidiEvent();
00039         showScaled = true;
00040         showFFT = false;
00041 
00042         int lx1 = 50;
00043         int lx2 = 280;
00044         int ly1 = 50;
00045         int lx3 = 510;
00046 
00047         int yy1 = 50;                   // wave
00048         int yy2 = yy1+30;               // show key
00049         int yy3 = yy2+30;               // canvas
00050         int yy4 = yy3+210;              // freq
00051         int yy5 = yy4+30;               // fm
00052         int yy6 = yy5+30;               // pwm
00053         int yy6d = yy6 + 30;
00054         int yy7 = yy6d+10;              // output
00055         int yy7d = yy7 + 30;
00056         
00057         int yy8 = yy7d+10;              // quality
00058         int yy9 = yy8+30;               // interpol
00059         int yy10 = yy9+30;              // samplelength
00060         int yy11 = yy10+30;             // oversampling
00061         int yy11d = yy11+30;    // oversampling
00062 
00063 
00064 
00065         // setup Labels
00066 
00067         label0 = new Label("Oscillator [Sample based]");
00068         label1 = new Label("Wave");
00069         label2 = new Label("Show MidiKey:");
00070         label5 = new Label("Output:");
00071 
00072         label6 = new Label("Quality:");;
00073         label7 = new Label("Interpolation:");
00074         label8 = new Label("Samplelength:");
00075         label9 = new Label("Oversampling:");            
00076 
00077         label0->PutInWinForm(myWindow, 10, 10, 200, 30);
00078         
00079         label1->PutInWinForm(myWindow, lx1, yy1, 200, 30);
00080         label2->PutInWinForm(myWindow, lx1, yy2, 200, 30);
00081         label5->PutInWinForm(myWindow, lx1, yy7, 200, 30);
00082 
00083         label6->PutInWinForm(myWindow, lx1, yy8, 200, 30);
00084         label7->PutInWinForm(myWindow, lx1, yy9, 200, 30);
00085         label8->PutInWinForm(myWindow, lx1, yy10, 200, 30);
00086         label9->PutInWinForm(myWindow, lx1, yy11, 200, 30);
00087 
00088         // setup ComboBoxes
00089 
00090         WavSel1DD = new ComboBox();
00091         WavSel1DD->PutInWinForm(myWindow, lx1+70, yy1, 100, 140);
00092         WavSel1DD->add(WS1DD1 = new ComboBoxItem("Preset:"));
00093         WavSel1DD->add(WS1DD2 = new ComboBoxItem("User:"));
00094         WavSel1DD->setSelected(WS1DD1);
00095 
00096         WavSel2DD = new ComboBox();
00097         WavSel2DD->PutInWinForm(myWindow, lx1+180, yy1, 130, 140);
00098         WavSel2DD->add(WS2DD1 = new ComboBoxItem("Sine"));
00099         WavSel2DD->add(WS2DD2 = new ComboBoxItem("Saw"));
00100         WavSel2DD->add(WS2DD3 = new ComboBoxItem("Triangle"));
00101         WavSel2DD->add(WS2DD4 = new ComboBoxItem("Square"));
00102         WavSel2DD->add(WS2DD5 = new ComboBoxItem("XX 1"));
00103         WavSel2DD->add(WS2DD6 = new ComboBoxItem("XX 2"));
00104         WavSel2DD->add(WS2DD7 = new ComboBoxItem("XX 3"));
00105         WavSel2DD->add(WS2DD8 = new ComboBoxItem("XX 4"));
00106         WavSel2DD->setSelected(WS2DD1);
00107 
00108         WavSel3DD = new ComboBox();
00109         WavSel3DD->PutInWinForm(myWindow, lx1+180, yy1-30, 200, 140);
00110         WavSel3DD->add(WS3DD1 = new ComboBoxItem("None available.."));
00111         WavSel3DD->setSelected(WS3DD1);
00112         WavSel3DD->hide();
00113 
00114         outputDD = new ComboBox();
00115         outputDD->PutInWinForm(myWindow, lx1+100, yy7, (lx3-80)-(lx1+100), 140);
00116         outputDD->add(outputDD1 = new ComboBoxItem("None available.."));
00117         outputDD->setSelected(outputDD1);
00118         
00119 
00120         interpolDD = new ComboBox();
00121         interpolDD->PutInWinForm(myWindow, lx1+100, yy9,(lx3-80)-(lx1+100), 140);
00122         interpolDD->add(interpolDD1 = new ComboBoxItem("None."));
00123         interpolDD->add(interpolDD2 = new ComboBoxItem("Linear."));
00124         interpolDD->add(interpolDD3 = new ComboBoxItem("Cubic Hermite."));
00125         interpolDD->add(interpolDD4 = new ComboBoxItem("5 Point Spline."));
00126         interpolDD->add(interpolDD5 = new ComboBoxItem("Pure Sine (reference signal)"));
00127         interpolDD->setSelected(interpolDD1);
00128 
00129                 
00130         // Setup Canvases.
00131 
00132         waveCanvas = new MCanvas();
00133         waveCanvas->PutInWinForm(myWindow, lx1,yy3,430,200);
00134         
00135         // Setup Inputs
00136 
00137         freqInput = new AInput("Freq", 100.0f, 22000.0f, 1000, false);
00138         FMInput = new AInput("FM", 0.0001f, 1.5f, 1000, false);
00139         PWMInput = new AInput("PWM", 0.0001f, 1.5f, 1000, false);
00140         midikeyInput = new SlideInput("Show Midi Key:", 21.0, 107.0, 86, true);
00141 
00142         freqInput->PutInWinForm(myWindow, lx1,yy4,200,200);
00143         FMInput->PutInWinForm(myWindow, lx1,yy5,200,200);
00144         PWMInput->PutInWinForm(myWindow, lx1,yy6,200,200);
00145         midikeyInput->PutInWinForm(myWindow, lx1, yy2 , 200,200);
00146 
00147         // Setup buttons
00148 
00149         OKButton = new Button("OK");
00150         CancelButton = new Button("Cancel");
00151         EditButton = new Button("Edit..");
00152         scaleButton = new Button("Scale..");
00153         FFTButton = new Button("FFT.");
00154         
00155         OKButton->PutInWinForm(myWindow,lx3,ly1,100,28);
00156         CancelButton->PutInWinForm(myWindow,lx3,ly1+40,100,28);
00157         EditButton->PutInWinForm(myWindow,lx1+320,yy1,100,28);
00158         scaleButton->PutInWinForm(myWindow,lx3,yy3+40,100,28);
00159         FFTButton->PutInWinForm(myWindow,lx3,yy3+80,100,28);
00160 
00161         // Setup Dividers
00162 
00163         div1 = new divider();
00164         div2 = new divider();
00165         div3 = new divider();
00166         div4 = new divider();
00167         div5 = new divider();
00168 
00169         div1->PutInWinForm(myWindow, lx3-15, 5, 5, 600);
00170         div2->PutInWinForm(myWindow, 5, 29, lx3-35, 5);
00171         div3->PutInWinForm(myWindow, lx1, yy6d, lx3-80 ,5);
00172         div4->PutInWinForm(myWindow, lx1, yy7d, lx3-80, 5);
00173         div5->PutInWinForm(myWindow, lx1, yy11d, lx3-80, 5);
00174 
00175         // Debug:
00176         myText2 = new TextBox();
00177         myText2->PutInWinForm(myWindow,lx1+600,450,lx2+200-lx1,100);
00178 
00179         GUIControl::GUIInstance->ErrorTextBox = myText2;
00180         OKButton->setEventHandler(this);
00181         CancelButton->setEventHandler(this);
00182         midikeyInput->setEventHandler(this);
00183         WavSel2DD->setEventHandler(this);
00184         interpolDD->setEventHandler(this);
00185         FFTButton->setEventHandler(this);
00186         scaleButton->setEventHandler(this);
00187         /*
00188         myText->setEventHandler(this);
00189         myText2->setEventHandler(this);
00190 
00191         OscDropDown->setEventHandler(this);
00192         freqInput->setEventHandler(this);
00193         QInput->setEventHandler(this);
00194         */
00195         myWindow->show();
00196         drawSample();
00197 }
00198 
00199 void OscDialog::MessageLoop() {
00200         // Enter Loop until Exit.
00201         GUIControl::GUIInstance->MessageLoop();
00202 }
00203 
00204 OscDialog::~OscDialog() {
00205         // Clean Up.
00206         delete(myWindow);
00207 }
00208 
00209 
00210 void OscDialog::drawSample()
00211 {
00212         
00213                 int w=waveCanvas->getWidth()-5;
00214                 int h=waveCanvas->getHeight()-15;
00215                 
00216                 int MAX = 1.1;
00217                 int MIN = -1.1;
00218 
00219                 waveCanvas->clear();
00220 
00221                 float * data;
00222 
00223                 int samples = 0;
00224                 int usamples = 1+(int)(48000.0f/mySampler->hostVoice->getMidiFreq());
00225                 float slen=(48000.0f/mySampler->hostVoice->getMidiFreq());
00226                 if (showScaled) {
00227                         samples = 1+(int)(48000.0f/mySampler->hostVoice->getMidiFreq());
00228                 } else {
00229                     samples = 8192;
00230                 }
00231                  samples = 4096;
00232                  if (showFFT) {samples *= 1;}
00233                 data = new float[samples];
00234                 mySampler->resetPhase();
00235                 for (int j=0; j<samples; j++)
00236                 {       mySampler->update();
00237                         data[j]=mySampler->Output1->read();
00238                 //      std::cout << data[j] << std::endl;
00239                 }
00240 
00241                 if (!showFFT)
00242                 {
00243                         // Draw the sample in time domain
00244                         waveCanvas->setColor(1,1,251);
00245                 float xpos;
00246                 {   waveCanvas->setColor(210,210,10);
00247                         for (int i=0; i<samples; i++) 
00248                         {       
00249                                 if (!showScaled) {
00250                                         xpos = ((float)(i * w)/samples) ;
00251                                 } else {
00252                                         xpos = ((float)(i * w)/slen) ;
00253                                         while (xpos > w) {xpos -= w;}
00254                                 }
00255 
00256                                 waveCanvas->line((int)xpos,h-5-((h-10)*(data[i]-MIN)/(MAX-MIN)),
00257                                                                 (int)xpos,h/2           );      
00258                                 waveCanvas->putPixel((int)xpos,h-5-((h-10)*(data[i]-MIN)/(MAX-MIN)),1,1,1);
00259                                 
00260                         }
00261                 }
00262 
00263                 } else
00264                 {
00265                         // Draw the sample in frequency domain.
00266                         waveCanvas->setColor(210,10,10);
00267                         float * mdata;
00268                         float xpos;
00269                         float val,phase;
00270                         mdata = new float[samples * 2];
00271                         for (int j = 0; j<samples; j++)
00272                         {
00273                                 mdata[j*2]=data[j];
00274                                 mdata[j*2+1]=0;
00275                         }
00276                         Fourier::transform(mdata, samples, 1);
00277                         MIN = 10000;
00278                         {
00279                         for (int i=0; i<samples*2; i=i+2) 
00280                         {       
00281                                 val = sqrt(mdata[i]*mdata[i]+mdata[i+1]*mdata[i+1]);
00282                                 phase = std::arg(std::complex<float>(mdata[i],mdata[i+1]));
00283                                 if (val < 12) val = 12.0;
00284                                 val = 20.0*log(val);
00285                                 mdata[i]=val;
00286                                 mdata[i+1]=phase;
00287                                 if (MIN > val) {MIN = val;}
00288                                 if (MAX < val) {MAX = val;}
00289                         }}
00290 
00291                         for (int i=0; i<samples/2; i++) 
00292                         {       waveCanvas->setColor(210,10,10);
00293                                 xpos = ((float)(i * w)/(samples/2)) ;
00294                                 waveCanvas->line((int)xpos,h/2-((h/2-10)*(mdata[i*2]-MIN)/(MAX-MIN)),
00295                                                                 (int)xpos,h/2           );
00296                                 if (true) {
00297                                 waveCanvas->setColor(10,210,10);
00298                                 waveCanvas->line((int)xpos,h/2-((h/2-10)*(mdata[i*2+1]-3.14)/(6.28)),
00299                                                                 (int)xpos,h/2           );
00300                                 }
00301                                 //waveCanvas->putPixel((int)xpos,h-5-((h-10)*(mdata[i]-MIN)/(MAX-MIN)),2,2,2);                  
00302                         }
00303                         delete [] mdata;
00304                 }
00305                 delete [] data;
00306                 waveCanvas->doDraw();
00307 }
00308 
00309 
00310 void OscDialog::HandleEvents(event ev) {
00311         if ((ev.EventSource==OKButton)) 
00312         {   
00313                 drawSample();
00314                 std::cout << "OK BUTTON" << std::endl;
00315         }
00316 
00317         if ((ev.EventSource==midikeyInput)) 
00318         {   
00319                 midikey = midikeyInput->getValue();
00320                 mySampler->hostVoice->setMidiNote(midikey);
00321                 mySampler->hostVoice->setMidiFreq(myFreqTable->freqTab[midikey]);
00322                 mySampler->newMidiEvent();
00323                 drawSample();           
00324         }
00325 
00326         if ((ev.EventSource==scaleButton)) {
00327                 showScaled = !showScaled;
00328                 drawSample();
00329         }
00330 
00331         if ((ev.EventSource==FFTButton)) {
00332                 showFFT = !showFFT;
00333                 drawSample();
00334         }
00335 
00336         if ((ev.EventSource==WavSel2DD) && (HIWORD(ev.wParam)==CBN_SELCHANGE)) 
00337         {       
00338                 // Turn off VST update() calls.
00339                 SynthEngine::active(false);
00340 
00341                 if (WavSel2DD->getSelected() == WS2DD2) {
00342                         for (int i=0; i<modCount; i++)
00343                                 getSampler(i)->setSawtooth(1024);
00344                         drawSample();   
00345                 } 
00346                 if (WavSel2DD->getSelected() == WS2DD3) {
00347                         for (int i=0; i<modCount; i++)
00348                           getSampler(i)->setTriangle(1024);
00349                         drawSample();   
00350                 }
00351                 if (WavSel2DD->getSelected() == WS2DD4) {
00352                         for (int i=0; i<modCount; i++)
00353                           getSampler(i)->setSquare(1024);
00354                         drawSample();   
00355                 }
00356                 if (WavSel2DD->getSelected() == WS2DD1) {
00357                         for (int i=0; i<modCount; i++)
00358                           getSampler(i)->setSine(1024);
00359                         drawSample();   
00360                 }
00361                 if (WavSel2DD->getSelected() == WS2DD5) {
00362                         for (int i=0; i<modCount; i++)
00363                           getSampler(i)->setWave1(1024);
00364                         drawSample();   
00365                 }
00366                 if (WavSel2DD->getSelected() == WS2DD6) {
00367                         for (int i=0; i<modCount; i++)
00368                           getSampler(i)->setWave2(1024);
00369                         drawSample();   
00370                 }
00371                 if (WavSel2DD->getSelected() == WS2DD7) {
00372                         for (int i=0; i<modCount; i++)
00373                           getSampler(i)->setWave3(1024);
00374                         drawSample();   
00375                 }
00376                 if (WavSel2DD->getSelected() == WS2DD8) {
00377                         for (int i=0; i<modCount; i++)
00378                           getSampler(i)->setWave4(1024);
00379                         drawSample();   
00380                 }
00381 
00382                 // Turn on VST update() calls.
00383                 SynthEngine::active(true);
00384         }
00385 
00386 
00387         if ((ev.EventSource==interpolDD) && (HIWORD(ev.wParam)==CBN_SELCHANGE)) 
00388         {
00389                 // We got an event from the Interpolation Drop Down box.
00390                 
00391                 if (interpolDD->getSelected() == interpolDD1) {
00392                         // 'none'
00393                         //MessageBox( NULL, "Cool", "NONE SElected", MB_OK | MB_ICONINFORMATION );
00394                         for (int i=0; i<modCount; i++)
00395                                 getSampler(i)->interpolationType = Sampler::none;                       
00396                 } 
00397 
00398                 if (interpolDD->getSelected() == interpolDD2) {
00399                         // 'linear'
00400                         for (int i=0; i<modCount; i++)
00401                           getSampler(i)->interpolationType = Sampler::linear;                   
00402                 }
00403                 if (interpolDD->getSelected() == interpolDD3) {
00404                         // 'Cubic spline'
00405                         for (int i=0; i<modCount; i++)
00406                           getSampler(i)->interpolationType = Sampler::cubicHermite;
00407                 }
00408                 if (interpolDD->getSelected() == interpolDD4) {
00409                         // '5 Point Spline'
00410                         for (int i=0; i<modCount; i++)
00411                           getSampler(i)->interpolationType = Sampler::spline5Point;
00412         
00413                 }
00414                 if (interpolDD->getSelected() == interpolDD5) {
00415                         // 'Pure Sine'
00416                         for (int i=0; i<modCount; i++)
00417                           getSampler(i)->interpolationType = Sampler::pureSine;
00418         
00419                 }
00420                 drawSample();
00421         }
00422         
00423 
00424         
00425 }
00426 
00427 }; // end of namespace: SynthGUI

Syntopia Project. Visit the web page, or the SourceForge page.
Docs made by Doxygen. Email: Mikael Christensen