Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload audio once per frame #559

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include <audio/conversion/s16_to_float.h>
#include <audio/audio_resampler.h>

extern retro_audio_sample_batch_t audio_batch_cb;
extern void retro_audio_queue(const int16_t *data, int32_t samples);

static unsigned MAX_AUDIO_FRAMES = 2048;

Expand Down Expand Up @@ -162,14 +162,10 @@ static void aiLenChanged(void* user_data, const void* buffer, size_t size)
resampler->process(resampler_audio_data, &data);
convert_float_to_s16(audio_out_buffer_s16, audio_out_buffer_float, data.output_frames * 2);

out = audio_out_buffer_s16;
out = audio_out_buffer_s16;

retro_audio_queue(out, data.output_frames * 2);

while (data.output_frames)
{
size_t ret = audio_batch_cb(out, data.output_frames);
data.output_frames -= ret;
out += ret * 2;
}
if (remain_frames)
{
raw_data = raw_data + frames * 2;
Expand Down
64 changes: 63 additions & 1 deletion libretro/libretro.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#ifdef __MINGW32__
#define _CRT_RAND_S
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -223,6 +227,57 @@ extern struct cheat_ctx g_cheat_ctx;
static bool emuThreadRunning = false;
static pthread_t emuThread;

/* Audio output buffer */
static struct {
int16_t *data;
int32_t size;
int32_t capacity;
} output_audio_buffer = {NULL, 0, 0};

static void ensure_output_audio_buffer_capacity(int32_t capacity)
{
if (capacity <= output_audio_buffer.capacity) {
return;
}

output_audio_buffer.data = realloc(output_audio_buffer.data, capacity * sizeof(*output_audio_buffer.data));
output_audio_buffer.capacity = capacity;
log_cb(RETRO_LOG_DEBUG, "Output audio buffer capacity set to %d\n", capacity);
}

static void init_output_audio_buffer(int32_t capacity)
{
output_audio_buffer.data = NULL;
output_audio_buffer.size = 0;
output_audio_buffer.capacity = 0;
ensure_output_audio_buffer_capacity(capacity);
}

static void free_output_audio_buffer()
{
free(output_audio_buffer.data);
output_audio_buffer.data = NULL;
output_audio_buffer.size = 0;
output_audio_buffer.capacity = 0;
}

static void upload_output_audio_buffer()
{
audio_batch_cb(output_audio_buffer.data, output_audio_buffer.size / 2);
output_audio_buffer.size = 0;
}

void retro_audio_queue(const int16_t *data, int32_t samples)
{
if ((samples < 1) || !emu_initialized)
return;

if (output_audio_buffer.capacity - output_audio_buffer.size < samples)
ensure_output_audio_buffer_capacity((output_audio_buffer.capacity + samples) * 1.5);
memcpy(output_audio_buffer.data + output_audio_buffer.size, data, samples * sizeof(*output_audio_buffer.data));
output_audio_buffer.size += samples;
}

// after the controller's CONTROL* member has been assigned we can update
// them straight from here...
extern struct
Expand Down Expand Up @@ -665,8 +720,10 @@ void retro_get_system_av_info(struct retro_system_av_info *info)
if (current_rdp_type == RDP_PLUGIN_PARALLEL)
parallel_get_geometry(&info->geometry);
#endif
info->timing.fps = vi_expected_refresh_rate_from_tv_standard(ROM_PARAMS.systemtype);
info->timing.sample_rate = 44100.0;
info->timing.fps = (ROM_PARAMS.systemtype == SYSTEM_PAL)
? info->timing.sample_rate / 882
: info->timing.sample_rate / 736;
}

unsigned retro_get_region (void)
Expand Down Expand Up @@ -725,6 +782,8 @@ void retro_init(void)
m64p_error ret = CoreStartup(FRONTEND_API_VERSION, ".", ".", NULL, n64DebugCallback, 0, n64StateCallback);
if(ret && log_cb)
log_cb(RETRO_LOG_ERROR, CORE_NAME ": failed to initialize core (err=%i)\n", ret);

init_output_audio_buffer(2048);
}

void retro_deinit(void)
Expand All @@ -742,6 +801,7 @@ void retro_deinit(void)

CoreShutdown();
deinit_audio_libretro();
free_output_audio_buffer();

if (perf_cb.perf_log)
perf_cb.perf_log();
Expand Down Expand Up @@ -2056,6 +2116,8 @@ void retro_run (void)
// screen_pitch will be 0 for GLN
video_cb(NULL, retro_screen_width, retro_screen_height, screen_pitch);
}

upload_output_audio_buffer();
}

void retro_reset (void)
Expand Down