| 1 |
/* Emacs style mode select -*- C -*- |
| 2 |
*----------------------------------------------------------------------------- |
| 3 |
* |
| 4 |
* |
| 5 |
* PrBoom: a Doom port merged with LxDoom and LSDLDoom |
| 6 |
* based on BOOM, a modified and improved DOOM engine |
| 7 |
* |
| 8 |
* Copyright (C) 2011 by |
| 9 |
* Nicholai Main |
| 10 |
* |
| 11 |
* This program is free software; you can redistribute it and/or |
| 12 |
* modify it under the terms of the GNU General Public License |
| 13 |
* as published by the Free Software Foundation; either version 2 |
| 14 |
* of the License, or (at your option) any later version. |
| 15 |
* |
| 16 |
* This program is distributed in the hope that it will be useful, |
| 17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
* GNU General Public License for more details. |
| 20 |
* |
| 21 |
* You should have received a copy of the GNU General Public License |
| 22 |
* along with this program; if not, write to the Free Software |
| 23 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 24 |
* 02111-1307, USA. |
| 25 |
* |
| 26 |
* DESCRIPTION: |
| 27 |
* |
| 28 |
*--------------------------------------------------------------------- |
| 29 |
*/ |
| 30 |
|
| 31 |
// TODO: some duplicated code with this and the fluidplayer should be |
| 32 |
// split off or something |
| 33 |
|
| 34 |
#ifdef HAVE_CONFIG_H |
| 35 |
#include "config.h" |
| 36 |
#endif |
| 37 |
|
| 38 |
#include "musicplayer.h" |
| 39 |
|
| 40 |
#ifndef HAVE_LIBPORTMIDI |
| 41 |
|
| 42 |
#include <stddef.h> |
| 43 |
|
| 44 |
static const char *pm_name (void) |
| 45 |
{ |
| 46 |
return "portmidi midi player (DISABLED)"; |
| 47 |
} |
| 48 |
|
| 49 |
static int pm_init (int samplerate) |
| 50 |
{ |
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
const music_player_t pm_player = |
| 55 |
{ |
| 56 |
pm_name, |
| 57 |
pm_init, |
| 58 |
NULL, |
| 59 |
NULL, |
| 60 |
NULL, |
| 61 |
NULL, |
| 62 |
NULL, |
| 63 |
NULL, |
| 64 |
NULL, |
| 65 |
NULL, |
| 66 |
NULL |
| 67 |
}; |
| 68 |
|
| 69 |
#else // HAVE_LIBPORTMIDI |
| 70 |
|
| 71 |
#include <portmidi.h> |
| 72 |
#include <porttime.h> |
| 73 |
#include <stdio.h> |
| 74 |
#include <stdlib.h> |
| 75 |
#include <string.h> |
| 76 |
|
| 77 |
#include "lprintf.h" |
| 78 |
#include "midifile.h" |
| 79 |
|
| 80 |
#include "dsda/configuration.h" |
| 81 |
|
| 82 |
static midi_event_t **events; |
| 83 |
static int eventpos; |
| 84 |
static midi_file_t *midifile; |
| 85 |
|
| 86 |
static int pm_playing; |
| 87 |
static int pm_paused; |
| 88 |
static int pm_looping; |
| 89 |
static int pm_volume = -1; |
| 90 |
static double spmc; |
| 91 |
static double pm_delta; |
| 92 |
|
| 93 |
static unsigned long trackstart; |
| 94 |
|
| 95 |
static PortMidiStream *pm_stream; |
| 96 |
|
| 97 |
#define SYSEX_BUFF_SIZE 1024 |
| 98 |
static byte sysexbuff[SYSEX_BUFF_SIZE]; |
| 99 |
static int sysexbufflen; |
| 100 |
|
| 101 |
static const char *mus_portmidi_reset_type; // portmidi reset type |
| 102 |
static int mus_portmidi_reset_delay; // portmidi delay after reset |
| 103 |
static int mus_portmidi_filter_sysex; // portmidi block sysex from midi files |
| 104 |
static int mus_portmidi_reverb_level; // portmidi reverb send level |
| 105 |
static int mus_portmidi_chorus_level; // portmidi chorus send level |
| 106 |
|
| 107 |
// latency: we're generally writing timestamps slightly in the past (from when the last time |
| 108 |
// render was called to this time. portmidi latency instruction must be larger than that window |
| 109 |
// so the messages appear in the future. ~46-47ms is the nominal length if i_sound.c gets its way |
| 110 |
#define DRIVER_LATENCY 80 // ms |
| 111 |
// driver event buffer needs to be big enough to hold however many events occur in latency time |
| 112 |
#define DRIVER_BUFFER 1024 // events |
| 113 |
|
| 114 |
static const char *pm_name (void) |
| 115 |
{ |
| 116 |
return "portmidi midi player"; |
| 117 |
} |
| 118 |
|
| 119 |
#ifdef _MSC_VER |
| 120 |
#define WIN32_LEAN_AND_MEAN |
| 121 |
#include <windows.h> |
| 122 |
#include <delayimp.h> |
| 123 |
#endif |
| 124 |
|
| 125 |
static dboolean channel_used[16]; |
| 126 |
|
| 127 |
#define DEFAULT_VOLUME 100 |
| 128 |
static int channel_volume[16]; |
| 129 |
static float volume_scale; |
| 130 |
|
| 131 |
static dboolean use_reset_delay; |
| 132 |
static byte *sysex_reset; |
| 133 |
static byte gs_reset[] = {0xf0, 0x41, 0x10, 0x42, 0x12, 0x40, 0x00, 0x7f, 0x00, 0x41, 0xf7}; |
| 134 |
static byte gm_system_on[] = {0xf0, 0x7e, 0x7f, 0x09, 0x01, 0xf7}; |
| 135 |
static byte gm2_system_on[] = {0xf0, 0x7e, 0x7f, 0x09, 0x03, 0xf7}; |
| 136 |
static byte xg_system_on[] = {0xf0, 0x43, 0x10, 0x4c, 0x00, 0x00, 0x7e, 0x00, 0xf7}; |
| 137 |
static PmEvent event_notes_off[16]; |
| 138 |
static PmEvent event_sound_off[16]; |
| 139 |
static PmEvent event_reset[16 * 6]; |
| 140 |
static PmEvent event_pbs[16 * 6]; |
| 141 |
static PmEvent event_reverb[16]; |
| 142 |
static PmEvent event_chorus[16]; |
| 143 |
|
| 144 |
static void reset_device (void) |
| 145 |
{ |
| 146 |
Pm_Write(pm_stream, event_notes_off, 16); |
| 147 |
Pm_Write(pm_stream, event_sound_off, 16); |
| 148 |
|
| 149 |
if (sysex_reset == NULL) |
| 150 |
Pm_Write(pm_stream, event_reset, 16 * 6); |
| 151 |
else |
| 152 |
Pm_WriteSysEx(pm_stream, 0, sysex_reset); |
| 153 |
|
| 154 |
Pm_Write(pm_stream, event_pbs, 16 * 6); |
| 155 |
|
| 156 |
if (mus_portmidi_reverb_level > -1 || sysex_reset == NULL) |
| 157 |
Pm_Write(pm_stream, event_reverb, 16); |
| 158 |
|
| 159 |
if (mus_portmidi_chorus_level > -1 || sysex_reset == NULL) |
| 160 |
Pm_Write(pm_stream, event_chorus, 16); |
| 161 |
|
| 162 |
use_reset_delay = mus_portmidi_reset_delay > 0; |
| 163 |
memset(channel_used, 0, sizeof(channel_used)); |
| 164 |
} |
| 165 |
|
| 166 |
static void init_reset_buffer (void) |
| 167 |
{ |
| 168 |
int i; |
| 169 |
PmEvent *reset = event_reset; |
| 170 |
PmEvent *pbs = event_pbs; |
| 171 |
int reverb = mus_portmidi_reverb_level; |
| 172 |
int chorus = mus_portmidi_chorus_level; |
| 173 |
|
| 174 |
for (i = 0; i < 16; ++i) |
| 175 |
{ |
| 176 |
event_notes_off[i].message = Pm_Message(0xB0 | i, 0x7B, 0x00); |
| 177 |
event_sound_off[i].message = Pm_Message(0xB0 | i, 0x78, 0x00); |
| 178 |
|
| 179 |
reset[0].message = Pm_Message(0xB0 | i, 0x79, 0x00); // reset all controllers |
| 180 |
reset[1].message = Pm_Message(0xB0 | i, 0x07, 0x64); // channel volume |
| 181 |
reset[2].message = Pm_Message(0xB0 | i, 0x0A, 0x40); // pan |
| 182 |
reset[3].message = Pm_Message(0xB0 | i, 0x00, 0x00); // bank select msb |
| 183 |
reset[4].message = Pm_Message(0xB0 | i, 0x20, 0x00); // bank select lsb |
| 184 |
reset[5].message = Pm_Message(0xC0 | i, 0x00, 0x00); // program change |
| 185 |
reset += 6; |
| 186 |
|
| 187 |
pbs[0].message = Pm_Message(0xB0 | i, 0x64, 0x00); // pitch bend sens RPN LSB |
| 188 |
pbs[1].message = Pm_Message(0xB0 | i, 0x65, 0x00); // pitch bend sens RPN MSB |
| 189 |
pbs[2].message = Pm_Message(0xB0 | i, 0x06, 0x02); // data entry MSB |
| 190 |
pbs[3].message = Pm_Message(0xB0 | i, 0x26, 0x00); // data entry LSB |
| 191 |
pbs[4].message = Pm_Message(0xB0 | i, 0x64, 0x7F); // null RPN LSB |
| 192 |
pbs[5].message = Pm_Message(0xB0 | i, 0x65, 0x7F); // null RPN MSB |
| 193 |
pbs += 6; |
| 194 |
} |
| 195 |
|
| 196 |
if (!strcasecmp(mus_portmidi_reset_type, "gs")) |
| 197 |
sysex_reset = gs_reset; |
| 198 |
else if (!strcasecmp(mus_portmidi_reset_type, "gm")) |
| 199 |
sysex_reset = gm_system_on; |
| 200 |
else if (!strcasecmp(mus_portmidi_reset_type, "gm2")) |
| 201 |
sysex_reset = gm2_system_on; |
| 202 |
else if (!strcasecmp(mus_portmidi_reset_type, "xg")) |
| 203 |
sysex_reset = xg_system_on; |
| 204 |
else |
| 205 |
sysex_reset = NULL; |
| 206 |
|
| 207 |
// if no reverb specified and no SysEx reset selected, then use GM default |
| 208 |
if (reverb == -1 && sysex_reset == NULL) |
| 209 |
reverb = 40; |
| 210 |
|
| 211 |
if (reverb > -1) |
| 212 |
{ |
| 213 |
for (i = 0; i < 16; ++i) |
| 214 |
event_reverb[i].message = Pm_Message(0xB0 | i, 0x5B, reverb); |
| 215 |
} |
| 216 |
|
| 217 |
// if no chorus specified and no SysEx reset selected, then use GM default |
| 218 |
if (chorus == -1 && sysex_reset == NULL) |
| 219 |
chorus = 0; |
| 220 |
|
| 221 |
if (chorus > -1) |
| 222 |
{ |
| 223 |
for (i = 0; i < 16; ++i) |
| 224 |
event_chorus[i].message = Pm_Message(0xB0 | i, 0x5D, chorus); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
static int pm_init (int samplerate) |
| 229 |
{ |
| 230 |
PmDeviceID outputdevice; |
| 231 |
const PmDeviceInfo *oinfo; |
| 232 |
int i; |
| 233 |
char devname[64]; |
| 234 |
const char* snd_mididev; |
| 235 |
|
| 236 |
mus_portmidi_reset_type = dsda_StringConfig(dsda_config_mus_portmidi_reset_type); |
| 237 |
mus_portmidi_reset_delay = dsda_IntConfig(dsda_config_mus_portmidi_reset_delay); |
| 238 |
mus_portmidi_filter_sysex = dsda_IntConfig(dsda_config_mus_portmidi_filter_sysex); |
| 239 |
mus_portmidi_reverb_level = dsda_IntConfig(dsda_config_mus_portmidi_reverb_level); |
| 240 |
mus_portmidi_chorus_level = dsda_IntConfig(dsda_config_mus_portmidi_chorus_level); |
| 241 |
|
| 242 |
if (Pm_Initialize () != pmNoError) |
| 243 |
{ |
| 244 |
lprintf (LO_WARN, "portmidiplayer: Pm_Initialize () failed\n"); |
| 245 |
return 0; |
| 246 |
} |
| 247 |
|
| 248 |
outputdevice = Pm_GetDefaultOutputDeviceID (); |
| 249 |
|
| 250 |
if (outputdevice == pmNoDevice) |
| 251 |
{ |
| 252 |
lprintf (LO_WARN, "portmidiplayer: No output devices available\n"); |
| 253 |
Pm_Terminate (); |
| 254 |
return 0; |
| 255 |
} |
| 256 |
|
| 257 |
// look for a device that matches the user preference |
| 258 |
|
| 259 |
snd_mididev = dsda_StringConfig(dsda_config_snd_mididev); |
| 260 |
|
| 261 |
lprintf (LO_DEBUG, "portmidiplayer device list:\n"); |
| 262 |
for (i = 0; i < Pm_CountDevices (); i++) |
| 263 |
{ |
| 264 |
oinfo = Pm_GetDeviceInfo (i); |
| 265 |
if (!oinfo || !oinfo->output) |
| 266 |
continue; |
| 267 |
snprintf (devname, sizeof(devname), "%s:%s", oinfo->interf, oinfo->name); |
| 268 |
if (strlen (snd_mididev) && strstr (devname, snd_mididev)) |
| 269 |
{ |
| 270 |
outputdevice = i; |
| 271 |
lprintf (LO_DEBUG, ">>%s\n", devname); |
| 272 |
} |
| 273 |
else |
| 274 |
{ |
| 275 |
lprintf (LO_DEBUG, " %s\n", devname); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
oinfo = Pm_GetDeviceInfo (outputdevice); |
| 280 |
|
| 281 |
lprintf (LO_DEBUG, "portmidiplayer: Opening device %s:%s for output\n", oinfo->interf, oinfo->name); |
| 282 |
|
| 283 |
if (Pm_OpenOutput(&pm_stream, outputdevice, NULL, DRIVER_BUFFER, NULL, NULL, DRIVER_LATENCY) != pmNoError) |
| 284 |
{ |
| 285 |
lprintf (LO_WARN, "portmidiplayer: Pm_OpenOutput () failed\n"); |
| 286 |
Pm_Terminate (); |
| 287 |
return 0; |
| 288 |
} |
| 289 |
|
| 290 |
init_reset_buffer(); |
| 291 |
reset_device(); |
| 292 |
|
| 293 |
for (int i = 0; i < 16; i++) |
| 294 |
channel_volume[i] = DEFAULT_VOLUME; |
| 295 |
|
| 296 |
return 1; |
| 297 |
} |
| 298 |
|
| 299 |
static void pm_stop (void); |
| 300 |
|
| 301 |
static void pm_shutdown (void) |
| 302 |
{ |
| 303 |
if (pm_stream) |
| 304 |
{ |
| 305 |
// stop all sound, in case of hanging notes |
| 306 |
if (pm_playing) |
| 307 |
pm_stop(); |
| 308 |
|
| 309 |
/* ugly deadlock in portmidi win32 implementation: |
| 310 |
|
| 311 |
main thread gets stuck in Pm_Close |
| 312 |
midi thread (started by windows) gets stuck in winmm_streamout_callback |
| 313 |
|
| 314 |
winapi ref says: |
| 315 |
"Applications should not call any multimedia functions from inside the callback function, |
| 316 |
as doing so can cause a deadlock. Other system functions can safely be called from the callback." |
| 317 |
|
| 318 |
winmm_streamout_callback calls midiOutUnprepareHeader. oops? |
| 319 |
|
| 320 |
|
| 321 |
since timestamps are slightly in the future, it's very possible to have some messages still in |
| 322 |
the windows midi queue when Pm_Close is called. this is normally no problem, but if one so happens |
| 323 |
to dequeue and call winmm_streamout_callback at the exact right moment... |
| 324 |
|
| 325 |
fix: at this point, we've stopped generating midi messages. sleep for more than DRIVER_LATENCY to ensure |
| 326 |
all messages are flushed. |
| 327 |
|
| 328 |
not a fix: calling Pm_Abort(); then midiStreamStop deadlocks instead of midiStreamClose. |
| 329 |
*/ |
| 330 |
Pt_Sleep (DRIVER_LATENCY * 2); |
| 331 |
|
| 332 |
Pm_Close (pm_stream); |
| 333 |
Pm_Terminate (); |
| 334 |
pm_stream = NULL; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
static const void *pm_registersong (const void *data, unsigned len) |
| 339 |
{ |
| 340 |
midimem_t mf; |
| 341 |
|
| 342 |
mf.len = len; |
| 343 |
mf.pos = 0; |
| 344 |
mf.data = data; |
| 345 |
|
| 346 |
midifile = MIDI_LoadFile (&mf); |
| 347 |
|
| 348 |
if (!midifile) |
| 349 |
{ |
| 350 |
return NULL; |
| 351 |
} |
| 352 |
|
| 353 |
events = MIDI_GenerateFlatList (midifile); |
| 354 |
if (!events) |
| 355 |
{ |
| 356 |
MIDI_FreeFile (midifile); |
| 357 |
return NULL; |
| 358 |
} |
| 359 |
eventpos = 0; |
| 360 |
|
| 361 |
spmc = MIDI_spmc (midifile, NULL, 1000); |
| 362 |
|
| 363 |
// handle not used |
| 364 |
return data; |
| 365 |
} |
| 366 |
|
| 367 |
static void writeevent (unsigned long when, int eve, int channel, int v1, int v2) |
| 368 |
{ |
| 369 |
PmMessage m; |
| 370 |
|
| 371 |
m = Pm_Message (eve | channel, v1, v2); |
| 372 |
Pm_WriteShort (pm_stream, when, m); |
| 373 |
} |
| 374 |
|
| 375 |
static void write_volume (unsigned long when, int channel, int volume) |
| 376 |
{ |
| 377 |
int vol = volume * volume_scale + 0.5f; |
| 378 |
writeevent (when, MIDI_EVENT_CONTROLLER, channel, MIDI_CONTROLLER_MAIN_VOLUME, vol); |
| 379 |
channel_volume[channel] = volume; |
| 380 |
} |
| 381 |
|
| 382 |
static void update_volume (void) |
| 383 |
{ |
| 384 |
for (int i = 0; i < 16; i++) |
| 385 |
write_volume (0, i, channel_volume[i]); |
| 386 |
} |
| 387 |
|
| 388 |
static void reset_volume (void) |
| 389 |
{ |
| 390 |
for (int i = 0; i < 16; i++) |
| 391 |
write_volume (0, i, DEFAULT_VOLUME); |
| 392 |
} |
| 393 |
|
| 394 |
static void pm_setvolume (int v) |
| 395 |
{ |
| 396 |
if (pm_volume == v) |
| 397 |
return; |
| 398 |
|
| 399 |
pm_volume = v; |
| 400 |
volume_scale = pm_volume / 15.0f; |
| 401 |
update_volume(); |
| 402 |
} |
| 403 |
|
| 404 |
static void pm_unregistersong (const void *handle) |
| 405 |
{ |
| 406 |
if (events) |
| 407 |
{ |
| 408 |
MIDI_DestroyFlatList (events); |
| 409 |
events = NULL; |
| 410 |
} |
| 411 |
if (midifile) |
| 412 |
{ |
| 413 |
MIDI_FreeFile (midifile); |
| 414 |
midifile = NULL; |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
static void pm_pause (void) |
| 419 |
{ |
| 420 |
pm_paused = 1; |
| 421 |
Pm_Write(pm_stream, event_notes_off, 16); |
| 422 |
Pm_Write(pm_stream, event_sound_off, 16); |
| 423 |
} |
| 424 |
|
| 425 |
static void pm_resume (void) |
| 426 |
{ |
| 427 |
pm_paused = 0; |
| 428 |
trackstart = Pt_Time (); |
| 429 |
} |
| 430 |
|
| 431 |
static void pm_play (const void *handle, int looping) |
| 432 |
{ |
| 433 |
eventpos = 0; |
| 434 |
pm_looping = looping; |
| 435 |
pm_playing = 1; |
| 436 |
pm_delta = 0.0; |
| 437 |
if (pm_volume != -1) // set pm_volume first, see pm_setvolume() |
| 438 |
reset_volume(); |
| 439 |
trackstart = Pt_Time (); |
| 440 |
} |
| 441 |
|
| 442 |
static dboolean is_sysex_reset (byte *msg, int len) |
| 443 |
{ |
| 444 |
if (len < 6) |
| 445 |
return false; |
| 446 |
|
| 447 |
switch (msg[1]) |
| 448 |
{ |
| 449 |
case 0x41: // roland |
| 450 |
switch (msg[3]) |
| 451 |
{ |
| 452 |
case 0x42: // gs |
| 453 |
switch (msg[4]) |
| 454 |
{ |
| 455 |
case 0x12: // dt1 |
| 456 |
if (len == 11 && |
| 457 |
msg[5] == 0x00 && // address msb |
| 458 |
msg[6] == 0x00 && // address |
| 459 |
msg[7] == 0x7F && // address lsb |
| 460 |
((msg[8] == 0x00 && // data (mode-1) |
| 461 |
msg[9] == 0x01) || // checksum (mode-1) |
| 462 |
(msg[8] == 0x01 && // data (mode-2) |
| 463 |
msg[9] == 0x00))) // checksum (mode-2) |
| 464 |
{ |
| 465 |
// sc-88 system mode set |
| 466 |
// F0 41 <dev> 42 12 00 00 7F 00 01 F7 (mode-1) |
| 467 |
// F0 41 <dev> 42 12 00 00 7F 01 00 F7 (mode-2) |
| 468 |
return true; |
| 469 |
} |
| 470 |
else if (len == 11 && |
| 471 |
msg[5] == 0x40 && // address msb |
| 472 |
msg[6] == 0x00 && // address |
| 473 |
msg[7] == 0x7F && // address lsb |
| 474 |
msg[8] == 0x00 && // data (gs reset) |
| 475 |
msg[9] == 0x41) // checksum |
| 476 |
{ |
| 477 |
// gs reset |
| 478 |
// F0 41 <dev> 42 12 40 00 7F 00 41 F7 |
| 479 |
return true; |
| 480 |
} |
| 481 |
break; |
| 482 |
} |
| 483 |
break; |
| 484 |
} |
| 485 |
break; |
| 486 |
|
| 487 |
case 0x43: // yamaha |
| 488 |
switch (msg[3]) |
| 489 |
{ |
| 490 |
case 0x2B: // tg300 |
| 491 |
if (len == 10 && |
| 492 |
msg[4] == 0x00 && // start address b20 - b14 |
| 493 |
msg[5] == 0x00 && // start address b13 - b7 |
| 494 |
msg[6] == 0x7F && // start address b6 - b0 |
| 495 |
msg[7] == 0x00 && // data |
| 496 |
msg[8] == 0x01) // checksum |
| 497 |
{ |
| 498 |
// tg300 all parameter reset |
| 499 |
// F0 43 <dev> 2B 00 00 7F 00 01 F7 |
| 500 |
return true; |
| 501 |
} |
| 502 |
break; |
| 503 |
|
| 504 |
case 0x4C: // xg |
| 505 |
if (len == 9 && |
| 506 |
msg[4] == 0x00 && // address high |
| 507 |
msg[5] == 0x00 && // address mid |
| 508 |
(msg[6] == 0x7E || // address low (xg system on) |
| 509 |
msg[6] == 0x7F) && // address low (xg all parameter reset) |
| 510 |
msg[7] == 0x00) // data |
| 511 |
{ |
| 512 |
// xg system on, xg all parameter reset |
| 513 |
// F0 43 <dev> 4C 00 00 7E 00 F7 |
| 514 |
// F0 43 <dev> 4C 00 00 7F 00 F7 |
| 515 |
return true; |
| 516 |
} |
| 517 |
break; |
| 518 |
} |
| 519 |
break; |
| 520 |
|
| 521 |
case 0x7E: // universal non-real time |
| 522 |
switch (msg[3]) |
| 523 |
{ |
| 524 |
case 0x09: // general midi |
| 525 |
if (len == 6 && |
| 526 |
(msg[4] == 0x01 || // gm system on |
| 527 |
msg[4] == 0x02 || // gm system off |
| 528 |
msg[4] == 0x03)) // gm2 system on |
| 529 |
{ |
| 530 |
// gm system on/off, gm2 system on |
| 531 |
// F0 7E <dev> 09 01 F7 |
| 532 |
// F0 7E <dev> 09 02 F7 |
| 533 |
// F0 7E <dev> 09 03 F7 |
| 534 |
return true; |
| 535 |
} |
| 536 |
break; |
| 537 |
} |
| 538 |
break; |
| 539 |
} |
| 540 |
|
| 541 |
return false; |
| 542 |
} |
| 543 |
|
| 544 |
static void writesysex (unsigned long when, int etype, byte *data, int len) |
| 545 |
{ |
| 546 |
// sysex messages in midi files (smf 1.0 pages 6-7): |
| 547 |
// complete: (F0 ... F7) |
| 548 |
// multi-packet: (F0 ...) + (F7 ...) + ... + (F7 ... F7) |
| 549 |
// escape sequence: (F7 ...) |
| 550 |
|
| 551 |
if (len + sysexbufflen > SYSEX_BUFF_SIZE - 1) |
| 552 |
{ |
| 553 |
// ignore messages that are too long |
| 554 |
sysexbufflen = 0; |
| 555 |
return; |
| 556 |
} |
| 557 |
|
| 558 |
if (etype == MIDI_EVENT_SYSEX_SPLIT && sysexbufflen == 0) |
| 559 |
{ |
| 560 |
// ignore escape sequence |
| 561 |
return; |
| 562 |
} |
| 563 |
|
| 564 |
if (etype == MIDI_EVENT_SYSEX) |
| 565 |
{ |
| 566 |
// start a new message (discards any previous incomplete message) |
| 567 |
sysexbuff[0] = MIDI_EVENT_SYSEX; |
| 568 |
sysexbufflen = 1; |
| 569 |
} |
| 570 |
|
| 571 |
memcpy (sysexbuff + sysexbufflen, data, len); |
| 572 |
sysexbufflen += len; |
| 573 |
|
| 574 |
// process message if it's complete, otherwise do nothing yet |
| 575 |
if (sysexbuff[sysexbufflen - 1] == MIDI_EVENT_SYSEX_SPLIT) |
| 576 |
{ |
| 577 |
Pm_WriteSysEx (pm_stream, when, sysexbuff); |
| 578 |
|
| 579 |
if (is_sysex_reset(sysexbuff, sysexbufflen)) |
| 580 |
{ |
| 581 |
reset_volume(); |
| 582 |
memset(channel_used, 0, sizeof(channel_used)); |
| 583 |
} |
| 584 |
|
| 585 |
sysexbufflen = 0; |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
static void pm_stop (void) |
| 590 |
{ |
| 591 |
pm_playing = 0; |
| 592 |
|
| 593 |
// songs can be stopped at any time, so reset everything |
| 594 |
reset_device(); |
| 595 |
|
| 596 |
// abort any partial sysex |
| 597 |
sysexbufflen = 0; |
| 598 |
} |
| 599 |
|
| 600 |
static void pm_render (void *vdest, unsigned bufflen) |
| 601 |
{ |
| 602 |
// wherever you see samples in here, think milliseconds |
| 603 |
unsigned long when = trackstart; |
| 604 |
unsigned long newtime = Pt_Time (); |
| 605 |
unsigned int samples; |
| 606 |
|
| 607 |
memset (vdest, 0, bufflen * 4); |
| 608 |
|
| 609 |
if (!pm_playing || pm_paused) |
| 610 |
return; |
| 611 |
|
| 612 |
while (1) |
| 613 |
{ |
| 614 |
midi_event_t *currevent = events[eventpos]; |
| 615 |
|
| 616 |
// how many samples away event is |
| 617 |
double eventdelta = currevent->delta_time * spmc; |
| 618 |
|
| 619 |
// delay after reset, for real devices only (e.g. roland sc-55) |
| 620 |
if (use_reset_delay) |
| 621 |
eventdelta += mus_portmidi_reset_delay; |
| 622 |
|
| 623 |
// how many we will render (rounding down); include delta offset |
| 624 |
samples = eventdelta + pm_delta; |
| 625 |
|
| 626 |
if (when + samples > newtime) |
| 627 |
{ |
| 628 |
// overshoot; render some samples without processing an event |
| 629 |
pm_delta -= (newtime - when); // save offset |
| 630 |
trackstart = newtime; |
| 631 |
return; |
| 632 |
} |
| 633 |
|
| 634 |
use_reset_delay = false; |
| 635 |
pm_delta += eventdelta - samples; |
| 636 |
when += samples; |
| 637 |
|
| 638 |
switch (currevent->event_type) |
| 639 |
{ |
| 640 |
case MIDI_EVENT_SYSEX: |
| 641 |
case MIDI_EVENT_SYSEX_SPLIT: |
| 642 |
if (!mus_portmidi_filter_sysex) |
| 643 |
writesysex (when, currevent->event_type, currevent->data.sysex.data, currevent->data.sysex.length); |
| 644 |
break; |
| 645 |
case MIDI_EVENT_META: |
| 646 |
switch (currevent->data.meta.type) |
| 647 |
{ |
| 648 |
case MIDI_META_SET_TEMPO: |
| 649 |
spmc = MIDI_spmc (midifile, currevent, 1000); |
| 650 |
break; |
| 651 |
case MIDI_META_END_OF_TRACK: |
| 652 |
if (pm_looping) |
| 653 |
{ |
| 654 |
eventpos = 0; |
| 655 |
// prevent hanging notes (doom2.wad MAP14, MAP22) |
| 656 |
for (int i = 0; i < 16; i++) |
| 657 |
{ |
| 658 |
if (channel_used[i]) |
| 659 |
{ |
| 660 |
writeevent(when, 0xB0, i, 0x79, 0x00); // reset all controllers |
| 661 |
write_volume(when, i, DEFAULT_VOLUME); // reset volume |
| 662 |
channel_used[i] = false; |
| 663 |
} |
| 664 |
} |
| 665 |
continue; |
| 666 |
} |
| 667 |
pm_stop(); |
| 668 |
return; |
| 669 |
} |
| 670 |
break; // not interested in most metas |
| 671 |
case MIDI_EVENT_CONTROLLER: |
| 672 |
if (currevent->data.channel.param1 == MIDI_CONTROLLER_MAIN_VOLUME) |
| 673 |
{ |
| 674 |
write_volume (when, currevent->data.channel.channel, currevent->data.channel.param2); |
| 675 |
channel_used[currevent->data.channel.channel] = true; |
| 676 |
break; |
| 677 |
} |
| 678 |
else if (currevent->data.channel.param1 == 0x79) |
| 679 |
{ |
| 680 |
// ms gs synth resets volume if "reset all controllers" value isn't zero |
| 681 |
writeevent (when, 0xB0, currevent->data.channel.channel, 0x79, 0x00); |
| 682 |
channel_used[currevent->data.channel.channel] = true; |
| 683 |
break; |
| 684 |
} |
| 685 |
// fall through |
| 686 |
default: |
| 687 |
writeevent (when, currevent->event_type, currevent->data.channel.channel, currevent->data.channel.param1, currevent->data.channel.param2); |
| 688 |
channel_used[currevent->data.channel.channel] = true; |
| 689 |
break; |
| 690 |
} |
| 691 |
|
| 692 |
eventpos++; |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
const music_player_t pm_player = |
| 697 |
{ |
| 698 |
pm_name, |
| 699 |
pm_init, |
| 700 |
pm_shutdown, |
| 701 |
pm_setvolume, |
| 702 |
pm_pause, |
| 703 |
pm_resume, |
| 704 |
pm_registersong, |
| 705 |
pm_unregistersong, |
| 706 |
pm_play, |
| 707 |
pm_stop, |
| 708 |
pm_render |
| 709 |
}; |
| 710 |
|
| 711 |
#endif // HAVE_LIBPORTMIDI |