Subversion Repositories Games.Descent

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 pmbaty 1
/*
2
 * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3
 * It is copyright by its individual contributors, as recorded in the
4
 * project's Git history.  See COPYING.txt at the top level for license
5
 * terms and a link to the Git history.
6
 */
7
/*
8
 *
9
 * SDL digital audio support
10
 *
11
 *
12
 */
13
 
14
#include <stdlib.h>
15
#include <stdio.h>
16
#include <string.h>
17
#include <SDL.h>
18
#include <digi_audio.h>
19
#include "dxxerror.h"
20
#include "vecmat.h"
21
#include "digi.h"
22
#include "sounds.h"
23
#include "config.h"
24
#include "args.h"
25
#include "piggy.h"
26
 
27
#include "compiler-range_for.h"
28
 
29
namespace dcx {
30
 
31
//changed on 980905 by adb to increase number of concurrent sounds
32
//end changes by adb
33
#define SOUND_BUFFER_SIZE 1024
34
 
35
#define MIN_VOLUME 10
36
 
37
/* This table is used to add two sound values together and pin
38
 * the value to avoid overflow.  (used with permission from ARDI)
39
 * DPH: Taken from SDL/src/SDL_mixer.c.
40
 */
41
constexpr uint8_t mix8[] =
42
{
43
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
44
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
45
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
47
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
48
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
55
  0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
56
  0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
57
  0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24,
58
  0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
59
  0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A,
60
  0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,
61
  0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
62
  0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B,
63
  0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
64
  0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71,
65
  0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C,
66
  0x7D, 0x7E, 0x7F, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
67
  0x88, 0x89, 0x8A, 0x8B, 0x8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F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
83
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
84
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
85
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
86
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
87
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
88
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
89
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
90
};
91
 
92
static int digi_initialised = 0;
93
 
94
struct sound_slot {
95
        int soundno;
96
        bool playing;   // Is there a sample playing on this channel?
97
        bool looped;    // Play this sample looped?
98
        bool persistent; // This can't be pre-empted
99
        fix pan;       // 0 = far left, 1 = far right
100
        fix volume;    // 0 = nothing, 1 = fully on
101
        //changed on 980905 by adb from char * to unsigned char *
102
        unsigned char *samples;
103
        sound_object *soundobj;   // Which soundobject is on this channel
104
        //end changes by adb
105
        unsigned int length; // Length of the sample
106
        unsigned int position; // Position we are at at the moment.
107
};
108
 
109
static void digi_audio_stop_sound(sound_slot &s)
110
{
111
        s.playing = 0;
112
        s.soundobj = sound_object_none;
113
        s.persistent = 0;
114
}
115
 
116
static std::array<sound_slot, 32> SoundSlots;
117
 
118
}
119
 
