summaryrefslogtreecommitdiffstats
path: root/arts/midi/midisend.cc
blob: 2b976d61bdcf4969ce2121f0fd1c6a95227e7fe7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*

Copyright (C) 1999 Emmeran Seehuber
                   [email protected]

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/

/*
  Changes:
  16.09.1999 Emmeran "Emmy" Seehuber <[email protected]>
    - Implementeted mapping of channels and pitches.
    - Reworked option parsing, now using getopt(). 
      Note: The parameters of the programms have changed !
*/

/*
** This program was in original by David G. Slomin.
** It was released to the Public Domain on 1/25/99.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "midisend.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

using namespace std;

int input_fd = -1, test = 0, verbose = 0;
char cFileName[1025];
int optch;
CMidiMap Map;

void usage(char *prog)
{
	fprintf(stderr,"\n");
	fprintf(stderr,"Usage: %s [ -f <mididevice> ] [ -m <mapfile> ] [ -v ] [ -t <loop> ]\n",prog);
	fprintf(stderr,"   -f   the mididevice to read the input from.\n");
	fprintf(stderr,"        Default is /dev/midi. If you specify a dash, it is stdin\n");
  fprintf(stderr,"   -m   the mapfile to load.\n");
  fprintf(stderr,"   -v   verbose output.\n");
  fprintf(stderr,"   -t   test mode. Generates a testoutput on the midibus\n");
  fprintf(stderr,"   -l   long test mode. Generates a testoutput on the midibus\n");
	exit(1);
}

void parseArgs(int argc, char** argv)
{
  // Setup default
  strcpy(cFileName,"/dev/midi");

	while((optch = getopt(argc,argv,"m:f:vtl")) > 0)
	{
		switch(optch)
		{
			case 'm': if( !Map.readMap(optarg) ) 
                  fprintf(stderr,"%s: can't read file %s!\n",argv[0],optarg);
				break;
			case 't': test = 1;
				break;
			case 'l': test = 2;
				break;
#ifdef VERSION
			case 'v': verbose = 1; printf("MidiSend %s\n", VERSION );
				break;
#endif
			case 'f': strncpy(cFileName,optarg,1024);
				break;
			default: usage(argv[0]);
				break;
		}
	}
}


#ifdef COMMON_BINARY
int midisend_main(int argc, char *argv[])
#else
int main(int argc, char *argv[])
#endif
{
   Arts::Dispatcher dispatcher;
   Arts::MidiManager manager = Arts::Reference("global:Arts_MidiManager");

   if (manager.isNull())
   {
      fprintf(stderr, "%s trouble: No midimanager object found; please start "
         			  "artsd.\n",argv[0]);
      exit(EXIT_FAILURE);
   }

   /*
   ** MIDI input initialization.
   */

   parseArgs(argc,argv);

   string title = string("midisend (") + cFileName +")";
   Arts::MidiClient client
       = manager.addClient(Arts::mcdPlay,Arts::mctApplication,title,"midisend");
   Arts::MidiPort port = client.addOutputPort();

   if(test)
   {
      if( verbose )
        printf("performing test ...\n");
      unsigned long i,max=5000;
      if(test==2) max = 20000;
      for(i=0;i<max;i++)
      {
         port.processCommand(
             Arts::MidiCommand(Arts::mcsNoteOn, 60+(i%12), 100));
         port.processCommand(
             Arts::MidiCommand(Arts::mcsNoteOff,60+(i%12), 0));
      }
      exit(0);
   }

   if( verbose )
     printf("trying to open %s ...", cFileName );

   input_fd = open(cFileName,O_RDONLY);

   if(input_fd == -1)
   {
      fprintf(stderr,"\n%s trouble: can't open input device!\n",argv[0]);
      exit(EXIT_FAILURE);
   }
   else if( verbose )
     printf(" ok!\n");


   /*
   ** Main loop.
   */

   if(verbose)
     printf("beginning loop ...\n");

   unsigned char msg[3];

   while (1)
   {
      midimsgRead(input_fd, msg);
      switch (midimsgGetMessageType(msg))
      {
         case MIDIMSG_NOTE_OFF:
            Map.mapMsg(msg);
            port.processCommand(
				Arts::MidiCommand(Arts::mcsNoteOff|midimsgGetChannel(msg),
								midimsgGetPitch(msg), midimsgGetVelocity(msg)));
            if( verbose ) 
              printf("NoteOff: Channel %d, Pitch %3d\n", midimsgGetChannel(msg),midimsgGetPitch(msg));
            break;
         case MIDIMSG_NOTE_ON:
            Map.mapMsg(msg);
            port.processCommand(
				Arts::MidiCommand(Arts::mcsNoteOn|midimsgGetChannel(msg),
								midimsgGetPitch(msg), midimsgGetVelocity(msg)));
            if( verbose )
              printf("NoteOn : Channel %d, Pitch %3d, Velocity %2d\n", midimsgGetChannel(msg),
                     midimsgGetPitch(msg),midimsgGetVelocity(msg));
            break;
		case MIDIMSG_PITCH_WHEEL:
            Map.mapMsg(msg);
            port.processCommand(
				Arts::MidiCommand(Arts::mcsPitchWheel|midimsgGetChannel(msg),
								midimsgGetPitch(msg), midimsgGetVelocity(msg)));
            if( verbose )
              printf("PitchWheel : Channel %d, LSB %3d MSB %3d\n", midimsgGetChannel(msg),
                     midimsgGetPitch(msg),midimsgGetVelocity(msg));
 			break;
      }
   }
}

