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
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
// Disable symbol overrides for FILE as that is used in FLAC headers
#define FORBIDDEN_SYMBOL_EXCEPTION_FILE
#include "dsmain.h"
#include "cdaudio.h"
#include "backends/fs/ds/ds-fs.h"
#include "common/config-manager.h"
#include "NDS/scummvm_ipc.h"
#define WAV_FORMAT_IMA_ADPCM 0x14
#define BUFFER_SIZE 8192
#define BUFFER_CHUNK_SIZE (BUFFER_SIZE >> 2)
namespace DS {
namespace CD {
struct WaveHeader {
char riff[4]; // 'RIFF'
u32 size; // Size of the file
char wave[4]; // 'WAVE'
// fmt chunk
char fmt[4]; // 'fmt '
u32 fmtSize; // Chunk size
u16 fmtFormatTag; // Format of this file
u16 fmtChannels; // Num channels
u32 fmtSamPerSec; // Samples per second
u32 fmtBytesPerSec; // Bytes per second
u16 fmtBlockAlign; // Block alignment
u16 fmtBitsPerSam; // Bits per sample
u16 fmtExtraData; // Number of extra fmt bytes
u16 fmtExtra; // Samples per block (only for IMA-ADPCM files)
} __attribute__ ((packed));
struct chunkHeader {
char name[4];
u32 size;
} __attribute__ ((packed));
struct Header {
s16 firstSample;
char stepTableIndex;
char reserved;
} __attribute__ ((packed));
struct decoderFormat {
s16 initial;
unsigned char tableIndex;
unsigned char test;
unsigned char sample[1024];
} __attribute__ ((packed));
static bool s_started = false;
static bool s_active = false;
static WaveHeader waveHeader;
static Header blockHeader;
static FILE *s_file;
static int fillPos;
static bool isPlayingFlag = false;
static s16 *audioBuffer;
static u32 sampleNum;
static s16 *decompressionBuffer;
static int s_numLoops;
static int blockCount;
static int dataChunkStart;
static int blocksLeft;
static bool trackStartsAt2 = false;
// These are from Microsoft's document on DVI ADPCM
static const int stepTab[ 89 ] = {
7, 8, 9, 10, 11, 12, 13, 14,
16, 17, 19, 21, 23, 25, 28, 31,
34, 37, 41, 45, 50, 55, 60, 66,
73, 80, 88, 97, 107, 118, 130, 143,
157, 173, 190, 209, 230, 253, 279, 307,
337, 371, 408, 449, 494, 544, 598, 658,
724, 796, 876, 963, 1060, 1166, 1282, 1411,
1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024,
3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484,
7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794,
32767 };
static const int indexTab[ 16 ] = { -1, -1, -1, -1, 2, 4, 6, 8,
-1, -1, -1, -1, 2, 4, 6, 8 };
void playNextBlock();
void decompressBlock();
void allocBuffers() {
}
void setActive(bool active) {
s_active = active;
}
bool getActive() {
return s_active;
}
void playTrack(int track, int numLoops, int startFrame, int duration) {
Common::String path = ConfMan.get("path");
if (isPlayingFlag) {
stopTrack();
}
if (trackStartsAt2) {
track++;
}
char str[100];
if (path.lastChar() != '/')
path += '/';
Common::String fname;
sprintf(str, "track%d.wav", track);
fname = path + str;
s_file = DS::std_fopen(fname.c_str(), "rb");
if (!s_file) {
sprintf(str, "track%02d.wav", track);
fname = path + str;
s_file = DS::std_fopen(fname.c_str(), "rb");
}
if (!s_file) {
consolePrintf("Failed to open %s!\n", path.c_str());
return;
}
DS::std_fread(&waveHeader, sizeof(waveHeader), 1, s_file);
consolePrintf("File: %s\n", fname.c_str());
consolePrintf("Playing track %d\n", track);
consolePrintf("Format: %d\n", waveHeader.fmtFormatTag);
consolePrintf("Rate : %d\n", waveHeader.fmtSamPerSec);
consolePrintf("Bits : %d\n", waveHeader.fmtBitsPerSam);
consolePrintf("BlkSz : %d\n", waveHeader.fmtExtra);
if ((waveHeader.fmtFormatTag != 17) && (waveHeader.fmtFormatTag != 20)) {
consolePrintf("Wave file is in the wrong format! You must use IMA-ADPCM 4-bit mono.\n");
DS::std_fclose(s_file);
return;
}
for (int r = 0; r < 8; r++) {
IPC->adpcm.buffer[r] = (u8 * volatile) (decoderFormat *) malloc(waveHeader.fmtBlockAlign);
IPC->adpcm.filled[r] = false;
IPC->adpcm.arm7Dirty[r] = false;
}
// Skip chunks until we reach the data chunk
chunkHeader chunk;
DS::std_fread(&chunk, sizeof(chunkHeader), 1, s_file);
while (!((chunk.name[0] == 'd') && (chunk.name[1] == 'a') && (chunk.name[2] == 't') && (chunk.name[3] == 'a'))) {
DS::std_fseek(s_file, chunk.size, SEEK_CUR);
DS::std_fread(&chunk, sizeof(chunkHeader), 1, s_file);
}
dataChunkStart = DS::std_ftell(s_file);
sampleNum = 0;
blockCount = 0;
IPC->streamFillNeeded[0] = true;
IPC->streamFillNeeded[1] = true;
IPC->streamFillNeeded[2] = true;
IPC->streamFillNeeded[3] = true;
if (!s_started) {
fillPos = 0;
audioBuffer = (s16 *) malloc(BUFFER_SIZE * 2);
decompressionBuffer = (s16 *) malloc(waveHeader.fmtExtra * 2);
s_started = true;
// consolePrintf("****Starting buffer*****\n");
memset(audioBuffer, 0, BUFFER_SIZE * 2);
memset(decompressionBuffer, 0, waveHeader.fmtExtra * 2);
DS::playSound(audioBuffer, BUFFER_SIZE * 2, false, false, waveHeader.fmtSamPerSec);
}
fillPos = (IPC->streamPlayingSection + 1) & 3;
isPlayingFlag = true;
// Startframe is a 75Hz timer. Dunno why, since nothing else
// seems to run at that rate.
int tenths = (startFrame * 10) / 75;
// Seek to the nearest block start to the start time
int samples = (tenths * waveHeader.fmtSamPerSec) / 10;
int block = samples / waveHeader.fmtExtra;
if (duration == 0) {
blocksLeft = 0;
} else {
blocksLeft = ((((duration * 100) / 75) * (waveHeader.fmtSamPerSec)) / (waveHeader.fmtExtra) / 100) + 10;
}
// consolePrintf("Playing %d blocks (%d)\n\n", blocksLeft, duration);
// No need to seek if we're starting from the beginning
if (block != 0) {
DS::std_fseek(s_file, dataChunkStart + block * waveHeader.fmtBlockAlign, SEEK_SET);
// consolePrintf("Startframe: %d msec: %d (%d,%d)\n", startFrame, tenthssec, samples, block);
}
//decompressBlock();
playNextBlock();
s_numLoops = numLoops;
}
void update() {
playNextBlock();
}
#ifdef ARM_ADPCM
// FIXME: This code, as well as the source file adpcm_arm.s, are
// apparently unused. Maybe that is a mistake? Or maybe there is a bug
// in ARM_adpcm (then this should be reported and fixed). Or maybe there
// are other good reasons to prefer the C code, but then this as well as
// the assembler source file should be removed.
extern "C" void ARM_adpcm(int *block, int len, int stepTableIndex,
int firstSample, s16 *decompressionBuffer);
#endif
void decompressBlock() {
int block[2048];
bool loop = false;
blockCount++;
if (blockCount < 10) return;
do {
DS::std_fread(&blockHeader, sizeof(blockHeader), 1, s_file);
DS::std_fread(&block[0], waveHeader.fmtBlockAlign - sizeof(blockHeader), 1, s_file);
if (DS::std_feof(s_file)) {
// Reached end of file, so loop
if ((s_numLoops == -1) || (s_numLoops > 1)) {
// Seek file to first packet
if (s_numLoops != -1) {
s_numLoops--;
}
DS::std_fseek(s_file, dataChunkStart, SEEK_SET);
loop = true;
} else {
// Fill decompression buffer with zeros to prevent glitching
for (int r = 0; r < waveHeader.fmtExtra; r++) {
decompressionBuffer[r] = 0;
}
// consolePrintf("Stopping music\n");
stopTrack();
return;
}
} else {
loop = false;
}
} while (loop);
if (blocksLeft > 0) {
blocksLeft--;
// consolePrintf("%d ", blocksLeft);
if (blocksLeft == 0) {
stopTrack();
return;
}
}
#ifdef ARM_ADPCM
ARM_adpcm(block, waveHeader.fmtExtra,
blockHeader.stepTableIndex,
blockHeader.firstSample,
decompressionBuffer);
#else
// First sample is in header
decompressionBuffer[0] = blockHeader.firstSample;
// Set up initial table indeces
int stepTableIndex = blockHeader.stepTableIndex;
int prevSample = blockHeader.firstSample;
// consolePrintf("Decompressing block step=%d fs=%d\n", stepTableIndex, prevSample);
for (int r = 0; r < waveHeader.fmtExtra - 1; r++) {
int word = block[r >> 3];
int offset = 0;
switch (7 - (r & 0x0007)) {
case 0:
offset = (word & 0xF0000000) >> 28;
break;
case 1:
offset = (word & 0x0F000000) >> 24;
break;
case 2:
offset = (word & 0x00F00000) >> 20;
break;
case 3:
offset = (word & 0x000F0000) >> 16;
break;
case 4:
offset = (word & 0x0000F000) >> 12;
break;
case 5:
offset = (word & 0x00000F00) >> 8;
break;
case 6:
offset = (word & 0x000000F0) >> 4;
break;
case 7:
offset = (word & 0x0000000F);
break;
}
int diff = 0;
if (offset & 4) {
diff = diff + stepTab[stepTableIndex];
}
if (offset & 2) {
diff = diff + (stepTab[stepTableIndex] >> 1);
}
if (offset & 1) {
diff = diff + (stepTab[stepTableIndex] >> 2);
}
diff = diff + (stepTab[stepTableIndex] >> 3);
if (offset & 8) {
diff = -diff;
}
int newSample = prevSample + diff;
if (newSample > 32767) newSample = 32767;
if (newSample < -32768) newSample = -32768;
decompressionBuffer[r + 1] = newSample;
prevSample = newSample;
stepTableIndex += indexTab[offset];
if (stepTableIndex > 88) stepTableIndex = 88;
if (stepTableIndex < 0) stepTableIndex = 0;
}
#endif
}
void playNextBlock() {
if (!isPlayingFlag)
return;
int lastBlockId = -1;
while (IPC->adpcm.semaphore); // Wait for buffer to become free if needed
IPC->adpcm.semaphore = true; // Lock the buffer structure to prevent clashing with the ARM7
// DC_FlushAll();
//-8644, 25088
for (int block = fillPos + 1; block < fillPos + 4; block++) {
int blockId = block & 3;
if (IPC->streamFillNeeded[blockId]) {
IPC->streamFillNeeded[blockId] = false;
// DC_FlushAll();
/* if (!(REG_KEYINPUT & KEY_R)) {
//consolePrintf("Align: %d First: %d Step:%d Res:%d\n", waveHeader.fmtBlockAlign, blockHeader.firstSample, blockHeader.stepTableIndex, blockHeader.reserved);
consolePrintf("Filling buffer %d\n", blockId);
}*/
for (int r = blockId * BUFFER_CHUNK_SIZE; r < (blockId + 1) * BUFFER_CHUNK_SIZE; r++) {
if (isPlayingFlag) {
audioBuffer[r] = decompressionBuffer[sampleNum++];
if (sampleNum >= waveHeader.fmtExtra) {
decompressBlock();
sampleNum = 0;
}
}
}
lastBlockId = blockId;
IPC->streamFillNeeded[blockId] = false;
// DC_FlushAll();
}
}
if (lastBlockId != -1) {
fillPos = lastBlockId;
/* if (!(REG_KEYINPUT & KEY_R)) {
consolePrintf("Frame fill done\n");
}*/
}
IPC->adpcm.semaphore = false; // Release the buffer structure
// DC_FlushAll();
}
void stopTrack() {
if (!isPlayingFlag)
return;
DS::std_fclose(s_file);
isPlayingFlag = false;
for (int r = 0; r < BUFFER_SIZE; r++) {
audioBuffer[r] = 0;
}
for (int r = 0; r < waveHeader.fmtExtra; r++) {
decompressionBuffer[r] = 0;
}
// DS::stopSound(1);
// free(audioBuffer);
// free(decompressionBuffer);
DC_FlushAll();
}
bool trackExists(int num) {
Common::String path;
char fname[128];
FILE *file;
sprintf(fname, "track%d.wav", num);
path = ConfMan.get("path");
if (path.lastChar() != '/')
path += '/';
path += fname;
consolePrintf("Looking for %s...", path.c_str());
file = DS::std_fopen(path.c_str(), "r");
if (file) {
consolePrintf("Success!\n");
setActive(true);
DS::std_fclose(file);
return true;
}
sprintf(fname, "track%02d.wav", num);
path = ConfMan.get("path");
if (path.lastChar() != '/')
path += '/';
path += fname;
consolePrintf("Looking for %s...", path.c_str());
file = DS::std_fopen(path.c_str(), "r");
if (file) {
consolePrintf("Success!\n");
setActive(true);
DS::std_fclose(file);
return true;
}
setActive(false);
consolePrintf("Failed!\n");
return false;
}
bool checkCD() {
// Need to check whethe CD audio files are present - do this by trying to open Track1.wav.
consolePrintf("Attempted to open cd drive\n");
if (trackExists(1)) {
trackStartsAt2 = false;
return true;
} else if (trackExists(2)) {
trackStartsAt2 = true;
return true;
} else {
return false;
}
}
bool isPlaying() {
return isPlayingFlag;
}
}
} // End of namespace DS
|