aboutsummaryrefslogtreecommitdiff
path: root/src/midiio/src/MidiOutPort_oss.cpp
blob: 74f17c4e99ae46216fd15eb6388716b807568611 (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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Fri Dec 18 19:22:20 PST 1998
// Last Modified: Fri Jan  8 04:26:16 PST 1999
// Last Modified: Wed May 10 17:00:11 PDT 2000 (name change from _linux to _oss)
// Filename:      ...sig/code/control/MidiOutPort/linux/MidiOutPort_oss.cpp
// Web Address:   http://sig.sapp.org/src/sig/MidiOutPort_oss.cpp
// Syntax:        C++ 
//
// Description:   Operating-System specific interface for
//                basic MIDI output capabilities in Linux using
//                OSS sound drivers.  Privately inherited by the
//                MidiOutPort class.
// 

#ifdef LINUX

#include "MidiOutPort_oss.h"
#include <iostream>
#include <stdlib.h>

// initialized static variables
int       MidiOutPort_oss::numDevices      = 0;
int       MidiOutPort_oss::objectCount     = 0;
int*      MidiOutPort_oss::portObjectCount = NULL;
int       MidiOutPort_oss::channelOffset   = 0;
int*      MidiOutPort_oss::trace           = NULL;
std::ostream*  MidiOutPort_oss::tracedisplay    = &std::cout;



//////////////////////////////
// 
// MidiOutPort_oss::MidiOutPort_oss
//	default values: autoOpen = 1
//


MidiOutPort_oss::MidiOutPort_oss(void) {
   if (objectCount == 0) {
      initialize();
   }
   objectCount++;

   port = -1;
   setPort(0);
}


MidiOutPort_oss::MidiOutPort_oss(int aPort, int autoOpen) {
   if (objectCount == 0) {
      initialize();
   }
   objectCount++;

   port = -1;
   setPort(aPort);
   if (autoOpen) {
      open();
   }
}



//////////////////////////////
//
// MidiOutPort_oss::~MidiOutPort_oss
//

MidiOutPort_oss::~MidiOutPort_oss() {
   objectCount--;
   if (objectCount == 0) {
      deinitialize();
   } else if (objectCount < 0) {
      std::cerr << "Error: bad MidiOutPort object count!: " << objectCount << std::endl;
      exit(1);
   }
}



//////////////////////////////
//
// MidiOutPort_oss::close
//

void MidiOutPort_oss::close(void) {
   // don't close anything, because the 
   // Linux driver keeps all of the ports open while the
   // midi driver (/dev/sequencer) is running.
}



//////////////////////////////
//
// MidiOutPort_oss::closeAll
//

void MidiOutPort_oss::closeAll(void) {
   // the Linux MIDI driver will close the /dev/sequencer device
   // which will close all MIDI output ports at the same time.
   Sequencer_oss::close();
}



//////////////////////////////
//
// MidiOutPort_oss::getChannelOffset -- returns zero if MIDI channel 
//     offset is 0, or 1 if offset is 1.
//

int MidiOutPort_oss::getChannelOffset(void) const {
   return channelOffset;
}



//////////////////////////////
//
// MidiOutPort_oss::getName -- returns the name of the port.
//	returns "" if no name. Name is valid until getName is called again.
//

const char* MidiOutPort_oss::getName(void) {
   if (getPort() == -1) { 
      return "Null OSS MIDI Output";
   }
   return getOutputName(getPort());
}


const char* MidiOutPort_oss::getName(int i) {
   return Sequencer_oss::getOutputName(i);
}



//////////////////////////////
//
// MidiOutPort_oss::getNumPorts -- returns the number of available
// 	ports for MIDI output
//

int MidiOutPort_oss::getNumPorts(void) {
   return Sequencer_oss::getNumOutputs();
}



//////////////////////////////
//
// MidiOutPort_oss::getPort -- returns the port to which this
//	object belongs (as set with the setPort function).
//

int MidiOutPort_oss::getPort(void) {
   return port;
}



//////////////////////////////
//
// MidiOutPort_oss::getPortStatus -- 0 if closed, 1 if open
//

int MidiOutPort_oss::getPortStatus(void) {
   // Linux MIDI devices are all open at the same time,
   // so if one is open, then they all are.
   return is_open();
}



//////////////////////////////
//
// MidiOutPort_oss::getTrace -- returns true if trace is on or
//	false if off.  If trace is on, then prints to standard output
//	the Midi message being sent.
//

int MidiOutPort_oss::getTrace(void) {
   if (getPort() == -1) return -1;

   return trace[getPort()];
}



//////////////////////////////
//
// MidiOutPort_oss::rawsend -- send the Midi command and its parameters
//

int MidiOutPort_oss::rawsend(int command, int p1, int p2) {
   if (getPort() == -1) return 0;

   int status;
   uchar mdata[3] = {(uchar)command, (uchar)p1, (uchar)p2};
   status = write(getPort(), mdata, 3);   

   if (getTrace()) {
      if (status == 1) {
         std::cout << "(" << std::hex << (int)mdata[0] << std::dec << ":"
              << (int)mdata[1] << "," << (int)mdata[2] << ")";
         std::cout.flush();
      } else {
         std::cout << "(" << std::hex << (int)mdata[0] << std::dec << "X"
              << (int)mdata[1] << "," << (int)mdata[2] << ")";
         std::cout.flush();
      }
   }      

   return status;
}


int MidiOutPort_oss::rawsend(int command, int p1) {
   if (getPort() == -1) return 0;   

   int status;
   uchar mdata[2] = {(uchar)command, (uchar)p1};

   status = write(getPort(), mdata, 2);   

   if (getTrace()) {
      if (status == 1) {
         std::cout << "(" << std::hex << (int)mdata[0] << std::dec << ":"
              << (int)mdata[1] << ")";
         std::cout.flush();
      } else {
         std::cout << "(" << std::hex << (int)mdata[0] << std::dec << "X"
              << (int)mdata[1] << "," << (int)mdata[2] << ")";
         std::cout.flush();
      }
   }
 
   return status;
}


int MidiOutPort_oss::rawsend(int command) {
   if (getPort() == -1) return 0;   

   int status;
   uchar mdata[1] = {(uchar)command};

   status = write(getPort(), mdata, 1);

   if (getTrace()) {
      if (status == 1) {
         std::cout << "(" << std::hex << (int)mdata[0] << ")";
         std::cout.flush();
      } else {
         std::cout << "(" << std::hex << (int)mdata[0] << ")";
         std::cout.flush();
      }
   }

   return status;
}


int MidiOutPort_oss::rawsend(uchar* array, int size) {
   if (getPort() == -1) return 0;   

   int status;
   status = write(getPort(), array, size);
   
   if (getTrace()) {
      if (status == 1) {
         std::cout << "(array)";
         std::cout.flush();
      } else {
         std::cout << "(XarrayX)";
         std::cout.flush();
      }
   }

   return status;
}



//////////////////////////////
//
// MidiOutPort_oss::open -- returns true if MIDI output port was
//	opened.
//

int MidiOutPort_oss::open(void) {
   return Sequencer_oss::open();
}



//////////////////////////////
//
// MidiOutPort_oss::setChannelOffset -- sets the MIDI channel offset, 
//     either 0 or 1.
//

void MidiOutPort_oss::setChannelOffset(int anOffset) {
   switch (anOffset) {
      case 0:   channelOffset = 0;   break;
      case 1:   channelOffset = 1;   break;
      default:
         std::cout << "Error:  Channel offset can be only 0 or 1." << std::endl;
         exit(1);
   }
}



//////////////////////////////
//
// MidiOutPort_oss::setPort --
//

void MidiOutPort_oss::setPort(int aPort) {
   if (aPort < -1 || aPort >= getNumPorts()) {
      std::cerr << "Error: maximum port number is: " << getNumPorts()-1
           << ", but you tried to access port: " << aPort << std::endl;
      exit(1);
   }

   if (port != -1) {
      portObjectCount[port]--;
   }
   port = aPort;
   if (port != -1) {
      portObjectCount[port]++;
   }
}



//////////////////////////////
//
// MidiOutPort_oss::setTrace -- if false, then won't print
//      Midi messages to standard output.
//

int MidiOutPort_oss::setTrace(int aState) {
   if (getPort() == -1) return -1;

   int oldtrace = trace[getPort()];
   if (aState == 0) {
      trace[getPort()] = 0;
   } else {
      trace[getPort()] = 1;
   }
   return oldtrace;
}



//////////////////////////////
//
// MidiOutPort_oss::sysex -- send a system exclusive message.
//     The message must start with a 0xf0 byte and end with
//     a 0xf7 byte.
//

int MidiOutPort_oss::sysex(uchar* array, int size) {
   if (size == 0 || array[0] != 0xf0) {
      std::cout << "Error: invalid sysex message" << std::endl;
      exit(1);
   }

   return rawsend(array,size);
}



//////////////////////////////
//
// MidiOutPort_oss::toggleTrace
//

void MidiOutPort_oss::toggleTrace(void) {
   if (getPort() == -1) return;

   trace[getPort()] = !trace[getPort()];
}



///////////////////////////////////////////////////////////////////////////
//
// Private functions
//



//////////////////////////////
//
// MidiOutPort_oss::deinitialize -- sets up storage if necessary
//	This function should be called if the current object is
//	the first object to be created.
//

void MidiOutPort_oss::deinitialize(void) {
   closeAll();
   if (portObjectCount != NULL) delete [] portObjectCount;
   portObjectCount = NULL;
   if (trace != NULL) delete [] trace;
   trace = NULL;
}



//////////////////////////////
//
// MidiOutPort_oss::initialize -- sets up storage if necessary
//	This function should be called if the current object is
//	the first object to be created.
//

void MidiOutPort_oss::initialize(void) {
   // get the number of ports
   numDevices = getNumOutputs();
   if  (getNumPorts() <= 0) {
      std::cerr << "Warning: no MIDI output devices" << std::endl;
      portObjectCount = NULL;
      trace = NULL;
   } else {
      // allocate space for object count on each port:
      if (portObjectCount != NULL) delete [] portObjectCount;
      portObjectCount = new int[numDevices];
   
      // allocate space for trace variable for each port:
      if (trace != NULL) delete [] trace;
      trace = new int[numDevices];
   
      // initialize the static arrays
      for (int i=0; i<getNumPorts(); i++) {
         portObjectCount[i] = 0;
         trace[i] = 0;
      }
   }
}



//////////////////////////////
//
// MidiOutPort_oss::setPortStatus
//

void MidiOutPort_oss::setPortStatus(int aStatus) {
   // not used in Linux implementation
}


#endif  // LINUX



// md5sum:	c09dbe18ce8a0ff6ff11030d43a98c4a  - MidiOutPort_oss.cpp =css= 20030102