1
//=========================================================
4
// $Id: jackmidi.cpp,v 1.1.1.1 2010/01/27 09:06:43 terminator356 Exp $
5
// (C) Copyright 1999-2010 Werner Schweer (ws@seh.de)
6
//=========================================================
12
#include <jack/jack.h>
13
//#include <jack/midiport.h>
20
#include "../midiport.h"
21
#include "../midiseq.h"
22
#include "../midictrl.h"
27
#include "../mplugins/midiitransform.h"
28
#include "../mplugins/mitplugin.h"
31
// Turn on debug messages.
32
//#define JACK_MIDI_DEBUG
34
extern unsigned int volatile lastExtMidiSyncTick;
36
///int jackmidi_pi[2];
37
///int jackmidi_po[2];
39
//extern muse_jack_midi_buffer jack_midi_out_data[JACK_MIDI_CHANNELS];
40
//extern muse_jack_midi_buffer jack_midi_in_data[JACK_MIDI_CHANNELS];
41
///extern jack_port_t *midi_port_in[JACK_MIDI_CHANNELS];
42
///extern jack_port_t *midi_port_out[JACK_MIDI_CHANNELS];
44
///MidiJackDevice* gmdev = NULL;
47
//static snd_seq_addr_t musePort;
49
//int MidiJackDevice::_nextOutIdNum = 0;
50
//int MidiJackDevice::_nextInIdNum = 0;
52
//int JackMidiPortList::_nextOutIdNum = 0;
53
//int JackMidiPortList::_nextInIdNum = 0;
55
//JackMidiPortList jackMidiClientPorts;
59
//---------------------------------------------------------
61
//---------------------------------------------------------
63
JackMidiPortList::JackMidiPortList()
68
JackMidiPortList::~JackMidiPortList()
73
iJackMidiPort JackMidiPortList::createClientPort(int flags) // 1 = writable, 2 = readable - do not mix
78
snprintf(buf, 80, "muse-jack-midi-out-%d", _nextOutIdNum);
79
jack_port_t* _client_jackport = (jack_port_t*)audioDevice->registerOutPort(buf, true);
80
if(_client_jackport == NULL)
82
fprintf(stderr, "JackMidiPortList::createClientPort failed to register jack-midi-out\n");
83
//return QString("Could not register jack-midi-out client port");
88
JackMidiPort jmp(_client_jackport, QString(buf), flags);
90
return insert(begin(), std::pair<jack_port_t*, JackMidiPort>(_client_jackport, jmp));
97
snprintf(buf, 80, "muse-jack-midi-in-%d", _nextInIdNum);
98
jack_port_t* _client_jackport = (jack_port_t*)audioDevice->registerInPort(buf, true);
99
if(_client_jackport == NULL)
101
fprintf(stderr, "JackMidiPortList::createClientPort failed to register jack-midi-in\n");
106
JackMidiPort jmp(_client_jackport, QString(buf), flags);
108
return insert(begin(), std::pair<jack_port_t*, JackMidiPort>(_client_jackport, jmp));
114
// Return true if removed.
115
bool JackMidiPortList::removeClientPort(jack_port_t* port)
117
iJackMidiPort ijp = find(port);
122
if(ijp->second._flags & 1)
125
if(ijp->second._flags & 2)
130
audioDevice->unregisterPort(port);
136
//---------------------------------------------------------
138
//---------------------------------------------------------
140
//MidiJackDevice::MidiJackDevice(const int& a, const QString& n)
141
MidiJackDevice::MidiJackDevice(jack_port_t* jack_port, const QString& n)
144
//_client_jackport = 0;
145
_client_jackport = jack_port;
150
MidiJackDevice::~MidiJackDevice()
152
#ifdef JACK_MIDI_DEBUG
153
printf("MidiJackDevice::~MidiJackDevice()\n");
156
audioDevice->unregisterPort(_client_jackport);
161
//---------------------------------------------------------
163
//---------------------------------------------------------
165
int MidiJackDevice::selectRfd()
167
return jackmidi_pi[0];
170
int MidiJackDevice::selectWfd()
172
return jackmidi_po[0];
176
//---------------------------------------------------------
177
// createJackMidiDevice
178
// If name parameter is blank, creates a new (locally) unique one.
179
//---------------------------------------------------------
181
//QString MidiJackDevice::createJackMidiDevice(int rwflags) // 1:Writable 2: Readable. Do not mix.
182
MidiDevice* MidiJackDevice::createJackMidiDevice(QString name, int rwflags) // 1:Writable 2: Readable. Do not mix.
184
/// _openFlags &= _rwFlags; // restrict to available bits
186
/// #ifdef JACK_MIDI_DEBUG
187
/// printf("MidiJackDevice::open %s\n", name.latin1());
190
//jack_port_t* jp = jack_port_by_name(_client, name().latin1());
191
/// jack_port_t* jp = (jack_port_t*)audioDevice->findPort(name().latin1());
195
/// printf("MidiJackDevice::open: Jack midi port %s not found!\n", name().latin1());
196
/// _writeEnable = false;
197
/// _readEnable = false;
198
/// return QString("Jack midi port not found");
201
/// int pf = jack_port_flags(jp);
203
//if(!name.isEmpty())
206
// if(audioDevice->findPort(name.latin1()))
208
// fprintf(stderr, "MidiJackDevice::createJackMidiDevice failed! Given port name %s already exists!\n", name.latin1());
213
jack_port_t* client_jackport = NULL;
217
// If Jack port can receive data from us and we actually want to...
218
//if((pf & JackPortIsInput) && (_openFlags & 1))
223
//snprintf(buf, 80, "muse-jack-midi-out-%d", _nextOutIdNum);
224
for(int i = 0; ; ++i)
226
//snprintf(buf, 80, "midi-out-%d", i);
227
name.sprintf("midi-out-%d", i);
229
if(!midiDevices.find(name))
232
//if(!audioDevice->findPort(buf))
234
//client_jackport = (jack_port_t*)audioDevice->registerOutPort(buf, true);
235
if(audioDevice->deviceType() == AudioDevice::JACK_AUDIO) // p3.3.52
237
client_jackport = (jack_port_t*)audioDevice->registerOutPort(name.latin1(), true);
247
fprintf(stderr, "MidiJackDevice::createJackMidiDevice failed! Can't find unused output port name!\n");
251
//name = QString(buf);
255
if(audioDevice->deviceType() == AudioDevice::JACK_AUDIO) // p3.3.52
257
client_jackport = (jack_port_t*)audioDevice->registerOutPort(name.latin1(), true);
260
fprintf(stderr, "MidiJackDevice::createJackMidiDevice failed creating output port name %s\n", name.latin1());
268
client_jackport = (jack_port_t*)audioDevice->registerOutPort(name.latin1(), true);
271
for(int i = 0; ; ++i)
273
snprintf(buf, 80, "midi-out-%d", i);
275
//if(!audioDevice->findPort(buf))
277
client_jackport = (jack_port_t*)audioDevice->registerOutPort(buf, true);
283
fprintf(stderr, "MidiJackDevice::createJackMidiDevice failed! Can't find unused output port name!\n");
292
//client_jackport = (jack_port_t*)audioDevice->registerOutPort(name.latin1(), true);
293
//if(client_jackport == NULL)
295
// fprintf(stderr, "MidiJackDevice::createJackMidiDevice failed to register jack midi client output port %s\n", name.latin1());
302
else // Note docs say it can't be both input and output.
303
// If Jack port can send data to us and we actually want it...
304
//if((pf & JackPortIsOutput) && (_openFlags & 2))
309
//snprintf(buf, 80, "muse-jack-midi-in-%d", _nextInIdNum);
310
for(int i = 0; ; ++i)
312
//snprintf(buf, 80, "midi-in-%d", i);
313
name.sprintf("midi-in-%d", i);
315
if(!midiDevices.find(name))
318
//if(!audioDevice->findPort(buf))
320
//client_jackport = (jack_port_t*)audioDevice->registerInPort(buf, true);
321
if(audioDevice->deviceType() == AudioDevice::JACK_AUDIO) // p3.3.52
323
client_jackport = (jack_port_t*)audioDevice->registerInPort(name.latin1(), true);
333
fprintf(stderr, "MidiJackDevice::createJackMidiDevice failed! Can't find unused input port name!\n");
337
//name = QString(buf);
341
if(audioDevice->deviceType() == AudioDevice::JACK_AUDIO) // p3.3.52
343
client_jackport = (jack_port_t*)audioDevice->registerInPort(name.latin1(), true);
346
fprintf(stderr, "MidiJackDevice::createJackMidiDevice failed creating input port name %s\n", name.latin1());
352
//client_jackport = (jack_port_t*)audioDevice->registerInPort(name.latin1(), true);
354
//if(client_jackport == NULL)
356
// fprintf(stderr, "MidiJackDevice::createJackMidiDevice failed to register jack midi client input port %s\n", name.latin1());
357
//_readEnable = false;
358
//return QString("Could not register jack-midi-in client port");
366
//if(client_jackport == NULL) // p3.3.52 Removed. Allow the device to be created even if Jack isn't running.
369
MidiJackDevice* dev = new MidiJackDevice(client_jackport, name);
370
dev->setrwFlags(rwflags);
371
midiDevices.add(dev);
375
//---------------------------------------------------------
377
//---------------------------------------------------------
379
void MidiJackDevice::setName(const QString& s)
381
#ifdef JACK_MIDI_DEBUG
382
printf("MidiJackDevice::setName %s new name:%s\n", name().latin1(), s.latin1());
385
if(clientPort()) // p3.3.52 Added check.
386
audioDevice->setPortName(clientPort(), s.latin1());
389
//---------------------------------------------------------
391
//---------------------------------------------------------
393
QString MidiJackDevice::open()
395
_openFlags &= _rwFlags; // restrict to available bits
397
#ifdef JACK_MIDI_DEBUG
398
printf("MidiJackDevice::open %s\n", name().latin1());
402
//jack_port_t* jp = jack_port_by_name(_client, name().latin1());
403
jack_port_t* jp = (jack_port_t*)audioDevice->findPort(name().latin1());
407
printf("MidiJackDevice::open: Jack midi port %s not found!\n", name().latin1());
408
_writeEnable = false;
410
return QString("Jack midi port not found");
413
int pf = jack_port_flags(jp);
415
// If Jack port can receive data from us and we actually want to...
416
if((pf & JackPortIsInput) && (_openFlags & 1))
419
snprintf(buf, 80, "muse-jack-midi-out-%d", _nextOutIdNum);
420
_client_jackport = (jack_port_t*)audioDevice->registerOutPort(buf, true);
421
if(_client_jackport == NULL)
423
fprintf(stderr, "MidiJackDevice::open failed to register jack-midi-out\n");
424
_writeEnable = false;
425
return QString("Could not register jack-midi-out client port");
431
///audioDevice->connect(_client_jackport, jp);
435
else // Note docs say it can't be both input and output.
436
// If Jack port can send data to us and we actually want it...
437
if((pf & JackPortIsOutput) && (_openFlags & 2))
440
snprintf(buf, 80, "muse-jack-midi-in-%d", _nextInIdNum);
441
_client_jackport = (jack_port_t*)audioDevice->registerInPort(buf, true);
442
if(_client_jackport == NULL)
444
fprintf(stderr, "MidiJackDevice::open failed to register jack-midi-in\n");
446
return QString("Could not register jack-midi-in client port");
451
///audioDevice->connect(jp, _client_jackport);
457
_writeEnable = bool(_openFlags & 1);
458
_readEnable = bool(_openFlags & 2);
460
return QString("OK");
463
//---------------------------------------------------------
465
//---------------------------------------------------------
467
void MidiJackDevice::close()
469
#ifdef JACK_MIDI_DEBUG
470
printf("MidiJackDevice::close %s\n", name().latin1());
476
int pf = jack_port_flags(_client_jackport);
478
if(pf & JackPortIsOutput)
481
if(pf & JackPortIsInput)
483
audioDevice->unregisterPort(_client_jackport);
484
_client_jackport = 0;
485
_writeEnable = false;
491
_writeEnable = false;
495
//jack_port_t* jp = jack_port_by_name(_client, name().latin1());
496
jack_port_t* jp = (jack_port_t*)audioDevice->findPort(name().latin1());
500
printf("MidiJackDevice::close: Jack midi port %s not found!\n", name().latin1());
501
_writeEnable = false;
506
//int pf = jack_port_flags(jp);
508
// If Jack port can receive data from us and we actually want to...
509
//if((pf & JackPortIsInput) && (_openFlags & 1))
510
if(jack_port_connected_to(midi_port_out[0], name().latin1()))
513
/// audioDevice->disconnect(midi_port_out[0], jp);
514
_writeEnable = false;
516
else // Note docs say it can't be both input and output.
517
// If Jack port can send data to us and we actually want it...
518
//if((pf & JackPortIsOutput) && (_openFlags & 2))
519
if(jack_port_connected_to(midi_port_in[0], name().latin1()))
521
/// audioDevice->disconnect(jp, midi_port_in[0]);
527
//---------------------------------------------------------
529
//---------------------------------------------------------
531
void MidiJackDevice::writeRouting(int level, Xml& xml) const
534
// If this device is not actually in use by the song, do not write any routes.
535
// This prevents bogus routes from being saved and propagated in the med file.
540
if(rwFlags() & 2) // Readable
542
//RouteList* rl = _inRoutes;
543
//for (ciRoute r = rl->begin(); r != rl->end(); ++r)
544
for (ciRoute r = _inRoutes.begin(); r != _inRoutes.end(); ++r)
546
if(!r->name().isEmpty())
548
xml.tag(level++, "Route");
550
//xml.strTag(level, "srcNode", r->name());
551
//xml.tag(level, "source type=\"%d\" name=\"%s\"/", r->type, r->name().latin1());
552
s = QT_TR_NOOP("source");
553
if(r->type != Route::TRACK_ROUTE)
554
s += QString(QT_TR_NOOP(" type=\"%1\"")).arg(r->type);
556
//s += QString(QT_TR_NOOP(" name=\"%1\"/")).arg(r->name());
557
s += QString(QT_TR_NOOP(" name=\"%1\"/")).arg(Xml::xmlString(r->name()));
560
//xml.strTag(level, "dstNode", name());
561
//xml.tag(level, "dest type=\"%d\" name=\"%s\"/", Route::JACK_MIDI_ROUTE, name().latin1());
562
//xml.tag(level, "dest type=\"%d\" name=\"%s\"/", Route::MIDI_DEVICE_ROUTE, name().latin1());
563
//xml.tag(level, "dest devtype=\"%d\" name=\"%s\"/", MidiDevice::JACK_MIDI, name().latin1());
564
xml.tag(level, "dest devtype=\"%d\" name=\"%s\"/", MidiDevice::JACK_MIDI, Xml::xmlString(name()).latin1());
566
xml.etag(level--, "Route");
571
for (ciRoute r = _outRoutes.begin(); r != _outRoutes.end(); ++r)
573
if(!r->name().isEmpty())
575
s = QT_TR_NOOP("Route");
577
s += QString(QT_TR_NOOP(" channel=\"%1\"")).arg(r->channel);
579
//xml.tag(level++, "Route");
583
//xml.strTag(level, "srcNode", name());
585
//xml.tag(level, "source type=\"%d\" channel=\"%d\" name=\"%s\"/", Route::JACK_MIDI_ROUTE, r->channel, name().latin1());
586
//xml.tag(level, "source type=\"%d\" channel=\"%d\" name=\"%s\"/", Route::MIDI_DEVICE_ROUTE, r->channel, name().latin1());
587
xml.tag(level, "source devtype=\"%d\" channel=\"%d\" name=\"%s\"/", MidiDevice::JACK_MIDI, r->channel, name().latin1());
589
//xml.tag(level, "source type=\"%d\" name=\"%s\"/", Route::JACK_MIDI_ROUTE, name().latin1());
590
//xml.tag(level, "source type=\"%d\" name=\"%s\"/", Route::MIDI_DEVICE_ROUTE, name().latin1());
592
//xml.tag(level, "source devtype=\"%d\" name=\"%s\"/", MidiDevice::JACK_MIDI, name().latin1());
593
xml.tag(level, "source devtype=\"%d\" name=\"%s\"/", MidiDevice::JACK_MIDI, Xml::xmlString(name()).latin1());
596
//xml.strTag(level, "dstNode", r->name());
599
if(r->type == Route::MIDI_DEVICE_ROUTE)
600
xml.tag(level, "dest devtype=\"%d\" channel=\"%d\" name=\"%s\"/", r->device->deviceType(), r->channel, r->name().latin1());
602
xml.tag(level, "dest type=\"%d\" channel=\"%d\" name=\"%s\"/", r->type, r->channel, r->name().latin1());
606
if(r->type == Route::MIDI_DEVICE_ROUTE)
607
xml.tag(level, "dest devtype=\"%d\" name=\"%s\"/", r->device->deviceType(), r->name().latin1());
609
xml.tag(level, "dest type=\"%d\" name=\"%s\"/", r->type, r->name().latin1());
613
s = QT_TR_NOOP("dest");
614
if(r->type == Route::MIDI_DEVICE_ROUTE)
615
s += QString(QT_TR_NOOP(" devtype=\"%1\"")).arg(r->device->deviceType());
617
if(r->type != Route::TRACK_ROUTE)
618
s += QString(QT_TR_NOOP(" type=\"%1\"")).arg(r->type);
620
//s += QString(QT_TR_NOOP(" name=\"%1\"/")).arg(r->name());
621
s += QString(QT_TR_NOOP(" name=\"%1\"/")).arg(Xml::xmlString(r->name()));
625
xml.etag(level--, "Route");
631
if(rwFlags() & 1) // Writable
633
//RouteList* rl = _outRoutes;
634
//for (ciRoute r = rl->begin(); r != rl->end(); ++r)
635
for (ciRoute r = _outRoutes.begin(); r != _outRoutes.end(); ++r)
637
if(!r->name().isEmpty())
639
xml.tag(level++, "Route");
641
//xml.strTag(level, "srcNode", name());
642
//if(r->channel != -1)
643
// xml.tag(level, "srcNode type=\"%d\" channel=\"%d\" name=\"%s\"", Route::JACK_MIDI_ROUTE, r->channel, name().latin1());
645
xml.tag(level, "source type=\"%d\" name=\"%s\"/", Route::JACK_MIDI_ROUTE, name().latin1());
647
//xml.strTag(level, "dstNode", r->name());
648
xml.tag(level, "dest type=\"%d\" name=\"%s\"/", r->type, r->name().latin1());
650
xml.etag(level--, "Route");
657
//---------------------------------------------------------
659
//---------------------------------------------------------
661
/* FIX: if we fail to transmit the event,
662
* we return false (indicating OK). Otherwise
663
* it seems muse will retry forever
665
bool MidiJackDevice::putMidiEvent(const MidiPlayEvent& /*event*/)
668
int give, channel = event.channel();
671
if(channel >= JACK_MIDI_CHANNELS) return false;
673
// buffer up events, because jack eats them in chunks, if
674
// the buffer is full, there isn't so much to do, than
677
give = jack_midi_out_data[channel].give;
678
if(jack_midi_out_data[channel].buffer[give*4+3]){
679
fprintf(stderr, "WARNING: muse-to-jack midi-buffer is full, channel=%u\n", channel);
682
// copy event(note-on etc..), pitch and volume
683
// see http://www.midi.org/techspecs/midimessages.php
684
switch(event.type()){
686
jack_midi_out_data[channel].buffer[give*4+0] = 0x80;
687
jack_midi_out_data[channel].buffer[give*4+1] = event.dataA() & 0x7f;
688
jack_midi_out_data[channel].buffer[give*4+2] = event.dataB() & 0x7f;
691
jack_midi_out_data[channel].buffer[give*4+0] = 0x90;
692
jack_midi_out_data[channel].buffer[give*4+1] = event.dataA() & 0x7f;
693
jack_midi_out_data[channel].buffer[give*4+2] = event.dataB() & 0x7f;
696
jack_midi_out_data[channel].buffer[give*4+0] = 0xb0;
697
jack_midi_out_data[channel].buffer[give*4+1] = event.dataA() & 0x7f;
698
jack_midi_out_data[channel].buffer[give*4+2] = event.dataB() & 0x7f;
701
jack_midi_out_data[channel].buffer[give*4+0] = 0xc0;
702
jack_midi_out_data[channel].buffer[give*4+1] = event.dataA() & 0x7f;
703
jack_midi_out_data[channel].buffer[give*4+2] = 0;
706
jack_midi_out_data[channel].buffer[give*4+0] = 0xE0;
707
// convert muse pitch-bend to midi standard
708
x = 0x2000 + event.dataA();
709
jack_midi_out_data[channel].buffer[give*4+1] = x & 0x7f;
710
jack_midi_out_data[channel].buffer[give*4+2] = (x >> 8) & 0x7f;
713
fprintf(stderr, "jack-midi-out %u WARNING: unknown event %x\n", channel, event.type());
716
jack_midi_out_data[channel].buffer[give*4+3] = 1; // mark state of this slot
717
// finally increase give position
719
if(give >= JACK_MIDI_BUFFER_SIZE){
722
jack_midi_out_data[channel].give = give;
730
//---------------------------------------------------------
732
// return false if event is delivered
733
//---------------------------------------------------------
735
bool MidiJackDevice::putEvent(int* event)
742
//---------------------------------------------------------
744
//---------------------------------------------------------
746
void MidiJackDevice::recordEvent(MidiRecordEvent& event)
748
// Set the loop number which the event came in at.
749
//if(audio->isRecording())
750
if(audio->isPlaying())
751
event.setLoopNum(audio->loopCount());
753
if (midiInputTrace) {
754
printf("Jack MidiInput: ");
758
int typ = event.type();
762
int idin = midiPorts[_port].syncInfo().idIn();
764
//---------------------------------------------------
765
// filter some SYSEX events
766
//---------------------------------------------------
768
if (typ == ME_SYSEX) {
769
const unsigned char* p = event.data();
773
//&& ((p[1] == 0x7f) || (p[1] == rxDeviceId))) {
774
&& ((p[1] == 0x7f) || (idin == 0x7f) || (p[1] == idin))) {
777
midiSeq->mmcInput(_port, p, n);
781
//mtcInputFull(p, n);
782
midiSeq->mtcInputFull(_port, p, n);
786
else if (p[0] == 0x7e) {
787
//nonRealtimeSystemSysex(p, n);
788
midiSeq->nonRealtimeSystemSysex(_port, p, n);
794
// Trigger general activity indicator detector. Sysex has no channel, don't trigger.
795
midiPorts[_port].syncInfo().trigActDetect(event.channel());
799
// process midi event input filtering and
803
processMidiInputTransformPlugins(event);
805
if (filterEvent(event, midiRecordType, false))
808
if (!applyMidiInputTransformation(event)) {
810
printf(" midi input transformation: event filtered\n");
815
// transfer noteOn events to gui for step recording and keyboard
818
if (typ == ME_NOTEON) {
819
int pv = ((event.dataA() & 0xff)<<8) + (event.dataB() & 0xff);
823
//if(_recordFifo.put(MidiPlayEvent(event)))
824
// printf("MidiJackDevice::recordEvent: fifo overflow\n");
827
// Do not bother recording if it is NOT actually being used by a port.
828
// Because from this point on, process handles things, by selected port.
832
// Split the events up into channel fifos. Special 'channel' number 17 for sysex events.
833
unsigned int ch = (typ == ME_SYSEX)? MIDI_CHANNELS : event.channel();
834
if(_recordFifo[ch].put(MidiPlayEvent(event)))
835
printf("MidiJackDevice::recordEvent: fifo channel %d overflow\n", ch);
838
//---------------------------------------------------------
840
//---------------------------------------------------------
842
void MidiJackDevice::eventReceived(jack_midi_event_t* ev)
844
MidiRecordEvent event;
847
// NOTE: From MusE-2. Not done here in Muse-1 (yet).
848
// move all events 2*segmentSize into the future to get
849
// jitterfree playback
852
// -+----------+----------+----------+-
854
// catch process play
856
// const SeqTime* st = audio->seqTime();
858
//unsigned curFrame = st->startFrame() + segmentSize;
859
// unsigned curFrame = st->lastFrameTime;
860
//int frameOffset = audio->getFrameOffset();
861
unsigned pos = audio->pos().frame();
863
//event.setTime(pos + ev->time);
864
event.setTime(extSyncFlag.value() ? lastExtMidiSyncTick : (pos + ev->time));
866
event.setChannel(*(ev->buffer) & 0xf);
867
int type = *(ev->buffer) & 0xf0;
868
int a = *(ev->buffer + 1) & 0x7f;
869
int b = *(ev->buffer + 2) & 0x7f;
875
event.setA(*(ev->buffer + 1));
876
event.setB(*(ev->buffer + 2));
880
event.setA(*(ev->buffer + 1));
884
event.setA(((b << 7) + a) - 8192);
889
int type = *(ev->buffer) & 0xff;
894
// TODO: Deal with large sysex, which are broken up into chunks!
895
// For now, do not accept if the last byte is not EOX, meaning it's a chunk with more chunks to follow.
896
if(*(((unsigned char*)ev->buffer) + ev->size - 1) != ME_SYSEX_END)
898
printf("MidiJackDevice::eventReceived sysex chunks not supported!\n");
902
//event.setTime(0); // mark as used
903
event.setType(ME_SYSEX);
904
event.setData((unsigned char*)(ev->buffer + 1), ev->size - 2);
908
midiSeq->mtcInputQuarter(_port, *(ev->buffer + 1));
912
midiSeq->setSongPosition(_port, *(ev->buffer + 1) | (*(ev->buffer + 2) >> 2 )); // LSB then MSB
923
midiSeq->realtimeSystemInput(_port, type);
929
printf("MidiJackDevice::eventReceived unsupported system event 0x%02x\n", type);
936
printf("MidiJackDevice::eventReceived unknown event 0x%02x\n", type);
937
//printf("MidiJackDevice::eventReceived unknown event 0x%02x size:%d buf:0x%02x 0x%02x 0x%02x ...0x%02x\n", type, ev->size, *(ev->buffer), *(ev->buffer + 1), *(ev->buffer + 2), *(ev->buffer + (ev->size - 1)));
941
if (midiInputTrace) {
942
printf("MidiInput<%s>: ", name().latin1());
946
#ifdef JACK_MIDI_DEBUG
947
printf("MidiJackDevice::eventReceived time:%d type:%d ch:%d A:%d B:%d\n", event.time(), event.type(), event.channel(), event.dataA(), event.dataB());
950
// Let recordEvent handle it from here, with timestamps, filtering, gui triggering etc.
954
//---------------------------------------------------------
956
//---------------------------------------------------------
958
void MidiJackDevice::collectMidiEvents()
963
if(!_client_jackport)
965
void* port_buf = jack_port_get_buffer(_client_jackport, segmentSize);
967
jack_midi_event_t event;
968
jack_nframes_t eventCount = jack_midi_get_event_count(port_buf);
969
for (jack_nframes_t i = 0; i < eventCount; ++i)
971
jack_midi_event_get(&event, port_buf, i);
973
#ifdef JACK_MIDI_DEBUG
974
printf("MidiJackDevice::collectMidiEvents number:%d time:%d\n", i, event.time);
977
eventReceived(&event);
981
//---------------------------------------------------------
983
// return true if event cannot be delivered
984
//---------------------------------------------------------
986
bool MidiJackDevice::putEvent(const MidiPlayEvent& ev)
992
#ifdef JACK_MIDI_DEBUG
993
printf("MidiJackDevice::putEvent time:%d type:%d ch:%d A:%d B:%d\n", ev.time(), ev.type(), ev.channel(), ev.dataA(), ev.dataB());
996
bool rv = eventFifo.put(ev);
998
printf("MidiJackDevice::putEvent: port overflow\n");
1003
//---------------------------------------------------------
1005
// return true if successful
1006
//---------------------------------------------------------
1008
//void JackAudioDevice::putEvent(Port port, const MidiEvent& e)
1009
bool MidiJackDevice::queueEvent(const MidiPlayEvent& e)
1010
//bool MidiJackDevice::queueEvent(const MidiPlayEvent& e)
1012
// Perhaps we can find use for this value later, together with the Jack midi MusE port(s).
1013
// No big deal if not. Not used for now.
1014
//int port = e.port();
1016
//if(port >= JACK_MIDI_CHANNELS)
1019
//if (midiOutputTrace) {
1020
// printf("MidiOut<%s>: jackMidi: ", portName(port).toLatin1().data());
1025
// printf("MidiJackDevice::queueEvent\n");
1027
if(!_client_jackport)
1029
void* pb = jack_port_get_buffer(_client_jackport, segmentSize);
1031
//unsigned frameCounter = ->frameTime();
1032
int frameOffset = audio->getFrameOffset();
1033
unsigned pos = audio->pos().frame();
1034
int ft = e.time() - frameOffset - pos;
1038
if (ft >= (int)segmentSize) {
1039
printf("MidiJackDevice::queueEvent: Event time:%d out of range. offset:%d ft:%d (seg=%d)\n", e.time(), frameOffset, ft, segmentSize);
1040
if (ft > (int)segmentSize)
1041
ft = segmentSize - 1;
1044
#ifdef JACK_MIDI_DEBUG
1045
printf("MidiJackDevice::queueEvent time:%d type:%d ch:%d A:%d B:%d\n", e.time(), e.type(), e.channel(), e.dataA(), e.dataB());
1055
#ifdef JACK_MIDI_DEBUG
1056
printf("MidiJackDevice::queueEvent note on/off polyafter controller or pitch\n");
1059
unsigned char* p = jack_midi_event_reserve(pb, ft, 3);
1061
fprintf(stderr, "MidiJackDevice::queueEvent #1: buffer overflow, event lost\n");
1064
p[0] = e.type() | e.channel();
1073
#ifdef JACK_MIDI_DEBUG
1074
printf("MidiJackDevice::queueEvent program or aftertouch\n");
1077
unsigned char* p = jack_midi_event_reserve(pb, ft, 2);
1079
fprintf(stderr, "MidiJackDevice::queueEvent #2: buffer overflow, event lost\n");
1082
p[0] = e.type() | e.channel();
1088
#ifdef JACK_MIDI_DEBUG
1089
printf("MidiJackDevice::queueEvent sysex\n");
1092
const unsigned char* data = e.data();
1094
unsigned char* p = jack_midi_event_reserve(pb, ft, len+2);
1096
fprintf(stderr, "MidiJackDevice::queueEvent #3: buffer overflow, event lost\n");
1101
memcpy(p+1, data, len);
1109
printf("MidiJackDevice::queueEvent: event type %x not supported\n", e.type());
1117
//---------------------------------------------------------
1119
//---------------------------------------------------------
1121
void MidiJackDevice::processEvent(const MidiPlayEvent& event)
1123
//int frameOffset = audio->getFrameOffset();
1124
//unsigned pos = audio->pos().frame();
1126
int chn = event.channel();
1127
unsigned t = event.time();
1128
int a = event.dataA();
1129
int b = event.dataB();
1130
// Perhaps we can find use for this value later, together with the Jack midi MusE port(s).
1131
// No big deal if not. Not used for now.
1132
int port = event.port();
1134
// TODO: No sub-tick playback resolution yet, with external sync.
1135
// Just do this 'standard midi 64T timing thing' for now until we figure out more precise external timings.
1136
// Does require relatively short audio buffers, in order to catch the resolution, but buffer <= 256 should be OK...
1137
// Tested OK so far with 128.
1138
if(extSyncFlag.value())
1139
t = audio->getFrameOffset() + audio->pos().frame();
1140
//t = frameOffset + pos;
1142
#ifdef JACK_MIDI_DEBUG
1143
printf("MidiJackDevice::processEvent time:%d type:%d ch:%d A:%d B:%d\n", event.time(), event.type(), event.channel(), event.dataA(), event.dataB());
1146
if(event.type() == ME_PROGRAM)
1148
// don't output program changes for GM drum channel
1149
//if (!(song->mtype() == MT_GM && chn == 9)) {
1150
int hb = (a >> 16) & 0xff;
1151
int lb = (a >> 8) & 0xff;
1155
//printf("MidiJackDevice::processEvent ME_PROGRAM time:%d type:%d ch:%d A:%d B:%d hb:%d lb:%d pr:%d\n",
1156
// event.time(), event.type(), event.channel(), event.dataA(), event.dataB(), hb, lb, pr);
1159
queueEvent(MidiPlayEvent(t, port, chn, ME_CONTROLLER, CTRL_HBANK, hb));
1161
queueEvent(MidiPlayEvent(t+1, port, chn, ME_CONTROLLER, CTRL_LBANK, lb));
1162
queueEvent(MidiPlayEvent(t+2, port, chn, ME_PROGRAM, pr, 0));
1166
if(event.type() == ME_PITCHBEND)
1170
//printf("MidiJackDevice::processEvent ME_PITCHBEND v:%d time:%d type:%d ch:%d A:%d B:%d\n", v, event.time(), event.type(), event.channel(), event.dataA(), event.dataB());
1172
queueEvent(MidiPlayEvent(t, port, chn, ME_PITCHBEND, v & 0x7f, (v >> 7) & 0x7f));
1175
if(event.type() == ME_CONTROLLER)
1177
//int a = event.dataA();
1178
//int b = event.dataB();
1179
// Perhaps we can find use for this value later, together with the Jack midi MusE port(s).
1180
// No big deal if not. Not used for now.
1181
//int port = event.port();
1187
int nv = midiPorts[_port].nullSendValue();
1190
nvh = (nv >> 8) & 0xff;
1199
//printf("MidiJackDevice::processEvent CTRL_PITCH v:%d time:%d type:%d ch:%d A:%d B:%d\n", v, event.time(), event.type(), event.channel(), event.dataA(), event.dataB());
1201
queueEvent(MidiPlayEvent(t, port, chn, ME_PITCHBEND, v & 0x7f, (v >> 7) & 0x7f));
1203
else if (a == CTRL_PROGRAM)
1205
// don't output program changes for GM drum channel
1206
//if (!(song->mtype() == MT_GM && chn == 9)) {
1207
int hb = (b >> 16) & 0xff;
1208
int lb = (b >> 8) & 0xff;
1212
//printf("MidiJackDevice::processEvent CTRL_PROGRAM time:%d type:%d ch:%d A:%d B:%d hb:%d lb:%d pr:%d\n",
1213
// event.time(), event.type(), event.channel(), event.dataA(), event.dataB(), hb, lb, pr);
1216
queueEvent(MidiPlayEvent(t, port, chn, ME_CONTROLLER, CTRL_HBANK, hb));
1218
queueEvent(MidiPlayEvent(t+1, port, chn, ME_CONTROLLER, CTRL_LBANK, lb));
1219
queueEvent(MidiPlayEvent(t+2, port, chn, ME_PROGRAM, pr, 0));
1223
else if (a == CTRL_MASTER_VOLUME)
1225
unsigned char sysex[] = {
1226
0x7f, 0x7f, 0x04, 0x01, 0x00, 0x00
1228
sysex[1] = deviceId();
1229
sysex[4] = b & 0x7f;
1230
sysex[5] = (b >> 7) & 0x7f;
1231
queueEvent(MidiPlayEvent(t, port, ME_SYSEX, sysex, 6));
1234
else if (a < CTRL_14_OFFSET)
1235
{ // 7 Bit Controller
1237
//queueEvent(museport, MidiPlayEvent(t, port, chn, event));
1239
else if (a < CTRL_RPN_OFFSET)
1240
{ // 14 bit high resolution controller
1241
int ctrlH = (a >> 8) & 0x7f;
1242
int ctrlL = a & 0x7f;
1243
int dataH = (b >> 7) & 0x7f;
1244
int dataL = b & 0x7f;
1245
queueEvent(MidiPlayEvent(t, port, chn, ME_CONTROLLER, ctrlH, dataH));
1246
queueEvent(MidiPlayEvent(t+1, port, chn, ME_CONTROLLER, ctrlL, dataL));
1248
else if (a < CTRL_NRPN_OFFSET)
1249
{ // RPN 7-Bit Controller
1250
int ctrlH = (a >> 8) & 0x7f;
1251
int ctrlL = a & 0x7f;
1252
queueEvent(MidiPlayEvent(t, port, chn, ME_CONTROLLER, CTRL_HRPN, ctrlH));
1253
queueEvent(MidiPlayEvent(t+1, port, chn, ME_CONTROLLER, CTRL_LRPN, ctrlL));
1254
queueEvent(MidiPlayEvent(t+2, port, chn, ME_CONTROLLER, CTRL_HDATA, b));
1257
// Select null parameters so that subsequent data controller events do not upset the last *RPN controller.
1258
//sendNullRPNParams(chn, false);
1261
queueEvent(MidiPlayEvent(t, port, chn, ME_CONTROLLER, CTRL_HRPN, nvh & 0x7f));
1265
queueEvent(MidiPlayEvent(t, port, chn, ME_CONTROLLER, CTRL_LRPN, nvl & 0x7f));
1267
//else if (a < CTRL_RPN14_OFFSET)
1268
else if (a < CTRL_INTERNAL_OFFSET)
1269
{ // NRPN 7-Bit Controller
1270
int ctrlH = (a >> 8) & 0x7f;
1271
int ctrlL = a & 0x7f;
1272
queueEvent(MidiPlayEvent(t, port, chn, ME_CONTROLLER, CTRL_HNRPN, ctrlH));
1273
queueEvent(MidiPlayEvent(t+1, port, chn, ME_CONTROLLER, CTRL_LNRPN, ctrlL));
1274
queueEvent(MidiPlayEvent(t+2, port, chn, ME_CONTROLLER, CTRL_HDATA, b));
1277
//sendNullRPNParams(chn, true);
1280
queueEvent(MidiPlayEvent(t, port, chn, ME_CONTROLLER, CTRL_HNRPN, nvh & 0x7f));
1284
queueEvent(MidiPlayEvent(t, port, chn, ME_CONTROLLER, CTRL_LNRPN, nvl & 0x7f));
1286
else if (a < CTRL_NRPN14_OFFSET)
1287
{ // RPN14 Controller
1288
int ctrlH = (a >> 8) & 0x7f;
1289
int ctrlL = a & 0x7f;
1290
int dataH = (b >> 7) & 0x7f;
1291
int dataL = b & 0x7f;
1292
queueEvent(MidiPlayEvent(t, port, chn, ME_CONTROLLER, CTRL_HRPN, ctrlH));
1293
queueEvent(MidiPlayEvent(t+1, port, chn, ME_CONTROLLER, CTRL_LRPN, ctrlL));
1294
queueEvent(MidiPlayEvent(t+2, port, chn, ME_CONTROLLER, CTRL_HDATA, dataH));
1295
queueEvent(MidiPlayEvent(t+3, port, chn, ME_CONTROLLER, CTRL_LDATA, dataL));
1298
//sendNullRPNParams(chn, false);
1301
queueEvent(MidiPlayEvent(t, port, chn, ME_CONTROLLER, CTRL_HRPN, nvh & 0x7f));
1305
queueEvent(MidiPlayEvent(t, port, chn, ME_CONTROLLER, CTRL_LRPN, nvl & 0x7f));
1307
else if (a < CTRL_NONE_OFFSET)
1308
{ // NRPN14 Controller
1309
int ctrlH = (a >> 8) & 0x7f;
1310
int ctrlL = a & 0x7f;
1311
int dataH = (b >> 7) & 0x7f;
1312
int dataL = b & 0x7f;
1313
queueEvent(MidiPlayEvent(t, port, chn, ME_CONTROLLER, CTRL_HNRPN, ctrlH));
1314
queueEvent(MidiPlayEvent(t+1, port, chn, ME_CONTROLLER, CTRL_LNRPN, ctrlL));
1315
queueEvent(MidiPlayEvent(t+2, port, chn, ME_CONTROLLER, CTRL_HDATA, dataH));
1316
queueEvent(MidiPlayEvent(t+3, port, chn, ME_CONTROLLER, CTRL_LDATA, dataL));
1319
//sendNullRPNParams(chn, true);
1322
queueEvent(MidiPlayEvent(t, port, chn, ME_CONTROLLER, CTRL_HNRPN, nvh & 0x7f));
1326
queueEvent(MidiPlayEvent(t, port, chn, ME_CONTROLLER, CTRL_LNRPN, nvl & 0x7f));
1330
printf("MidiJackDevice::processEvent: unknown controller type 0x%x\n", a);
1336
//queueEvent(MidiPlayEvent(t, port, chn, event));
1340
//---------------------------------------------------------
1341
// processMidi called from audio process only.
1342
//---------------------------------------------------------
1344
void MidiJackDevice::processMidi()
1346
if(!_client_jackport)
1348
void* port_buf = jack_port_get_buffer(_client_jackport, segmentSize);
1349
jack_midi_clear_buffer(port_buf);
1351
while(!eventFifo.isEmpty())
1353
MidiPlayEvent e(eventFifo.get());
1354
int evTime = e.time();
1355
// Is event marked to be played immediately?
1358
// Nothing to do but stamp the event to be queued for frame 0+.
1359
//e.setTime(frameOffset + pos);
1360
e.setTime(audio->getFrameOffset() + audio->pos().frame());
1363
#ifdef JACK_MIDI_DEBUG
1364
printf("MidiJackDevice::processMidi eventFifo time:%d type:%d ch:%d A:%d B:%d\n", e.time(), e.type(), e.channel(), e.dataA(), e.dataB());
1367
//el->insert(eventFifo.get());
1372
MPEventList* el = playEvents();
1376
iMPEvent i = nextPlayEvent();
1377
for(; i != el->end(); ++i)
1379
// p3.3.39 Update hardware state so knobs and boxes are updated. Optimize to avoid re-setting existing values.
1380
// Same code as in MidiPort::sendEvent()
1383
MidiPort* mp = &midiPorts[_port];
1384
if(i->type() == ME_CONTROLLER)
1386
int da = i->dataA();
1387
int db = i->dataB();
1388
db = mp->limitValToInstrCtlRange(da, db);
1389
if(!mp->setHwCtrlState(i->channel(), da, db))
1391
//mp->setHwCtrlState(i->channel(), da, db);
1394
if(i->type() == ME_PITCHBEND)
1397
//printf("MidiJackDevice::processMidi playEvents ME_PITCHBEND time:%d type:%d ch:%d A:%d B:%d\n", (*i).time(), (*i).type(), (*i).channel(), (*i).dataA(), (*i).dataB());
1399
int da = mp->limitValToInstrCtlRange(CTRL_PITCH, i->dataA());
1400
if(!mp->setHwCtrlState(i->channel(), CTRL_PITCH, da))
1402
//mp->setHwCtrlState(i->channel(), CTRL_PITCH, da);
1404
//(MidiPlayEvent(t, port, chn, ME_PITCHBEND, v & 0x7f, (v >> 7) & 0x7f));
1407
if(i->type() == ME_PROGRAM)
1409
if(!mp->setHwCtrlState(i->channel(), CTRL_PROGRAM, i->dataA()))
1411
//mp->setHwCtrlState(i->channel(), CTRL_PROGRAM, i->dataA());
1418
setNextPlayEvent(i);
1421
//---------------------------------------------------------
1423
// return true on error
1424
//---------------------------------------------------------
1431
memset(jack_midi_out_data, 0, JACK_MIDI_CHANNELS * sizeof(muse_jack_midi_buffer));
1432
memset(jack_midi_in_data, 0, JACK_MIDI_CHANNELS * sizeof(muse_jack_midi_buffer));
1434
MidiJackDevice* dev = new MidiJackDevice(adr, QString("jack-midi"));
1435
dev->setrwFlags(3); // set read and write flags
1437
if(pipe(jackmidi_pi) < 0){
1438
fprintf(stderr, "cant create midi-jack input pipe\n");
1440
if(pipe(jackmidi_po) < 0){
1441
fprintf(stderr, "cant create midi-jack output pipe\n");
1444
midiDevices.add(dev);
1446
gmdev = dev; // proclaim the global jack-midi instance
1448
//jackScanMidiPorts();
1460
//JackPort(int a, const char* s, int f) {
1461
JackPort(int a, const QString& s, int f) {
1470
static std::list<JackPort> portList;
1472
//---------------------------------------------------------
1473
// jackScanMidiPorts
1474
//---------------------------------------------------------
1476
void jackScanMidiPorts()
1483
name = strdup("namex");
1484
portList.push_back(JackPort(adr, name, 0));
1486
// check for devices to add
1488
for (std::list<JackPort>::iterator k = portList.begin(); k != portList.end(); ++k) {
1489
iMidiDevice i = midiDevices.begin();
1490
for (;i != midiDevices.end(); ++i) {
1491
//MidiJackDevice* d = dynamic_cast<MidiJackDevice*>(*i);
1493
//if (d == 0) continue;
1494
//if ((k->adr.client == d->adr.client) && (k->adr.port == d->adr.port)) {
1498
if (i == midiDevices.end()) {
1500
MidiJackDevice* dev = new MidiJackDevice(k->adr, QString(k->name));
1501
dev->setrwFlags(k->flags);
1502
midiDevices.add(dev);
1509
//---------------------------------------------------------
1511
//---------------------------------------------------------
1512
static void handle_jack_midi_in(int channel)
1514
MidiRecordEvent event;
1516
t = jack_midi_in_data[channel].buffer[0];
1517
n = jack_midi_in_data[channel].buffer[1];
1518
v = jack_midi_in_data[channel].buffer[2];
1520
event.setType(0); // mark as unused
1521
event.setPort(gmdev->midiPort());
1524
if(t == 0x90){ // note on
1525
fprintf(stderr, "jackProcessMidiInput note-on\n");
1526
event.setChannel(channel);
1527
event.setType(ME_NOTEON);
1530
}else if (t == 0x80){ // note off
1531
fprintf(stderr, "jackProcessMidiInput note-off\n");
1532
event.setChannel(channel);
1533
event.setType(ME_NOTEOFF);
1537
fprintf(stderr, "WARNING: unknown midi-in on channel %d: %x,%x,%x\n",
1542
gmdev->recordEvent(event);
1543
midiPorts[gmdev->midiPort()].syncInfo().trigActDetect(event.channel());
1547
void MidiJackDevice::processInput()
1551
read(gmdev->selectRfd(), &buf, 1);
1554
for(i = 0; i < JACK_MIDI_CHANNELS; i++){
1555
if(jack_midi_in_data[i].buffer[3]){
1557
handle_jack_midi_in(i);
1558
jack_midi_in_data[i].buffer[3] = 0;
b'\\ No newline at end of file'