From b980ff2250d185ea3c7d30ec5741e4193b605d28 Mon Sep 17 00:00:00 2001 From: Paul Adenot Date: Wed, 5 Jan 2022 18:11:58 +0000 Subject: [PATCH] Bug 1630569 - Change the limit at which audio is muted with high or low playback rates, and make it and other parameters configurable via prefs. r=alwu Differential Revision: https://phabricator.services.mozilla.com/D135066 --- dom/html/HTMLMediaElement.cpp | 12 ++----- dom/media/AudioStream.cpp | 13 +++++-- .../mediasink/AudioDecoderInputTrack.cpp | 13 +++++-- modules/libpref/init/StaticPrefList.yaml | 34 +++++++++++++++++++ 4 files changed, 57 insertions(+), 15 deletions(-) diff --git a/dom/html/HTMLMediaElement.cpp b/dom/html/HTMLMediaElement.cpp index 79842fe4d915..4b11da6fcc8c 100644 --- a/dom/html/HTMLMediaElement.cpp +++ b/dom/html/HTMLMediaElement.cpp @@ -181,12 +181,6 @@ static const uint32_t STALL_MS = 3000; static const double MIN_PLAYBACKRATE = 1.0 / 16; // Maximum playbackRate for a media static const double MAX_PLAYBACKRATE = 16.0; -// These are the limits beyonds which SoundTouch does not perform too well and -// when speech is hard to understand anyway. Threshold above which audio is -// muted -static const double THRESHOLD_HIGH_PLAYBACKRATE_AUDIO = 4.0; -// Threshold under which audio is muted -static const double THRESHOLD_LOW_PLAYBACKRATE_AUDIO = 0.25; static double ClampPlaybackRate(double aPlaybackRate) { MOZ_ASSERT(aPlaybackRate >= 0.0); @@ -6763,10 +6757,10 @@ void HTMLMediaElement::SetPlaybackRate(double aPlaybackRate, ErrorResult& aRv) { } mPlaybackRate = aPlaybackRate; - + // Playback rate threshold above which audio is muted. + uint32_t threshold = StaticPrefs::media_audio_playbackrate_muting_threshold(); if (mPlaybackRate != 0.0 && - (mPlaybackRate > THRESHOLD_HIGH_PLAYBACKRATE_AUDIO || - mPlaybackRate < THRESHOLD_LOW_PLAYBACKRATE_AUDIO)) { + (mPlaybackRate > threshold || mPlaybackRate < 1. / threshold)) { SetMutedInternal(mMuted | MUTED_BY_INVALID_PLAYBACK_RATE); } else { SetMutedInternal(mMuted & ~MUTED_BY_INVALID_PLAYBACK_RATE); diff --git a/dom/media/AudioStream.cpp b/dom/media/AudioStream.cpp index 8810bcaa5bea..cab67c08c6d1 100644 --- a/dom/media/AudioStream.cpp +++ b/dom/media/AudioStream.cpp @@ -28,6 +28,7 @@ #include "Tracing.h" #include "webaudio/blink/DenormalDisabler.h" #include "AudioThreadRegistry.h" +#include "mozilla/StaticPrefs_media.h" // Use abort() instead of exception in SoundTouch. #define ST_NO_EXCEPTION_HANDLING 1 @@ -178,9 +179,15 @@ nsresult AudioStream::EnsureTimeStretcherInitializedUnlocked() { // We are going to use a smaller 10ms sequence size to improve speech // clarity, giving more resolution at high tempo and less reverb at low // tempo. Maintain 15ms seekwindow and 8ms overlap for smoothness. - mTimeStretcher->setSetting(SETTING_SEQUENCE_MS, 10); - mTimeStretcher->setSetting(SETTING_SEEKWINDOW_MS, 15); - mTimeStretcher->setSetting(SETTING_OVERLAP_MS, 8); + mTimeStretcher->setSetting( + SETTING_SEQUENCE_MS, + StaticPrefs::media_audio_playbackrate_soundtouch_sequence_ms()); + mTimeStretcher->setSetting( + SETTING_SEEKWINDOW_MS, + StaticPrefs::media_audio_playbackrate_soundtouch_seekwindow_ms()); + mTimeStretcher->setSetting( + SETTING_OVERLAP_MS, + StaticPrefs::media_audio_playbackrate_soundtouch_overlap_ms()); } return NS_OK; } diff --git a/dom/media/mediasink/AudioDecoderInputTrack.cpp b/dom/media/mediasink/AudioDecoderInputTrack.cpp index daaf193dfab2..7f970f0e4f87 100644 --- a/dom/media/mediasink/AudioDecoderInputTrack.cpp +++ b/dom/media/mediasink/AudioDecoderInputTrack.cpp @@ -6,6 +6,7 @@ #include "MediaData.h" #include "mozilla/ScopeExit.h" +#include "mozilla/StaticPrefs_media.h" #include "Tracing.h" // Use abort() instead of exception in SoundTouch. @@ -638,9 +639,15 @@ void AudioDecoderInputTrack::EnsureTimeStretcher() { // We are going to use a smaller 10ms sequence size to improve speech // clarity, giving more resolution at high tempo and less reverb at low // tempo. Maintain 15ms seekwindow and 8ms overlap for smoothness. - mTimeStretcher->setSetting(SETTING_SEQUENCE_MS, 10); - mTimeStretcher->setSetting(SETTING_SEEKWINDOW_MS, 15); - mTimeStretcher->setSetting(SETTING_OVERLAP_MS, 8); + mTimeStretcher->setSetting( + SETTING_SEQUENCE_MS, + StaticPrefs::media_audio_playbackrate_soundtouch_sequence_ms()); + mTimeStretcher->setSetting( + SETTING_SEEKWINDOW_MS, + StaticPrefs::media_audio_playbackrate_soundtouch_seekwindow_ms()); + mTimeStretcher->setSetting( + SETTING_OVERLAP_MS, + StaticPrefs::media_audio_playbackrate_soundtouch_overlap_ms()); SetTempoAndRateForTimeStretcher(); LOG("Create TimeStretcher (channel=%d, playbackRate=%f, preservePitch=%d)", GetChannelCountForTimeStretcher(), mPlaybackRate, mPreservesPitch); diff --git a/modules/libpref/init/StaticPrefList.yaml b/modules/libpref/init/StaticPrefList.yaml index 94fccc9dfe16..e7859ccd8c96 100644 --- a/modules/libpref/init/StaticPrefList.yaml +++ b/modules/libpref/init/StaticPrefList.yaml @@ -9236,6 +9236,40 @@ mirror: always #endif +# When the playback rate of an HTMLMediaElement is greater than this value, or +# lower than the inverse of this value, the audio is muted. +- name: media.audio.playbackrate.muting_threshold + type: uint32_t + value: 8 + mirror: always + +# Time-stretch algorithm single processing sequence length in milliseconds. +# This determines to how long sequences the original sound is chopped in the +# time-stretch algorithm. +- name: media.audio.playbackrate.soundtouch_sequence_ms + type: RelaxedAtomicInt32 + value: 10 + mirror: always + +# Time-stretch algorithm seeking window length in milliseconds for algorithm +# that finds the best possible overlapping location. This determines from how +# wide window the algorithm may look for an optimal joining location when mixing +# the sound sequences back together. +- name: media.audio.playbackrate.soundtouch_seekwindow_ms + type: RelaxedAtomicInt32 + value: 15 + mirror: always + +# Time-stretch algorithm overlap length in milliseconds. When the chopped sound +# sequences are mixed back together, to form a continuous sound stream, this +# parameter defines over how long period the two consecutive sequences are let +# to overlap each other. +- name: media.audio.playbackrate.soundtouch_overlap_ms + type: RelaxedAtomicInt32 + value: 8 + mirror: always + + #--------------------------------------------------------------------------- # Prefs starting with "midi." #---------------------------------------------------------------------------