Wing Commander II
Reconstructed Win32 source (1996)
Toggle main menu visibility
Loading...
Searching...
No Matches
dsps.cpp
Go to the documentation of this file.
1
/*
2
* ix / D:\rnd\prj\ix\win95\dsp\dsps.cpp
3
*
4
* Original address range: 0x004451B5 - 0x00445F5F (10 functions)
5
* Range is exact: recovered from this module's own assert __FILE__ anchors.
6
* Built /Od /Oi like the rest of ix -- do not optimise this file.
7
*
8
* A "stream" is a ring buffer fed by the streamer thread and played through a
9
* voice slot allocated after the regular voices: voice[voice_count + index].
10
*/
11
#include "
ix.h
"
12
#include <dos.h>
13
#include <stdlib.h>
14
15
int
g_3_nStreamCount
;
/* 0x005c5750 */
16
int
g_nStreamsTaken
;
/* 0x005c5754 */
17
IxStream
g_1_streams
[2];
/* 0x005c5758 */
18
19
/* The stream operations further down. With the stream table's
20
* initializer, their named parameters give this unit retail's symbol-key
21
* numbering. */
22
void
ix_dsps_prepare
(
int
stream);
23
void
ix_dsps_play
(
int
stream);
24
void
ix_dsps_stop
(
int
stream);
25
void
ix_dsps_set_volume
(
int
stream,
unsigned
short
volume);
26
int
ix_dsps_get_buffer_free
(
int
stream);
27
void
ix_dsps_unlock
(
int
stream);
28
29
/* Function start: 0x48AA15 */
/* source lines 26, 27, 28, 32 */
30
void
ix_dsps_alloc
(
int
stream,
unsigned
int
size,
int
freq,
int
bps,
int
channels)
31
{
32
IxVoice
*v;
33
34
if
(stream < 0 || stream >=
g_3_nStreamCount
) {
35
ix_log_printf
(
"Fatal [%s - %d]:\n"
,
IX_DSPS_FILE
, 26);
36
ix_log_printf
(
"invalid stream index"
);
37
exit(-1);
38
}
39
if
((
g_1_streams
[stream].flags &
IX_STREAM_ALLOCATED
) != 0) {
40
ix_log_printf
(
"Fatal [%s - %d]:\n"
,
IX_DSPS_FILE
, 27);
41
ix_log_printf
(
"stream already allocated!"
);
42
exit(-1);
43
}
44
if
((
g_1_streams
[stream].flags &
IX_STREAM_PLAYING
) != 0) {
45
ix_log_printf
(
"Fatal [%s - %d]:\n"
,
IX_DSPS_FILE
, 28);
46
ix_log_printf
(
"stream still playing!"
);
47
exit(-1);
48
}
49
g_1_streams
[stream].
buffer
=
new
unsigned
char
[size];
50
if
(
g_1_streams
[stream].buffer == 0) {
51
ix_log_printf
(
"Fatal [%s - %d]:\n"
,
IX_DSPS_FILE
, 32);
52
ix_log_printf
(
"failed to allocate stream buffer"
);
53
exit(-1);
54
}
55
g_1_streams
[stream].
size
= size;
56
g_1_streams
[stream].
writePos
= 0;
57
g_1_streams
[stream].
playPos
=
g_1_streams
[stream].
writePos
;
58
g_1_streams
[stream].
pending
=
g_1_streams
[stream].
playPos
;
59
InitializeCriticalSection(&
g_1_streams
[stream].cs);
60
61
v = &
g_2_voices
[
g_nVoiceCapacity
+ stream];
62
v->
flags
=
IX_VOICE_FLAG4
| 1;
63
if
(bps == 16)
64
v->
flags
|=
IX_VOICE_16BIT
;
65
else
66
;
67
if
(channels == 2)
68
v->
flags
|=
IX_VOICE_STEREO
;
69
else
70
;
71
v->
cursor
=
g_1_streams
[stream].
buffer
;
72
v->
start
= v->
cursor
;
73
v->
end
= v->
cursor
+ size;
74
v->
volume
= 0x7fff;
75
v->
leftGain
= v->
volume
;
76
v->
rightGain
= v->
volume
;
77
v->
leftGainHi
= (
unsigned
char)((
unsigned
short
)v->
volume
>> 8);
78
v->
rightGainHi
= (
unsigned
char)((
unsigned
short
)v->
volume
>> 8);
79
v->
field_10
= 0;
80
v->
rate
= (short)((freq << 8) /
IX_MIXER_BASE_RATE
);
81
g_nStreamsTaken
=
g_nStreamsTaken
+ 1;
82
g_1_streams
[stream].
flags
|=
IX_STREAM_ALLOCATED
;
83
}
84
85
void
ix_dsps_stop
(
int
stream);
86
87
/* Function start: 0x48ACCB */
/* source lines 62, 63 */
88
void
ix_dsps_free
(
int
stream)
89
{
90
if
(stream < 0 || stream >=
g_3_nStreamCount
) {
91
ix_log_printf
(
"Fatal [%s - %d]:\n"
,
IX_DSPS_FILE
, 62);
92
ix_log_printf
(
"invalid stream index"
);
93
exit(-1);
94
}
95
if
((
g_1_streams
[stream].flags &
IX_STREAM_ALLOCATED
) == 0) {
96
ix_log_printf
(
"Fatal [%s - %d]:\n"
,
IX_DSPS_FILE
, 63);
97
ix_log_printf
(
"stream is not allocated!"
);
98
exit(-1);
99
}
100
if
((
g_1_streams
[stream].flags &
IX_STREAM_PLAYING
) != 0)
101
ix_dsps_stop
(stream);
102
DeleteCriticalSection(&
g_1_streams
[stream].cs);
103
delete
[]
g_1_streams
[stream].
buffer
;
104
g_nStreamsTaken
=
g_nStreamsTaken
- 1;
105
g_1_streams
[stream].
flags
&=
~IX_STREAM_ALLOCATED
;
106
}
107
108
/* Function start: 0x48ADE2 */
/* source lines 77, 78, 79 */
109
void
ix_dsps_prepare
(
int
stream)
110
{
111
if
(stream < 0 || stream >=
g_3_nStreamCount
) {
112
ix_log_printf
(
"Fatal [%s - %d]:\n"
,
IX_DSPS_FILE
, 77);
113
ix_log_printf
(
"invalid stream index"
);
114
exit(-1);
115
}
116
if
((
g_1_streams
[stream].flags &
IX_STREAM_ALLOCATED
) == 0) {
117
ix_log_printf
(
"Fatal [%s - %d]:\n"
,
IX_DSPS_FILE
, 78);
118
ix_log_printf
(
"stream is not ready!"
);
119
exit(-1);
120
}
121
if
((
g_1_streams
[stream].flags &
IX_STREAM_PLAYING
) != 0) {
122
ix_log_printf
(
"Fatal [%s - %d]:\n"
,
IX_DSPS_FILE
, 79);
123
ix_log_printf
(
"stream is playing!"
);
124
exit(-1);
125
}
126
g_1_streams
[stream].
playPos
= 0;
127
g_1_streams
[stream].
pending
=
g_1_streams
[stream].
playPos
;
128
g_1_streams
[stream].
writePos
=
g_1_streams
[stream].
pending
;
129
g_2_voices
[
g_nVoiceCapacity
+ stream].
cursor
=
130
g_1_streams
[stream].
buffer
;
131
}
132
133
/* Function start: 0x48AF38 */
/* source lines 87, 88, 89 */
134
void
ix_dsps_play
(
int
stream)
135
{
136
IxVoice
*v;
137
138
if
(stream < 0 || stream >=
g_3_nStreamCount
) {
139
ix_log_printf
(
"Fatal [%s - %d]:\n"
,
IX_DSPS_FILE
, 87);
140
ix_log_printf
(
"invalid stream index"
);
141
exit(-1);
142
}
143
if
((
g_1_streams
[stream].flags &
IX_STREAM_ALLOCATED
) == 0) {
144
ix_log_printf
(
"Fatal [%s - %d]:\n"
,
IX_DSPS_FILE
, 88);
145
ix_log_printf
(
"stream is not ready!"
);
146
exit(-1);
147
}
148
if
((
g_1_streams
[stream].flags &
IX_STREAM_PLAYING
) != 0) {
149
ix_log_printf
(
"Fatal [%s - %d]:\n"
,
IX_DSPS_FILE
, 89);
150
ix_log_printf
(
"stream is already playing!"
);
151
exit(-1);
152
}
153
v = &
g_2_voices
[
g_nVoiceCapacity
+ stream];
154
v->
cursor
=
g_1_streams
[stream].
playPos
155
+
g_1_streams
[stream].
buffer
;
156
v->
flags
|=
IX_VOICE_ACTIVE
;
157
g_1_streams
[stream].
flags
|=
IX_STREAM_PLAYING
;
158
}
159
160
/* Function start: 0x48B068 */
/* source lines 99, 100, 101 */
161
void
ix_dsps_stop
(
int
stream)
162
{
163
if
(stream < 0 || stream >=
g_3_nStreamCount
) {
164
ix_log_printf
(
"Fatal [%s - %d]:\n"
,
IX_DSPS_FILE
, 99);
165
ix_log_printf
(
"invalid stream index"
);
166
exit(-1);
167
}
168
if
((
g_1_streams
[stream].flags &
IX_STREAM_ALLOCATED
) == 0) {
169
ix_log_printf
(
"Fatal [%s - %d]:\n"
,
IX_DSPS_FILE
, 100);
170
ix_log_printf
(
"stream is not ready!"
);
171
exit(-1);
172
}
173
if
((
g_1_streams
[stream].flags &
IX_STREAM_PLAYING
) == 0) {
174
ix_log_printf
(
"Fatal [%s - %d]:\n"
,
IX_DSPS_FILE
, 101);
175
ix_log_printf
(
"stream isnt playing!"
);
176
exit(-1);
177
}
178
g_2_voices
[
g_nVoiceCapacity
+ stream].
flags
&=
~IX_VOICE_ACTIVE
;
179
g_1_streams
[stream].
flags
&=
~IX_STREAM_PLAYING
;
180
}
181
182
void
ix_dsps_lock
(
int
stream,
unsigned
int
requestedBytes,
183
unsigned
char
**buffer,
unsigned
int
*lockedBytes);
184
unsigned
int
ix_dsps_get_flags
(
int
stream);
185
void
ix_dsps_unlock
(
int
stream);
186
187
/* Function start: 0x48B166 */
/* source lines 109, 110 */
188
void
ix_dsps_set_volume
(
int
stream,
unsigned
short
vol)
189
{
190
IxVoice
*v;
191
192
if
(stream < 0 || stream >=
g_3_nStreamCount
) {
193
ix_log_printf
(
"Fatal [%s - %d]:\n"
,
IX_DSPS_FILE
, 109);
194
ix_log_printf
(
"invalid stream index"
);
195
exit(-1);
196
}
197
if
((
g_1_streams
[stream].flags &
IX_STREAM_ALLOCATED
) == 0) {
198
ix_log_printf
(
"Fatal [%s - %d]:\n"
,
IX_DSPS_FILE
, 110);
199
ix_log_printf
(
"stream is not ready!"
);
200
exit(-1);
201
}
202
v = &
g_2_voices
[
g_nVoiceCapacity
+ stream];
203
v->
volume
=
204
(
unsigned
short)(((
int
)(vol & 0xffff) * 0x7fff) / 0xffff);
205
v->
leftGain
= v->
volume
;
206
v->
rightGain
= v->
volume
;
207
v->
leftGainHi
= (
unsigned
char)((
unsigned
short
)v->
volume
>> 8);
208
v->
rightGainHi
= (
unsigned
char)((
unsigned
short
)v->
volume
>> 8);
209
}
210
211
/* Function start: 0x48B26B */
/* source line 122 */
212
unsigned
int
ix_dsps_get_flags
(
int
stream)
213
{
214
if
(stream < 0 || stream >=
g_3_nStreamCount
) {
215
ix_log_printf
(
"Fatal [%s - %d]:\n"
,
IX_DSPS_FILE
, 122);
216
ix_log_printf
(
"invalid stream index"
);
217
exit(-1);
218
}
219
return
g_1_streams
[stream].
flags
;
220
}
221
222
/* Function start: 0x48B2CF */
/* source lines 128, 129 */
223
int
ix_dsps_get_buffer_free
(
int
stream)
224
{
225
int
consumed;
226
unsigned
int
played;
227
228
if
(stream < 0 || stream >=
g_3_nStreamCount
) {
229
ix_log_printf
(
"Fatal [%s - %d]:\n"
,
IX_DSPS_FILE
, 128);
230
ix_log_printf
(
"invalid stream index"
);
231
exit(-1);
232
}
233
if
((
g_1_streams
[stream].flags &
IX_STREAM_ALLOCATED
) == 0) {
234
ix_log_printf
(
"Fatal [%s - %d]:\n"
,
IX_DSPS_FILE
, 129);
235
ix_log_printf
(
"stream is not ready!"
);
236
exit(-1);
237
}
238
consumed = 0;
239
EnterCriticalSection(&
g_1_streams
[stream].cs);
240
played =
g_2_voices
[
g_nVoiceCapacity
+ stream].
cursor
241
-
g_1_streams
[stream].
buffer
;
242
if
((
int
)
g_1_streams
[stream].pending < 0) {
243
g_1_streams
[stream].
pending
= 0;
244
g_1_streams
[stream].
playPos
= played;
245
LeaveCriticalSection(&
g_1_streams
[stream].cs);
246
return
g_1_streams
[stream].
size
;
247
}
248
if
(
g_1_streams
[stream].playPos < played) {
249
consumed = played -
g_1_streams
[stream].
playPos
;
250
g_1_streams
[stream].
pending
=
251
(int)
g_1_streams
[stream].pending - consumed;
252
}
253
else
if
(
g_1_streams
[stream].playPos != played) {
254
consumed = (
g_1_streams
[stream].
size
255
-
g_1_streams
[stream].
playPos
) + played;
256
g_1_streams
[stream].
pending
=
257
(int)
g_1_streams
[stream].pending - consumed;
258
}
259
g_1_streams
[stream].
playPos
= played;
260
LeaveCriticalSection(&
g_1_streams
[stream].cs);
261
return
g_1_streams
[stream].
size
-
g_1_streams
[stream].
pending
;
262
}
263
264
/* Function start: 0x48B53B */
/* source lines 169, 172, 173 */
265
void
ix_dsps_lock
(
int
stream,
unsigned
int
requestedBytes,
266
unsigned
char
**buffer,
unsigned
int
*lockedBytes)
267
{
268
IxStream
*s;
269
270
if
(stream < 0 || stream >=
g_3_nStreamCount
) {
271
ix_log_printf
(
"Fatal [%s - %d]:\n"
,
IX_DSPS_FILE
, 169);
272
ix_log_printf
(
"invalid stream index"
);
273
exit(-1);
274
}
275
s = &
g_1_streams
[stream];
276
if
((
g_1_streams
[stream].flags &
IX_STREAM_ALLOCATED
) == 0) {
277
ix_log_printf
(
"Fatal [%s - %d]:\n"
,
IX_DSPS_FILE
, 172);
278
ix_log_printf
(
"stream is not ready!"
);
279
exit(-1);
280
}
281
if
((
g_1_streams
[stream].flags &
IX_STREAM_LOCKED
) != 0) {
282
ix_log_printf
(
"Fatal [%s - %d]:\n"
,
IX_DSPS_FILE
, 173);
283
ix_log_printf
(
"stream is already locked!"
);
284
exit(-1);
285
}
286
s->
lockPtr
= s->
buffer
+ s->
writePos
;
287
if
(s->
writePos
+ requestedBytes > s->
size
)
288
requestedBytes = s->
size
- s->
writePos
;
289
s->
lockLen
= requestedBytes;
290
*buffer = s->
lockPtr
;
291
*lockedBytes = s->
lockLen
;
292
g_1_streams
[stream].
flags
|=
IX_STREAM_LOCKED
;
293
}
294
295
/* Function start: 0x48B69C */
/* source lines 188, 191, 192 */
296
void
ix_dsps_unlock
(
int
stream)
297
{
298
IxStream
*s;
299
300
if
(stream < 0 || stream >=
g_3_nStreamCount
) {
301
ix_log_printf
(
"Fatal [%s - %d]:\n"
,
IX_DSPS_FILE
, 188);
302
ix_log_printf
(
"invalid stream index"
);
303
exit(-1);
304
}
305
s = &
g_1_streams
[stream];
306
if
((s->
flags
&
IX_STREAM_ALLOCATED
) == 0) {
307
ix_log_printf
(
"Fatal [%s - %d]:\n"
,
IX_DSPS_FILE
, 191);
308
ix_log_printf
(
"stream is not ready!"
);
309
exit(-1);
310
}
311
if
((s->
flags
&
IX_STREAM_LOCKED
) == 0) {
312
ix_log_printf
(
"Fatal [%s - %d]:\n"
,
IX_DSPS_FILE
, 192);
313
ix_log_printf
(
"stream isn't locked!"
);
314
exit(-1);
315
}
316
s->
writePos
+= s->
lockLen
;
317
if
(s->
writePos
>= s->
size
)
318
s->
writePos
= s->
writePos
- s->
size
;
319
s->
pending
+= s->
lockLen
;
320
s->
flags
&=
~IX_STREAM_LOCKED
;
321
}
ix_dsps_set_volume
void ix_dsps_set_volume(int stream, unsigned short volume)
Definition
dsps.cpp:188
ix_dsps_prepare
void ix_dsps_prepare(int stream)
Definition
dsps.cpp:109
ix_dsps_unlock
void ix_dsps_unlock(int stream)
Definition
dsps.cpp:296
g_1_streams
IxStream g_1_streams[2]
Definition
dsps.cpp:17
ix_dsps_get_flags
unsigned int ix_dsps_get_flags(int stream)
Definition
dsps.cpp:212
g_nStreamsTaken
int g_nStreamsTaken
Definition
dsps.cpp:16
ix_dsps_free
void ix_dsps_free(int stream)
Definition
dsps.cpp:88
g_3_nStreamCount
int g_3_nStreamCount
Definition
dsps.cpp:15
ix_dsps_stop
void ix_dsps_stop(int stream)
Definition
dsps.cpp:161
ix_dsps_play
void ix_dsps_play(int stream)
Definition
dsps.cpp:134
ix_dsps_get_buffer_free
int ix_dsps_get_buffer_free(int stream)
Definition
dsps.cpp:223
ix_dsps_alloc
void ix_dsps_alloc(int stream, unsigned int size, int freq, int bps, int channels)
Definition
dsps.cpp:30
ix_dsps_lock
void ix_dsps_lock(int stream, unsigned int requestedBytes, unsigned char **buffer, unsigned int *lockedBytes)
Definition
dsps.cpp:265
g_nVoiceCapacity
int g_nVoiceCapacity
Definition
dspv.cpp:21
g_2_voices
IxVoice g_2_voices[34]
Definition
dspv.cpp:19
ix.h
Audio classes, DirectSound interfaces and shared ix state.
IX_DSPS_FILE
#define IX_DSPS_FILE
Definition
ix.h:217
ix_log_printf
void ix_log_printf(const char *fmt,...)
Definition
stream.cpp:164
IX_STREAM_LOCKED
@ IX_STREAM_LOCKED
Definition
ix.h:195
IX_STREAM_ALLOCATED
@ IX_STREAM_ALLOCATED
Definition
ix.h:193
IX_STREAM_PLAYING
@ IX_STREAM_PLAYING
Definition
ix.h:194
IX_MIXER_BASE_RATE
#define IX_MIXER_BASE_RATE
Definition
ix.h:160
IX_VOICE_STEREO
@ IX_VOICE_STEREO
Definition
ix.h:45
IX_VOICE_FLAG4
@ IX_VOICE_FLAG4
Definition
ix.h:43
IX_VOICE_16BIT
@ IX_VOICE_16BIT
Definition
ix.h:44
IX_VOICE_ACTIVE
@ IX_VOICE_ACTIVE
Definition
ix.h:42
IxStream
Definition
ix.h:198
IxStream::playPos
unsigned int playPos
Definition
ix.h:203
IxStream::buffer
unsigned char * buffer
Definition
ix.h:200
IxStream::pending
unsigned int pending
Definition
ix.h:204
IxStream::lockPtr
unsigned char * lockPtr
Definition
ix.h:205
IxStream::writePos
unsigned int writePos
Definition
ix.h:202
IxStream::flags
unsigned int flags
Definition
ix.h:199
IxStream::size
unsigned int size
Definition
ix.h:201
IxStream::lockLen
unsigned int lockLen
Definition
ix.h:206
IxVoice
Definition
ix.h:48
IxVoice::start
unsigned char * start
Definition
ix.h:51
IxVoice::rate
short rate
Definition
ix.h:54
IxVoice::cursor
unsigned char * cursor
Definition
ix.h:50
IxVoice::leftGain
short leftGain
Definition
ix.h:58
IxVoice::end
unsigned char * end
Definition
ix.h:52
IxVoice::field_10
short field_10
Definition
ix.h:53
IxVoice::rightGainHi
unsigned char rightGainHi
Definition
ix.h:57
IxVoice::rightGain
short rightGain
Definition
ix.h:59
IxVoice::flags
unsigned int flags
Definition
ix.h:49
IxVoice::leftGainHi
unsigned char leftGainHi
Definition
ix.h:56
IxVoice::volume
unsigned short volume
Definition
ix.h:55
src
ix
dsps.cpp
Generated by
1.18.0