-
Notifications
You must be signed in to change notification settings - Fork 0
/
wlaudio.cpp
672 lines (580 loc) · 16.9 KB
/
wlaudio.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
#include "wl_def.h"
#include "wlaudio.h"
#include "wlpage.h"
#include "wlcache.h"
#include "wlinput.h"
#include "sound.h"
#include <QDebug>
extern WLPage* wlpage;
extern WLCache* wlcache;
extern WLInput* wlinput;
extern void gsleep(int ms);
static quint8** g_soundTable;
static quint8 * volatile g_alSound;
WLAudio::WLAudio():
m_adLib(false), m_soundBlaster(false), m_soundPositioned(0),
m_numReadySamples(0), m_curAlSound(0), m_curAlSoundPtr(0), m_curAlLengthLeft(0),
m_soundTimeCounter(5), m_originalSampleRate(7042), m_sqMaxTracks(10),
m_alChar(0x20), m_alScale(0x40), m_alAttack(0x60), m_alSus(0x80), m_alWave(0xe0),
m_alFreqL(0xa0), m_alFreqH(0xb0), m_alFeedCon(0xc0), m_alEffects(0xbd),
m_nextSoundPos(false)
{
QAudioFormat format;
#if QT_VERSION >= 0x050000
format.setSampleRate(param_samplerate);
format.setChannelCount(2);
#else
format.setFrequency(param_samplerate);
format.setChannels(2);
#endif
format.setByteOrder(QAudioFormat::LittleEndian);
format.setCodec("audio/pcm");
format.setSampleSize(16);
format.setSampleType(QAudioFormat::SignedInt);
m_soundServer = new GameSoundServer(format);
m_samplesPerMusicTick = param_samplerate / 700;
if(YM3812Init(1,3579545,param_samplerate)) {
printf("Unable to create virtual OPL!!\n");
}
for(int i = 1;i < 0xf6;i++)
YM3812Write(0,i,0);
YM3812Write(0, 1, 0x20); // Set WSE=1
m_adLib = true;
m_soundBlaster = true;
m_alTimeCount = 0;
setSoundMode(sdm_Off);
setMusicMode(smm_Off);
setupDigi();
}
WLAudio::~WLAudio()
{
musicOff();
stopSound();
free(m_digiList);
delete m_soundServer;
}
qint32 WLAudio::getChannelForDigi(qint32 which)
{
if(m_digiChannel[which] != -1) return m_digiChannel[which];
return 1;
}
void WLAudio::positionSound(int leftVol, int rightVol)
{
m_leftPosition = leftVol;
m_rightPosition = rightVol;
m_nextSoundPos = true;
}
bool WLAudio::playSound(const QString& snd)
{
qint8 ispos;
SoundCommon* s;
qint32 lp,rp;
lp = m_leftPosition;
rp = m_rightPosition;
m_leftPosition = 0;
m_rightPosition = 0;
ispos = m_nextSoundPos;
m_nextSoundPos = false;
qint32 sound = wlcache->findSound(snd);
if (sound < 0 || (m_digiMode == sds_Off && m_soundMode == sdm_Off))
return 0;
s = (SoundCommon*) g_soundTable[sound];
if ((m_soundMode != sdm_Off) && !s)
Quit("SD_PlaySound() - Uncached sound");
if ((m_digiMode != sds_Off) && (m_digiMap[sound] != -1)) {
if ((m_digiMode == sds_PC) && (m_soundMode == sdm_PC)) {
return 0;
} else {
int channel = playDigitized(m_digiMap[sound], lp, rp);
m_soundPositioned = ispos;
m_digiNumber = sound;
m_digiPriority = s->priority;
return channel + 1;
}
return(true);
}
if (m_soundMode == sdm_Off)
return 0;
if (!s->length)
Quit("SD_PlaySound() - Zero length sound");
if (s->priority < m_soundPriority)
return 0;
switch (m_soundMode) {
case sdm_PC:
break;
case sdm_AdLib:
alPlaySound((AdLibSound_t*)s);
break;
default:
break;
}
m_soundNumber = sound;
m_soundPriority = s->priority;
return 0;
}
void WLAudio::setPosition(int channel, int leftPos,int rightPos)
{
Q_UNUSED(channel)
if((leftPos < 0) || (leftPos > 15) || (rightPos < 0) || (rightPos > 15)
|| ((leftPos == 15) && (rightPos == 15)))
Quit("SD_SetPosition: Illegal position");
switch (m_digiMode){
case sds_SoundBlaster:
break;
default:
break;
}
}
void WLAudio::stopSound()
{
if (m_digiPlaying)
stopDigitized();
switch (m_soundMode)
{
case sdm_PC:
break;
case sdm_AdLib:
alStopSound();
break;
default:
break;
}
m_soundPositioned = false;
soundFinished();
}
void WLAudio::waitSoundDone()
{
while (soundPlaying())
gsleep(5);
}
void WLAudio::startMusic(int chunk)
{
musicOff();
if (m_musicMode == smm_AdLib) {
qint32 chunkLen = wlcache->cacheAudio(chunk);
m_sqHack = (quint16 *)(void *) wlcache->audio(chunk); // alignment is correct
if(*m_sqHack == 0) m_sqHackLen = m_sqHackSeqLen = chunkLen;
else m_sqHackLen = m_sqHackSeqLen = *m_sqHack++;
m_sqHackPtr = m_sqHack;
m_sqHackTime = 0;
m_alTimeCount = 0;
musicOn();
}
}
void WLAudio::continueMusic(int chunk, int startoffs)
{
musicOff();
if (m_musicMode == smm_AdLib) {
qint32 chunkLen = wlcache->cacheAudio(chunk);
m_sqHack = (quint16 *)(void *) wlcache->audio(chunk); // alignment is correct
if(*m_sqHack == 0) m_sqHackLen = m_sqHackSeqLen = chunkLen;
else m_sqHackLen = m_sqHackSeqLen = *m_sqHack++;
m_sqHackPtr = m_sqHack;
if(startoffs >= m_sqHackLen) {
Quit("SD_StartMusic: Illegal startoffs provided!");
}
for(int i = 0; i < startoffs; i += 2) {
quint8 reg = *(quint8*)m_sqHackPtr;
quint8 val = *(((quint8*)m_sqHackPtr) + 1);
if(reg >= 0xb1 && reg <= 0xb8) val &= 0xdf; // disable play note flag
else if(reg == 0xbd) val &= 0xe0; // disable drum flags
alOut(reg,val);
m_sqHackPtr += 2;
m_sqHackLen -= 4;
}
m_sqHackTime = 0;
m_alTimeCount = 0;
musicOn();
}
}
void WLAudio::musicOn()
{
m_sqActive = true;
}
void WLAudio::fadeOutMusic()
{
switch (m_musicMode) {
case smm_AdLib:
musicOff();
break;
default:
break;
}
}
quint16 WLAudio::musicOff()
{
quint16 i;
m_sqActive = false;
switch (m_musicMode) {
case smm_AdLib:
alOut(m_alEffects, 0);
for (i = 0;i < m_sqMaxTracks;i++)
alOut(m_alFreqH + i + 1, 0);
break;
default:
break;
}
return (int) (m_sqHackPtr-m_sqHack);
}
bool WLAudio::musicPlaying()
{
qint8 result;
switch (m_musicMode) {
case smm_AdLib:
result = m_sqActive;
break;
default:
result = false;
break;
}
return(result);
}
bool WLAudio::setSoundMode(SDMode mode)
{
qint8 result = false;
quint16 tableoffset;
stopSound();
if ((mode == sdm_AdLib) && !m_adLib)
mode = sdm_PC;
switch (mode) {
case sdm_Off:
tableoffset = wlcache->findSound("LASTSOUND"); //STARTADLIBSOUNDS;
result = true;
break;
case sdm_PC:
tableoffset = 0; //STARTPCSOUNDS;
result = true;
break;
case sdm_AdLib:
tableoffset = wlcache->findSound("LASTSOUND"); //STARTADLIBSOUNDS;
if (m_adLib)
result = true;
break;
default:
Quit("SD_SetSoundMode: Invalid sound mode %i", mode);
return false;
}
g_soundTable = (quint8**)wlcache->audioData(tableoffset);
if (result && (mode != m_soundMode)) {
shutDevice();
m_soundMode = mode;
startDevice();
}
return(result);
}
bool WLAudio::setMusicMode(SMMode mode)
{
qint8 result = false;
fadeOutMusic();
while (musicPlaying())
gsleep(5);
switch (mode) {
case smm_Off:
result = true;
break;
case smm_AdLib:
if (m_adLib)
result = true;
break;
}
if (result)
m_musicMode = mode;
return(result);
}
quint16 WLAudio::soundPlaying()
{
qint8 result = false;
switch (m_soundMode) {
case sdm_PC:
result = m_pcSound? true : false;
break;
case sdm_AdLib:
result = g_alSound? true : false;
break;
default:
break;
}
if (result)
return(m_soundNumber);
else
return(false);
}
void WLAudio::setDigiDevice(SDSMode mode)
{
bool deviceNotPresent = false;
if (mode == m_digiMode)
return;
stopDigitized();
switch (mode) {
case sds_SoundBlaster:
if (!m_soundBlaster)
deviceNotPresent = true;
break;
default:
break;
}
if (!deviceNotPresent) {
m_digiMode = mode;
}
}
void WLAudio::prepareSound(int which)
{
if(m_digiList == NULL)
Quit("SD_PrepareSound(%i): DigiList not initialized!\n", which);
int page = m_digiList[which].startpage;
int size = m_digiList[which].length;
quint8 *origsamples = (quint8*)wlpage->sound(page);
if (origsamples + size >= (quint8*)wlpage->end())
Quit("SD_PrepareSound(%i): Sound reaches out of page file!\n", which);
int destsamples = (int) ((float) size * (float) param_samplerate
/ (float) m_originalSampleRate);
quint8 *wavebuffer = (quint8*)malloc(sizeof(headChunk_t) + sizeof(waveChunk_t)
+ destsamples * 2); // dest are 16-bit samples
if(wavebuffer == NULL)
Quit("Unable to allocate wave buffer for sound %i!\n", which);
headChunk_t head = {{'R','I','F','F'}, 0, {'W','A','V','E'},
{'f','m','t',' '}, 0x10, 0x0001, 1, param_samplerate, param_samplerate*2, 2, 16};
waveChunk_t dhead = {{'d', 'a', 't', 'a'}, destsamples*2};
head.filelenminus8 = sizeof(head) + destsamples*2; // (sizeof(dhead)-8 = 0)
memcpy(wavebuffer, &head, sizeof(head));
memcpy(wavebuffer+sizeof(head), &dhead, sizeof(dhead));
qint16 *newsamples = (qint16 *)(void *) (wavebuffer + sizeof(headChunk_t)
+ sizeof(waveChunk_t));
float cursample = 0.F;
float samplestep = (float) m_originalSampleRate / (float) param_samplerate;
for(int i=0; i<destsamples; i++, cursample+=samplestep) {
newsamples[i] = getSample((float)size * (float)i / (float)destsamples,
origsamples, size);
}
mixChunk_t sample;
sample.buffer = (char*)wavebuffer;
sample.length = sizeof(headChunk_t)+sizeof(waveChunk_t)+destsamples*2;
sample.volume = 100;
sample.pos = 0;
m_soundServer->setSound(which,&sample);
}
qint32 WLAudio::playDigitized(quint16 which, qint32 leftPos, qint32 rightPos)
{
if (!m_digiMode)
return 0;
if (which >= m_numDigi)
Quit("SD_PlayDigitized: bad sound number %i", which);
int channel = getChannelForDigi(which);
setPosition(channel, leftPos,rightPos);
m_digiPlaying = true;
mixChunk_t sample;
m_soundServer->sound(which,&sample);
if (sample.length == 0) {
qWarning("SoundChunks[%d] is NULL!",which);
return 0;
}
m_soundServer->playSound(which);
return channel;
}
void WLAudio::stopDigitized()
{
m_digiPlaying = false;
m_digiNumber = 0;
m_digiPriority = 0;
m_soundPositioned = false;
if ((m_digiMode == sds_PC) && (m_soundMode == sdm_PC))
soundFinished();
switch (m_digiMode) {
case sds_PC:
break;
case sds_SoundBlaster:
break;
default:
break;
}
}
void WLAudio::soundFinished()
{
m_soundNumber = 0;
m_soundPriority = 0;
}
qint16 WLAudio::getSample(float csample, quint8 *samples, int size)
{
float s0=0, s1=0, s2=0;
int cursample = (int) csample;
float sf = csample - (float) cursample;
if(cursample-1 >= 0) s0 = (float) (samples[cursample-1] - 128);
s1 = (float) (samples[cursample] - 128);
if(cursample+1 < size) s2 = (float) (samples[cursample+1] - 128);
float val = s0*sf*(sf-1)/2 - s1*(sf*sf-1) + s2*(sf+1)*sf/2;
qint32 intval = (qint32) (val * 256);
if(intval < -32768) intval = -32768;
else if(intval > 32767) intval = 32767;
return (qint16) intval;
}
void WLAudio::channelFinished(qint32 channel)
{
m_channelSoundPos[channel].valid = 0;
}
void WLAudio::setupDigi()
{
quint16 *soundInfoPage=(quint16*)wlpage->page(wlpage->totalChunks()-1);
m_numDigi = (quint16)wlpage->pageSize(wlpage->totalChunks()-1)/4;
m_digiList = (digiInfo_t*) malloc(m_numDigi * sizeof(digiInfo_t));
int i;
for(i = 0; i < m_numDigi; i++) {
m_digiList[i].startpage = soundInfoPage[i * 2];
if((int) m_digiList[i].startpage >= (int)wlpage->totalChunks() - 1) {
m_numDigi = i;
break;
}
int lastPage;
if(i < m_numDigi - 1) {
lastPage = soundInfoPage[i * 2 + 2];
if(lastPage == 0 || lastPage + (int)wlpage->soundStart() > (int)wlpage->totalChunks() - 1) lastPage = wlpage->totalChunks() - 1;
else lastPage += wlpage->soundStart();
} else lastPage = wlpage->totalChunks() - 1;
int size = 0;
for(int page = (int)wlpage->soundStart() + m_digiList[i].startpage; page < lastPage; page++)
size += wlpage->pageSize(page);
if(lastPage == (int)wlpage->totalChunks() - 1 && (int)wlpage->soundInfoPagePadded()) size--;
if((size & 0xffff0000) != 0 && (size & 0xffff) < soundInfoPage[i * 2 + 1])
size -= 0x10000;
size = (size & 0xffff0000) | soundInfoPage[i * 2 + 1];
m_digiList[i].length = size;
}
for(i = 0; i < (qint32)wlcache->findSound("LASTSOUND"); i++) {
m_digiMap[i] = -1;
m_digiChannel[i] = -1;
}
}
void WLAudio::alStopSound()
{
g_alSound = 0;
alOut(m_alFreqH + 0, 0);
}
void WLAudio::alSetFXInst(Instrument_t *inst)
{
quint8 c,m;
m = 0; // modulator cell for channel 0
c = 3; // carrier cell for channel 0
alOut(m + m_alChar,inst->mChar);
alOut(m + m_alScale,inst->mScale);
alOut(m + m_alAttack,inst->mAttack);
alOut(m + m_alSus,inst->mSus);
alOut(m + m_alWave,inst->mWave);
alOut(c + m_alChar,inst->cChar);
alOut(c + m_alScale,inst->cScale);
alOut(c + m_alAttack,inst->cAttack);
alOut(c + m_alSus,inst->cSus);
alOut(c + m_alWave,inst->cWave);
alOut(m_alFeedCon,0);
}
void WLAudio::alPlaySound(AdLibSound_t *sound)
{
Instrument_t *inst;
quint8 *data;
alStopSound();
m_alLengthLeft = sound->common.length;
data = sound->data;
m_alBlock = ((sound->block & 7) << 2) | 0x20;
inst = &sound->inst;
if (!(inst->mSus | inst->cSus)) {
Quit("alPlaySound() - Bad instrument");
}
alSetFXInst(inst);
g_alSound = (quint8*)data;
}
void WLAudio::shutAl()
{
g_alSound = 0;
alOut(m_alEffects,0);
alOut(m_alFreqH + 0,0);
alSetFXInst(&m_alZeroInst);
}
void WLAudio::startAl()
{
alOut(m_alEffects, 0);
alSetFXInst(&m_alZeroInst);
}
void WLAudio::shutDevice()
{
switch (m_soundMode)
{
case sdm_PC:
break;
case sdm_AdLib:
shutAl();
break;
default:
break;
}
m_soundMode = sdm_Off;
}
void WLAudio::startDevice()
{
switch (m_soundMode) {
case sdm_AdLib:
startAl();
break;
default:
break;
}
m_soundNumber = 0;
m_soundPriority = 0;
}
void WLAudio::musicPlayer(void *udata, quint8 *stream, int len)
{
Q_UNUSED(udata)
int stereolen = len>>1;
int sampleslen = stereolen>>1;
qint16 *stream16 = (qint16*) (void *) stream; // expect correct alignment
while(1) {
if(m_numReadySamples) {
if(m_numReadySamples < sampleslen) {
YM3812UpdateOne(0, stream16, m_numReadySamples);
stream16 += m_numReadySamples * 2;
sampleslen -= m_numReadySamples;
} else {
YM3812UpdateOne(0, stream16, sampleslen);
m_numReadySamples -= sampleslen;
return;
}
}
m_soundTimeCounter--;
if(!m_soundTimeCounter) {
m_soundTimeCounter = 5;
if(m_curAlSound != g_alSound) {
m_curAlSound = m_curAlSoundPtr = g_alSound;
m_curAlLengthLeft = m_alLengthLeft;
}
if(m_curAlSound) {
if(*m_curAlSoundPtr) {
alOut(m_alFreqL, *m_curAlSoundPtr);
alOut(m_alFreqH, m_alBlock);
} else alOut(m_alFreqH, 0);
m_curAlSoundPtr++;
m_curAlLengthLeft--;
if(!m_curAlLengthLeft) {
m_curAlSound = g_alSound = 0;
m_soundNumber = 0;
m_soundPriority = 0;
alOut(m_alFreqH, 0);
}
}
}
if(m_sqActive) {
do {
if(m_sqHackTime > m_alTimeCount) break;
m_sqHackTime = m_alTimeCount + *(m_sqHackPtr+1);
alOut(*(quint8*)m_sqHackPtr, *(((quint8*)m_sqHackPtr)+1));
m_sqHackPtr += 2;
m_sqHackLen -= 4;
}
while(m_sqHackLen > 0);
m_alTimeCount++;
if(!m_sqHackLen) {
m_sqHackPtr = m_sqHack;
m_sqHackLen = m_sqHackSeqLen;
m_sqHackTime = 0;
m_alTimeCount = 0;
}
}
m_numReadySamples = m_samplesPerMusicTick;
}
}