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

History | View | Annotate | Download (9.71 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-2010 Richard Bown and Chris Cannam.
7
  
8
    Permission is hereby granted, free of charge, to any person
9
    obtaining a copy of this software and associated documentation
10
    files (the "Software"), to deal in the Software without
11
    restriction, including without limitation the rights to use, copy,
12
    modify, merge, publish, distribute, sublicense, and/or sell copies
13
    of the Software, and to permit persons to whom the Software is
14
    furnished to do so, subject to the following conditions:
15

16
    The above copyright notice and this permission notice shall be
17
    included in all copies or substantial portions of the Software.
18

19
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
23
    ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
24
    CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26

27
    Except as contained in this notice, the names of the authors
28
    shall not be used in advertising or otherwise to promote the sale,
29
    use or other dealings in this Software without prior written
30
    authorization.
31
*/
32

    
33
#ifndef _MIDI_EVENT_H_
34
#define _MIDI_EVENT_H_
35

    
36
#include <QString>
37
#include <string>
38
#include <iostream>
39

    
40
typedef unsigned char MIDIByte;
41

    
42
namespace MIDIConstants
43
{
44
    static const char *const MIDI_FILE_HEADER         = "MThd";
45
    static const char *const MIDI_TRACK_HEADER        = "MTrk";
46

    
47
    static const MIDIByte MIDI_STATUS_BYTE_MASK       = 0x80;
48
    static const MIDIByte MIDI_MESSAGE_TYPE_MASK      = 0xF0;
49
    static const MIDIByte MIDI_CHANNEL_NUM_MASK       = 0x0F;
50

    
51
    static const MIDIByte MIDI_NOTE_OFF               = 0x80;
52
    static const MIDIByte MIDI_NOTE_ON                = 0x90;
53
    static const MIDIByte MIDI_POLY_AFTERTOUCH        = 0xA0;
54
    static const MIDIByte MIDI_CTRL_CHANGE            = 0xB0;
55
    static const MIDIByte MIDI_PROG_CHANGE            = 0xC0;
56
    static const MIDIByte MIDI_CHNL_AFTERTOUCH        = 0xD0;
57
    static const MIDIByte MIDI_PITCH_BEND             = 0xE0;
58
    static const MIDIByte MIDI_SELECT_CHNL_MODE       = 0xB0;
59
    static const MIDIByte MIDI_SYSTEM_EXCLUSIVE       = 0xF0;
60
    static const MIDIByte MIDI_TC_QUARTER_FRAME       = 0xF1;
61
    static const MIDIByte MIDI_SONG_POSITION_PTR      = 0xF2;
62
    static const MIDIByte MIDI_SONG_SELECT            = 0xF3;
63
    static const MIDIByte MIDI_TUNE_REQUEST           = 0xF6;
64
    static const MIDIByte MIDI_END_OF_EXCLUSIVE       = 0xF7;
65
    static const MIDIByte MIDI_TIMING_CLOCK           = 0xF8;
66
    static const MIDIByte MIDI_START                  = 0xFA;
67
    static const MIDIByte MIDI_CONTINUE               = 0xFB;
68
    static const MIDIByte MIDI_STOP                   = 0xFC;
69
    static const MIDIByte MIDI_ACTIVE_SENSING         = 0xFE;
70
    static const MIDIByte MIDI_SYSTEM_RESET           = 0xFF;
71
    static const MIDIByte MIDI_SYSEX_NONCOMMERCIAL    = 0x7D;
72
    static const MIDIByte MIDI_SYSEX_NON_RT           = 0x7E;
73
    static const MIDIByte MIDI_SYSEX_RT               = 0x7F;
74
    static const MIDIByte MIDI_SYSEX_RT_COMMAND       = 0x06;
75
    static const MIDIByte MIDI_SYSEX_RT_RESPONSE      = 0x07;
76
    static const MIDIByte MIDI_MMC_STOP               = 0x01;
77
    static const MIDIByte MIDI_MMC_PLAY               = 0x02;
78
    static const MIDIByte MIDI_MMC_DEFERRED_PLAY      = 0x03;
79
    static const MIDIByte MIDI_MMC_FAST_FORWARD       = 0x04;
80
    static const MIDIByte MIDI_MMC_REWIND             = 0x05;
81
    static const MIDIByte MIDI_MMC_RECORD_STROBE      = 0x06;
82
    static const MIDIByte MIDI_MMC_RECORD_EXIT        = 0x07;
83
    static const MIDIByte MIDI_MMC_RECORD_PAUSE       = 0x08;
84
    static const MIDIByte MIDI_MMC_PAUSE              = 0x08;
85
    static const MIDIByte MIDI_MMC_EJECT              = 0x0A;
86
    static const MIDIByte MIDI_MMC_LOCATE             = 0x44;
87
    static const MIDIByte MIDI_FILE_META_EVENT        = 0xFF;
88
    static const MIDIByte MIDI_SEQUENCE_NUMBER        = 0x00;
89
    static const MIDIByte MIDI_TEXT_EVENT             = 0x01;
90
    static const MIDIByte MIDI_COPYRIGHT_NOTICE       = 0x02;
91
    static const MIDIByte MIDI_TRACK_NAME             = 0x03;
92
    static const MIDIByte MIDI_INSTRUMENT_NAME        = 0x04;
93
    static const MIDIByte MIDI_LYRIC                  = 0x05;
94
    static const MIDIByte MIDI_TEXT_MARKER            = 0x06;
95
    static const MIDIByte MIDI_CUE_POINT              = 0x07;
96
    static const MIDIByte MIDI_CHANNEL_PREFIX         = 0x20;
97
    static const MIDIByte MIDI_CHANNEL_PREFIX_OR_PORT = 0x21;
98
    static const MIDIByte MIDI_END_OF_TRACK           = 0x2F;
99
    static const MIDIByte MIDI_SET_TEMPO              = 0x51;
100
    static const MIDIByte MIDI_SMPTE_OFFSET           = 0x54;
101
    static const MIDIByte MIDI_TIME_SIGNATURE         = 0x58;
102
    static const MIDIByte MIDI_KEY_SIGNATURE          = 0x59;
103
    static const MIDIByte MIDI_SEQUENCER_SPECIFIC     = 0x7F;
104
    static const MIDIByte MIDI_CONTROLLER_BANK_MSB      = 0x00;
105
    static const MIDIByte MIDI_CONTROLLER_VOLUME        = 0x07;
106
    static const MIDIByte MIDI_CONTROLLER_BANK_LSB      = 0x20;
107
    static const MIDIByte MIDI_CONTROLLER_MODULATION    = 0x01;
108
    static const MIDIByte MIDI_CONTROLLER_PAN           = 0x0A;
109
    static const MIDIByte MIDI_CONTROLLER_SUSTAIN       = 0x40;
110
    static const MIDIByte MIDI_CONTROLLER_RESONANCE     = 0x47;
111
    static const MIDIByte MIDI_CONTROLLER_RELEASE       = 0x48;
112
    static const MIDIByte MIDI_CONTROLLER_ATTACK        = 0x49;
113
    static const MIDIByte MIDI_CONTROLLER_FILTER        = 0x4A;
114
    static const MIDIByte MIDI_CONTROLLER_REVERB        = 0x5B;
115
    static const MIDIByte MIDI_CONTROLLER_CHORUS        = 0x5D;
116
    static const MIDIByte MIDI_CONTROLLER_NRPN_1        = 0x62;
117
    static const MIDIByte MIDI_CONTROLLER_NRPN_2        = 0x63;
118
    static const MIDIByte MIDI_CONTROLLER_RPN_1         = 0x64;
119
    static const MIDIByte MIDI_CONTROLLER_RPN_2         = 0x65;
120
    static const MIDIByte MIDI_CONTROLLER_SOUNDS_OFF    = 0x78;
121
    static const MIDIByte MIDI_CONTROLLER_RESET         = 0x79;
122
    static const MIDIByte MIDI_CONTROLLER_LOCAL         = 0x7A;
123
    static const MIDIByte MIDI_CONTROLLER_ALL_NOTES_OFF = 0x7B;
124
    static const MIDIByte MIDI_PERCUSSION_CHANNEL       = 9;
125

    
126
    typedef enum {
127
        MIDI_SINGLE_TRACK_FILE          = 0x00,
128
        MIDI_SIMULTANEOUS_TRACK_FILE    = 0x01,
129
        MIDI_SEQUENTIAL_TRACK_FILE      = 0x02,
130
        MIDI_FILE_BAD_FORMAT            = 0xFF
131
    } MIDIFileFormatType;
132
}
133

    
134
class MIDIEvent
135
{
136
public:
137
    MIDIEvent(unsigned long deltaTime,
138
              MIDIByte eventCode,
139
              MIDIByte data1 = 0,
140
              MIDIByte data2 = 0) :
141
        m_deltaTime(deltaTime),
142
        m_duration(0),
143
        m_eventCode(eventCode),
144
        m_data1(data1),
145
        m_data2(data2),
146
        m_metaEventCode(0)
147
    { }
148

    
149
    MIDIEvent(unsigned long deltaTime,
150
              MIDIByte eventCode,
151
              MIDIByte metaEventCode,
152
              const std::string &metaMessage) :
153
        m_deltaTime(deltaTime),
154
        m_duration(0),
155
        m_eventCode(eventCode),
156
        m_data1(0),
157
        m_data2(0),
158
        m_metaEventCode(metaEventCode),
159
        m_metaMessage(metaMessage)
160
    { }
161

    
162
    MIDIEvent(unsigned long deltaTime,
163
              MIDIByte eventCode,
164
              const std::string &sysEx) :
165
        m_deltaTime(deltaTime),
166
        m_duration(0),
167
        m_eventCode(eventCode),
168
        m_data1(0),
169
        m_data2(0),
170
        m_metaEventCode(0),
171
        m_metaMessage(sysEx)
172
    { }
173

    
174
    ~MIDIEvent() { }
175

    
176
    void setTime(const unsigned long &time) { m_deltaTime = time; }
177
    void setDuration(const unsigned long& duration) { m_duration = duration;}
178
    unsigned long addTime(const unsigned long &time) {
179
        m_deltaTime += time;
180
        return m_deltaTime;
181
    }
182

    
183
    int getMessageType() const
184
        { return (m_eventCode & MIDIConstants::MIDI_MESSAGE_TYPE_MASK); }
185

    
186
    int getChannelNumber() const
187
        { return (m_eventCode & MIDIConstants::MIDI_CHANNEL_NUM_MASK); }
188

    
189
    unsigned long getTime() const { return m_deltaTime; }
190
    unsigned long getDuration() const { return m_duration; }
191

    
192
    int getPitch() const { return m_data1; }
193
    int getVelocity() const { return m_data2; }
194
    int getData1() const { return m_data1; }
195
    int getData2() const { return m_data2; }
196
    int getEventCode() const { return m_eventCode; }
197

    
198
    bool isMeta() const { return (m_eventCode == MIDIConstants::MIDI_FILE_META_EVENT); }
199

    
200
    int getMetaEventCode() const { return m_metaEventCode; }
201
    std::string getMetaMessage() const { return m_metaMessage; }
202
    void setMetaMessage(const std::string &meta) { m_metaMessage = meta; }
203

    
204
    friend bool operator<(const MIDIEvent &a, const MIDIEvent &b);
205

    
206
private:
207
    unsigned long  m_deltaTime;
208
    unsigned long  m_duration;
209
    MIDIByte       m_eventCode;
210
    MIDIByte       m_data1;         // or Note
211
    MIDIByte       m_data2;         // or Velocity
212
    MIDIByte       m_metaEventCode;
213
    std::string    m_metaMessage;
214
};
215

    
216
// Comparator for sorting
217
//
218
struct MIDIEventCmp
219
{
220
    bool operator()(const MIDIEvent &mE1, const MIDIEvent &mE2) const
221
    { return mE1.getTime() < mE2.getTime(); }
222

    
223
    bool operator()(const MIDIEvent *mE1, const MIDIEvent *mE2) const
224
    { return mE1->getTime() < mE2->getTime(); }
225
};
226

    
227
class MIDIException : virtual public std::exception
228
{
229
public:
230
    MIDIException(QString message) throw() : m_message(message) {
231
        std::cerr << "WARNING: MIDI exception: "
232
                  << message.toLocal8Bit().data() << std::endl;
233
    }
234
    virtual ~MIDIException() throw() { }
235

    
236
    virtual const char *what() const throw() {
237
        return m_message.toLocal8Bit().data();
238
    }
239

    
240
protected:
241
    QString m_message;
242
};
243

    
244
#endif