-
Notifications
You must be signed in to change notification settings - Fork 3
/
WinMIDIPlayer.h
800 lines (705 loc) · 23.4 KB
/
WinMIDIPlayer.h
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
//----------------------------------------------------------------------------------------------------------------------
// MIDI Player library using Win32 WinMM MIDI streaming API
// Copyright (c) 2024 Samuel Gomes
//
// This is a heavily modified version of the Win32 native MIDI codec from SDL_mixer
// https://github.com/libsdl-org/SDL_mixer/tree/main/src/codecs/native_midi
//
// native_midi: Hardware Midi support for the SDL_mixer library
// Copyright(C) 2000, 2001 Florian 'Proff' Schulze <florian.proff.schulze@gmx.net>
// 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.
// 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 :
// 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.
//----------------------------------------------------------------------------------------------------------------------
#pragma once
#include "Common.h"
#include "Types.h"
#include "Math/Math.h"
#include "MemFile.h"
#include <algorithm>
#include <cstdint>
#include <windows.h>
/* MIDI Status Bytes */
#define MIDI_STATUS_NOTE_OFF 0x8
#define MIDI_STATUS_NOTE_ON 0x9
#define MIDI_STATUS_AFTERTOUCH 0xA
#define MIDI_STATUS_CONTROLLER 0xB
#define MIDI_STATUS_PROG_CHANGE 0xC
#define MIDI_STATUS_PRESSURE 0xD
#define MIDI_STATUS_PITCH_WHEEL 0xE
#define MIDI_STATUS_SYSEX 0xF
/* The constant 'MThd' */
#define MIDI_MAGIC 0x4d546864
/* We store the midi events in a linked list; this way it is
easy to shuffle the tracks together later on; and we are
flexible in the size of each element.
*/
struct MIDIEvent
{
uint32_t time; /* Time at which this midi events occurs */
uint8_t status; /* Status byte */
uint8_t data[2]; /* 1 or 2 bytes additional data for most events */
uint32_t extraLen; /* For some SysEx events, we need additional storage */
uint8_t *extraData;
MIDIEvent *next;
};
/* A single midi track as read from the midi file */
struct MIDITrack
{
uint8_t *data; /* MIDI message stream */
int len; /* length of the track data */
};
/* A midi file, stripped down to the absolute minimum - division & track data */
struct MIDIFile
{
int division; /* number of pulses per quarter note (ppqn) */
int nTracks; /* number of tracks */
MIDITrack *track; /* tracks */
};
struct MIDISong
{
bool MusicLoaded;
bool MusicPlaying;
bool MusicPaused;
int Loops;
int CurrentHdr;
MIDIHDR MIDIStreamHdr[2];
MIDIEVENT *NewEvents;
uint16_t ppqn;
int Size;
int NewPos;
};
static UINT MIDIDevice = MIDI_MAPPER;
static HMIDISTRM hMIDIStream = nullptr;
static MIDISong *pMIDISong = nullptr;
/* Get Variable Length Quantity */
static int MIDIGetVLQ(MIDITrack *track, int *currentPos)
{
int l = 0;
uint8_t c;
for (;;)
{
c = track->data[*currentPos];
(*currentPos)++;
l += (c & 0x7f);
if (!(c & 0x80))
return l;
l <<= 7;
}
}
/* Create a single MIDIEvent */
static MIDIEvent *MIDICreateEvent(uint32_t time, uint8_t evnt, uint8_t a, uint8_t b)
{
auto newEvent = (MIDIEvent *)calloc(1, sizeof(MIDIEvent));
if (newEvent)
{
newEvent->time = time;
newEvent->status = evnt;
newEvent->data[0] = a;
newEvent->data[1] = b;
}
return newEvent;
}
/* Release a MIDIEvent list after usage. */
static void MIDIFreeEventList(MIDIEvent *head)
{
MIDIEvent *cur, *next;
cur = head;
while (cur)
{
next = cur->next;
if (cur->extraData)
free(cur->extraData);
free(cur);
cur = next;
}
}
/* Convert a single midi track to a list of MIDIEvents */
static MIDIEvent *MIDITrackToStream(MIDITrack *track)
{
uint32_t atime = 0;
uint32_t len = 0;
uint8_t evnt, type, a, b;
uint8_t laststatus = 0;
uint8_t lastchan = 0;
int currentPos = 0;
int end = 0;
MIDIEvent *head = MIDICreateEvent(0, 0, 0, 0); /* dummy event to make handling the list easier */
MIDIEvent *currentEvent = head;
while (!end)
{
if (currentPos >= track->len)
break; /* End of data stream reached */
atime += MIDIGetVLQ(track, ¤tPos);
evnt = track->data[currentPos++];
/* Handle SysEx seperatly */
if (((evnt >> 4) & 0x0F) == MIDI_STATUS_SYSEX)
{
if (evnt == 0xFF)
{
type = track->data[currentPos];
currentPos++;
switch (type)
{
case 0x2f: /* End of data marker */
end = 1;
[[fallthrough]];
case 0x51: /* Tempo change */
/*
a=track->data[currentPos];
b=track->data[currentPos+1];
c=track->data[currentPos+2];
AddEvent(song, atime, MEVT_TEMPO, c, b, a);
*/
break;
}
}
else
type = 0;
len = MIDIGetVLQ(track, ¤tPos);
/* Create an event and attach the extra data, if any */
currentEvent->next = MIDICreateEvent(atime, evnt, type, 0);
currentEvent = currentEvent->next;
if (nullptr == currentEvent)
{
MIDIFreeEventList(head);
return nullptr;
}
if (len)
{
currentEvent->extraLen = len;
currentEvent->extraData = (uint8_t *)malloc(len);
if (currentEvent->extraData)
memcpy(currentEvent->extraData, &(track->data[currentPos]), len);
currentPos += len;
}
}
else
{
a = evnt;
if (a & 0x80) /* It's a status byte */
{
/* Extract channel and status information */
lastchan = a & 0x0F;
laststatus = (a >> 4) & 0x0F;
/* Read the next byte which should always be a data byte */
a = track->data[currentPos++] & 0x7F;
}
switch (laststatus)
{
case MIDI_STATUS_NOTE_OFF:
case MIDI_STATUS_NOTE_ON: /* Note on */
case MIDI_STATUS_AFTERTOUCH: /* Key Pressure */
case MIDI_STATUS_CONTROLLER: /* Control change */
case MIDI_STATUS_PITCH_WHEEL: /* Pitch wheel */
b = track->data[currentPos++] & 0x7F;
currentEvent->next = MIDICreateEvent(atime, (uint8_t)((laststatus << 4) + lastchan), a, b);
currentEvent = currentEvent->next;
if (nullptr == currentEvent)
{
MIDIFreeEventList(head);
return nullptr;
}
break;
case MIDI_STATUS_PROG_CHANGE: /* Program change */
case MIDI_STATUS_PRESSURE: /* Channel pressure */
a &= 0x7f;
currentEvent->next = MIDICreateEvent(atime, (uint8_t)((laststatus << 4) + lastchan), a, 0);
currentEvent = currentEvent->next;
if (nullptr == currentEvent)
{
MIDIFreeEventList(head);
return nullptr;
}
break;
default: /* Sysex already handled above */
break;
}
}
}
currentEvent = head->next;
free(head); /* release the dummy head event */
return currentEvent;
}
/*
* Convert a midi song, consisting of up to 32 tracks, to a list of MIDIEvents.
* To do so, first convert the tracks seperatly, then interweave the resulting
* MIDIEvent-Lists to one big list.
*/
static MIDIEvent *MIDIToStream(MIDIFile *mididata)
{
MIDIEvent **track;
MIDIEvent *head = MIDICreateEvent(0, 0, 0, 0); /* dummy event to make handling the list easier */
MIDIEvent *currentEvent = head;
int trackID;
if (nullptr == head)
return nullptr;
track = (MIDIEvent **)calloc(1, sizeof(MIDIEvent *) * mididata->nTracks);
if (nullptr == track)
{
free(head);
return nullptr;
}
/* First, convert all tracks to MIDIEvent lists */
for (trackID = 0; trackID < mididata->nTracks; trackID++)
track[trackID] = MIDITrackToStream(&mididata->track[trackID]);
/* Now, merge the lists. */
/* TODO */
for (;;)
{
uint32_t lowestTime = INT_MAX;
int currentTrackID = -1;
/* Find the next event */
for (trackID = 0; trackID < mididata->nTracks; trackID++)
{
if (track[trackID] && (track[trackID]->time < lowestTime))
{
currentTrackID = trackID;
lowestTime = track[currentTrackID]->time;
}
}
/* Check if we processes all events */
if (currentTrackID == -1)
break;
currentEvent->next = track[currentTrackID];
track[currentTrackID] = track[currentTrackID]->next;
currentEvent = currentEvent->next;
lowestTime = 0;
}
/* Make sure the list is properly terminated */
currentEvent->next = 0;
currentEvent = head->next;
free(track);
free(head); /* release the dummy head event */
return currentEvent;
}
static bool MIDIReadFile(MIDIFile *mididata, uintptr_t src)
{
int i = 0;
uint32_t ID;
uint32_t size;
uint16_t format;
uint16_t tracks;
uint16_t division;
if (!mididata)
return false;
if (!src)
return false;
/* Make sure this is really a MIDI file */
ID = MemFile_ReadLong(src);
if (__builtin_bswap32(ID) != MIDI_MAGIC)
return false;
/* Header size must be 6 */
size = MemFile_ReadLong(src);
size = __builtin_bswap32(size);
if (size != 6)
return false;
/* We only support format 0 and 1, but not 2 */
format = MemFile_ReadInteger(src);
format = __builtin_bswap16(format);
if (format != 0 && format != 1)
return false;
tracks = MemFile_ReadInteger(src);
tracks = __builtin_bswap16(tracks);
mididata->nTracks = tracks;
/* Allocate tracks */
mididata->track = (MIDITrack *)calloc(1, sizeof(MIDITrack) * mididata->nTracks);
if (nullptr == mididata->track)
{
goto bail;
}
/* Retrieve the PPQN value, needed for playback */
division = MemFile_ReadInteger(src);
mididata->division = __builtin_bswap16(division);
for (i = 0; i < tracks; i++)
{
ID = MemFile_ReadLong(src); /* We might want to verify this is MTrk... */
TOOLBOX64_DEBUG_CHECK(__builtin_bswap32(ID) == 0x4d54726b);
size = MemFile_ReadLong(src);
size = __builtin_bswap32(size);
mididata->track[i].len = size;
mididata->track[i].data = (uint8_t *)malloc(size);
if (nullptr == mididata->track[i].data)
{
goto bail;
}
MemFile_Read(src, reinterpret_cast<uintptr_t>(mididata->track[i].data), size);
}
return true;
bail:
for (; i >= 0; i--)
{
if (mididata->track[i].data)
free(mididata->track[i].data);
}
return false;
}
/* Load a midifile to memory, converting it to a list of MIDIEvents.
This function returns a linked lists of MIDIEvents, 0 if an error occurred.
*/
static MIDIEvent *MIDICreateEventList(uintptr_t src, uint16_t *division)
{
MIDIFile *mididata = nullptr;
MIDIEvent *eventList;
int trackID;
mididata = (MIDIFile *)calloc(1, sizeof(MIDIFile));
if (!mididata)
return nullptr;
/* Open the file */
if (src)
{
/* Read in the data */
if (!MIDIReadFile(mididata, src))
{
free(mididata);
return nullptr;
}
}
else
{
free(mididata);
return nullptr;
}
if (division)
*division = (uint16_t)mididata->division;
eventList = MIDIToStream(mididata);
if (nullptr == eventList)
{
free(mididata);
return nullptr;
}
for (trackID = 0; trackID < mididata->nTracks; trackID++)
{
if (mididata->track[trackID].data)
free(mididata->track[trackID].data);
}
free(mididata->track);
free(mididata);
return eventList;
}
static bool MIDIBlockOut()
{
if ((pMIDISong->MusicLoaded) && (pMIDISong->NewEvents))
{
// proff 12/8/98: Added for safety
pMIDISong->CurrentHdr = !pMIDISong->CurrentHdr;
MIDIHDR *hdr = &pMIDISong->MIDIStreamHdr[pMIDISong->CurrentHdr];
midiOutUnprepareHeader((HMIDIOUT)hMIDIStream, hdr, sizeof(MIDIHDR));
if (pMIDISong->NewPos >= pMIDISong->Size)
return false;
int BlockSize = (pMIDISong->Size - pMIDISong->NewPos);
if (BlockSize <= 0)
return false;
if (BlockSize > 36000)
BlockSize = 36000;
hdr->lpData = (LPSTR)((unsigned char *)pMIDISong->NewEvents + pMIDISong->NewPos);
pMIDISong->NewPos += BlockSize;
hdr->dwBufferLength = BlockSize;
hdr->dwBytesRecorded = BlockSize;
hdr->dwFlags = 0;
hdr->dwOffset = 0;
MMRESULT err = midiOutPrepareHeader((HMIDIOUT)hMIDIStream, hdr, sizeof(MIDIHDR));
if (err != MMSYSERR_NOERROR)
return false;
err = midiStreamOut(hMIDIStream, hdr, sizeof(MIDIHDR));
return false;
}
return true;
}
static void MIDIToStream(MIDIEvent *evntlist)
{
int eventcount;
MIDIEvent *evnt;
MIDIEVENT *newevent;
eventcount = 0;
evnt = evntlist;
while (evnt)
{
eventcount++;
evnt = evnt->next;
}
pMIDISong->NewEvents = (MIDIEVENT *)malloc(eventcount * 3 * sizeof(DWORD));
if (!pMIDISong->NewEvents)
return;
memset(pMIDISong->NewEvents, 0, (eventcount * 3 * sizeof(DWORD)));
eventcount = 0;
evnt = evntlist;
newevent = pMIDISong->NewEvents;
while (evnt)
{
int status = (evnt->status & 0xF0) >> 4;
switch (status)
{
case MIDI_STATUS_NOTE_OFF:
case MIDI_STATUS_NOTE_ON:
case MIDI_STATUS_AFTERTOUCH:
case MIDI_STATUS_CONTROLLER:
case MIDI_STATUS_PROG_CHANGE:
case MIDI_STATUS_PRESSURE:
case MIDI_STATUS_PITCH_WHEEL:
newevent->dwDeltaTime = evnt->time;
newevent->dwEvent = (evnt->status | 0x80) | (evnt->data[0] << 8) | (evnt->data[1] << 16) | (MEVT_SHORTMSG << 24);
newevent = (MIDIEVENT *)((char *)newevent + (3 * sizeof(DWORD)));
eventcount++;
break;
case MIDI_STATUS_SYSEX:
if (evnt->status == 0xFF && evnt->data[0] == 0x51) /* Tempo change */
{
int tempo = evnt->extraData ? ((evnt->extraData[0] << 16) | (evnt->extraData[1] << 8) | evnt->extraData[2]) : 120;
newevent->dwDeltaTime = evnt->time;
newevent->dwEvent = (MEVT_TEMPO << 24) | tempo;
newevent = (MIDIEVENT *)((char *)newevent + (3 * sizeof(DWORD)));
eventcount++;
}
break;
}
evnt = evnt->next;
}
pMIDISong->Size = eventcount * 3 * sizeof(DWORD);
{
int time;
int temptime;
pMIDISong->NewPos = 0;
time = 0;
newevent = pMIDISong->NewEvents;
while (pMIDISong->NewPos < pMIDISong->Size)
{
temptime = newevent->dwDeltaTime;
newevent->dwDeltaTime -= time;
time = temptime;
if ((pMIDISong->NewPos + 12) >= pMIDISong->Size)
newevent->dwEvent |= MEVT_F_CALLBACK;
newevent = (MIDIEVENT *)((char *)newevent + (3 * sizeof(DWORD)));
pMIDISong->NewPos += 12;
}
}
pMIDISong->NewPos = 0;
pMIDISong->MusicLoaded = true;
}
static void CALLBACK MIDIProc(HMIDIIN hMIDI, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
{
(void)hMIDI;
(void)dwInstance;
(void)dwParam2;
switch (uMsg)
{
case MOM_DONE:
if ((pMIDISong->MusicLoaded) && (dwParam1 == (DWORD_PTR)&pMIDISong->MIDIStreamHdr[pMIDISong->CurrentHdr]))
MIDIBlockOut();
break;
case MOM_POSITIONCB:
if ((pMIDISong->MusicLoaded) && (dwParam1 == (DWORD_PTR)&pMIDISong->MIDIStreamHdr[pMIDISong->CurrentHdr]))
{
if (pMIDISong->Loops)
{
if (pMIDISong->Loops > 0)
--pMIDISong->Loops;
pMIDISong->NewPos = 0;
MIDIBlockOut();
}
else
{
pMIDISong->MusicPlaying = false;
}
}
break;
default:
break;
}
}
/// @brief This loads and starts playing a MIDI file from memory.
/// Specifying a new file while another one is playing will stop the previous file and then start playing the new one.
/// If buffer is NULL, then it will shutdown MIDI playback and free allocated resources
/// @param buffer A buffer containing a Standard MIDI file
/// @param bufferSize The size of the buffer
/// @return QB_TRUE if the call succeeded. QB_FALSE otherwise
inline qb_bool __MIDI_PlayFromMemory(const char *buffer, size_t bufferSize)
{
static auto isMIDIAvailable = false;
static auto isMIDIAvailableChecked = false;
// If initial detection failed then don't bother
if (isMIDIAvailableChecked && !isMIDIAvailable)
return QB_FALSE;
// if we have not tried MIDI detection then attempt it and set the detection flag
if (!isMIDIAvailable)
{
isMIDIAvailableChecked = true;
auto merr = midiStreamOpen(&hMIDIStream, &MIDIDevice, (DWORD)1, (DWORD_PTR)MIDIProc, (DWORD_PTR)0, CALLBACK_FUNCTION);
if (merr != MMSYSERR_NOERROR)
{
midiStreamClose(hMIDIStream);
hMIDIStream = nullptr;
isMIDIAvailable = false;
return QB_FALSE;
}
// The MIDI steam will be closed by the if block below
isMIDIAvailable = true;
}
// Close any MIDI streams being used
if (hMIDIStream)
{
midiStreamStop(hMIDIStream);
midiStreamClose(hMIDIStream);
hMIDIStream = nullptr;
}
// Free any playing song
if (pMIDISong)
{
if (pMIDISong->NewEvents)
free(pMIDISong->NewEvents);
free(pMIDISong);
pMIDISong = nullptr;
}
// Open the MIDI file only if filename is not NULL
if (!buffer || !bufferSize)
{
return QB_TRUE; // Shutdown successfull
}
else
{
auto f = MemFile_Create(reinterpret_cast<uintptr_t>(buffer), bufferSize);
if (!f)
{
return QB_FALSE;
}
pMIDISong = (MIDISong *)malloc(sizeof(MIDISong));
if (!pMIDISong)
{
MemFile_Destroy(f);
return QB_FALSE;
}
ZERO_OBJECT(pMIDISong);
/* Attempt to load the midi file */
MIDIEvent *evntlist = nullptr;
evntlist = MIDICreateEventList(f, &pMIDISong->ppqn);
if (!evntlist)
{
free(pMIDISong);
pMIDISong = nullptr;
MemFile_Destroy(f);
return QB_FALSE;
}
MIDIToStream(evntlist);
MIDIFreeEventList(evntlist);
auto merr = midiStreamOpen(&hMIDIStream, &MIDIDevice, (DWORD)1, (DWORD_PTR)MIDIProc, (DWORD_PTR)0, CALLBACK_FUNCTION);
if (merr != MMSYSERR_NOERROR)
{
hMIDIStream = nullptr;
free(pMIDISong);
pMIDISong = nullptr;
MemFile_Destroy(f);
return QB_FALSE;
}
pMIDISong->NewPos = 0;
pMIDISong->MusicPlaying = true;
pMIDISong->Loops = 0; // play only once by default
MIDIPROPTIMEDIV mptd = {};
mptd.cbStruct = sizeof(MIDIPROPTIMEDIV);
mptd.dwTimeDiv = pMIDISong->ppqn;
merr = midiStreamProperty(hMIDIStream, (LPBYTE)&mptd, MIDIPROP_SET | MIDIPROP_TIMEDIV);
MIDIBlockOut();
merr = midiStreamRestart(hMIDIStream);
MemFile_Destroy(f);
return QB_TRUE;
}
return QB_FALSE;
}
/// @brief Stops playback, unloads the MIDI file from memory and frees any allocated resource
void MIDI_Stop()
{
__MIDI_PlayFromMemory(nullptr, 0);
}
/// <summary>
/// Checks if a MIDI song is playing
/// </summary>
/// <returns>True if playing. False otherwise</returns>
inline qb_bool MIDI_IsPlaying()
{
return pMIDISong && pMIDISong->MusicPlaying ? QB_TRUE : QB_FALSE;
}
/// @brief Sets a MIDI tune to loop
/// @param loops The number of times the playback should loop. Playback can be looped forever by specifying a negative number (like QB_TRUE)
void MIDI_Loop(int32_t loops)
{
if (pMIDISong)
pMIDISong->Loops = loops;
}
/// @brief Returns true if the file is set to loop forever or is still looping
/// @return QB_TRUE if looping, QB_FALSE otherwise
inline qb_bool MIDI_IsLooping()
{
return pMIDISong && pMIDISong->Loops ? QB_TRUE : QB_FALSE;
}
/// @brief Pauses or unpauses MIDI playback
/// @param pause True to pause or False to unpause
void MIDI_Pause(int8_t state)
{
if (hMIDIStream && pMIDISong)
{
if (state)
{
midiStreamPause(hMIDIStream);
pMIDISong->MusicPaused = true;
}
else
{
midiStreamRestart(hMIDIStream);
pMIDISong->MusicPaused = false;
}
}
}
/// @brief Returns true if MIDI playback is paused
/// @return QB_TRUE if paused, QB_FALSE otherwise
inline qb_bool MIDI_IsPaused()
{
return pMIDISong && pMIDISong->MusicPlaying && !pMIDISong->MusicPaused ? QB_FALSE : QB_TRUE;
}
/// @brief Set the MIDI playback volume
/// @param volume A floating point value (0.0 to 1.0)
void MIDI_SetVolume(float volume)
{
if (hMIDIStream)
{
volume = std::clamp(volume, 0.0f, 1.0f);
auto calcVolume = int(65535.0f * volume);
midiOutSetVolume((HMIDIOUT)hMIDIStream, MAKELONG(calcVolume, calcVolume));
}
}
/// @brief Returns the current MIDI volume
/// @return A floating point value (0.0 to 1.0)
inline float MIDI_GetVolume()
{
DWORD dwVolume = 0xFFFF;
if (hMIDIStream)
{
if (midiOutGetVolume((HMIDIOUT)hMIDIStream, &dwVolume) == MMSYSERR_NOERROR)
dwVolume = LOWORD(dwVolume);
else
dwVolume = 0xFFFF;
}
return (float)dwVolume / 65535.0f;
}
/// @brief This is a quick and dirty function to play simple single sounds asynchronously and can be great for playing looping music.
/// This can playback WAV files that use compressed audio using Windows ACM codecs!
/// @param buffer A pointer to .WAV file in memory
/// @param loop If this is true the sound loops forever until it is stopped
/// @return QB_TRUE if the call succeeds. QB_FALSE otherwise
inline qb_bool Sound_PlayFromMemory(const char *buffer, int8_t loop)
{
return (IS_STRING_EMPTY(buffer) ? PlaySoundA(NULL, NULL, 0) : PlaySoundA(buffer, NULL, SND_ASYNC | SND_MEMORY | (loop ? SND_LOOP : 0) | SND_NODEFAULT)) ? QB_TRUE : QB_FALSE;
}
/// @brief Stops any playing sound
inline void Sound_Stop()
{
PlaySoundA(NULL, NULL, 0);
}