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

SynthVoice.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 
00008 
00009 #include "SynthVoice.h"
00010 
00011 namespace SynthCore {
00012 
00013 SynthVoice::SynthVoice(Synth * S, bool defaultSetup) {
00014         HostSynth = S;
00015         noteOn = false;
00016         aLive = false;
00017         MidiFreq=440.0;
00018         playingNote = 0;
00019 
00020         
00021         if (defaultSetup) {
00022                 myVFO = new Sampler(this); add(myVFO);
00023                 myEnvelope = new Envelope3(this); 
00024                 myEnvelope->killVoice = true; 
00025                 myEnvelope->setDuration(48000);
00026                 myEnvelope->myData->setADSR(0.1f,0.2f,0.5f,0.2f);
00027                 add(myEnvelope);
00028                 myRez = new BiQuad(this);  add(myRez);
00029                 myEnv2 = new Envelope3(this); add(myEnv2);
00030 
00031                 // The diagram is as follows:
00032                 // FMOSC -> ENVELOPE -> BIQUADRATIC RESONANCE FILTER -> OUTPUT
00033 
00034                 myEnvelope->Input1->ConnectFrom(myVFO->Output1);
00035                 myRez->Input1->ConnectFrom(myEnvelope->Output1);
00036 
00037                 myRez->FreqInput->ConnectFrom(myEnv2->Output1);
00038 
00039                 SynthOutput = myRez->Output1;   
00040         }
00041 }
00042 
00043 
00044 
00045 void SynthVoice::newMidiEvent()
00046 {       // Pass event to all modules.
00047         for(ModuleMapIter it = voiceMap.begin(); it != voiceMap.end(); ++it)
00048                 (*it)->newMidiEvent();
00049 }
00050 
00051 void SynthVoice::add(Module * m)
00052 {
00053         voiceMap.push_back(m);
00054         HostSynth->add(m);
00055 }
00056 
00057 SynthVoice * SynthVoice::sharedClone() {
00058         // Iterate over 
00059         SynthVoice * SV = new SynthVoice(HostSynth, false);
00060 
00061         std::map<Output *, Output *> OrigToCopy;
00062 
00063         // Well make copies and build a map over Orig's  output pointers and Copy output pointers
00064         {
00065         for(ModuleMapIter it = voiceMap.begin(); it != voiceMap.end(); ++it) {
00066 
00067                 Module * M = (*it)->sharedClone();
00068                 M->hostVoice = SV;
00069 
00070                 for (int i = 0; i < (*it)->getNoOutputs(); i++) {
00071                         Output * OutOrig = (*it)->getOutput(i);
00072                         Output * OutCopy = M->getOutput(i);             
00073                         OrigToCopy[OutOrig] = OutCopy;
00074                         cout << "Registering orig:" << (int)OutOrig << " to copy:" << (int)(OutCopy) << endl;
00075                 }
00076 
00077                 // Copy the input ConFrom pointers (even though we need to overwrite them later!)
00078                 {
00079                 for (int i = 0; i < (*it)->getNoInputs(); i++) {
00080                         M->getInput(i)->ConFrom = (*it)->getInput(i)->ConFrom;
00081 
00082                 }}
00083 
00084 
00085                 SV->add(M);
00086                 
00087         }}
00088 
00089         // Now rewire the Synth copy.
00090         for(ModuleMapIter it = SV->voiceMap.begin(); it != SV->voiceMap.end(); ++it) {
00091 
00092                 
00093                 for (int i = 0; i < (*it)->getNoInputs(); i++) {
00094                         Input * in = (*it)->getInput(i);
00095 
00096                         if (in->ConFrom != 0) {
00097                                 cout << "Trying to find: " << in->ConFrom << endl;
00098                                 std::map<Output *, Output *>::iterator it = OrigToCopy.find(in->ConFrom);                       
00099                                 if (it != OrigToCopy.end()) { 
00100                                         Output * out;
00101                                     out = (*it).second; 
00102                                         in->ConnectFrom(out);
00103                                 } else {
00104                                         cout << "BAD CLONE ERROR!"<< endl;
00105                                 }
00106                         }
00107 
00108                 }
00109 
00110                 
00111         }
00112 
00113         // Connect the SynthOutput
00114         {
00115         std::map<Output *, Output *>::iterator it = OrigToCopy.find(SynthOutput);
00116         if (it != OrigToCopy.end()) { 
00117                 SV->SynthOutput = (*it).second; 
00118                 
00119         } else {
00120                 cout << "SynthOutput error!"<< endl;
00121         }}
00122 
00123         return SV;
00124 }
00125 
00128 SynthVoice::~SynthVoice() {
00129 
00130 }
00131 
00132 }; // end of namespace: SynthCore

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