Skip to content

Commit

Permalink
Use audiomanager to get device channel count
Browse files Browse the repository at this point in the history
Signed-off-by: Jyotiraditya Panda <[email protected]>

	modified:   app/src/main/kotlin/org/akanework/gramophone/ui/components/FullBottomSheet.kt
  • Loading branch information
imjyotiraditya committed Nov 20, 2024
1 parent 431ddb9 commit 93174df
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ class GramophonePlaybackService : MediaLibraryService(), MediaSessionService.Lis
timerDuration = null
}

private val audioManager by lazy {
getSystemService(Context.AUDIO_SERVICE) as AudioManager
}

private var timerDuration: Long? = null
set(value) {
field = value
Expand Down Expand Up @@ -609,7 +613,7 @@ class GramophonePlaybackService : MediaLibraryService(), MediaSessionService.Lis
override fun onTracksChanged(tracks: Tracks) {
val mediaItem = controller!!.currentMediaItem

AudioFormatDetector.detectAudioFormat(tracks, controller).let { info ->
AudioFormatDetector.detectAudioFormat(tracks, controller, audioManager).let { info ->
audioFormat = info
mediaSession?.broadcastCustomCommand(
SessionCommand(SERVICE_GET_AUDIO_FORMAT, Bundle.EMPTY),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.akanework.gramophone.logic.utils

import android.media.AudioManager
import android.os.Parcelable
import androidx.media3.common.C
import androidx.media3.common.Format
Expand Down Expand Up @@ -43,20 +44,26 @@ class AudioFormatDetector {
val sampleRate: Int,
val bitDepth: Int,
val isLossless: Boolean,
val channelCount: Int,
val sourceChannels: Int,
val deviceChannels: Int,
val isDownMixing: Boolean,
val bitrate: Int?,
val mimeType: String?,
val spatialFormat: SpatialFormat,
val encoderPadding: Int?,
val encoderDelay: Int?
) : Parcelable {
override fun toString(): String {
val outputStr = if (isDownMixing) {
"(Down mixed to $deviceChannels channels)"
} else ""

return """
Audio Format Details:
Quality Tier: $quality
Sample Rate: $sampleRate Hz
Bit Depth: $bitDepth bit
Channel Count: $channelCount
Source Channels: $sourceChannels $outputStr
Lossless: $isLossless
Spatial Format: $spatialFormat
Codec: $mimeType
Expand All @@ -66,7 +73,16 @@ class AudioFormatDetector {
}

@UnstableApi
fun detectAudioFormat(tracks: Tracks, player: Player?): AudioFormatInfo? {
fun detectAudioFormat(
tracks: Tracks,
player: Player?,
audioManager: AudioManager
): AudioFormatInfo? {
// TODO: Consider using Media3/ExoPlayer APIs instead of AudioManager for getting device channel information
val activeDevice = audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS)
.firstOrNull { device -> device.isSink }
val deviceChannels = activeDevice?.channelCounts?.maxOrNull() ?: 2

for (group in tracks.groups) {
if (group.type == C.TRACK_TYPE_AUDIO) {
for (i in 0 until group.length) {
Expand All @@ -80,7 +96,9 @@ class AudioFormatDetector {
val bitDepth = detectBitDepth(format)
val isLossless = isLosslessFormat(format.sampleMimeType)
val spatialFormat = detectSpatialFormat(format)
val channelCount = format.channelCount
val sourceChannels = format.channelCount

val isDownMixing = sourceChannels > deviceChannels

val quality = determineQualityTier(
sampleRate = sampleRate,
Expand All @@ -93,7 +111,9 @@ class AudioFormatDetector {
sampleRate = sampleRate,
bitDepth = bitDepth,
isLossless = isLossless,
channelCount = channelCount,
sourceChannels = sourceChannels,
deviceChannels = deviceChannels,
isDownMixing = isDownMixing,
bitrate = bitrate,
mimeType = format.sampleMimeType,
spatialFormat = spatialFormat,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,10 +691,16 @@ class FullBottomSheet
}
bottomSheetFullQualityDetails.setCompoundDrawablesRelative(drawable, null, null, null)

val channelInfo = if (info.isDownMixing) {
"${info.sourceChannels}ch ➝ ${info.deviceChannels}ch"
} else {
"${info.sourceChannels}ch"
}

bottomSheetFullQualityDetails.text = buildString {
append("${info.bitDepth}bit")
append(" / ${info.sampleRate / 1000f}kHz")
append(" / ${info.channelCount} ch")
append(" / $channelInfo")
info.bitrate?.let {
append(" / ${it / 1000}kbps")
}
Expand Down

0 comments on commit 93174df

Please sign in to comment.