//--------------------------------------------
// The mapping stuff
//--------------------------------------------

bool CMidiMap::readMap(const char* pszFileName)
{
  if( verbose )
    printf("reading mapfile %s ...\n", pszFileName);

  FILE *file = fopen(pszFileName,"r");
  if( !file )
     return false;

  char cBuffer[1024+1];
  char* pszLine;
  int nLine = 0;
  while( (pszLine = fgets(cBuffer,1024,file)) ) {
    nLine++;
    parseLine(pszLine,pszFileName,nLine);
  }
  fclose(file);

  return true;
}

bool CMidiMap::getNextWord(char*& pszLine, char*& pszWord)
{
  // First skip all leading blanks, etc.
  bool bCont = true;
  while(bCont) {
    char cHelp = *pszLine;
    switch( cHelp ) 
    {
      case 0: return false; // Out of line
      case ' ': 
      case '\n':
      case '\r':
      case '\t': pszLine++; break; // Goto next char
      default: bCont = false; break; // NonSpace character -> Word begins here.
    }
  }
  
  // The word starts here
  pszWord = pszLine;

  // And now, goto the end of the word.
  bCont = true;
  while(bCont) {
    char cHelp = *pszLine;
    switch( cHelp ) 
    {
      case 0: 
      case ',':
      case ';':
      case ' ': 
      case '\n':
      case '\r':
      case '\t': *pszLine++ = 0; bCont = false; break; // Goto next char
      default: pszLine++; break;
    }
  }

  return true;
}

