2013-12-08 22:44:43 +01:00
|
|
|
/*
|
|
|
|
* Author: Harry van Haaren 2013
|
|
|
|
* harryhaaren@gmail.com
|
2016-12-27 13:47:19 +01:00
|
|
|
*
|
2013-12-08 22:44:43 +01:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2016-12-27 13:47:19 +01:00
|
|
|
*
|
2013-12-08 22:44:43 +01:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2016-12-27 13:47:19 +01:00
|
|
|
*
|
2013-12-08 22:44:43 +01:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
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"
|
|
|
|
|
2013-07-31 18:31:47 +02:00
|
|
|
extern Jack* jack;
|
|
|
|
|
2013-10-02 18:42:52 +02:00
|
|
|
MidiIO::MidiIO() :
|
2016-12-27 13:47:19 +01:00
|
|
|
portsRegistered(false),
|
|
|
|
jackInputPort(0),
|
|
|
|
jackOutputPort(0)
|
2013-09-30 15:57:17 +02:00
|
|
|
{
|
2019-06-09 10:41:19 +02:00
|
|
|
//LOOPP_NOTE("MidiIO %i",this);
|
2013-09-30 15:57:17 +02:00
|
|
|
}
|
2013-10-01 18:42:16 +02:00
|
|
|
|
2013-11-10 19:12:28 +01:00
|
|
|
MidiIO::~MidiIO()
|
|
|
|
{
|
2016-12-27 13:47:19 +01:00
|
|
|
jack->unregisterMidiIO( this );
|
|
|
|
|
2019-06-09 10:41:19 +02:00
|
|
|
//LOOPP_NOTE("~MidiIO unregistring ports");
|
2016-12-27 13:47:19 +01:00
|
|
|
jack_port_unregister( jack->getJackClientPointer(), jackInputPort );
|
|
|
|
jack_port_unregister( jack->getJackClientPointer(), jackOutputPort );
|
2013-11-10 19:12:28 +01:00
|
|
|
}
|
|
|
|
|
2013-10-01 18:42:16 +02:00
|
|
|
|
2013-10-02 18:42:52 +02:00
|
|
|
void MidiIO::writeMidi( unsigned char* data )
|
2013-10-02 04:05:10 +02:00
|
|
|
{
|
2016-12-27 13:47:19 +01:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2013-10-02 04:05:10 +02:00
|
|
|
}
|
|
|
|
|
2013-10-11 22:33:20 +02:00
|
|
|
int MidiIO::registerMidiPorts(std::string name)
|
2013-10-01 18:42:16 +02:00
|
|
|
{
|
2016-12-27 13:47:19 +01:00
|
|
|
if( !jack ) {
|
2019-06-09 10:41:19 +02:00
|
|
|
LOOPP_ERROR("Attempted register of controller, JACK not instantiated yet!");
|
|
|
|
return LOOPP_RETURN_ERROR;
|
2016-12-27 13:47:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
jack_client_t* c = jack->getJackClientPointer();
|
|
|
|
|
|
|
|
// register the JACK MIDI ports
|
|
|
|
stringstream i;
|
|
|
|
i << name << " in";
|
|
|
|
jackInputPort = jack_port_register( c,
|
|
|
|
i.str().c_str(),
|
|
|
|
JACK_DEFAULT_MIDI_TYPE,
|
|
|
|
JackPortIsInput,
|
|
|
|
0 );
|
|
|
|
stringstream o;
|
|
|
|
o << name << " out";
|
|
|
|
jackOutputPort = jack_port_register( c,
|
|
|
|
o.str().c_str(),
|
|
|
|
JACK_DEFAULT_MIDI_TYPE,
|
|
|
|
JackPortIsOutput,
|
|
|
|
0 );
|
|
|
|
|
|
|
|
if ( jackInputPort && jackOutputPort ) {
|
2019-06-09 10:41:19 +02:00
|
|
|
//LOOPP_NOTE("%i, %i", jackInputPort, jackOutputPort );
|
2016-12-27 13:47:19 +01:00
|
|
|
portsRegistered = true;
|
2019-06-09 10:41:19 +02:00
|
|
|
return LOOPP_RETURN_OK;
|
2016-12-27 13:47:19 +01:00
|
|
|
} else {
|
2019-06-09 10:41:19 +02:00
|
|
|
LOOPP_ERROR("Error registering JACK ports" );
|
|
|
|
return LOOPP_RETURN_ERROR;
|
2016-12-27 13:47:19 +01:00
|
|
|
}
|
2013-10-01 18:42:16 +02:00
|
|
|
}
|
|
|
|
|
2013-10-01 22:41:39 +02:00
|
|
|
|
2013-10-03 16:59:34 +02:00
|
|
|
void MidiIO::initBuffers(int nframes)
|
|
|
|
{
|
2016-12-27 13:47:19 +01:00
|
|
|
if ( !portsRegistered )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// clear the output buffer
|
|
|
|
void* outputBuffer= (void*) jack_port_get_buffer( jackOutputPort, nframes );
|
|
|
|
jack_midi_clear_buffer( outputBuffer );
|
2013-10-03 16:59:34 +02:00
|
|
|
}
|
|
|
|
|
2013-10-02 18:42:52 +02:00
|
|
|
void MidiIO::process(int nframes)
|
2013-10-01 18:42:16 +02:00
|
|
|
{
|
2016-12-27 13:47:19 +01:00
|
|
|
if ( !portsRegistered )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// get port buffers and setup
|
|
|
|
void* inputBuffer = (void*) jack_port_get_buffer( jackInputPort, nframes );
|
|
|
|
|
|
|
|
jack_midi_event_t event;
|
|
|
|
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]) );
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
2013-10-01 18:42:16 +02:00
|
|
|
}
|