From 05ed4684f4ae1fb9b07818cc0e61d4c363aae1f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Mi=C5=9B?= Date: Fri, 28 Jul 2023 12:51:03 +0200 Subject: [PATCH] Prevent 44100Hz for Opus (fixes #8) --- .../bar/OutputFormatBottomSheetFragment.kt | 21 +++++++++++++++++++ .../com/patrykmis/bar/format/SampleRate.kt | 13 +++++++++--- app/src/main/res/values-pl/strings.xml | 2 ++ app/src/main/res/values/strings.xml | 2 ++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/com/patrykmis/bar/OutputFormatBottomSheetFragment.kt b/app/src/main/kotlin/com/patrykmis/bar/OutputFormatBottomSheetFragment.kt index f2d02f42f..75cbbd6bc 100644 --- a/app/src/main/kotlin/com/patrykmis/bar/OutputFormatBottomSheetFragment.kt +++ b/app/src/main/kotlin/com/patrykmis/bar/OutputFormatBottomSheetFragment.kt @@ -7,6 +7,7 @@ import android.view.ViewGroup import androidx.core.view.isVisible import com.google.android.material.bottomsheet.BottomSheetDialogFragment import com.google.android.material.chip.ChipGroup +import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.slider.Slider import com.patrykmis.bar.databinding.BottomSheetChipBinding import com.patrykmis.bar.databinding.OutputFormatBottomSheetBinding @@ -146,15 +147,35 @@ class OutputFormatBottomSheetFragment : BottomSheetDialogFragment(), } } + private fun checkSampleRateAndShowDialogIfNeeded(sampleRate: SampleRate?) { + if (prefs.format?.name == "OGG/Opus" && sampleRate?.value == 44_100u) { + prefs.sampleRate = SampleRate(48_000u) + showUnsupportedSampleRateDialog() + } else { + prefs.sampleRate = sampleRate + } + } + + private fun showUnsupportedSampleRateDialog() { + MaterialAlertDialogBuilder(requireContext()) + .setMessage(getString(R.string.unsupported_sample_rate)) + .setNeutralButton(getString(android.R.string.ok)) { _, _ -> + refreshSampleRate() + } + .show() + } + override fun onCheckedChanged(group: ChipGroup, checkedIds: MutableList) { when (group) { binding.nameGroup -> { prefs.format = chipIdToFormat[checkedIds.first()]!! refreshParam() + checkSampleRateAndShowDialogIfNeeded(prefs.sampleRate) } binding.sampleRateGroup -> { prefs.sampleRate = chipIdToSampleRate[checkedIds.first()] + checkSampleRateAndShowDialogIfNeeded(prefs.sampleRate) } } } diff --git a/app/src/main/kotlin/com/patrykmis/bar/format/SampleRate.kt b/app/src/main/kotlin/com/patrykmis/bar/format/SampleRate.kt index ec657481d..2d9ff41af 100644 --- a/app/src/main/kotlin/com/patrykmis/bar/format/SampleRate.kt +++ b/app/src/main/kotlin/com/patrykmis/bar/format/SampleRate.kt @@ -1,5 +1,8 @@ package com.patrykmis.bar.format +import android.media.AudioFormat +import android.media.AudioRecord + import com.patrykmis.bar.Preferences @JvmInline @@ -27,13 +30,17 @@ value class SampleRate(val value: UInt) { /** * Get the saved sample rate from the preferences. * - * If the saved sample rate is no longer valid or no sample rate is selected, then [default] - * is returned. + * If the saved sample rate is no longer valid or no sample rate is selected; or + * saved format is "OGG/Opus" and saved sample rate is 44.1 kHz + * then [default] is returned. */ fun fromPreferences(prefs: Preferences): SampleRate { val savedSampleRate = prefs.sampleRate + val savedFormat = prefs.format - if (savedSampleRate != null && all.contains(savedSampleRate)) { + if (savedSampleRate != null && all.contains(savedSampleRate) && + !(savedFormat?.name == "OGG/Opus" && savedSampleRate == SampleRate(44_100u)) + ) { return savedSampleRate } diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index 0d8169e3a..33e7adba4 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -61,4 +61,6 @@ Nagrywanie dźwięku + + Ta częstotliwość próbkowania nie jest obsługiwana przez wybrany format. Ustawiono na domyślną 48000 Hz. diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3950fff43..2617886fa 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -61,4 +61,6 @@ Mic recording + + This sample rate is unsupported by the chosen format. Defaulting to 48000 Hz.