Skip to content

Latest commit

 

History

History
295 lines (264 loc) · 6.5 KB

native_midi_haiku.cpp

File metadata and controls

295 lines (264 loc) · 6.5 KB
 
Jan 18, 2011
Jan 18, 2011
1
/*
Jan 5, 2012
Jan 5, 2012
2
native_midi_haiku: Native Midi support on Haiku for the SDL_mixer library
Dec 31, 2011
Dec 31, 2011
3
Copyright (C) 2010 Egor Suvorov <egor_suvorov@mail.ru>
Jan 18, 2011
Jan 18, 2011
4
Dec 31, 2011
Dec 31, 2011
5
6
7
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Jan 18, 2011
Jan 18, 2011
8
Dec 31, 2011
Dec 31, 2011
9
10
11
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
Jan 18, 2011
Jan 18, 2011
12
Dec 31, 2011
Dec 31, 2011
13
14
15
16
17
18
19
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jan 18, 2011
Jan 18, 2011
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
*/
#include "SDL_config.h"
#ifdef __HAIKU__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <MidiStore.h>
#include <MidiDefs.h>
#include <MidiSynthFile.h>
#include <algorithm>
#include <assert.h>
extern "C" {
#include "native_midi.h"
#include "native_midi_common.h"
}
bool compareMIDIEvent(const MIDIEvent &a, const MIDIEvent &b)
{
return a.time < b.time;
}
class MidiEventsStore : public BMidi
{
public:
MidiEventsStore()
{
fPlaying = false;
Jan 1, 2012
Jan 1, 2012
48
fLoops = 0;
Jan 18, 2011
Jan 18, 2011
49
}
Jun 2, 2013
Jun 2, 2013
50
virtual status_t Import(SDL_RWops *src)
Jan 18, 2011
Jan 18, 2011
51
{
Jun 2, 2013
Jun 2, 2013
52
fEvs = CreateMIDIEventList(src, &fDivision);
Jan 18, 2011
Jan 18, 2011
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
if (!fEvs) {
return B_BAD_MIDI_DATA;
}
fTotal = 0;
for (MIDIEvent *x = fEvs; x; x = x->next) fTotal++;
fPos = fTotal;
sort_events();
return B_OK;
}
virtual void Run()
{
fPlaying = true;
fPos = 0;
MIDIEvent *ev = fEvs;
uint32 startTime = B_NOW;
Jan 1, 2012
Jan 1, 2012
70
while (KeepRunning())
Jan 18, 2011
Jan 18, 2011
71
{
Jan 1, 2012
Jan 1, 2012
72
73
if (!ev) {
if (fLoops && fEvs) {
Jul 9, 2013
Jul 9, 2013
74
if (fLoops > 0) --fLoops;
Jan 1, 2012
Jan 1, 2012
75
76
77
78
79
fPos = 0;
ev = fEvs;
} else
break;
}
Jan 18, 2011
Jan 18, 2011
80
81
82
83
84
85
86
87
88
89
90
91
92
93
SprayEvent(ev, ev->time + startTime);
ev = ev->next;
fPos++;
}
fPos = fTotal;
fPlaying = false;
}
virtual ~MidiEventsStore()
{
if (!fEvs) return;
FreeMIDIEventList(fEvs);
fEvs = 0;
}
Jan 1, 2012
Jan 1, 2012
94
bool IsPlaying()
Jan 18, 2011
Jan 18, 2011
95
{
Jan 1, 2012
Jan 1, 2012
96
return fPlaying;
Jan 18, 2011
Jan 18, 2011
97
98
}
Jan 1, 2012
Jan 1, 2012
99
void SetLoops(int loops)
Jan 18, 2011
Jan 18, 2011
100
{
Jan 1, 2012
Jan 1, 2012
101
fLoops = loops;
Jan 18, 2011
Jan 18, 2011
102
103
104
105
106
107
108
}
protected:
MIDIEvent *fEvs;
Uint16 fDivision;
int fPos, fTotal;
Jan 1, 2012
Jan 1, 2012
109
int fLoops;
Jan 18, 2011
Jan 18, 2011
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
bool fPlaying;
void SprayEvent(MIDIEvent *ev, uint32 time)
{
switch (ev->status & 0xF0)
{
case B_NOTE_OFF:
SprayNoteOff((ev->status & 0x0F) + 1, ev->data[0], ev->data[1], time);
break;
case B_NOTE_ON:
SprayNoteOn((ev->status & 0x0F) + 1, ev->data[0], ev->data[1], time);
break;
case B_KEY_PRESSURE:
SprayKeyPressure((ev->status & 0x0F) + 1, ev->data[0], ev->data[1], time);
break;
case B_CONTROL_CHANGE:
SprayControlChange((ev->status & 0x0F) + 1, ev->data[0], ev->data[1], time);
break;
case B_PROGRAM_CHANGE:
SprayProgramChange((ev->status & 0x0F) + 1, ev->data[0], time);
break;
case B_CHANNEL_PRESSURE:
SprayChannelPressure((ev->status & 0x0F) + 1, ev->data[0], time);
break;
case B_PITCH_BEND:
SprayPitchBend((ev->status & 0x0F) + 1, ev->data[0], ev->data[1], time);
break;
case 0xF:
switch (ev->status)
{
case B_SYS_EX_START:
SpraySystemExclusive(ev->extraData, ev->extraLen, time);
May 22, 2013
May 22, 2013
142
break;
Jan 18, 2011
Jan 18, 2011
143
144
145
146
147
148
149
case B_MIDI_TIME_CODE:
case B_SONG_POSITION:
case B_SONG_SELECT:
case B_CABLE_MESSAGE:
case B_TUNE_REQUEST:
case B_SYS_EX_END:
SpraySystemCommon(ev->status, ev->data[0], ev->data[1], time);
May 22, 2013
May 22, 2013
150
break;
Jan 18, 2011
Jan 18, 2011
151
152
153
154
155
156
case B_TIMING_CLOCK:
case B_START:
case B_STOP:
case B_CONTINUE:
case B_ACTIVE_SENSING:
SpraySystemRealTime(ev->status, time);
May 22, 2013
May 22, 2013
157
break;
Jan 18, 2011
Jan 18, 2011
158
159
case B_SYSTEM_RESET:
if (ev->data[0] == 0x51 && ev->data[1] == 0x03)
May 22, 2013
May 22, 2013
160
161
162
163
164
165
166
167
168
169
{
assert(ev->extraLen == 3);
int val = (ev->extraData[0] << 16) | (ev->extraData[1] << 8) | ev->extraData[2];
int tempo = 60000000 / val;
SprayTempoChange(tempo, time);
}
else
{
SpraySystemRealTime(ev->status, time);
}
Jan 18, 2011
Jan 18, 2011
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
}
break;
}
}
void sort_events()
{
MIDIEvent *items = new MIDIEvent[fTotal];
MIDIEvent *x = fEvs;
for (int i = 0; i < fTotal; i++)
{
memcpy(items + i, x, sizeof(MIDIEvent));
x = x->next;
}
std::sort(items, items + fTotal, compareMIDIEvent);
x = fEvs;
for (int i = 0; i < fTotal; i++)
{
MIDIEvent *ne = x->next;
memcpy(x, items + i, sizeof(MIDIEvent));
x->next = ne;
x = ne;
}
for (x = fEvs; x && x->next; x = x->next)
assert(x->time <= x->next->time);
delete[] items;
}
};
BMidiSynth synth;
struct _NativeMidiSong {
MidiEventsStore *store;
} *currentSong = NULL;
char lasterr[1024];
Oct 17, 2017
Oct 17, 2017
209
int native_midi_detect(void)
Jan 18, 2011
Jan 18, 2011
210
211
212
213
214
215
216
217
218
219
220
221
{
status_t res = synth.EnableInput(true, false);
return res == B_OK;
}
void native_midi_setvolume(int volume)
{
if (volume < 0) volume = 0;
if (volume > 128) volume = 128;
synth.SetVolume(volume / 128.0);
}
Jun 2, 2013
Jun 2, 2013
222
NativeMidiSong *native_midi_loadsong_RW(SDL_RWops *src, int freesrc)
Jan 18, 2011
Jan 18, 2011
223
224
225
{
NativeMidiSong *song = new NativeMidiSong;
song->store = new MidiEventsStore;
Jun 2, 2013
Jun 2, 2013
226
status_t res = song->store->Import(src);
Jan 18, 2011
Jan 18, 2011
227
228
229
230
231
232
233
234
if (res != B_OK)
{
snprintf(lasterr, sizeof lasterr, "Cannot Import() midi file: status_t=%d", res);
delete song->store;
delete song;
return NULL;
}
Jun 2, 2013
Jun 2, 2013
235
236
237
238
239
240
else
{
if (freesrc) {
SDL_RWclose(src);
}
}
Jan 18, 2011
Jan 18, 2011
241
242
243
244
245
246
247
248
return song;
}
void native_midi_freesong(NativeMidiSong *song)
{
if (song == NULL) return;
song->store->Stop();
song->store->Disconnect(&synth);
May 22, 2013
May 22, 2013
249
if (currentSong == song)
Jan 18, 2011
Jan 18, 2011
250
251
252
253
254
255
{
currentSong = NULL;
}
delete song->store;
delete song; song = 0;
}
Oct 18, 2017
Oct 18, 2017
256
Jan 1, 2012
Jan 1, 2012
257
void native_midi_start(NativeMidiSong *song, int loops)
Jan 18, 2011
Jan 18, 2011
258
259
260
{
native_midi_stop();
song->store->Connect(&synth);
Jan 1, 2012
Jan 1, 2012
261
song->store->SetLoops(loops);
Jan 18, 2011
Jan 18, 2011
262
263
264
song->store->Start();
currentSong = song;
}
Oct 18, 2017
Oct 18, 2017
265
266
267
268
269
270
271
272
273
void native_midi_pause(void)
{
}
void native_midi_resume(void)
{
}
Oct 17, 2017
Oct 17, 2017
274
void native_midi_stop(void)
Sep 8, 2011
Sep 8, 2011
275
276
277
278
279
280
281
282
{
if (currentSong == NULL) return;
currentSong->store->Stop();
currentSong->store->Disconnect(&synth);
while (currentSong->store->IsPlaying())
usleep(1000);
currentSong = NULL;
}
Oct 18, 2017
Oct 18, 2017
283
Oct 17, 2017
Oct 17, 2017
284
int native_midi_active(void)
Sep 8, 2011
Sep 8, 2011
285
286
{
if (currentSong == NULL) return 0;
Jan 1, 2012
Jan 1, 2012
287
return currentSong->store->IsPlaying();
Sep 8, 2011
Sep 8, 2011
288
289
290
291
292
293
294
295
}
const char* native_midi_error(void)
{
return lasterr;
}
#endif /* __HAIKU__ */