To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / CollidoscopeApp / include / MIDI.h @ 5:75b744078d66
History | View | Annotate | Download (3.32 KB)
| 1 | 5:75b744078d66 | f | /*
|
|---|---|---|---|
| 2 | |||
| 3 | Copyright (C) 2016 Queen Mary University of London
|
||
| 4 | Author: Fiore Martin
|
||
| 5 | |||
| 6 | This file is part of Collidoscope.
|
||
| 7 | |||
| 8 | Collidoscope is free software: you can redistribute it and/or modify
|
||
| 9 | it under the terms of the GNU General Public License as published by
|
||
| 10 | the Free Software Foundation, either version 3 of the License, or
|
||
| 11 | (at your option) any later version.
|
||
| 12 | |||
| 13 | This program is distributed in the hope that it will be useful,
|
||
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
| 16 | GNU General Public License for more details.
|
||
| 17 | |||
| 18 | You should have received a copy of the GNU General Public License
|
||
| 19 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
| 20 | */
|
||
| 21 | |||
| 22 | 0:02467299402e | f | #pragma once
|
| 23 | |||
| 24 | #include "RtMidi.h" |
||
| 25 | #include <memory> |
||
| 26 | #include <mutex> |
||
| 27 | #include <array> |
||
| 28 | |||
| 29 | class Config; |
||
| 30 | |||
| 31 | |||
| 32 | namespace collidoscope {
|
||
| 33 | |||
| 34 | |||
| 35 | class MIDIException : public std::exception |
||
| 36 | {
|
||
| 37 | public:
|
||
| 38 | |||
| 39 | MIDIException( std::string message ) : mMessage( message ) {}
|
||
| 40 | |||
| 41 | virtual const std::string& getMessage( void ) const { return mMessage; } |
||
| 42 | |||
| 43 | #ifdef _WINDOWS
|
||
| 44 | const char* what() const override { return mMessage.c_str(); } |
||
| 45 | #else
|
||
| 46 | const char* what() const noexcept override { return mMessage.c_str(); } |
||
| 47 | #endif
|
||
| 48 | |||
| 49 | protected:
|
||
| 50 | std::string mMessage; |
||
| 51 | }; |
||
| 52 | |||
| 53 | 3:7fb593d53361 | f | /**
|
| 54 | * A MIDI message
|
||
| 55 | */
|
||
| 56 | 0:02467299402e | f | class MIDIMessage |
| 57 | {
|
||
| 58 | friend class MIDI; |
||
| 59 | public:
|
||
| 60 | |||
| 61 | enum class Voice { eNoteOn, eNoteOff, ePitchBend, eControlChange, eIgnore };
|
||
| 62 | |||
| 63 | Voice getVoice() { return mVoice; }
|
||
| 64 | |||
| 65 | unsigned char getChannel() { return mChannel; } |
||
| 66 | |||
| 67 | 3:7fb593d53361 | f | /**
|
| 68 | * First byte of MIDI data
|
||
| 69 | */
|
||
| 70 | 0:02467299402e | f | unsigned char getData_1() { return mData1; } |
| 71 | |||
| 72 | 3:7fb593d53361 | f | /**
|
| 73 | * Second byte of MIDI data
|
||
| 74 | */
|
||
| 75 | 0:02467299402e | f | unsigned char getData_2() { return mData2; } |
| 76 | |||
| 77 | private:
|
||
| 78 | |||
| 79 | Voice mVoice = Voice::eIgnore; |
||
| 80 | unsigned char mChannel; |
||
| 81 | unsigned char mData1; |
||
| 82 | unsigned char mData2; |
||
| 83 | |||
| 84 | |||
| 85 | }; |
||
| 86 | |||
| 87 | 3:7fb593d53361 | f | /**
|
| 88 | * Handles MIDI messages from the keyboards and Teensy. It uses RtMidi library.
|
||
| 89 | *
|
||
| 90 | */
|
||
| 91 | 0:02467299402e | f | class MIDI |
| 92 | {
|
||
| 93 | |||
| 94 | public:
|
||
| 95 | |||
| 96 | MIDI(); |
||
| 97 | ~MIDI(); |
||
| 98 | |||
| 99 | void setup( const Config& ); |
||
| 100 | |||
| 101 | 3:7fb593d53361 | f | /**
|
| 102 | * Check new incoming messages and stores them into the vector passed as argument by reference.
|
||
| 103 | */
|
||
| 104 | 0:02467299402e | f | void checkMessages( std::vector< MIDIMessage >& );
|
| 105 | |||
| 106 | private:
|
||
| 107 | |||
| 108 | 3:7fb593d53361 | f | // callback passed to RtMidi library
|
| 109 | 0:02467299402e | f | static void RtMidiInCallback( double deltatime, std::vector<unsigned char> *message, void *userData ); |
| 110 | |||
| 111 | 3:7fb593d53361 | f | // parse RtMidi messages and turns them into more readable collidoscope::MIDIMessages
|
| 112 | 0:02467299402e | f | MIDIMessage parseRtMidiMessage( std::vector<unsigned char> *message ); |
| 113 | |||
| 114 | 3:7fb593d53361 | f | // messages to pass to checkMessages caller
|
| 115 | 0:02467299402e | f | std::vector< MIDIMessage > mMIDIMessages; |
| 116 | 3:7fb593d53361 | f | // use specific variables for pitch bend messages. Pitch bend messages are coming
|
| 117 | // from the strip sensors that are very jerky and send a lot of values. So instead
|
||
| 118 | // of saving all the messages in mMIDIMessages just save the last received in mPitchBendMessages
|
||
| 119 | // and optimize away redundant messages.
|
||
| 120 | 0:02467299402e | f | std::array< MIDIMessage, NUM_WAVES > mPitchBendMessages; |
| 121 | 3:7fb593d53361 | f | // Same principle of pitch bend messages
|
| 122 | 0:02467299402e | f | std::array< MIDIMessage, NUM_WAVES > mFilterMessages; |
| 123 | |||
| 124 | 3:7fb593d53361 | f | // vecotr containing all the MIDI input devices detected.
|
| 125 | 0:02467299402e | f | std::vector< std::unique_ptr <RtMidiIn> > mInputs; |
| 126 | 3:7fb593d53361 | f | // Used for mutual access to the MIDI messages by the MIDI thread and the graphic thread.
|
| 127 | 0:02467299402e | f | std::mutex mMutex; |
| 128 | }; |
||
| 129 | |||
| 130 | |||
| 131 | |||
| 132 | } // collidsocope } |