Skip to content
Merged
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
Expand Up @@ -2162,9 +2162,13 @@ class PlaybackService : MediaLibraryService() {
VolumeLaw.mediaOutputType(am.getDevices(AudioManager.GET_DEVICES_OUTPUTS).map { it.type })
}

/** The output type whose unusable volume curve was last logged. */
@Volatile private var unusableCurveLoggedFor: Int? = null

/**
* The device's own gain in dB at each STREAM_MUSIC index, on the output
* media plays on now; null where the platform cannot say (below API 28).
* media plays on now; null where the platform cannot say (below API 28)
* or gives a table that is not one ([VolumeLaw.isUsableCurve]).
* On an absolute-volume output (most Bluetooth A2DP and LE audio, HDMI
* with CEC) the sink applies its own curve to the index, and this one
* only stands in for it.
Expand All @@ -2173,11 +2177,21 @@ class PlaybackService : MediaLibraryService() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) return null
return try {
val min = am.getStreamMinVolume(AudioManager.STREAM_MUSIC)
val table = (0..am.getStreamMaxVolume(AudioManager.STREAM_MUSIC)).map { index ->
val max = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
val table = (0..max).map { index ->
if (index < min) Double.NEGATIVE_INFINITY
else am.getStreamVolumeDb(AudioManager.STREAM_MUSIC, index, deviceType).toDouble()
}
table::get
if (VolumeLaw.isUsableCurve(max, table::get)) {
table::get
} else {
// Asked on every volume change: warn only when the output changes.
if (unusableCurveLoggedFor != deviceType) {
unusableCurveLoggedFor = deviceType
Log.w(TAG, "Unusable dB volume curve for output type $deviceType; using native steps: $table")
}
null
}
} catch (e: IllegalArgumentException) {
Log.w(TAG, "No volume curve for output type $deviceType", e)
null
Expand Down
28 changes: 28 additions & 0 deletions android/app/src/main/java/com/sendspindroid/playback/VolumeLaw.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,34 @@ internal object VolumeLaw {
/** The spec's linear amplitude for [volume]. */
fun amplitude(volume: Int): Float = (volume.coerceIn(0, 100) / 100.0).pow(1.5).toFloat()

/**
* Whether [dbAt] is a dB attenuation curve that [indexFor] can go by:
* finite, below 0 dB at the lowest audible index and rising to at most
* 0 dB at [maxIndex]. Some vendors return something else (Samsung One UI
* gives linear amplitudes from 0 to 1), and the caller then falls back to
* the device's own steps. Leading minus-infinity entries, the indices
* below the stream's minimum, are skipped.
*/
fun isUsableCurve(maxIndex: Int, dbAt: (Int) -> Double): Boolean {
val lowest = (1..maxIndex).firstOrNull { dbAt(it) != Double.NEGATIVE_INFINITY } ?: return false
if (lowest >= maxIndex) return false
val first = dbAt(lowest)
val last = dbAt(maxIndex)
if (!first.isFinite() || !last.isFinite()) return false
if (first >= -TOLERANCE_DB || last <= first + TOLERANCE_DB) return false
// Positive gain at maximum is possible in theory, but the Android API
// describes 0 dB as the typical maximum, and a table that is not in dB
// can end above it.
if (last > TOLERANCE_DB) return false
var previous = first
for (index in lowest + 1..maxIndex) {
val current = dbAt(index)
if (!current.isFinite() || current + TOLERANCE_DB < previous) return false
previous = current
}
return true
}

