-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmidi.c
More file actions
588 lines (491 loc) · 16.3 KB
/
Copy pathmidi.c
File metadata and controls
588 lines (491 loc) · 16.3 KB
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
#include "quakedef.h"
#include "winquake.h"
#include "midstuff.h"
#include "midi.h"
BOOL bMidiInited,bFileOpen, bPlaying, bBuffersPrepared;
BOOL bPaused, bLooped;
UINT uMIDIDeviceID = MIDI_MAPPER, uCallbackStatus;
int nCurrentBuffer, nEmptyBuffers;
DWORD dwBufferTickLength, dwTempoMultiplier, dwCurrentTempo, dwProgressBytes;
DWORD dwVolumePercent, dwVolCache[NUM_CHANNELS];
HMIDISTRM hStream;
CONVERTINFO ciStreamBuffers[NUM_STREAM_BUFFERS];
// From mstrconv.c
extern INFILESTATE ifs;
// Private to this module...
static HANDLE hBufferReturnEvent;
static void FreeBuffers(void);
void MidiErrorMessageBox(MMRESULT mmr)
{
char temp[1024];
midiOutGetErrorText(mmr,temp,sizeof(temp));
Con_Printf("%s\n",temp);
}
void MIDI_Play_f (void)
{
if (Cmd_Argc () == 2)
{
MIDI_Play(Cmd_Argv(1));
}
}
void MIDI_Stop_f (void)
{
MIDI_Stop();
}
void MIDI_Pause_f (void)
{
MIDI_Pause();
}
void MIDI_Loop_f (void)
{
if (Cmd_Argc () == 2)
{
if (strcmpi(Cmd_Argv(1),"on") == 0 || strcmpi(Cmd_Argv(1),"1") == 0)
MIDI_Loop(1);
else if (strcmpi(Cmd_Argv(1),"off") == 0 || strcmpi(Cmd_Argv(1),"0") == 0)
MIDI_Loop(0);
else if (strcmpi(Cmd_Argv(1),"toggle") == 0)
MIDI_Loop(2);
}
if (bLooped) Con_Printf("MIDI music will be looped\n");
else Con_Printf("MIDI music will not be looped\n");
}
void MIDI_Volume_f (void)
{
if (Cmd_Argc () == 2)
{
dwVolumePercent = atol(Cmd_Argv(1))*65535/100;
midiOutSetVolume((HMIDIOUT)hStream,(dwVolumePercent<<16)+dwVolumePercent);
/* dwVolumePercent = atol(Cmd_Argv(1))*10;
SetAllChannelVolumes(dwVolumePercent);*/
}
else
{
Con_Printf("MIDI volume is %d\n", dwVolumePercent/(65535/100));
}
}
BOOL MIDI_Init(void)
{
MMRESULT mmrRetVal;
hBufferReturnEvent = CreateEvent(NULL,FALSE,FALSE,"Wait For Buffer Return");
if(COM_CheckParm("-nomidi"))
{
bMidiInited = 0;
return FALSE;
}
mmrRetVal = midiStreamOpen(&hStream,&uMIDIDeviceID,(DWORD)1,(DWORD)MidiProc,(DWORD)0,CALLBACK_FUNCTION);
if(mmrRetVal != MMSYSERR_NOERROR )
{
bMidiInited = 0;
MidiErrorMessageBox( mmrRetVal );
return FALSE;
}
Cmd_AddCommand ("midi_play", MIDI_Play_f);
Cmd_AddCommand ("midi_stop", MIDI_Stop_f);
Cmd_AddCommand ("midi_pause", MIDI_Pause_f);
Cmd_AddCommand ("midi_loop", MIDI_Loop_f);
Cmd_AddCommand ("midi_volume", MIDI_Volume_f);
dwTempoMultiplier = 100;
dwVolumePercent = 0xffff;
bFileOpen = FALSE;
bPlaying = FALSE;
bLooped = TRUE;
bPaused = FALSE;
bBuffersPrepared = FALSE;
uCallbackStatus = 0;
bMidiInited = 1;
return TRUE;
}
void MIDI_Play(char *Name)
{
MMRESULT mmrRetVal;
char Temp[100];
if (!bMidiInited) //don't try to play if there is no midi
return;
sprintf(Temp, "midi/%s.mid", Name);
MIDI_Stop();
if (StreamBufferSetup(Temp))
{
Con_Printf("Couldn't load midi file %s\n",Temp);
}
else
{
bFileOpen = TRUE;
Con_Printf("Playing midi file %s\n",Temp);
uCallbackStatus = 0;
mmrRetVal = midiStreamRestart(hStream);
if (mmrRetVal != MMSYSERR_NOERROR)
{
MidiErrorMessageBox(mmrRetVal);
return;
}
midiOutSetVolume((HMIDIOUT)hStream, (dwVolumePercent<<16)+dwVolumePercent);
bPlaying = TRUE;
}
}
void MIDI_Pause(void)
{
if(bPaused)
midiStreamRestart(hStream);
else
midiStreamPause(hStream);
bPaused = !bPaused;
}
void MIDI_Loop(int NewValue)
{
if (NewValue == 2)
bLooped = !bLooped;
else bLooped = NewValue;
}
void MIDI_Stop(void)
{
MMRESULT mmrRetVal;
if(bFileOpen || bPlaying)// || uCallbackStatus != STATUS_CALLBACKDEAD)
{
bPlaying = bPaused = FALSE;
if (uCallbackStatus != STATUS_CALLBACKDEAD && uCallbackStatus != STATUS_WAITINGFOREND)
uCallbackStatus = STATUS_KILLCALLBACK;
mmrRetVal = midiStreamStop(hStream);
if (mmrRetVal != MMSYSERR_NOERROR)
{
MidiErrorMessageBox(mmrRetVal);
return;
}
mmrRetVal = midiOutReset((HMIDIOUT)hStream);
if(mmrRetVal != MMSYSERR_NOERROR)
{
MidiErrorMessageBox(mmrRetVal);
return;
}
if(WaitForSingleObject(hBufferReturnEvent,DEBUG_CALLBACK_TIMEOUT) == WAIT_TIMEOUT)
{
//Con_Printf("Timed out waiting for MIDI callback\n");
uCallbackStatus = STATUS_CALLBACKDEAD;
}
}
if (uCallbackStatus == STATUS_CALLBACKDEAD)
{
uCallbackStatus = 0;
if (bFileOpen)
{
ConverterCleanup();
FreeBuffers();
if (hStream)
{
mmrRetVal = midiStreamClose(hStream);
if (mmrRetVal != MMSYSERR_NOERROR)
{
MidiErrorMessageBox(mmrRetVal);
}
hStream = NULL;
}
bFileOpen = FALSE;
}
}
}
void MIDI_Cleanup(void)
{
MMRESULT mmrRetVal;
MIDI_Stop();
CloseHandle(hBufferReturnEvent);
if(hStream)
{
mmrRetVal = midiStreamClose(hStream);
if(mmrRetVal != MMSYSERR_NOERROR)
{
MidiErrorMessageBox(mmrRetVal);
}
hStream = NULL;
}
}
/*****************************************************************************/
/* FreeBuffers() */
/* */
/* This function unprepares and frees all our buffers -- something we must */
/* do to work around a bug in MMYSYSTEM that prevents a device from playing */
/* back properly unless it is closed and reopened after each stop. */
/*****************************************************************************/
void FreeBuffers(void)
{
DWORD idx;
MMRESULT mmrRetVal;
if(bBuffersPrepared)
{
for(idx=0;idx<NUM_STREAM_BUFFERS;idx++)
{
mmrRetVal = midiOutUnprepareHeader((HMIDIOUT)hStream,&ciStreamBuffers[idx].mhBuffer,sizeof(MIDIHDR));
if(mmrRetVal != MMSYSERR_NOERROR)
{
MidiErrorMessageBox(mmrRetVal);
}
}
bBuffersPrepared = FALSE;
}
// Free our stream buffers...
for(idx=0;idx<NUM_STREAM_BUFFERS;idx++)
if(ciStreamBuffers[idx].mhBuffer.lpData)
{
GlobalFreePtr( ciStreamBuffers[idx].mhBuffer.lpData);
ciStreamBuffers[idx].mhBuffer.lpData = NULL;
}
}
/*****************************************************************************/
/* StreamBufferSetup() */
/* */
/* This function uses the filename stored in the global character array to*/
/* open a MIDI file. Then it goes tabout converting at least the first part of*/
/* that file into a midiStream buffer for playback. */
/*****************************************************************************/
BOOL StreamBufferSetup(char *Name)
{
int nChkErr;
BOOL bFoundEnd = FALSE;
DWORD dwConvertFlag, idx;
MMRESULT mmrRetVal;
MIDIPROPTIMEDIV mptd;
if(!hStream)
{
mmrRetVal = midiStreamOpen(&hStream,&uMIDIDeviceID,(DWORD)1,(DWORD)MidiProc,(DWORD)0,CALLBACK_FUNCTION);
if(mmrRetVal != MMSYSERR_NOERROR)
{
MidiErrorMessageBox(mmrRetVal);
return(TRUE);
}
}
for(idx=0;idx<NUM_STREAM_BUFFERS;idx++)
{
ciStreamBuffers[idx].mhBuffer.dwBufferLength = OUT_BUFFER_SIZE;
ciStreamBuffers[idx].mhBuffer.lpData = GlobalAllocPtr(GHND,OUT_BUFFER_SIZE);
if(ciStreamBuffers[idx].mhBuffer.lpData == NULL)
{
// Buffers we already allocated will be killed by WM_DESTROY
// after we fail on the create by returning with -1
return(-1);
}
}
if(ConverterInit(Name))
return(TRUE);
// Initialize the volume cache array to some pre-defined value
for(idx=0;idx<NUM_CHANNELS;idx++)
dwVolCache[idx] = VOL_CACHE_INIT;
mptd.cbStruct = sizeof(mptd);
mptd.dwTimeDiv = ifs.dwTimeDivision;
mmrRetVal = midiStreamProperty(hStream,(LPBYTE)&mptd,MIDIPROP_SET | MIDIPROP_TIMEDIV);
if(mmrRetVal != MMSYSERR_NOERROR)
{
MidiErrorMessageBox(mmrRetVal);
ConverterCleanup();
return(TRUE);
}
nEmptyBuffers = 0;
dwConvertFlag = CONVERTF_RESET;
for(nCurrentBuffer=0;nCurrentBuffer<NUM_STREAM_BUFFERS;nCurrentBuffer++)
{
// Tell the converter to convert up to one entire buffer's length of output
// data. Also, set a flag so it knows to reset any saved state variables it
// may keep from call to call.
ciStreamBuffers[nCurrentBuffer].dwStartOffset = 0;
ciStreamBuffers[nCurrentBuffer].dwMaxLength = OUT_BUFFER_SIZE;
ciStreamBuffers[nCurrentBuffer].tkStart = 0;
ciStreamBuffers[nCurrentBuffer].bTimesUp = FALSE;
nChkErr = ConvertToBuffer(dwConvertFlag,&ciStreamBuffers[nCurrentBuffer]);
if (nChkErr != CONVERTERR_NOERROR)
{
if (nChkErr == CONVERTERR_DONE)
{
bFoundEnd = TRUE;
}
else
{
DebugPrint("Initial conversion pass failed");
ConverterCleanup();
return(TRUE);
}
}
ciStreamBuffers[nCurrentBuffer].mhBuffer.dwBytesRecorded = ciStreamBuffers[nCurrentBuffer].dwBytesRecorded;
if(!bBuffersPrepared)
{
mmrRetVal = midiOutPrepareHeader((HMIDIOUT)hStream,&ciStreamBuffers[nCurrentBuffer].mhBuffer,sizeof(MIDIHDR));
if(mmrRetVal != MMSYSERR_NOERROR)
{
MidiErrorMessageBox(mmrRetVal);
ConverterCleanup();
return(TRUE);
}
}
mmrRetVal = midiStreamOut(hStream,&ciStreamBuffers[nCurrentBuffer].mhBuffer,sizeof(MIDIHDR));
if(mmrRetVal != MMSYSERR_NOERROR)
{
MidiErrorMessageBox(mmrRetVal);
break;
}
dwConvertFlag = 0;
if(bFoundEnd)
break;
}
bBuffersPrepared = TRUE;
nCurrentBuffer = 0;
return(FALSE);
}
/*****************************************************************************/
/* MidiProc() */
/* */
/* This is the callback handler which continually refills MIDI data buffers*/
/* as they're returned to us from the audio subsystem. */
/*****************************************************************************/
void CALLBACK MidiProc(HMIDIIN hMidi,UINT uMsg,DWORD dwInstance,DWORD dwParam1,DWORD dwParam2)
{
static int nWaitingBuffers = 0;
MIDIEVENT *pme;
MIDIHDR *pmh;
MMRESULT mmrRetVal;
int nChkErr;
switch(uMsg)
{
case MOM_DONE:
if(uCallbackStatus == STATUS_CALLBACKDEAD)
{
return;
}
nEmptyBuffers++;
if(uCallbackStatus == STATUS_WAITINGFOREND)
{
if(nEmptyBuffers < NUM_STREAM_BUFFERS)
{
return;
}
else
{
uCallbackStatus = STATUS_CALLBACKDEAD;
MIDI_Stop();
SetEvent(hBufferReturnEvent);
return;
}
}
// This flag is set whenever the callback is waiting for all buffers to
// come back.
if(uCallbackStatus == STATUS_KILLCALLBACK)
{
// Count NUM_STREAM_BUFFERS-1 being returned for the last time
if(nEmptyBuffers < NUM_STREAM_BUFFERS)
{
return;
}
// Then send a stop message when we get the last buffer back...
else
{
// Change the status to callback dead
uCallbackStatus = STATUS_CALLBACKDEAD;
SetEvent( hBufferReturnEvent );
return;
}
}
dwProgressBytes += ciStreamBuffers[nCurrentBuffer].mhBuffer.dwBytesRecorded;
///////////////////////////////////////////////////////////////////////////////
// Fill an available buffer with audio data again...
if(bPlaying && nEmptyBuffers)
{
ciStreamBuffers[nCurrentBuffer].dwStartOffset = 0;
ciStreamBuffers[nCurrentBuffer].dwMaxLength = OUT_BUFFER_SIZE;
ciStreamBuffers[nCurrentBuffer].tkStart = 0;
ciStreamBuffers[nCurrentBuffer].dwBytesRecorded = 0;
ciStreamBuffers[nCurrentBuffer].bTimesUp = FALSE;
nChkErr = ConvertToBuffer(0,&ciStreamBuffers[nCurrentBuffer]);
if(nChkErr != CONVERTERR_NOERROR)
{
if(nChkErr == CONVERTERR_DONE)
{
// Don't include this one in the count
nWaitingBuffers = NUM_STREAM_BUFFERS - 1;
uCallbackStatus = STATUS_WAITINGFOREND;
return;
}
else
{
Con_Printf( "MidiProc() conversion pass failed!" );
ConverterCleanup();
return;
}
}
ciStreamBuffers[nCurrentBuffer].mhBuffer.dwBytesRecorded = ciStreamBuffers[nCurrentBuffer].dwBytesRecorded;
mmrRetVal = midiStreamOut(hStream,&ciStreamBuffers[nCurrentBuffer].mhBuffer,sizeof(MIDIHDR));
if(mmrRetVal != MMSYSERR_NOERROR )
{
MidiErrorMessageBox( mmrRetVal );
ConverterCleanup();
return;
}
nCurrentBuffer = ( nCurrentBuffer + 1 ) % NUM_STREAM_BUFFERS;
nEmptyBuffers--;
}
break;
case MOM_POSITIONCB:
pmh = (MIDIHDR *)dwParam1;
pme = (MIDIEVENT *)(pmh->lpData + pmh->dwOffset);
if (MIDIEVENT_TYPE( pme->dwEvent ) == MIDI_CTRLCHANGE)
{
if (MIDIEVENT_DATA1( pme->dwEvent ) == MIDICTRL_VOLUME_LSB)
{
DebugPrint( "Got an LSB volume event" );
break;
}
if (MIDIEVENT_DATA1( pme->dwEvent ) != MIDICTRL_VOLUME)
break;
// Mask off the channel number and cache the volume data byte
dwVolCache[ MIDIEVENT_CHANNEL( pme->dwEvent )] = MIDIEVENT_VOLUME( pme->dwEvent );
// Post a message so that the main program knows to counteract
// the effects of the volume event in the stream with its own
// generated event which reflects the proper trackbar position.
/* PostMessage( hWndMain, WM_MSTREAM_UPDATEVOLUME,MIDIEVENT_CHANNEL( pme->dwEvent ), 0L );*/
}
break;
default:
break;
}
return;
}
/****************************************************************************/
/* SetAllChannelVolumes() */
/* */
/* Given a percent in tenths of a percent, sets volume on all channels to */
/* reflect the new value. */
/****************************************************************************/
void SetAllChannelVolumes(DWORD dwVolumePercent)
{
DWORD dwEvent, dwStatus, dwVol, idx;
MMRESULT mmrRetVal;
/* if(!bPlaying)
return;*/
for(idx=0,dwStatus=MIDI_CTRLCHANGE;idx<NUM_CHANNELS;idx++,dwStatus++)
{
dwVol = ( dwVolCache[idx] * dwVolumePercent ) / 1000;
dwEvent = dwStatus | ((DWORD)MIDICTRL_VOLUME << 8) | ((DWORD)dwVol << 16);
mmrRetVal = midiOutShortMsg((HMIDIOUT)hStream, dwEvent);
if(mmrRetVal != MMSYSERR_NOERROR )
{
MidiErrorMessageBox(mmrRetVal);
return;
}
}
}
/****************************************************************************/
/* SetChannelVolume() */
/* */
/* Given a percent in tenths of a percent, sets volume on a specified */
/* channel to reflect the new value. */
/****************************************************************************/
void SetChannelVolume(DWORD dwChannel, DWORD dwVolumePercent)
{
DWORD dwEvent, dwVol;
MMRESULT mmrRetVal;
if(!bPlaying)
return;
dwVol = (dwVolCache[dwChannel] * dwVolumePercent) / 1000;
dwEvent = MIDI_CTRLCHANGE | dwChannel | ((DWORD)MIDICTRL_VOLUME << 8) | ((DWORD)dwVol << 16);
mmrRetVal = midiOutShortMsg((HMIDIOUT)hStream, dwEvent);
if(mmrRetVal != MMSYSERR_NOERROR )
{
MidiErrorMessageBox( mmrRetVal );
return;
}
}