aboutsummaryrefslogtreecommitdiff
path: root/shell/audio/sdl
diff options
context:
space:
mode:
Diffstat (limited to 'shell/audio/sdl')
-rw-r--r--shell/audio/sdl/sound_output.c19
1 files changed, 17 insertions, 2 deletions
diff --git a/shell/audio/sdl/sound_output.c b/shell/audio/sdl/sound_output.c
index f14bb1d..ae1ab83 100644
--- a/shell/audio/sdl/sound_output.c
+++ b/shell/audio/sdl/sound_output.c
@@ -1,11 +1,14 @@
#include <sys/ioctl.h>
#include <stdint.h>
+#include <stdbool.h>
#include <fcntl.h>
#include <unistd.h>
#include <SDL/SDL.h>
#include "sound_output.h"
+#define UNDERRUN_THRESHOLD 0.5
+
static int32_t BUFFSIZE;
static uint8_t *buffer;
static uint32_t buf_read_pos = 0;
@@ -36,14 +39,20 @@ static int32_t sdl_read_buffer(uint8_t* data, int32_t len)
static void sdl_write_buffer(uint8_t* data, int32_t len)
{
+ SDL_LockAudio();
for(uint32_t i = 0; i < len; i += 4)
{
- if(buffered_bytes == BUFFSIZE) return; // just drop samples
+ while (buffered_bytes == BUFFSIZE) {
+ SDL_UnlockAudio();
+ usleep(1000);
+ SDL_LockAudio();
+ }
*(int32_t*)((char*)(buffer + buf_write_pos)) = *(int32_t*)((char*)(data + i));
//memcpy(buffer + buf_write_pos, data + i, 4);
buf_write_pos = (buf_write_pos + 4) % BUFFSIZE;
buffered_bytes += 4;
}
+ SDL_UnlockAudio();
}
void sdl_callback(void *unused, uint8_t *stream, int32_t len)
@@ -90,9 +99,15 @@ uint32_t Audio_Init()
void Audio_Write(int16_t* restrict buffer, uint32_t buffer_size)
{
- SDL_LockAudio();
sdl_write_buffer(buffer, buffer_size * 4);
+}
+
+bool Audio_Underrun_Likely() {
+ bool underrun_likely = false;
+ SDL_LockAudio();
+ underrun_likely = buffered_bytes < BUFFSIZE * UNDERRUN_THRESHOLD;
SDL_UnlockAudio();
+ return underrun_likely;
}
void Audio_Close()