/**
* The index for [volume]: the lowest one whose device gain reaches the
* target, so the gain left for the track is at most 0 dB. A volume above
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import io.mockk.mockkObject
import io.mockk.runs
import io.mockk.unmockkAll
import io.mockk.verify
import kotlin.math.pow
import kotlin.math.roundToInt
import kotlinx.coroutines.flow.MutableStateFlow
import org.junit.After
import org.junit.Assert.assertEquals
Expand Down Expand Up @@ -94,6 +96,7 @@ class PlaybackServiceTvVolumeTest {
UserSettings.resetForTesting()
ShadowTvAudioManager.fullVolume = false
ShadowTvAudioManager.volumeFixed = false
ShadowTvAudioManager.volumeDb = null
}

private fun idle() = shadowOf(Looper.getMainLooper()).idle()
Expand Down Expand Up @@ -234,12 +237,34 @@ class PlaybackServiceTvVolumeTest {
assertEquals(deviceVolume(10), service.playbackState.value.volume)
assertEquals(1f, trackGain(service), 0f)
}

@Test
fun `an output whose table is not in dB takes the device's own steps`() {
// One UI's linear amplitudes, 3 dB a step (#380): read as dB, every
// one reaches every target, so each volume took index 1
ShadowTvAudioManager.volumeDb = { if (it == 0) 0f else 10f.pow(-3f * (maxIndex - it) / 20f) }
routeMediaTo(AudioDeviceInfo.TYPE_BLUETOOTH_A2DP)
val service = start()

for (volume in listOf(20, 50, 100)) {
service.setVolume(volume / 100f)
val expected = (volume * maxIndex / 100f).roundToInt()
assertEquals(expected, audioManager.getStreamVolume(AudioManager.STREAM_MUSIC))
assertEquals(1f, trackGain(service), 0f)
}

// The hardware keys: every index reported as 100
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 7, 0)
syncOutputVolume(service)
assertEquals((7 * 100f / maxIndex).roundToInt(), service.playbackState.value.volume)
}
}

/**
* AudioManager on an Android TV box over HDMI. [fullVolume]: the platform
* keeps the STREAM_MUSIC index at max whatever is set (HDMI with CEC), and
* isVolumeFixed does not say so. [volumeFixed]: a fixed-volume device.
* [volumeDb]: what getStreamVolumeDb gives at an index, if not Robolectric's own.
*/
@Implements(AudioManager::class)
class ShadowTvAudioManager : ShadowAudioManager() {
Expand All @@ -252,8 +277,13 @@ class ShadowTvAudioManager : ShadowAudioManager() {
@Implementation
fun isVolumeFixed(): Boolean = volumeFixed

@Implementation
override fun getStreamVolumeDb(streamType: Int, index: Int, deviceType: Int): Float =
volumeDb?.invoke(index) ?: super.getStreamVolumeDb(streamType, index, deviceType)

companion object {
var fullVolume = false
var volumeFixed = false
var volumeDb: ((Int) -> Float)? = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,57 @@ class VolumeLawTest {
}
}

@Test
fun `valid Android dB curve is retained`() {
assertTrue(VolumeLaw.isUsableCurve(maxIndex, dbAt))
}

@Test
fun `zero to one dB table is invalid on every device`() {
val broken: (Int) -> Double = { it.toDouble() / maxIndex }
assertFalse(VolumeLaw.isUsableCurve(maxIndex, broken))
}

@Test
fun `the linear amplitudes a Samsung tablet reports are not a dB curve`() {
// getStreamVolumeDb on a Galaxy Tab A8 (One UI, A2DP), from #380:
// every value reaches 100's target, so every volume took index 1
val samsung = listOf(
0.0, 0.007943281903862953, 0.01122018601745367, 0.015848929062485695,
0.02238721214234829, 0.03162277489900589, 0.04466835409402847, 0.06309574097394943,
0.08912508934736252, 0.1258925348520279, 0.17782793939113617, 0.25118863582611084,
0.3548133671283722, 0.501187264919281, 0.7079457640647888, 1.0,
)
assertEquals(1, VolumeLaw.indexFor(50, maxIndex, samsung::get))
assertFalse(VolumeLaw.isUsableCurve(maxIndex, samsung::get))
}

@Test
fun `indices below the stream minimum do not make a curve unusable`() {
// The service puts minus infinity below getStreamMinVolume
val minIndex = 3
val curve: (Int) -> Double = { if (it < minIndex) Double.NEGATIVE_INFINITY else dbAt(it) }
assertTrue(VolumeLaw.isUsableCurve(maxIndex, curve))
// Nothing audible, or only the top index
assertFalse(VolumeLaw.isUsableCurve(maxIndex) { Double.NEGATIVE_INFINITY })
assertFalse(VolumeLaw.isUsableCurve(maxIndex) { if (it < maxIndex) Double.NEGATIVE_INFINITY else 0.0 })
assertFalse(VolumeLaw.isUsableCurve(1, dbAt))
}

@Test
fun `flat reversed nonfinite positive and dipping dB tables are invalid`() {
val flat: (Int) -> Double = { if (it == 0) Double.NEGATIVE_INFINITY else 0.0 }
val reversed: (Int) -> Double = { -it.toDouble() }
val nan: (Int) -> Double = { if (it == 8) Double.NaN else dbAt(it) }
val positive: (Int) -> Double = { dbAt(it) + 3.0 }
val dip: (Int) -> Double = { if (it == 8) dbAt(7) - 1.0 else dbAt(it) }
assertFalse(VolumeLaw.isUsableCurve(maxIndex, flat))
assertFalse(VolumeLaw.isUsableCurve(maxIndex, reversed))
assertFalse(VolumeLaw.isUsableCurve(maxIndex, nan))
assertFalse(VolumeLaw.isUsableCurve(maxIndex, positive))
assertFalse(VolumeLaw.isUsableCurve(maxIndex, dip))
}

@Test
fun `a volume above 0 is never the silent index`() {
assertEquals(0, VolumeLaw.indexFor(0, maxIndex, dbAt))
Expand Down
Loading