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
History | View | Annotate | Download (3.35 KB)
| 1 |
/*
|
|---|---|
| 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 |
#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 |
// Exception thrown by MIDI system
|
| 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 |
/**
|
| 54 |
* A MIDI message
|
| 55 |
*/
|
| 56 |
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 |
/**
|
| 68 |
* First byte of MIDI data
|
| 69 |
*/
|
| 70 |
unsigned char getData_1() { return mData1; } |
| 71 |
|
| 72 |
/**
|
| 73 |
* Second byte of MIDI data
|
| 74 |
*/
|
| 75 |
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 |
/**
|
| 88 |
* Handles MIDI messages from the keyboards and Teensy. It uses RtMidi library.
|
| 89 |
*
|
| 90 |
*/
|
| 91 |
class MIDI |
| 92 |
{
|
| 93 |
|
| 94 |
public:
|
| 95 |
|
| 96 |
MIDI(); |
| 97 |
~MIDI(); |
| 98 |
|
| 99 |
void setup( const Config& ); |
| 100 |
|
| 101 |
/**
|
| 102 |
* Check new incoming messages and stores them into the vector passed as argument by reference.
|
| 103 |
*/
|
| 104 |
void checkMessages( std::vector< MIDIMessage >& );
|
| 105 |
|
| 106 |
private:
|
| 107 |
|
| 108 |
// callback passed to RtMidi library
|
| 109 |
static void RtMidiInCallback( double deltatime, std::vector<unsigned char> *message, void *userData ); |
| 110 |
|
| 111 |
// parse RtMidi messages and turns them into more readable collidoscope::MIDIMessages
|
| 112 |
MIDIMessage parseRtMidiMessage( std::vector<unsigned char> *message ); |
| 113 |
|
| 114 |
// messages to pass to checkMessages caller
|
| 115 |
std::vector< MIDIMessage > mMIDIMessages; |
| 116 |
// 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 |
std::array< MIDIMessage, NUM_WAVES > mPitchBendMessages; |
| 121 |
// Same principle as mPitchBendMessages
|
| 122 |
std::array< MIDIMessage, NUM_WAVES > mFilterMessages; |
| 123 |
|
| 124 |
// vector containing all the MIDI input devices detected.
|
| 125 |
std::vector< std::unique_ptr <RtMidiIn> > mInputs; |
| 126 |
// Used for mutual access to the MIDI messages by the MIDI thread and the graphic thread.
|
| 127 |
std::mutex mMutex; |
| 128 |
}; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
} // collidsocope }
|