forked from ValleyBell/libvgm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cpp
803 lines (697 loc) · 18.8 KB
/
player.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
#ifdef _WIN32
//#define _WIN32_WINNT 0x500 // for GetConsoleWindow()
#include <windows.h>
#ifdef _DEBUG
#include <crtdbg.h>
#endif
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <vector>
#include <string>
#ifdef _WIN32
extern "C" int __cdecl _getch(void); // from conio.h
extern "C" int __cdecl _kbhit(void);
#else
#include <unistd.h> // for STDIN_FILENO and usleep()
#include <termios.h>
#include <sys/time.h> // for struct timeval in _kbhit()
#define Sleep(msec) usleep(msec * 1000)
#endif
#include <common_def.h>
#include "utils/DataLoader.h"
#include "utils/FileLoader.h"
#include "utils/MemoryLoader.h"
#include "player/playerbase.hpp"
#include "player/s98player.hpp"
#include "player/droplayer.hpp"
#include "player/vgmplayer.hpp"
#include "audio/AudioStream.h"
#include "audio/AudioStream_SpcDrvFuns.h"
#include "emu/Resampler.h"
#include "utils/OSMutex.h"
//#define USE_MEMORY_LOADER 1 // define to use the in-memory loader
int main(int argc, char* argv[]);
static UINT8 *SlurpFile(const char *fileName, UINT32 *fileSize);
static UINT8 GetPlayerForFile(DATA_LOADER *dLoad, PlayerBase** retPlayer);
static const char* GetFileTitle(const char* filePath);
static UINT32 CalcCurrentVolume(UINT32 playbackSmpl);
static UINT32 FillBuffer(void* drvStruct, void* userParam, UINT32 bufSize, void* Data);
static UINT8 FilePlayCallback(PlayerBase* player, void* userParam, UINT8 evtType, void* evtParam);
static UINT32 GetNthAudioDriver(UINT8 adrvType, INT32 drvNumber);
static UINT8 InitAudioSystem(void);
static UINT8 DeinitAudioSystem(void);
static UINT8 StartAudioDevice(void);
static UINT8 StopAudioDevice(void);
static UINT8 StartDiskWriter(const char* fileName);
static UINT8 StopDiskWriter(void);
#ifndef _WIN32
static void changemode(UINT8 noEcho);
static int _kbhit(void);
#define _getch getchar
#endif
static UINT32 smplSize;
static void* audDrv;
static void* audDrvLog;
static UINT32 smplAlloc;
static WAVE_32BS* smplData;
static UINT32 localAudBufSize;
static void* localAudBuffer;
static OS_MUTEX* renderMtx; // render thread mutex
static UINT32 sampleRate = 44100;
static UINT32 maxLoops = 2;
static bool manualRenderLoop = false;
static volatile UINT8 playState;
static UINT32 idWavOut;
static UINT32 idWavOutDev;
static UINT32 idWavWrt;
static INT32 AudioOutDrv = 0;
static INT32 WaveWrtDrv = -1;
static UINT32 masterVol = 0x10000; // fixed point 16.16
static UINT32 fadeSmplStart;
static UINT32 fadeSmplTime;
int main(int argc, char* argv[])
{
int argbase;
UINT8 retVal;
DATA_LOADER *dLoad;
PlayerBase* player;
int curSong;
bool needRefresh;
if (argc < 2)
{
printf("Usage: %s inputfile\n", argv[0]);
return 0;
}
argbase = 1;
#ifdef _WIN32
SetConsoleOutputCP(65001); // set UTF-8 codepage
#endif
retVal = InitAudioSystem();
if (retVal)
return 1;
retVal = StartAudioDevice();
if (retVal)
{
DeinitAudioSystem();
return 1;
}
playState = 0x00;
for (curSong = argbase; curSong < argc; curSong ++)
{
printf("Loading %s ... ", GetFileTitle(argv[curSong]));
fflush(stdout);
player = NULL;
#ifdef USE_MEMORY_LOADER
UINT32 fileSize;
UINT8 *fileData = SlurpFile(argv[curSong],&fileSize);
dLoad = MemoryLoader_Init(fileData, fileSize);
#else
dLoad = FileLoader_Init(argv[curSong]);
#endif
if(dLoad == NULL) continue;
DataLoader_SetPreloadBytes(dLoad,0x100);
retVal = DataLoader_Load(dLoad);
if (retVal)
{
DataLoader_CancelLoading(dLoad);
fprintf(stderr, "Error 0x%02X loading file!\n", retVal);
continue;
}
retVal = GetPlayerForFile(dLoad, &player);
if (retVal)
{
DataLoader_CancelLoading(dLoad);
if (player != NULL)
delete player;
fprintf(stderr, "Error 0x%02X loading file!\n", retVal);
continue;
}
player->SetCallback(&FilePlayCallback, NULL);
if (player->GetPlayerType() == FCC_S98)
{
S98Player* s98play = dynamic_cast<S98Player*>(player);
const S98_HEADER* s98hdr = s98play->GetFileHeader();
printf("S98 v%u, Total Length: %.2f s, Loop Length: %.2f s, Tick Rate: %u/%u", s98hdr->fileVer,
player->Tick2Second(player->GetTotalTicks()), player->Tick2Second(player->GetLoopTicks()),
s98hdr->tickMult, s98hdr->tickDiv);
}
else if (player->GetPlayerType() == FCC_VGM)
{
VGMPlayer* vgmplay = dynamic_cast<VGMPlayer*>(player);
const VGM_HEADER* vgmhdr = vgmplay->GetFileHeader();
printf("VGM v%3X, Total Length: %.2f s, Loop Length: %.2f s", vgmhdr->fileVer,
player->Tick2Second(player->GetTotalTicks()), player->Tick2Second(player->GetLoopTicks()));
}
else if (player->GetPlayerType() == FCC_DRO)
{
DROPlayer* droplay = dynamic_cast<DROPlayer*>(player);
const DRO_HEADER* drohdr = droplay->GetFileHeader();
const char* hwType;
if (drohdr->hwType == 0)
hwType = "OPL2";
else if (drohdr->hwType == 1)
hwType = "DualOPL2";
else if (drohdr->hwType == 2)
hwType = "OPL3";
else
hwType = "unknown";
printf("DRO v%u, Total Length: %.2f s, HW Type: %s", drohdr->verMajor,
player->Tick2Second(player->GetTotalTicks()), hwType);
}
const char* songTitle = NULL;
const char* songAuthor = NULL;
const char* songGame = NULL;
const char* songSystem = NULL;
const char* songDate = NULL;
const char* songComment = NULL;
const char* const* tagList = player->GetTags();
for (const char* const* t = tagList; *t; t += 2)
{
if (!strcmp(t[0], "TITLE"))
songTitle = t[1];
else if (!strcmp(t[0], "ARTIST"))
songAuthor = t[1];
else if (!strcmp(t[0], "GAME"))
songGame = t[1];
else if (!strcmp(t[0], "SYSTEM"))
songSystem = t[1];
else if (!strcmp(t[0], "DATE"))
songDate = t[1];
else if (!strcmp(t[0], "COMMENT"))
songComment = t[1];
}
if (songTitle != NULL && songTitle[0] != '\0')
printf("\nSong Title: %s", songTitle);
if (songAuthor != NULL && songAuthor[0] != '\0')
printf("\nSong Author: %s", songAuthor);
if (songGame != NULL && songGame[0] != '\0')
printf("\nSong Game: %s", songGame);
if (songSystem != NULL && songSystem[0] != '\0')
printf("\nSong System: %s", songSystem);
if (songDate != NULL && songDate[0] != '\0')
printf("\nSong Date: %s", songDate);
if (songComment != NULL && songComment[0] != '\0')
printf("\nSong Comment: %s", songComment);
putchar('\n');
player->SetSampleRate(sampleRate);
player->Start();
fadeSmplTime = player->GetSampleRate() * 4;
fadeSmplStart = (UINT32)-1;
StartDiskWriter("waveOut.wav");
if (audDrv != NULL)
retVal = AudioDrv_SetCallback(audDrv, FillBuffer, &player);
else
retVal = 0xFF;
manualRenderLoop = (retVal != 0x00);
#ifndef _WIN32
changemode(1);
#endif
playState &= ~PLAYSTATE_END;
needRefresh = true;
while(! (playState & PLAYSTATE_END))
{
if (! (playState & PLAYSTATE_PAUSE))
needRefresh = true; // always update when playing
if (needRefresh)
{
const char* pState;
if (playState & PLAYSTATE_PAUSE)
pState = "Paused";
else if (fadeSmplStart != (UINT32)-1)
pState = "Fading";
else
pState = "Playing";
printf("%s %.2f / %.2f ... \r", pState, player->Sample2Second(player->GetCurSample()),
player->Tick2Second(player->GetTotalPlayTicks(maxLoops)));
fflush(stdout);
needRefresh = false;
}
if (manualRenderLoop && ! (playState & PLAYSTATE_PAUSE))
{
UINT32 wrtBytes = FillBuffer(audDrvLog, &player, localAudBufSize, localAudBuffer);
AudioDrv_WriteData(audDrvLog, wrtBytes, localAudBuffer);
}
else
{
Sleep(50);
}
if (_kbhit())
{
int inkey = _getch();
int letter = toupper(inkey);
if (letter == ' ' || letter == 'P')
{
playState ^= PLAYSTATE_PAUSE;
if (audDrv != NULL)
{
if (playState & PLAYSTATE_PAUSE)
AudioDrv_Pause(audDrv);
else
AudioDrv_Resume(audDrv);
}
}
else if (letter == 'R') // restart
{
OSMutex_Lock(renderMtx);
player->Reset();
fadeSmplStart = (UINT32)-1;
OSMutex_Unlock(renderMtx);
}
else if (letter == 'B') // previous file
{
if (curSong > argbase)
{
playState |= PLAYSTATE_END;
curSong -= 2;
}
}
else if (letter == 'N') // next file
{
if (curSong + 1 < argc)
playState |= PLAYSTATE_END;
}
else if (inkey == 0x1B || letter == 'Q') // quit
{
playState |= PLAYSTATE_END;
curSong = argc - 1;
}
else if (letter == 'F') // fade out
{
fadeSmplStart = player->GetCurSample();
}
needRefresh = true;
}
}
#ifndef _WIN32
changemode(0);
#endif
// remove callback to prevent further rendering
// also waits for render thread to finish its work
if (audDrv != NULL)
AudioDrv_SetCallback(audDrv, NULL, NULL);
StopDiskWriter();
player->Stop();
player->UnloadFile();
DataLoader_Deinit(dLoad);
delete player; player = NULL; dLoad = NULL;
#ifdef USE_MEMORY_LOADER
free(fileData);
#endif
} // end for(curSong)
StopAudioDevice();
DeinitAudioSystem();
printf("Done.\n");
#if defined(_DEBUG) && (_MSC_VER >= 1400)
// doesn't work well with C++ containers
//if (_CrtDumpMemoryLeaks())
// _getch();
#endif
return 0;
}
static UINT8 *SlurpFile(const char *fileName, UINT32 *fileSize)
{
*fileSize = 0;
FILE *hFile = fopen(fileName,"rb");
UINT32 hFileSize;
UINT8 *fileData;
if(hFile == NULL) return NULL;
fseek(hFile,0,SEEK_END);
hFileSize = ftell(hFile);
rewind(hFile);
fileData = (UINT8 *)malloc(hFileSize);
if(fileData == NULL) return NULL;
if(fread(fileData,1,hFileSize,hFile) != hFileSize)
{
free(fileData);
return NULL;
}
*fileSize = hFileSize;
return fileData;
}
static UINT8 GetPlayerForFile(DATA_LOADER *dLoad, PlayerBase** retPlayer)
{
UINT8 retVal;
PlayerBase* player;
if (! VGMPlayer::IsMyFile(dLoad))
player = new VGMPlayer;
else if (! S98Player::IsMyFile(dLoad))
player = new S98Player;
else if (! DROPlayer::IsMyFile(dLoad))
player = new DROPlayer;
else
return 0xFF;
retVal = player->LoadFile(dLoad);
if (retVal < 0x80)
*retPlayer = player;
else
delete player;
return retVal;
}
static const char* GetFileTitle(const char* filePath)
{
const char* dirSep1;
const char* dirSep2;
dirSep1 = strrchr(filePath, '/');
dirSep2 = strrchr(filePath, '\\');
if (dirSep2 > dirSep1)
dirSep1 = dirSep2;
return (dirSep1 == NULL) ? filePath : (dirSep1 + 1);
}
#if 1
#define VOLCALC64
#define VOL_BITS 16 // use .X fixed point for working volume
#else
#define VOL_BITS 8 // use .X fixed point for working volume
#endif
#define VOL_SHIFT (16 - VOL_BITS) // shift for master volume -> working volume
// Pre- and post-shifts are used to make the calculations as accurate as possible
// without causing the sample data (likely 24 bits) to overflow while applying the volume gain.
// Smaller values for VOL_PRESH are more accurate, but have a higher risk of overflows during calculations.
// (24 + VOL_POSTSH) must NOT be larger than 31
#define VOL_PRESH 4 // sample data pre-shift
#define VOL_POSTSH (VOL_BITS - VOL_PRESH) // post-shift after volume multiplication
static UINT32 CalcCurrentVolume(UINT32 playbackSmpl)
{
UINT32 curVol; // 16.16 fixed point
// 1. master volume
curVol = masterVol;
// 2. apply fade-out factor
if (playbackSmpl >= fadeSmplStart)
{
UINT32 fadeSmpls;
UINT64 fadeVol; // 64 bit for less type casts when doing multiplications with .16 fixed point
fadeSmpls = playbackSmpl - fadeSmplStart;
if (fadeSmpls >= fadeSmplTime)
return 0x0000; // going beyond fade time -> volume 0
fadeVol = (UINT64)fadeSmpls * 0x10000 / fadeSmplTime;
fadeVol = 0x10000 - fadeVol; // fade from full volume to silence
fadeVol = fadeVol * fadeVol; // logarithmic fading sounds nicer
curVol = (UINT32)((fadeVol * curVol) >> 32);
}
return curVol;
}
static UINT32 FillBuffer(void* drvStruct, void* userParam, UINT32 bufSize, void* data)
{
PlayerBase* player;
UINT32 basePbSmpl;
UINT32 smplCount;
UINT32 smplRendered;
INT16* SmplPtr16;
UINT32 curSmpl;
WAVE_32BS fnlSmpl; // final sample value
INT32 curVolume;
smplCount = bufSize / smplSize;
if (! smplCount)
return 0;
player = *(PlayerBase**)userParam;
if (player == NULL)
{
memset(data, 0x00, smplCount * smplSize);
return smplCount * smplSize;
}
if (! (player->GetState() & PLAYSTATE_PLAY))
{
fprintf(stderr, "Player Warning: calling Render while not playing! playState = 0x%02X\n", player->GetState());
memset(data, 0x00, smplCount * smplSize);
return smplCount * smplSize;
}
OSMutex_Lock(renderMtx);
if (smplCount > smplAlloc)
smplCount = smplAlloc;
memset(smplData, 0, smplCount * sizeof(WAVE_32BS));
basePbSmpl = player->GetCurSample();
smplRendered = player->Render(smplCount, smplData);
smplCount = smplRendered;
curVolume = (INT32)CalcCurrentVolume(basePbSmpl) >> VOL_SHIFT;
SmplPtr16 = (INT16*)data;
for (curSmpl = 0; curSmpl < smplCount; curSmpl ++, basePbSmpl ++, SmplPtr16 += 2)
{
if (basePbSmpl >= fadeSmplStart)
{
UINT32 fadeSmpls;
fadeSmpls = basePbSmpl - fadeSmplStart;
if (fadeSmpls >= fadeSmplTime && ! (playState & PLAYSTATE_END))
{
playState |= PLAYSTATE_END;
break;
}
curVolume = (INT32)CalcCurrentVolume(basePbSmpl) >> VOL_SHIFT;
}
// Input is about 24 bits (some cores might output a bit more)
fnlSmpl = smplData[curSmpl];
#ifdef VOLCALC64
fnlSmpl.L = (INT32)( ((INT64)fnlSmpl.L * curVolume) >> VOL_BITS );
fnlSmpl.R = (INT32)( ((INT64)fnlSmpl.R * curVolume) >> VOL_BITS );
#else
fnlSmpl.L = ((fnlSmpl.L >> VOL_PRESH) * curVolume) >> VOL_POSTSH;
fnlSmpl.R = ((fnlSmpl.R >> VOL_PRESH) * curVolume) >> VOL_POSTSH;
#endif
fnlSmpl.L >>= 8; // 24 bit -> 16 bit
fnlSmpl.R >>= 8;
if (fnlSmpl.L < -0x8000)
fnlSmpl.L = -0x8000;
else if (fnlSmpl.L > +0x7FFF)
fnlSmpl.L = +0x7FFF;
if (fnlSmpl.R < -0x8000)
fnlSmpl.R = -0x8000;
else if (fnlSmpl.R > +0x7FFF)
fnlSmpl.R = +0x7FFF;
SmplPtr16[0] = (INT16)fnlSmpl.L;
SmplPtr16[1] = (INT16)fnlSmpl.R;
}
OSMutex_Unlock(renderMtx);
return curSmpl * smplSize;
}
static UINT8 FilePlayCallback(PlayerBase* player, void* userParam, UINT8 evtType, void* evtParam)
{
switch(evtType)
{
case PLREVT_START:
//printf("Playback started.\n");
break;
case PLREVT_STOP:
//printf("Playback stopped.\n");
break;
case PLREVT_LOOP:
{
UINT32* curLoop = (UINT32*)evtParam;
if (*curLoop >= maxLoops)
{
if (fadeSmplTime)
{
if (fadeSmplStart == (UINT32)-1)
fadeSmplStart = player->GetCurSample();
}
else
{
printf("Loop End.\n");
playState |= PLAYSTATE_END;
return 0x01;
}
}
printf("Loop %u.\n", 1 + *curLoop);
}
break;
case PLREVT_END:
playState |= PLAYSTATE_END;
printf("Song End.\n");
break;
}
return 0x00;
}
static UINT32 GetNthAudioDriver(UINT8 adrvType, INT32 drvNumber)
{
if (drvNumber == -1)
return (UINT32)-1;
UINT32 drvCount;
UINT32 curDrv;
INT32 typedDrv;
AUDDRV_INFO* drvInfo;
// go through all audio drivers get the ID of the requested Output/Disk Writer driver
drvCount = Audio_GetDriverCount();
for (typedDrv = 0, curDrv = 0; curDrv < drvCount; curDrv ++)
{
Audio_GetDriverInfo(curDrv, &drvInfo);
if (drvInfo->drvType == adrvType)
{
if (typedDrv == drvNumber)
return curDrv;
typedDrv ++;
}
}
return (UINT32)-1;
}
// initialize audio system and search for requested audio drivers
static UINT8 InitAudioSystem(void)
{
AUDDRV_INFO* drvInfo;
UINT8 retVal;
retVal = OSMutex_Init(&renderMtx, 0);
printf("Opening Audio Device ...\n");
retVal = Audio_Init();
if (retVal == AERR_NODRVS)
return retVal;
idWavOut = GetNthAudioDriver(ADRVTYPE_OUT, AudioOutDrv);
idWavOutDev = 0; // default device
if (AudioOutDrv != -1 && idWavOut == (UINT32)-1)
{
fprintf(stderr, "Requested Audio Output driver not found!\n");
Audio_Deinit();
return AERR_NODRVS;
}
idWavWrt = GetNthAudioDriver(ADRVTYPE_DISK, WaveWrtDrv);
audDrv = NULL;
if (idWavOut != (UINT32)-1)
{
Audio_GetDriverInfo(idWavOut, &drvInfo);
printf("Using driver %s.\n", drvInfo->drvName);
retVal = AudioDrv_Init(idWavOut, &audDrv);
if (retVal)
{
fprintf(stderr, "WaveOut: Driver Init Error: %02X\n", retVal);
Audio_Deinit();
return retVal;
}
#ifdef AUDDRV_DSOUND
if (drvInfo->drvSig == ADRVSIG_DSOUND)
DSound_SetHWnd(AudioDrv_GetDrvData(audDrv), GetDesktopWindow());
#endif
}
audDrvLog = NULL;
if (idWavWrt != (UINT32)-1)
{
retVal = AudioDrv_Init(idWavWrt, &audDrvLog);
if (retVal)
audDrvLog = NULL;
}
return 0x00;
}
static UINT8 DeinitAudioSystem(void)
{
UINT8 retVal;
retVal = 0x00;
if (audDrv != NULL)
{
retVal = AudioDrv_Deinit(&audDrv); audDrv = NULL;
}
if (audDrvLog != NULL)
{
AudioDrv_Deinit(&audDrvLog); audDrvLog = NULL;
}
Audio_Deinit();
OSMutex_Deinit(renderMtx); renderMtx = NULL;
return retVal;
}
static UINT8 StartAudioDevice(void)
{
AUDIO_OPTS* opts;
UINT8 retVal;
opts = NULL;
smplAlloc = 0x00;
smplData = NULL;
if (audDrv != NULL)
opts = AudioDrv_GetOptions(audDrv);
else if (audDrvLog != NULL)
opts = AudioDrv_GetOptions(audDrvLog);
if (opts == NULL)
return 0xFF;
opts->sampleRate = sampleRate;
opts->numChannels = 2;
opts->numBitsPerSmpl = 16;
smplSize = opts->numChannels * opts->numBitsPerSmpl / 8;
if (audDrv != NULL)
{
printf("Opening Device %u ...\n", idWavOutDev);
retVal = AudioDrv_Start(audDrv, idWavOutDev);
if (retVal)
{
fprintf(stderr, "Device Init Error: %02X\n", retVal);
return retVal;
}
smplAlloc = AudioDrv_GetBufferSize(audDrv) / smplSize;
localAudBufSize = 0;
}
else
{
smplAlloc = opts->sampleRate / 4;
localAudBufSize = smplAlloc * smplSize;
}
smplData = (WAVE_32BS*)malloc(smplAlloc * sizeof(WAVE_32BS));
localAudBuffer = localAudBufSize ? malloc(localAudBufSize) : NULL;
return 0x00;
}
static UINT8 StopAudioDevice(void)
{
UINT8 retVal;
retVal = 0x00;
if (audDrv != NULL)
retVal = AudioDrv_Stop(audDrv);
free(smplData); smplData = NULL;
free(localAudBuffer); localAudBuffer = NULL;
return retVal;
}
static UINT8 StartDiskWriter(const char* fileName)
{
AUDIO_OPTS* opts;
UINT8 retVal;
if (audDrvLog == NULL)
return 0x00;
opts = AudioDrv_GetOptions(audDrvLog);
if (audDrv != NULL)
*opts = *AudioDrv_GetOptions(audDrv);
WavWrt_SetFileName(AudioDrv_GetDrvData(audDrvLog), fileName);
retVal = AudioDrv_Start(audDrvLog, 0);
if (! retVal && audDrv != NULL)
AudioDrv_DataForward_Add(audDrv, audDrvLog);
return retVal;
}
static UINT8 StopDiskWriter(void)
{
if (audDrvLog == NULL)
return 0x00;
if (audDrv != NULL)
AudioDrv_DataForward_Remove(audDrv, audDrvLog);
return AudioDrv_Stop(audDrvLog);
}
#ifndef _WIN32
static struct termios oldterm;
static UINT8 termmode = 0xFF;
static void changemode(UINT8 noEcho)
{
if (termmode == 0xFF)
{
tcgetattr(STDIN_FILENO, &oldterm);
termmode = 0;
}
if (termmode == noEcho)
return;
if (noEcho)
{
struct termios newterm;
newterm = oldterm;
newterm.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newterm);
termmode = 1;
}
else
{
tcsetattr(STDIN_FILENO, TCSANOW, &oldterm);
termmode = 0;
}
return;
}
static int _kbhit(void)
{
struct timeval tv;
fd_set rdfs;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&rdfs);
FD_SET(STDIN_FILENO, &rdfs);
select(STDIN_FILENO + 1, &rdfs, NULL, NULL, &tv);
return FD_ISSET(STDIN_FILENO, &rdfs);;
}
#endif