Twelve: Extend BaseAudioProcessor instead of creating our own
It does everything we do now, but it's from the library.
Change-Id: I6b35ae1eb2a5e1f11fcea256ae3b1c416bdbc587
diff --git a/app/src/main/java/org/lineageos/twelve/services/InfoAudioProcessor.kt b/app/src/main/java/org/lineageos/twelve/services/InfoAudioProcessor.kt
new file mode 100644
index 0000000..83de62b
--- /dev/null
+++ b/app/src/main/java/org/lineageos/twelve/services/InfoAudioProcessor.kt
@@ -0,0 +1,45 @@
+/*
+ * SPDX-FileCopyrightText: 2024 The LineageOS Project
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package org.lineageos.twelve.services
+
+import androidx.annotation.OptIn
+import androidx.media3.common.audio.AudioProcessor
+import androidx.media3.common.audio.BaseAudioProcessor
+import androidx.media3.common.util.UnstableApi
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.flow.asStateFlow
+import java.nio.ByteBuffer
+
+/**
+ * [AudioProcessor] that does nothing other than exposing the [AudioProcessor.AudioFormat].
+ * Here the input is the output.
+ */
+@OptIn(UnstableApi::class)
+class InfoAudioProcessor : BaseAudioProcessor() {
+ private var audioFormat = AudioProcessor.AudioFormat.NOT_SET
+ set(value) {
+ field = value
+ _audioFormatFlow.value = value.takeIf { it != AudioProcessor.AudioFormat.NOT_SET }
+ }
+
+ override fun onConfigure(inputAudioFormat: AudioProcessor.AudioFormat) =
+ super.onConfigure(inputAudioFormat).also {
+ audioFormat = inputAudioFormat
+ }
+
+ override fun queueInput(inputBuffer: ByteBuffer) {
+ error("Should not be called")
+ }
+
+ override fun onReset() {
+ audioFormat = AudioProcessor.AudioFormat.NOT_SET
+ }
+
+ companion object {
+ private val _audioFormatFlow = MutableStateFlow<AudioProcessor.AudioFormat?>(null)
+ val audioFormatFlow = _audioFormatFlow.asStateFlow()
+ }
+}
diff --git a/app/src/main/java/org/lineageos/twelve/services/ProxyAudioProcessor.kt b/app/src/main/java/org/lineageos/twelve/services/ProxyAudioProcessor.kt
deleted file mode 100644
index d97ed0f..0000000
--- a/app/src/main/java/org/lineageos/twelve/services/ProxyAudioProcessor.kt
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * SPDX-FileCopyrightText: 2024 The LineageOS Project
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package org.lineageos.twelve.services
-
-import androidx.annotation.OptIn
-import androidx.media3.common.audio.AudioProcessor
-import androidx.media3.common.util.UnstableApi
-import kotlinx.coroutines.flow.MutableStateFlow
-import kotlinx.coroutines.flow.asStateFlow
-import java.nio.ByteBuffer
-
-/**
- * [AudioProcessor] that does nothing other than exposing the [AudioProcessor.AudioFormat].
- * Here the input is the output.
- */
-@OptIn(UnstableApi::class)
-class ProxyAudioProcessor : AudioProcessor {
- private var pendingAudioFormat = AudioProcessor.AudioFormat.NOT_SET
- private var audioFormat = AudioProcessor.AudioFormat.NOT_SET
- set(value) {
- field = value
- _audioFormatFlow.value = value.takeIf { it != AudioProcessor.AudioFormat.NOT_SET }
- }
- private var buffer = AudioProcessor.EMPTY_BUFFER
- private var isEnded = true
-
- override fun configure(inputAudioFormat: AudioProcessor.AudioFormat) = inputAudioFormat.also {
- this.pendingAudioFormat = it
- }
-
- override fun isActive() = pendingAudioFormat !== AudioProcessor.AudioFormat.NOT_SET
-
- override fun queueInput(inputBuffer: ByteBuffer) {
- this.buffer = inputBuffer
- }
-
- override fun queueEndOfStream() {
- isEnded = true
- }
-
- override fun getOutput() = buffer.also {
- buffer = AudioProcessor.EMPTY_BUFFER
- }
-
- override fun isEnded() = isEnded && buffer === AudioProcessor.EMPTY_BUFFER
-
- override fun flush() {
- buffer = AudioProcessor.EMPTY_BUFFER
- isEnded = false
- audioFormat = pendingAudioFormat
- }
-
- override fun reset() {
- flush()
- pendingAudioFormat = AudioProcessor.AudioFormat.NOT_SET
- audioFormat = AudioProcessor.AudioFormat.NOT_SET
- }
-
- companion object {
- private val _audioFormatFlow = MutableStateFlow<AudioProcessor.AudioFormat?>(null)
- val audioFormatFlow = _audioFormatFlow.asStateFlow()
- }
-}
diff --git a/app/src/main/java/org/lineageos/twelve/services/TurntableRenderersFactory.kt b/app/src/main/java/org/lineageos/twelve/services/TurntableRenderersFactory.kt
index bd3a9c8..d19791f 100644
--- a/app/src/main/java/org/lineageos/twelve/services/TurntableRenderersFactory.kt
+++ b/app/src/main/java/org/lineageos/twelve/services/TurntableRenderersFactory.kt
@@ -26,7 +26,7 @@
) = DefaultAudioSink.Builder(context)
.setEnableFloatOutput(enableFloatOutput)
.setEnableAudioTrackPlaybackParams(enableAudioTrackPlaybackParams)
- .setAudioProcessors(arrayOf(ProxyAudioProcessor()))
+ .setAudioProcessors(arrayOf(InfoAudioProcessor()))
.setAudioTrackBufferSizeProvider(ProxyDefaultAudioTrackBufferSizeProvider)
.build()
}
diff --git a/app/src/main/java/org/lineageos/twelve/viewmodels/NowPlayingStatsViewModel.kt b/app/src/main/java/org/lineageos/twelve/viewmodels/NowPlayingStatsViewModel.kt
index d971feb..d66cf1e 100644
--- a/app/src/main/java/org/lineageos/twelve/viewmodels/NowPlayingStatsViewModel.kt
+++ b/app/src/main/java/org/lineageos/twelve/viewmodels/NowPlayingStatsViewModel.kt
@@ -21,7 +21,7 @@
import org.lineageos.twelve.models.AudioOutputMode
import org.lineageos.twelve.models.AudioStreamInformation
import org.lineageos.twelve.models.Encoding
-import org.lineageos.twelve.services.ProxyAudioProcessor
+import org.lineageos.twelve.services.InfoAudioProcessor
import org.lineageos.twelve.services.ProxyDefaultAudioTrackBufferSizeProvider
class NowPlayingStatsViewModel(application: Application) : NowPlayingViewModel(application) {
@@ -126,7 +126,7 @@
*/
@androidx.annotation.OptIn(UnstableApi::class)
val outputAudioStreamInformation = combine(
- ProxyAudioProcessor.audioFormatFlow,
+ InfoAudioProcessor.audioFormatFlow,
hasOutputInformation,
) { audioFormat, hasOutputInformation ->
audioFormat?.takeIf { hasOutputInformation != false }?.let {