From 3cd1cfb51cb2e29aa7b1f301b55b723a0c2a44ee Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 7 Mar 2014 15:24:49 +0100 Subject: [PATCH] ao_null: add option for simulated device speed Helps with testing and debugging. --- DOCS/man/en/ao.rst | 5 +++++ audio/out/ao_null.c | 10 ++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/DOCS/man/en/ao.rst b/DOCS/man/en/ao.rst index 14f7a5726e..ac95be760b 100644 --- a/DOCS/man/en/ao.rst +++ b/DOCS/man/en/ao.rst @@ -178,6 +178,11 @@ Available audio output drivers are: ``outburst`` Simulated chunk size in samples. + ``speed`` + Simulated audio playback speed as a multiplier. Usually, a real audio + device will not go exactly as fast as the system clock. It will deviate + just a little, and this option helps simulating this. + ``pcm`` Raw PCM/WAVE file writer audio output diff --git a/audio/out/ao_null.c b/audio/out/ao_null.c index 7caee039e0..a6b40fdb76 100644 --- a/audio/out/ao_null.c +++ b/audio/out/ao_null.c @@ -45,6 +45,7 @@ struct priv { int untimed; float bufferlen; // seconds + float speed; // multiplier // Minimal unit of audio samples that can be written at once. If play() is // called with sizes not aligned to this, a rounded size will be returned. @@ -66,7 +67,7 @@ static void drain(struct ao *ao) double now = mp_time_sec(); if (priv->buffered > 0) { - priv->buffered -= (now - priv->last_time) * ao->samplerate; + priv->buffered -= (now - priv->last_time) * ao->samplerate * priv->speed; if (priv->buffered < 0) { if (!priv->playing_final) MP_ERR(ao, "buffer underrun\n"); @@ -101,7 +102,7 @@ static void uninit(struct ao *ao, bool cut_audio) { struct priv *priv = ao->priv; if (!cut_audio && !priv->paused) - mp_sleep_us(1000.0 * 1000.0 * priv->buffered / ao->samplerate); + mp_sleep_us(1000000.0 * priv->buffered / ao->samplerate / priv->speed); } // stop playing and empty buffers (for seeking/pause) @@ -165,6 +166,9 @@ static float get_delay(struct ao *ao) struct priv *priv = ao->priv; drain(ao); + + // Note how get_delay returns the delay in audio device time (instead of + // adjusting for speed), since most AOs seem to also do that. return priv->buffered / (double)ao->samplerate; } @@ -185,11 +189,13 @@ const struct ao_driver audio_out_null = { .priv_defaults = &(const struct priv) { .bufferlen = 0.2, .outburst = 256, + .speed = 1, }, .options = (const struct m_option[]) { OPT_FLAG("untimed", untimed, 0), OPT_FLOATRANGE("buffer", bufferlen, 0, 0, 100), OPT_INTRANGE("outburst", outburst, 0, 1, 100000), + OPT_FLOATRANGE("speed", speed, 0, 0, 10000), {0} }, };