Wing Commander II
Reconstructed Win32 source (1996)
Loading...
Searching...
No Matches
system.cpp
Go to the documentation of this file.
1/*
2 * ix / D:\Rnd\prj\ix\src\sound\system.cpp
3 *
4 * Retail address range: 0x00469C80 - 0x0046A49F (19 functions)
5 * Object boundaries follow retail padding and alignment; diagnostic paths
6 * identify the source subsystem (see docs/ORDER.md).
7 *
8 * Implement in ADDRESS ORDER -- MSVC emits functions in source order, so the
9 * list below is the original source order of this file.
10 */
11#include "ix.h"
12#include <memory.h>
13
14unsigned int g_dwIxSystemBits; /* 0x005c4b1c */
15IxSound *g_1_pFreeSoundHead; /* 0x005c4b20 */
16IxSample *g_7_pSampleList; /* 0x005c4b24 */
17int g_nActiveVoices; /* 0x005c4b28 */
18int g_nSystemVoiceCount; /* 0x005c4b2c */
19IxSound *g_1_pActiveSoundList; /* 0x005c4b30 */
21
22#define IX_SYSTEM_FILE "D:\\Rnd\\prj\\ix\\src\\sound\\system.cpp"
23
24/* Function start: 0x469C80 */ /* source line 33 */
25extern "C" int ix_system_init(void)
26{
27 if ((g_dwIxSystemBits & 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 }
41 }
42 return 0;
43}
44
45/* Function start: 0x469D27 */
46extern "C" void ix_system_service_sounds(void)
47{
48 if (g_1_pWaitingSoundList != 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 (g_1_pActiveSoundList != 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: 0x469E73 */
93extern "C" void ix_system_shutdown(void)
94{
95 if ((g_dwIxSystemBits & 1) != 0) {
99 g_dwIxSystemBits &= ~1U;
100 }
101}
102
103/* Function start: 0x469EA6 */
104extern "C" void ix_system_configure(int option, void *value)
105{
106 ix_dsp_configure(option, value);
107}
108
109/* Function start: 0x469EC6 */
110extern "C" unsigned short ix_system_get_master_volume(void)
111{
113}
114
115/* Function start: 0x469EDB */
116extern "C" void ix_system_set_master_volume(unsigned short volume)
117{
119}
120
121/* Function start: 0x469EF7 */
122extern "C" int ix_system_get_voice_count(void)
123{
124 return g_nSystemVoiceCount;
125}
126
127/* Function start: 0x469F0C */ /* source line 137 */
128extern "C" void ix_system_set_voice_count(int voiceCount)
129{
130 if (g_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: 0x469F63 */
140{
141 IxSample *sample;
142
143 sample = (IxSample *)g_pIxMalloc(sizeof(IxSample));
144 if (sample != 0)
145 sample->ix_sample_construct();
146 return sample;
147}
148
149/* Function start: 0x469F99 */
150extern "C" void ix_system_delete_sample(IxSample *sample)
151{
152 if (sample != 0) {
153 sample->ix_sample_destruct();
154 g_pIxFree(sample);
155 }
156}
157
158/* Function start: 0x469FC8 */
159extern "C" void ix_system_delete_all_samples(void)
160{
161 IxSample *sample;
162 IxSample *next;
163
164 sample = g_7_pSampleList;
165 while (sample != 0) {
166 next = sample->next;
168 sample = next;
169 }
170}
171
172/* Function start: 0x46A00D */
174{
175 IxSound *sound;
176
177 sound = (IxSound *)g_pIxMalloc(sizeof(IxSound));
178 if (sound != 0)
180 return sound;
181}
182
183/* Function start: 0x46A047 */
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 g_pIxFree(sound);
193 }
194}
195
196/* Function start: 0x46A090 */
197extern "C" void ix_system_delete_all_sounds(void)
198{
199 IxSound *current;
200 IxSound *activeNext;
201 IxSound *waitingNext;
202 IxSound *freeNext;
203
204 current = g_1_pActiveSoundList;
205 while (current != 0) {
206 activeNext = current->next;
207 ix_system_delete_sound(current);
208 current = activeNext;
209 }
210 current = g_1_pWaitingSoundList;
211 while (current != 0) {
212 waitingNext = current->next;
213 ix_system_delete_sound(current);
214 current = waitingNext;
215 }
216 current = g_1_pFreeSoundHead;
217 while (current != 0) {
218 freeNext = current->next;
219 ix_system_delete_sound(current);
220 current = freeNext;
221 }
222}
223
224/* Function start: 0x46A139 */
226{
227 int voice = sound->voice;
228
230 sound->voice = -1;
231 sound->flags &= ~IX_SOUND_HAS_VOICE;
233 return voice;
234}
235
236/* Function start: 0x46A17A */
237void ix_system_assign_voice(IxSound *sound, int voice)
238{
239 IxSample *sample = sound->sample;
240 unsigned int position;
241 unsigned int elapsedTime;
242 unsigned int loopLength;
243
244 elapsedTime = ix_dsp_get_tick() - sound->startTime;
245 position = ((sound->pitchOffset + sample->frequency) * elapsedTime) / 15;
246 if ((sound->flags & IX_SOUND_LOOPING) != 0) {
247 loopLength = sample->loopEnd - sample->loopStart;
248 if (sample->loopEnd < position)
249 position = sample->loopStart +
250 (position - sample->loopStart) % loopLength;
251 if (sample->rateNumerator == sample->rateDenominator)
252 position = sample->bytesPerFrame * position;
253 else
254 position = (sample->bytesPerFrame * sample->rateNumerator *
255 position) / sample->rateDenominator;
257 voice, sample->buffer + sample->loopStart * sample->bytesPerFrame,
258 sample->bytesPerFrame * loopLength);
259 ix_dspv_set_position(voice, position);
260 ix_dspv_set_flag4(voice, 1);
261 } else {
262 if (sample->rateNumerator == sample->rateDenominator)
263 position = sample->bytesPerFrame * position;
264 else
265 position = (sample->bytesPerFrame * sample->rateNumerator *
266 position) / sample->rateDenominator;
267 ix_dspv_set_buffer(voice, sample->buffer,
268 sample->bytesPerFrame * sample->sampleCount);
269 ix_dspv_set_position(voice, position);
270 ix_dspv_set_flag4(voice, 0);
271 }
272 ix_dspv_set_channels(voice, sample->channels);
274 ix_dspv_set_frequency(voice, sample->frequency + sound->pitchOffset);
275 ix_dspv_set_pan(voice, sound->pan);
276 ix_dspv_set_volume(voice, sound->volume);
277 ix_dspv_set_active(voice);
278 sound->voice = voice;
279 sound->flags |= IX_SOUND_HAS_VOICE;
280 sound->flags &= ~(IX_SOUND_VOLUME_DIRTY |
284}
285
286/* Function start: 0x46A3A1 */
288 unsigned int minimumPriority)
289{
290 IxSound *best = 0;
291
292 while (sound != 0) {
293 if (sound->priority > minimumPriority) {
294 minimumPriority = sound->priority;
295 best = sound;
296 }
297 sound = sound->next;
298 }
299 return best;
300}
301
302/* Function start: 0x46A3F4 */
304 unsigned int maximumPriority)
305{
306 IxSound *best = 0;
307
308 while (sound != 0) {
309 if (sound->priority < maximumPriority) {
310 maximumPriority = sound->priority;
311 best = sound;
312 }
313 sound = sound->next;
314 }
315 return best;
316}
317
318/* Function start: 0x46A447 */
320{
321 int voice = 0;
322
323 while (voice < g_nSystemVoiceCount) {
324 if ((ix_dspv_get_flags(voice) & IX_VOICE_ACTIVE) == 0)
325 return voice;
326 voice++;
327 }
328 return -1;
329}
int ix_dsp_init(void)
Definition dsp.cpp:40
void ix_dsp_configure(int option, void *value)
Definition dsp.cpp:82
int ix_dsp_get_voice_count(void)
Definition dsp.cpp:169
unsigned int ix_dsp_get_tick(void)
Definition dsp.cpp:139
void ix_dsp_shutdown(void)
Definition dsp.cpp:63
void ix_dsp_set_voice_count(int voiceCount)
Definition dsp.cpp:175
void *__cdecl * g_pIxMalloc(unsigned int)
unsigned short ix_dsp_get_master_volume(void)
Definition dsp.cpp:213
void ix_dsp_set_master_volume(unsigned short volume)
Definition dsp.cpp:219
void ix_dspv_set_channels(int voice, int channels)
Definition dspv.cpp:208
unsigned int ix_dspv_get_flags(int voice)
Definition dspv.cpp:66
void ix_dspv_set_active(int voice)
Definition dspv.cpp:31
void ix_dspv_set_flag4(int voice, int on)
Definition dspv.cpp:78
void ix_dspv_set_bits_per_sample(int voice, int bps)
Definition dspv.cpp:189
void ix_dspv_set_buffer(int voice, unsigned char *buffer, int length)
Definition dspv.cpp:87
void ix_dspv_set_pan(int voice, unsigned short angle)
Definition dspv.cpp:147
void ix_dspv_set_position(int voice, int offset)
Definition dspv.cpp:119
void ix_dspv_set_frequency(int voice, unsigned int hz)
Definition dspv.cpp:177
void ix_dspv_clear_active(int voice)
Definition dspv.cpp:42
void ix_dspv_set_volume(int voice, short volume)
Definition dspv.cpp:135
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:211
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
Audio classes, DirectSound interfaces and shared ix state.
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:319
IxSample * g_7_pSampleList
Definition system.cpp:16
IxSound * g_1_pWaitingSoundList
Definition system.cpp:20
int g_nSystemVoiceCount
Definition system.cpp:18
IxSound * g_1_pFreeSoundHead
Definition system.cpp:15
void ix_log_printf(const char *fmt,...)
Definition stream.cpp:164
IxSound * ix_system_find_lowest_playing(IxSound *sound, unsigned int maximumPriority)
Definition system.cpp:303
int ix_system_release_voice(IxSound *sound)
Definition system.cpp:225
IxSound * ix_system_find_highest_waiting(IxSound *sound, unsigned int minimumPriority)
Definition system.cpp:287
@ IX_SOUND_LOOPING
Definition ix.h:102
@ IX_SOUND_HAS_VOICE
Definition ix.h:104
@ IX_SOUND_VOLUME_DIRTY
Definition ix.h:105
@ IX_SOUND_PAN_DIRTY
Definition ix.h:107
@ IX_SOUND_FREQUENCY_DIRTY
Definition ix.h:106
@ IX_SOUND_DELETE_ON_STOP
Definition ix.h:103
@ IX_SOUND_PLAYING
Definition ix.h:101
@ IX_VOICE_ACTIVE
Definition ix.h:42
int g_nActiveVoices
Definition system.cpp:17
void ix_system_assign_voice(IxSound *sound, int voice)
Definition system.cpp:237
void ix_system_delete_sample(IxSample *sample)
Definition system.cpp:150
IxSound * g_1_pActiveSoundList
Definition system.cpp:19
unsigned int g_dwIxSystemBits
Definition system.cpp:14
unsigned char * buffer
Definition ix.h:71
unsigned int frequency
Definition ix.h:78
unsigned int sampleCount
Definition ix.h:72
IxSample * next
Definition ix.h:82
unsigned int bitsPerSample
Definition ix.h:73
void ix_sample_construct(void)
Definition sample.cpp:58
unsigned int channels
Definition ix.h:74
unsigned int loopStart
Definition ix.h:79
unsigned int loopEnd
Definition ix.h:80
unsigned int rateNumerator
Definition ix.h:76
unsigned int rateDenominator
Definition ix.h:77
unsigned int bytesPerFrame
Definition ix.h:75
void ix_sample_destruct(void)
Definition sample.cpp:70
void ix_sound_unlink_from_free_list(void)
Definition sound.cpp:57
IxSound * next
Definition ix.h:123
unsigned short volume
Definition ix.h:119
unsigned short pan
Definition ix.h:120
unsigned int flags
Definition game.h:138
int voice
Definition ix.h:114
IxSample * sample
Definition game.h:139
unsigned int stopTime
Definition ix.h:116
unsigned int priority
Definition ix.h:117
int pitchOffset
Definition ix.h:118
unsigned int startTime
Definition ix.h:115
void ix_system_sound_construct_centred(IxSample *newSample)
Definition sound.cpp:51
#define IX_SYSTEM_FILE
Definition system.cpp:22