// // Programmer: Craig Stuart Sapp // Creation Date: Wed May 10 16:16:21 PDT 2000 // Last Modified: Sun May 14 20:44:12 PDT 2000 // Last Modified: Thu Jun 24 02:35:06 PDT 2004 (frozen as ALSA 0.9 interface) // Filename: ...sig/code/control/MidiOutPort/alsa/MidiOutPort_alsa09.cpp // Web Address: http://sig.sapp.org/src/sig/MidiOutPort_alsa09.cpp // Syntax: C++ // // Description: Operating-System specific interface for // basic MIDI output capabilities in Linux using // ALSA sound drivers. Privately inherited by the // MidiOutPort class via the MidiOutPort_alsa09 class. // #if defined(LINUX) && defined(ALSA09) #include "MidiOutPort_alsa09.h" #include #ifndef OLDCPP #include using namespace std; #else #include #endif // initialized static variables int MidiOutPort_alsa09::numDevices = 0; int MidiOutPort_alsa09::objectCount = 0; int* MidiOutPort_alsa09::portObjectCount = NULL; int MidiOutPort_alsa09::channelOffset = 0; int* MidiOutPort_alsa09::trace = NULL; ostream* MidiOutPort_alsa09::tracedisplay = &cout; ////////////////////////////// // // MidiOutPort_alsa09::MidiOutPort_alsa09 // default values: autoOpen = 1 // #include MidiOutPort_alsa09::MidiOutPort_alsa09(void) { if (objectCount == 0) { initialize(); } objectCount++; port = -1; setPort(0); } MidiOutPort_alsa09::MidiOutPort_alsa09(int aPort, int autoOpen) { if (objectCount == 0) { initialize(); } objectCount++; port = -1; setPort(aPort); if (autoOpen) { open(); } } ////////////////////////////// // // MidiOutPort_alsa09::~MidiOutPort_alsa09 -- // MidiOutPort_alsa09::~MidiOutPort_alsa09() { objectCount--; if (objectCount == 0) { deinitialize(); } else if (objectCount < 0) { cout << "Error: bad MidiOutPort object count!: " << objectCount << endl; exit(1); } } ////////////////////////////// // // MidiOutPort_alsa09::close -- // void MidiOutPort_alsa09::close(void) { Sequencer_alsa09::closeOutput(getPort()); } ////////////////////////////// // // MidiOutPort_alsa09::closeAll -- // void MidiOutPort_alsa09::closeAll(void) { int i; for (i=0; i= getNumPorts()) { cout << "Error: maximum port number is: " << getNumPorts()-1 << ", but you tried to access port: " << aPort << endl; exit(1); } if (port != -1) { portObjectCount[port]--; } port = aPort; if (port != -1) { portObjectCount[port]++; } } ////////////////////////////// // // MidiOutPort_alsa09::setTrace -- if false, then won't print // Midi messages to standard output. // int MidiOutPort_alsa09::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_alsa09::sysex -- send a system exclusive message. // The message must start with a 0xf0 byte and end with // a 0xf7 byte. // int MidiOutPort_alsa09::sysex(uchar* array, int size) { if (getPort() == -1) { return 2; } if (size == 0 || array[0] != 0xf0 || array[size-1] != 0xf7) { cout << "Error: invalid sysex message" << endl; exit(1); } return rawsend(array,size); } ////////////////////////////// // // MidiOutPort_alsa09::toggleTrace -- // void MidiOutPort_alsa09::toggleTrace(void) { if (getPort() == -1) return; trace[getPort()] = !trace[getPort()]; } /////////////////////////////////////////////////////////////////////////// // // Private functions // ////////////////////////////// // // MidiOutPort_alsa09::deinitialize -- sets up storage if necessary // This function should be called if the current object is // the first object to be created. // void MidiOutPort_alsa09::deinitialize(void) { closeAll(); if (portObjectCount != NULL) delete [] portObjectCount; portObjectCount = NULL; if (trace != NULL) delete [] trace; trace = NULL; } ////////////////////////////// // // MidiOutPort_alsa09::initialize -- sets up storage if necessary // This function should be called if the current object is // the first object to be created. // void MidiOutPort_alsa09::initialize(void) { // get the number of ports numDevices = getNumOutputs(); if (getNumPorts() <= 0) { cout << "Warning: no MIDI output devices" << 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