26 #include "midimapper.h"
40 mapExpressionToVolumeEvents=0;
41 if ((name==NULL)||(name[0]==0))
48 channelPatchForced[i]=-1;
50 for (i=0;i<128;i++) patchmap[i]=i;
58 if (_filename) free(_filename);
62 void MidiMapper::deallocateMaps(
void)
65 for (i=0;i<16;i++) channelKeymap[i]=NULL;
66 for (i=0;i<128;i++) patchKeymap[i]=NULL;
76 void MidiMapper::getValue(
char *s,
char *v)
79 while ((*c!=0)&&(*c!=
'=')) c++;
93 void MidiMapper::removeSpaces(
char *s)
96 while ((*a!=0)&&(*a==
' ')) a++;
97 if (*a==0) {*s=0;
return;};
100 while ((*a!=0)&&(*a!=
' ')&&(*a!=10)&&(*a!=13))
106 while ((*a!=0)&&((*a==
' ')||(*a==10)||(*a==13))) a++;
108 if (*a==0) {*s=0;
return;};
114 int MidiMapper::countWords(
char *s)
125 void MidiMapper::getWord(
char *t,
char *s,
int w)
129 while ((*s!=0)&&(i<w))
134 while ((*s!=0)&&(*s!=
' ')&&(*s!=10)&&(*s!=13))
146 FILE *fh = fopen(name,
"rt");
147 if ( fh == NULL ) { _ok = -1;
return; };
150 if ( _filename != NULL ) free(_filename);
151 _filename = strdup(name);
152 #ifdef MIDIMAPPERDEBUG
153 printf(
"Loading mapper ...\n");
158 while ((!feof(fh))&&((s[0]==0)||(s[0]==
'#'))) fgets(s,100,fh);
159 if (strncmp(s,
"DEFINE",6)==0)
161 if (strncmp(&s[7],
"PATCHMAP",8)==0) readPatchmap(fh);
163 if (strncmp(&s[7],
"KEYMAP",6)==0) readKeymap(fh,s);
165 if (strncmp(&s[7],
"CHANNELMAP",10)==0) readChannelmap(fh);
168 printf(
"ERROR: Unknown DEFINE line in map file\n");
173 printf(
"The midi map file will be ignored\n");
178 else if (strncmp(s,
"OPTIONS",7)==0) readOptions(fh);
183 MidiMapper::Keymap *MidiMapper::createKeymap(
char *name,uchar use_same_note,uchar note)
185 Keymap *km=
new Keymap;
186 strncpy(km->name, name, KM_NAME_SIZE);
187 km->name[KM_NAME_SIZE - 1] = 0;
190 if (use_same_note==1)
204 void MidiMapper::addKeymap(Keymap *newkm)
213 while (km->next!=NULL) km=km->next;
219 MidiMapper::Keymap *MidiMapper::keymap(
char *n)
222 while ((km!=NULL)&&(strcmp(km->name,n)!=0)) km=km->next;
226 void MidiMapper::readOptions(FILE *fh)
228 #ifdef MIDIMAPPERDEBUG
229 printf(
"Loading Options ... \n");
239 while ((s[0]==0)||(s[0]==
'#')) fgets(s,100,fh);
240 if (strncmp(s,
"PitchBenderRatio",16)==0)
246 pitchBenderRatio=atoi(t);
248 else if (strncmp(s,
"MapExpressionToVolumeEvents",27)==0) mapExpressionToVolumeEvents=1;
249 else if (strncmp(s,
"END",3)==0)
255 printf(
"ERROR: Invalid option in OPTIONS section of map file : (%s)\n",s);
262 void MidiMapper::readPatchmap(FILE *fh)
270 #ifdef MIDIMAPPERDEBUG
271 printf(
"Loading Patch map ... \n");
276 while ((s[0]==0)||(s[0]==
'#')) fgets(s,100,fh);
286 if (strcmp(t,
"AllKeysTo")==0)
291 printf(
"ERROR: Invalid option in PATCHMAP section of map file\n");
296 sprintf(name,
"AllKeysTo%s",t);
297 patchKeymap[i]=createKeymap(name,1,atoi(t));
308 while ((s[0]==0)||(s[0]==
'#')||(s[0]==10)||(s[0]==13)) fgets(s,100,fh);
309 if (strncmp(s,
"END",3)!=0)
311 printf(
"ERROR: End of section not found in map file\n");
317 void MidiMapper::readKeymap(FILE *fh,
char *first_line)
321 #ifdef MIDIMAPPERDEBUG
322 printf(
"Loading Key map ... %s",first_line);
324 removeSpaces(first_line);
325 getWord(v,first_line,2);
326 Keymap *km=
new Keymap;
327 strncpy(km->name, v, KM_NAME_SIZE);
328 km->name[KM_NAME_SIZE - 1] = 0;
334 while ((s[0]==0)||(s[0]==
'#')) fgets(s,100,fh);
341 while ((s[0]==0)||(s[0]==
'#')||(s[0]==10)||(s[0]==13)) fgets(s,100,fh);
342 if (strncmp(s,
"END",3)!=0)
344 printf(
"ERROR: End of section not found in map file\n");
351 void MidiMapper::readChannelmap(FILE *fh)
358 #ifdef MIDIMAPPERDEBUG
359 printf(
"Loading Channel map ... \n");
364 while ((s[0]==0)||(s[0]==
'#')) fgets(s,100,fh);
369 channelKeymap[i]=NULL;
370 channelPatchForced[i]=-1;
375 if (strcmp(t,
"Keymap")==0)
380 printf(
"ERROR: Invalid option in CHANNELMAP section of map file\n");
385 channelKeymap[i]=keymap(t);
387 else if (strcmp(t,
"ForcePatch")==0)
392 printf(
"ERROR: Invalid option in CHANNELMAP section of map file\n");
397 channelPatchForced[i]=atoi(t);
401 channelmap[i]=atoi(t);
408 while ((s[0]==0)||(s[0]==
'#')||(s[0]==10)||(s[0]==13)) fgets(s,100,fh);
409 if (strncmp(s,
"END",3)!=0)
411 printf(
"END of section not found in map file\n");
420 return (_filename)? _filename :
"";
425 uchar notemapped=note;
426 if (patchKeymap[pgm]!=NULL) notemapped=patchKeymap[pgm]->key[note];
427 if (channelKeymap[chn]!=NULL) notemapped=channelKeymap[chn]->key[note];
433 return (channelPatchForced[chn] == -1) ?
434 patchmap[pgm] : (uchar)channelPatchForced[chn] ;
441 short pbs=((short)msb<<7) | (lsb & 0x7F);
443 short pbs2=(((long)pbs*pitchBenderRatio)/4096);
444 #ifdef MIDIMAPPERDEBUG
445 printf(
"Pitch Bender (%d): %d -> %d \n",chn,pbs,pbs2);
449 msb=(pbs2 >> 7)&0x7F;
455 if ((mapExpressionToVolumeEvents)&&(ctl==11)) ctl=7;
const char * filename(void)
Returns the path and name of the file which the object loaded the mapper from.
void loadFile(const char *name)
Loads a MIDI Mapper definition file (you don't need to use this if you used a correct filename in con...
void controller(uchar chn, uchar &ctl, uchar &v)
Returns the value which a given controller and its value should be mapped to when played on channel c...
MidiMapper(const char *name)
Constructor.
uchar patch(uchar chn, uchar pgm)
Returns the patch which pgm used on channel chn should be mapped to.
void pitchBender(uchar chn, uchar &lsb, uchar &msb)
Returns the value which the pitch bender on channel chn should be mapped to.
uchar key(uchar chn, uchar pgm, uchar note)
Returns the key that key note playing a pgm patch on channel chn should be mapped to...