Debian Codegraph @

src/cdogs/cwolfmap/audio.c

raw symbols SCIP
1 #include "audio.h"
2
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 #include "audio_bs6.h"
7 #include "audio_n3d.h"
8 #include "audio_sod.h"
9 #include "audio_wl1.h"
10 #include "audio_wl6.h"
11 #include "mame/fmopl.h"
12
13 #define PATH_MAX 4096
14 static int volume = 20;
15 static const int oplChip = 0;
16 #define OPL_CHANNELS 9
17 #define MUSIC_RATE 700
18 #define SOUND_RATE 140 // Also affects PC Speaker sounds
19 #define SOUND_TICKS (MUSIC_RATE / SOUND_RATE)
20 #define SAMPLES_PER_MUSIC_TICK (MUSIC_SAMPLE_RATE / MUSIC_RATE)
21
22 #pragma pack(push, 1)
23 typedef struct
24 {
25 uint8_t mChar, cChar, mScale, cScale, mAttack, cAttack, mSus, cSus, mWave,
26 cWave, nConn,
27
28 // These are only for Muse - these bytes are really unused
29 voice, mode;
30 uint8_t unused[3];
31 } AlInstrument;
32
33 typedef struct
34 {
35 uint32_t length;
36 uint16_t priority;
37 AlInstrument inst;
38 uint8_t block;
39 uint8_t data[1];
40 } AdLibSound;
41 #pragma pack(pop)
42
43 static const AlInstrument ChannelRelease = {
44 0, 0, 0x3F, 0x3F, 0xFF, 0xFF, 0xF, 0xF, 0, 0, 0,
45
46 0, 0, {0, 0, 0}};
47
48 #define alOut(n, b) YM3812Write(oplChip, n, b, &volume)
49
50 // Register addresses
51 // Operator stuff
52 #define alChar 0x20
53 #define alScale 0x40
54 #define alAttack 0x60
55 #define alSus 0x80
56 #define alWave 0xe0
57 // Channel stuff
58 #define alFreqL 0xa0
59 #define alFreqH 0xb0
60 #define alFeedCon 0xc0
61 // Global stuff
62 #define alEffects 0xbd
63
64 static void AlSetChanInst(const AlInstrument *inst, unsigned int chan)
65 {
66 static const uint8_t chanOps[OPL_CHANNELS] = {0, 1, 2, 8, 9,
67 0xA, 0x10, 0x11, 0x12};
68 uint8_t c, m;
69
70 m = chanOps[chan]; // modulator cell for channel
71 c = m + 3; // carrier cell for channel
72 alOut(m + alChar, inst->mChar);
73 alOut(m + alScale, inst->mScale);
74 alOut(m + alAttack, inst->mAttack);
75 alOut(m + alSus, inst->mSus);
76 alOut(m + alWave, inst->mWave);
77 alOut(c + alChar, inst->cChar);
78 alOut(c + alScale, inst->cScale);
79 alOut(c + alAttack, inst->cAttack);
80 alOut(c + alSus, inst->cSus);
81 alOut(c + alWave, inst->cWave);
82
83 alOut(chan + alFreqL, 0);
84 alOut(chan + alFreqH, 0);
85 alOut(chan + alFeedCon, 0);
86 }
87
88 bool CWAudioInit(void)
89 {
90 // Init adlib
91 if (YM3812Init(1, 3579545, MUSIC_SAMPLE_RATE))
92 {
93 fprintf(stderr, "Unable to create virtual OPL\n");
94 return false;
95 }
96 return true;
97 }
98 void CWAudioTerminate(void)
99 {
100 YM3812Shutdown();
101 }
102
103 int CWAudioLoadHead(CWAudioHead *head, const char *path)
104 {
105 int err = 0;
106 FILE *f = fopen(path, "rb");
107 if (!f)
108 {
109 err = -1;
110 fprintf(stderr, "Failed to read %s\n", path);
111 goto bail;
112 }
113
114 CWAudioHeadFree(head);
115
116 fseek(f, 0, SEEK_END);
117 const long fsize = ftell(f);
118 fseek(f, 0, SEEK_SET);
119 head->nOffsets = fsize / sizeof(uint32_t);
120 head->offsets = malloc(head->nOffsets * sizeof(uint32_t));
121 if (fread(head->offsets, sizeof(uint32_t), head->nOffsets, f) !=
122 head->nOffsets)
123 {
124 err = -1;
125 fprintf(stderr, "Failed to read audio head\n");
126 goto bail;
127 }
128
129 for (int i = 1; i < 0xf6; i++)
130 {
131 YM3812Write(oplChip, i, 0, &volume);
132 }
133 YM3812Write(oplChip, 1, 0x20, &volume); // Set WSE=1
134
135 bail:
136 if (f)
137 {
138 fclose(f);
139 }
140 return err;
141 }
142
143 void CWAudioHeadFree(CWAudioHead *head)
144 {
145 free(head->offsets);
146 head->nOffsets = 0;
147 }
148
149 int CWAudioLoadAudioT(CWAudio *audio, const CWMapType type, const char *path)
150 {
151 int err = 0;
152 FILE *f = fopen(path, "rb");
153 if (!f)
154 {
155 err = -1;
156 fprintf(stderr, "Failed to read %s\n", path);
157 goto bail;
158 }
159
160 free(audio->data);
161
162 const uint32_t len = audio->head.offsets[audio->head.nOffsets - 1];
163 audio->data = malloc(len);
164 if (fread(audio->data, 1, len, f) != len)
165 {
166 err = -1;
167 fprintf(stderr, "Failed to read audio data\n");
168 goto bail;
169 }
170 switch (type)
171 {
172 case CWMAPTYPE_WL1:
173 CWAudioWL1LoadAudioT(audio);
174 break;
175 case CWMAPTYPE_WL6:
176 CWAudioWL6LoadAudioT(audio);
177 break;
178 case CWMAPTYPE_SOD:
179 CWAudioSODLoadAudioT(audio);
180 break;
181 case CWMAPTYPE_BS1:
182 case CWMAPTYPE_BS6:
183 CWAudioBS6LoadAudioT(audio);
184 break;
185 case CWMAPTYPE_N3D:
186 CWAudioN3DLoadAudioT(audio);
187 break;
188 default:
189 fprintf(stderr, "Unknown map type\n");
190 err = -1;
191 goto bail;
192 }
193
194 bail:
195 if (f)
196 {
197 fclose(f);
198 }
199 return err;
200 }
201
202 void CWAudioFree(CWAudio *audio)
203 {
204 CWAudioHeadFree(&audio->head);
205 free(audio->data);
206 WAD_Close(audio->wad);
207 }
208
209 int CWAudioGetAdlibSoundRaw(
210 const CWAudio *audio, const int i, const char **data, size_t *len)
211 {
212 int err = 0;
213 const int off = audio->head.offsets[i + audio->startAdlibSounds];
214 *len = audio->head.offsets[i + audio->startAdlibSounds + 1] - off;
215 if (*len == 0)
216 {
217 fprintf(stderr, "No audio len for track %d\n", i);
218 err = -1;
219 goto bail;
220 }
221 *data = &audio->data[off];
222
223 bail:
224 return err;
225 }
226
227 int CWAudioGetAdlibSound(
228 const CWAudio *audio, const int idx, char **data, size_t *len)
229 {
230 *data = NULL;
231 *len = 0;
232 const char *rawData;
233 size_t rawLen;
234 int err = CWAudioGetAdlibSoundRaw(audio, idx, &rawData, &rawLen);
235 if (err != 0)
236 {
237 goto bail;
238 }
239
240 const AdLibSound *sound = (const AdLibSound *)rawData;
241 const uint8_t alBlock = ((sound->block & 7) << 2) | 0x20;
242 AlSetChanInst(&sound->inst, 0);
243
244 const uint8_t *alSound = sound->data;
245 *len = sound->length * SAMPLES_PER_MUSIC_TICK * SOUND_TICKS *
246 MUSIC_AUDIO_CHANNELS * 2;
247 *data = malloc(*len);
248 int16_t *stream16 = (int16_t *)*data;
249 for (int alLengthLeft = (int)sound->length; alLengthLeft > 0;
250 alLengthLeft--)
251 {
252 // THIS is the way the original Wolfenstein 3-D code handled it!
253 if (*alSound)
254 {
255 alOut(alFreqL, *alSound);
256 alOut(alFreqH, alBlock);
257 }
258 else
259 alOut(alFreqH, 0);
260 alSound++;
261
262 for (int i = 0; i < SOUND_TICKS; i++)
263 {
264 YM3812UpdateOne(oplChip, stream16, SAMPLES_PER_MUSIC_TICK);
265 stream16 += SAMPLES_PER_MUSIC_TICK * MUSIC_AUDIO_CHANNELS;
266 }
267 }
268 alOut(alFreqH, 0);
269
270 return err;
271
272 bail:
273 if (err != 0)
274 {
275 free(*data);
276 *data = NULL;
277 }
278 return err;
279 }
280
281 int CWAudioGetMusicRaw(
282 const CWAudio *audio, const int i, const char **data, size_t *len)
283 {
284 int err = 0;
285 const int off = audio->head.offsets[i + audio->startMusic];
286 *len = audio->head.offsets[i + audio->startMusic + 1] - off;
287 if (*len == 0)
288 {
289 fprintf(stderr, "No music len for track %d\n", i);
290 err = -1;
291 goto bail;
292 }
293 if (*len == 88)
294 {
295 *len = 0;
296 goto bail;
297 }
298 *data = &audio->data[off];
299
300 bail:
301 return err;
302 }
303
304 int CWAudioGetMusic(
305 CWAudio *audio, const CWMapType type, const int idx, char **data,
306 size_t *len)
307 {
308 *data = NULL;
309 *len = 0;
310 int err = 0;
311
312 if (type == CWMAPTYPE_N3D)
313 {
314 wadentry_t *entry = WAD_GetEntry(audio->wad, idx);
315 *data = malloc(entry->length);
316 *len = entry->length;
317 err = WAD_GetEntryData(audio->wad, entry, (unsigned char *)*data);
318 }
319 else
320 {
321 const char *rawData;
322 size_t rawLen;
323 err = CWAudioGetMusicRaw(audio, idx, &rawData, &rawLen);
324 if (err != 0)
325 {
326 goto bail;
327 }
328 if (rawLen == 0)
329 {
330 goto bail;
331 }
332
333 for (int i = 0; i < OPL_CHANNELS; i++)
334 {
335 AlSetChanInst(&ChannelRelease, i);
336 }
337
338 // Measure length of music
339 const uint16_t *sqHack = (const uint16_t *)rawData;
340 int sqHackLen;
341 if (*sqHack == 0)
342 {
343 // LumpLength?
344 sqHackLen = (int)rawLen;
345 }
346 else
347 {
348 sqHackLen = *sqHack++;
349 }
350 const uint16_t *sqHackPtr = sqHack;
351
352 int sqHackTime = 0;
353 int alTimeCount;
354 for (alTimeCount = 0; sqHackLen > 0; alTimeCount++)
355 {
356 do
357 {
358 if (sqHackTime > alTimeCount)
359 break;
360 sqHackTime = alTimeCount + *(sqHackPtr + 1);
361 sqHackPtr += 2;
362 sqHackLen -= 4;
363 } while (sqHackLen > 0);
364 }
365
366 // Decode music
367 // 2 bytes per sample (16-bit audio fmt)
368 *len = alTimeCount * SAMPLES_PER_MUSIC_TICK * MUSIC_AUDIO_CHANNELS * 2;
369 *data = malloc(*len);
370 int16_t *stream16 = (int16_t *)*data;
371
372 sqHack = (const uint16_t *)rawData;
373 if (*sqHack == 0)
374 {
375 // LumpLength?
376 sqHackLen = (int)rawLen;
377 }
378 else
379 {
380 sqHackLen = *sqHack++;
381 }
382 sqHackPtr = sqHack;
383 sqHackTime = 0;
384 for (alTimeCount = 0; sqHackLen > 0; alTimeCount++)
385 {
386 do
387 {
388 if (sqHackTime > alTimeCount)
389 break;
390 sqHackTime = alTimeCount + *(sqHackPtr + 1);
391 alOut(
392 *(const uint8_t *)sqHackPtr,
393 *(((const uint8_t *)sqHackPtr) + 1));
394 sqHackPtr += 2;
395 sqHackLen -= 4;
396 } while (sqHackLen > 0);
397
398 YM3812UpdateOne(oplChip, stream16, SAMPLES_PER_MUSIC_TICK);
399
400 stream16 += SAMPLES_PER_MUSIC_TICK * MUSIC_AUDIO_CHANNELS;
401 }
402 }
403
404 return err;
405
406 bail:
407 if (err != 0)
408 {
409 free(*data);
410 *data = NULL;
411 }
412 return err;
413 }
414
415 int CWAudioGetLevelMusic(const CWMapType type, const int level)
416 {
417 switch (type)
418 {
419 case CWMAPTYPE_WL1:
420 case CWMAPTYPE_WL6:
421 return CWAudioWL6GetLevelMusic(level);
422 case CWMAPTYPE_SOD:
423 return CWAudioSODGetLevelMusic(level);
424 case CWMAPTYPE_N3D:
425 return CWAudioN3DGetLevelMusic(level);
426 default:
427 return -1;
428 }
429 }
430
431 int CWAudioGetSong(const CWMapType type, const CWSongType song)
432 {
433 switch (type)
434 {
435 case CWMAPTYPE_WL1:
436 case CWMAPTYPE_WL6:
437 return CWAudioWL6GetSong(song);
438 case CWMAPTYPE_SOD:
439 return CWAudioSODGetSong(song);
440 case CWMAPTYPE_N3D:
441 return CWAudioN3DGetSong(song);
442 default:
443 return -1;
444 }
445 }