Wing Commander II
Reconstructed Win32 source (1996)
Loading...
Searching...
No Matches
sound.c
Go to the documentation of this file.
1/*
2 * Wave playback, sound bookkeeping and volume settings.
3 *
4 * Retail object 0x423CA0-0x424A5F (link order; see docs/ORDER.md).
5 */
6#include "game.h"
7
8ActiveSoundEntry *g_pActiveSoundHead = 0; /* 0x004961a8 */
9ActiveSoundEntry *g_pActiveSoundTail = 0; /* 0x004961ac */
10int g_bAudioSystemInitialized = 0; /* 0x004961b0 */
11WaveTableEntry *g_pWaveTableHead = 0; /* 0x004961b4 */
12WaveTableEntry *g_pWaveTableTail = 0; /* 0x004961b8 */
13
14/* Uninitialized C++ definitions: this unit's .bss, which LINK orders by the
15 * identifiers' hash keys (the spellings follow the original addresses). */
16WaveTableEntry *g_1_pVoiceWav; /* 0x004a2650 */
18IxSound *g_pSpeechSnd; /* 0x004a2658 */
19int g_16_nSpeechCompletionDelay; /* 0x004a265c */
20int g_bSpeechSoundActive; /* 0x004a2660 */
21IxSound *g_9_pSnowStaticAudio; /* 0x004a2664 */
22int g_9_nNextSfx16PlaybackFrame; /* 0x004a2668 */
23int g_bSpeechPlaybackDone; /* 0x004a266c */
24
25/* Function start: 0x423CA0 */
26void InitializeAudioSystem(HWND window)
27{
28 if (g_nAudioEnabled == 0)
29 return;
31 ix_system_configure(3, (void *)1);
32 ix_system_configure(0, window);
36 }
37}
38
39/* Function start: 0x423D02 */
53
54/* Function start: 0x423D4F */
69
70#pragma function(strcmp)
71/* Function start: 0x423DBB */
73{
75
76 while (entry != 0) {
77 if (strcmp(entry->name, name) == 0)
78 return entry;
79 entry = entry->next;
80 }
81 return 0;
82}
83
84/* Sound-object callees from the ix library. */
85int __fastcall ix_sound_is_playing(IxSound *sound);
87
88/* Function start: 0x423E12 */
90{
91 WaveTableEntry *entry;
92 WaveTableEntry *prev;
93
94 entry = g_pWaveTableHead;
95 prev = 0;
96 while (entry != 0 && entry != target) {
97 prev = entry;
98 entry = entry->next;
99 }
100 if (entry != 0) {
101 if (prev != 0)
102 prev->next = entry->next;
103 if (entry == g_pWaveTableTail && prev != 0)
104 g_pWaveTableTail = prev;
105 if (entry == g_pWaveTableHead)
106 g_pWaveTableHead = entry->next;
107 free(entry->name);
108 free(entry);
109 }
110 return;
111}
112
113/* Function start: 0x423ED1 */
115{
116 WaveTableEntry *entry;
117 WaveTableEntry *next;
118
119 entry = g_pWaveTableHead;
120 next = 0;
121 while (entry != 0) {
122 next = entry->next;
123 free(entry->name);
124 free(entry);
125 entry = next;
126 }
128 return;
129}
130
131/* Function start: 0x423F3F */
146
147/* Function start: 0x423FAB */
149{
151 ActiveSoundEntry *prev = 0;
152
153 while (entry != 0 && entry != target) {
154 prev = entry;
155 entry = entry->next;
156 }
157 if (entry != 0) {
158 if (prev != 0)
159 prev->next = entry->next;
160 if (g_pActiveSoundTail == entry && prev != 0)
161 g_pActiveSoundTail = prev;
162 if (g_pActiveSoundHead == entry)
163 g_pActiveSoundHead = entry->next;
164 free(entry);
165 }
166}
167
168/* Function start: 0x42405E */
170{
171 ActiveSoundEntry *entry;
172
173 entry = g_pActiveSoundHead;
174 while (entry != 0) {
175 if (ix_sound_get_sample(entry->sound) == sample)
176 return entry;
177 entry = entry->next;
178 }
179 return 0;
180}
181
182/* Function start: 0x4240AE */
184{
186
187 while (entry != 0) {
188 ActiveSoundEntry *next = entry->next;
189
190 if (ix_sound_is_playing(entry->sound) == 0) {
193 }
194 entry = next;
195 }
196}
197
198/* Function start: 0x424113 */
199void StopSoundsUsingWave(const char *name)
200{
201 WaveTableEntry *waveEntry;
202 ActiveSoundEntry *entry;
203
204 if (g_nAudioEnabled == 0)
205 return;
206 waveEntry = FindWaveTableEntryByName(name);
207 if (waveEntry != 0) {
208 while ((entry = FindActiveSoundEntryBySample(waveEntry->sample)) != 0) {
211 }
212 }
213}
214
215#pragma function(memcmp, strcpy, strlen)
216/* Function start: 0x42418C */
217void playWAVE(const char *filename, int looping, int volume)
218{
219 char errorMsg[100];
220 long size;
221 IxSound *voice;
222 int fd;
223 unsigned char *fileData;
224 WaveTableEntry *wave;
225
226 if (memcmp(filename, "sfx16.wav", 9) == 0) {
227 /* This unit reads the frame counter as a dword. */
228 if (*(int *)&g_nSpaceFrame >
231 *(int *)&g_nSpaceFrame + 100;
232 else
233 return;
234 }
235 if (g_nAudioEnabled == 0)
236 return;
237 volume /= 2;
239 SoundDebugPrintf("%s\n", filename);
240 wave = FindWaveTableEntryByName(filename);
241 if (wave != 0) {
242 if (looping != 0) {
243 ActiveSoundEntry *active;
244
245 active = AllocateActiveSoundEntry();
246 active->sound = ix_system_new_sound(wave->sample);
247 active->sound->ix_system_sound_set_volume(volume);
248 ix_sound_start(active->sound);
249 } else {
250 voice = ix_system_new_sound(wave->sample);
252 voice->ix_system_sound_set_volume(volume);
253 ix_sound_start(voice);
254 }
255 } else {
256 fd = _open(filename, 0x8000);
257 if (fd == -1) {
258 sprintf(errorMsg, "playWAVE Unable to open file '%s'", filename);
259 MessageBoxA(0, errorMsg, "Error", MB_ICONHAND);
260 exit(1);
261 }
262 size = filelength(fd);
263 fileData = (unsigned char *)malloc((unsigned int)size);
264 _read(fd, fileData, (unsigned int)size);
265 _close(fd);
266
267 wave = AllocateWaveTableEntry();
269 wave->sample->ix_sample_load_wav(fileData, size);
270 if (looping != 0) {
271 ActiveSoundEntry *active;
272
274 active = AllocateActiveSoundEntry();
275 active->sound = ix_system_new_sound(wave->sample);
276 active->sound->ix_system_sound_set_volume(volume);
277 ix_sound_start(active->sound);
278 } else {
279 voice = ix_system_new_sound(wave->sample);
281 voice->ix_system_sound_set_volume(volume);
282 ix_sound_start(voice);
283 }
284 wave->name = (char *)malloc(strlen(filename) + 1);
285 strcpy(wave->name, filename);
286 free(fileData);
287 }
288 return;
289}
290
291/* Function start: 0x424417 */
292void PlayRawSpeechBuffer(void *buffer, size_t size, int interrupt)
293{
294 int index;
295
296 if (buffer == 0)
297 return;
298 for (index = 0; index < (int)size; index++) {
299 ((unsigned char *)buffer)[index] =
300 (unsigned char)(((unsigned char *)buffer)[index] + 0x80);
301 }
303 "Playing RAW with length of %d stop=%d\n", size, interrupt);
304 if (g_pSpeechSnd != 0 &&
306 if (interrupt != 0) {
309 } else
310 return;
311 }
315 buffer, size, 0x29ab, 8, 1);
322 (char *)malloc(strlen("RAW") + 1);
323 strcpy(g_1_pVoiceWav->name, "RAW");
326 if (g_1_bSpaceFlightActive == 0)
328}
329
330/* Function start: 0x4245A2 */
332{
336 if (g_9_pSnowStaticAudio != 0) {
337 /* The bulk delete already stops, unlinks, and frees every IxSound.
338 The original's following calls therefore use a stale pointer. */
343 }
344}
345
346/* Function start: 0x4245F8 */
348{
349 unsigned char *fileData;
350 long size;
351 int file;
352
353 if (g_nAudioEnabled == 0)
354 return;
356 if (g_9_pSnowStaticAudio == 0) {
357 file = _open("sfx22.wav", 0x8000);
358 if (file == -1) {
359 MessageBoxA(0,
360 "playWAVE Unable to open file 'sfx23",
361 "Notice", MB_ICONHAND);
362 exit(1);
363 }
364 size = filelength(file);
365 fileData = (unsigned char *)malloc((unsigned int)size);
366 _read(file, fileData, (unsigned int)size);
367 _close(file);
368
372 fileData, size);
377
379 (char *)malloc(strlen("SNOW") + 1);
380 strcpy(g_pSnowStaticClip->name, "SNOW");
381 free(fileData);
382 } else if (ix_sound_is_playing(
383 g_9_pSnowStaticAudio) == 0) {
385 }
386}
387
388/* Function start: 0x42476B */
390{
391 if (g_nAudioEnabled == 0)
392 return;
394 if (g_pSpeechSnd != 0 &&
398 if (g_1_bSpaceFlightActive != 0) {
401 }
403 if (g_1_bSpaceFlightActive == 0)
406 if (g_1_bSpaceFlightActive == 0)
408 }
409 }
410}
411
412/* Function start: 0x42482E */
413void SetSoundEffectsVolume(int volume)
414{
415 if (g_nAudioEnabled == 0)
416 return;
417 ix_system_set_master_volume((unsigned short)volume);
418}
419
420/* Function start: 0x42485C */
421LONG RegistryQueryValue(HKEY key, LPCSTR name, DWORD type,
422 LPBYTE data, DWORD size)
423{
424 return RegQueryValueExA(key, name, 0, &type, data, &size);
425}
426
427/* Function start: 0x424888 */
428void RegistryStoreValue(HKEY key, LPCSTR name, DWORD type,
429 const BYTE *data, DWORD size)
430{
431 RegSetValueExA(key, name, 0, type, data, size);
432}
433
434/* One copy of each registry value name serves both the load and the save:
435 * the original has a single address for each. */
436static char g_szMusicVolumeValueName[] = "MusicVolume"; /* 0x004961c0 */
437static char g_szSfxVolumeValueName[] = "SFXVolume"; /* 0x004961d0 */
438
439/* Function start: 0x4248B4 */
441{
442 HKEY key;
443
444 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,
445 "Software\\Origin Systems\\WC: Kilrathi Saga",
446 0, KEY_ALL_ACCESS, &key) == ERROR_SUCCESS) {
448 (LPBYTE)&g_nMusicVolumeSetting,
449 sizeof(g_nMusicVolumeSetting)) !=
450 ERROR_SUCCESS) {
453 (const BYTE *)&g_nMusicVolumeSetting,
454 sizeof(g_nMusicVolumeSetting));
455 }
456 if (RegistryQueryValue(key, g_szSfxVolumeValueName, REG_DWORD,
457 (LPBYTE)&g_nSfxVolumeSetting,
458 sizeof(g_nSfxVolumeSetting)) !=
459 ERROR_SUCCESS) {
460 g_nSfxVolumeSetting = 0x14;
462 (const BYTE *)&g_nSfxVolumeSetting,
463 sizeof(g_nSfxVolumeSetting));
464 }
465 }
466 RegCloseKey(key);
467 return;
468}
469
470/* Function start: 0x424980 */
472{
473 HKEY key;
474
475 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,
476 "Software\\Origin Systems\\WC: Kilrathi Saga",
477 0, KEY_ALL_ACCESS, &key) == ERROR_SUCCESS) {
479 (const BYTE *)&g_nMusicVolumeSetting,
480 sizeof(g_nMusicVolumeSetting));
482 (const BYTE *)&g_nSfxVolumeSetting,
483 sizeof(g_nSfxVolumeSetting));
484 }
485 RegCloseKey(key);
486}
487
488/* Function start: 0x424A00 */
490{
491 if (enabled != 0)
492 flags |= 2;
493 else
494 flags &= ~2U;
495}
496
497/* The inline accessor called by FindActiveSoundEntryBySample. Its declaration
498 * makes the debug compiler emit a separate COMDAT, matching the aligned
499 * out-of-line copy after this unit's main code. */
500/* Function start: 0x424A40 */
502{
503 return sound->sample;
504}
int ix_system_init(void)
Definition system.cpp:25
void ix_system_set_master_volume(unsigned short volume)
Definition system.cpp:116
void ix_system_delete_all_samples(void)
Definition system.cpp:159
void LoadVolumeSettingsFromRegistry(void)
Definition sound.c:440
void SaveVolumeSettingsToRegistry(void)
Definition sound.c:471
ActiveSoundEntry * AllocateActiveSoundEntry(void)
Definition sound.c:132
void __fastcall ix_sound_stop(IxSound *sound)
Definition sound.cpp:211
void ServiceAudioStream(void)
Definition sound.c:40
void RemoveWaveTableEntry(WaveTableEntry *entry)
Definition sound.c:89
void RemoveActiveSoundEntry(ActiveSoundEntry *entry)
Definition sound.c:148
void PlaySnowStaticSound(void)
Definition sound.c:347
void playWAVE(const char *filename, int looping, int volume)
Definition sound.c:217
IxSample *__fastcall ix_sound_get_sample(IxSound *sound)
Definition sound.c:501
int __fastcall ix_sound_is_playing(IxSound *sound)
Definition sound.cpp:361
WaveTableEntry * AllocateWaveTableEntry(void)
Definition sound.c:55
void SoundDebugPrintf(const char *fmt,...)
Definition mono.c:64
IxSound * ix_system_new_sound(IxSample *sample)
Definition system.cpp:173
WaveTableEntry * FindWaveTableEntryByName(const char *name)
Definition sound.c:72
void ix_system_shutdown(void)
Definition system.cpp:93
void stop_all_sounds(void)
Definition sound.c:331
void ix_system_configure(int option, void *value)
Definition system.cpp:104
ActiveSoundEntry * FindActiveSoundEntryBySample(IxSample *sample)
Definition sound.c:169
void ServiceSoundSystem(void)
Definition sound.c:389
LONG RegistryQueryValue(HKEY key, LPCSTR name, DWORD type, LPBYTE data, DWORD size)
Definition sound.c:421
void InitializeAudioSystem(HWND window)
Definition sound.c:26
void ix_system_set_voice_count(int voiceCount)
Definition system.cpp:128
void PlayRawSpeechBuffer(void *buffer, size_t size, int interrupt)
Definition sound.c:292
void ix_system_delete_all_sounds(void)
Definition system.cpp:197
void ReleaseFinishedSoundEntries(void)
Definition sound.c:183
IxSample * ix_system_new_sample(void)
Definition system.cpp:139
void StopSoundsUsingWave(const char *name)
Definition sound.c:199
void FreeWaveTable(void)
Definition sound.c:114
void __fastcall ix_sound_start(IxSound *sound)
Definition sound.cpp:103
void ix_system_delete_sound(IxSound *sound)
Definition system.cpp:184
void SetCinematicFrameTiming(float frameRate)
Definition dib.cpp:212
void __fastcall ix_sound_release(IxSound *sound)
Definition sound.cpp:189
void SetSoundEffectsVolume(int volume)
Definition sound.c:413
void ix_system_service_sounds(void)
Definition system.cpp:46
void RegistryStoreValue(HKEY key, LPCSTR name, DWORD type, const BYTE *data, DWORD size)
Definition sound.c:428
Shared Win32 declarations, types and library interfaces.
IxSound * g_9_pSnowStaticAudio
Definition sound.c:21
int g_1_bSpaceFlightActive
Definition main.c:2004
int g_bSpeechSoundActive
Definition sound.c:20
WaveTableEntry * g_pSnowStaticClip
Definition sound.c:17
ActiveSoundEntry * g_pActiveSoundHead
Definition sound.c:8
int g_9_nNextSfx16PlaybackFrame
Definition sound.c:22
WaveTableEntry * g_pWaveTableHead
Definition sound.c:11
int g_nInputPressCount
Definition winmain.c:1354
int g_bSpeechPlaybackDone
Definition sound.c:23
WaveTableEntry * g_1_pVoiceWav
Definition sound.c:16
IxSound * g_pSpeechSnd
Definition sound.c:18
short g_nSpaceFrame
Definition viewrect.c:32
ActiveSoundEntry * g_pActiveSoundTail
Definition sound.c:9
int g_nSfxVolumeSetting
Definition main.c:1934
int g_bAudioSystemInitialized
Definition sound.c:10
WaveTableEntry * g_pWaveTableTail
Definition sound.c:12
int g_nMusicVolumeSetting
Definition main.c:1935
int g_16_nSpeechCompletionDelay
Definition sound.c:19
int g_nAudioEnabled
Definition winmain.c:1349
static char g_szMusicVolumeValueName[]
Definition sound.c:436
static char g_szSfxVolumeValueName[]
Definition sound.c:437
struct ActiveSoundEntry * next
Definition game.h:348
IxSound * sound
Definition game.h:347
void ix_sample_set_looping(int enabled)
Definition sound.c:489
unsigned int flags
Definition game.h:128
int ix_sample_load_wav(void *data, int bytes)
Definition sample.cpp:199
int ix_sample_load_raw(void *data, int bytes, int frequency, int bitsPerSample, int channels)
Definition sample.cpp:292
IxSample * sample
Definition game.h:139
void ix_system_sound_set_volume(unsigned short volume)
Definition sound.cpp:69
void ix_sound_set_delete_on_stop(int enabled)
Definition sound.cpp:352
struct WaveTableEntry * next
Definition game.h:342
IxSample * sample
Definition game.h:340
char * name
Definition game.h:338