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: | Revision:

root / MIDIEvent.h @ 1:3e65e0344413

History | View | Annotate | Download (8.45 KB)

1
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2

    
3
/*
4
   This is a modified version of a source file from the 
5
   Rosegarden MIDI and audio sequencer and notation editor.
6
   This file copyright 2000-2006 Richard Bown and Chris Cannam.
7
*/
8

    
9
#ifndef _MIDI_EVENT_H_
10
#define _MIDI_EVENT_H_
11

    
12
#include <QString>
13
#include <string>
14
#include <iostream>
15

    
16
typedef unsigned char MIDIByte;
17

    
18
namespace MIDIConstants
19
{
20
    static const char *const MIDI_FILE_HEADER         = "MThd";
21
    static const char *const MIDI_TRACK_HEADER        = "MTrk";
22

    
23
    static const MIDIByte MIDI_STATUS_BYTE_MASK       = 0x80;
24
    static const MIDIByte MIDI_MESSAGE_TYPE_MASK      = 0xF0;
25
    static const MIDIByte MIDI_CHANNEL_NUM_MASK       = 0x0F;
26

    
27
    static const MIDIByte MIDI_NOTE_OFF               = 0x80;
28
    static const MIDIByte MIDI_NOTE_ON                = 0x90;
29
    static const MIDIByte MIDI_POLY_AFTERTOUCH        = 0xA0;
30
    static const MIDIByte MIDI_CTRL_CHANGE            = 0xB0;
31
    static const MIDIByte MIDI_PROG_CHANGE            = 0xC0;
32
    static const MIDIByte MIDI_CHNL_AFTERTOUCH        = 0xD0;
33
    static const MIDIByte MIDI_PITCH_BEND             = 0xE0;
34
    static const MIDIByte MIDI_SELECT_CHNL_MODE       = 0xB0;
35
    static const MIDIByte MIDI_SYSTEM_EXCLUSIVE       = 0xF0;
36
    static const MIDIByte MIDI_TC_QUARTER_FRAME       = 0xF1;
37
    static const MIDIByte MIDI_SONG_POSITION_PTR      = 0xF2;
38
    static const MIDIByte MIDI_SONG_SELECT            = 0xF3;
39
    static const MIDIByte MIDI_TUNE_REQUEST           = 0xF6;
40
    static const MIDIByte MIDI_END_OF_EXCLUSIVE       = 0xF7;
41
    static const MIDIByte MIDI_TIMING_CLOCK           = 0xF8;
42
    static const MIDIByte MIDI_START                  = 0xFA;
43
    static const MIDIByte MIDI_CONTINUE               = 0xFB;
44
    static const MIDIByte MIDI_STOP                   = 0xFC;
45
    static const MIDIByte MIDI_ACTIVE_SENSING         = 0xFE;
46
    static const MIDIByte MIDI_SYSTEM_RESET           = 0xFF;
47
    static const MIDIByte MIDI_SYSEX_NONCOMMERCIAL    = 0x7D;
48
    static const MIDIByte MIDI_SYSEX_NON_RT           = 0x7E;
49
    static const MIDIByte MIDI_SYSEX_RT               = 0x7F;
50
    static const MIDIByte MIDI_SYSEX_RT_COMMAND       = 0x06;
51
    static const MIDIByte MIDI_SYSEX_RT_RESPONSE      = 0x07;
52
    static const MIDIByte MIDI_MMC_STOP               = 0x01;
53
    static const MIDIByte MIDI_MMC_PLAY               = 0x02;
54
    static const MIDIByte MIDI_MMC_DEFERRED_PLAY      = 0x03;
55
    static const MIDIByte MIDI_MMC_FAST_FORWARD       = 0x04;
56
    static const MIDIByte MIDI_MMC_REWIND             = 0x05;
57
    static const MIDIByte MIDI_MMC_RECORD_STROBE      = 0x06;
58
    static const MIDIByte MIDI_MMC_RECORD_EXIT        = 0x07;
59
    static const MIDIByte MIDI_MMC_RECORD_PAUSE       = 0x08;
60
    static const MIDIByte MIDI_MMC_PAUSE              = 0x08;
61
    static const MIDIByte MIDI_MMC_EJECT              = 0x0A;
62
    static const MIDIByte MIDI_MMC_LOCATE             = 0x44;
63
    static const MIDIByte MIDI_FILE_META_EVENT        = 0xFF;
64
    static const MIDIByte MIDI_SEQUENCE_NUMBER        = 0x00;
65
    static const MIDIByte MIDI_TEXT_EVENT             = 0x01;
66
    static const MIDIByte MIDI_COPYRIGHT_NOTICE       = 0x02;
67
    static const MIDIByte MIDI_TRACK_NAME             = 0x03;
68
    static const MIDIByte MIDI_INSTRUMENT_NAME        = 0x04;
69
    static const MIDIByte MIDI_LYRIC                  = 0x05;
70
    static const MIDIByte MIDI_TEXT_MARKER            = 0x06;
71
    static const MIDIByte MIDI_CUE_POINT              = 0x07;
72
    static const MIDIByte MIDI_CHANNEL_PREFIX         = 0x20;
73
    static const MIDIByte MIDI_CHANNEL_PREFIX_OR_PORT = 0x21;
74
    static const MIDIByte MIDI_END_OF_TRACK           = 0x2F;
75
    static const MIDIByte MIDI_SET_TEMPO              = 0x51;
76
    static const MIDIByte MIDI_SMPTE_OFFSET           = 0x54;
77
    static const MIDIByte MIDI_TIME_SIGNATURE         = 0x58;
78
    static const MIDIByte MIDI_KEY_SIGNATURE          = 0x59;
79
    static const MIDIByte MIDI_SEQUENCER_SPECIFIC     = 0x7F;
80
    static const MIDIByte MIDI_CONTROLLER_BANK_MSB      = 0x00;
81
    static const MIDIByte MIDI_CONTROLLER_VOLUME        = 0x07;
82
    static const MIDIByte MIDI_CONTROLLER_BANK_LSB      = 0x20;
83
    static const MIDIByte MIDI_CONTROLLER_MODULATION    = 0x01;
84
    static const MIDIByte MIDI_CONTROLLER_PAN           = 0x0A;
85
    static const MIDIByte MIDI_CONTROLLER_SUSTAIN       = 0x40;
86
    static const MIDIByte MIDI_CONTROLLER_RESONANCE     = 0x47;
87
    static const MIDIByte MIDI_CONTROLLER_RELEASE       = 0x48;
88
    static const MIDIByte MIDI_CONTROLLER_ATTACK        = 0x49;
89
    static const MIDIByte MIDI_CONTROLLER_FILTER        = 0x4A;
90
    static const MIDIByte MIDI_CONTROLLER_REVERB        = 0x5B;
91
    static const MIDIByte MIDI_CONTROLLER_CHORUS        = 0x5D;
92
    static const MIDIByte MIDI_CONTROLLER_NRPN_1        = 0x62;
93
    static const MIDIByte MIDI_CONTROLLER_NRPN_2        = 0x63;
94
    static const MIDIByte MIDI_CONTROLLER_RPN_1         = 0x64;
95
    static const MIDIByte MIDI_CONTROLLER_RPN_2         = 0x65;
96
    static const MIDIByte MIDI_CONTROLLER_SOUNDS_OFF    = 0x78;
97
    static const MIDIByte MIDI_CONTROLLER_RESET         = 0x79;
98
    static const MIDIByte MIDI_CONTROLLER_LOCAL         = 0x7A;
99
    static const MIDIByte MIDI_CONTROLLER_ALL_NOTES_OFF = 0x7B;
100
    static const MIDIByte MIDI_PERCUSSION_CHANNEL       = 9;
101

    
102
    typedef enum {
103
        MIDI_SINGLE_TRACK_FILE          = 0x00,
104
        MIDI_SIMULTANEOUS_TRACK_FILE    = 0x01,
105
        MIDI_SEQUENTIAL_TRACK_FILE      = 0x02,
106
        MIDI_FILE_BAD_FORMAT            = 0xFF
107
    } MIDIFileFormatType;
108
}
109

    
110
class MIDIEvent
111
{
112
public:
113
    MIDIEvent(unsigned long deltaTime,
114
              MIDIByte eventCode,
115
              MIDIByte data1 = 0,
116
              MIDIByte data2 = 0) :
117
        m_deltaTime(deltaTime),
118
        m_duration(0),
119
        m_eventCode(eventCode),
120
        m_data1(data1),
121
        m_data2(data2),
122
        m_metaEventCode(0)
123
    { }
124

    
125
    MIDIEvent(unsigned long deltaTime,
126
              MIDIByte eventCode,
127
              MIDIByte metaEventCode,
128
              const std::string &metaMessage) :
129
        m_deltaTime(deltaTime),
130
        m_duration(0),
131
        m_eventCode(eventCode),
132
        m_data1(0),
133
        m_data2(0),
134
        m_metaEventCode(metaEventCode),
135
        m_metaMessage(metaMessage)
136
    { }
137

    
138
    MIDIEvent(unsigned long deltaTime,
139
              MIDIByte eventCode,
140
              const std::string &sysEx) :
141
        m_deltaTime(deltaTime),
142
        m_duration(0),
143
        m_eventCode(eventCode),
144
        m_data1(0),
145
        m_data2(0),
146
        m_metaEventCode(0),
147
        m_metaMessage(sysEx)
148
    { }
149

    
150
    ~MIDIEvent() { }
151

    
152
    void setTime(const unsigned long &time) { m_deltaTime = time; }
153
    void setDuration(const unsigned long& duration) { m_duration = duration;}
154
    unsigned long addTime(const unsigned long &time) {
155
        m_deltaTime += time;
156
        return m_deltaTime;
157
    }
158

    
159
    int getMessageType() const
160
        { return (m_eventCode & MIDIConstants::MIDI_MESSAGE_TYPE_MASK); }
161

    
162
    int getChannelNumber() const
163
        { return (m_eventCode & MIDIConstants::MIDI_CHANNEL_NUM_MASK); }
164

    
165
    unsigned long getTime() const { return m_deltaTime; }
166
    unsigned long getDuration() const { return m_duration; }
167

    
168
    int getPitch() const { return m_data1; }
169
    int getVelocity() const { return m_data2; }
170
    int getData1() const { return m_data1; }
171
    int getData2() const { return m_data2; }
172
    int getEventCode() const { return m_eventCode; }
173

    
174
    bool isMeta() const { return (m_eventCode == MIDIConstants::MIDI_FILE_META_EVENT); }
175

    
176
    int getMetaEventCode() const { return m_metaEventCode; }
177
    std::string getMetaMessage() const { return m_metaMessage; }
178
    void setMetaMessage(const std::string &meta) { m_metaMessage = meta; }
179

    
180
    friend bool operator<(const MIDIEvent &a, const MIDIEvent &b);
181

    
182
private:
183
    unsigned long  m_deltaTime;
184
    unsigned long  m_duration;
185
    MIDIByte       m_eventCode;
186
    MIDIByte       m_data1;         // or Note
187
    MIDIByte       m_data2;         // or Velocity
188
    MIDIByte       m_metaEventCode;
189
    std::string    m_metaMessage;
190
};
191

    
192
// Comparator for sorting
193
//
194
struct MIDIEventCmp
195
{
196
    bool operator()(const MIDIEvent &mE1, const MIDIEvent &mE2) const
197
    { return mE1.getTime() < mE2.getTime(); }
198

    
199
    bool operator()(const MIDIEvent *mE1, const MIDIEvent *mE2) const
200
    { return mE1->getTime() < mE2->getTime(); }
201
};
202

    
203
class MIDIException : virtual public std::exception
204
{
205
public:
206
    MIDIException(QString message) throw() : m_message(message) {
207
        std::cerr << "WARNING: MIDI exception: "
208
                  << message.toLocal8Bit().data() << std::endl;
209
    }
210
    virtual ~MIDIException() throw() { }
211

    
212
    virtual const char *what() const throw() {
213
        return m_message.toLocal8Bit().data();
214
    }
215

    
216
protected:
217
    QString m_message;
218
};
219

    
220
#endif