Wing Commander
Reconstructed Win32 source (1996)
Toggle main menu visibility
Loading...
Searching...
No Matches
sound.cpp
Go to the documentation of this file.
1
/**
2
* @file
3
* @ingroup ixlib
4
* @brief Sound objects: initialization, volume, pan, frequency and priority.
5
*
6
* Originally `D:\Rnd\prj\ix\src\sound\sound.cpp`, address range
7
* 447a20-4486af. The assert paths identify the source; linker INT3 fill marks
8
* the object boundaries.
9
*/
10
#include "
ix.h
"
11
#include <assert.h>
12
13
#define IX_SOUND_FILE "D:\\Rnd\\prj\\ix\\src\\sound\\sound.cpp"
14
15
/* Function start: 0x00447A20 */
16
void
IxSound::ix_system_sound_init
(
IxSample
*newSample,
17
unsigned
char
newBasePriority,
18
unsigned
int
initialVolume,
19
int
newPitchOffset,
20
unsigned
short
newPan)
21
{
22
flags
=
IX_SOUND_ALLOCATED
;
23
voice
= -1;
24
sample
= newSample;
25
pitchOffset
= newPitchOffset;
26
volume
= 0xffff;
27
pan
= newPan;
28
basePriority
= newBasePriority;
29
previous
= 0;
30
next
=
previous
;
31
stopTime
= (
unsigned
int)-1;
32
startTime
=
stopTime
;
33
priority
= ((int)
volume
<< 8) / 0xffff +
34
(
pitchOffset
<< 8) / 0xac44 +
basePriority
;
35
next
=
pIdleSoundList
;
36
previous
= 0;
37
if
(
pIdleSoundList
!= 0)
38
pIdleSoundList
->
previous
=
this
;
39
pIdleSoundList
=
this
;
40
}
41
42
/* Function start: 0x00447B17 */
43
void
IxSound::ix_system_sound_construct
(
IxSample
*newSample,
44
unsigned
char
newBasePriority)
45
{
46
ix_system_sound_init
(newSample, newBasePriority, 0xffff, 0, 0);
47
}
48
49
/* Function start: 0x00447B48 */
50
void
IxSound::ix_system_sound_construct_centred
(
IxSample
*newSample)
51
{
52
ix_system_sound_init
(newSample, 0x80, 0xffff, 0, 0);
53
}
54
55
/* Function start: 0x00447B7A */
56
void
__fastcall
ix_sound_unlink_from_free_list
(
IxSound
*sound)
57
{
58
if
(sound ==
pIdleSoundList
)
59
pIdleSoundList
= sound->
next
;
60
if
(sound->
next
!= 0)
61
sound->
next
->
previous
= sound->
previous
;
62
if
(sound->
previous
!= 0)
63
sound->
previous
->
next
= sound->
next
;
64
sound->
flags
&=
~IX_SOUND_ALLOCATED
;
65
}
66
67
/* Function start: 0x00447BE7 */
68
void
IxSound::ix_system_sound_set_volume
(
unsigned
short
newVolume)
69
{
70
flags
|=
IX_SOUND_VOLUME_DIRTY
;
71
volume
= newVolume;
72
ix_sound_reprioritise
();
73
}
74
75
/* Function start: 0x00447C18 */
76
void
IxSound::ix_system_sound_set_pan
(
unsigned
short
newPan)
77
{
78
flags
|=
IX_SOUND_PAN_DIRTY
;
79
pan
= newPan;
80
}
81
82
/* Function start: 0x00447C44 */
83
void
IxSound::ix_system_sound_set_frequency
(
int
newPitchOffset)
84
{
85
flags
|=
IX_SOUND_FREQUENCY_DIRTY
;
86
if
(-(
int
)(
sample
->
frequency
- 1) <= newPitchOffset) {
87
pitchOffset
= newPitchOffset < 0xac44 ? newPitchOffset : 0xac44;
88
}
else
{
89
pitchOffset
= -(int)(
sample
->
frequency
- 1);
90
}
91
ix_sound_reprioritise
();
92
}
93
94
/* Function start: 0x00447CAF */
95
void
IxSound::ix_system_sound_set_priority
(
unsigned
char
newBasePriority)
96
{
97
basePriority
= newBasePriority;
98
ix_sound_reprioritise
();
99
}
100
101
/* Function start: 0x00447CD8 */
/* source line 103 */
102
extern
"C"
void
__fastcall
ix_sound_start
(
IxSound
*sound)
103
{
104
int
voice;
105
IxSound
*lowestPlaying;
106
int
releasedVoice;
107
108
if
((sound->
flags
&
IX_SOUND_PLAYING
) != 0) {
109
ix_log_printf
(
"Warning [%s - %d]:\n"
,
IX_SOUND_FILE
, 103);
110
ix_log_printf
(
"sound already started!"
);
111
return
;
112
}
113
114
sound->
flags
|=
IX_SOUND_PLAYING
;
115
sound->
startTime
=
ix_dsp_get_tick
();
116
if
((sound->
sample
->
flags
&
IX_SAMPLE_LOOPING
) != 0) {
117
sound->
flags
|=
IX_SOUND_LOOPING
;
118
sound->
flags
&=
~IX_SOUND_RELEASE_PENDING
;
119
sound->
stopTime
= (
unsigned
int)-1;
120
}
else
{
121
sound->
stopTime
= sound->
startTime
+
122
(sound->
sample
->
sampleCount
* 15) /
123
(sound->
sample
->
frequency
+ sound->
pitchOffset
);
124
}
125
126
if
(
nActiveVoices
<
nSystemVoices
) {
127
voice =
ix_system_find_free_voice
();
128
129
if
(sound ==
pIdleSoundList
)
130
pIdleSoundList
= sound->
next
;
131
if
(sound->
next
!= 0)
132
sound->
next
->
previous
= sound->
previous
;
133
if
(sound->
previous
!= 0)
134
sound->
previous
->
next
= sound->
next
;
135
sound->
next
=
pPlayingSoundList
;
136
sound->
previous
= 0;
137
if
(
pPlayingSoundList
!= 0)
138
pPlayingSoundList
->
previous
= sound;
139
pPlayingSoundList
= sound;
140
ix_system_assign_voice
(sound, voice);
141
}
else
{
142
lowestPlaying =
ix_system_find_lowest_playing
(
143
pPlayingSoundList
, sound->
priority
);
144
if
(lowestPlaying != 0) {
145
releasedVoice =
ix_system_release_voice
(lowestPlaying);
146
147
if
(lowestPlaying ==
pPlayingSoundList
)
148
pPlayingSoundList
= lowestPlaying->
next
;
149
if
(lowestPlaying->
next
!= 0)
150
lowestPlaying->
next
->
previous
= lowestPlaying->
previous
;
151
if
(lowestPlaying->
previous
!= 0)
152
lowestPlaying->
previous
->
next
= lowestPlaying->
next
;
153
lowestPlaying->
next
=
pQueuedSoundList
;
154
lowestPlaying->
previous
= 0;
155
if
(
pQueuedSoundList
!= 0)
156
pQueuedSoundList
->
previous
= lowestPlaying;
157
pQueuedSoundList
= lowestPlaying;
158
159
if
(sound ==
pIdleSoundList
)
160
pIdleSoundList
= sound->
next
;
161
if
(sound->
next
!= 0)
162
sound->
next
->
previous
= sound->
previous
;
163
if
(sound->
previous
!= 0)
164
sound->
previous
->
next
= sound->
next
;
165
sound->
next
=
pPlayingSoundList
;
166
sound->
previous
= 0;
167
if
(
pPlayingSoundList
!= 0)
168
pPlayingSoundList
->
previous
= sound;
169
pPlayingSoundList
= sound;
170
ix_system_assign_voice
(sound, releasedVoice);
171
}
else
{
172
if
(sound ==
pIdleSoundList
)
173
pIdleSoundList
= sound->
next
;
174
if
(sound->
next
!= 0)
175
sound->
next
->
previous
= sound->
previous
;
176
if
(sound->
previous
!= 0)
177
sound->
previous
->
next
= sound->
next
;
178
sound->
next
=
pQueuedSoundList
;
179
sound->
previous
= 0;
180
if
(
pQueuedSoundList
!= 0)
181
pQueuedSoundList
->
previous
= sound;
182
pQueuedSoundList
= sound;
183
}
184
}
185
}
186
187
/* Function start: 0x0044801E */
/* source lines 151, 152 */
188
extern
"C"
void
__fastcall
ix_sound_release
(
IxSound
*sound)
189
{
190
if
((sound->
flags
&
IX_SOUND_PLAYING
) == 0) {
191
ix_log_printf
(
"Warning [%s - %d]:\n"
,
IX_SOUND_FILE
, 151);
192
ix_log_printf
(
"can't release non playing sound!"
);
193
return
;
194
}
195
if
((sound->
flags
&
IX_SOUND_LOOPING
) == 0) {
196
ix_log_printf
(
"Warning [%s - %d]:\n"
,
IX_SOUND_FILE
, 152);
197
ix_log_printf
(
"can't release non looping sound!"
);
198
return
;
199
}
200
if
((sound->
flags
&
IX_SOUND_HAS_VOICE
) != 0) {
201
ix_dspv_clear_flag4
(sound->
voice
);
202
sound->
flags
|=
IX_SOUND_RELEASE_PENDING
;
203
}
else
{
204
ix_sound_stop
(sound);
205
}
206
}
207
208
/* Function start: 0x004480CF */
/* source line(s) 165: can't stop sound that's not playing! */
209
extern
"C"
void
__fastcall
ix_sound_stop
(
IxSound
*sound)
210
{
211
IxSound
*replacement;
212
int
voice;
213
214
if
((sound->
flags
&
IX_SOUND_PLAYING
) == 0) {
215
ix_log_printf
(
"Warning [%s - %d]:\n"
,
IX_SOUND_FILE
, 165);
216
ix_log_printf
(
"can't stop sound that's not playing!"
);
217
return
;
218
}
219
220
sound->
flags
&=
~IX_SOUND_PLAYING
;
221
if
((sound->
flags
&
IX_SOUND_HAS_VOICE
) != 0) {
222
voice =
ix_system_release_voice
(sound);
223
replacement =
ix_system_find_highest_waiting
(
224
pQueuedSoundList
, 0);
225
226
if
(sound ==
pPlayingSoundList
)
227
pPlayingSoundList
= sound->
next
;
228
if
(sound->
next
!= 0)
229
sound->
next
->
previous
= sound->
previous
;
230
if
(sound->
previous
!= 0)
231
sound->
previous
->
next
= sound->
next
;
232
sound->
next
=
pIdleSoundList
;
233
sound->
previous
= 0;
234
if
(
pIdleSoundList
!= 0)
235
pIdleSoundList
->
previous
= sound;
236
pIdleSoundList
= sound;
237
238
if
(replacement != 0) {
239
ix_system_assign_voice
(replacement, voice);
240
if
(replacement ==
pQueuedSoundList
)
241
pQueuedSoundList
= replacement->
next
;
242
if
(replacement->
next
!= 0)
243
replacement->
next
->
previous
= replacement->
previous
;
244
if
(replacement->
previous
!= 0)
245
replacement->
previous
->
next
= replacement->
next
;
246
replacement->
next
=
pPlayingSoundList
;
247
replacement->
previous
= 0;
248
if
(
pPlayingSoundList
!= 0)
249
pPlayingSoundList
->
previous
= replacement;
250
pPlayingSoundList
= replacement;
251
}
252
}
else
{
253
if
(sound ==
pQueuedSoundList
)
254
pQueuedSoundList
= sound->
next
;
255
if
(sound->
next
!= 0)
256
sound->
next
->
previous
= sound->
previous
;
257
if
(sound->
previous
!= 0)
258
sound->
previous
->
next
= sound->
next
;
259
sound->
next
=
pIdleSoundList
;
260
sound->
previous
= 0;
261
if
(
pIdleSoundList
!= 0)
262
pIdleSoundList
->
previous
= sound;
263
pIdleSoundList
= sound;
264
}
265
266
if
((sound->
flags
&
IX_SOUND_DELETE_ON_STOP
) != 0)
267
ix_system_delete_sound
(sound);
268
}
269
270
/* Function start: 0x0044831A */
271
void
IxSound::ix_sound_reprioritise
(
void
)
272
{
273
unsigned
int
newPriority;
274
275
newPriority = (
volume
<< 8) / 0xffff +
276
(
pitchOffset
<< 8) / 0xac44 +
basePriority
;
277
if
((
flags
&
IX_SOUND_HAS_VOICE
) != 0) {
278
if
(
priority
> newPriority) {
279
IxSound
*waitingSound =
ix_system_find_highest_waiting
(
280
pQueuedSoundList
, newPriority);
281
if
(waitingSound != 0) {
282
int
releasedVoice =
ix_system_release_voice
(
this
);
283
284
if
(
this
==
pPlayingSoundList
)
285
pPlayingSoundList
=
next
;
286
if
(
next
!= 0)
287
next
->
previous
=
previous
;
288
if
(
previous
!= 0)
289
previous
->
next
=
next
;
290
next
=
pQueuedSoundList
;
291
previous
= 0;
292
if
(
pQueuedSoundList
!= 0)
293
pQueuedSoundList
->
previous
=
this
;
294
pQueuedSoundList
=
this
;
295
296
if
(waitingSound ==
pQueuedSoundList
)
297
pQueuedSoundList
= waitingSound->
next
;
298
if
(waitingSound->
next
!= 0)
299
waitingSound->
next
->
previous
= waitingSound->
previous
;
300
if
(waitingSound->
previous
!= 0)
301
waitingSound->
previous
->
next
= waitingSound->
next
;
302
waitingSound->
next
=
pPlayingSoundList
;
303
waitingSound->
previous
= 0;
304
if
(
pPlayingSoundList
!= 0)
305
pPlayingSoundList
->
previous
= waitingSound;
306
pPlayingSoundList
= waitingSound;
307
ix_system_assign_voice
(waitingSound, releasedVoice);
308
}
309
}
310
}
else
if
((
flags
&
IX_SOUND_PLAYING
) != 0 &&
priority
< newPriority) {
311
IxSound
*playingSound =
ix_system_find_lowest_playing
(
312
pPlayingSoundList
, newPriority);
313
if
(playingSound != 0) {
314
int
reassignedVoice =
ix_system_release_voice
(playingSound);
315
316
if
(playingSound ==
pPlayingSoundList
)
317
pPlayingSoundList
= playingSound->
next
;
318
if
(playingSound->
next
!= 0)
319
playingSound->
next
->
previous
= playingSound->
previous
;
320
if
(playingSound->
previous
!= 0)
321
playingSound->
previous
->
next
= playingSound->
next
;
322
playingSound->
next
=
pQueuedSoundList
;
323
playingSound->
previous
= 0;
324
if
(
pQueuedSoundList
!= 0)
325
pQueuedSoundList
->
previous
= playingSound;
326
pQueuedSoundList
= playingSound;
327
328
if
(
this
==
pQueuedSoundList
)
329
pQueuedSoundList
=
next
;
330
if
(
next
!= 0)
331
next
->
previous
=
previous
;
332
if
(
previous
!= 0)
333
previous
->
next
=
next
;
334
next
=
pPlayingSoundList
;
335
previous
= 0;
336
if
(
pPlayingSoundList
!= 0)
337
pPlayingSoundList
->
previous
=
this
;
338
pPlayingSoundList
=
this
;
339
ix_system_assign_voice
(
this
, reassignedVoice);
340
}
341
}
342
priority
= newPriority;
343
}
344
345
/* Function start: 0x00448645 */
346
void
IxSound::ix_sound_set_delete_on_stop
(
int
enabled)
347
{
348
if
(enabled != 0)
349
flags
|=
IX_SOUND_DELETE_ON_STOP
;
350
else
351
flags
&=
~IX_SOUND_DELETE_ON_STOP
;
352
}
353
354
/* Function start: 0x00448678 */
355
extern
"C"
int
__fastcall
ix_sound_is_playing
(
IxSound
*sound)
356
{
357
return
(sound->
flags
&
IX_SOUND_PLAYING
) != 0;
358
}
ix_dsp_get_tick
unsigned int ix_dsp_get_tick(void)
Definition
dsp.cpp:134
ix_dspv_clear_flag4
void ix_dspv_clear_flag4(int voice)
Definition
dspv.cpp:47
ix.h
The library's classes and shared declarations.
ix_system_find_free_voice
int ix_system_find_free_voice(void)
Definition
system.cpp:316
pIdleSoundList
IxSound * pIdleSoundList
Definition
system.cpp:15
pPlayingSoundList
IxSound * pPlayingSoundList
Definition
system.cpp:19
IX_SAMPLE_LOOPING
@ IX_SAMPLE_LOOPING
Definition
ix.h:134
ix_log_printf
void ix_log_printf(const char *fmt,...)
Definition
ixlog.cpp:32
ix_system_find_lowest_playing
IxSound * ix_system_find_lowest_playing(IxSound *sound, unsigned int maximumPriority)
Definition
system.cpp:300
ix_system_release_voice
int ix_system_release_voice(IxSound *sound)
Definition
system.cpp:222
ix_system_find_highest_waiting
IxSound * ix_system_find_highest_waiting(IxSound *sound, unsigned int minimumPriority)
Definition
system.cpp:284
IX_SOUND_LOOPING
@ IX_SOUND_LOOPING
Definition
ix.h:141
IX_SOUND_HAS_VOICE
@ IX_SOUND_HAS_VOICE
Definition
ix.h:143
IX_SOUND_VOLUME_DIRTY
@ IX_SOUND_VOLUME_DIRTY
Definition
ix.h:144
IX_SOUND_RELEASE_PENDING
@ IX_SOUND_RELEASE_PENDING
Definition
ix.h:147
IX_SOUND_PAN_DIRTY
@ IX_SOUND_PAN_DIRTY
Definition
ix.h:146
IX_SOUND_ALLOCATED
@ IX_SOUND_ALLOCATED
Definition
ix.h:139
IX_SOUND_FREQUENCY_DIRTY
@ IX_SOUND_FREQUENCY_DIRTY
Definition
ix.h:145
IX_SOUND_DELETE_ON_STOP
@ IX_SOUND_DELETE_ON_STOP
Definition
ix.h:142
IX_SOUND_PLAYING
@ IX_SOUND_PLAYING
Definition
ix.h:140
pQueuedSoundList
IxSound * pQueuedSoundList
Definition
system.cpp:20
ix_system_assign_voice
void ix_system_assign_voice(IxSound *sound, int voice)
Definition
system.cpp:234
nSystemVoices
int nSystemVoices
Definition
system.cpp:18
nActiveVoices
int nActiveVoices
Definition
system.cpp:17
ix_sound_unlink_from_free_list
void __fastcall ix_sound_unlink_from_free_list(IxSound *sound)
Definition
sound.cpp:56
IX_SOUND_FILE
#define IX_SOUND_FILE
Definition
sound.cpp:13
IxSample
Definition
wc1base.h:54
IxSample::flags
unsigned int flags
Definition
wc1base.h:55
IxSample::frequency
unsigned int frequency
Definition
ix.h:117
IxSample::sampleCount
unsigned int sampleCount
Definition
ix.h:111
IxSound
Definition
wc1base.h:61
IxSound::ix_sound_reprioritise
void ix_sound_reprioritise(void)
Definition
sound.cpp:271
IxSound::next
IxSound * next
Definition
ix.h:162
IxSound::volume
unsigned short volume
Definition
ix.h:158
IxSound::ix_system_sound_construct
void ix_system_sound_construct(IxSample *newSample, unsigned char newBasePriority)
Definition
sound.cpp:43
IxSound::ix_system_sound_set_volume
void ix_system_sound_set_volume(unsigned short newVolume)
Definition
sound.cpp:68
IxSound::previous
IxSound * previous
Definition
ix.h:161
IxSound::pan
unsigned short pan
Definition
ix.h:159
IxSound::ix_system_sound_set_frequency
void ix_system_sound_set_frequency(int newPitchOffset)
Definition
sound.cpp:83
IxSound::flags
unsigned int flags
Definition
wc1base.h:62
IxSound::voice
int voice
Definition
ix.h:153
IxSound::ix_system_sound_set_pan
void ix_system_sound_set_pan(unsigned short newPan)
Definition
sound.cpp:76
IxSound::sample
IxSample * sample
Definition
wc1base.h:63
IxSound::stopTime
unsigned int stopTime
Definition
ix.h:155
IxSound::priority
unsigned int priority
Definition
ix.h:156
IxSound::ix_system_sound_set_priority
void ix_system_sound_set_priority(unsigned char newBasePriority)
Definition
sound.cpp:95
IxSound::pitchOffset
int pitchOffset
Definition
ix.h:157
IxSound::startTime
unsigned int startTime
Definition
ix.h:154
IxSound::ix_system_sound_construct_centred
void ix_system_sound_construct_centred(IxSample *newSample)
Definition
sound.cpp:50
IxSound::ix_system_sound_init
void ix_system_sound_init(IxSample *newSample, unsigned char newBasePriority, unsigned int initialVolume, int newPitchOffset, unsigned short newPan)
Definition
sound.cpp:16
IxSound::ix_sound_set_delete_on_stop
void ix_sound_set_delete_on_stop(int enabled)
Definition
sound.cpp:346
IxSound::basePriority
unsigned char basePriority
Definition
ix.h:160
ix_sound_stop
void __fastcall ix_sound_stop(IxSound *sound)
Definition
sound.cpp:209
ix_sound_is_playing
int __fastcall ix_sound_is_playing(IxSound *sound)
Definition
sound.cpp:355
ix_sound_start
void __fastcall ix_sound_start(IxSound *sound)
Definition
sound.cpp:102
ix_system_delete_sound
void ix_system_delete_sound(IxSound *sound)
Definition
system.cpp:184
ix_sound_release
void __fastcall ix_sound_release(IxSound *sound)
Definition
sound.cpp:188
src
ix
sound.cpp
Generated by
1.18.0