Define the audio output buffer in milliseconds

In theory, this buffer must be dimensioned for a target duration, so its
size in bytes should depend on the sample rate.
This commit is contained in:
Romain Vimont 2023-03-11 09:31:32 +01:00
parent 238ab872ba
commit bb509d9317

View file

@ -13,7 +13,7 @@
#define SC_AV_SAMPLE_FMT AV_SAMPLE_FMT_FLT #define SC_AV_SAMPLE_FMT AV_SAMPLE_FMT_FLT
#define SC_SDL_SAMPLE_FMT AUDIO_F32 #define SC_SDL_SAMPLE_FMT AUDIO_F32
#define SC_AUDIO_OUTPUT_BUFFER_SAMPLES 240 // 5ms at 48000Hz #define SC_AUDIO_OUTPUT_BUFFER_MS 5
static inline uint32_t static inline uint32_t
bytes_to_samples(struct sc_audio_player *ap, size_t bytes) { bytes_to_samples(struct sc_audio_player *ap, size_t bytes) {
@ -202,7 +202,7 @@ sc_audio_player_frame_sink_push(struct sc_frame_sink *sink,
bool played = ap->played; bool played = ap->played;
if (played) { if (played) {
uint32_t max_buffered_samples = ap->target_buffering uint32_t max_buffered_samples = ap->target_buffering
+ 12 * SC_AUDIO_OUTPUT_BUFFER_SAMPLES + 12 * SC_AUDIO_OUTPUT_BUFFER_MS * ap->sample_rate / 1000
+ ap->target_buffering / 10; + ap->target_buffering / 10;
if (buffered_samples > max_buffered_samples) { if (buffered_samples > max_buffered_samples) {
uint32_t skip_samples = buffered_samples - max_buffered_samples; uint32_t skip_samples = buffered_samples - max_buffered_samples;
@ -231,7 +231,7 @@ sc_audio_player_frame_sink_push(struct sc_frame_sink *sink,
// max_initial_buffering samples, this would cause unnecessary delay // max_initial_buffering samples, this would cause unnecessary delay
// (and glitches to compensate) on start. // (and glitches to compensate) on start.
uint32_t max_initial_buffering = ap->target_buffering uint32_t max_initial_buffering = ap->target_buffering
+ 2 * SC_AUDIO_OUTPUT_BUFFER_SAMPLES; + 2 * SC_AUDIO_OUTPUT_BUFFER_MS * ap->sample_rate / 1000;
if (buffered_samples > max_initial_buffering) { if (buffered_samples > max_initial_buffering) {
uint32_t skip_samples = buffered_samples - max_initial_buffering; uint32_t skip_samples = buffered_samples - max_initial_buffering;
size_t skip_bytes = samples_to_bytes(ap, skip_samples); size_t skip_bytes = samples_to_bytes(ap, skip_samples);
@ -298,7 +298,7 @@ sc_audio_player_frame_sink_open(struct sc_frame_sink *sink,
.freq = ctx->sample_rate, .freq = ctx->sample_rate,
.format = SC_SDL_SAMPLE_FMT, .format = SC_SDL_SAMPLE_FMT,
.channels = nb_channels, .channels = nb_channels,
.samples = SC_AUDIO_OUTPUT_BUFFER_SAMPLES, .samples = SC_AUDIO_OUTPUT_BUFFER_MS * ctx->sample_rate / 1000,
.callback = sc_audio_player_sdl_callback, .callback = sc_audio_player_sdl_callback,
.userdata = ap, .userdata = ap,
}; };