rt300@0
|
1 //
|
rt300@0
|
2 // pdSynthWrapper.h
|
rt300@0
|
3 // tweakathlon
|
rt300@0
|
4 //
|
rt300@0
|
5 // Created by Robert Tubb on 14/01/2014.
|
rt300@0
|
6 //
|
rt300@0
|
7 //
|
rt300@0
|
8
|
rt300@0
|
9 #pragma once
|
rt300@0
|
10
|
rt300@0
|
11 #include "AppCore.h"
|
rt300@0
|
12 #include "eventLogger.h"
|
rt300@0
|
13 #include "ofxPd.h"
|
rt300@0
|
14 #include <string>
|
rt300@0
|
15 #include "boost/bind.hpp"
|
rt300@0
|
16 #include "boost/function.hpp"
|
rt300@0
|
17 #include "SynthParam.h"
|
rt300@1
|
18 #include "presetManager.h"
|
rt300@0
|
19
|
rt300@0
|
20 //---------------------------------------------------------------------
|
rt300@0
|
21 //---------------------------------------------------------------------
|
rt300@0
|
22 //---------------------------------------------------------------------
|
rt300@0
|
23
|
rt300@0
|
24 class PDSynthWrapper {
|
rt300@0
|
25 public:
|
rt300@0
|
26 void init(AppCore* aCore, string sp){
|
rt300@0
|
27 core = aCore;
|
rt300@0
|
28
|
rt300@0
|
29 synthPrefix = sp;
|
rt300@0
|
30 // init all the params, refer to synth in pd patch
|
rt300@0
|
31 timbreParams.push_back(SynthParam(64,aCore,"Pitch",sp));
|
rt300@0
|
32 timbreParams.push_back(SynthParam(64,aCore,"Pulse",sp));
|
rt300@0
|
33 timbreParams.push_back(SynthParam(64,aCore,"Attack",sp));
|
rt300@0
|
34 timbreParams.push_back(SynthParam(64,aCore,"Decay",sp));
|
rt300@0
|
35 timbreParams.push_back(SynthParam(64,aCore,"FiltTyp",sp));
|
rt300@0
|
36 timbreParams.push_back(SynthParam(64,aCore,"FiltFrq",sp));
|
rt300@2
|
37
|
rt300@2
|
38 timbreParams.push_back(SynthParam(64,aCore,"SPAM",sp));
|
rt300@2
|
39 timbreParams.push_back(SynthParam(64,aCore,"FILTH",sp));
|
rt300@0
|
40 //timbreParams.push_back(SynthParam(64,aCore,"reson",sp));
|
rt300@0
|
41
|
rt300@0
|
42 if (timbreParams.size() != TOTAL_NUM_PARAMS){
|
rt300@0
|
43 cout << "ERROR ERROR: WRONG NUM OF timbreParams or TOTAL_NUM_PARAMS" << endl;
|
rt300@0
|
44 }
|
rt300@0
|
45 cout << "initialised synth: " << sp << " with " << timbreParams.size() << " params" << endl;
|
rt300@0
|
46 };
|
rt300@0
|
47 void sendAllParams(){
|
rt300@0
|
48 std::vector<SynthParam>::const_iterator psp;
|
rt300@0
|
49 for(psp = timbreParams.begin(); psp < timbreParams.end(); psp++){
|
rt300@0
|
50 psp->sendToPD();
|
rt300@0
|
51 }
|
rt300@0
|
52 }
|
rt300@0
|
53 void sendVolume(int value){
|
rt300@0
|
54 List toPD;
|
rt300@0
|
55 toPD.addSymbol(synthPrefix);
|
rt300@0
|
56 toPD.addSymbol("Volume");
|
rt300@0
|
57 toPD.addFloat(value); // rounding here??
|
rt300@0
|
58
|
rt300@0
|
59 core->pd.sendList("fromOF", toPD);
|
rt300@0
|
60 }
|
rt300@0
|
61 void setNoteLength(int lms){
|
rt300@0
|
62 List toPD;
|
rt300@0
|
63 toPD.addSymbol(synthPrefix);
|
rt300@0
|
64 toPD.addSymbol("noteLength");
|
rt300@0
|
65 toPD.addFloat(lms); // volume here, just in case??
|
rt300@0
|
66
|
rt300@0
|
67 core->pd.sendList("fromOF", toPD);
|
rt300@0
|
68 }
|
rt300@0
|
69 const int getParamValueForID(int pid){
|
rt300@0
|
70 int v = 0;
|
rt300@0
|
71 std::vector<SynthParam>::const_iterator psp;
|
rt300@0
|
72 for(psp = timbreParams.begin(); psp < timbreParams.end(); psp++){
|
rt300@0
|
73 if (psp->getID() == pid){
|
rt300@0
|
74 v = psp->getValue();
|
rt300@0
|
75 return v;
|
rt300@0
|
76 }
|
rt300@0
|
77
|
rt300@0
|
78 }
|
rt300@0
|
79 cout << "ERROR ERROR getParamValueForID not found" << endl;
|
rt300@1
|
80 return -1;
|
rt300@0
|
81 }
|
rt300@0
|
82 const int getMappingIDForName(string name) const{
|
rt300@0
|
83
|
rt300@0
|
84 int rID = -1;
|
rt300@0
|
85 std::vector<SynthParam>::const_iterator psp;
|
rt300@0
|
86 for(psp = timbreParams.begin(); psp < timbreParams.end(); psp++){
|
rt300@0
|
87 if (psp->getName() == name){
|
rt300@0
|
88 rID = psp->getID();
|
rt300@0
|
89 return rID;
|
rt300@0
|
90 }
|
rt300@0
|
91
|
rt300@0
|
92 }
|
rt300@0
|
93 cout << "ERROR ERROR getMappingIDForName not found" << endl;
|
rt300@1
|
94 return -1;
|
rt300@0
|
95 };
|
rt300@0
|
96 const string getNameForMappingID(int pid) const{
|
rt300@0
|
97
|
rt300@0
|
98 string rname = "no name";
|
rt300@0
|
99 std::vector<SynthParam>::const_iterator psp;
|
rt300@0
|
100 for(psp = timbreParams.begin(); psp < timbreParams.end(); psp++){
|
rt300@0
|
101 if (psp->getID() == pid){
|
rt300@0
|
102 rname = psp->getName();
|
rt300@0
|
103 return rname;
|
rt300@0
|
104 }
|
rt300@0
|
105
|
rt300@0
|
106 }
|
rt300@0
|
107 cout << "ERROR ERROR getNameForMappingID not found" << endl;
|
rt300@1
|
108 return "error";
|
rt300@0
|
109 };
|
rt300@0
|
110 vector<int> getMappingIDForIndices(vector<int> idx){
|
rt300@0
|
111 vector<int> result;
|
rt300@0
|
112
|
rt300@0
|
113 for(int i = 0 ; i < idx.size(); i++){
|
rt300@0
|
114 if (idx[i] < timbreParams.size()){
|
rt300@0
|
115 int mapid = timbreParams[idx[i]].getID();
|
rt300@0
|
116
|
rt300@0
|
117 result.push_back(mapid);
|
rt300@0
|
118 //cout << " Map id for param no: " << idx[i] << " is " << *(result.end()-1);
|
rt300@0
|
119 }else{
|
rt300@0
|
120 cout << "ERROR ERROR: index bigger than num timbre params" << endl;
|
rt300@0
|
121
|
rt300@0
|
122 }
|
rt300@0
|
123 }
|
rt300@0
|
124 return result;
|
rt300@0
|
125 }
|
rt300@0
|
126
|
rt300@0
|
127 void paramChangeCallback(int mappingID, int value){
|
rt300@0
|
128
|
rt300@0
|
129 // look for id in params
|
rt300@0
|
130 std::vector<SynthParam>::iterator psp;
|
rt300@0
|
131 for(psp = timbreParams.begin(); psp < timbreParams.end(); psp++){
|
rt300@0
|
132 if ( psp->getID() == mappingID){
|
rt300@0
|
133 psp->setValue(value);
|
rt300@0
|
134 return;
|
rt300@0
|
135 }
|
rt300@0
|
136 }
|
rt300@0
|
137 cout << "ERROR ERROR: paramChangeCallback mappingID not found" << endl;
|
rt300@0
|
138 };
|
rt300@0
|
139
|
rt300@0
|
140 void trigger(){
|
rt300@0
|
141 // play the noise
|
rt300@0
|
142 List toPD;
|
rt300@0
|
143 toPD.addSymbol(synthPrefix);
|
rt300@0
|
144 toPD.addSymbol("playSound");
|
rt300@0
|
145 toPD.addFloat(1.0); // volume here, just in case??
|
rt300@0
|
146
|
rt300@0
|
147 core->pd.sendList("fromOF", toPD);
|
rt300@0
|
148
|
rt300@0
|
149 };
|
rt300@0
|
150
|
rt300@0
|
151 void randomiseParams(){
|
rt300@0
|
152
|
rt300@0
|
153 cout << " randomising" << endl;
|
rt300@0
|
154 std::vector<SynthParam>::iterator psp;
|
rt300@0
|
155 for(psp = timbreParams.begin(); psp < timbreParams.end(); psp++){
|
rt300@0
|
156
|
rt300@0
|
157 int value = ofRandom(0,127);
|
rt300@0
|
158 psp->setValue(value);
|
rt300@0
|
159
|
rt300@0
|
160 }
|
rt300@0
|
161
|
rt300@0
|
162 };
|
rt300@0
|
163
|
rt300@0
|
164 void setSameAsOtherSynth(const PDSynthWrapper* otherSynth){
|
rt300@0
|
165 // loop thru all params and set them to other synth
|
rt300@0
|
166 std::vector<SynthParam>::iterator psp;
|
rt300@0
|
167 for(psp = timbreParams.begin(); psp < timbreParams.end(); psp++){
|
rt300@0
|
168 psp->setValue(otherSynth->getParamValueFromName(psp->getName()));
|
rt300@0
|
169
|
rt300@0
|
170 }
|
rt300@0
|
171 };
|
rt300@0
|
172
|
rt300@0
|
173 vector<int> randomiseParamSubset(int howMany){
|
rt300@0
|
174 vector<int> randomisedIDs;
|
rt300@0
|
175 for (int i=0;i<howMany;i++){
|
rt300@0
|
176
|
rt300@0
|
177 int value = ofRandom(0,127);
|
rt300@0
|
178 timbreParams[i].setValue(value);
|
rt300@0
|
179 randomisedIDs.push_back(i);
|
rt300@0
|
180
|
rt300@0
|
181 }
|
rt300@0
|
182 return randomisedIDs;
|
rt300@0
|
183
|
rt300@0
|
184 };
|
rt300@0
|
185
|
rt300@0
|
186 const int getParamValueFromName(string name) const{
|
rt300@0
|
187
|
rt300@0
|
188 int value = -1;
|
rt300@0
|
189 std::vector<SynthParam>::const_iterator psp;
|
rt300@0
|
190 for(psp = timbreParams.begin(); psp < timbreParams.end(); psp++){
|
rt300@0
|
191 if (psp->getName() == name){
|
rt300@0
|
192 value = psp->getValue();
|
rt300@0
|
193 return value;
|
rt300@0
|
194 }
|
rt300@0
|
195
|
rt300@0
|
196 }
|
rt300@0
|
197 cout << "ERROR ERROR: getParamValueFromName name not found" << endl;
|
rt300@1
|
198 return -1;
|
rt300@0
|
199 };
|
rt300@0
|
200
|
rt300@0
|
201 const int getNumParams(){
|
rt300@0
|
202 return timbreParams.size();
|
rt300@0
|
203 };
|
rt300@0
|
204
|
rt300@0
|
205 void setAllParams(vector<int> params){
|
rt300@0
|
206 if(params.size() != timbreParams.size()){
|
rt300@0
|
207 cout << "Error not right number of params in set in synth" << endl;
|
rt300@0
|
208 return;
|
rt300@0
|
209 }
|
rt300@0
|
210 std::vector<SynthParam>::iterator psp;
|
rt300@0
|
211 int i=0;
|
rt300@0
|
212 for(psp = timbreParams.begin(); psp < timbreParams.end(); psp++){
|
rt300@0
|
213 psp->setValue(params[i]);
|
rt300@0
|
214 i++;
|
rt300@0
|
215 }
|
rt300@0
|
216
|
rt300@0
|
217
|
rt300@0
|
218 }
|
rt300@0
|
219 vector<int> getAllParamValues(){
|
rt300@0
|
220 vector<int> pl;
|
rt300@0
|
221 std::vector<SynthParam>::const_iterator psp;
|
rt300@0
|
222 for(psp = timbreParams.begin(); psp < timbreParams.end(); psp++){
|
rt300@0
|
223 pl.push_back(psp->getValue());
|
rt300@0
|
224 }
|
rt300@0
|
225 return pl;
|
rt300@0
|
226 }
|
rt300@1
|
227
|
rt300@1
|
228 void storeParamsAsPreset(string name){
|
rt300@1
|
229 presetManager.savePreset(name, getAllParamValues());
|
rt300@1
|
230 }
|
rt300@0
|
231 private:
|
rt300@0
|
232 string synthPrefix;
|
rt300@0
|
233 AppCore* core;
|
rt300@1
|
234 PresetManager presetManager; // TODO not here
|
rt300@0
|
235 vector<SynthParam> timbreParams; // array of everything in synth
|
rt300@0
|
236
|
rt300@0
|
237 };
|
rt300@0
|
238
|
rt300@0
|
239 //---------------------------------------------------------------------
|
rt300@0
|
240 //--------------------------------------------------------------------- |