~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to arts/midi/midisend.cc

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
Copyright (C) 1999 Emmeran Seehuber
 
4
                   the_emmy@gmx.de
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
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.
 
19
 
 
20
*/
 
21
 
 
22
/*
 
23
  Changes:
 
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 !
 
28
*/
 
29
 
 
30
/*
 
31
** This program was in original by David G. Slomin.
 
32
** It was released to the Public Domain on 1/25/99.
 
33
*/
 
34
 
 
35
#ifdef HAVE_CONFIG_H
 
36
#include "config.h"
 
37
#endif
 
38
 
 
39
#include "midisend.h"
 
40
#include <sys/types.h>
 
41
#include <sys/stat.h>
 
42
#include <fcntl.h>
 
43
 
 
44
using namespace std;
 
45
 
 
46
int input_fd = -1, test = 0, verbose = 0;
 
47
char cFileName[1025];
 
48
int optch;
 
49
CMidiMap Map;
 
50
 
 
51
void usage(char *prog)
 
52
{
 
53
        fprintf(stderr,"\n");
 
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");
 
61
        exit(1);
 
62
}
 
63
 
 
64
void parseArgs(int argc, char** argv)
 
65
{
 
66
  // Setup default
 
67
  strcpy(cFileName,"/dev/midi");
 
68
 
 
69
        while((optch = getopt(argc,argv,"m:f:vtl")) > 0)
 
70
        {
 
71
                switch(optch)
 
72
                {
 
73
                        case 'm': if( !Map.readMap(optarg) ) 
 
74
                  fprintf(stderr,"%s: can't read file %s!\n",argv[0],optarg);
 
75
                                break;
 
76
                        case 't': test = 1;
 
77
                                break;
 
78
                        case 'l': test = 2;
 
79
                                break;
 
80
      case 'v': verbose = 1; printf("MidiSend %s\n", VERSION );
 
81
        break;
 
82
                        case 'f': strncpy(cFileName,optarg,1024);
 
83
                                break;
 
84
                        default: usage(argv[0]);
 
85
                                break;
 
86
                }
 
87
        }
 
88
}
 
89
 
 
90
 
 
91
#ifdef COMMON_BINARY
 
92
int midisend_main(int argc, char *argv[])
 
93
#else
 
94
int main(int argc, char *argv[])
 
95
#endif
 
