1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
|
/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */
#include <base/system.h>
#include <engine/e_client_interface.h>
#include <engine/e_config.h>
#include <engine/e_engine.h>
#include "SDL.h"
#include <engine/external/wavpack/wavpack.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
enum
{
NUM_SAMPLES = 512,
NUM_VOICES = 64,
NUM_CHANNELS = 16,
MAX_FRAMES = 1024
};
typedef struct
{
short *data;
int num_frames;
int rate;
int channels;
int loop_start;
int loop_end;
} SAMPLE;
typedef struct
{
int vol;
int pan;
} CHANNEL;
typedef struct
{
SAMPLE *snd;
CHANNEL *channel;
int tick;
int vol; /* 0 - 255 */
int flags;
int x, y;
} VOICE;
static SAMPLE samples[NUM_SAMPLES] = { {0} };
static VOICE voices[NUM_VOICES] = { {0} };
static CHANNEL channels[NUM_CHANNELS] = { {255, 0} };
static LOCK sound_lock = 0;
static int sound_enabled = 0;
static int center_x = 0;
static int center_y = 0;
static int mixing_rate = 48000;
static volatile int sound_volume = 100;
static int next_voice = 0;
void snd_set_channel(int cid, float vol, float pan)
{
channels[cid].vol = (int)(vol*255.0f);
channels[cid].pan = (int)(pan*255.0f); /* TODO: this is only on and off right now */
}
static int play(int cid, int sid, int flags, float x, float y)
{
int vid = -1;
int i;
lock_wait(sound_lock);
/* search for voice */
for(i = 0; i < NUM_VOICES; i++)
{
int id = (next_voice + i) % NUM_VOICES;
if(!voices[id].snd)
{
vid = id;
next_voice = id+1;
break;
}
}
/* voice found, use it */
if(vid != -1)
{
voices[vid].snd = &samples[sid];
voices[vid].channel = &channels[cid];
voices[vid].tick = 0;
voices[vid].vol = 255;
voices[vid].flags = flags;
voices[vid].x = (int)x;
voices[vid].y = (int)y;
}
lock_release(sound_lock);
return vid;
}
int snd_play_at(int cid, int sid, int flags, float x, float y)
{
return play(cid, sid, flags|SNDFLAG_POS, x, y);
}
int snd_play(int cid, int sid, int flags)
{
return play(cid, sid, flags, 0, 0);
}
void snd_stop(int vid)
{
/* TODO: a nice fade out */
lock_wait(sound_lock);
voices[vid].snd = 0;
lock_release(sound_lock);
}
/* TODO: there should be a faster way todo this */
static short int2short(int i)
{
if(i > 0x7fff)
return 0x7fff;
else if(i < -0x7fff)
return -0x7fff;
return i;
}
static int iabs(int i)
{
if(i<0)
return -i;
return i;
}
static void mix(short *final_out, unsigned frames)
{
int mix_buffer[MAX_FRAMES*2] = {0};
int i, s;
int master_vol;
/* aquire lock while we are mixing */
lock_wait(sound_lock);
master_vol = sound_volume;
for(i = 0; i < NUM_VOICES; i++)
{
if(voices[i].snd)
{
/* mix voice */
VOICE *v = &voices[i];
int *out = mix_buffer;
int step = v->snd->channels; /* setup input sources */
short *in_l = &v->snd->data[v->tick*step];
short *in_r = &v->snd->data[v->tick*step+1];
int end = v->snd->num_frames-v->tick;
int rvol = v->channel->vol;
int lvol = v->channel->vol;
/* make sure that we don't go outside the sound data */
if(frames < end)
end = frames;
/* check if we have a mono sound */
if(v->snd->channels == 1)
in_r = in_l;
/* volume calculation */
if(v->flags&SNDFLAG_POS && v->channel->pan)
{
/* TODO: we should respect the channel panning value */
const int range = 1500; /* magic value, remove */
int dx = v->x - center_x;
int dy = v->y - center_y;
int dist = sqrt(dx*dx+dy*dy); /* double here. nasty */
int p = iabs(dx);
if(dist < range)
{
/* panning */
if(dx > 0)
lvol = ((range-p)*lvol)/range;
else
rvol = ((range-p)*rvol)/range;
/* falloff */
lvol = (lvol*(range-dist))/range;
rvol = (rvol*(range-dist))/range;
}
else
{
lvol = 0;
rvol = 0;
}
}
/* process all frames */
for(s = 0; s < end; s++)
{
*out++ += (*in_l)*lvol;
*out++ += (*in_r)*rvol;
in_l += step;
in_r += step;
v->tick++;
}
/* free voice if not used any more */
if(v->tick == v->snd->num_frames)
v->snd = 0;
}
}
/* release the lock */
lock_release(sound_lock);
{
/* clamp accumulated values */
/* TODO: this seams slow */
for(i = 0; i < frames; i++)
{
int j = i<<1;
int vl = ((mix_buffer[j]*master_vol)/101)>>8;
int vr = ((mix_buffer[j+1]*master_vol)/101)>>8;
final_out[j] = int2short(vl);
final_out[j+1] = int2short(vr);
}
}
#if defined(CONF_ARCH_ENDIAN_BIG)
swap_endian(final_out, sizeof(short), frames * 2);
#endif
}
static void sdlcallback(void *unused, Uint8 *stream, int len)
{
mix((short *)stream, len/2/2);
}
int snd_init()
{
SDL_AudioSpec format;
sound_lock = lock_create();
if(!config.snd_enable)
return 0;
mixing_rate = config.snd_rate;
/* Set 16-bit stereo audio at 22Khz */
format.freq = config.snd_rate;
format.format = AUDIO_S16;
format.channels = 2;
format.samples = config.snd_buffer_size;
format.callback = sdlcallback;
format.userdata = NULL;
/* Open the audio device and start playing sound! */
if(SDL_OpenAudio(&format, NULL) < 0)
{
dbg_msg("client/sound", "unable to open audio: %s", SDL_GetError());
return -1;
}
else
dbg_msg("client/sound", "sound init successful");
SDL_PauseAudio(0);
sound_enabled = 1;
snd_update(); /* update the volume */
return 0;
}
int snd_update()
{
/* update volume */
int wanted_volume = config.snd_volume;
if(!gfx_window_active() && config.snd_nonactive_mute)
wanted_volume = 0;
if(wanted_volume != sound_volume)
{
lock_wait(sound_lock);
sound_volume = wanted_volume;
lock_release(sound_lock);
}
return 0;
}
int snd_shutdown()
{
SDL_CloseAudio();
lock_destroy(sound_lock);
return 0;
}
int snd_alloc_id()
{
/* TODO: linear search, get rid of it */
unsigned sid;
for(sid = 0; sid < NUM_SAMPLES; sid++)
{
if(samples[sid].data == 0x0)
return sid;
}
return -1;
}
static void rate_convert(int sid)
{
SAMPLE *snd = &samples[sid];
int num_frames = 0;
short *new_data = 0;
int i;
/* make sure that we need to convert this sound */
if(!snd->data || snd->rate == mixing_rate)
return;
/* allocate new data */
num_frames = (int)((snd->num_frames/(float)snd->rate)*mixing_rate);
new_data = mem_alloc(num_frames*snd->channels*sizeof(short), 1);
for(i = 0; i < num_frames; i++)
{
/* resample TODO: this should be done better, like linear atleast */
float a = i/(float)num_frames;
int f = (int)(a*snd->num_frames);
if(f >= snd->num_frames)
f = snd->num_frames-1;
/* set new data */
if(snd->channels == 1)
new_data[i] = snd->data[f];
else if(snd->channels == 2)
{
new_data[i*2] = snd->data[f*2];
new_data[i*2+1] = snd->data[f*2+1];
}
}
/* free old data and apply new */
mem_free(snd->data);
snd->data = new_data;
snd->num_frames = num_frames;
}
static IOHANDLE file = NULL;
static int read_data(void *buffer, int size)
{
return io_read(file, buffer, size);
}
int snd_load_wv(const char *filename)
{
SAMPLE *snd;
int sid = -1;
char error[100];
WavpackContext *context;
/* don't waste memory on sound when we are stress testing */
if(config.dbg_stress)
return -1;
/* no need to load sound when we are running with no sound */
if(!sound_enabled)
return 1;
file = engine_openfile(filename, IOFLAG_READ); /* TODO: use system.h stuff for this */
if(!file)
{
dbg_msg("sound/wv", "failed to open %s", filename);
return -1;
}
sid = snd_alloc_id();
if(sid < 0)
return -1;
snd = &samples[sid];
context = WavpackOpenFileInput(read_data, error);
if (context)
{
int samples = WavpackGetNumSamples(context);
int bitspersample = WavpackGetBitsPerSample(context);
unsigned int samplerate = WavpackGetSampleRate(context);
int channels = WavpackGetNumChannels(context);
int *data;
int *src;
short *dst;
int i;
snd->channels = channels;
snd->rate = samplerate;
if(snd->channels > 2)
{
dbg_msg("sound/wv", "file is not mono or stereo. filename='%s'", filename);
return -1;
}
/*
if(snd->rate != 44100)
{
dbg_msg("sound/wv", "file is %d Hz, not 44100 Hz. filename='%s'", snd->rate, filename);
return -1;
}*/
if(bitspersample != 16)
{
dbg_msg("sound/wv", "bps is %d, not 16, filname='%s'", bitspersample, filename);
return -1;
}
data = (int *)mem_alloc(4*samples*channels, 1);
WavpackUnpackSamples(context, data, samples); /* TODO: check return value */
src = data;
snd->data = (short *)mem_alloc(2*samples*channels, 1);
dst = snd->data;
for (i = 0; i < samples*channels; i++)
*dst++ = (short)*src++;
mem_free(data);
snd->num_frames = samples;
snd->loop_start = -1;
snd->loop_end = -1;
}
else
{
dbg_msg("sound/wv", "failed to open %s: %s", filename, error);
}
io_close(file);
file = NULL;
if(config.debug)
dbg_msg("sound/wv", "loaded %s", filename);
rate_convert(sid);
return sid;
}
void snd_set_listener_pos(float x, float y)
{
center_x = (int)x;
center_y = (int)y;
}
|