Main Page | Namespaces | Classes | Compounds | Files | Compound Members | Related polyphony.cpp00001 // 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 #include "Polyphony.h" 00009 #include "SynthVoice.h" 00010 00011 namespace SynthCore { 00012 00013 00014 Polyphony::Polyphony() { 00015 hostVoice = 0; 00016 00017 Output1 = new Output(this); 00018 00019 polyVoices = 0; 00020 polyVoicesInv = 1.0; 00021 } 00022 00023 Polyphony::~Polyphony() { 00024 delete Output1; 00025 clearInputs(); 00026 }; 00027 00028 void Polyphony::addVoice(SynthVoice * voice) { 00029 Input * newInput = new Input(this); 00030 newInput->ConnectFrom(voice->SynthOutput); 00031 00032 // One vector is be enough! So this is kind of a hack 00033 inputs.push_back(newInput); 00034 voices.push_back(voice); 00035 00036 setVoices(inputs.size()); 00037 } 00038 00039 void Polyphony::clearInputs() { 00040 for (int i = 0; i < inputs.size(); i++) 00041 { 00042 delete inputs[i]; 00043 } 00044 00045 inputs.clear(); 00046 voices.clear(); 00047 00048 polyVoices = 0; 00049 polyVoicesInv = 1.0; 00050 } 00051 00052 void Polyphony::setVoices(int number) { 00053 // We need to do a lot more than this here! 00054 // IE clear tables and so on. 00055 polyVoices = number; 00056 00057 // We dont wont do divisions when we dont have to. 00058 polyVoicesInv = 1/(float) polyVoices; 00059 } 00060 00061 void Polyphony::update() { 00062 // Adds synthesizer signals. 00063 temp = 0.0f; 00064 00065 for (int i = 0; i < inputs.size(); i++) 00066 { 00067 // Is this more efficient than reading the input? 00068 if (voices[i]-> aLive) { temp += voices[i]->SynthOutput->value; } 00069 } 00070 00071 Output1->write(temp * polyVoicesInv); 00072 } 00073 00074 00075 std::string Polyphony::getXML(int indent ) { 00076 00077 std::string S = Utils::space(indent) + "<Module ID=\""+Utils::intToString((int)this)+"\" >"+ Utils::newline(); 00078 S += Utils::space(indent+2) + "<TypeID>" + typeid(*this).name() + "</TypeID>" + Utils::newline(); 00079 00080 S += saveParameter("polyVoices",polyVoices, indent+2); 00081 00082 S += Output1->getXML("Output1", indent+2); 00083 00084 S += Utils::space(indent+2) + "<Inputs>" + Utils::newline(); 00085 for (int i = 0; i < inputs.size(); i++) 00086 { 00087 S += inputs[i]->getXML("Input", indent+4); 00088 } 00089 S += Utils::space(indent+2) + "</Inputs>" + Utils::newline(); 00090 00091 00092 S += saveID(indent+2); 00093 00094 S += Utils::space(indent) + "</Module>" + Utils::newline(); 00095 return S; 00096 } 00097 00098 void Polyphony::loadXML(XMLNode * n, bool firstPass) { 00099 cout << "Parsing Polyphony XML" << endl; 00100 00101 string TypeID = n->getTagText("TypeID") ; 00102 if (typeid(*this).name() != TypeID) { 00103 throw parseError("Trying to put "+TypeID+" into "+typeid(*this).name()); 00104 } 00105 00106 00107 00108 if (firstPass) { 00109 loadParameter("polyVoices",polyVoices, n); 00110 Output1->loadXML("Output1", n); 00111 } else { 00112 ; 00113 } 00114 00115 }; 00116 00117 00118 Module * Polyphony::newInstance(SynthVoice * S1) { 00119 return new Polyphony(); 00120 } 00121 00122 00123 00124 00125 }; // end of namespace: SynthCore Docs made by Doxygen. Email: Mikael Christensen |