summaryrefslogtreecommitdiff
path: root/sound.c
diff options
context:
space:
mode:
Diffstat (limited to 'sound.c')
-rw-r--r--sound.c51
1 files changed, 50 insertions, 1 deletions
diff --git a/sound.c b/sound.c
index 5593bbe..4794ce2 100644
--- a/sound.c
+++ b/sound.c
@@ -19,7 +19,9 @@
#include "common.h"
+#ifndef __LIBRETRO__
#include <SDL.h>
+#endif
u32 global_enable_audio = 1;
direct_sound_struct direct_sound_channel[2];
@@ -27,8 +29,10 @@ gbc_sound_struct gbc_sound_channel[4];
u32 sound_frequency = 44100;
+#ifndef __LIBRETRO__
SDL_mutex *sound_mutex;
static SDL_cond *sound_cv;
+#endif
#ifdef PSP_BUILD
u32 audio_buffer_size_number = 1;
@@ -44,7 +48,9 @@ static u32 sound_buffer_base;
static u32 sound_last_cpu_ticks;
static fixed16_16 gbc_sound_tick_step;
+#ifndef __LIBRETRO__
static u32 sound_exit_flag;
+#endif
// Queue 1, 2, or 4 samples to the top of the DS FIFO, wrap around circularly
@@ -447,6 +453,7 @@ void update_gbc_sound(u32 cpu_ticks)
gbc_sound_partial_ticks &= 0xFFFF;
}
+#ifndef __LIBRETRO__
SDL_LockMutex(sound_mutex);
if(synchronize_flag)
{
@@ -487,6 +494,7 @@ void update_gbc_sound(u32 cpu_ticks)
}
}
+#endif
if(sound_on == 1)
{
gs = gbc_sound_channel + 0;
@@ -562,9 +570,11 @@ void update_gbc_sound(u32 cpu_ticks)
gbc_sound_buffer_index =
(gbc_sound_buffer_index + (buffer_ticks * 2)) % BUFFER_SIZE;
+#ifndef __LIBRETRO__
SDL_UnlockMutex(sound_mutex);
SDL_CondSignal(sound_cv);
+#endif
}
#define sound_copy_normal() \
@@ -595,7 +605,7 @@ void update_gbc_sound(u32 cpu_ticks)
} \
-void sound_callback(void *userdata, Uint8 *stream, int length)
+void sound_callback(void *userdata, u8 *stream, int length)
{
u32 sample_length = length / 2;
u32 _length;
@@ -604,6 +614,7 @@ void sound_callback(void *userdata, Uint8 *stream, int length)
s16 *source;
s32 current_sample;
+#ifndef __LIBRETRO__
SDL_LockMutex(sound_mutex);
while(((gbc_sound_buffer_index - sound_buffer_base) % BUFFER_SIZE) <
@@ -611,6 +622,7 @@ void sound_callback(void *userdata, Uint8 *stream, int length)
{
SDL_CondWait(sound_cv, sound_mutex);
}
+#endif
if(global_enable_audio)
{
@@ -645,9 +657,11 @@ void sound_callback(void *userdata, Uint8 *stream, int length)
}
}
+#ifndef __LIBRETRO__
SDL_CondSignal(sound_cv);
SDL_UnlockMutex(sound_mutex);
+#endif
}
// Special thanks to blarrg for the LSFR frequency used in Meridian, as posted
@@ -689,7 +703,9 @@ void reset_sound()
gbc_sound_struct *gs = gbc_sound_channel;
u32 i;
+#ifndef __LIBRETRO__
SDL_LockMutex(sound_mutex);
+#endif
sound_on = 0;
sound_buffer_base = 0;
@@ -723,13 +739,16 @@ void reset_sound()
gs->active_flag = 0;
}
+#ifndef __LIBRETRO__
SDL_UnlockMutex(sound_mutex);
+#endif
}
void sound_exit()
{
gbc_sound_buffer_index =
(sound_buffer_base + audio_buffer_size) % BUFFER_SIZE;
+#ifndef __LIBRETRO__
SDL_PauseAudio(1);
sound_exit_flag = 1;
SDL_CondSignal(sound_cv);
@@ -739,10 +758,12 @@ void sound_exit()
sound_mutex = NULL;
SDL_DestroyCond(sound_cv);
sound_cv = NULL;
+#endif
}
void init_sound(int need_reset)
{
+#ifndef __LIBRETRO__
SDL_AudioSpec sound_settings;
sound_exit_flag = 0;
@@ -779,6 +800,8 @@ void init_sound(int need_reset)
printf("audio: freq %d, size %d\n", sound_frequency, audio_buffer_size);
#endif
+#endif
+
gbc_sound_tick_step =
float_to_fp16_16(256.0f / sound_frequency);
@@ -788,7 +811,9 @@ void init_sound(int need_reset)
if (need_reset)
reset_sound();
+#ifndef __LIBRETRO__
SDL_PauseAudio(0);
+#endif
}
#define sound_savestate_builder(type) \
@@ -811,3 +836,27 @@ void sound_##type##_savestate(file_tag_type savestate_file) \
sound_savestate_builder(read);
sound_savestate_builder(write_mem);
+
+#ifdef __LIBRETRO__
+#include "libretro.h"
+
+static retro_audio_sample_batch_t audio_batch_cb;
+void retro_set_audio_sample(retro_audio_sample_t cb) { }
+void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
+
+void render_audio(void)
+{
+ static s16 stream_base[1024];
+ u32 _length;
+ s16 *source;
+ u32 i;
+ s32 current_sample;
+
+ while (((gbc_sound_buffer_index - sound_buffer_base) % BUFFER_SIZE) > 512) {
+ sound_copy(sound_buffer_base, 512, normal);
+ audio_batch_cb(stream_base, 256);
+ sound_buffer_base += 512;
+ }
+}
+#endif
+