Commit 448671a9 authored by Vladimir Serbinenko's avatar Vladimir Serbinenko
Browse files

Support other audio formats

parent e5a0489c
......@@ -15,18 +15,21 @@ typedef struct {
} sound_buf_t;
static sound_buf_t sound_buf;
static int io_sound_chans;
static SDL_sem * sem;
static void callback(void * dummy, Uint8 * outbuf, int len)
{
int len_samples = len / SAMPLE_SIZE;
int len_samples = len / SAMPLE_SIZE / io_sound_chans;
int normalized = sound_buf.outptr % SOUND_BUFSIZE;
int prewrap = SOUND_BUFSIZE - normalized;
if (prewrap > len_samples)
prewrap = len_samples;
memcpy(outbuf, &sound_buf.buf[normalized], prewrap * SAMPLE_SIZE);
memcpy(outbuf + prewrap * SAMPLE_SIZE, &sound_buf.buf[0],
(len_samples - prewrap) * SAMPLE_SIZE);
memcpy(outbuf, &sound_buf.buf[normalized * io_sound_chans],
prewrap * SAMPLE_SIZE * io_sound_chans);
memcpy(outbuf + prewrap * SAMPLE_SIZE * io_sound_chans, &sound_buf.buf[0],
(len_samples - prewrap) * SAMPLE_SIZE * io_sound_chans);
sound_buf.outptr += len_samples;
SDL_SemPost(sem);
}
......@@ -37,8 +40,6 @@ static void sound_finish() {
SDL_DestroySemaphore(sem);
}
static SDL_AudioSpec desired;
void platform_sound_flush() {
while (sound_buf.inptr - sound_buf.outptr > SOUND_BUFSIZE / 2) {
SDL_SemWait(sem);
......@@ -46,10 +47,12 @@ void platform_sound_flush() {
}
void sound_write_sample(short val) {
short * p = &sound_buf.buf[sound_buf.inptr++ % SOUND_BUFSIZE];
int i;
if (sound_buf.buf == NULL)
return;
*p = val;
for (i = 0; i < io_sound_chans; i++)
sound_buf.buf[(sound_buf.inptr % SOUND_BUFSIZE) * io_sound_chans + i] = val;
sound_buf.inptr++;
if (sound_buf.inptr >= sound_buf.outptr + SOUND_BUFSIZE) {
platform_sound_flush();
}
......@@ -73,22 +76,31 @@ void platform_sound_init() {
fprintf(stderr, _("Failed to initialize audio subsystem\n"));
}
desired.format = 16;
desired.channels = 1;
desired.freq = io_sound_freq;
desired.samples = SOUND_BUFSIZE / 2;
desired.callback = callback;
if (-1 == SDL_OpenAudio(&desired, 0)) {
SDL_AudioSpec desired = {
.format = 16,
.channels = 1,
.freq = io_sound_freq,
.samples = SOUND_BUFSIZE / 2,
.callback = callback
};
SDL_AudioSpec obtained;
if (-1 == SDL_OpenAudio(&desired, &obtained)) {
fprintf(stderr, _("Failed to initialize sound, freq %d, %d samples\n"), io_sound_freq, SOUND_BUFSIZE);
nflag = 0;
return;
}
fprintf (stderr, "Got %d channels, %d Hz, %d samples\n",
obtained.channels, obtained.freq, obtained.samples);
io_sound_freq = obtained.freq;
io_sound_chans = obtained.channels;
sem = SDL_CreateSemaphore(2);
sound_buf.inptr = 0;
sound_buf.outptr = 0;
sound_buf.buf = malloc(SOUND_BUFSIZE * SAMPLE_SIZE);
sound_buf.buf = malloc(SOUND_BUFSIZE * SAMPLE_SIZE * io_sound_chans);
if (!sound_buf.buf) {
fprintf(stderr, _("Failed to allocate sound buffers\n"));
exit(1);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment