Wing Commander
Reconstructed Win32 source (1996)
Loading...
Searching...
No Matches
system.cpp
Go to the documentation of this file.
1/**
2 * @file
3 * @ingroup ixlib
4 * @brief Sound-system state and voice allocation.
5 *
6 * Originally `D:\Rnd\prj\ix\src\sound\system.cpp`, address range
7 * 447200-447a1f. The assert paths identify the source; linker INT3 fill marks
8 * the object boundaries.
9 */
10#include "ix.h"
11#include <assert.h>
12#include <limits.h>
13
14unsigned int dwSoundStateFlags; /* 0x00598608 */
15IxSound *pIdleSoundList; /* 0x0059860C */
16IxSample *pSamples; /* 0x00598610 */
17int nActiveVoices; /* 0x00598614 */
18int nSystemVoices; /* 0x00598618 */
19IxSound *pPlayingSoundList; /* 0x0059861C */
20IxSound *pQueuedSoundList; /* 0x00598620 */
21
22#define IX_SYSTEM_FILE "D:\\Rnd\\prj\\ix\\src\\sound\\system.cpp"
23
24/* Function start: 0x00447200 */ /* source line 33 */
25extern "C" int ix_system_init(void)
26{
27 if ((dwSoundStateFlags & 1) == 0) {
28 if (ix_dsp_init() != 0) {
29 ix_log_printf("Warning [%s - %d]:\n", IX_SYSTEM_FILE, 33);
30 ix_log_printf("Failed to init DSP");
31 return -1;
32 }
33 nActiveVoices = 0;
38 pSamples = 0;
41 }
42 return 0;
43}
44
45/* Function start: 0x004472A7 */
46extern "C" void ix_system_service_sounds(void)
47{
48 if (pQueuedSoundList != 0) {
50 unsigned int now = ix_dsp_get_tick();
51
52 while (sound != 0) {
53 IxSound *next = sound->next;
54
55 if (sound->stopTime != (unsigned int)-1 &&
56 sound->stopTime < now)
57 ix_sound_stop(sound);
58 sound = next;
59 }
60 }
61
62 if (pPlayingSoundList != 0) {
64
65 while (sound != 0) {
66 IxSound *next = sound->next;
67
68 if ((sound->flags & (IX_SOUND_VOLUME_DIRTY |
70 IX_SOUND_PAN_DIRTY)) != 0) {
71 unsigned int flags = sound->flags;
72 int voice = sound->voice;
73
74 sound->flags &= ~(IX_SOUND_VOLUME_DIRTY |
77 if ((flags & IX_SOUND_VOLUME_DIRTY) != 0)
78 ix_dspv_set_volume(voice, sound->volume);
79 if ((flags & IX_SOUND_PAN_DIRTY) != 0)
80 ix_dspv_set_pan(voice, sound->pan);
81 if ((flags & IX_SOUND_FREQUENCY_DIRTY) != 0)
83 voice, sound->sample->frequency + sound->pitchOffset);
84 }
85 if ((ix_dspv_get_flags(sound->voice) & IX_VOICE_ACTIVE) == 0)
86 ix_sound_stop(sound);
87 sound = next;
88 }
89 }
90}
91
92/* Function start: 0x004473F3 */
93extern "C" void ix_system_shutdown(void)
94{
95 if ((dwSoundStateFlags & 1) != 0) {
99 dwSoundStateFlags &= ~1U;
100 }
101}
102
103/* Function start: 0x00447426 */
104extern "C" void ix_system_configure(int option, void *value)
105{
106 ix_dsp_configure(option, value);
107}
108
109/* Function start: 0x00447446 */
110extern "C" unsigned short ix_system_get_master_volume(void)
111{
113}
114
115/* Function start: 0x0044745B */
116extern "C" void ix_system_set_master_volume(unsigned short volume)
117{
119}
120
121/* Function start: 0x00447477 */
122extern "C" int ix_system_get_voice_count(void)
123{
124 return nSystemVoices;
125}
126
127/* Function start: 0x0044748C */ /* source line 137 */
128extern "C" void ix_system_set_voice_count(int voiceCount)
129{
130 if (nActiveVoices != 0) {
131 ix_log_printf("Warning [%s - %d]:\n", IX_SYSTEM_FILE, 137);
132 ix_log_printf("Not a good ideal to change the number of voices while some are playing.");
133 }
134 ix_dsp_set_voice_count(voiceCount);
136}
137
138/* Function start: 0x004474E3 */
140{
141 IxSample *sample;
142
143 sample = (IxSample *)pIxMalloc(sizeof(IxSample));
144 if (sample != 0)
145 sample->ix_sample_construct();
146 return sample;
147}
148
149/* Function start: 0x00447519 */
150extern "C" void ix_system_delete_sample(IxSample *sample)
151{
152 if (sample != 0) {
153 sample->ix_sample_destruct();
154 pIxFree(sample);
155 }
156}
157
158/* Function start: 0x00447548 */
159extern "C" void ix_system_delete_all_samples(void)
160{
161 IxSample *sample;
162 IxSample *next;
163
164 sample = pSamples;
165 while (sample != 0) {
166 next = sample->next;
168 sample = next;
169 }
170}
171
172/* Function start: 0x0044758D */
174{
175 IxSound *sound;
176
177 sound = (IxSound *)pIxMalloc(sizeof(IxSound));
178 if (sound != 0)
180 return sound;
181}
182
183/* Function start: 0x004475C7 */
184extern "C" void ix_system_delete_sound(IxSound *sound)
185{
186 if (sound != 0) {
187 if ((sound->flags & IX_SOUND_PLAYING) != 0) {
189 ix_sound_stop(sound);
190 }
192 pIxFree(sound);
193 }
194}
195
196/* Function start: 0x00447610 */
197extern "C" void ix_system_delete_all_sounds(void)
198{
199 IxSound *sound;
200
201 sound = pPlayingSoundList;
202 while (sound != 0) {
203 IxSound *nextActive = sound->next;
205 sound = nextActive;
206 }
207 sound = pQueuedSoundList;
208 while (sound != 0) {
209 IxSound *nextWaiting = sound->next;
211 sound = nextWaiting;
212 }
213 sound = pIdleSoundList;
214 while (sound != 0) {
215 IxSound *nextFree = sound->next;
217 sound = nextFree;
218 }
219}
220
221/* Function start: 0x004476B9 */
223{
224 int voice = sound->voice;
225
227 sound->voice = -1;
228 sound->flags &= ~IX_SOUND_HAS_VOICE;
230 return voice;
231}
232
233/* Function start: 0x004476FA */
234void ix_system_assign_voice(IxSound *sound, int voice)
235{
236 IxSample *sample = sound->sample;
237
238 {
239 unsigned int elapsed = ix_dsp_get_tick() - sound->startTime;
240 unsigned int position =
241 ((sound->pitchOffset + sample->frequency) * elapsed) / 15;
242 if ((sound->flags & IX_SOUND_LOOPING) != 0) {
243 unsigned int loopLength = sample->loopEnd - sample->loopStart;
244 if (sample->loopEnd < position)
245 position = sample->loopStart +
246 (position - sample->loopStart) % loopLength;
247 if (sample->rateNumerator == sample->rateDenominator)
248 position = sample->bytesPerFrame * position;
249 else
250 position = (sample->bytesPerFrame * sample->rateNumerator *
251 position) / sample->rateDenominator;
253 voice, sample->buffer + sample->loopStart * sample->bytesPerFrame,
254 sample->bytesPerFrame * loopLength);
255 ix_dspv_set_position(voice, position);
256 ix_dspv_set_flag4(voice, 1);
257 } else {
258 if (sample->rateNumerator == sample->rateDenominator)
259 position = sample->bytesPerFrame * position;
260 else
261 position = (sample->bytesPerFrame * sample->rateNumerator *
262 position) / sample->rateDenominator;
263 ix_dspv_set_buffer(voice, sample->buffer,
264 sample->bytesPerFrame * sample->sampleCount);
265 ix_dspv_set_position(voice, position);
266 ix_dspv_set_flag4(voice, 0);
267 }
268 }
269 ix_dspv_set_channels(voice, sample->channels);
271 ix_dspv_set_frequency(voice, sample->frequency + sound->pitchOffset);
272 ix_dspv_set_pan(voice, sound->pan);
273 ix_dspv_set_volume(voice, sound->volume);
274 ix_dspv_set_active(voice);
275 sound->voice = voice;
276 sound->flags |= IX_SOUND_HAS_VOICE;
277 sound->flags &= ~(IX_SOUND_VOLUME_DIRTY |
281}
282
283/* Function start: 0x00447921 */
285 unsigned int minimumPriority)
286{
287 IxSound *best = 0;
288
289 while (sound != 0) {
290 if (sound->priority > minimumPriority) {
291 minimumPriority = sound->priority;
292 best = sound;
293 }
294 sound = sound->next;
295 }
296 return best;
297}
298
299/* Function start: 0x00447974 */
301 unsigned int maximumPriority)
302{
303 IxSound *best = 0;
304
305 while (sound != 0) {
306 if (sound->priority < maximumPriority) {
307 maximumPriority = sound->priority;
308 best = sound;
309 }
310 sound = sound->next;
311 }
312 return best;
313}
314
315/* Function start: 0x004479C7 */
317{
318 int voice = 0;
319
320 while (voice < nSystemVoices) {
321 if ((ix_dspv_get_flags(voice) & IX_VOICE_ACTIVE) == 0)
322 return voice;
323 voice++;
324 }
325 return -1;
326}
int ix_dsp_init(void)
Definition dsp.cpp:35
void ix_dsp_configure(int option, void *value)
Definition dsp.cpp:77
int ix_dsp_get_voice_count(void)
Definition dsp.cpp:164
unsigned int ix_dsp_get_tick(void)
Definition dsp.cpp:134
void ix_dsp_shutdown(void)
Definition dsp.cpp:58
void ix_dsp_set_voice_count(int voiceCount)
Definition dsp.cpp:170
void *__cdecl * pIxMalloc(unsigned int)
unsigned short ix_dsp_get_master_volume(void)
Definition dsp.cpp:206
void ix_dsp_set_master_volume(unsigned short volume)
Definition dsp.cpp:212
void ix_dspv_set_channels(int voice, int channels)
Definition dspv.cpp:201
void ix_dspv_set_buffer(int voice, unsigned char *p, int len)
Definition dspv.cpp:79
unsigned int ix_dspv_get_flags(int voice)
Definition dspv.cpp:58
void ix_dspv_set_active(int voice)
Definition dspv.cpp:25
void ix_dspv_set_volume(int voice, short vol)
Definition dspv.cpp:122
void ix_dspv_set_flag4(int voice, int on)
Definition dspv.cpp:70
void ix_dspv_set_bits_per_sample(int voice, int bps)
Definition dspv.cpp:182
void ix_dspv_set_position(int voice, int off)
Definition dspv.cpp:109
void ix_dspv_set_pan(int voice, unsigned short angle)
Definition dspv.cpp:134
void ix_dspv_set_frequency(int voice, unsigned int hz)
Definition dspv.cpp:170
void ix_dspv_clear_active(int voice)
Definition dspv.cpp:36
The library's classes and shared declarations.
unsigned short ix_system_get_master_volume(void)
Definition system.cpp:110
int ix_system_get_voice_count(void)
Definition system.cpp:122
int ix_system_find_free_voice(void)
Definition system.cpp:316
IxSound * pIdleSoundList
Definition system.cpp:15
IxSound * pPlayingSoundList
Definition system.cpp:19
IxSample * pSamples
Definition system.cpp:16
unsigned int dwSoundStateFlags
Definition system.cpp:14
void ix_log_printf(const char *fmt,...)
Definition ixlog.cpp:32
IxSound * ix_system_find_lowest_playing(IxSound *sound, unsigned int maximumPriority)
Definition system.cpp:300
int ix_system_release_voice(IxSound *sound)
Definition system.cpp:222
IxSound * ix_system_find_highest_waiting(IxSound *sound, unsigned int minimumPriority)
Definition system.cpp:284
@ IX_SOUND_LOOPING
Definition ix.h:141
@ IX_SOUND_HAS_VOICE
Definition ix.h:143
@ IX_SOUND_VOLUME_DIRTY
Definition ix.h:144
@ IX_SOUND_PAN_DIRTY
Definition ix.h:146
@ IX_SOUND_FREQUENCY_DIRTY
Definition ix.h:145
@ IX_SOUND_DELETE_ON_STOP
Definition ix.h:142
@ IX_SOUND_PLAYING
Definition ix.h:140
IxSound * pQueuedSoundList
Definition system.cpp:20
@ IX_VOICE_ACTIVE
Definition ix.h:51
void ix_system_assign_voice(IxSound *sound, int voice)
Definition system.cpp:234
void ix_system_delete_sample(IxSample *sample)
Definition system.cpp:150
int nSystemVoices
Definition system.cpp:18
int nActiveVoices
Definition system.cpp:17
void __fastcall ix_sound_unlink_from_free_list(IxSound *sound)
Definition sound.cpp:56
unsigned char * buffer
Definition ix.h:110
unsigned int frequency
Definition ix.h:117
unsigned int sampleCount
Definition ix.h:111
IxSample * next
Definition ix.h:121
unsigned int bitsPerSample
Definition ix.h:112
void ix_sample_construct(void)
Definition sample.cpp:56
unsigned int channels
Definition ix.h:113
unsigned int loopStart
Definition ix.h:118
unsigned int loopEnd
Definition ix.h:119
unsigned int rateNumerator
Definition ix.h:115
unsigned int rateDenominator
Definition ix.h:116
unsigned int bytesPerFrame
Definition ix.h:114
void ix_sample_destruct(void)
Definition sample.cpp:68
IxSound * next
Definition ix.h:162
unsigned short volume
Definition ix.h:158
unsigned short pan
Definition ix.h:159
unsigned int flags
Definition wc1base.h:62
int voice
Definition ix.h:153
IxSample * sample
Definition wc1base.h:63
unsigned int stopTime
Definition ix.h:155
unsigned int priority
Definition ix.h:156
int pitchOffset
Definition ix.h:157
unsigned int startTime
Definition ix.h:154
void ix_system_sound_construct_centred(IxSample *newSample)
Definition sound.cpp:50
#define IX_SYSTEM_FILE
Definition system.cpp:22
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 __fastcall ix_sound_stop(IxSound *sound)
Definition sound.cpp:209
IxSound * ix_system_new_sound(IxSample *sample)
Definition system.cpp:173
void ix_system_shutdown(void)
Definition system.cpp:93
void ix_system_configure(int option, void *value)
Definition system.cpp:104
void ix_system_set_voice_count(int voiceCount)
Definition system.cpp:128
void ix_system_delete_all_sounds(void)
Definition system.cpp:197
IxSample * ix_system_new_sample(void)
Definition system.cpp:139
void ix_system_delete_sound(IxSound *sound)
Definition system.cpp:184
void ix_system_service_sounds(void)
Definition system.cpp:46