forked from sdlpal/sdlpal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
native_midi.cpp
335 lines (288 loc) · 8.33 KB
/
native_midi.cpp
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
161
162
163
164
165
166
167
168
169
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/* -*- mode: c; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
//
// Copyright (c) 2009-2011, Wei Mingzhi <whistler_wmz@users.sf.net>.
// Copyright (c) 2011-2021, SDLPAL development team.
// All rights reserved.
//
// This file is part of SDLPAL.
//
// SDLPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License, version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// native_midi.cpp: Native Windows Runtime MIDI player for SDLPal.
// @Author: Lou Yihua, 2017
//
#include "pch.h"
#include "AsyncHelper.h"
#include "NativeBuffer.h"
#include <SDL.h>
#include <memory>
#include <future>
#include <thread>
#include <condition_variable>
#include <mutex>
#include <atomic>
#include "native_midi/native_midi.h"
#include "native_midi/native_midi_common.h"
using namespace Windows::Devices::Midi;
struct MidiEvent
{
IMidiMessage^ message;
uint32_t deltaTime; // time in ticks
uint32_t tempo; // microseconds per quarter note
std::chrono::system_clock::duration DeltaTimeAsTick(uint16_t ppq)
{
return std::chrono::system_clock::duration((int64_t)deltaTime * tempo * 10 / ppq);
}
void Send(MidiSynthesizer^ synthesizer) { synthesizer->SendMessage(message); }
};
struct _NativeMidiSong {
std::vector<MidiEvent> Events;
std::thread Thread;
std::mutex Mutex;
std::condition_variable Stop;
MidiSynthesizer^ Synthesizer;
int Size;
int Position;
Uint16 ppq; // parts (ticks) per quarter note
volatile bool Playing;
bool Loaded;
bool Looping;
_NativeMidiSong()
: Synthesizer(nullptr), Size(0), Position(0)
, ppq(0), Playing(false), Loaded(false), Looping(false)
{ }
};
static MidiSystemResetMessage^ ResetMessage = ref new MidiSystemResetMessage();
enum class MidiSystemMessage {
Exclusive = 0,
TimeCode = 1,
SongPositionPointer = 2,
SongSelect = 3,
TuneRequest = 6,
TimingClock = 8,
Start = 10,
Continue = 11,
Stop = 12,
ActiveSensing = 14,
SystemReset = 15
};
static void MIDItoStream(NativeMidiSong *song, MIDIEvent *eventlist)
{
int eventcount = 0, prevtime = 0, tempo = 500000;
for (MIDIEvent* event = eventlist; event; event = event->next)
{
if (event->status != 0xFF)
eventcount++;
}
song->Events.resize(song->Size = eventcount);
song->Position = 0;
song->Loaded = true;
eventcount = 0;
for (MIDIEvent* event = eventlist; event; event = event->next)
{
IMidiMessage^ message = nullptr;
int status = (event->status & 0xF0) >> 4;
switch (status)
{
case MIDI_STATUS_NOTE_OFF:
message = ref new MidiNoteOffMessage(event->status - (status << 4), event->data[0], event->data[1]);
break;
case MIDI_STATUS_NOTE_ON:
message = ref new MidiNoteOnMessage(event->status - (status << 4), event->data[0], event->data[1]);
break;
case MIDI_STATUS_AFTERTOUCH:
message = ref new MidiPolyphonicKeyPressureMessage(event->status - (status << 4), event->data[0], event->data[1]);
break;
case MIDI_STATUS_CONTROLLER:
message = ref new MidiControlChangeMessage(event->status - (status << 4), event->data[0], event->data[1]);
break;
case MIDI_STATUS_PROG_CHANGE:
message = ref new MidiProgramChangeMessage(event->status - (status << 4), event->data[0]);
break;
case MIDI_STATUS_PRESSURE:
message = ref new MidiChannelPressureMessage(event->status - (status << 4), event->data[0]);
break;
case MIDI_STATUS_PITCH_WHEEL:
message = ref new MidiPitchBendChangeMessage(event->status - (status << 4), event->data[0] | (event->data[1] << 7));
break;
case MIDI_STATUS_SYSEX:
switch ((MidiSystemMessage)(event->status & 0xF))
{
case MidiSystemMessage::Exclusive:
{
auto buffer = NativeBuffer::GetIBuffer(event->extraData, event->extraLen);
if (buffer)
{
message = ref new MidiSystemExclusiveMessage(buffer);
delete buffer;
}
}
break;
case MidiSystemMessage::TimeCode:
message = ref new MidiTimeCodeMessage(event->extraData[0] >> 4, event->extraData[0] & 0xF);
break;
case MidiSystemMessage::SongPositionPointer:
message = ref new MidiSongPositionPointerMessage(event->extraData[0] | (event->extraData[1] << 7));
break;
case MidiSystemMessage::SongSelect:
message = ref new MidiSongSelectMessage(event->extraData[0]);
break;
case MidiSystemMessage::TuneRequest:
message = ref new MidiTuneRequestMessage();
break;
case MidiSystemMessage::TimingClock:
message = ref new MidiTimingClockMessage();
break;
case MidiSystemMessage::Start:
message = ref new MidiStartMessage();
break;
case MidiSystemMessage::Continue:
message = ref new MidiContinueMessage();
break;
case MidiSystemMessage::Stop:
message = ref new MidiStopMessage();
break;
case MidiSystemMessage::ActiveSensing:
message = ref new MidiActiveSensingMessage();
break;
case MidiSystemMessage::SystemReset:
// This message is only used as meta-event in MIDI files
if (event->data[0] == 0x51)
tempo = (event->extraData[0] << 16) | (event->extraData[1] << 8) | event->extraData[2];
break;
}
break;
}
if (message)
{
song->Events[eventcount].message = message;
song->Events[eventcount].deltaTime = event->time - prevtime;
song->Events[eventcount].tempo = tempo;
prevtime = event->time; eventcount++;
}
}
}
int native_midi_detect()
{
auto synthesizer = AWait(MidiSynthesizer::CreateAsync());
if (synthesizer)
{
delete synthesizer;
return 1;
}
return 0;
}
NativeMidiSong *native_midi_loadsong(const char *midifile)
{
/* Attempt to load the midi file */
auto rw = SDL_RWFromFile(midifile, "rb");
if (rw)
{
auto song = native_midi_loadsong_RW(rw);
SDL_RWclose(rw);
return song;
}
return nullptr;
}
NativeMidiSong *native_midi_loadsong_RW(SDL_RWops *rw)
{
std::unique_ptr<NativeMidiSong> newsong(new NativeMidiSong);
if (newsong)
{
auto evntlist = CreateMIDIEventList(rw, &newsong->ppq);
if (evntlist)
{
MIDItoStream(newsong.get(), evntlist);
FreeMIDIEventList(evntlist);
if (newsong->Synthesizer = AWait(MidiSynthesizer::CreateAsync()))
return newsong.release();
}
}
return nullptr;
}
void native_midi_freesong(NativeMidiSong *song)
{
if (song)
{
native_midi_stop(song);
if (song->Synthesizer)
delete song->Synthesizer;
delete song;
}
}
void native_midi_start(NativeMidiSong *song, int looping)
{
if (!song) return;
native_midi_stop(song);
song->Playing = true;
song->Looping = looping ? true : false;
song->Thread = std::move(std::thread([](NativeMidiSong *song)->void {
auto time = std::chrono::system_clock::now();
while (song->Playing)
{
do
{
song->Events[song->Position++].Send(song->Synthesizer);
} while (song->Position < song->Size && song->Events[song->Position].deltaTime == 0);
if (song->Position < song->Size)
{
auto mutex = std::unique_lock<std::mutex>(song->Mutex);
time += std::chrono::system_clock::duration(song->Events[song->Position].DeltaTimeAsTick(song->ppq));
while (song->Playing)
{
if (song->Stop.wait_until(mutex, time) == std::cv_status::timeout)
break;
}
}
else if (song->Playing = song->Looping)
{
song->Position = 0;
song->Synthesizer->SendMessage(ResetMessage);
}
}
}, song));
}
void native_midi_stop(NativeMidiSong *song)
{
if (song)
{
song->Playing = false;
song->Stop.notify_all();
if (song->Thread.joinable())
song->Thread.join();
song->Thread = std::move(std::thread());
if (song->Synthesizer)
{
song->Synthesizer->SendMessage(ResetMessage);
}
}
}
int native_midi_active(NativeMidiSong *song)
{
return (song && song->Playing) ? 1 : 0;
}
void native_midi_setvolume(NativeMidiSong *song, int volume)
{
if (song && song->Synthesizer)
{
if (volume > 127)
volume = 127;
else if (volume < 0)
volume = 0;
song->Synthesizer->Volume = (double)volume / 127.0;
}
}
const char *native_midi_error(NativeMidiSong *song)
{
return "";
}