96
{
 
97
   Arts::Dispatcher dispatcher;
 
98
   Arts::MidiManager manager = Arts::Reference("global:Arts_MidiManager");
 
99
 
 
100
   if (manager.isNull())
 
101
   {
 
102
      fprintf(stderr, "%s trouble: No midimanager object found; please start "
 
103
                                  "artsd.\n",argv[0]);
 
104
      exit(EXIT_FAILURE);
 
105
   }
 
106
 
 
107
   /*
 
108
   ** MIDI input initialization.
 
109
   */
 
110
 
 
111
   parseArgs(argc,argv);
 
112
 
 
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();
 
117
 
 
118
   if(test)
 
119
   {
 
120
      if( verbose )
 
121
        printf("performing test ...\n");
 
122
      unsigned long i,max=5000;
 
123
      if(test==2) max = 20000;
 
124
      for(i=0;i<max;i++)
 
125
      {
 
126
         port.processCommand(
 
127
             Arts::MidiCommand(Arts::mcsNoteOn, 60+(i%12), 100));
 
128
         port.processCommand(
 
129
             Arts::MidiCommand(Arts::mcsNoteOff,60+(i%12), 0));
 
130
      }
 
131
      exit(0);
 
132
   }
 
133
 
 
134
   if( verbose )
 
135
     printf("trying to open %s ...", cFileName );
 
136
 
 
137
   input_fd = open(cFileName,O_RDONLY);
 
138
 
 
139
   if(input_fd == -1)
 
140
   {
 
141
      fprintf(stderr,"\n%s trouble: can't open input device!\n",argv[0]);
 
142
      exit(EXIT_FAILURE);
 
143
   }
 
144
   else if( verbose )
 
145
     printf(" ok!\n");
 
146
 
 
147
 
 
148
   /*
 
149
   ** Main loop.
 
150
   */
 
151
 
 
152
   if(verbose)
 
153
     printf("beginning loop ...\n");
 
154
 
 
155
   unsigned char msg[3];
 
156
 
 
157
   while (1)
 
158
   {
 
159
      midimsgRead(input_fd, msg);
 
160
      switch (midimsgGetMessageType(msg))
 
161
      {
 
162
         case MIDIMSG_NOTE_OFF:
 
163
            Map.mapMsg(msg);
 
164
            port.processCommand(
 
165
                                Arts::MidiCommand(Arts::mcsNoteOff|midimsgGetChannel(msg),
 
166
                                                                midimsgGetPitch(msg), midimsgGetVelocity(msg)));
 
167
            if( verbose ) 
 
168
              printf("NoteOff: Channel %d, Pitch %3d\n", midimsgGetChannel(msg),midimsgGetPitch(msg));
 
169
            break;
 
170
         case MIDIMSG_NOTE_ON:
 
171
            Map.mapMsg(msg);
 
172
            port.processCommand(
 
173
                                Arts::MidiCommand(Arts::mcsNoteOn|midimsgGetChannel(msg),
 
174
                                                                midimsgGetPitch(msg), midimsgGetVelocity(msg)));
 
175
            if( verbose )
 
176
              printf("NoteOn : Channel %d, Pitch %3d, Velocity %2d\n", midimsgGetChannel(msg),
 
177
                     midimsgGetPitch(msg),midimsgGetVelocity(msg));
 
178
            break;
 
179
                case MIDIMSG_PITCH_WHEEL:
 
180
            Map.mapMsg(msg);
 
181
            port.processCommand(
 
182
                                Arts::MidiCommand(Arts::mcsPitchWheel|midimsgGetChannel(msg),
 
183
                                                                midimsgGetPitch(msg), midimsgGetVelocity(msg)));
 
184
            if( verbose )
 
185
              printf("PitchWheel : Channel %d, LSB %3d MSB %3d\n", midimsgGetChannel(msg),
 
186
                     midimsgGetPitch(msg),midimsgGetVelocity(msg));
 
187
                        break;
 
188
      }
 
189
   }
 
190
}
 
191
 
 
192
//--------------------------------------------
 
193
// The mapping stuff
 
194
//--------------------------------------------
 
195
 
 
196
bool CMidiMap::readMap(const char* pszFileName)
 
197
{
 
198
  if( verbose )
 
199
    printf("reading mapfile %s ...\n", pszFileName);
 
200
 
 
201
  FILE *file = fopen(pszFileName,"r");
 
202
  if( !file )
 
203
     return false;
 
204
 
 
205
  char cBuffer[1024+1];
 
206
  char* pszLine;
 
207
  int nLine = 0;
 
208
  while( (pszLine = fgets(cBuffer,1024,file)) ) {
 
209
    nLine++;
 
210
    parseLine(pszLine,pszFileName,nLine);
 
211
  }
 
212
  fclose(file);
 
213
 
 
214
  return true;
 
215
}
 
216
 
 
217
bool CMidiMap::getNextWord(char*& pszLine, char*& pszWord)
 
218
{
 
219
  // First skip all leading blanks, etc.
 
220
  bool bCont = true;
 
221
  while(bCont) {
 
222
    char cHelp = *pszLine;
 
223
    switch( cHelp ) 
 
224
    {
 
225
      case 0: return false; // Out of line
 
226
      case ' ': 
 
227
      case '\n':
 
228
      case '\r':
 
229
      case '\t': pszLine++; break; // Goto next char
 
230
      default: bCont = false; break; // NonSpace charater -> Word begins here.
 
231
    }
 
232
  }
 
233
  
 
234
  // The word starts here
 
235
  pszWord = pszLine;
 
236
 
 
237
  // And now, goto the end of the word.
 
238
  bCont = true;
 
239
  while(bCont) {
 
240
    char cHelp = *pszLine;
 
241
    switch( cHelp ) 
 
242
    {
 
243
      case 0: 
 
244
      case ',':
 
245
      case ';':
 
246
      case ' ': 
 
247
      case '\n':
 
248
      case '\r':
 
249
      case '\t': *pszLine++ = 0; bCont = false; break; // Goto next char
 
250
      default: pszLine++; break;
 
251
    }
 
252
  }
 
253
 
 
254
  return true;
 
255
}
 
