annotate 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
rev   line source
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@562 20 MIDIInput::MIDIInput(QString name) :
Chris@562 21 m_rtmidi(),
Chris@562 22 m_buffer(1023)
Chris@562 23 {
Chris@562 24 try {
Chris@562 25 m_rtmidi = new RtMidiIn(name.toStdString());
Chris@562 26 m_rtmidi->setCallback(staticCallback, this);
Chris@562 27 m_rtmidi->openPort();
Chris@562 28 } catch (RtError e) {
Chris@562 29 e.printMessage();
Chris@562 30 delete m_rtmidi;
Chris@562 31 m_rtmidi = 0;
Chris@562 32 }
Chris@562 33 }
Chris@562 34
Chris@562 35 MIDIInput::~MIDIInput()
Chris@562 36 {
Chris@562 37 delete m_rtmidi;
Chris@562 38 }
Chris@562 39
Chris@562 40 void
Chris@562 41 MIDIInput::staticCallback(double timestamp, std::vector<unsigned char> *message,
Chris@562 42 void *userData)
Chris@562 43 {
Chris@562 44 ((MIDIInput *)userData)->callback(timestamp, message);
Chris@562 45 }
Chris@562 46
Chris@562 47 void
Chris@562 48 MIDIInput::callback(double timestamp, std::vector<unsigned char> *message)
Chris@562 49 {
Chris@562 50 std::cerr << "MIDIInput::callback(" << timestamp << ")" << std::endl;
Chris@562 51
Chris@562 52 }
Chris@562 53
Chris@562 54 MIDIEvent
Chris@562 55 MIDIInput::readEvent()
Chris@562 56 {
Chris@562 57 MIDIEvent *event = m_buffer.readOne();
Chris@562 58 MIDIEvent revent = *event;
Chris@562 59 delete event;
Chris@562 60 return revent;
Chris@562 61 }
Chris@562 62
Chris@562 63 void
Chris@562 64 MIDIInput::postEvent(MIDIEvent e)
Chris@562 65 {
Chris@562 66 int count = 0, max = 5;
Chris@562 67 while (m_buffer.getWriteSpace() == 0) {
Chris@562 68 if (count == max) {
Chris@562 69 std::cerr << "ERROR: MIDIInput::postEvent: MIDI event queue is full and not clearing -- abandoning incoming event" << std::endl;
Chris@562 70 return;
Chris@562 71 }
Chris@562 72 std::cerr << "WARNING: MIDIInput::postEvent: MIDI event queue (capacity " << m_buffer.getSize() << " is full!" << std::endl;
Chris@562 73 std::cerr << "Waiting for something to be processed" << std::endl;
Chris@562 74 #ifdef _WIN32
Chris@562 75 Sleep(1);
Chris@562 76 #else
Chris@562 77 sleep(1);
Chris@562 78 #endif
Chris@562 79 count++;
Chris@562 80 }
Chris@562 81
Chris@562 82 MIDIEvent *me = new MIDIEvent(e);
Chris@562 83 m_buffer.write(&me, 1);
Chris@562 84 emit eventsAvailable();
Chris@562 85 }
Chris@562 86