-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathi_sound.c
669 lines (544 loc) · 15.4 KB
/
i_sound.c
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
#include "doomdef.h"
#include "z_zone.h"
#include "i_system.h"
#include "i_sound.h"
#include "m_argv.h"
#include "m_misc.h"
#include "w_wad.h"
#include "st_stuff.h"
#ifdef LINUX
#include <SDL/SDL.h>
#endif
// UNIX hack, to be removed.
#ifdef SNDSERV
// Separate sound server process.
FILE* sndserver=0;
char* sndserver_filename = "./sndserver ";
#elif SNDINTR
// Update all 30 millisecs, approx. 30fps synchronized.
// Linux resolution is allegedly 10 millisecs,
// scale is microseconds.
#define SOUND_INTERVAL 500
// Get the interrupt. Set duration in millisecs.
int I_SoundSetTimer( int duration_of_tick );
void I_SoundDelTimer( void );
#else
// None?
#endif
// A quick hack to establish a protocol between
// synchronous mix buffer updates and asynchronous
// audio writes. Probably redundant with gametic.
static int flag = 0;
// The number of internal mixing channels,
// the samples calculated for each mixing step,
// the size of the 16bit, 2 hardware channel (stereo)
// mixing buffer, and the samplerate of the raw data.
// Needed for calling the actual sound output.
#define SAMPLECOUNT 512
// It is 2 for 16bit, and 2 for two channels.
#define BUFMUL 4
#define MIXBUFFERSIZE (SAMPLECOUNT*BUFMUL)
#define SAMPLERATE 48000 // Hz
#define SAMPLESIZE 2 // 16bit
// The actual lengths of all sound effects.
int lengths[NUMSFX];
uint16_t rates[NUMSFX];
// The actual output device.
int audio_fd;
// The channel step amount...
int channelstep[NUM_SFX_CHANNELS];
// The channel data pointers, start and end.
unsigned char* channels[NUM_SFX_CHANNELS];
unsigned char* channelsend[NUM_SFX_CHANNELS];
// Time/gametic that the channel started playing,
// used to determine oldest, which automatically
// has lowest priority.
// In case number of active sounds exceeds
// available channels.
int channelstart[NUM_SFX_CHANNELS];
// SFX id of the playing sound effect.
// Used to catch duplicates (like chainsaw).
int channelids[NUM_SFX_CHANNELS];
// Pitch to stepping lookup, unused.
int steptable[256];
// Volume lookups.
int vol_lookup[128*256];
// Hardware left and right channel volume lookup.
int* channelleftvol_lookup[NUM_SFX_CHANNELS];
int* channelrightvol_lookup[NUM_SFX_CHANNELS];
#ifndef LINUX
#define num_frames_per_chunk (48000 / 35)
static const size_t chunk_size = ((num_frames_per_chunk * sizeof(uint32_t)) + 0xfff) & ~0xfff;
static uint32_t __attribute__((aligned(0x1000))) chunks[2][chunk_size / sizeof(uint32_t)];
static audio_output_t output;
static audio_output_buffer_t buffers[2];
static handle_t event;
#endif
//
// This function loads the sound data from the WAD lump,
// for single sound.
//
void*
getsfx
( char* sfxname,
int* len )
{
unsigned char* sfx;
unsigned char* paddedsfx;
int i;
int size;
int paddedsize;
char name[20];
int sfxlump;
// Get the sound data from the WAD, allocate lump
// in zone memory.
sprintf(name, "ds%s", sfxname);
// Now, there is a severe problem with the
// sound handling, in it is not (yet/anymore)
// gamemode aware. That means, sounds from
// DOOM II will be requested even with DOOM
// shareware.
// The sound list is wired into sounds.c,
// which sets the external variable.
// I do not do runtime patches to that
// variable. Instead, we will use a
// default sound for replacement.
if ( W_CheckNumForName(name) == -1 )
sfxlump = W_GetNumForName("dspistol");
else
sfxlump = W_GetNumForName(name);
size = W_LumpLength( sfxlump );
// Debug.
// fprintf( stderr, "." );
//fprintf( stderr, " -loading %s (lump %d, %d bytes)\n",
// sfxname, sfxlump, size );
//fflush( stderr );
sfx = (unsigned char*)W_CacheLumpNum( sfxlump);
// Pads the sound effect out to the mixing buffer size.
// The original realloc would interfere with zone memory.
paddedsize = ((size-8 + (SAMPLECOUNT-1)) / SAMPLECOUNT) * SAMPLECOUNT;
// Allocate from zone memory.
paddedsfx = (unsigned char*)Z_Malloc( paddedsize+8, PU_STATIC, 0 );
// ddt: (unsigned char *) realloc(sfx, paddedsize+8);
// This should interfere with zone memory handling,
// which does not kick in in the soundserver.
// Now copy and pad.
memcpy( paddedsfx, sfx, size );
for (i=size ; i<paddedsize+8 ; i++)
paddedsfx[i] = 128;
// Remove the cached lump.
Z_Free( sfx );
// Preserve padded length.
*len = paddedsize;
// Return allocated padded data.
return (void *) (paddedsfx + 8);
}
//
// SFX API
// Note: this was called by S_Init.
// However, whatever they did in the
// old DPMS based DOS version, this
// were simply dummies in the Linux
// version.
// See soundserver initdata().
//
void I_SetChannels()
{
// Init internal lookups (raw data, mixing buffer, channels).
// This function sets up internal lookups used during
// the mixing process.
int i;
int j;
// int* steptablemid = steptable + 128;
// Okay, reset internal mixing channels to zero.
/*for (i=0; i<NUM_CHANNELS; i++)
{
channels[i] = 0;
}*/
// This table provides step widths for pitch parameters.
// I fail to see that this is currently used.
// for (i=-128 ; i<128 ; i++)
// steptablemid[i] = (int)(pow(2.0, (i/64.0))*65536.0);
// steptablemid[i] = (int)(((i/64.0)*(i/64.0))*65536.0);
// Generates volume lookup tables
// which also turn the unsigned samples
// into signed samples.
for (i=0 ; i<128 ; i++)
for (j=0 ; j<256 ; j++)
vol_lookup[i*256+j] = (i*(j-128)*256)/127;
}
void I_SetSfxVolume(int volume)
{
// Identical to DOS.
// Basically, this should propagate
// the menu/config file setting
// to the state variable used in
// the mixing.
snd_SfxVolume = volume;
}
// MUSIC API - dummy. Some code from DOS version.
void I_SetMusicVolume(int volume)
{
// Internal state variable.
snd_MusicVolume = volume;
// Now set volume on output device.
// Whatever( snd_MusciVolume );
}
//
// Retrieve the raw data lump index
// for a given SFX name.
//
int I_GetSfxLumpNum(sfxinfo_t* sfx)
{
char namebuf[9];
sprintf(namebuf, "ds%s", sfx->name);
return W_GetNumForName(namebuf);
}
//
// Starting a sound means adding it
// to the current list of active sounds
// in the internal channels.
// As the SFX info struct contains
// e.g. a pointer to the raw data,
// it is ignored.
// As our sound handling does not handle
// priority, it is ignored.
// Pitching (that is, increased speed of playback)
// is set, but currently not used by mixing.
//
int
I_StartSound
( int sfxid,
int volume,
int seperation,
int pitch,
int priority,
int slot )
{
// set slot sound
int i;
int rc = -1;
int oldest = gametic;
int oldestnum = 0;
int rightvol;
int leftvol;
// Okay, in the less recent channel,
// we will handle the new SFX.
// Set pointer to raw data.
channels[slot] = (unsigned char *)S_sfx[sfxid].data + 16;
// Set pointer to end of raw data.
channelsend[slot] = channels[slot] + lengths[sfxid];
// [kg] support multiple rates
channelstep[slot] = 0;
// Should be gametic, I presume.
channelstart[slot] = gametic;
// Separation, that is, orientation/stereo.
// range is: 1 - 256
seperation += 1;
// Per left/right channel.
// x^2 seperation,
// adjust volume properly.
leftvol =
volume - ((volume*seperation*seperation) >> 16); ///(256*256);
seperation = seperation - 257;
rightvol =
volume - ((volume*seperation*seperation) >> 16);
// Sanity check, clamp volume.
if(rightvol < 0)
rightvol = 0;
if(rightvol > 127)
rightvol = 127;
if(leftvol < 0)
leftvol = 0;
if(leftvol > 127)
leftvol = 127;
// Get the proper lookup table piece
// for this volume level???
channelleftvol_lookup[slot] = &vol_lookup[leftvol*256];
channelrightvol_lookup[slot] = &vol_lookup[rightvol*256];
// Preserve sound SFX id,
// e.g. for avoiding duplicates of chainsaw.
channelids[slot] = sfxid;
return slot;
}
void I_StopSound(int slot)
{
channels[slot] = channelsend[slot];
}
int I_SoundIsPlaying(int slot)
{
if(!channels[slot])
return 0;
return channels[slot] != channelsend[slot];
}
//
// This function loops all active (internal) sound
// channels, retrieves a given number of samples
// from the raw sound data, modifies it according
// to the current (internal) channel parameters,
// mixes the per channel samples into the global
// mixbuffer, clamping it to the allowed range,
// and sets up everything for transferring the
// contents of the mixbuffer to the (two)
// hardware channels (left and right, that is).
//
// This function currently supports only 16bit.
//
#ifdef LINUX
void SND_Mix(void *unused, int16_t *mixbuffer, int len)
{
int samples = len / BUFMUL;
#else
void SND_Mix(void *unused, int16_t *mixbuffer, int samples)
{
#endif
// Mix current sound data.
// Data, from raw sound, for right and left.
register unsigned int sample;
register int dl;
register int dr;
// Pointers in global mixbuffer, left, right, end.
signed short* leftout;
signed short* rightout;
signed short* leftend;
// Step in mixbuffer, left and right, thus two.
int step;
// Mixing channel index.
int chan;
// Left and right channel
// are in global mixbuffer, alternating.
leftout = mixbuffer;
rightout = mixbuffer+1;
// Determine end, for left channel only
// (right channel is implicit).
leftend = mixbuffer + samples*2;
// [kg] normal or half speed
if(!netgame && (in_weapon_menu || players[consoleplayer].cheats & CF_SLOWMO))
step = 1; // half
else
step = 0; // normal
// Mix sounds into the mixing buffer.
// Loop over step*SAMPLECOUNT,
// that is 512 values for two channels.
while (leftout != leftend)
{
// Reset left/right value.
dl = 0;
dr = 0;
// Love thy L2 chache - made this a loop.
// Now more channels could be set at compile time
// as well. Thus loop those channels.
for ( chan = 0; chan < NUM_SFX_CHANNELS; chan++ )
{
// Check channel, if active.
if (channels[ chan ])
{
// Get the raw data from the channel.
sample = *channels[ chan ];
// Add left and right part
// for this channel (sound)
// to the current data.
// Adjust volume accordingly.
dl += channelleftvol_lookup[ chan ][sample] * 2;
dr += channelrightvol_lookup[ chan ][sample] * 2;
channelstep[chan] += rates[channelids[chan]] >> step;
if(channelstep[chan] >= SAMPLERATE)
{
channelstep[chan] -= SAMPLERATE;
channels[chan]++;
}
// Check whether we are done.
if (channels[ chan ] >= channelsend[ chan ])
channels[ chan ] = 0;
}
}
// Clamp to range. Left hardware channel.
// Has been char instead of short.
// if (dl > 127) *leftout = 127;
// else if (dl < -128) *leftout = -128;
// else *leftout = dl;
if (dl > 0x7fff)
*leftout = 0x7fff;
else if (dl < -0x8000)
*leftout = -0x8000;
else
*leftout = dl;
// Same for right hardware channel.
if (dr > 0x7fff)
*rightout = 0x7fff;
else if (dr < -0x8000)
*rightout = -0x8000;
else
*rightout = dr;
// Increment current pointers in mixbuffer.
leftout += 2;
rightout += 2;
}
}
void
I_UpdateSoundParams
( int slot,
int volume,
int seperation,
int pitch)
{
int rightvol;
int leftvol;
// Separation, that is, orientation/stereo.
// range is: 1 - 256
seperation += 1;
// Per left/right channel.
// x^2 seperation,
// adjust volume properly.
leftvol =
volume - ((volume*seperation*seperation) >> 16); ///(256*256);
seperation = seperation - 257;
rightvol =
volume - ((volume*seperation*seperation) >> 16);
// Sanity check, clamp volume.
if (rightvol < 0 || rightvol > 127)
I_Error("rightvol out of bounds");
if (leftvol < 0 || leftvol > 127)
I_Error("leftvol out of bounds");
// Get the proper lookup table piece
// for this volume level???
channelleftvol_lookup[slot] = &vol_lookup[leftvol*256];
channelrightvol_lookup[slot] = &vol_lookup[rightvol*256];
}
void I_ShutdownSound(void)
{
#ifndef LINUX
audio_ipc_output_stop(&output);
audio_ipc_output_close(&output);
audio_ipc_finalize();
#endif
}
#ifndef LINUX
void I_UpdateSound()
{
audio_output_buffer_t *released;
uint32_t num;
result_t r;
r = svcWaitSynchronization(&num, &event, 1, 0);
if(r)
return;
svcResetSignal(event);
while(1)
{
r = audio_ipc_output_get_released_buffer(&output, &num, &released);
if(r)
I_Error("I_UpdateSound: audio_ipc_output_get_released_buffer failed 0x%08X", r);
if(!released)
break;
SND_Mix(NULL, released->sample_data, num_frames_per_chunk);
released->data_size = num_frames_per_chunk * sizeof(uint32_t);
r = audio_ipc_output_append_buffer(&output, released);
if(r)
I_Error("I_UpdateSound: audio_ipc_output_append_buffer failed 0x%08X", r);
}
}
#endif
void
I_InitSound()
{
#ifdef LINUX
SDL_AudioSpec fmt;
fmt.freq = SAMPLERATE;
fmt.format = AUDIO_S16;
fmt.channels = 2;
fmt.samples = SAMPLECOUNT;
fmt.callback = (void*)SND_Mix;
fmt.userdata = NULL;
if(SDL_OpenAudio(&fmt, NULL) < 0)
{
printf("SOUND: audio open failed\n");
return;
}
SDL_PauseAudio(0);
#else
result_t r;
int i;
char names[8][0x20];
uint32_t num_names;
r = audio_ipc_init();
if(r)
I_Error("I_InitSound: audio_ipc_init failed 0x%08X", r);
r = audio_ipc_list_outputs(&names[0], 8, &num_names);
if(r)
I_Error("I_InitSound: audio_ipc_list_outputs failed 0x%08X", r);
printf("I_InitSound: got %i output(s)\n", num_names);
r = audio_ipc_open_output(names[0], &output);
if(r)
I_Error("I_InitSound: audio_ipc_open_output failed 0x%08X", r);
if(output.sample_rate != SAMPLERATE || output.sample_format != PCM_INT16 || output.num_channels != 2)
I_Error("I_InitSound: rate %i channels %i format %i is not supported\n", output.sample_rate, output.num_channels, output.sample_format);
r = audio_ipc_output_register_buffer_event(&output, &event);
if(r)
I_Error("I_InitSound: audio_ipc_output_register_buffer_event failed 0x%08X", r);
for(int i = 0; i < 2; i++)
{
buffers[i].ptr = &buffers[i].sample_data;
buffers[i].sample_data = chunks[i];
buffers[i].buffer_size = sizeof(chunks[i]);
buffers[i].data_size = num_frames_per_chunk * sizeof(uint32_t);
buffers[i].unknown = 0;
SND_Mix(NULL, buffers[i].sample_data, num_frames_per_chunk);
r = audio_ipc_output_append_buffer(&output, &buffers[i]);
if(r)
I_Error("I_InitSound: audio_ipc_output_append_buffer %i failed 0x%08X", i, r);
}
r = audio_ipc_output_start(&output);
if(r)
I_Error("I_InitSound: audio_ipc_output_start failed 0x%08X", r);
#endif
}
//
// MUSIC API.
// Still no music done.
// Remains. Dummies.
//
void I_InitMusic(void) { }
void I_ShutdownMusic(void) { }
static int looping=0;
static int musicdies=-1;
void I_PlaySong(int handle, int looping)
{
// UNUSED.
handle = looping = 0;
musicdies = gametic + TICRATE*30;
}
void I_PauseSong (int handle)
{
// UNUSED.
handle = 0;
}
void I_ResumeSong (int handle)
{
// UNUSED.
handle = 0;
}
void I_StopSong(int handle)
{
// UNUSED.
handle = 0;
looping = 0;
musicdies = 0;
}
void I_UnRegisterSong(int handle)
{
// UNUSED.
handle = 0;
}
int I_RegisterSong(void* data)
{
// UNUSED.
data = NULL;
return 1;
}
// Is the song playing?
int I_QrySongPlaying(int handle)
{
// UNUSED.
handle = 0;
return looping || musicdies > gametic;
}