256
 
 
257
void CMidiMap::parseLine(char* pszLine, const char* pszConfigFile, int nConfigLine )
 
258
{
 
259
  char* pszWord = 0;
 
260
  bool bOk = true;
 
261
 
 
262
  // Get first word of the line
 
263
  bOk = bOk && getNextWord(pszLine,pszWord);
 
264
  if( !bOk )
 
265
    return;
 
266
 
 
267
  // Skip comments
 
268
  if( *pszWord == '#' )
 
269
    return;
 
270
 
 
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);
 
280
    if( bOk ) {
 
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;
 
285
      }
 
286
    }
 
287
    else {
 
288
      printf("midisend: (PRC) missing parameters at %s:%d\n", pszConfigFile,nConfigLine);
 
289
    }
 
290
    return;
 
291
  }
 
292
 
 
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);
 
302
    if( bOk ) {
 
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;
 
307
      }
 
308
    }
 
309
    else {
 
310
      printf("midisend: (PRD) missing parameters at %s:%d\n", pszConfigFile,nConfigLine);
 
311
    }
 
312
    return;
 
313
  }
 
314
 
 
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);
 
322
    if( bOk ) {
 
323
      channelMaps[nOrigChannel].nChannel = nOrigChannel;
 
324
      channelMaps[nOrigChannel].channelRemaps[nPitch].nPitch = nPitch;
 
325
      channelMaps[nOrigChannel].channelRemaps[nPitch].nChannel = nToChannel;
 
326
    }
 
327
    else {
 
328
      printf("midisend: (PTC) missing parameters at %s:%d\n", pszConfigFile,nConfigLine);
 
329
    }
 
330
    return;
 
331
  }
 
332
 
 
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);
 
340
    if( bOk ) {
 
341
      channelMaps[nOrigChannel].nChannel = nOrigChannel;
 
342
      channelMaps[nOrigChannel].pitchRemaps[nPitch].nPitch = nPitch;
 
343
      channelMaps[nOrigChannel].pitchRemaps[nPitch].nToPitch = nToPitch;
 
344
    }
 
345
    else {
 
346
      printf("midisend: (PTP) missing parameters at %s:%d\n", pszConfigFile,nConfigLine);
 
347
    }
 
348
    return;
 
349
  }
 
350
 
 
351
  printf("midisend: Unknown command at %s:%d\n", pszConfigFile,nConfigLine);
 
352
}
 
353
 
 
354
void CMidiMap::mapMsg(Byte* msg)
 
355
{
 
356
  // Get out the data for mapping
 
357
  int nChannel = midimsgGetChannel(msg);
 
358
  int nPitch = midimsgGetPitch(msg);
 
359
 
 
360
  // Is there something to map for this channel ?
 
361
  if( channelMaps.find(nChannel) != channelMaps.end() ) {
 
362
    // => Yes, than do it.
 
363
 
 
364
    // C/P => C
 
365
    if( channelMaps[nChannel].channelRemaps.find(nPitch) != channelMaps[nChannel].channelRemaps.end() ) 
 
366
      midimsgSetChannel(msg,channelMaps[nChannel].channelRemaps[nPitch].nChannel);
 
367
 
 
368
    // C/P => P
 
369
    if( channelMaps[nChannel].pitchRemaps.find(nPitch) != channelMaps[nChannel].pitchRemaps.end() ) {
 
370
      midimsgSetPitch(msg,channelMaps[nChannel].pitchRemaps[nPitch].nToPitch);
 
371
    }
 
372
  }
 
373
}