Debian Codegraph @

src/gui/midiMap.cpp

raw
1 #include "gui.h"
2 #include "guiConst.h"
3 #include "../ta-log.h"
4
5 int MIDIMap::at(const TAMidiMessage& where) {
6 if (map==NULL) return 0;
7 int type=(where.type>>4)-8;
8 int chan=where.type&15;
9 int data1=where.data[0];
10 int data2=where.data[1];
11 if (type<0) return 0;
12 if (map[type]==NULL) return 0;
13
14 int** chanMap=map[type][chan];
15 if (chanMap==NULL) {
16 chanMap=map[type][16];
17 if (chanMap==NULL) return 0;
18 }
19
20 int* dataMap=chanMap[data1];
21 if (dataMap==NULL) {
22 dataMap=chanMap[128];
23 if (dataMap==NULL) return 0;
24 }
25
26 int ret=dataMap[data2];
27 if (ret==0) {
28 ret=dataMap[128];
29 if (ret==0) { // maybe this is not the correct mapping
30 dataMap=chanMap[128];
31 if (dataMap==NULL) {
32 chanMap=map[type][16];
33 if (chanMap==NULL) return 0;
34
35 dataMap=chanMap[data1];
36 if (dataMap==NULL) {
37 dataMap=chanMap[128];
38 if (dataMap==NULL) return 0;
39 }
40
41 ret=dataMap[data2];
42 if (ret==0) {
43 ret=dataMap[128];
44 }
45 } else {
46 ret=dataMap[data2];
47 if (ret==0) {
48 ret=dataMap[128];
49 }
50 }
51 }
52 }
53
54 return ret;
55 }
56
57 #define UNDERSTAND_OPTION(x) if (optionNameS==#x) { \
58 x=std::stoi(optionValueS); \
59 }
60
61 #define UNDERSTAND_FLOAT_OPTION(x) if (optionNameS==#x) { \
62 x=std::stof(optionValueS); \
63 }
64
65 #define UNDERSTAND_ARRAY_OPTION(x,yMax) if (optionNameS==#x) { \
66 if (optionIndex<0 || optionIndex>=yMax) { \
67 logW("MIDI map array option %d out of range (0-%d) at line %d: %s",optionIndex,yMax,curLine,line); \
68 break; \
69 } \
70 x[optionIndex]=std::stoi(optionValueS); \
71 }
72
73 bool MIDIMap::read(String path) {
74 char line[4096];
75 int curLine=1;
76 FILE* f=fopen(path.c_str(),"rb");
77 if (f==NULL) {
78 if (errno!=ENOENT) {
79 logE("error while loading MIDI mapping! %s",strerror(errno));
80 }
81 return false;
82 }
83
84 binds.clear();
85 while (fgets(line,4096,f)) {
86 char* nlPos=strrchr(line,'\n');
87 if (nlPos!=NULL) *nlPos=0;
88 if (strstr(line,"aOption")==line) {
89 char optionName[256];
90 int optionIndex=-1;
91 char optionValue[256];
92 String optionNameS, optionValueS;
93
94 int result=sscanf(line,"aOption %255s %d %255s",optionName,&optionIndex,optionValue);
95 if (result!=3) {
96 logW("MIDI map garbage data at line %d: %s",curLine,line);
97 break;
98 }
99
100 optionNameS=optionName;
101 optionValueS=optionValue;
102
103 try {
104 UNDERSTAND_ARRAY_OPTION(valueInputSpecificStyle,18) else
105 UNDERSTAND_ARRAY_OPTION(valueInputSpecificMSB,18) else
106 UNDERSTAND_ARRAY_OPTION(valueInputSpecificLSB,18) else
107 UNDERSTAND_ARRAY_OPTION(valueInputSpecificSingle,18) else {
108 logW("MIDI map unknown array option %s at line %d: %s",optionName,curLine,line);
109 }
110 } catch (std::out_of_range& e) {
111 logW("MIDI map invalid value %s for array option %s at line %d: %s",optionValue,optionName,curLine,line);
112 } catch (std::invalid_argument& e) {
113 logW("MIDI map invalid value %s for array option %s at line %d: %s",optionValue,optionName,curLine,line);
114 }
115
116 curLine++;
117 continue;
118 }
119 if (strstr(line,"option")==line) {
120 char optionName[256];
121 char optionValue[256];
122 String optionNameS, optionValueS;
123 int result=sscanf(line,"option %255s %255s",optionName,optionValue);
124 if (result!=2) {
125 logW("MIDI map garbage data at line %d: %s",curLine,line);
126 break;
127 }
128
129 optionNameS=optionName;
130 optionValueS=optionValue;
131
132 try {
133 UNDERSTAND_OPTION(noteInput) else
134 UNDERSTAND_OPTION(volInput) else
135 UNDERSTAND_OPTION(rawVolume) else
136 UNDERSTAND_OPTION(polyInput) else
137 UNDERSTAND_OPTION(directChannel) else
138 UNDERSTAND_OPTION(programChange) else
139 UNDERSTAND_OPTION(midiClock) else
140 UNDERSTAND_OPTION(midiTimeCode) else
141 UNDERSTAND_OPTION(yamahaFMResponse) else
142 UNDERSTAND_OPTION(directProgram) else
143 UNDERSTAND_OPTION(valueInputStyle) else
144 UNDERSTAND_OPTION(valueInputControlMSB) else
145 UNDERSTAND_OPTION(valueInputControlLSB) else
146 UNDERSTAND_OPTION(valueInputControlSingle) else
147 UNDERSTAND_FLOAT_OPTION(volExp) else {
148 logW("MIDI map unknown option %s at line %d: %s",optionName,curLine,line);
149 }
150 } catch (std::out_of_range& e) {
151 logW("MIDI map invalid value %s for option %s at line %d: %s",optionValue,optionName,curLine,line);
152 } catch (std::invalid_argument& e) {
153 logW("MIDI map invalid value %s for option %s at line %d: %s",optionValue,optionName,curLine,line);
154 }
155
156 curLine++;
157 continue;
158 }
159
160 char bindAction[256];
161 MIDIBind bind;
162 int result=sscanf(line,"%d %d %d %d %255s",&bind.type,&bind.channel,&bind.data1,&bind.data2,bindAction);
163 if (result!=5 || result==EOF) {
164 logW("MIDI map garbage data at line %d: %s",curLine,line);
165 break;
166 }
167
168 bool foundAction=false;
169 for (int i=0; i<GUI_ACTION_MAX; i++) {
170 if (strcmp(bindAction,guiActions[i].name)==0) {
171 bind.action=i;
172 foundAction=true;
173 break;
174 }
175 }
176 if (!foundAction) {
177 logW("MIDI map unknown action %s at line %d: %s",bindAction,curLine,line);
178 break;
179 }
180
181 binds.push_back(bind);
182 curLine++;
183 }
184
185 fclose(f);
186 return true;
187 }
188
189 #define WRITE_OPTION(x) fprintf(f,"option " #x " %d\n",x);
190 #define WRITE_FLOAT_OPTION(x) fprintf(f,"option " #x " %f\n",x);
191 #define WRITE_ARRAY_OPTION(x,y) fprintf(f,"aOption " #x " %d %d\n",y,x[y]);
192
193 bool MIDIMap::write(String path) {
194 FILE* f=fopen(path.c_str(),"wb");
195 if (f==NULL) {
196 logE("error while saving MIDI mapping! %s",strerror(errno));
197 return false;
198 }
199
200 WRITE_OPTION(noteInput);
201 WRITE_OPTION(volInput);
202 WRITE_OPTION(rawVolume);
203 WRITE_OPTION(polyInput);
204 WRITE_OPTION(directChannel);
205 WRITE_OPTION(programChange);
206 WRITE_OPTION(midiClock);
207 WRITE_OPTION(midiTimeCode);
208 WRITE_OPTION(yamahaFMResponse);
209 WRITE_OPTION(directProgram);
210 WRITE_OPTION(valueInputStyle);
211 WRITE_OPTION(valueInputControlMSB);
212 WRITE_OPTION(valueInputControlLSB);
213 WRITE_OPTION(valueInputControlSingle);
214 WRITE_FLOAT_OPTION(volExp);
215
216 for (int i=0; i<18; i++) {
217 WRITE_ARRAY_OPTION(valueInputSpecificStyle,i);
218 WRITE_ARRAY_OPTION(valueInputSpecificMSB,i);
219 WRITE_ARRAY_OPTION(valueInputSpecificLSB,i);
220 WRITE_ARRAY_OPTION(valueInputSpecificSingle,i);
221 }
222
223 for (MIDIBind& i: binds) {
224 if (fprintf(f,"%d %d %d %d %s\n",i.type,i.channel,i.data1,i.data2,guiActions[i.action].name)<0) {
225 logW("did not write MIDI mapping entirely! %s",strerror(errno));
226 break;
227 }
228 }
229
230 fclose(f);
231 return true;
232 }
233
234 void MIDIMap::deinit() {
235 if (map!=NULL) {
236 for (int i=0; i<8; i++) {
237 if (map[i]!=NULL) {
238 for (int j=0; j<17; j++) {
239 if (map[i][j]!=NULL) {
240 for (int k=0; k<129; k++) {
241 if (map[i][j][k]!=NULL) {
242 delete[] map[i][j][k];
243 }
244 }
245 delete[] map[i][j];
246 }
247 }
248 delete[] map[i];
249 }
250 }
251 delete[] map;
252 map=NULL;
253 }
254 }
255
256 void MIDIMap::compile() {
257 deinit();
258
259 map=new int***[8];
260 memset(map,0,sizeof(int***)*8);
261 for (MIDIBind& i: binds) {
262 if (i.type<8 || i.type>15) continue;
263 if (i.channel<0 || i.channel>16) continue;
264 if (i.data1<0 || i.data1>128) continue;
265 if (i.data2<0 || i.data2>128) continue;
266
267 if (map[i.type-8]==NULL) {
268 map[i.type-8]=new int**[17];
269 memset(map[i.type-8],0,sizeof(int**)*17);
270 }
271 if (map[i.type-8][i.channel]==NULL) {
272 map[i.type-8][i.channel]=new int*[129];
273 memset(map[i.type-8][i.channel],0,sizeof(int*)*129);
274 }
275 if (map[i.type-8][i.channel][i.data1]==NULL) {
276 map[i.type-8][i.channel][i.data1]=new int[129];
277 memset(map[i.type-8][i.channel][i.data1],0,sizeof(int)*129);
278 }
279
280 map[i.type-8][i.channel][i.data1][i.data2]=i.action;
281 logD("MIDI mapping %d %d %d %d to %d",i.type-8,i.channel,i.data1,i.data2,i.action);
282 }
283 }