Skip to content

Commit

Permalink
Prevent 44100Hz for Opus (fixes #8)
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrykMis committed Jul 28, 2023
1 parent c7960f6 commit fc4cbb1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Int>) {
when (group) {
binding.nameGroup -> {
prefs.format = chipIdToFormat[checkedIds.first()]!!
refreshParam()
checkSampleRateAndShowDialogIfNeeded(prefs.sampleRate)
}

binding.sampleRateGroup -> {
prefs.sampleRate = chipIdToSampleRate[checkedIds.first()]
checkSampleRateAndShowDialogIfNeeded(prefs.sampleRate)
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions app/src/main/kotlin/com/patrykmis/bar/format/SampleRate.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.patrykmis.bar.format

import android.media.AudioFormat
import android.media.AudioRecord

import com.patrykmis.bar.Preferences

@JvmInline
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-pl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@

<!-- Quick settings tile -->
<string name="quick_settings_mic_label">Nagrywanie dźwięku</string>

<string name="unsupported_sample_rate">Ta częstotliwość próbkowania nie jest obsługiwana przez wybrany format. Ustawiono na domyślną 48000 Hz.</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@

<!-- Quick settings tile -->
<string name="quick_settings_mic_label">Mic recording</string>

<string name="unsupported_sample_rate">This sample rate is unsupported by the chosen format. Defaulting to 48000 Hz.</string>
</resources>

0 comments on commit fc4cbb1

Please sign in to comment.