comparison data/midi/MIDIInput.cpp @ 567:e6d35670e1df

* Somewhat better MIDI-based time instant timing
author Chris Cannam
date Wed, 25 Feb 2009 11:15:22 +0000
parents e2281de2361c
children 9773aadbae0c
comparison
equal deleted inserted replaced
566:e2281de2361c 567:e6d35670e1df
15 15
16 #include "MIDIInput.h" 16 #include "MIDIInput.h"
17 17
18 #include "rtmidi/RtMidi.h" 18 #include "rtmidi/RtMidi.h"
19 19
20 MIDIInput::MIDIInput(QString name) : 20 MIDIInput::MIDIInput(QString name, FrameTimer *timer) :
21 m_rtmidi(), 21 m_rtmidi(),
22 m_frameTimer(timer),
22 m_buffer(1023) 23 m_buffer(1023)
23 { 24 {
24 try { 25 try {
25 m_rtmidi = new RtMidiIn(name.toStdString()); 26 m_rtmidi = new RtMidiIn(name.toStdString());
26 m_rtmidi->setCallback(staticCallback, this); 27 m_rtmidi->setCallback(staticCallback, this);
46 47
47 void 48 void
48 MIDIInput::callback(double timestamp, std::vector<unsigned char> *message) 49 MIDIInput::callback(double timestamp, std::vector<unsigned char> *message)
49 { 50 {
50 std::cerr << "MIDIInput::callback(" << timestamp << ")" << std::endl; 51 std::cerr << "MIDIInput::callback(" << timestamp << ")" << std::endl;
51 unsigned long deltaTime = 0; 52 // In my experience so far, the timings passed to this function
52 if (timestamp > 0) deltaTime = (unsigned long)(timestamp * 100000); //!!! for now! 53 // are not reliable enough to use. We request instead an audio
54 // frame time from whatever FrameTimer we have been given, and use
55 // that as the event time.
53 if (!message || message->empty()) return; 56 if (!message || message->empty()) return;
54 MIDIEvent ev(deltaTime, 57 unsigned long t = m_frameTimer->getFrame();
58 MIDIEvent ev(t,
55 (*message)[0], 59 (*message)[0],
56 message->size() > 1 ? (*message)[1] : 0, 60 message->size() > 1 ? (*message)[1] : 0,
57 message->size() > 2 ? (*message)[2] : 0); 61 message->size() > 2 ? (*message)[2] : 0);
58 postEvent(ev); 62 postEvent(ev);
59 } 63 }