void CMidiMap::parseLine(char* pszLine, const char* pszConfigFile, int nConfigLine )
{
  char* pszWord = 0;
  bool bOk = true;

  // Get first word of the line
  bOk = bOk && getNextWord(pszLine,pszWord);
  if( !bOk )
    return;

  // Skip comments
  if( *pszWord == '#' )
    return;

  if( strcmp(pszWord,"PRC") == 0 ) {
    bOk = bOk && getNextWord(pszLine,pszWord);
    int nOrigChannel = atol(pszWord);
    bOk = bOk && getNextWord(pszLine,pszWord);
    int nStart = atol(pszWord);
    bOk = bOk && getNextWord(pszLine,pszWord);
    int nEnd = atol(pszWord);
    bOk = bOk && getNextWord(pszLine,pszWord);
    int nChannel = atol(pszWord);
    if( bOk ) {
      channelMaps[nOrigChannel].nChannel = nOrigChannel;
      for( int i = nStart; i <= nEnd; i++ ) {
        channelMaps[nOrigChannel].channelRemaps[i].nPitch = i;
        channelMaps[nOrigChannel].channelRemaps[i].nChannel = nChannel;
      }
    }
    else {
      printf("midisend: (PRC) missing parameters at %s:%d\n", pszConfigFile,nConfigLine);
    }
    return;
  }

  if( strcmp(pszWord,"PRD") == 0 ) {
    bOk = bOk && getNextWord(pszLine,pszWord);
    int nOrigChannel = atol(pszWord);
    bOk = bOk && getNextWord(pszLine,pszWord);
    int nStart = atol(pszWord);
    bOk = bOk && getNextWord(pszLine,pszWord);
    int nEnd = atol(pszWord);
    bOk = bOk && getNextWord(pszLine,pszWord);
    int nPitchDiff = atol(pszWord);
    if( bOk ) {
      channelMaps[nOrigChannel].nChannel = nOrigChannel;
      for( int i = nStart; i <= nEnd; i++ ) {
        channelMaps[nOrigChannel].pitchRemaps[i].nPitch = i;
        channelMaps[nOrigChannel].pitchRemaps[i].nToPitch = i + nPitchDiff;
      }
    }
    else {
      printf("midisend: (PRD) missing parameters at %s:%d\n", pszConfigFile,nConfigLine);
    }
    return;
  }

  if( strcmp(pszWord,"PTC") == 0 ) {
    bOk = bOk && getNextWord(pszLine,pszWord);
    int nOrigChannel = atol(pszWord);
    bOk = bOk && getNextWord(pszLine,pszWord);
    int nPitch = atol(pszWord);
    bOk = bOk && getNextWord(pszLine,pszWord);
    int nToChannel = atol(pszWord);
    if( bOk ) {
      channelMaps[nOrigChannel].nChannel = nOrigChannel;
      channelMaps[nOrigChannel].channelRemaps[nPitch].nPitch = nPitch;
      channelMaps[nOrigChannel].channelRemaps[nPitch].nChannel = nToChannel;
    }
    else {
      printf("midisend: (PTC) missing parameters at %s:%d\n", pszConfigFile,nConfigLine);
    }
    return;
  }

  if( strcmp(pszWord,"PTP") == 0 ) {
    bOk = bOk && getNextWord(pszLine,pszWord);
    int nOrigChannel = atol(pszWord);
    bOk = bOk && getNextWord(pszLine,pszWord);
    int nPitch = atol(pszWord);
    bOk = bOk && getNextWord(pszLine,pszWord);
    int nToPitch = atol(pszWord);
    if( bOk ) {
      channelMaps[nOrigChannel].nChannel = nOrigChannel;
      channelMaps[nOrigChannel].pitchRemaps[nPitch].nPitch = nPitch;
      channelMaps[nOrigChannel].pitchRemaps[nPitch].nToPitch = nToPitch;
    }
    else {
      printf("midisend: (PTP) missing parameters at %s:%d\n", pszConfigFile,nConfigLine);
    }
    return;
  }

  printf("midisend: Unknown command at %s:%d\n", pszConfigFile,nConfigLine);
}

void CMidiMap::mapMsg(Byte* msg)
{
  // Get out the data for mapping
  int nChannel = midimsgGetChannel(msg);
  int nPitch = midimsgGetPitch(msg);

  // Is there something to map for this channel ?
  if( channelMaps.find(nChannel) != channelMaps.end() ) {
    // => Yes, than do it.

    // C/P => C
    if( channelMaps[nChannel].channelRemaps.find(nPitch) != channelMaps[nChannel].channelRemaps.end() ) 
      midimsgSetChannel(msg,channelMaps[nChannel].channelRemaps[nPitch].nChannel);

    // C/P => P
    if( channelMaps[nChannel].pitchRemaps.find(nPitch) != channelMaps[nChannel].pitchRemaps.end() ) {
      midimsgSetPitch(msg,channelMaps[nChannel].pitchRemaps[nPitch].nToPitch);
    }
  }
}