comparison data/midi/MIDIInput.cpp @ 562:ecce042cc374

* Wire up MIDI input port (as far as printing a message when an event comes in, anyway)
author Chris Cannam
date Mon, 23 Feb 2009 17:50:17 +0000
parents
children 3086876472dc
comparison
equal deleted inserted replaced
561:863ad4b1f841 562:ecce042cc374
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006-2009 Chris Cannam and QMUL.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
14 */
15
16 #include "MIDIInput.h"
17
18 #include "rtmidi/RtMidi.h"
19
20 MIDIInput::MIDIInput(QString name) :
21 m_rtmidi(),
22 m_buffer(1023)
23 {
24 try {
25 m_rtmidi = new RtMidiIn(name.toStdString());
26 m_rtmidi->setCallback(staticCallback, this);
27 m_rtmidi->openPort();
28 } catch (RtError e) {
29 e.printMessage();
30 delete m_rtmidi;
31 m_rtmidi = 0;
32 }
33 }
34
35 MIDIInput::~MIDIInput()
36 {
37 delete m_rtmidi;
38 }
39
40 void
41 MIDIInput::staticCallback(double timestamp, std::vector<unsigned char> *message,
42 void *userData)
43 {
44 ((MIDIInput *)userData)->callback(timestamp, message);
45 }
46
47 void
48 MIDIInput::callback(double timestamp, std::vector<unsigned char> *message)
49 {
50 std::cerr << "MIDIInput::callback(" << timestamp << ")" << std::endl;
51
52 }
53
54 MIDIEvent
55 MIDIInput::readEvent()
56 {
57 MIDIEvent *event = m_buffer.readOne();
58 MIDIEvent revent = *event;
59 delete event;
60 return revent;
61 }
62
63 void
64 MIDIInput::postEvent(MIDIEvent e)
65 {
66 int count = 0, max = 5;
67 while (m_buffer.getWriteSpace() == 0) {
68 if (count == max) {
69 std::cerr << "ERROR: MIDIInput::postEvent: MIDI event queue is full and not clearing -- abandoning incoming event" << std::endl;
70 return;
71 }
72 std::cerr << "WARNING: MIDIInput::postEvent: MIDI event queue (capacity " << m_buffer.getSize() << " is full!" << std::endl;
73 std::cerr << "Waiting for something to be processed" << std::endl;
74 #ifdef _WIN32
75 Sleep(1);
76 #else
77 sleep(1);
78 #endif
79 count++;
80 }
81
82 MIDIEvent *me = new MIDIEvent(e);
83 m_buffer.write(&me, 1);
84 emit eventsAvailable();
85 }
86