3
Copyright (C) 1999 Emmeran Seehuber
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24
16.09.1999 Emmeran "Emmy" Seehuber <the_emmy@gmx.de>
25
- Implementeted mapping of channels and pitches.
26
- Reworked option parsing, now using getopt().
27
Note: The paramters of the programms have changed !
31
** This program was in original by David G. Slomin.
32
** It was released to the Public Domain on 1/25/99.
40
#include <sys/types.h>
46
int input_fd = -1, test = 0, verbose = 0;
51
void usage(char *prog)
54
fprintf(stderr,"Usage: %s [ -f <mididevice> ] [ -m <mapfile> ] [ -v ] [ -t <loop> ]\n",prog);
55
fprintf(stderr," -f the mididevice to read the input from.\n");
56
fprintf(stderr," Default is /dev/midi. If you specify a dash, it is stdin\n");
57
fprintf(stderr," -m the mapfile to load.\n");
58
fprintf(stderr," -v verbose output.\n");
59
fprintf(stderr," -t test mode. Generates a testoutput on the midibus\n");
60
fprintf(stderr," -l long test mode. Generates a testoutput on the midibus\n");
64
void parseArgs(int argc, char** argv)
67
strcpy(cFileName,"/dev/midi");
69
while((optch = getopt(argc,argv,"m:f:vtl")) > 0)
73
case 'm': if( !Map.readMap(optarg) )
74
fprintf(stderr,"%s: can't read file %s!\n",argv[0],optarg);
80
case 'v': verbose = 1; printf("MidiSend %s\n", VERSION );
82
case 'f': strncpy(cFileName,optarg,1024);
84
default: usage(argv[0]);
92
int midisend_main(int argc, char *argv[])
94
int main(int argc, char *argv[])
97
Arts::Dispatcher dispatcher;
98
Arts::MidiManager manager = Arts::Reference("global:Arts_MidiManager");
100
if (manager.isNull())
102
fprintf(stderr, "%s trouble: No midimanager object found; please start "
108
** MIDI input initialization.
111
parseArgs(argc,argv);
113
string title = string("midisend (") + cFileName +")";
114
Arts::MidiClient client
115
= manager.addClient(Arts::mcdPlay,Arts::mctApplication,title,"midisend");
116
Arts::MidiPort port = client.addOutputPort();
121
printf("performing test ...\n");
122
unsigned long i,max=5000;
123
if(test==2) max = 20000;
127
Arts::MidiCommand(Arts::mcsNoteOn, 60+(i%12), 100));
129
Arts::MidiCommand(Arts::mcsNoteOff,60+(i%12), 0));
135
printf("trying to open %s ...", cFileName );
137
input_fd = open(cFileName,O_RDONLY);
141
fprintf(stderr,"\n%s trouble: can't open input device!\n",argv[0]);
153
printf("beginning loop ...\n");
155
unsigned char msg[3];
159
midimsgRead(input_fd, msg);
160
switch (midimsgGetMessageType(msg))
162
case MIDIMSG_NOTE_OFF:
165
Arts::MidiCommand(Arts::mcsNoteOff|midimsgGetChannel(msg),
166
midimsgGetPitch(msg), midimsgGetVelocity(msg)));
168
printf("NoteOff: Channel %d, Pitch %3d\n", midimsgGetChannel(msg),midimsgGetPitch(msg));
170
case MIDIMSG_NOTE_ON:
173
Arts::MidiCommand(Arts::mcsNoteOn|midimsgGetChannel(msg),
174
midimsgGetPitch(msg), midimsgGetVelocity(msg)));
176
printf("NoteOn : Channel %d, Pitch %3d, Velocity %2d\n", midimsgGetChannel(msg),
177
midimsgGetPitch(msg),midimsgGetVelocity(msg));
179
case MIDIMSG_PITCH_WHEEL:
182
Arts::MidiCommand(Arts::mcsPitchWheel|midimsgGetChannel(msg),
183
midimsgGetPitch(msg), midimsgGetVelocity(msg)));
185
printf("PitchWheel : Channel %d, LSB %3d MSB %3d\n", midimsgGetChannel(msg),
186
midimsgGetPitch(msg),midimsgGetVelocity(msg));
192
//--------------------------------------------
194
//--------------------------------------------
196
bool CMidiMap::readMap(const char* pszFileName)
199
printf("reading mapfile %s ...\n", pszFileName);
201
FILE *file = fopen(pszFileName,"r");
205
char cBuffer[1024+1];
208
while( (pszLine = fgets(cBuffer,1024,file)) ) {
210
parseLine(pszLine,pszFileName,nLine);
217
bool CMidiMap::getNextWord(char*& pszLine, char*& pszWord)
219
// First skip all leading blanks, etc.
222
char cHelp = *pszLine;
225
case 0: return false; // Out of line
229
case '\t': pszLine++; break; // Goto next char
230
default: bCont = false; break; // NonSpace charater -> Word begins here.
234
// The word starts here
237
// And now, goto the end of the word.
240
char cHelp = *pszLine;
249
case '\t': *pszLine++ = 0; bCont = false; break; // Goto next char
250
default: pszLine++; break;
257
void CMidiMap::parseLine(char* pszLine, const char* pszConfigFile, int nConfigLine )
262
// Get first word of the line
263
bOk = bOk && getNextWord(pszLine,pszWord);
268
if( *pszWord == '#' )
271
if( strcmp(pszWord,"PRC") == 0 ) {
272
bOk = bOk && getNextWord(pszLine,pszWord);
273
int nOrigChannel = atol(pszWord);
274
bOk = bOk && getNextWord(pszLine,pszWord);
275
int nStart = atol(pszWord);
276
bOk = bOk && getNextWord(pszLine,pszWord);
277
int nEnd = atol(pszWord);
278
bOk = bOk && getNextWord(pszLine,pszWord);
279
int nChannel = atol(pszWord);
281
channelMaps[nOrigChannel].nChannel = nOrigChannel;
282
for( int i = nStart; i <= nEnd; i++ ) {
283
channelMaps[nOrigChannel].channelRemaps[i].nPitch = i;
284
channelMaps[nOrigChannel].channelRemaps[i].nChannel = nChannel;
288
printf("midisend: (PRC) missing parameters at %s:%d\n", pszConfigFile,nConfigLine);
293
if( strcmp(pszWord,"PRD") == 0 ) {
294
bOk = bOk && getNextWord(pszLine,pszWord);
295
int nOrigChannel = atol(pszWord);
296
bOk = bOk && getNextWord(pszLine,pszWord);
297
int nStart = atol(pszWord);
298
bOk = bOk && getNextWord(pszLine,pszWord);
299
int nEnd = atol(pszWord);
300
bOk = bOk && getNextWord(pszLine,pszWord);
301
int nPitchDiff = atol(pszWord);
303
channelMaps[nOrigChannel].nChannel = nOrigChannel;
304
for( int i = nStart; i <= nEnd; i++ ) {
305
channelMaps[nOrigChannel].pitchRemaps[i].nPitch = i;
306
channelMaps[nOrigChannel].pitchRemaps[i].nToPitch = i + nPitchDiff;
310
printf("midisend: (PRD) missing parameters at %s:%d\n", pszConfigFile,nConfigLine);
315
if( strcmp(pszWord,"PTC") == 0 ) {
316
bOk = bOk && getNextWord(pszLine,pszWord);
317
int nOrigChannel = atol(pszWord);
318
bOk = bOk && getNextWord(pszLine,pszWord);
319
int nPitch = atol(pszWord);
320
bOk = bOk && getNextWord(pszLine,pszWord);
321
int nToChannel = atol(pszWord);
323
channelMaps[nOrigChannel].nChannel = nOrigChannel;
324
channelMaps[nOrigChannel].channelRemaps[nPitch].nPitch = nPitch;
325
channelMaps[nOrigChannel].channelRemaps[nPitch].nChannel = nToChannel;
328
printf("midisend: (PTC) missing parameters at %s:%d\n", pszConfigFile,nConfigLine);
333
if( strcmp(pszWord,"PTP") == 0 ) {
334
bOk = bOk && getNextWord(pszLine,pszWord);
335
int nOrigChannel = atol(pszWord);
336
bOk = bOk && getNextWord(pszLine,pszWord);
337
int nPitch = atol(pszWord);
338
bOk = bOk && getNextWord(pszLine,pszWord);
339
int nToPitch = atol(pszWord);
341
channelMaps[nOrigChannel].nChannel = nOrigChannel;
342
channelMaps[nOrigChannel].pitchRemaps[nPitch].nPitch = nPitch;
343
channelMaps[nOrigChannel].pitchRemaps[nPitch].nToPitch = nToPitch;
346
printf("midisend: (PTP) missing parameters at %s:%d\n", pszConfigFile,nConfigLine);
351
printf("midisend: Unknown command at %s:%d\n", pszConfigFile,nConfigLine);
354
void CMidiMap::mapMsg(Byte* msg)
356
// Get out the data for mapping
357
int nChannel = midimsgGetChannel(msg);
358
int nPitch = midimsgGetPitch(msg);
360
// Is there something to map for this channel ?
361
if( channelMaps.find(nChannel) != channelMaps.end() ) {
362
// => Yes, than do it.
365
if( channelMaps[nChannel].channelRemaps.find(nPitch) != channelMaps[nChannel].channelRemaps.end() )
366
midimsgSetChannel(msg,channelMaps[nChannel].channelRemaps[nPitch].nChannel);
369
if( channelMaps[nChannel].pitchRemaps.find(nPitch) != channelMaps[nChannel].pitchRemaps.end() ) {
370
midimsgSetPitch(msg,channelMaps[nChannel].pitchRemaps[nPitch].nToPitch);