Loopp/src/observer/midi.cxx

101 lines
2.5 KiB
C++
Raw Normal View History

2013-07-31 18:19:15 +02:00
#include "midi.hxx"
2013-10-01 18:42:16 +02:00
#include <sstream>
2013-07-31 18:19:15 +02:00
#include "../jack.hxx"
extern Jack* jack;
2013-10-02 18:42:52 +02:00
MidiIO::MidiIO() :
jackInputPort(0),
jackOutputPort(0)
{
2013-11-02 00:31:18 +01:00
//LUPPP_NOTE("MidiIO %i",this);
}
2013-10-01 18:42:16 +02:00
MidiIO::~MidiIO()
{
jack->unregisterMidiIO( this );
LUPPP_NOTE("~MidiIO unregistring ports");
jack_port_unregister( jack->getJackClientPointer(), jackInputPort );
jack_port_unregister( jack->getJackClientPointer(), jackOutputPort );
}
2013-10-01 18:42:16 +02:00
2013-10-02 18:42:52 +02:00
void MidiIO::writeMidi( unsigned char* data )
{
void* portBuffer = jack_port_get_buffer( jackOutputPort, jack->getBuffersize() );
unsigned char* buffer = jack_midi_event_reserve( portBuffer, 0, 3);
if( buffer == 0 )
{
return;
}
else
{
//memcpy( buffer, data, sizeof(unsigned char)*3 );
buffer[0] = data[0];
buffer[1] = data[1];
buffer[2] = data[2];
}
}
int MidiIO::registerMidiPorts(std::string name)
2013-10-01 18:42:16 +02:00
{
// register the JACK MIDI ports
stringstream i;
2013-10-02 03:11:23 +02:00
i << name << " in";
jackInputPort = jack_port_register( jack->getJackClientPointer(),
2013-10-01 18:42:16 +02:00
i.str().c_str(),
JACK_DEFAULT_MIDI_TYPE,
JackPortIsInput,
0 );
stringstream o;
2013-10-02 03:11:23 +02:00
o << name << " out";
jackOutputPort = jack_port_register( jack->getJackClientPointer(),
2013-10-01 18:42:16 +02:00
o.str().c_str(),
JACK_DEFAULT_MIDI_TYPE,
JackPortIsOutput,
0 );
if ( jackInputPort && jackOutputPort )
{
//LUPPP_NOTE("%i, %i", jackInputPort, jackOutputPort );
return LUPPP_RETURN_OK;
}
else
{
LUPPP_ERROR("Error registering JACK ports" );
return LUPPP_RETURN_ERROR;
}
2013-10-01 18:42:16 +02:00
}
void MidiIO::initBuffers(int nframes)
{
// clear the output buffer
void* outputBuffer= (void*) jack_port_get_buffer( jackOutputPort, nframes );
jack_midi_clear_buffer( outputBuffer );
}
2013-10-02 18:42:52 +02:00
void MidiIO::process(int nframes)
2013-10-01 18:42:16 +02:00
{
// get port buffers and setup
2013-10-02 03:11:23 +02:00
void* inputBuffer = (void*) jack_port_get_buffer( jackInputPort, nframes );
2013-10-01 18:42:16 +02:00
jack_midi_event_t event;
2013-10-01 18:42:16 +02:00
int index = 0;
int event_count = (int) jack_midi_get_event_count( inputBuffer );
while ( index < event_count )
{
jack_midi_event_get(&event, inputBuffer, index);
midi( (unsigned char*) &event.buffer[0] );
//printf( "MIDI %i %i %i\n", int(event.buffer[0]), int(event.buffer[1]), int(event.buffer[2]) );
2013-10-01 18:42:16 +02:00
index++;
}
}