To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / CollidoscopeApp / include / MIDI.h @ 3:7fb593d53361

History | View | Annotate | Download (2.59 KB)

1 0:02467299402e f
#pragma once
2
3
#include "RtMidi.h"
4
#include <memory>
5
#include <mutex>
6
#include <array>
7
8
class Config;
9
10
11
namespace collidoscope {
12
13
14
class MIDIException : public std::exception
15
{
16
public:
17
18
    MIDIException( std::string message ) : mMessage( message ) {}
19
20
    virtual const std::string& getMessage( void ) const { return mMessage; }
21
22
#ifdef _WINDOWS
23
    const char* what() const override { return mMessage.c_str(); }
24
#else
25
    const char* what() const noexcept override { return mMessage.c_str(); }
26
#endif
27
28
protected:
29
    std::string mMessage;
30
};
31
32 3:7fb593d53361 f
/**
33
 * A MIDI message
34
 */
35 0:02467299402e f
class MIDIMessage
36
{
37
    friend class MIDI;
38
public:
39
40
    enum class Voice { eNoteOn, eNoteOff, ePitchBend, eControlChange, eIgnore };
41
42
    Voice getVoice() { return mVoice; }
43
44
    unsigned char getChannel() { return mChannel; }
45
46 3:7fb593d53361 f
    /**
47
     * First byte of MIDI data
48
     */
49 0:02467299402e f
    unsigned char getData_1() { return mData1; }
50
51 3:7fb593d53361 f
    /**
52
     * Second byte of MIDI data
53
     */
54 0:02467299402e f
    unsigned char getData_2() { return mData2; }
55
56
private:
57
58
    Voice mVoice = Voice::eIgnore;
59
    unsigned char mChannel;
60
    unsigned char mData1;
61
    unsigned char mData2;
62
63
64
};
65
66 3:7fb593d53361 f
/**
67
 * Handles MIDI messages from the keyboards and Teensy. It uses RtMidi library.
68
 *
69
 */
70 0:02467299402e f
class MIDI
71
{
72
73
public:
74
75
    MIDI();
76
    ~MIDI();
77
78
    void setup( const Config& );
79
80 3:7fb593d53361 f
    /**
81
     * Check new incoming messages and stores them into the vector passed as argument by reference.
82
     */
83 0:02467299402e f
    void checkMessages( std::vector< MIDIMessage >&  );
84
85
private:
86
87 3:7fb593d53361 f
    // callback passed to RtMidi library
88 0:02467299402e f
    static void RtMidiInCallback( double deltatime, std::vector<unsigned char> *message, void *userData );
89
90 3:7fb593d53361 f
    // parse RtMidi messages and turns them into more readable collidoscope::MIDIMessages
91 0:02467299402e f
    MIDIMessage parseRtMidiMessage( std::vector<unsigned char> *message );
92
93 3:7fb593d53361 f
    // messages to pass to checkMessages caller
94 0:02467299402e f
    std::vector< MIDIMessage > mMIDIMessages;
95 3:7fb593d53361 f
    // use specific variables for pitch bend messages. Pitch bend messages are coming
96
    // from the strip sensors that are very jerky and send a lot of values. So instead
97
    // of saving all the messages in mMIDIMessages just save the last received in mPitchBendMessages
98
    // and optimize away redundant messages.
99 0:02467299402e f
    std::array< MIDIMessage, NUM_WAVES > mPitchBendMessages;
100 3:7fb593d53361 f
    // Same principle of pitch bend messages
101 0:02467299402e f
    std::array< MIDIMessage, NUM_WAVES > mFilterMessages;
102
103 3:7fb593d53361 f
    // vecotr containing all the MIDI input devices detected.
104 0:02467299402e f
    std::vector< std::unique_ptr <RtMidiIn> > mInputs;
105 3:7fb593d53361 f
    // Used for mutual access to the MIDI messages by the MIDI thread and the graphic thread.
106 0:02467299402e f
    std::mutex mMutex;
107
};
108
109
110
111
}  // collidsocope }