Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
@file:androidx.annotation.OptIn(markerClass = [androidx.media3.common.util.UnstableApi::class])

package com.github.damontecres.wholphin.playback.leveling

import android.os.SystemClock
import androidx.media3.exoplayer.analytics.AnalyticsListener
import androidx.media3.exoplayer.audio.AudioSink
import androidx.media3.exoplayer.source.LoadEventInfo
import androidx.media3.exoplayer.source.MediaLoadData
import com.github.damontecres.wholphin.BuildConfig
import timber.log.Timber

/** Debug-only route timing. No URLs, credentials, buffer contents or per-buffer logging. */
class PlaybackRouteTrace(
private val processorState: VolumeLevelingState,
) : AnalyticsListener {
override fun onPlaybackStateChanged(
eventTime: AnalyticsListener.EventTime,
state: Int,
) = event(eventTime, "state=$state")

override fun onLoadStarted(
eventTime: AnalyticsListener.EventTime,
info: LoadEventInfo,
data: MediaLoadData,
) = event(eventTime, "load-start id=${info.loadTaskId} type=${data.dataType}")

override fun onLoadCompleted(
eventTime: AnalyticsListener.EventTime,
info: LoadEventInfo,
data: MediaLoadData,
) = event(eventTime, "load-end id=${info.loadTaskId} durationMs=${info.loadDurationMs} bytes=${info.bytesLoaded}")

override fun onAudioTrackInitialized(
eventTime: AnalyticsListener.EventTime,
config: AudioSink.AudioTrackConfig,
) = event(eventTime, "audio-track encoding=${config.encoding} offload=${config.offload} tunneling=${config.tunneling}")

override fun onAudioPositionAdvancing(
eventTime: AnalyticsListener.EventTime,
playoutStartSystemTimeMs: Long,
) = event(eventTime, "audio-advancing")

override fun onRenderedFirstFrame(
eventTime: AnalyticsListener.EventTime,
output: Any,
renderTimeMs: Long,
) = event(eventTime, "first-frame renderMs=$renderTimeMs")

override fun onAudioDecoderInitialized(
eventTime: AnalyticsListener.EventTime,
decoderName: String,
initializedTimestampMs: Long,
initializationDurationMs: Long,
) = event(eventTime, "audio-decoder=$decoderName initMs=$initializationDurationMs")

override fun onVideoDecoderInitialized(
eventTime: AnalyticsListener.EventTime,
decoderName: String,
initializedTimestampMs: Long,
initializationDurationMs: Long,
) = event(eventTime, "video-decoder=$decoderName initMs=$initializationDurationMs")

private fun event(
time: AnalyticsListener.EventTime,
detail: String,
) {
mark("$detail eventMs=${time.realtimeMs} positionMs=${time.currentPlaybackPositionMs}")
processor(processorState)
}

companion object {
/** Application-thread diagnostics; the audio callback only updates primitive counters. */
fun processor(state: VolumeLevelingState) {
if (BuildConfig.DEBUG) {
val format = state.debugFormat
mark(
"processor rate=${format.sampleRate} channels=${format.channelCount} encoding=${format.encoding} " +
"mode=${state.debugAppliedMode} frames=${state.debugProcessedFrames} " +
"resets=${state.debugResetCount} resetEstimateLufs=${state.debugResetEstimateLufs}",
)
}
}

fun mark(detail: String) {
if (BuildConfig.DEBUG) Timber.tag("VolumeSwitch").d("atMs=${SystemClock.elapsedRealtime()} $detail")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
@file:androidx.annotation.OptIn(markerClass = [androidx.media3.common.util.UnstableApi::class])

package com.github.damontecres.wholphin.playback.leveling

import androidx.media3.common.C
import androidx.media3.common.audio.AudioProcessor.AudioFormat
import androidx.media3.common.audio.AudioProcessor.StreamMetadata
import androidx.media3.common.audio.AudioProcessor.UnhandledAudioFormatException
import androidx.media3.common.audio.BaseAudioProcessor
import com.github.damontecres.wholphin.BuildConfig
import com.github.damontecres.wholphin.playback.leveling.core.LevelingMode
import com.github.damontecres.wholphin.playback.leveling.core.VolumeLeveler
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.util.concurrent.atomic.AtomicLong
import kotlin.math.roundToInt

/** UI writes mode/epoch only; the playback thread exclusively owns the DSP and buffers. */
class VolumeLevelingState(
initialMode: LevelingMode = LevelingMode.OFF,
) {
@Volatile
var mode: LevelingMode = initialMode

internal val mediaEpoch = AtomicLong()

// Debug snapshots: single audio-thread writer, application-thread reader. No callback allocation.
@Volatile internal var debugFormat = AudioFormat.NOT_SET

@Volatile internal var debugProcessedFrames = 0L

@Volatile internal var debugAppliedMode = LevelingMode.OFF

@Volatile internal var debugResetCount = 0L

@Volatile internal var debugResetEstimateLufs = Float.NaN

internal fun recordDebugReset(dsp: VolumeLeveler) {
if (BuildConfig.DEBUG) {
debugResetEstimateLufs = dsp.levelEstimateLufs
debugResetCount++
}
}

fun onMediaItemTransition() {
mediaEpoch.incrementAndGet()
}
}

/**
* Removes the core's initial delay and drains its tail, retaining one output frame per input frame.
* Scratch and direct output storage are allocated only on flush/format change. Large decoder buffers
* are consumed in bounded chunks; Media3 calls queueInput again after consuming getOutput().
*
* Off copies input straight to output and leaves the core idle. Turning it on mid-stream starts the
* core fresh and fades it in; turning it off fades it out, drains its delay and returns to copying.
*/
class VolumeLevelingAudioProcessor(
val state: VolumeLevelingState,
) : BaseAudioProcessor() {
private var core: VolumeLeveler? = null
private var format = AudioFormat.NOT_SET
private var samples = FloatArray(0)
private var processed = FloatArray(0)
private var holdback = 0
private var pendingFrames = 0
private var epoch = 0L
private var tailOutput: ByteBuffer = EMPTY_BUFFER
private var endQueued = false
private var running = false
private var fadeIn = false
private var offFrames = 0

internal val levelerRunning: Boolean get() = running

override fun onConfigure(inputAudioFormat: AudioFormat): AudioFormat {
if (
(
inputAudioFormat.encoding != C.ENCODING_PCM_16BIT &&
inputAudioFormat.encoding != C.ENCODING_PCM_FLOAT
) ||
inputAudioFormat.sampleRate !in 8000..384000 ||
inputAudioFormat.channelCount !in 1..32
) {
throw UnhandledAudioFormatException(inputAudioFormat)
}
// configure may precede draining the OLD format. Install the new DSP only at flush.
return inputAudioFormat
}

override fun onFlush(streamMetadata: StreamMetadata) {
if (inputAudioFormat == AudioFormat.NOT_SET) return
val changed = format != inputAudioFormat || core == null
if (changed) {
core?.reset()
format = inputAudioFormat
core = VolumeLeveler(format.sampleRate, format.channelCount)
if (BuildConfig.DEBUG) state.debugFormat = format
state.recordDebugReset(core!!)
val frames = maxOf(CHUNK_FRAMES, core!!.latencyFrames)
samples = FloatArray(frames * format.channelCount)
processed = FloatArray(samples.size)
tailOutput = ByteBuffer.allocateDirect(core!!.latencyFrames * format.bytesPerFrame).order(ByteOrder.nativeOrder())
} else {
core!!.flush()
}
val newEpoch = state.mediaEpoch.get()
val on = state.mode != LevelingMode.OFF
// An estimate from before the core went idle is as stale as a previous item's.
if (epoch != newEpoch || (on && !running && !changed)) {
core!!.reset()
state.recordDebugReset(core!!)
}
epoch = newEpoch
running = on
fadeIn = false
offFrames = 0
holdback = core!!.latencyFrames
pendingFrames = 0
endQueued = false
tailOutput.clear()
tailOutput.flip()
// Reserve enough for a transition's old tail followed by one chunk of the new item.
replaceOutputBuffer((CHUNK_FRAMES + core!!.latencyFrames) * format.bytesPerFrame).flip()
}

override fun queueInput(inputBuffer: ByteBuffer) {
if (!inputBuffer.hasRemaining() || hasPendingOutput()) return
val dsp = checkNotNull(core)
require(inputBuffer.remaining() % format.bytesPerFrame == 0) { "Incomplete PCM frame" }
val output = replaceOutputBuffer((CHUNK_FRAMES + dsp.latencyFrames) * format.bytesPerFrame)
val mode = state.mode
val newEpoch = state.mediaEpoch.get()
if (epoch != newEpoch) {
epoch = newEpoch
if (running) {
// A seamless transition need not cause a sink flush. Preserve the previous item's tail
// before resetting its estimate, then hold back the new item's priming frames.
drain(output)
if (mode == LevelingMode.OFF) {
running = false
} else {
restart(dsp)
}
}
}
if (running && mode == LevelingMode.OFF && offFrames >= format.sampleRate * OFF_SETTLE_MS / 1000) {
// The core's output is dry again; release its delayed frames and stop running it.
drain(output)
running = false
} else if (!running && mode != LevelingMode.OFF) {
// Start from Off so the fresh core fades in instead of switching abruptly.
dsp.mode = LevelingMode.OFF
restart(dsp)
running = true
fadeIn = true
}
val frames = minOf(CHUNK_FRAMES, inputBuffer.remaining() / format.bytesPerFrame)
if (!running) {
val limit = inputBuffer.limit()
inputBuffer.limit(inputBuffer.position() + frames * format.bytesPerFrame)
output.put(inputBuffer)
inputBuffer.limit(limit)
if (BuildConfig.DEBUG) state.debugAppliedMode = LevelingMode.OFF
output.flip()
return
}
val count = frames * format.channelCount
var i = 0
if (format.encoding == C.ENCODING_PCM_16BIT) {
while (i < count) {
samples[i++] = inputBuffer.short / 32768f
}
} else {
while (i < count) {
samples[i++] = inputBuffer.float
}
}
val applied = if (fadeIn) LevelingMode.OFF else mode
fadeIn = false
offFrames = if (applied == LevelingMode.OFF) offFrames + frames else 0
dsp.mode = applied
dsp.process(samples, 0, processed, 0, frames)
if (BuildConfig.DEBUG) {
state.debugAppliedMode = applied
state.debugProcessedFrames += frames
}
val written = emit(output, frames)
pendingFrames += frames - written
output.flip()
}

private fun restart(dsp: VolumeLeveler) {
dsp.reset()
state.recordDebugReset(dsp)
holdback = dsp.latencyFrames
pendingFrames = 0
offFrames = 0
}

private fun emit(
output: ByteBuffer,
frames: Int,
): Int {
val skipped = minOf(holdback, frames)
holdback -= skipped
var i = skipped * format.channelCount
val end = frames * format.channelCount
if (format.encoding == C.ENCODING_PCM_16BIT) {
while (i < end) {
val value = (processed[i++].coerceIn(-1f, 1f) * 32768f).roundToInt()
output.putShort(value.coerceIn(-32768, 32767).toShort())
}
} else {
while (i < end) output.putFloat(processed[i++])
}
return frames - skipped
}

private fun drain(output: ByteBuffer) {
if (pendingFrames == 0) return
val dsp = checkNotNull(core)
samples.fill(0f)
// Keep the most recently processed mode through the old tail. The next queueInput reads
// the latest UI mode; an EOS notification itself must not invent a new transition.
dsp.process(samples, 0, processed, 0, dsp.latencyFrames)
emit(output, dsp.latencyFrames)
pendingFrames = 0
}

override fun onQueueEndOfStream() {
if (core == null || endQueued) return
endQueued = true
// The pipeline may still hold getOutput()'s previous buffer when it signals EOS. A separate
// preallocated tail buffer avoids overwriting that unconsumed audio.
tailOutput.clear()
drain(tailOutput)
tailOutput.flip()
}

override fun getOutput(): ByteBuffer {
val output = super.getOutput()
return if (output.hasRemaining()) output else tailOutput
}

override fun isEnded(): Boolean = super.isEnded() && !tailOutput.hasRemaining()

override fun onReset() {
core?.reset()
core = null
format = AudioFormat.NOT_SET
samples = FloatArray(0)
processed = FloatArray(0)
pendingFrames = 0
holdback = 0
tailOutput = EMPTY_BUFFER
running = false
fadeIn = false
offFrames = 0
}

private companion object {
const val CHUNK_FRAMES = 1024

// Twice the core's 100 ms Off transition.
const val OFF_SETTLE_MS = 200
}
}
Loading