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 @ 4:ab6db404403a

History | View | Annotate | Download (2.59 KB)

1
#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
/**
33
 * A MIDI message 
34
 */ 
35
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
    /**
47
     * First byte of MIDI data 
48
     */ 
49
    unsigned char getData_1() { return mData1; }
50

    
51
    /**
52
     * Second byte of MIDI data 
53
     */ 
54
    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
/**
67
 * Handles MIDI messages from the keyboards and Teensy. It uses RtMidi library.
68
 *
69
 */ 
70
class MIDI
71
{
72

    
73
public:
74

    
75
    MIDI();
76
    ~MIDI();
77

    
78
    void setup( const Config& );
79

    
80
    /**
81
     * Check new incoming messages and stores them into the vector passed as argument by reference.
82
     */ 
83
    void checkMessages( std::vector< MIDIMessage >&  );
84

    
85
private:
86

    
87
    // callback passed to RtMidi library 
88
    static void RtMidiInCallback( double deltatime, std::vector<unsigned char> *message, void *userData );
89

    
90
    // parse RtMidi messages and turns them into more readable collidoscope::MIDIMessages
91
    MIDIMessage parseRtMidiMessage( std::vector<unsigned char> *message );
92

    
93
    // messages to pass to checkMessages caller 
94
    std::vector< MIDIMessage > mMIDIMessages;
95
    // 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
    std::array< MIDIMessage, NUM_WAVES > mPitchBendMessages;
100
    // Same principle of pitch bend messages 
101
    std::array< MIDIMessage, NUM_WAVES > mFilterMessages;
102

    
103
    // vecotr containing all the MIDI input devices detected.
104
    std::vector< std::unique_ptr <RtMidiIn> > mInputs;
105
    // Used for mutual access to the MIDI messages by the MIDI thread and the graphic thread.  
106
    std::mutex mMutex;
107
};
108

    
109

    
110

    
111
}  // collidsocope }