From 7d7e7c2b02da640abdc0be29296ff68b0d302046 Mon Sep 17 00:00:00 2001 From: Kenji Rikitake Date: Tue, 5 Mar 2024 13:44:49 +0900 Subject: [PATCH 1/4] AudioOutput.cpp: show output latency timing --- sfmbase/AudioOutput.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sfmbase/AudioOutput.cpp b/sfmbase/AudioOutput.cpp index cbbd766..d2e7084 100644 --- a/sfmbase/AudioOutput.cpp +++ b/sfmbase/AudioOutput.cpp @@ -177,6 +177,9 @@ PortAudioOutput::PortAudioOutput(const PaDeviceIndex device_index, return; } + fprintf(stderr, "suggestedLatency = %f\n", m_outputparams.suggestedLatency); + + m_paerror = Pa_StartStream(m_stream); if (m_paerror != paNoError) { add_paerror("Pa_StartStream()"); From 6bbb3514b3ebac6b2e0dbf36393e1e607977928c Mon Sep 17 00:00:00 2001 From: Kenji Rikitake Date: Tue, 5 Mar 2024 14:33:06 +0900 Subject: [PATCH 2/4] PortAudioOutput: set latency to 40ms minimum --- sfmbase/AudioOutput.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/sfmbase/AudioOutput.cpp b/sfmbase/AudioOutput.cpp index d2e7084..86dffdd 100644 --- a/sfmbase/AudioOutput.cpp +++ b/sfmbase/AudioOutput.cpp @@ -164,6 +164,13 @@ PortAudioOutput::PortAudioOutput(const PaDeviceIndex device_index, Pa_GetDeviceInfo(m_outputparams.device)->defaultHighOutputLatency; m_outputparams.hostApiSpecificStreamInfo = NULL; + // values of m_outputparams.suggestedLatency from PortAudio: + // Mac mini 2023 with macOS 14.3.1: 0.014717 + // Ubuntu 22.04.4 on x86_64: 0.034830 + if (m_outputparams.suggestedLatency < 0.04) { + m_outputparams.suggestedLatency = 0.04; + } + m_paerror = Pa_OpenStream(&m_stream, NULL, // no input @@ -179,7 +186,6 @@ PortAudioOutput::PortAudioOutput(const PaDeviceIndex device_index, fprintf(stderr, "suggestedLatency = %f\n", m_outputparams.suggestedLatency); - m_paerror = Pa_StartStream(m_stream); if (m_paerror != paNoError) { add_paerror("Pa_StartStream()"); From 653e7387d3d3f8b69bf37bb1bac0bbba4a3e9a1c Mon Sep 17 00:00:00 2001 From: Kenji Rikitake Date: Wed, 6 Mar 2024 15:30:03 +0900 Subject: [PATCH 3/4] Add PortAudioOutput::minimum_latency, set to 40 milliseconds --- include/AudioOutput.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/include/AudioOutput.h b/include/AudioOutput.h index 6c4b955..5838ec1 100644 --- a/include/AudioOutput.h +++ b/include/AudioOutput.h @@ -98,6 +98,18 @@ class SndfileOutput : public AudioOutput { class PortAudioOutput : public AudioOutput { public: + // Static variables. + + // Minimum latency for audio output in seconds + + // Values of m_outputparams.suggestedLatency from PortAudio: + // Mac mini 2023 with macOS 14.3.1: 0.014717 + // Ubuntu 22.04.4 on x86_64: 0.034830 + // Kenji's experiments show that + // 40ms (0.04) is sufficient for macOS, Ubuntu, and Raspberry Pi OS + + static constexpr PaTime minimum_latency = 0.04; + // // Construct PortAudio output stream. // From 1d31577de43e79e13ab5cb26b0361eab4e29c2d9 Mon Sep 17 00:00:00 2001 From: Kenji Rikitake Date: Wed, 6 Mar 2024 15:31:44 +0900 Subject: [PATCH 4/4] PortAudioOutput: use static variable minimum_latency --- sfmbase/AudioOutput.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/sfmbase/AudioOutput.cpp b/sfmbase/AudioOutput.cpp index 86dffdd..5b3d0ff 100644 --- a/sfmbase/AudioOutput.cpp +++ b/sfmbase/AudioOutput.cpp @@ -164,13 +164,13 @@ PortAudioOutput::PortAudioOutput(const PaDeviceIndex device_index, Pa_GetDeviceInfo(m_outputparams.device)->defaultHighOutputLatency; m_outputparams.hostApiSpecificStreamInfo = NULL; - // values of m_outputparams.suggestedLatency from PortAudio: - // Mac mini 2023 with macOS 14.3.1: 0.014717 - // Ubuntu 22.04.4 on x86_64: 0.034830 - if (m_outputparams.suggestedLatency < 0.04) { - m_outputparams.suggestedLatency = 0.04; + // Guarantee minimum latency. + if (m_outputparams.suggestedLatency < minimum_latency) { + m_outputparams.suggestedLatency = minimum_latency; } + fprintf(stderr, "suggestedLatency = %f\n", m_outputparams.suggestedLatency); + m_paerror = Pa_OpenStream(&m_stream, NULL, // no input @@ -184,8 +184,6 @@ PortAudioOutput::PortAudioOutput(const PaDeviceIndex device_index, return; } - fprintf(stderr, "suggestedLatency = %f\n", m_outputparams.suggestedLatency); - m_paerror = Pa_StartStream(m_stream); if (m_paerror != paNoError) { add_paerror("Pa_StartStream()");