// // Programmer: Craig Stuart Sapp // Creation Date: Wed May 10 16:16:21 PDT 2000 // Last Modified: Sun May 14 20:44:12 PDT 2000 // Filename: ...sig/code/control/MidiOutPort/alsa/MidiOutPort_alsa.cpp // Web Address: http://sig.sapp.org/src/sig/MidiOutPort_alsa.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_alsa class. // #if defined(LINUX) && defined(ALSA) #include "MidiOutPort_alsa.h" #include #ifndef OLDCPP #include using namespace std; #else #include #endif // initialized static variables int MidiOutPort_alsa::numDevices = 0; int MidiOutPort_alsa::objectCount = 0; int* MidiOutPort_alsa::portObjectCount = NULL; int MidiOutPort_alsa::channelOffset = 0; int* MidiOutPort_alsa::trace = NULL; ostream* MidiOutPort_alsa::tracedisplay = &cout; ////////////////////////////// // // MidiOutPort_alsa::MidiOutPort_alsa // default values: autoOpen = 1 // #include MidiOutPort_alsa::MidiOutPort_alsa(void) { if (objectCount == 0) { initialize(); } objectCount++; port = -1; setPort(0); } MidiOutPort_alsa::MidiOutPort_alsa(int aPort, int autoOpen) { if (objectCount == 0) { initialize(); } objectCount++; port = -1; setPort(aPort); if (autoOpen) { open(); } } ////////////////////////////// // // MidiOutPort_alsa::~MidiOutPort_alsa -- // MidiOutPort_alsa::~MidiOutPort_alsa() { objectCount--; if (objectCount == 0) { deinitialize(); } else if (objectCount < 0) { cout << "Error: bad MidiOutPort object count!: " << objectCount << endl; exit(1); } } ////////////////////////////// // // MidiOutPort_alsa::close -- // void MidiOutPort_alsa::close(void) { Sequencer_alsa::closeOutput(getPort()); } ////////////////////////////// // // MidiOutPort_alsa::closeAll -- // void MidiOutPort_alsa::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_alsa::setTrace -- if false, then won't print // Midi messages to standard output. // int MidiOutPort_alsa::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_alsa::sysex -- send a system exclusive message. // The message must start with a 0xf0 byte and end with // a 0xf7 byte. // int MidiOutPort_alsa::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_alsa::toggleTrace -- // void MidiOutPort_alsa::toggleTrace(void) { if (getPort() == -1) return; trace[getPort()] = !trace[getPort()]; } /////////////////////////////////////////////////////////////////////////// // // Private functions // ////////////////////////////// // // MidiOutPort_alsa::deinitialize -- sets up storage if necessary // This function should be called if the current object is // the first object to be created. // void MidiOutPort_alsa::deinitialize(void) { closeAll(); if (portObjectCount != NULL) delete [] portObjectCount; portObjectCount = NULL; if (trace != NULL) delete [] trace; trace = NULL; } ////////////////////////////// // // MidiOutPort_alsa::initialize -- sets up storage if necessary // This function should be called if the current object is // the first object to be created. // void MidiOutPort_alsa::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