From ceac394c2133d44e81db2eb633ff54a9ad6ce7c5 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Thu, 10 Nov 2005 05:52:11 +0000 Subject: This commit was generated by cvs2svn to compensate for changes in r3865, which included commits to RCS files with non-trunk default branches. svn path=/trunk/extensions/gripd/; revision=3866 --- src/midiio/src/MidiOutPort_alsa.cpp | 469 ++++++++++++++++++++++++++++++++++++ 1 file changed, 469 insertions(+) create mode 100644 src/midiio/src/MidiOutPort_alsa.cpp (limited to 'src/midiio/src/MidiOutPort_alsa.cpp') diff --git a/src/midiio/src/MidiOutPort_alsa.cpp b/src/midiio/src/MidiOutPort_alsa.cpp new file mode 100644 index 0000000..07f4e5d --- /dev/null +++ b/src/midiio/src/MidiOutPort_alsa.cpp @@ -0,0 +1,469 @@ +// +// 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 +#include + +// 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; +std::ostream* MidiOutPort_alsa::tracedisplay = &std::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) { + std::cout << "Error: bad MidiOutPort object count!: " << objectCount << std::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()) { + std::cout << "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_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) { + std::cout << "Error: invalid sysex message" << std::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) { + std::cout << "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