-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAudioStreamer.cpp
More file actions
795 lines (690 loc) · 24.4 KB
/
Copy pathAudioStreamer.cpp
File metadata and controls
795 lines (690 loc) · 24.4 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
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
/**
* Sound3D - An open source 3D Audio Library
*
* Copyright (c) 2013 Jorma Rebane
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "AudioStreamer.h"
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <Windows.h> // LoadLibrary, FreeLibrary
#include <stdio.h> // fopen
#include <stdlib.h> // printf
#include <sys/types.h> // off_t
#ifdef _DEBUG
#define indebug(x) x
#else
#define indebug(x) // do nothing in release
#endif
// declare the SHARED segment
#pragma comment(linker, "/section:SHARED,RWS")
// declare placement new and matching delete
static inline void* operator new(size_t sz, void* dst) { return dst; }
static inline void operator delete(void* mem, void* dst) { ; }
namespace S3D
{
//// Low Latency File IO straight to Windows API, with no beating around the bush
// opens a file with read-only rights
inline void* file_open_ro(const char* filename)
{
void* fh = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (fh == INVALID_HANDLE_VALUE) return NULL;
return fh;
}
// close the file
inline int file_close(void* handle)
{
CloseHandle(handle);
return 0;
}
// reads bytes from an opened file
inline int file_read(void* handle, void* dst, size_t size)
{
DWORD bytesRead;
ReadFile(handle, dst, size, &bytesRead, NULL);
return (int)bytesRead;
}
// seeks the file pointer
inline off_t file_seek(void* handle, off_t offset, int whence)
{
return SetFilePointer(handle, offset, NULL, whence);
}
// tells the current file position
inline off_t file_tell(void* handle)
{
return SetFilePointer(handle, 0, NULL, FILE_CURRENT);
}
////
// Getting Audio file format impl.
////////
#pragma region GetAudioFileFormat
enum AudioFileFormat { INVALID, WAV, MP3, OGG, };
static int GetExtension(const char* file) // returns the extension in the string as an integer
{
const char* ext = strrchr(file, '.');
if (!ext || ext == file)
return 0; // no extension || start of filename was '.'
return *(int*)ext; // force the extension bytes into an int
}
static AudioFileFormat GetAudioFileFormatByExtension(const char* file) // pretty cheap actually - we just check the file extension!
{
int ext = GetExtension(file);
if (!ext) return AudioFileFormat::INVALID; // no extension or no file found.
switch (ext) // interpret the extension as an int
{ // abuse little endian byte order:
case 'vaw.': return AudioFileFormat::WAV; // WAV file
case '3pm.': return AudioFileFormat::MP3; // MP3 file
case 'ggo.': return AudioFileFormat::OGG; // Ogg Vorbis file
default:
indebug(printf("Unsupported audio file extension: %s\n", file));
indebug(printf("Supported formats: wav mp3 ogg\n"));
return AudioFileFormat::INVALID; // error
}
}
static bool checkMP3Tag(void* buffer)
{
#pragma pack(push)
#pragma pack(1)
struct MP3TAGV2 {
char id3[3];
unsigned char vermajor;
unsigned char verminor;
unsigned char flags;
unsigned int size;
};
#pragma pack(pop)
MP3TAGV2 tag = *(MP3TAGV2*)buffer;
if (tag.id3[0] == 'I' && tag.id3[1] == 'D' && tag.id3[2] == '3')
return true;
return false; // not an mp3 header
}
static AudioFileFormat GetAudioFileFormatByHeader(const char* file) // a bit heavier - we actually check the file header
{
FILE* f = fopen(file, "rb"); // open file 'read-binary'
if (f == NULL)
{
indebug(printf("File not found: \"%s\"\n", file));
return AudioFileFormat::INVALID; // file doesn't exist
}
// MP3 has a header tag, needs 10 bytes
// WAV has a large header with byte fields [file + 0]='RIFF' and [file + 8]='WAVE', needs 12bytes
// OGG has a 32-bit "capture pattern" sync field 'OggS', needs 4 bytes
int buffer[3]; // WAV requires most, so 12 bytes
fread(buffer, sizeof(buffer), 1, f);
fclose(f); f = NULL;
if (buffer[0] == 'FFIR' && buffer[2] == 'EVAW')
return AudioFileFormat::WAV;
else if (buffer[0] == 'SggO')
return AudioFileFormat::OGG;
else if (checkMP3Tag(buffer))
return AudioFileFormat::MP3;
return AudioFileFormat::INVALID;
}
AudioStreamer* CreateAudioStreamer(const char* file)
{
AudioFileFormat fmt = GetAudioFileFormatByExtension(file);
if (!fmt) fmt = GetAudioFileFormatByHeader(file);
if (!fmt) return nullptr; // unsupported format
switch(fmt) {
case AudioFileFormat::WAV: return new WAVStreamer();
case AudioFileFormat::MP3: return new MP3Streamer();
case AudioFileFormat::OGG: return new OGGStreamer();
}
return nullptr; // ok?... unsupported format
}
bool CreateAudioStreamer(AudioStreamer* as, const char* file)
{
if (!as) return false; // oh well...
as->CloseStream(); // just in case...
AudioFileFormat fmt = GetAudioFileFormatByExtension(file);
if (!fmt) fmt = GetAudioFileFormatByHeader(file);
if (!fmt) return false; // unsupported format
switch (fmt) {
case AudioFileFormat::WAV: new (as) WAVStreamer(); break;
case AudioFileFormat::MP3: new (as) MP3Streamer(); break;
case AudioFileFormat::OGG: new (as) OGGStreamer(); break;
default: return false; // unsupported format
}
return true; // everything went ok
}
#pragma endregion
//////
// AudioStreamer and WAVStreamer impl
//
#pragma region WAVStreamer
struct RIFFCHUNK
{
union {
int ID; // chunk ID
char IDStr[4]; // chunk ID as string
};
int Size; // chunk SIZE
};
struct WAVHEADER
{
RIFFCHUNK Header; // Contains the letters "RIFF" && (SizeOfFile - 8) in bytes
union {
int Format; // Contains the letters "WAVE"
char FormatAsStr[4];
};
// The FMT sub-chunk
RIFFCHUNK Subchunk1; // Contains the letters "fmt " && 16 bytes in size
struct {
short AudioFormat; // Should be 1, otherwise this file is compressed!
short NumChannels; // Mono = 1, Stereo = 2
int SampleRate; // 8000, 22050, 44100, etc
int ByteRate; // == SampleRate * NumChannels * BitsPerSample/8
short BlockAlign; // == NumChannels * BitsPerSample/8
short BitsPerSample; // 8 bits == 8, 16 bits == 16, etc.
};
RIFFCHUNK NextChunk1; // Contains the letters "info" or "data"
int someData[4];
RIFFCHUNK NextChunk2; // maybe this one is data?
RIFFCHUNK* getDataChunk()
{
if (NextChunk1.ID == (int)'atad') return &NextChunk1;
if (NextChunk2.ID == (int)'atad') return &NextChunk2;
return nullptr;
}
};
/**
* Creates a new uninitialized AudioStreamer.
* You should call OpenStream(file) to initialize the stream.
*/
AudioStreamer::AudioStreamer()
: FileHandle(0), StreamSize(0), StreamPos(0), SampleRate(0), NumChannels(0), SampleSize(0), SampleBlockSize(0)
{
}
/**
* Creates and Opens a new AudioStreamer.
* @param file Full path to the audiofile to stream
*/
AudioStreamer::AudioStreamer(const char* file)
: FileHandle(0), StreamSize(0), StreamPos(0), SampleRate(0), NumChannels(0), SampleSize(0), SampleBlockSize(0)
{
OpenStream(file);
}
/**
* Destroys the AudioStream and frees all held resources
*/
AudioStreamer::~AudioStreamer()
{
CloseStream();
}
/**
* Opens a new stream for reading.
* @param file Audio file to open
* @return TRUE if stream is successfully opened and initialized. FALSE if the stream open failed or its already open.
*/
bool AudioStreamer::OpenStream(const char* file)
{
if (FileHandle) // dont allow reopen an existing stream
return false;
if (!(FileHandle = (int*)file_open_ro(file))) {
indebug(printf("Failed to open file: \"%s\"\n", file));
return false; // oh well;
}
WAVHEADER wav;
if (file_read(FileHandle, &wav, sizeof(wav)) <= 0) {
indebug(printf("Failed to load WAV header: \"%s\"\n", file));
return false; // invalid file
}
if (wav.Header.ID != (int)'FFIR' || wav.Format != (int)'EVAW') { // != "RIFF" || != "WAVE"
indebug(printf("Invalid WAV file header: %s\n", file));
return false; // invalid wav header
}
RIFFCHUNK* dataChunk = wav.getDataChunk();
if (!dataChunk) {
indebug(printf("Failed to find WAV <data> chunk.\n"));
return false; // invalid WAV file
}
// initialize essential variables
StreamSize = dataChunk->Size;
SampleRate = (unsigned int)wav.SampleRate;
NumChannels = (unsigned char)wav.NumChannels;
SampleSize = (unsigned char)(wav.BitsPerSample >> 3); // BPS/8 => SampleSize
SampleBlockSize = SampleSize * NumChannels; // [LL][RR] (1 to 4 bytes)
return true; // everything went ok
}
/**
* Closes the stream and releases all resources held.
*/
void AudioStreamer::CloseStream()
{
if (FileHandle)
{
file_close(FileHandle);
FileHandle = 0;
StreamSize = 0;
StreamPos = 0;
SampleRate = 0;
NumChannels = 0;
SampleSize = 0;
SampleBlockSize = 0;
}
}
/**
* Reads some Audio data from the underlying stream.
* Audio data is decoded into PCM format, suitable for OpenAL.
* @param dstBuffer Destination buffer that receives the data
* @param dstSize Number of bytes to read. 64KB is good for streaming (gives ~1.5s of playback sound).
* @return Number of bytes read. 0 if stream is uninitialized or end of stream reached.
*/
int AudioStreamer::ReadSome(void* dstBuffer, int dstSize)
{
if (!FileHandle)
return 0; // nothing to do here
int count = StreamSize - StreamPos; // calc available data from stream
if (count == 0) // if stream available bytes 0?
return 0; // EOS reached
if (count > dstSize) // if stream has more data than buffer
count = dstSize; // set bytes to read bigger
count -= count % SampleBlockSize; // make sure count is aligned to blockSize
if (file_read(FileHandle, dstBuffer, count) <= 0) {
StreamPos = StreamSize; // set EOS
return 0; // no bytes read
}
StreamPos += count;
return count;
}
/**
* Seeks to the appropriate byte position in the stream.
* This value is between: [0...StreamSize]
* @param streampos Position in the stream to seek to in BYTES
* @return The actual position where seeked, or 0 if out of bounds (this also means the stream was reset to 0).
*/
unsigned int AudioStreamer::Seek(unsigned int streampos)
{
if (int(streampos) >= StreamSize)
streampos = 0;
streampos -= streampos % SampleBlockSize; // align to PCM blocksize
int actual = streampos + sizeof(WAVHEADER); // skip the WAVHEADER
file_seek(FileHandle, actual, SEEK_SET);
StreamPos = streampos;
return streampos;
}
#pragma endregion
//////
// MP3Streamer impl
//
#pragma region MP3Streamer
#pragma data_seg("SHARED")
static HMODULE mpgDll = NULL;
static void (*mpg_exit)();
static int (*mpg_init)();
static int* (*mpg_new)(const char* decoder, int* error);
static int (*mpg_close)(int* mh);
static void (*mpg_delete)(int* mh);
static int (*mpg_open_handle)(int* mh, void* iohandle);
static int (*mpg_getformat)(int* mh, long* rate, int* channels, int* encoding);
static size_t (*mpg_length)(int* mh);
static size_t (*mpg_outblock)(int* mh);
static int (*mpg_encsize)(int encoding);
static int (*mpg_read)(int* mh, unsigned char* outmemory, size_t outmemsize, size_t* done);
static const char* (*mpg_strerror)(int* mh);
static int (*mpg_errcode)(int* mh);
static const char** (*mpg_supported_decoders)();
static size_t (*mpg_seek)(int* mh, size_t sampleOffset, int whence);
static const char* (*mpg_current_decoder)(int* mh);
typedef int (*mpg_read_func)(void*, void*, size_t);
typedef off_t (*mpg_seek_func)(void*, off_t, int);
typedef int (*mpg_close_func)(void*);
static int(*mpg_replace_reader_handle)(int* mh, mpg_read_func, mpg_seek_func, mpg_close_func);
#pragma data_seg()
template<class Proc> static inline void LoadMpgProc(Proc& outProcVar, const char* procName)
{
outProcVar = (Proc)GetProcAddress(mpgDll, procName);
}
static void _UninitMPG()
{
mpg_exit();
FreeLibrary(mpgDll);
mpgDll = 0;
}
static void _InitMPG()
{
static const char* mpglib = "libmpg123";
if (!(mpgDll = LoadLibraryA(mpglib)))
{
printf("Failed to load DLL %s!\n", mpglib);
return;
}
LoadMpgProc(mpg_exit, "mpg123_exit");
LoadMpgProc(mpg_init, "mpg123_init");
LoadMpgProc(mpg_new, "mpg123_new");
LoadMpgProc(mpg_close, "mpg123_close");
LoadMpgProc(mpg_delete, "mpg123_delete");
LoadMpgProc(mpg_open_handle, "mpg123_open_handle");
LoadMpgProc(mpg_getformat, "mpg123_getformat");
LoadMpgProc(mpg_length, "mpg123_length");
LoadMpgProc(mpg_outblock, "mpg123_outblock");
LoadMpgProc(mpg_encsize, "mpg123_encsize");
LoadMpgProc(mpg_read, "mpg123_read");
LoadMpgProc(mpg_strerror, "mpg123_strerror");
LoadMpgProc(mpg_errcode, "mpg123_errcode");
LoadMpgProc(mpg_supported_decoders, "mpg123_supported_decoders");
LoadMpgProc(mpg_seek, "mpg123_seek");
LoadMpgProc(mpg_current_decoder, "mpg123_current_decoder");
LoadMpgProc(mpg_replace_reader_handle, "mpg123_replace_reader_handle");
mpg_init();
atexit(_UninitMPG);
}
/**
* Creates a new unitialized MP3 AudioStreamer.
* You should call OpenStream(file) to initialize the stream.
*/
MP3Streamer::MP3Streamer() : AudioStreamer()
{
if (!mpgDll) _InitMPG();
}
/**
* Creates and Initializes a new MP3 AudioStreamer.
*/
MP3Streamer::MP3Streamer(const char* file) : AudioStreamer()
{
if (!mpgDll) _InitMPG();
OpenStream(file);
}
/**
* Destroys the AudioStream and frees all held resources
*/
MP3Streamer::~MP3Streamer()
{
CloseStream();
}
/**
* Opens a new stream for reading.
* @param file Audio file to open
* @return TRUE if stream is successfully opened and initialized. FALSE if the stream open failed or its already open.
*/
bool MP3Streamer::OpenStream(const char* file)
{
if (!mpgDll) return 0; // mpg123 not present
if (FileHandle) // dont allow reopen an existing stream
return false;
FileHandle = mpg_new(nullptr, nullptr);
mpg_replace_reader_handle(FileHandle, file_read, file_seek, file_close);
void* iohandle = file_open_ro(file);
if (!iohandle) {
indebug(printf("Failed to open file: \"%s\"\n", file));
return false;
}
mpg_open_handle(FileHandle, iohandle);
int rate, numChannels, encoding;
if (mpg_getformat(FileHandle, (long*)&rate, &numChannels, &encoding)) {
indebug(printf("Failed to read mp3 header format: \"%s\"\n", file));
return false;
}
int sampleSize = mpg_encsize(encoding);
// get the actual PCM data size: (NumSamples * NumChannels * SampleSize)
SampleSize = sampleSize;
SampleBlockSize = numChannels * sampleSize;
StreamSize = mpg_length(FileHandle) * SampleBlockSize;
SampleRate = rate;
NumChannels = numChannels;
return true;
}
/**
* Closes the stream and releases all resources held.
*/
void MP3Streamer::CloseStream()
{
if (!mpgDll) return; // mpg123 not present
if (FileHandle)
{
mpg_close(FileHandle);
mpg_delete(FileHandle);
FileHandle = 0;
StreamSize = 0;
StreamPos = 0;
SampleRate = 0;
NumChannels = 0;
SampleSize = 0;
SampleBlockSize = 0;
}
}
/**
* Reads some Audio data from the underlying stream.
* Audio data is decoded into PCM format, suitable for OpenAL.
* @param dstBuffer Destination buffer that receives the data
* @param dstSize Number of bytes to read
* @return Number of bytes read. 0 if stream is uninitialized or end of stream reached.
*/
int MP3Streamer::ReadSome(void* dstBuffer, int dstSize)
{
if (!mpgDll) return 0; // mpg123 not present
if (!FileHandle)
return 0; // nothing to do here
int count = StreamSize - StreamPos; // calc available data from stream
if (count == 0) // if stream available bytes 0?
return 0; // EOS reached
if (count > dstSize) // if stream has more data than buffer
count = dstSize; // set bytes to read bigger
count -= count % SampleBlockSize; // make sure count is aligned to blockSize
size_t bytesRead;
mpg_read(FileHandle, (unsigned char*)dstBuffer, count, &bytesRead);
StreamPos += bytesRead;
return bytesRead;
}
/**
* Seeks to the appropriate byte position in the stream.
* This value is between: [0...StreamSize]
* @param streampos Position in the stream to seek to in bytes
* @return The actual position where seeked, or 0 if out of bounds (this also means the stream was reset to 0).
*/
unsigned int MP3Streamer::Seek(unsigned int streampos)
{
if (!mpgDll) return 0;
if (int(streampos) >= StreamSize)
streampos = 0;
int actual = streampos / SampleBlockSize; // mpg_seek works by sample blocks, so lets select the sample
mpg_seek(FileHandle, actual, SEEK_SET);
return streampos;
}
#pragma endregion
////////
//// OGGStreamer impl.
////
#pragma region OggStreamer
#include "Decoders\Vorbis\vorbisfile.h"
#pragma data_seg("SHARED")
static HMODULE vfDll = NULL;
static int (*oggv_clear)(void* vf) = 0;
static long (*oggv_read)(void* vf, char* buffer, int length, int bigendiannp, int word, int sgned, int* bitstream) = 0;
static long (*oggv_pcm_seek)(void* vf, INT64 pos) = 0;
static UINT64 (*oggv_pcm_tell)(void* vf) = 0;
static UINT64 (*oggv_pcm_total)(void* vf, int i) = 0;
static vorbis_info* (*oggv_info)(void* vf, int link) = 0;
static vorbis_comment* (*oggv_comment)(void* vf, int link) = 0;
static int (*oggv_open_callbacks)(void* datasource, int* vf, char* initial, long ibytes, ov_callbacks cb) = 0;
#pragma data_seg()
static size_t oggv_read_func(void* ptr, size_t size, size_t nmemb, void* handle) {
return file_read(handle, ptr, size * nmemb);
}
static int oggv_seek_func(void* handle, INT64 offset, int whence) {
return file_seek(handle, (long)offset, whence);
}
static int oggv_close_func(void* handle) {
return file_close(handle);
}
static long oggv_tell_func(void* handle) {
return file_tell(handle);
}
template<class Proc> static inline void LoadVorbisProc(Proc* outProcVar, const char* procName)
{
*outProcVar = (Proc)GetProcAddress(vfDll, procName);
}
static void _UninitVorbis()
{
FreeLibrary(vfDll);
vfDll = NULL;
}
static void _InitVorbis()
{
static const char* vorbislib = "vorbisfile";
if (!(vfDll = LoadLibraryA(vorbislib))) // ogg.dll and vorbis.dll is loaded by vorbisfile.dll
{
printf("Failed to load DLL %s!\n", vorbislib);
return;
}
LoadVorbisProc(&oggv_clear, "ov_clear");
LoadVorbisProc(&oggv_read, "ov_read");
LoadVorbisProc(&oggv_pcm_seek, "ov_pcm_seek");
LoadVorbisProc(&oggv_pcm_tell, "ov_pcm_tell");
LoadVorbisProc(&oggv_pcm_total, "ov_pcm_total");
LoadVorbisProc(&oggv_info, "ov_info");
LoadVorbisProc(&oggv_comment, "ov_comment");
LoadVorbisProc(&oggv_open_callbacks, "ov_open_callbacks");
atexit(_UninitVorbis);
}
/**
* Creates a new unitialized OGG AudioStreamer.
* You should call OpenStream(file) to initialize the stream.
*/
OGGStreamer::OGGStreamer() : AudioStreamer()
{
if(!vfDll) _InitVorbis();
}
/**
* Creates and Initializes a new OGG AudioStreamer.
*/
OGGStreamer::OGGStreamer(const char* file) : AudioStreamer()
{
if(!vfDll) _InitVorbis();
OpenStream(file);
}
/**
* Destroys the AudioStream and frees all held resources
*/
OGGStreamer::~OGGStreamer()
{
CloseStream();
}
/**
* Opens a new stream for reading.
* @param file Audio file to open
* @return TRUE if stream is successfully opened and initialized. FALSE if the stream open failed or its already open.
*/
bool OGGStreamer::OpenStream(const char* file)
{
if (!vfDll) return false; // vorbis not present
if (FileHandle) // dont allow reopen an existing stream
return false;
void* iohandle = file_open_ro(file);
if (!iohandle) {
indebug(printf("Failed to open file: \"%s\"\n", file));
return false;
}
ov_callbacks cb = { oggv_read_func, oggv_seek_func, oggv_close_func, oggv_tell_func };
FileHandle = (int*)malloc(sizeof(OggVorbis_File)); // filehandle is actually Vorbis handle
if (int err = oggv_open_callbacks(iohandle, FileHandle, NULL, 0, cb)) {
const char* errmsg;
switch(err) {
case OV_EREAD: errmsg = "Error reading OGG file!"; break;
case OV_ENOTVORBIS: errmsg = "Not an OGG vorbis file!"; break;
case OV_EVERSION: errmsg = "Vorbis version mismatch!"; break;
case OV_EBADHEADER: errmsg = "Invalid vorbis bitstream header"; break;
case OV_EFAULT: errmsg = "Internal logic fault"; break;
}
CloseStream();
indebug(printf("Failed to open OGG file \"%s\": %s\n", file, errmsg));
return false;
}
vorbis_info* info = oggv_info(FileHandle, -1);
if(!info) {
CloseStream();
indebug(printf("Failed to acquire OGG stream format: \"%s\"\n", file));
return false;
}
SampleRate = int(info->rate);
NumChannels = info->channels;
SampleSize = 2; // OGG samples are always 16-bit
SampleBlockSize = 2 * NumChannels; // OGG samples are always 16-bit
StreamSize = (int)oggv_pcm_total(FileHandle, -1) * SampleBlockSize; // streamsize in total bytes
return true;
}
/**
* Closes the stream and releases all resources held.
*/
void OGGStreamer::CloseStream()
{
if(!vfDll) return; // vorbis not present
if(FileHandle)
{
oggv_clear(FileHandle);
free(FileHandle);
FileHandle = 0;
StreamSize = 0;
StreamPos = 0;
SampleRate = 0;
NumChannels = 0;
SampleSize = 0;
SampleBlockSize = 0;
}
}
/**
* Reads some Audio data from the underlying stream.
* Audio data is decoded into PCM format, suitable for OpenAL.
* @param dstBuffer Destination buffer that receives the data
* @param dstSize Number of bytes to read. 64KB is good for streaming (gives ~1.5s of playback sound).
* @return Number of bytes read. 0 if stream is uninitialized or end of stream reached before reading.
*/
int OGGStreamer::ReadSome(void* dstBuffer, int dstSize)
{
if(!vfDll) return 0; // vorbis not present
if(!FileHandle)
return 0; // nothing to do here
int count = StreamSize - StreamPos; // calc available data from stream
if(count == 0) // if stream available bytes 0?
return 0; // EOS reached
if(count > dstSize) // if stream has more data than buffer
count = dstSize; // set bytes to read bigger
count -= count % SampleBlockSize; // make sure count is aligned to blockSize
int current_section;
int bytesTotal = 0; // total bytes read
do
{
// TODO: Fix Ogg stream error:
// File handles are not shared, so creating 2x of the same stream can fail
// Find a way to share the stream or the data
int bytesRead = oggv_read(FileHandle, (char*)dstBuffer + bytesTotal,
(count - bytesTotal), 0, 2, 1, ¤t_section);
if (bytesRead == 0)
break; // EOF!
bytesTotal += bytesRead;
}
while (bytesTotal < count);
StreamPos += bytesTotal;
return bytesTotal;
}
/**
* Seeks to the appropriate byte position in the stream.
* This value is between: [0...StreamSize]
* @param streampos Position in the stream to seek to
* @return The actual position where seeked, or 0 if out of bounds (this also means the stream was reset to 0).
*/
unsigned int OGGStreamer::Seek(unsigned int streampos)
{
if (!vfDll) return 0; // vorbis not present
if (int(streampos) >= StreamSize) streampos = 0; // out of bounds, set to beginning
// TODO: Find the mysterious Ogg stream error
oggv_pcm_seek(FileHandle, streampos / SampleBlockSize); // seek PCM samples
return StreamPos = streampos; // finally, update the stream position
}
#pragma endregion
} // namespace S3D