forked from keendreams/keen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
id_sd.c
461 lines (402 loc) · 10.2 KB
/
id_sd.c
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
/*
* Keen Dreams SDL2 / Steam port
* Copyright (C) 2015 David Gow <[email protected]>
*
* 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.
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <SDL2/SDL.h>
#include "id_heads.h"
#define SDL_SoundFinished() {SoundNumber = (word)0; SoundPriority = 0;}
#define PC_PIT_RATE 1193182
#define SD_SFX_PART_RATE 140
/* In the original exe, upon setting a rate of 140Hz or 560Hz for some
* interrupt handler, the value 1192030 divided by the desired rate is
* calculated, to be programmed for timer 0 as a consequence.
* For THIS value, it is rather 1193182 that should be divided by it, in order
* to obtain a better approximation of the actual rate.
*/
#define SD_SOUND_PART_RATE_BASE 1192030
// Global variables (TODO more to add)
boolean AdLibPresent, NeedsMusic;
SDMode SoundMode;
SMMode MusicMode;
uint16_t SoundPriority, DigiPriority;
// Internal variables (TODO more to add)
static boolean SD_Started;
word SoundNumber,DigiNumber;
uint8_t **SoundTable;
// PC Sound variables
uint8_t pcLastSample, *pcSound;
uint32_t pcLengthLeft;
uint16_t pcSoundLookup[255];
boolean quiet_sfx;
// WARNING: These vars refer to the libSDL library!!!
SDL_AudioSpec SD_SDL_AudioSpec;
static boolean SD_SDL_AudioSubsystem_Up;
static uint32_t SD_SDL_SampleOffsetInSound, SD_SDL_SamplesPerPart/*, SD_SDL_MusSamplesPerPart*/;
static int SD_CurrentSoundChunk;
// Timing stuff from Omnispeak (thanks NY00123).
longword TimeCount; // Global time in ticks
uint16_t SpriteSync = 0;
// PIT timer divisor, scaled (bt 8 if music is on, 2 otherwise)
int16_t ScaledTimerDivisor;
// A few variables used for timing measurements (PC_PIT_RATE units per second)
uint64_t SD_LastPITTickTime;
uint32_t SD_GetTimeCount(void)
{
// FIXME: What happens when SDL_GetTicks() reaches the upper bound?
// May be challenging to fix... A proper solution should
// only work with *differences between SDL_GetTicks values*.
uint64_t currPitTicks = (uint64_t)(SDL_GetTicks()) * SD_SOUND_PART_RATE_BASE / 1000;
uint32_t ticksToAdd = (currPitTicks - SD_LastPITTickTime) / ScaledTimerDivisor;
SD_LastPITTickTime += ticksToAdd * ScaledTimerDivisor;
TimeCount += ticksToAdd;
return TimeCount;
}
void SD_SetTimeCount(uint32_t newval)
{
TimeCount = newval;
}
int32_t SD_GetLastTimeCount(void) { return lasttimecount; }
void SD_SetLastTimeCount(int32_t newval) { lasttimecount = newval; }
uint16_t SD_GetSpriteSync(void) { return SpriteSync; }
void SD_SetSpriteSync(uint16_t newval) { SpriteSync = newval; }
void SDL_t0Service(void);
int16_t soundPrev = 0, soundDiff = 0;
static SDL_mutex *sd_mutex;
// Custom DPCM decoder (cut filesize in half)
void SD_SDL_DecodeSound(int16_t *dest, int16_t *src, int lenInSamples)
{
while (lenInSamples--)
{
*dest = (*src + soundPrev) + soundDiff;
soundDiff = *dest - soundPrev;
soundPrev = *dest;
src++;
dest++;
}
}
void SD_SDL_CallBack(void *unused, Uint8 *stream, int len)
{
int16_t *currSamplePtr = (int16_t *)stream;
uint32_t currNumOfSamples;
boolean isPartCompleted;
#if SDL_VERSION_ATLEAST(1,3,0)
memset(stream, 0, len);
#endif
SDL_LockMutex(sd_mutex);
if (!SoundPriority)
{
SDL_UnlockMutex(sd_mutex);
return;
}
uint32_t length = SDL_SwapLE32(*(uint32_t *)(SoundTable[SoundNumber])) + 6;
int lenToCopy = SDL_min(len, length - SD_SDL_SampleOffsetInSound);
SD_SDL_DecodeSound(stream, SoundTable[SoundNumber] + SD_SDL_SampleOffsetInSound, lenToCopy/2);
SD_SDL_SampleOffsetInSound += lenToCopy;
if (SD_SDL_SampleOffsetInSound >= length)
{
SoundNumber = 0;
SoundPriority = 0;
}
SDL_UnlockMutex(sd_mutex);
}
void SDL_SetTimer0(int16_t int_8_divisor)
{
SD_SDL_SamplesPerPart = (int32_t)int_8_divisor * SD_SDL_AudioSpec.freq / PC_PIT_RATE;
ScaledTimerDivisor = int_8_divisor;
}
void SDL_SetIntsPerSecond(int16_t tickrate)
{
SDL_SetTimer0(((int32_t)SD_SOUND_PART_RATE_BASE / (int32_t)tickrate) & 0xFFFF);
}
/* NEVER call this from the callback!!! */
static boolean SDL_DetectAdlib(boolean assumepresence)
{
return true;
}
void SDL_ShutDevice(void)
{
SoundMode = sdm_Off;
}
void SDL_CleanDevice(void)
{
}
void SDL_StartDevice(void)
{
SoundNumber = (word)0; SoundPriority = 0;
}
void SDL_SetTimerSpeed(void)
{
int16_t scaleFactor = (MusicMode == smm_AdLib) ? 4 : 1;
SDL_SetIntsPerSecond(SD_SFX_PART_RATE * scaleFactor);
ScaledTimerDivisor *= (2*scaleFactor);
}
void SD_StopSound(void);
boolean SD_SetSoundMode(SDMode mode)
{
boolean any_sound; // FIXME: Should be set to false here?
int16_t offset; // FIXME: Should be set to 0 here?
SD_StopSound();
switch (mode)
{
case sdm_Off:
any_sound = true;
break;
case sdm_PC:
offset = 0;
any_sound = true;
break;
case sdm_AdLib:
if (!AdLibPresent)
{
break;
}
offset = NUMSOUNDS;
any_sound = true;
break;
default:
any_sound = false;
}
if (any_sound && (mode != SoundMode))
{
SDL_ShutDevice();
SoundMode = mode;
/* TODO: Is that useful? */
SoundTable = audiosegs + offset;
SDL_StartDevice();
}
SDL_SetTimerSpeed();
return any_sound;
}
boolean SD_MusicPlaying(void);
void SD_FadeOutMusic(void);
boolean SD_SetMusicMode(SMMode mode)
{
boolean result; // FIXME: Should be set to false here?
SD_FadeOutMusic();
while (SD_MusicPlaying())
{
// The original code simply waits in a busy loop.
// Bad idea for new code.
// TODO: What about checking for input/graphics/other status?
SDL_Delay(1);
}
switch (mode)
{
case smm_Off:
NeedsMusic = 0;
result = true;
break;
case smm_AdLib:
if (AdLibPresent)
{
NeedsMusic = 1;
result = true;
}
break;
default:
result = false;
}
if (result)
MusicMode = mode;
SDL_SetTimerSpeed();
return result;
}
void SD_Startup(void)
{
/****** TODO: FINISH! ******/
if (SD_Started)
{
return;
}
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0)
{
SD_SDL_AudioSubsystem_Up = false;
}
else
{
SD_SDL_AudioSpec.freq = 44100; // PCM rate
SD_SDL_AudioSpec.format = AUDIO_S16;
SD_SDL_AudioSpec.channels = 1;
// Under wine, small buffer sizes cause a lot of crackling, so we double the
// buffer size. This will result in a tiny amount (~10ms) of extra lag on windows,
// but it's a price I'm prepared to pay to not have my ears explode.
#ifdef _WIN32
SD_SDL_AudioSpec.samples = 1024;
#else
SD_SDL_AudioSpec.samples = 512;
#endif
SD_SDL_AudioSpec.callback = SD_SDL_CallBack;
SD_SDL_AudioSpec.userdata = NULL;
if (SDL_OpenAudio(&SD_SDL_AudioSpec, NULL))
{
SDL_QuitSubSystem(SDL_INIT_AUDIO);
SD_SDL_AudioSubsystem_Up = false;
}
else
{
#if 0
// TODO: This depends on music on/off? (560Hz vs 140Hz for general interrupt handler)
SD_SDL_SamplesPerPart = ((uint64_t)SD_SOUND_PART_RATE_BASE / SD_SFX_PART_RATE) * SD_SDL_AudioSpec.freq / PC_PIT_RATE;
SD_SDL_MusSamplesPerPart = ((uint64_t)SD_SOUND_PART_RATE_BASE / (4*SD_SFX_PART_RATE)) * SD_SDL_AudioSpec.freq / PC_PIT_RATE;
#endif
SD_SDL_AudioSubsystem_Up = true;
}
}
SD_SetTimeCount(0);
SD_SetSoundMode(sdm_Off);
SD_SetMusicMode(smm_Off);
sd_mutex = SDL_CreateMutex();
AdLibPresent = SDL_DetectAdlib(true);
if (SD_SDL_AudioSubsystem_Up)
{
SDL_PauseAudio(0);
}
SD_Started = true;
}
///////////////////////////////////////////////////////////////////////////
//
// SD_Default() - Sets up the default behaviour for the Sound Mgr whether
// the config file was present or not.
//
///////////////////////////////////////////////////////////////////////////
void
SD_Default(boolean gotit,SDMode sd,SMMode sm)
{
boolean gotsd,gotsm;
gotsd = gotsm = gotit;
if (gotsd) // Make sure requested sound hardware is available
{
switch (sd)
{
case sdm_AdLib:
gotsd = AdLibPresent;
break;
}
}
if (!gotsd)
{
if (AdLibPresent)
sd = sdm_AdLib;
else
sd = sdm_PC;
}
if (sd != SoundMode)
SD_SetSoundMode(sd);
if (gotsm) // Make sure requested music hardware is available
{
switch (sm)
{
case sdm_AdLib:
gotsm = AdLibPresent;
break;
}
}
if (!gotsm)
{
if (AdLibPresent)
sm = smm_AdLib;
}
if (sm != MusicMode)
SD_SetMusicMode(sm);
}
void SD_MusicOff(void);
void SD_Shutdown(void)
{
if (!SD_Started)
{
return;
}
if (SD_SDL_AudioSubsystem_Up)
{
SDL_CloseAudio();
SDL_QuitSubSystem(SDL_INIT_AUDIO);
SD_SDL_AudioSubsystem_Up = false;
}
SD_MusicOff();
//SD_StopSound();
SDL_ShutDevice();
SDL_CleanDevice();
SDL_DestroyMutex(sd_mutex);
// Some timer stuff not done here
SD_Started = false;
}
void SD_PlaySound(word sound)
{
uint32_t length;
uint16_t priority;
if (SoundMode == sdm_Off)
return;
if (!SoundTable[sound])
Quit("SD_PlaySound() - Uncached sound");
length = SDL_SwapLE32(*(uint32_t *)(SoundTable[sound]));
if (!length)
Quit("SD_PlaySound() - Zero length sound");
priority = SDL_SwapLE16(*(uint16_t *)(SoundTable[sound] + 4));
if (priority < SoundPriority)
{
return;
}
SDL_LockMutex(sd_mutex);
SD_SDL_SampleOffsetInSound = 6;
SoundNumber = sound;
SoundPriority = priority;
soundPrev = soundDiff = 0;
SDL_UnlockMutex(sd_mutex);
//printf("Playing sound %d, length %d (%d secs), prio %d\n", sound, length, length / (44100*2), priority);
}
uint16_t SD_SoundPlaying(void)
{
return SoundNumber;
}
void SD_StopSound(void)
{
if (!SD_Started)
return;
SDL_LockMutex(sd_mutex);
SDL_SoundFinished();
SDL_UnlockMutex(sd_mutex);
}
void SD_WaitSoundDone(void)
{
while (SoundPriority)
{
// The original code simply waits in a busy loop.
// Bad idea for new code.
// TODO: What about checking for input?
VW_GL_Present();
}
}
void SD_MusicOn(void)
{
}
/* NEVER call this from the callback!!! */
void SD_MusicOff(void)
{
}
void SD_StartMusic(MusicGroup *music)
{
}
void SD_FadeOutMusic(void)
{
}
boolean SD_MusicPlaying(void)
{
return false; // All it really does...
}