Chris@562
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@562
|
2
|
Chris@562
|
3 /*
|
Chris@562
|
4 Sonic Visualiser
|
Chris@562
|
5 An audio file viewer and annotation editor.
|
Chris@562
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@562
|
7 This file copyright 2006-2009 Chris Cannam and QMUL.
|
Chris@562
|
8
|
Chris@562
|
9 This program is free software; you can redistribute it and/or
|
Chris@562
|
10 modify it under the terms of the GNU General Public License as
|
Chris@562
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@562
|
12 License, or (at your option) any later version. See the file
|
Chris@562
|
13 COPYING included with this distribution for more information.
|
Chris@562
|
14 */
|
Chris@562
|
15
|
Chris@562
|
16 #include "MIDIInput.h"
|
Chris@562
|
17
|
Chris@562
|
18 #include "rtmidi/RtMidi.h"
|
Chris@562
|
19
|
Chris@1219
|
20 #include "system/System.h"
|
Chris@608
|
21
|
Chris@567
|
22 MIDIInput::MIDIInput(QString name, FrameTimer *timer) :
|
Chris@1397
|
23 m_rtmidi(0),
|
Chris@567
|
24 m_frameTimer(timer),
|
Chris@562
|
25 m_buffer(1023)
|
Chris@562
|
26 {
|
Chris@562
|
27 try {
|
Chris@1397
|
28 std::vector<RtMidi::Api> apis;
|
Chris@1397
|
29 RtMidi::getCompiledApi(apis);
|
Chris@1397
|
30 if (apis.empty()) {
|
Chris@1397
|
31 SVDEBUG << "MIDIInput: No RtMidi APIs compiled in" << endl;
|
Chris@1397
|
32 } else {
|
Chris@1397
|
33 m_rtmidi = new RtMidiIn(apis[0], name.toStdString());
|
Chris@1397
|
34 m_rtmidi->setCallback(staticCallback, this);
|
Chris@1397
|
35 m_rtmidi->openPort(0, tr("Input").toStdString());
|
Chris@1397
|
36 }
|
Chris@1397
|
37 } catch (RtMidiError e) {
|
Chris@562
|
38 e.printMessage();
|
Chris@562
|
39 delete m_rtmidi;
|
Chris@562
|
40 m_rtmidi = 0;
|
Chris@562
|
41 }
|
Chris@562
|
42 }
|
Chris@562
|
43
|
Chris@562
|
44 MIDIInput::~MIDIInput()
|
Chris@562
|
45 {
|
Chris@562
|
46 delete m_rtmidi;
|
Chris@562
|
47 }
|
Chris@562
|
48
|
Chris@562
|
49 void
|
Chris@562
|
50 MIDIInput::staticCallback(double timestamp, std::vector<unsigned char> *message,
|
Chris@562
|
51 void *userData)
|
Chris@562
|
52 {
|
Chris@562
|
53 ((MIDIInput *)userData)->callback(timestamp, message);
|
Chris@562
|
54 }
|
Chris@562
|
55
|
Chris@562
|
56 void
|
Chris@562
|
57 MIDIInput::callback(double timestamp, std::vector<unsigned char> *message)
|
Chris@562
|
58 {
|
Chris@690
|
59 SVDEBUG << "MIDIInput::callback(" << timestamp << ")" << endl;
|
Chris@567
|
60 // In my experience so far, the timings passed to this function
|
Chris@567
|
61 // are not reliable enough to use. We request instead an audio
|
Chris@567
|
62 // frame time from whatever FrameTimer we have been given, and use
|
Chris@567
|
63 // that as the event time.
|
Chris@566
|
64 if (!message || message->empty()) return;
|
Chris@567
|
65 unsigned long t = m_frameTimer->getFrame();
|
Chris@569
|
66 MIDIByte code = (*message)[0];
|
Chris@567
|
67 MIDIEvent ev(t,
|
Chris@569
|
68 code,
|
Chris@566
|
69 message->size() > 1 ? (*message)[1] : 0,
|
Chris@566
|
70 message->size() > 2 ? (*message)[2] : 0);
|
Chris@566
|
71 postEvent(ev);
|
Chris@562
|
72 }
|
Chris@562
|
73
|
Chris@562
|
74 MIDIEvent
|
Chris@562
|
75 MIDIInput::readEvent()
|
Chris@562
|
76 {
|
Chris@562
|
77 MIDIEvent *event = m_buffer.readOne();
|
Chris@562
|
78 MIDIEvent revent = *event;
|
Chris@562
|
79 delete event;
|
Chris@562
|
80 return revent;
|
Chris@562
|
81 }
|
Chris@562
|
82
|
Chris@562
|
83 void
|
Chris@562
|
84 MIDIInput::postEvent(MIDIEvent e)
|
Chris@562
|
85 {
|
Chris@562
|
86 int count = 0, max = 5;
|
Chris@562
|
87 while (m_buffer.getWriteSpace() == 0) {
|
Chris@562
|
88 if (count == max) {
|
Chris@843
|
89 cerr << "ERROR: MIDIInput::postEvent: MIDI event queue is full and not clearing -- abandoning incoming event" << endl;
|
Chris@562
|
90 return;
|
Chris@562
|
91 }
|
Chris@843
|
92 cerr << "WARNING: MIDIInput::postEvent: MIDI event queue (capacity " << m_buffer.getSize() << " is full!" << endl;
|
Chris@690
|
93 SVDEBUG << "Waiting for something to be processed" << endl;
|
Chris@562
|
94 #ifdef _WIN32
|
Chris@562
|
95 Sleep(1);
|
Chris@562
|
96 #else
|
Chris@562
|
97 sleep(1);
|
Chris@562
|
98 #endif
|
Chris@562
|
99 count++;
|
Chris@562
|
100 }
|
Chris@562
|
101
|
Chris@562
|
102 MIDIEvent *me = new MIDIEvent(e);
|
Chris@562
|
103 m_buffer.write(&me, 1);
|
Chris@562
|
104 emit eventsAvailable();
|
Chris@562
|
105 }
|
Chris@562
|
106
|