120
namespace dsx {
121
 
122
static SDL_AudioSpec WaveSpec;
123
static int next_channel = 0;
124
 
125
/* Audio mixing callback */
126
//changed on 980905 by adb to cleanup, add pan support and optimize mixer
127
static void audio_mixcallback(void *, Uint8 *stream, int len)
128
{
129
        Uint8 *streamend = stream + len;
130
        if (!digi_initialised)
131
                return;
132
 
133
        memset(stream, 0x80, len); // fix "static" sound bug on Mac OS X
134
 
135
        SDL_LockAudio();
136
 
137
        range_for (auto &sl, SoundSlots)
138
        {
139
                if (sl.playing) {
140
                        Uint8 *sldata = sl.samples + sl.position, *slend = sl.samples + sl.length;
141
                        Uint8 *sp = stream, s;
142
                        signed char v;
143
                        fix vl, vr;
144
                        int x;
145
 
146
                        if ((x = sl.pan) & 0x8000) {
147
                                vl = 0x20000 - x * 2;
148
                                vr = 0x10000;
149
                        } else {
150
                                vl = 0x10000;
151
                                vr = x * 2;
152
                        }
153
                        vl = fixmul(vl, (x = sl.volume));
154
                        vr = fixmul(vr, x);
155
                        while (sp < streamend) {
156
                                if (sldata == slend) {
157
                                        if (!sl.looped) {
158
                                                sl.playing = 0;
159
                                                break;
160
                                        }
161
                                        sldata = sl.samples;
162
                                }
163
                                v = *(sldata++) - 0x80;
164
                                s = *sp;
165
                                *(sp++) = mix8[ s + fixmul(v, vl) + 0x80 ];
166
                                s = *sp;
167
                                *(sp++) = mix8[ s + fixmul(v, vr) + 0x80 ];
168
                        }
169
                        sl.position = sldata - sl.samples;
170
                }
171
        }
172
 
173
        SDL_UnlockAudio();
174
}
175
//end changes by adb
176
 
177
/* Initialise audio devices. */
178
int digi_audio_init()
179
{
180
        if (SDL_InitSubSystem(SDL_INIT_AUDIO)<0) {
181
                Error("SDL audio initialisation failed: %s.",SDL_GetError());
182
        }
183
 
184
#if defined(DXX_BUILD_DESCENT_I)
185
        WaveSpec.freq = digi_sample_rate;
186
#elif defined(DXX_BUILD_DESCENT_II)
187
        WaveSpec.freq = GameArg.SndDigiSampleRate;
188
#endif
189
        //added/changed by Sam Lantinga on 12/01/98 for new SDL version
190
        WaveSpec.format = AUDIO_U8;
191
        WaveSpec.channels = 2;
192
        //end this section addition/change - SL
193
        WaveSpec.samples = SOUND_BUFFER_SIZE;
194
        WaveSpec.callback = audio_mixcallback;
195
 
196
        if ( SDL_OpenAudio(&WaveSpec, NULL) < 0 ) {
197
                //edited on 10/05/98 by Matt Mueller - should keep running, just with no sound.
198
                Warning("\nError: Couldn't open audio: %s\n", SDL_GetError());
199
                //killed  exit(2);
200
                return 1;
201
                //end edit -MM
202
        }
203
        SDL_PauseAudio(0);
204
 
205
        digi_initialised = 1;
206
 
207
        digi_audio_set_digi_volume( (GameCfg.DigiVolume*32768)/8 );
208
        return 0;
209
}
210
 
211
/* Toggle audio */
212
void digi_audio_reset() { }
213
 
214
/* Shut down audio */
215
void digi_audio_close()
216
{
217
        if (!digi_initialised) return;
218
        digi_initialised = 0;
219
#ifdef __MINGW32__
220
        SDL_Delay(500); // CloseAudio hangs if it's called too soon after opening?
221
#endif
222
        SDL_CloseAudio();
223
}
224
 
225
void digi_audio_stop_all_channels()
226
{
227
        range_for (auto &i, SoundSlots)
228
                digi_audio_stop_sound(i);
229
}
230
 
231
 
232
// Volume 0-F1_0
233
int digi_audio_start_sound(short soundnum, fix volume, int pan, int looping, int, int, sound_object *const soundobj)
234
{
235
        int i, starting_channel;
236
 
237
        if (!digi_initialised) return -1;
238
 
239
        if (soundnum < 0) return -1;
240
 
241
        SDL_LockAudio();
242
 
243
        Assert(GameSounds[soundnum].data != reinterpret_cast<void *>(-1));
244
 
245
        starting_channel = next_channel;
246
 
247
        while(1)
248
        {
249
                if (!SoundSlots[next_channel].playing)
250
                        break;
251
 
252
                if (!SoundSlots[next_channel].persistent)
253
                        break;  // use this channel!
254
 
255
                next_channel++;
256
                if (next_channel >= digi_max_channels)
257
                        next_channel = 0;
258
                if (next_channel == starting_channel)
259
                {
260
                        SDL_UnlockAudio();
261
                        return -1;
262
                }
263
        }
264
        if (SoundSlots[next_channel].playing)
265
        {
266
                SoundSlots[next_channel].playing = 0;
267
                if (SoundSlots[next_channel].soundobj != sound_object_none)
268
                {
269
                        digi_end_soundobj(*SoundSlots[next_channel].soundobj);
270
                }
271
                if (SoundQ_channel == next_channel)
272
                        SoundQ_end();
273
        }
274
 
275
#ifndef NDEBUG
276
        verify_sound_channel_free(next_channel);
277
#endif
278
 
279
        SoundSlots[next_channel].soundno = soundnum;
280
        SoundSlots[next_channel].samples = GameSounds[soundnum].data;
281
        SoundSlots[next_channel].length = GameSounds[soundnum].length;
282
        SoundSlots[next_channel].volume = fixmul(digi_volume, volume);
283
        SoundSlots[next_channel].pan = pan;
284
        SoundSlots[next_channel].position = 0;
285
        SoundSlots[next_channel].looped = looping;
286
        SoundSlots[next_channel].playing = 1;
287
        SoundSlots[next_channel].soundobj = soundobj;
288
        SoundSlots[next_channel].persistent = 0;
289
        if (soundobj || looping || volume > F1_0)
290
                SoundSlots[next_channel].persistent = 1;
291
 
292
        i = next_channel;
293
        next_channel++;
294
        if (next_channel >= digi_max_channels)
295
                next_channel = 0;
296
 
297
        SDL_UnlockAudio();
298
 
299
        return i;
300
}
301
 
302
//added on 980905 by adb from original source to make sfx volume work
303
void digi_audio_set_digi_volume( int dvolume )
304
{
305
        dvolume = fixmuldiv( dvolume, SOUND_MAX_VOLUME, 0x7fff);
306
        if ( dvolume > SOUND_MAX_VOLUME )
307
                digi_volume = SOUND_MAX_VOLUME;
308
        else if ( dvolume < 0 )
309
                digi_volume = 0;
310
        else
311
                digi_volume = dvolume;
312
 
313
        if ( !digi_initialised ) return;
314
 
315
        digi_sync_sounds();
316
}
317
//end edit by adb
318
 
319
int digi_audio_is_channel_playing(int channel)
320
{
321
        if (!digi_initialised)
322
                return 0;
323
 
324
        return SoundSlots[channel].playing;
325
}
326
 
327
void digi_audio_set_channel_volume(int channel, int volume)
328
{
329
        if (!digi_initialised)
330
                return;
331
 
332
        if (!SoundSlots[channel].playing)
333
                return;
334
 
335
        SoundSlots[channel].volume = fixmuldiv(volume, digi_volume, F1_0);
336
}
337
 
338
void digi_audio_set_channel_pan(int channel, int pan)
339
{
340
        if (!digi_initialised)
341
                return;
342
 
343
        if (!SoundSlots[channel].playing)
344
                return;
345
 
346
        SoundSlots[channel].pan = pan;
347
}
348
 
349
void digi_audio_stop_sound(int channel)
350
{
351
        digi_audio_stop_sound(SoundSlots[channel]);
352
}
353
 
354
void digi_audio_end_sound(int channel)
355
{
356
        if (!digi_initialised)
357
                return;
358
 
359
        if (!SoundSlots[channel].playing)
360
                return;
361
 
362
        SoundSlots[channel].soundobj = sound_object_none;
363
        SoundSlots[channel].persistent = 0;
364
}
365
 
366
}