ddnet/src/engine/client/snd.c

442 lines
8.6 KiB
C
Raw Normal View History

2007-11-25 19:42:40 +00:00
/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */
#include <engine/system.h>
#include <engine/interface.h>
#include <engine/config.h>
2007-05-22 15:03:32 +00:00
#include <engine/external/portaudio/portaudio.h>
#include <engine/external/wavpack/wavpack.h>
2007-09-25 19:48:52 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
2007-08-09 14:55:11 +00:00
enum
{
2007-10-08 20:34:11 +00:00
NUM_SAMPLES = 512,
2007-09-25 19:48:52 +00:00
NUM_VOICES = 64,
2007-10-08 20:34:11 +00:00
NUM_CHANNELS = 16,
2007-09-25 19:48:52 +00:00
MAX_FRAMES = 1024
};
2007-10-08 20:34:11 +00:00
typedef struct
2007-09-25 19:48:52 +00:00
{
2007-05-22 15:03:32 +00:00
short *data;
2007-10-08 20:34:11 +00:00
int num_frames;
2007-05-22 15:03:32 +00:00
int rate;
int channels;
2007-09-25 19:48:52 +00:00
int loop_start;
int loop_end;
2007-10-08 20:34:11 +00:00
} SAMPLE;
2007-10-08 20:34:11 +00:00
typedef struct
{
2007-10-08 20:34:11 +00:00
int vol;
int pan;
} CHANNEL;
typedef struct VOICE_t
{
2007-10-08 20:34:11 +00:00
SAMPLE *snd;
CHANNEL *channel;
int tick;
int vol; /* 0 - 255 */
2007-09-25 19:48:52 +00:00
int flags;
2007-10-08 20:34:11 +00:00
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;
2007-11-04 21:36:03 +00:00
static int sound_enabled = 0;
2007-10-08 20:34:11 +00:00
static int center_x = 0;
static int center_y = 0;
static int mixing_rate = 48000;
void snd_set_channel(int cid, float vol, float pan)
2007-05-22 15:03:32 +00:00
{
2007-10-08 20:34:11 +00:00
channels[cid].vol = (int)(vol*255.0f);
2007-10-08 20:36:37 +00:00
channels[cid].pan = (int)(pan*255.0f); /* TODO: this is only on and off right now */
2007-09-25 19:48:52 +00:00
}
2007-10-08 20:34:11 +00:00
static int play(int cid, int sid, int flags, float x, float y)
2007-05-22 15:03:32 +00:00
{
2007-10-08 20:34:11 +00:00
int vid = -1;
int i;
lock_wait(sound_lock);
/* search for voice */
/* TODO: fix this linear search */
for(i = 0; i < NUM_VOICES; i++)
{
if(!voices[i].snd)
{
vid = i;
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;
2007-09-25 19:48:52 +00:00
}
2007-10-08 20:34:11 +00:00
int snd_play_at(int cid, int sid, int flags, float x, float y)
{
2007-10-08 20:34:11 +00:00
return play(cid, sid, flags|SNDFLAG_POS, x, y);
2007-09-25 19:48:52 +00:00
}
2007-05-22 15:03:32 +00:00
2007-10-08 20:34:11 +00:00
int snd_play(int cid, int sid, int flags)
{
2007-10-08 20:34:11 +00:00
return play(cid, sid, flags, 0, 0);
}
2007-07-21 16:45:06 +00:00
2007-10-08 20:34:11 +00:00
void snd_stop(int vid)
{
2007-10-08 20:34:11 +00:00
/* TODO: a nice fade out */
lock_wait(sound_lock);
voices[vid].snd = 0;
lock_release(sound_lock);
}
2007-07-21 16:45:06 +00:00
2007-10-08 20:36:37 +00:00
/* TODO: there should be a faster way todo this */
2007-10-08 20:34:11 +00:00
static short int2short(int i)
{
2007-10-08 20:34:11 +00:00
if(i > 0x7fff)
return 0x7fff;
else if(i < -0x7fff)
return -0x7fff;
return i;
}
2007-07-21 16:45:06 +00:00
2007-10-08 20:34:11 +00:00
static int iabs(int i)
{
if(i<0)
return -i;
return i;
}
2007-10-08 20:34:11 +00:00
static void mix(short *final_out, unsigned frames)
{
int mix_buffer[MAX_FRAMES*2] = {0};
int i, s;
2007-07-21 16:45:06 +00:00
2007-10-08 20:34:11 +00:00
/* aquire lock while we are mixing */
lock_wait(sound_lock);
for(i = 0; i < NUM_VOICES; i++)
{
2007-10-08 20:34:11 +00:00
if(voices[i].snd)
2007-07-21 16:45:06 +00:00
{
2007-10-08 20:34:11 +00:00
/* mix voice */
VOICE *v = &voices[i];
int *out = mix_buffer;
2007-10-08 20:34:11 +00:00
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;
2007-05-22 15:03:32 +00:00
2007-10-08 20:34:11 +00:00
int rvol = v->channel->vol;
int lvol = v->channel->vol;
2007-05-22 15:03:32 +00:00
2007-10-08 20:34:11 +00:00
/* 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;
2007-07-23 17:30:29 +00:00
2007-10-08 20:34:11 +00:00
/* volume calculation */
if(v->flags&SNDFLAG_POS && v->channel->pan)
{
2007-10-08 20:36:37 +00:00
/* TODO: we should respect the channel panning value */
2007-10-08 20:34:11 +00:00
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)
2007-09-25 19:48:52 +00:00
{
2007-10-08 20:34:11 +00:00
/* panning */
if(dx > 0)
lvol = ((range-p)*lvol)/range;
2007-09-25 19:48:52 +00:00
else
2007-10-08 20:34:11 +00:00
rvol = ((range-p)*rvol)/range;
/* falloff */
lvol = (lvol*(range-dist))/range;
rvol = (rvol*(range-dist))/range;
2007-09-25 19:48:52 +00:00
}
2007-12-11 22:03:17 +00:00
else
{
lvol = 0;
rvol = 0;
}
2007-09-25 19:48:52 +00:00
}
2007-10-08 20:34:11 +00:00
/* 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;
}
2007-05-22 15:03:32 +00:00
}
2007-10-08 20:34:11 +00:00
/* release the lock */
lock_release(sound_lock);
{
2007-12-10 19:13:00 +00:00
int master_vol = config.snd_volume;
/* 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;
2007-05-22 15:03:32 +00:00
2007-12-10 19:13:00 +00:00
final_out[j] = int2short(vl);
final_out[j+1] = int2short(vr);
}
}
}
2007-05-22 15:03:32 +00:00
static int pacallback(const void *in, void *out, unsigned long frames, const PaStreamCallbackTimeInfo* time, PaStreamCallbackFlags status, void *user)
{
2007-09-25 19:48:52 +00:00
mix(out, frames);
return 0;
}
2007-09-25 19:48:52 +00:00
static PaStream *stream;
int snd_init()
2007-05-22 15:03:32 +00:00
{
PaStreamParameters params;
PaError err = Pa_Initialize();
2007-10-08 20:34:11 +00:00
2007-10-03 21:32:02 +00:00
sound_lock = lock_create();
2007-11-04 21:36:03 +00:00
if(!config.snd_enable)
return 0;
mixing_rate = config.snd_rate;
2007-10-03 21:32:02 +00:00
params.device = Pa_GetDefaultOutputDevice();
if(params.device < 0)
return 1;
params.channelCount = 2;
params.sampleFormat = paInt16;
params.suggestedLatency = Pa_GetDeviceInfo(params.device)->defaultLowOutputLatency;
params.hostApiSpecificStreamInfo = 0x0;
err = Pa_OpenStream(
2007-09-25 19:48:52 +00:00
&stream, /* passes back stream pointer */
0, /* no input channels */
&params, /* pointer to parameters */
2007-10-08 20:34:11 +00:00
mixing_rate, /* sample rate */
2007-09-25 19:48:52 +00:00
128, /* frames per buffer */
paClipOff, /* no clamping */
pacallback, /* specify our custom callback */
0x0); /* pass our data through to callback */
err = Pa_StartStream(stream);
2007-09-25 19:48:52 +00:00
2007-11-04 21:36:03 +00:00
sound_enabled = 1;
2007-09-25 19:48:52 +00:00
return 0;
2007-05-22 15:03:32 +00:00
}
int snd_shutdown()
2007-05-22 15:03:32 +00:00
{
Pa_StopStream(stream);
2007-09-25 19:48:52 +00:00
Pa_Terminate();
2007-05-22 15:03:32 +00:00
lock_destroy(sound_lock);
2007-09-25 19:48:52 +00:00
return 0;
2007-07-21 17:03:27 +00:00
}
2007-09-25 19:48:52 +00:00
int snd_alloc_id()
2007-05-22 15:03:32 +00:00
{
2007-10-08 20:34:11 +00:00
/* TODO: linear search, get rid of it */
2007-09-25 19:48:52 +00:00
unsigned sid;
2007-10-08 20:34:11 +00:00
for(sid = 0; sid < NUM_SAMPLES; sid++)
2007-09-25 19:48:52 +00:00
{
2007-10-08 20:34:11 +00:00
if(samples[sid].data == 0x0)
2007-09-25 19:48:52 +00:00
return sid;
}
return -1;
2007-05-22 15:03:32 +00:00
}
2007-10-08 20:34:11 +00:00
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;
}
2007-08-09 14:55:11 +00:00
static FILE *file = NULL;
static int read_data(void *buffer, int size)
{
return fread(buffer, 1, size, file);
}
int snd_load_wv(const char *filename)
{
2007-10-08 20:34:11 +00:00
SAMPLE *snd;
2007-09-25 19:48:52 +00:00
int sid = -1;
2007-08-09 14:55:11 +00:00
char error[100];
2007-10-06 17:01:06 +00:00
WavpackContext *context;
/* don't waste memory on sound when we are stress testing */
if(config.stress)
return -1;
2007-11-04 21:36:03 +00:00
/* no need to load sound when we are running with no sound */
if(!sound_enabled)
return 1;
file = fopen(filename, "rb"); /* TODO: use system.h stuff for this */
if(!file)
{
dbg_msg("sound/wv", "failed to open %s", filename);
return -1;
}
2007-08-09 14:55:11 +00:00
2007-09-25 19:48:52 +00:00
sid = snd_alloc_id();
if(sid < 0)
return -1;
2007-10-08 20:34:11 +00:00
snd = &samples[sid];
2007-09-25 19:48:52 +00:00
2007-10-06 17:01:06 +00:00
context = WavpackOpenFileInput(read_data, error);
2007-08-09 14:55:11 +00:00
if (context)
{
int samples = WavpackGetNumSamples(context);
int bitspersample = WavpackGetBitsPerSample(context);
unsigned int samplerate = WavpackGetSampleRate(context);
int channels = WavpackGetNumChannels(context);
2007-10-06 17:01:06 +00:00
int *data;
int *src;
short *dst;
int i;
2007-08-09 14:55:11 +00:00
2007-09-25 19:48:52 +00:00
snd->channels = channels;
snd->rate = samplerate;
2007-08-09 14:55:11 +00:00
2007-09-25 19:48:52 +00:00
if(snd->channels > 2)
2007-08-09 14:55:11 +00:00
{
dbg_msg("sound/wv", "file is not mono or stereo. filename='%s'", filename);
return -1;
}
/*
2007-09-25 19:48:52 +00:00
if(snd->rate != 44100)
2007-08-09 14:55:11 +00:00
{
2007-09-25 19:48:52 +00:00
dbg_msg("sound/wv", "file is %d Hz, not 44100 Hz. filename='%s'", snd->rate, filename);
2007-08-09 14:55:11 +00:00
return -1;
}*/
2007-08-09 14:55:11 +00:00
if(bitspersample != 16)
{
dbg_msg("sound/wv", "bps is %d, not 16, filname='%s'", bitspersample, filename);
return -1;
}
2007-10-06 17:01:06 +00:00
data = (int *)mem_alloc(4*samples*channels, 1);
WavpackUnpackSamples(context, data, samples); /* TODO: check return value */
src = data;
2007-08-09 14:55:11 +00:00
2007-09-25 19:48:52 +00:00
snd->data = (short *)mem_alloc(2*samples*channels, 1);
2007-10-06 17:01:06 +00:00
dst = snd->data;
2007-08-09 14:55:11 +00:00
for (i = 0; i < samples*channels; i++)
2007-08-09 14:55:11 +00:00
*dst++ = (short)*src++;
mem_free(data);
2007-10-08 20:34:11 +00:00
snd->num_frames = samples;
2007-09-25 19:48:52 +00:00
snd->loop_start = -1;
snd->loop_end = -1;
2007-08-09 14:55:11 +00:00
}
else
{
dbg_msg("sound/wv", "failed to open %s: %s", filename, error);
}
fclose(file);
file = NULL;
2007-09-25 19:48:52 +00:00
if(config.debug)
dbg_msg("sound/wv", "loaded %s", filename);
2007-08-09 14:55:11 +00:00
2007-10-08 20:34:11 +00:00
rate_convert(sid);
2007-09-25 19:48:52 +00:00
return sid;
2007-08-09 14:55:11 +00:00
}
void snd_set_listener_pos(float x, float y)
{
2007-10-08 20:34:11 +00:00
center_x = (int)x;
center_y = (int)y;
}