Wing Commander
Reconstructed Win32 source (1996)
Loading...
Searching...
No Matches
sound.c
Go to the documentation of this file.
1/**
2 * @file
3 * @ingroup audio
4 * @brief Sound-effect samples, playback and saved volume settings.
5 *
6 * Address range 42b160-42b99f; the launch data that follows marks the unit
7 * boundary. Compiled as C++ (/TP), since it calls ix member functions
8 * directly.
9 */
10#include "wc1.h"
11#include <malloc.h>
12
13static ActiveSoundEntry *pActiveSoundHead = 0; /* 0x0046a438 */
14static ActiveSoundEntry *pActiveSoundTail = 0; /* 0x0046a43c */
15static int bAudioSystemInitialized = 0; /* 0x0046a440 */
16static WaveTableEntry *pWaveTableHead = 0; /* 0x0046a444 */
17static WaveTableEntry *pWaveTableTail = 0; /* 0x0046a448 */
18static char szMusicVolumeValueName[] = "MusicVolume"; /* 0x0046a450 */
19static char szSfxVolumeValueName[] = "SFXVolume"; /* 0x0046a460 */
20
21static WaveTableEntry *pSnowStaticWave; /* 0x0047654c */
22static IxSound *pSnowStaticSound; /* 0x00476550 */
23
24/* Function start: 0x42B160 */
25void InitializeAudioSystem(HWND window)
26{
28 ix_system_configure(3, (void *)1);
29 ix_system_configure(0, window);
33 }
34}
35
36/* Function start: 0x42B1B0 */
47
48/* Function start: 0x42B1F0 */
63
64/* Function start: 0x42B240 */
66{
68
69 while (entry != 0) {
70 if (strcmp(entry->name, name) == 0)
71 return entry;
72 entry = entry->next;
73 }
74 return 0;
75}
76
77/* Function start: 0x42B290 */
79{
80 WaveTableEntry *previous = 0;
82
83 if (entry != 0) {
84 WaveTableEntry *searchTarget = target;
85
86 target = entry;
87 while (target != 0 && target != searchTarget) {
88 previous = target;
89 target = target->next;
90 }
91 entry = target;
92 }
93 if (entry != 0) {
94 if (previous != 0)
95 previous->next = entry->next;
96 if (pWaveTableTail == entry && previous != 0)
97 pWaveTableTail = previous;
98 if (pWaveTableHead == entry)
99 pWaveTableHead = entry->next;
100 free(entry->name);
101 free(entry);
102 }
103}
104
105/* Function start: 0x42B300 */
107{
109
110 while (entry != 0) {
111 WaveTableEntry *next = entry->next;
112
113 free(entry->name);
114 free(entry);
115 entry = next;
116 }
117 pWaveTableTail = 0;
118 pWaveTableHead = 0;
119}
120
121/* Function start: 0x42B340 */
136
137/* Function start: 0x42B390 */
139{
141 ActiveSoundEntry *previous = 0;
142
143 if (entry != 0) {
144 ActiveSoundEntry *searchTarget = target;
145
146 target = entry;
147 while (target != 0 && target != searchTarget) {
148 previous = target;
149 target = target->next;
150 }
151 entry = target;
152 }
153 if (entry != 0) {
154 if (previous != 0)
155 previous->next = entry->next;
156 if (pActiveSoundTail == entry && previous != 0)
157 pActiveSoundTail = previous;
158 if (pActiveSoundHead == entry)
159 pActiveSoundHead = entry->next;
160 free(entry);
161 }
162}
163
164/* Function start: 0x42B3F0 */
166{
168
169 for (;;) {
170 if (entry == 0)
171 return 0;
172 if (entry->sound->sample == sample)
173 return entry;
174 entry = entry->next;
175 }
176}
177
178/* Function start: 0x42B410 */
180{
182
183 while (entry != 0) {
184 ActiveSoundEntry *next = entry->next;
185
186 if (ix_sound_is_playing(entry->sound) == 0) {
189 }
190 entry = next;
191 }
192}
193
194/* Function start: 0x42B450 */
195void StopSoundsUsingWave(const char *name)
196{
197 WaveTableEntry *wave;
198 ActiveSoundEntry *entry;
199
200 if (bIxAudioEnabled != 0) {
201 wave = FindWaveTableEntryByName(name);
202 if (wave != 0) {
203 for (;;) {
205 if (entry == 0)
206 break;
209 }
210 }
211 }
212}
213
214/* Function start: 0x42B4A0 */
215void playWAVE(const char *filename, int looping, int volume)
216{
217 unsigned char *fileData;
218 WaveTableEntry *wave;
219 ActiveSoundEntry *active;
220 IxSound *sound;
221 long fileSize;
222 int file;
223
224 if (bIxAudioEnabled != 0) {
226 wave = FindWaveTableEntryByName(filename);
227 if (wave != 0) {
228 if (looping != 0) {
229 active = AllocateActiveSoundEntry();
230 active->sound = ix_system_new_sound(wave->sample);
231 active->sound->ix_system_sound_set_volume(volume);
232 ix_sound_start(active->sound);
233 return;
234 }
235 sound = ix_system_new_sound(wave->sample);
237 sound->ix_system_sound_set_volume(volume);
238 ix_sound_start(sound);
239 return;
240 }
241
242 file = _open(filename, 0x8000);
243 if (file == -1) {
244 MessageBoxA(0, "playWAVE Unable to open file '%s'",
245 filename, MB_ICONHAND);
246 exit(1);
247 }
248 fileSize = filelength(file);
249 fileData = (unsigned char *)malloc((unsigned int)fileSize);
250 _read(file, fileData, (unsigned int)fileSize);
251 _close(file);
252
253 wave = AllocateWaveTableEntry();
255 wave->sample->ix_sample_load_wav(fileData, fileSize);
256 if (looping != 0) {
257 wave->sample->flags |= 2;
258 active = AllocateActiveSoundEntry();
259 active->sound = ix_system_new_sound(wave->sample);
260 active->sound->ix_system_sound_set_volume(volume);
261 ix_sound_start(active->sound);
262 } else {
263 sound = ix_system_new_sound(wave->sample);
265 sound->ix_system_sound_set_volume(volume);
266 ix_sound_start(sound);
267 }
268
269 wave->name = (char *)malloc(strlen(filename) + 1);
270 strcpy(wave->name, filename);
271 free(fileData);
272 }
273}
274
275/* Function start: 0x42B640 */
277{
281 if (pSnowStaticSound != 0) {
282 /* The bulk delete already stops, unlinks, and frees every IxSound.
283 The original's following calls therefore use a stale pointer. */
287 pSnowStaticWave = 0;
288 }
289}
290
291/* Function start: 0x42B680 */
293{
294 unsigned char *fileData;
295 int file;
296 long fileSize;
297
298 if (bIxAudioEnabled != 0) {
300 if (pSnowStaticSound == 0) {
301 file = _open("sfx22.wav", 0x8000);
302 if (file == -1) {
303 MessageBoxA(0,
304 "playWAVE Unable to open file 'sfx23",
305 "Notice", MB_ICONHAND);
306 exit(1);
307 }
308 fileSize = filelength(file);
309 fileData = (unsigned char *)malloc((unsigned int)fileSize);
310 _read(file, fileData, (unsigned int)fileSize);
311 _close(file);
312
316 fileData, fileSize);
321
323 (char *)malloc(strlen("SNOW") + 1);
324 strcpy(pSnowStaticWave->name, "SNOW");
325 free(fileData);
326 } else if (ix_sound_is_playing(
327 pSnowStaticSound) == 0) {
329 }
330 }
331}
332
333/* Function start: 0x42B7D0 */
338
339/* Function start: 0x42B7E0 */
340void SetSoundEffectsVolume(int volume)
341{
342 if (volume >= 0 && volume < 65000)
343 ix_system_set_master_volume((unsigned short)volume);
344 SoundDebugPrintf("Setting SFX Volume to %d\n", volume);
345}
346
347/* Function start: 0x42B810 */
348LONG RegistryQueryValue(HKEY key, LPCSTR name, DWORD type,
349 LPBYTE data, DWORD size)
350{
351 return RegQueryValueExA(key, name, 0, &type, data, &size);
352}
353
354/* Function start: 0x42B840 */
355void RegistryStoreValue(HKEY key, LPCSTR name, DWORD type,
356 const BYTE *data, DWORD size)
357{
358 RegSetValueExA(key, name, 0, type, data, size);
359}
360
361/* Function start: 0x42B870 */
363{
364 HKEY key;
365
366 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,
367 "Software\\Origin Systems\\WC: Kilrathi Saga",
368 0, KEY_ALL_ACCESS, &key) == ERROR_SUCCESS) {
369 if (RegistryQueryValue(key, szMusicVolumeValueName, REG_DWORD,
370 (LPBYTE)&nMusicVolumeSetting,
371 sizeof(nMusicVolumeSetting)) !=
372 ERROR_SUCCESS) {
373 nMusicVolumeSetting = 0x14;
375 (const BYTE *)&nMusicVolumeSetting,
376 sizeof(nMusicVolumeSetting));
377 }
378 if (RegistryQueryValue(key, szSfxVolumeValueName, REG_DWORD,
379 (LPBYTE)&nSfxVolumeSetting,
380 sizeof(nSfxVolumeSetting)) !=
381 ERROR_SUCCESS) {
382 nSfxVolumeSetting = 0x14;
384 (const BYTE *)&nSfxVolumeSetting,
385 sizeof(nSfxVolumeSetting));
386 }
387 }
388 RegCloseKey(key);
389}
390
391/* Function start: 0x42B930 */
393{
394 HKEY key;
395
396 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,
397 "Software\\Origin Systems\\WC: Kilrathi Saga",
398 0, KEY_ALL_ACCESS, &key) == ERROR_SUCCESS) {
400 (const BYTE *)&nMusicVolumeSetting,
401 sizeof(nMusicVolumeSetting));
403 (const BYTE *)&nSfxVolumeSetting,
404 sizeof(nSfxVolumeSetting));
405 }
406 RegCloseKey(key);
407}
int nMusicVolumeSetting
Definition main.c:26
int bIxAudioEnabled
Definition winmain.c:30
int nSfxVolumeSetting
Definition main.c:25
static WaveTableEntry * pWaveTableTail
Definition sound.c:17
static IxSound * pSnowStaticSound
Definition sound.c:22
static int bAudioSystemInitialized
Definition sound.c:15
static char szMusicVolumeValueName[]
Definition sound.c:18
static ActiveSoundEntry * pActiveSoundHead
Definition sound.c:13
static WaveTableEntry * pWaveTableHead
Definition sound.c:16
static ActiveSoundEntry * pActiveSoundTail
Definition sound.c:14
static char szSfxVolumeValueName[]
Definition sound.c:19
static WaveTableEntry * pSnowStaticWave
Definition sound.c:21
struct ActiveSoundEntry * next
Definition wc1base.h:323
IxSound * sound
Definition wc1base.h:322
unsigned int flags
Definition wc1base.h:55
int ix_sample_load_wav(void *data, int bytes)
Definition sample.cpp:199
void ix_system_sound_set_volume(unsigned short newVolume)
Definition sound.cpp:68
IxSample * sample
Definition wc1base.h:63
void ix_sound_set_delete_on_stop(int enabled)
Definition sound.cpp:346
struct WaveTableEntry * next
Definition wc1base.h:317
IxSample * sample
Definition wc1base.h:315
char * name
Definition wc1base.h:313
Umbrella header for the game core.
void SoundDebugPrintf(const char *fmt,...)
Definition mono.c:382
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:362
void SaveVolumeSettingsToRegistry(void)
Definition sound.c:392
ActiveSoundEntry * AllocateActiveSoundEntry(void)
Definition sound.c:122
void __fastcall ix_sound_stop(IxSound *sound)
Definition sound.cpp:209
void ServiceAudioStream(void)
Definition sound.c:37
void RemoveWaveTableEntry(WaveTableEntry *entry)
Definition sound.c:78
void RemoveActiveSoundEntry(ActiveSoundEntry *entry)
Definition sound.c:138
void PlaySnowStaticSound(void)
Definition sound.c:292
void playWAVE(const char *filename, int looping, int volume)
Definition sound.c:215
int __fastcall ix_sound_is_playing(IxSound *sound)
Definition sound.cpp:355
WaveTableEntry * AllocateWaveTableEntry(void)
Definition sound.c:49
IxSound * ix_system_new_sound(IxSample *sample)
Definition system.cpp:173
WaveTableEntry * FindWaveTableEntryByName(const char *name)
Definition sound.c:65
void ix_system_shutdown(void)
Definition system.cpp:93
void stop_all_sounds(void)
Definition sound.c:276
void ix_system_configure(int option, void *value)
Definition system.cpp:104
ActiveSoundEntry * FindActiveSoundEntryBySample(IxSample *sample)
Definition sound.c:165
void ServiceSoundSystem(void)
Definition sound.c:334
LONG RegistryQueryValue(HKEY key, LPCSTR name, DWORD type, LPBYTE data, DWORD size)
Definition sound.c:348
void InitializeAudioSystem(HWND window)
Definition sound.c:25
void ix_system_set_voice_count(int voiceCount)
Definition system.cpp:128
void ix_system_delete_all_sounds(void)
Definition system.cpp:197
void ReleaseFinishedSoundEntries(void)
Definition sound.c:179
IxSample * ix_system_new_sample(void)
Definition system.cpp:139
void StopSoundsUsingWave(const char *name)
Definition sound.c:195
void FreeWaveTable(void)
Definition sound.c:106
void __fastcall ix_sound_start(IxSound *sound)
Definition sound.cpp:102
void ix_system_delete_sound(IxSound *sound)
Definition system.cpp:184
void __fastcall ix_sound_release(IxSound *sound)
Definition sound.cpp:188
void SetSoundEffectsVolume(int volume)
Definition sound.c:340
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:355