/
StreamingAudio.cpp
836 lines (677 loc) · 20.6 KB
/
StreamingAudio.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
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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
/************************************************************************************//**
* @file StreamingAudio.cpp
* @brief Streaming audio encapsulation class
*
* This file contains the class implementation of audio streaming services,
* based on the SoundServices functionality.
* @author Ralph Deane
*//*
* Copyright (c) 2001 Ralph Deane; All Rights Reserved
****************************************************************************************/
// As always, we include the One True Header File
#include "RabidFramework.h"
// ************************ BEGIN IMPLEMENTATION ***************************
/* ------------------------------------------------------------------------------------ */
// StreamingAudio
//
// Default constructor, takes a DirectSound pointer
/* ------------------------------------------------------------------------------------ */
StreamingAudio::StreamingAudio(LPDIRECTSOUND lpDS)
{
m_nDataPosition = 0; // Start of WAVE data in file
m_nCurrentPosition = 0; // Current read position
m_nDataSize = 0; // Wave data size, in bytes
m_fActive = false; // Playback active flag
m_pStream = NULL; // Sound buffer
m_nTimerID = 0; // Pumping timer ID
m_hWaveFile = NULL; // Handle to WAVE file
m_pWaveFormat = NULL; // Wave file format
m_pDS = lpDS; // Save DSound pointer
m_fEOF = false; // Not at end of file
m_bLoops = false; // Assume no looping
Mpeg3 = NULL;
Ogg = NULL;
mp3 = false;
ogg = false;
return;
}
/* ------------------------------------------------------------------------------------ */
// ~StreamingAudio
//
// Default destructor
/* ------------------------------------------------------------------------------------ */
StreamingAudio::~StreamingAudio()
{
if(ogg)
{
if(Ogg)
{
Ogg->Stop();
delete Ogg;
Ogg = NULL;
}
return;
}
timeKillEvent(m_nTimerID); // Stop the timer
if(mp3)
{
if(Mpeg3)
{
Mpeg3->StopMp3();
delete Mpeg3;
Mpeg3 = NULL;
}
return;
}
m_fActive = false; // Inactive now
if(m_hWaveFile != NULL)
{
mmioClose(m_hWaveFile, 0);
m_hWaveFile = NULL;
delete m_pWaveFormat;
m_pWaveFormat = NULL;
}
if(m_pStream != NULL)
{
m_pStream->Release(); // Drop the DSound buffer
m_pStream = NULL;
}
return;
}
/* ------------------------------------------------------------------------------------ */
// Create
//
// Creates a new audio stream and sets up a timer for it.
/* ------------------------------------------------------------------------------------ */
int StreamingAudio::Create(char *szFileName)
{
int nError = 0;
if(CCD->GetLogging())
{
char szDebug[512];
sprintf(szDebug, "[INFO] Loaded %s", szFileName);
CCD->ReportError(szDebug, false);
}
// Sanity check parameters
if(szFileName == NULL)
return RGF_FAILURE; // Wrong.
// changed RF064
int len = strlen(szFileName)-4;
if(stricmp((szFileName+len),".mp3") == 0)
{
if(Mpeg3 != NULL)
return RGF_FAILURE;
Mpeg3 = new CMp3Manager;
Mpeg3->OpenMediaFile(szFileName);
mp3 = true;
m_fActive = true;
// start a timer for this stream.
MMRESULT nTimer = timeSetEvent(125, 5, &TimerFunction, (DWORD)this,
TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
if(nTimer == NULL)
{
CCD->ReportError("[WARNING] StreamingAudio: Timer callback set-up failed", false);
return RGF_FAILURE; // Timer startup failed.
}
m_nTimerID = nTimer; // Save timer ID
return RGF_SUCCESS;
}
mp3 = false;
if(stricmp((szFileName+len), ".ogg") == 0)
{
if(Ogg != NULL)
return RGF_FAILURE;
Ogg = new OggAudio(m_pDS);
Ogg->Load(szFileName);
ogg = true;
m_fActive = true;
return RGF_SUCCESS;
}
ogg = false;
// start a timer for this stream.
MMRESULT nTimer = timeSetEvent(125, 5, &TimerFunction, (DWORD)this,
TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
if(nTimer == NULL)
{
CCD->ReportError("[WARNING] StreamingAudio: Timer callback set-up failed", false);
return RGF_FAILURE; // Timer startup failed.
}
m_nTimerID = nTimer; // Save timer ID
// end change RF064
// Check for stream availability
if(m_pStream != NULL)
return RGF_FAILURE; // Already loaded!
// Open up the WAVE file.
nError = WaveOpen(szFileName, &m_hWaveFile, &m_pWaveFormat, &m_rRiffData);
if(nError != RGF_SUCCESS)
{
char szBug[128];
sprintf(szBug, "[WARNING] File %s - Line %d: Failed to open wave file '%s'\n(Note: streamig audio can't be placed in VFS)",
__FILE__, __LINE__, szFileName);
CCD->ReportError(szBug, false);
return RGF_FAILURE; // Problem here, too!
}
// Get current position in file, which will be the start of the
// ..WAVE data.
m_nDataPosition = mmioSeek(m_hWaveFile, 0, SEEK_CUR);
m_nDataSize = m_rRiffData.cksize; // Save data size
// Fetch DirectSound interface we want
LPDIRECTSOUND pDSIF;
nError = m_pDS->QueryInterface(IID_IDirectSound, (LPVOID *)&pDSIF);
// Create a DSound buffer to stream into
DSBUFFERDESC theDesc;
memset(&theDesc, 0, sizeof(DSBUFFERDESC));
theDesc.dwSize = sizeof(DSBUFFERDESC);
theDesc.dwBufferBytes = kBufferSize;
theDesc.lpwfxFormat = m_pWaveFormat;
theDesc.dwFlags = DSBCAPS_CTRLVOLUME;
nError = pDSIF->CreateSoundBuffer(&theDesc, &m_pStream, NULL);
pDSIF->Release(); // Done w/ this.
if(nError != 0) // Error! Sick out.
{
char szBug[128];
sprintf(szBug, "StreamingAudio: Failed to create buffer for '%s'\n", szFileName);
OutputDebugString(szBug);
mmioClose(m_hWaveFile, 0);
m_hWaveFile = NULL;
delete m_pWaveFormat;
m_pWaveFormat = NULL;
return RGF_FAILURE;
}
m_nOffset = 0; // Start at top of buffer
// changed RF064
//m_fActive = true; // Set as active
// end change RF064
PumpWave(kBufferSize); // Initial buffer load
return RGF_SUCCESS;
}
/* ------------------------------------------------------------------------------------ */
// Play
//
// Start this stream buffer playing, with or without looping.
/* ------------------------------------------------------------------------------------ */
int StreamingAudio::Play(bool bLooping)
{
if(mp3)
{
if(Mpeg3 == NULL)
return RGF_FAILURE;
m_fActive = true;
m_bLoops = bLooping;
Mpeg3->PlayMp3((LONG)(CCD->MenuManager()->GetmVolLevel()*1000.0f), bLooping);
return RGF_SUCCESS;
}
LONG ScaledVolume;
LONG nVolume = (LONG)(CCD->MenuManager()->GetmVolLevel()*10000.0f);
if(nVolume <= 1)
ScaledVolume = -10000;
else if(nVolume > 10000)
ScaledVolume = 0;
else
//ScaledVolume = (LONG)(log10(nVolume)/4*10000 - 10000);
ScaledVolume = (LONG)(log10((double)nVolume)*2500 - 10000);
nVolume = ScaledVolume;
if(ogg)
{
if(Ogg == NULL)
return RGF_FAILURE;
m_fActive = true;
m_bLoops = bLooping;
Ogg->Play(bLooping);
SetVolume(nVolume);
return RGF_SUCCESS;
}
// Check for stream availability
if(m_pStream == NULL)
return RGF_FAILURE; // No stream
// changed RF064
if(m_fActive == true)
return RGF_SUCCESS;
// end change RF064
m_fActive = true;
m_bLoops = bLooping;
m_pStream->Play(0, 0, DSBPLAY_LOOPING); // Start playback
SetVolume(nVolume);
return RGF_SUCCESS;
}
/* ------------------------------------------------------------------------------------ */
// Stop
//
// Stop this stream from playing back.
/* ------------------------------------------------------------------------------------ */
int StreamingAudio::Stop()
{
if(mp3)
{
// changed RF064
m_fActive = false;
if(Mpeg3 == NULL)
return RGF_FAILURE;
Mpeg3->StopMp3();
return RGF_SUCCESS;
// end change RF064
}
if(ogg)
{
m_fActive = false;
if(Ogg == NULL)
return RGF_FAILURE;
Ogg->Stop();
return RGF_SUCCESS;
}
//Check for stream availability
if(m_pStream == NULL)
return RGF_FAILURE; // No stream
m_fActive = false;
m_pStream->Stop(); // Stop playback
return RGF_SUCCESS;
}
/* ------------------------------------------------------------------------------------ */
// Pause
//
// Pause/unpause this stream's playback.
/* ------------------------------------------------------------------------------------ */
int StreamingAudio::Pause()
{
if(mp3)
{
if(Mpeg3 == NULL)
return RGF_FAILURE;
if(m_fActive == false)
{
m_fActive = true;
Mpeg3->PlayMp3((LONG)(CCD->MenuManager()->GetmVolLevel()*1000.0f), m_bLoops);
}
else
{
m_fActive = false;
Mpeg3->StopMp3();
}
return RGF_SUCCESS;
}
if(ogg)
{
if(Ogg == NULL)
return RGF_FAILURE;
Ogg->Pause();
return RGF_SUCCESS;
}
// Check for stream availability
if(m_pStream == NULL)
return RGF_FAILURE; // No stream
if(m_fActive == false)
m_fActive = true;
else
m_fActive = false;
return RGF_SUCCESS;
}
/* ------------------------------------------------------------------------------------ */
// Delete
//
// Delete this stream - stop it and kill the timer.
/* ------------------------------------------------------------------------------------ */
int StreamingAudio::Delete()
{
if(ogg)
{
if(Ogg == NULL)
return RGF_FAILURE;
Ogg->Stop();
delete Ogg;
Ogg = NULL;
return RGF_SUCCESS;
}
timeKillEvent(m_nTimerID); // Kill timer
if(mp3)
{
if(Mpeg3 == NULL)
return RGF_FAILURE;
Mpeg3->StopMp3();
delete Mpeg3;
Mpeg3 = NULL;
return RGF_SUCCESS;
}
// Check for stream availability
if(m_pStream == NULL)
return RGF_FAILURE; // No stream
Stop(); // Stop playback
return RGF_SUCCESS;
}
/* ------------------------------------------------------------------------------------ */
// Rewind
//
// Reposition stream playback to the start of the wave data
/* ------------------------------------------------------------------------------------ */
int StreamingAudio::Rewind()
{
if(mp3)
{
if(Mpeg3 == NULL)
return RGF_FAILURE;
return RGF_SUCCESS;
}
if(ogg)
{
if(Ogg == NULL)
return RGF_FAILURE;
Ogg->Rewind();
return RGF_SUCCESS;
}
// Check for stream availability
if(m_pStream == NULL)
return RGF_FAILURE; // No stream
// Check to be sure it's active
if(m_fActive == false)
return RGF_FAILURE; // Not an active stream
// Ok, position to the start of the WAVE DATA
mmioSeek(m_hWaveFile, m_nDataPosition, SEEK_SET);
m_rRiffData.cksize = m_nDataSize; // Restore data size
m_fEOF = false; // Not EOF any longer
return RGF_SUCCESS;
}
/* ------------------------------------------------------------------------------------ */
// IsPlaying
//
// Returns TRUE if the stream is actively playing, FALSE otherwise
// ..If the stream is at EOF that qualifies as "not playing".
/* ------------------------------------------------------------------------------------ */
bool StreamingAudio::IsPlaying()
{
if(mp3)
{
return false;
}
if(ogg)
{
if(!Ogg)
return false;
return Ogg->IsPlaying();
}
// Check for stream availability
if(m_pStream == NULL)
return false; // No stream
if((m_fEOF == true) || (m_fActive == false))
return false; // Not playing
return true; // Is playing
}
/* ------------------------------------------------------------------------------------ */
// SetVolume
//
// Set the playback volume of the audio stream.
/* ------------------------------------------------------------------------------------ */
int StreamingAudio::SetVolume(LONG nVolume)
{
if(mp3)
{
if(Mpeg3 == NULL)
return RGF_FAILURE;
Mpeg3->SetVolume((nVolume+10000)/10);
return RGF_SUCCESS;
}
LONG ScaledVolume;
nVolume += 10000;;
if(nVolume <= 1)
ScaledVolume = -10000;
else if(nVolume > 10000)
ScaledVolume = 0;
else
//ScaledVolume = (LONG)(log10(nVolume)/4*10000 - 10000);
ScaledVolume = (LONG)(log10((double)nVolume)*2500 - 10000);
nVolume = ScaledVolume;
if(ogg)
{
if(Ogg == NULL)
return RGF_FAILURE;
Ogg->SetVolume(nVolume);
return RGF_SUCCESS;
}
// Sanity check parameter
if((m_pStream == NULL) || (nVolume > 0))
return RGF_FAILURE; // Bad parameter
m_pStream->SetVolume(nVolume); // Set it!
return RGF_SUCCESS;
}
// ******************* PRIVATE MEMBER FUNCTIONS **********************
/* ------------------------------------------------------------------------------------ */
// PumpWave
//
// Fill the streaming audio buffer from the wave file, making sure that
// ..there's always something (up to and including silence) in the
// ..buffer for playback.
/* ------------------------------------------------------------------------------------ */
int StreamingAudio::PumpWave(int nSize)
{
HRESULT hr;
void *lpbuf1 = NULL, *lpbuf2 = NULL;
DWORD dwsize1 = 0, dwsize2 = 0;
UINT nBytesRead = 0;
// Ok, we need to set up our "silence" value and adjust it for
// ..8bit samples if needed.
int nSilence = 0x0;
if(m_pWaveFormat->wBitsPerSample == 8)
nSilence = 0x80; // In case wave is 8-bit
// Ok, try to lock <n>K of the buffer. If it fails, just bail this
// ..function.
hr = m_pStream->Lock(m_nOffset, nSize, &lpbuf1, &dwsize1, &lpbuf2, &dwsize2, 0);
if(hr != DS_OK)
{
CCD->ReportError("[WARNING] StreamingAudio::PumpWave: Failed to lock", false);
return RGF_SUCCESS; // Fake it, bail out
}
if(lpbuf1 != NULL)
memset(lpbuf1, nSilence, dwsize1); // Clear to silence
if(lpbuf2 != NULL)
memset(lpbuf2, nSilence, dwsize2); // Here, also
m_nOffset = (m_nOffset + dwsize1 + dwsize2) % kBufferSize;
if(m_fEOF == true)
{
m_pStream->Unlock(lpbuf1, dwsize1, lpbuf2, dwsize2);
return RGF_SUCCESS; // End of file, pump silence
}
if(m_fActive == false)
{
m_pStream->Unlock(lpbuf1, dwsize1, lpbuf2, dwsize2);
return RGF_SUCCESS; // Inactive, pump silence
}
// Fine, read data into the circular buffer directly from the
// ..wave file if there's anything there.
// Fill block #1
hr = WaveRead(m_hWaveFile, dwsize1, (BYTE*)lpbuf1, &m_rRiffData, &nBytesRead);
if(nBytesRead != dwsize1) // End of wave file
{
m_fEOF = true;
// m_pStream->Unlock(lpbuf1, dwsize1, lpbuf2, dwsize2);
// return RGF_SUCCESS;
}
if((lpbuf2 != NULL) && (!m_fEOF))
{
// Fill block #2
hr = WaveRead(m_hWaveFile, dwsize2, (BYTE*)lpbuf2, &m_rRiffData, &nBytesRead);
if(nBytesRead != dwsize2) // End of wave file
m_fEOF = true;
}
// Unlock buffer, we're done with it for now.
m_pStream->Unlock(lpbuf1, dwsize1, lpbuf2, dwsize2);
// Ok, if we're at the end of file AND we're flagged to loop, rewind
// ..to the beginning of the buffer so we start from the top next
// ..time through.
if(m_fEOF && m_bLoops)
Rewind(); // Hope the sound designer looped the WAVE right!
return RGF_SUCCESS;
}
/* ------------------------------------------------------------------------------------ */
// GetMaxWriteSize
//
// Get the maximum number of bytes we can write into the current buffer.
/* ------------------------------------------------------------------------------------ */
DWORD StreamingAudio::GetMaxWriteSize()
{
DWORD dwWriteCursor, dwPlayCursor, dwMaxSize;
// Get current play position
if(m_pStream->GetCurrentPosition(&dwPlayCursor, &dwWriteCursor) == DS_OK)
{
if((UINT)m_nOffset <= dwPlayCursor)
{
// Our write position trails play cursor
dwMaxSize = dwPlayCursor - m_nOffset;
}
else // (m_cbBufOffset > dwPlayCursor)
{
// Play cursor has wrapped
dwMaxSize = kBufferSize - m_nOffset + dwPlayCursor;
}
}
else
dwMaxSize = 0;
return (dwMaxSize & 0xfffffffe);
}
/* ------------------------------------------------------------------------------------ */
// TimerFunction
//
// This function takes care of periodically pumping audio into
// ..the playback buffer for streaming waves. Based on the
// ..user information passed in (set up when the wave file was
// ..started streaming) it calls into the instance that is needed.
// ..Yeah, this is a little wacky, but it let's me stream the wave
// ..out without having to continually poll and stuff buffers.
/* ------------------------------------------------------------------------------------ */
void CALLBACK StreamingAudio::TimerFunction(UINT uID, UINT uMsg,
DWORD dwUser, DWORD dw1, DWORD dw2)
{
StreamingAudio *thePointer = (StreamingAudio*)dwUser;
if(thePointer->mp3)
{
if(thePointer->Mpeg3 == NULL)
return;
if(thePointer->m_fActive && thePointer->m_bLoops)
thePointer->Mpeg3->Refresh();
return;
}
DWORD nSize = thePointer->GetMaxWriteSize();
if(nSize != 0)
thePointer->PumpWave(nSize); // Pump stream
return;
}
/* ------------------------------------------------------------------------------------ */
// WaveOpen
//
// Open up a wave file, validate it, and return a handle to the open
// ..file and a WAVEFORMATEX structure containing the wave info.
/* ------------------------------------------------------------------------------------ */
int StreamingAudio::WaveOpen(char *szFileName, HMMIO *pFileID,
WAVEFORMATEX **ppwfxInfo, MMCKINFO *pckInRIFF)
{
HMMIO hmmioIn = NULL;
MMCKINFO ckIn; // chunk info. for general use.
PCMWAVEFORMAT pcmWaveFormat; // Temp PCM structure to load in.
WORD cbExtraAlloc = 0; // Extra bytes for waveformatex
int nError = 0; // Return value.
// Set up for the job
*ppwfxInfo = NULL;
if((hmmioIn = mmioOpen(szFileName, NULL, MMIO_ALLOCBUF | MMIO_READ)) == NULL)
return -1;
if(mmioDescend(hmmioIn, pckInRIFF, NULL, 0) != 0)
{
mmioClose(hmmioIn, 0); // Clean up
return -1;
}
// Check to make sure we're really opening a WAVE file
if((pckInRIFF->ckid != FOURCC_RIFF) || (pckInRIFF->fccType != mmioFOURCC('W', 'A', 'V', 'E')))
{
mmioClose(hmmioIn, 0); // Clean up
return -2;
}
// Search the input file for for the 'fmt ' chunk.
ckIn.ckid = mmioFOURCC('f', 'm', 't', ' ');
if(mmioDescend(hmmioIn, &ckIn, pckInRIFF, MMIO_FINDCHUNK) != 0)
{
mmioClose(hmmioIn, 0); // Clean up
return -3;
}
// Expect the 'fmt' chunk to be at least as large as <PCMWAVEFORMAT>;
// if there are extra parameters at the end, we'll ignore them.
if(ckIn.cksize < (long) sizeof(PCMWAVEFORMAT))
{
mmioClose(hmmioIn, 0); // Clean up
return -4;
}
// Read the 'fmt ' chunk into <pcmWaveFormat>
if(mmioRead(hmmioIn, (HPSTR) &pcmWaveFormat, (long) sizeof(pcmWaveFormat)) != (long) sizeof(pcmWaveFormat))
{
mmioClose(hmmioIn, 0); // Clean up
return -5;
}
// Ok, allocate the waveformatex, but if its not pcm
// format, read the next word, and thats how many extra
// bytes to allocate.
if(pcmWaveFormat.wf.wFormatTag == WAVE_FORMAT_PCM)
cbExtraAlloc = 0;
else
{
// Read in length of extra bytes.
if (mmioRead(hmmioIn, (LPSTR) &cbExtraAlloc, (long) sizeof(cbExtraAlloc)) != (long) sizeof(cbExtraAlloc))
{
mmioClose(hmmioIn, 0); // Clean up
return -6;
}
}
// Ok, now allocate that waveformatex structure.
if((*ppwfxInfo = (WAVEFORMATEX*)malloc(sizeof(WAVEFORMATEX)+cbExtraAlloc)) == NULL)
{
mmioClose(hmmioIn, 0); // Clean up
return -7;
}
// Copy the bytes from the pcm structure to the waveformatex structure
memcpy(*ppwfxInfo, &pcmWaveFormat, sizeof(pcmWaveFormat));
(*ppwfxInfo)->cbSize = cbExtraAlloc;
// Now, read those extra bytes into the structure, if cbExtraAlloc != 0.
if(cbExtraAlloc != 0)
{
if(mmioRead(hmmioIn, (LPSTR) (((BYTE*)&((*ppwfxInfo)->cbSize))+sizeof(cbExtraAlloc)),
(long) (cbExtraAlloc)) != (long) (cbExtraAlloc))
{
mmioClose(hmmioIn, 0); // Clean up
return -8;
}
}
// Ascend the input file out of the 'fmt ' chunk
if(mmioAscend(hmmioIn, &ckIn, 0) != 0)
{
mmioClose(hmmioIn, 0); // Clean up
return -9;
}
// Search the input file for for the 'data' chunk.
ckIn.ckid = mmioFOURCC('d', 'a', 't', 'a');
if(mmioDescend(hmmioIn, &ckIn, pckInRIFF, MMIO_FINDCHUNK) != 0)
{
mmioClose(hmmioIn, 0); // Clean up
return -10;
}
memcpy(pckInRIFF, &ckIn, sizeof(MMCKINFO)); // Set up for later use.
// Ok, return the handle to the file for later use...
*pFileID = hmmioIn;
return RGF_SUCCESS; // Ok, it worked!
}
/* ------------------------------------------------------------------------------------ */
// This will read wave data from the wave file. Makre sure we're descended into
// the data chunk, else this will fail bigtime!
// hmmioIn - Handle to mmio.
// cbRead - # of bytes to read.
// pbDest - Destination buffer to put bytes.
// cbActualRead- # of bytes actually read.
/* ------------------------------------------------------------------------------------ */
int StreamingAudio::WaveRead(HMMIO hmmioIn, int nSizeToRead, BYTE *pDestination,
MMCKINFO *pckIn, UINT *nBytesRead)
{
if(pckIn->cksize <= 0)
return -1; // No data bytes left!
if((UINT)nSizeToRead > pckIn->cksize)
nSizeToRead = pckIn->cksize; // Only read what's left
int nRead = mmioRead(hmmioIn, (char *)pDestination, (long)nSizeToRead);
pckIn->cksize -= nRead; // Decrement count
*nBytesRead = nRead; // Save byte count.
if(nRead != nSizeToRead)
return -1; // End of file
return 0; // Worked fine.
}
/* ----------------------------------- END OF FILE ------------------------------------ */