1 //=============================================================================
2 //  MuseScore
3 //  Music Composition & Notation
4 //
5 //  Copyright (C) 2002-2011 Werner Schweer
6 //
7 //  This program is free software; you can redistribute it and/or modify
8 //  it under the terms of the GNU General Public License version 2
9 //  as published by the Free Software Foundation and appearing in
10 //  the file LICENCE.GPL
11 //=============================================================================
12 
13 #ifndef __EVENT_H__
14 #define __EVENT_H__
15 
16 #include <map>
17 
18 namespace Ms {
19 
20 class Note;
21 class Harmony;
22 class XmlWriter;
23 class Score;
24 
25 enum class BeatType : char;
26 
27 // 4 is the default for the majority of synthesisers, aka VSTis
28 const int PITCH_BEND_SENSITIVITY = 4;
29 
30 const int MIDI_ON_SIGNAL = 127;
31 
32 //---------------------------------------------------------
33 //   Event types
34 //---------------------------------------------------------
35 
36 enum {
37       ME_INVALID    = 0,
38       ME_NOTEOFF    = 0x80,
39       ME_NOTEON     = 0x90,
40       ME_POLYAFTER  = 0xa0,
41       ME_CONTROLLER = 0xb0,
42       ME_PROGRAM    = 0xc0,
43       ME_AFTERTOUCH = 0xd0,
44       ME_PITCHBEND  = 0xe0,
45       ME_SYSEX      = 0xf0,
46       ME_META       = 0xff,
47       ME_SONGPOS    = 0xf2,
48       ME_ENDSYSEX   = 0xf7,
49       ME_CLOCK      = 0xf8,
50       ME_START      = 0xfa,
51       ME_CONTINUE   = 0xfb,
52       ME_STOP       = 0xfc,
53       ME_SENSE      = 0xfe,   // active sense (used by yamaha)
54 
55       ME_NOTE       = 0x1,
56       ME_CHORD      = 0x2,
57       ME_TICK1      = 0x3,  // metronome tick akzent
58       ME_TICK2      = 0x4,  // metronome tick
59       };
60 
61 //---------------------------------------------------------
62 //   Midi Meta Events
63 //---------------------------------------------------------
64 
65 enum {
66       META_SEQUENCE_NUMBER = 0,
67       META_TEXT            = 1,
68       META_COPYRIGHT       = 2,
69       META_TRACK_NAME      = 3,
70       META_INSTRUMENT_NAME = 4,
71       META_LYRIC           = 5,
72       META_MARKER          = 6,
73       META_CUE_POINT       = 7,
74       META_PROGRAM_NAME    = 8, // MIDI Meta Events 8 and 9 were defined as above by the MMA in 1998
75       META_DEVICE_NAME     = 9, // It is therefore necessary to redefine MuseScore's private meta events
76       META_TRACK_COMMENT   = 0xf, // Using the block starting 0x10 seems sensible as that is currently clear
77       META_TITLE           = 0x10,     // mscore extension
78       META_SUBTITLE        = 0x11,     // mscore extension
79       META_COMPOSER        = 0x12,   // mscore extension
80       META_TRANSLATOR      = 0x13,   // mscore extension
81       META_POET            = 0x14,   // mscore extension
82       META_PORT_CHANGE     = 0x21,
83       META_CHANNEL_PREFIX  = 0x22,
84       META_EOT             = 0x2f,  // end of track
85       META_TEMPO           = 0x51,
86       META_TIME_SIGNATURE  = 0x58,
87       META_KEY_SIGNATURE   = 0x59,
88       META_SPECIFIC        = 0x7F   // sequencer specific
89       };
90 
91 //---------------------------------------------------------
92 //   Midi Controller
93 //---------------------------------------------------------
94 
95 enum {
96       CTRL_HBANK              = 0x00,
97       CTRL_LBANK              = 0x20,
98 
99       CTRL_HDATA              = 0x06,
100       CTRL_LDATA              = 0x26,
101 
102       CTRL_HNRPN              = 0x63,
103       CTRL_LNRPN              = 0x62,
104 
105       CTRL_HRPN               = 0x65,
106       CTRL_LRPN               = 0x64,
107 
108       CTRL_MODULATION         = 0x01,
109       CTRL_BREATH             = 0x02,
110       CTRL_FOOT               = 0x04,
111       CTRL_PORTAMENTO_TIME_MSB= 0x05,
112       CTRL_VOLUME             = 0x07,
113       CTRL_PANPOT             = 0x0a,
114       CTRL_EXPRESSION         = 0x0b,
115       CTRL_PORTAMENTO_TIME_LSB= 0x25,
116       CTRL_SUSTAIN            = 0x40,
117       CTRL_PORTAMENTO         = 0x41,
118       CTRL_SOSTENUTO          = 0x42,
119       CTRL_SOFT_PEDAL         = 0x43,
120       CTRL_HARMONIC_CONTENT   = 0x47,
121       CTRL_RELEASE_TIME       = 0x48,
122       CTRL_ATTACK_TIME        = 0x49,
123 
124       CTRL_BRIGHTNESS         = 0x4a,
125       CTRL_PORTAMENTO_CONTROL = 0x54,
126       CTRL_REVERB_SEND        = 0x5b,
127       CTRL_CHORUS_SEND        = 0x5d,
128       CTRL_VARIATION_SEND     = 0x5e,
129 
130       CTRL_ALL_SOUNDS_OFF     = 0x78, // 120
131       CTRL_RESET_ALL_CTRL     = 0x79, // 121
132       CTRL_LOCAL_OFF          = 0x7a, // 122
133       CTRL_ALL_NOTES_OFF      = 0x7b,  // 123
134 
135       // special midi events are mapped to internal
136       // controller
137       //
138       CTRL_PROGRAM   = 0x81,
139       /*             = 0x82,*/
140       CTRL_PRESS     = 0x83,
141       CTRL_POLYAFTER = 0x84
142       };
143 
144 //---------------------------------------------------------
145 //   MidiCoreEvent
146 //---------------------------------------------------------
147 
148 class MidiCoreEvent {
149    protected:
150       uchar _type    = 0;
151       uchar _channel = 0;
152       uchar _a       = 0;
153       uchar _b       = 0;
154 
155    public:
MidiCoreEvent()156       MidiCoreEvent() {}
MidiCoreEvent(uchar t,uchar c,uchar a,uchar b)157       MidiCoreEvent(uchar t, uchar c, uchar a, uchar b)
158          : _type(t), _channel(c), _a(a), _b(b) {}
159 
set(uchar t,uchar c,uchar a,uchar b)160       void set(uchar t, uchar c, uchar a, uchar b) {
161             _type    = t;
162             _channel = c;
163             _a       = a;
164             _b       = b;
165             }
166 
type()167       uchar type() const             { return _type;    }
setType(uchar t)168       void  setType(uchar t)         { _type = t;       }
channel()169       uchar channel() const          { return _channel; }
setChannel(uchar c)170       void  setChannel(uchar c)      { _channel = c;    }
171 
dataA()172       int dataA() const              { return _a; }
pitch()173       int pitch() const              { return _a; }
controller()174       int controller() const         { return _a; }
175 
setDataA(int v)176       void setDataA(int v)           { _a = v; }
setPitch(int v)177       void setPitch(int v)           { _a = v; }
setController(int v)178       void setController(int v)      { _a = v; }
179 
dataB()180       int dataB() const              { return _b; }
velo()181       int velo() const               { return _b; }
value()182       int value() const              { return _b; }
183 
setDataB(int v)184       void setDataB(int v)           { _b = v; }
setVelo(int v)185       void setVelo(int v)            { _b = v; }
setValue(int v)186       void setValue(int v)           { _b = v; }
187 
setData(int a,int b)188       void setData(int a, int b)        { _a = a; _b = b; }
setData(int t,int a,int b)189       void setData(int t, int a, int b) { _type = t; _a = a; _b = b; }
190 
191       bool isChannelEvent() const;
192       void write(XmlWriter&) const;
193       bool operator==(const MidiCoreEvent& e) const {
194             return e._type == _type && e._channel == _channel && e._a == _a && e._b == _b;
195             }
196       };
197 
198 //---------------------------------------------------------
199 //   MidiEvent
200 //---------------------------------------------------------
201 
202 class MidiEvent : public MidiCoreEvent {
203 
204    protected:
205       uchar* _edata { nullptr };           // always zero terminated (_data[_len] == 0; )
206       int _len { 0 };
207       int _metaType { 0 };
208 
209    public:
MidiEvent()210       MidiEvent() {}
MidiEvent(uchar t,uchar c,uchar a,uchar b)211       MidiEvent(uchar t, uchar c, uchar a, uchar b)
212          : MidiCoreEvent(t, c, a, b), _edata(0), _len(0) {}
213 
edata()214       const uchar* edata() const     { return _edata; }
setEData(uchar * d)215       void setEData(uchar* d)        { _edata = d; }
len()216       int len() const                { return _len; }
setLen(int l)217       void setLen(int l)             { _len = l; }
metaType()218       int metaType() const           { return _metaType; }
setMetaType(int v)219       void setMetaType(int v)        { _metaType = v; }
220       };
221 
222 //---------------------------------------------------------
223 //   PlayEvent
224 //    interface to Synthesizer
225 //---------------------------------------------------------
226 
227 class PlayEvent : public MidiCoreEvent {
228 
229    protected:
230       float _tuning = .0f;
231 
232    public:
PlayEvent()233       PlayEvent() : MidiCoreEvent() {}
PlayEvent(const MidiCoreEvent & e)234       PlayEvent(const MidiCoreEvent& e) : MidiCoreEvent(e) {}
PlayEvent(uchar t,uchar c,uchar a,uchar b)235       PlayEvent(uchar t, uchar c, uchar a, uchar b)
236          : MidiCoreEvent(t, c, a, b) {}
tuning()237       float tuning() const           { return _tuning;  }
setTuning(float v)238       void setTuning(float v)        { _tuning = v;     }
239       };
240 
241 //---------------------------------------------------------
242 //   NPlayEvent
243 //    used for Sequencer interface
244 //---------------------------------------------------------
245 
246 class NPlayEvent : public PlayEvent {
247       const Note* _note{nullptr};
248       const Harmony* _harmony{nullptr};
249       int _origin = -1;
250       int _discard = 0;
251       bool _portamento = false;
252 
253    public:
NPlayEvent()254       NPlayEvent() : PlayEvent() {}
NPlayEvent(uchar t,uchar c,uchar a,uchar b)255       NPlayEvent(uchar t, uchar c, uchar a, uchar b)
256          : PlayEvent(t, c, a, b) {}
NPlayEvent(const MidiCoreEvent & e)257       NPlayEvent(const MidiCoreEvent& e) : PlayEvent(e) {}
258       NPlayEvent(BeatType beatType);
259 
note()260       const Note* note() const            { return _note;    }
setNote(const Note * v)261       void setNote(const Note* v)         { _note = v;       }
harmony()262       const Harmony* harmony() const      { return _harmony; }
setHarmony(const Harmony * v)263       void setHarmony(const Harmony* v)   { _harmony = v;    }
264 
getOriginatingStaff()265       int getOriginatingStaff() const { return _origin; }
setOriginatingStaff(int i)266       void setOriginatingStaff(int i) { _origin = i; }
setDiscard(int d)267       void setDiscard(int d) { _discard = d; }
discard()268       int discard() const { return _discard; }
269       bool isMuted() const;
setPortamento(bool p)270       void setPortamento(bool p) { _portamento = p; }
portamento()271       bool portamento() const {
272             return (_portamento == true ||
273                   (this->type() == ME_CONTROLLER &&
274                   (this->controller() == CTRL_PORTAMENTO || this->controller() == CTRL_PORTAMENTO_CONTROL ||
275                         this->controller() == CTRL_PORTAMENTO_TIME_MSB ||  this->controller() == CTRL_PORTAMENTO_TIME_LSB))); }
276       };
277 
278 //---------------------------------------------------------
279 //   Event
280 //---------------------------------------------------------
281 
282 class Event : public PlayEvent {
283       int _ontime;
284       int _noquantOntime;
285       int _noquantDuration;
286       int _duration;
287       int _tpc;               // tonal pitch class
288       int _voice;
289       QList<Event> _notes;
290       uchar* _edata;           // always zero terminated (_data[_len] == 0; )
291       int _len;
292       int _metaType;
293       const Note* _note;
294 
295    public:
296       Event();
297       Event(const Event&);
298       Event(int t);
299       ~Event();
300       bool operator==(const Event&) const;
301 
302       void write(XmlWriter&) const;
303       void dump() const;
304 
305 
noquantOntime()306       int noquantOntime() const      { return _noquantOntime;   }
setNoquantOntime(int v)307       void setNoquantOntime(int v)   { _noquantOntime = v;      }
noquantDuration()308       int noquantDuration() const    { return _noquantDuration; }
setNoquantDuration(int v)309       void setNoquantDuration(int v) { _noquantDuration = v;    }
310 
ontime()311       int ontime() const             { return _ontime; }
setOntime(int v)312       void setOntime(int v)          { _ontime = v; }
313 
duration()314       int duration() const           { return _duration; }
setDuration(int v)315       void setDuration(int v)        { _duration = v; }
voice()316       int voice() const              { return _voice; }
setVoice(int val)317       void setVoice(int val)         { _voice = val; }
offtime()318       int offtime() const            { return _ontime + _duration; }
notes()319       QList<Event>& notes()          { return _notes; }
edata()320       const uchar* edata() const     { return _edata; }
setEData(uchar * d)321       void setEData(uchar* d)        { _edata = d; }
len()322       int len() const                { return _len; }
setLen(int l)323       void setLen(int l)             { _len = l; }
metaType()324       int metaType() const           { return _metaType; }
setMetaType(int v)325       void setMetaType(int v)        { _metaType = v; }
tpc()326       int tpc() const                { return _tpc; }
setTpc(int v)327       void setTpc(int v)             { _tpc = v; }
note()328       const Note* note() const       { return _note; }
setNote(const Note * v)329       void setNote(const Note* v)    { _note = v; }
330       };
331 
332 //---------------------------------------------------------
333 //   EventList
334 //   EventMap
335 //---------------------------------------------------------
336 
337 class EventList : public QList<Event> {
338    public:
339       void insert(const Event&);
340       void insertNote(int channel, Note*);
341       };
342 
343 class EventMap : public std::multimap<int, NPlayEvent> {
344       int _highestChannel = 15;
345    public:
346       void fixupMIDI();
registerChannel(int c)347       void registerChannel(int c) { if (c > _highestChannel) _highestChannel = c; }
348       };
349 
350 typedef EventList::iterator iEvent;
351 typedef EventList::const_iterator ciEvent;
352 
353 extern QString midiMetaName(int meta);
354 
355 }
356 #endif
357 
358