dm: sandbox: Update sound to use two buffers
At present we use a single buffer for sound which means we cannot be playing one sound while queueing up the next. This wouldn't matter except that a long sound (more than a second) has to be created as a single buffer, thus using a lot of memory. To better mimic what real sound drivers do, add support for double buffering in sandbox. Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
parent
e96fa6c911
commit
e625b68b04
@ -13,6 +13,21 @@ enum {
|
|||||||
SAMPLE_RATE = 22050,
|
SAMPLE_RATE = 22050,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* struct buf_info - a data buffer holding audio data
|
||||||
|
*
|
||||||
|
* @pos: Current position playing in audio buffer
|
||||||
|
* @size: Size of data in audio buffer (0=empty)
|
||||||
|
* @alloced: Allocated size of audio buffer (max size it can hold)
|
||||||
|
* @data: Audio data
|
||||||
|
*/
|
||||||
|
struct buf_info {
|
||||||
|
uint pos;
|
||||||
|
uint size;
|
||||||
|
uint alloced;
|
||||||
|
uint8_t *data;
|
||||||
|
};
|
||||||
|
|
||||||
static struct sdl_info {
|
static struct sdl_info {
|
||||||
SDL_Surface *screen;
|
SDL_Surface *screen;
|
||||||
int width;
|
int width;
|
||||||
@ -20,12 +35,11 @@ static struct sdl_info {
|
|||||||
int depth;
|
int depth;
|
||||||
int pitch;
|
int pitch;
|
||||||
uint frequency;
|
uint frequency;
|
||||||
uint audio_pos;
|
|
||||||
uint audio_size;
|
|
||||||
uint sample_rate;
|
uint sample_rate;
|
||||||
uint8_t *audio_data;
|
|
||||||
bool audio_active;
|
bool audio_active;
|
||||||
bool inited;
|
bool inited;
|
||||||
|
int cur_buf;
|
||||||
|
struct buf_info buf[2];
|
||||||
} sdl;
|
} sdl;
|
||||||
|
|
||||||
static void sandbox_sdl_poll_events(void)
|
static void sandbox_sdl_poll_events(void)
|
||||||
@ -243,24 +257,37 @@ int sandbox_sdl_key_pressed(int keycode)
|
|||||||
|
|
||||||
void sandbox_sdl_fill_audio(void *udata, Uint8 *stream, int len)
|
void sandbox_sdl_fill_audio(void *udata, Uint8 *stream, int len)
|
||||||
{
|
{
|
||||||
|
struct buf_info *buf;
|
||||||
int avail;
|
int avail;
|
||||||
|
int i;
|
||||||
|
|
||||||
avail = sdl.audio_size - sdl.audio_pos;
|
for (i = 0; i < 2; i++) {
|
||||||
if (avail < len)
|
buf = &sdl.buf[sdl.cur_buf];
|
||||||
len = avail;
|
avail = buf->size - buf->pos;
|
||||||
|
if (avail <= 0) {
|
||||||
|
sdl.cur_buf = 1 - sdl.cur_buf;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (avail > len)
|
||||||
|
avail = len;
|
||||||
|
|
||||||
SDL_MixAudio(stream, sdl.audio_data + sdl.audio_pos, len,
|
SDL_MixAudio(stream, buf->data + buf->pos, avail,
|
||||||
SDL_MIX_MAXVOLUME);
|
SDL_MIX_MAXVOLUME);
|
||||||
sdl.audio_pos += len;
|
buf->pos += avail;
|
||||||
|
len -= avail;
|
||||||
|
|
||||||
/* Loop if we are at the end */
|
/* Move to next buffer if we are at the end */
|
||||||
if (sdl.audio_pos == sdl.audio_size)
|
if (buf->pos == buf->size)
|
||||||
sdl.audio_pos = 0;
|
buf->size = 0;
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int sandbox_sdl_sound_init(void)
|
int sandbox_sdl_sound_init(void)
|
||||||
{
|
{
|
||||||
SDL_AudioSpec wanted;
|
SDL_AudioSpec wanted;
|
||||||
|
int i;
|
||||||
|
|
||||||
if (sandbox_sdl_ensure_init())
|
if (sandbox_sdl_ensure_init())
|
||||||
return -1;
|
return -1;
|
||||||
@ -276,13 +303,20 @@ int sandbox_sdl_sound_init(void)
|
|||||||
wanted.callback = sandbox_sdl_fill_audio;
|
wanted.callback = sandbox_sdl_fill_audio;
|
||||||
wanted.userdata = NULL;
|
wanted.userdata = NULL;
|
||||||
|
|
||||||
sdl.audio_size = sizeof(uint16_t) * wanted.freq;
|
for (i = 0; i < 2; i++) {
|
||||||
sdl.audio_data = malloc(sdl.audio_size);
|
struct buf_info *buf = &sdl.buf[i];
|
||||||
if (!sdl.audio_data) {
|
|
||||||
printf("%s: Out of memory\n", __func__);
|
buf->alloced = sizeof(uint16_t) * wanted.freq * wanted.channels;
|
||||||
return -1;
|
buf->data = malloc(buf->alloced);
|
||||||
|
if (!buf->data) {
|
||||||
|
printf("%s: Out of memory\n", __func__);
|
||||||
|
if (i == 1)
|
||||||
|
free(sdl.buf[0].data);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
buf->pos = 0;
|
||||||
|
buf->size = 0;
|
||||||
}
|
}
|
||||||
sdl.audio_pos = 0;
|
|
||||||
|
|
||||||
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
|
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
|
||||||
printf("Unable to initialize SDL audio: %s\n", SDL_GetError());
|
printf("Unable to initialize SDL audio: %s\n", SDL_GetError());
|
||||||
@ -296,23 +330,27 @@ int sandbox_sdl_sound_init(void)
|
|||||||
}
|
}
|
||||||
sdl.audio_active = true;
|
sdl.audio_active = true;
|
||||||
sdl.sample_rate = wanted.freq;
|
sdl.sample_rate = wanted.freq;
|
||||||
|
sdl.cur_buf = 0;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
err:
|
err:
|
||||||
free(sdl.audio_data);
|
for (i = 0; i < 2; i++)
|
||||||
|
free(sdl.buf[i].data);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sandbox_sdl_sound_start(uint frequency)
|
int sandbox_sdl_sound_start(uint frequency)
|
||||||
{
|
{
|
||||||
|
struct buf_info *buf = &sdl.buf[0];
|
||||||
|
|
||||||
if (!sdl.audio_active)
|
if (!sdl.audio_active)
|
||||||
return -1;
|
return -1;
|
||||||
sdl.frequency = frequency;
|
sdl.frequency = frequency;
|
||||||
sound_create_square_wave(sdl.sample_rate,
|
sound_create_square_wave(sdl.sample_rate, (unsigned short *)buf->data,
|
||||||
(unsigned short *)sdl.audio_data,
|
buf->alloced, frequency);
|
||||||
sdl.audio_size, frequency);
|
buf->pos = 0;
|
||||||
sdl.audio_pos = 0;
|
buf->size = buf->alloced;
|
||||||
SDL_PauseAudio(0);
|
SDL_PauseAudio(0);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user