1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
//=========================================================
// MusE
// Linux Music Editor
// $Id: mpevent.cpp,v 1.6.2.2 2009/11/25 09:09:43 terminator356 Exp $
//
// (C) Copyright 2002-2004 Werner Schweer (ws@seh.de)
//=========================================================
#include "mpevent.h"
#include "helper.h"
#include "event.h"
#include "midictrl.h"
#include "midiport.h"
#include "muse/midi.h"
//---------------------------------------------------------
// MEvent
//---------------------------------------------------------
MEvent::MEvent(unsigned t, int port, int tpe, const unsigned char* data, int len)
{
_time = t;
_port = port;
edata.setData(data, len);
_type = tpe;
_loopNum = 0;
}
MEvent::MEvent(unsigned tick, int port, int channel, const Event& e)
{
setChannel(channel);
setTime(tick);
setPort(port);
setLoopNum(0);
switch(e.type()) {
case Note:
setType(ME_NOTEON);
setA(e.dataA());
setB(e.dataB());
break;
case Controller:
setType(ME_CONTROLLER);
setA(e.dataA()); // controller number
setB(e.dataB()); // controller value
break;
case PAfter:
setType(ME_POLYAFTER);
setA(e.dataA());
setB(e.dataB());
break;
case CAfter:
setType(ME_AFTERTOUCH);
setA(e.dataA());
setB(0);
break;
case Sysex:
setType(ME_SYSEX);
setData(e.eventData());
break;
default:
printf("MEvent::MEvent(): event type %d not implemented\n",
type());
break;
}
}
//---------------------------------------------------------
// dump
//---------------------------------------------------------
void MEvent::dump() const
{
printf("time:%d port:%d chan:%d ", _time, _port, _channel+1);
if (_type == 0x90) { // NoteOn
QString s = pitch2string(_a);
printf("NoteOn %s(0x%x) %d\n", s.latin1(), _a, _b);
}
else if (_type == 0xf0) {
printf("SysEx len %d 0x%0x ...\n", len(), data()[0]);
}
else
printf("type:0x%02x a=%d b=%d\n", _type, _a, _b);
}
//---------------------------------------------------------
// operator <
//---------------------------------------------------------
bool MEvent::operator<(const MEvent& e) const
{
if (time() != e.time())
return time() < e.time();
if (port() != e.port())
return port() < e.port();
// play note off events first to prevent overlapping
// notes
if (channel() == e.channel())
return type() == ME_NOTEOFF
|| (type() == ME_NOTEON && dataB() == 0);
int map[16] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15 };
return map[channel()] < map[e.channel()];
}
//---------------------------------------------------------
// put
// return true on fifo overflow
//---------------------------------------------------------
bool MidiFifo::put(const MidiPlayEvent& event)
{
if (size < MIDI_FIFO_SIZE) {
fifo[wIndex] = event;
wIndex = (wIndex + 1) % MIDI_FIFO_SIZE;
// q_atomic_increment(&size);
++size;
return false;
}
return true;
}
//---------------------------------------------------------
// get
//---------------------------------------------------------
MidiPlayEvent MidiFifo::get()
{
MidiPlayEvent event(fifo[rIndex]);
rIndex = (rIndex + 1) % MIDI_FIFO_SIZE;
// q_atomic_decrement(&size);
--size;
return event;
}
//---------------------------------------------------------
// peek
//---------------------------------------------------------
const MidiPlayEvent& MidiFifo::peek(int n)
{
int idx = (rIndex + n) % MIDI_FIFO_SIZE;
return fifo[idx];
}
//---------------------------------------------------------
// remove
//---------------------------------------------------------
void MidiFifo::remove()
{
rIndex = (rIndex + 1) % MIDI_FIFO_SIZE;
// q_atomic_decrement(&size);
--size;
}
|