diff --git a/composeApp/src/desktopMain/kotlin/win/taptap/musicmanager/client/DesktopAudioPlayer.kt b/composeApp/src/desktopMain/kotlin/win/taptap/musicmanager/client/DesktopAudioPlayer.kt index e758b15..f3e7b6b 100644 --- a/composeApp/src/desktopMain/kotlin/win/taptap/musicmanager/client/DesktopAudioPlayer.kt +++ b/composeApp/src/desktopMain/kotlin/win/taptap/musicmanager/client/DesktopAudioPlayer.kt @@ -5,6 +5,8 @@ import java.io.File import java.io.FileInputStream import java.io.InputStream import java.net.URI +import javax.sound.sampled.AudioFormat +import javax.sound.sampled.AudioInputStream import javax.sound.sampled.AudioSystem import javax.sound.sampled.SourceDataLine import kotlin.concurrent.thread @@ -14,10 +16,16 @@ import win.taptap.musicmanager.client.playback.AudioPlayer /** * Desktop playback through the MP3SPI decoder and a PCM line. Pause/resume is a * decoder loop gate, so no audio device is reopened. Seeking is not supported. + * + * Java Sound cannot open a line for the compressed MP3 format itself; the + * decoded stream has to be converted to PCM_SIGNED first. Opening the encoded + * format fails with "No line matching interface SourceDataLine supporting + * format MPEG1L3 ...". */ class DesktopAudioPlayer : AudioPlayer { private var input: InputStream? = null + private var stream: AudioInputStream? = null private var line: SourceDataLine? = null private var worker: Thread? = null private val lock = Object() @@ -41,7 +49,9 @@ class DesktopAudioPlayer : AudioPlayer { val raw = if (local) FileInputStream(File(source)) else URI(source).toURL().openStream() val buffered = BufferedInputStream(raw, 128 * 1024) input = buffered - val audio = AudioSystem.getAudioInputStream(buffered) + val encoded = AudioSystem.getAudioInputStream(buffered) + val audio = decodeToPcm(encoded) + stream = audio frameSize = audio.format.frameSize.takeIf { it > 0 } ?: 4 frameRate = audio.format.frameRate.takeIf { it > 0 } ?: 44_100f @@ -98,8 +108,10 @@ class DesktopAudioPlayer : AudioPlayer { runCatching { line?.flush() } worker?.interrupt() runCatching { worker?.join(400) } + runCatching { stream?.close() } runCatching { input?.close() } line = null + stream = null input = null worker = null } @@ -124,3 +136,28 @@ class DesktopAudioPlayer : AudioPlayer { stop() } } + +/** + * MP3SPI hands back a stream whose format is the compressed MPEG one. The audio + * device only understands uncompressed PCM, so convert to 16-bit signed PCM at + * the source sample rate before opening the line. + */ +internal fun decodeToPcm(encoded: AudioInputStream): AudioInputStream { + val pcm = encoded.format.toPcmFormat() + if (encoded.format.encoding == AudioFormat.Encoding.PCM_SIGNED) return encoded + return AudioSystem.getAudioInputStream(pcm, encoded) +} + +internal fun AudioFormat.toPcmFormat(): AudioFormat { + val rate = sampleRate.takeIf { it > 0 } ?: 44_100f + val channelCount = channels.takeIf { it > 0 } ?: 2 + return AudioFormat( + AudioFormat.Encoding.PCM_SIGNED, + rate, + 16, + channelCount, + channelCount * 2, + rate, + false, + ) +} diff --git a/composeApp/src/desktopTest/kotlin/win/taptap/musicmanager/client/DesktopAudioPlayerTest.kt b/composeApp/src/desktopTest/kotlin/win/taptap/musicmanager/client/DesktopAudioPlayerTest.kt new file mode 100644 index 0000000..4b6595e --- /dev/null +++ b/composeApp/src/desktopTest/kotlin/win/taptap/musicmanager/client/DesktopAudioPlayerTest.kt @@ -0,0 +1,34 @@ +package win.taptap.musicmanager.client + +import java.io.ByteArrayInputStream +import javax.sound.sampled.AudioFormat +import javax.sound.sampled.AudioSystem +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +class DesktopAudioPlayerTest { + + @Test + fun `mp3 is decoded to pcm before opening an audio line`() { + val bytes = checkNotNull(javaClass.getResourceAsStream("/sample.mp3")) { + "missing test fixture sample.mp3" + }.use { it.readBytes() } + + val encoded = AudioSystem.getAudioInputStream(ByteArrayInputStream(bytes)) + assertEquals("MPEG1L3", encoded.format.encoding.toString()) + + // Java Sound has no line for the compressed format; the player must + // convert to PCM_SIGNED first. + val pcm = decodeToPcm(encoded) + assertEquals(AudioFormat.Encoding.PCM_SIGNED, pcm.format.encoding) + assertEquals(44_100f, pcm.format.sampleRate) + assertEquals(16, pcm.format.sampleSizeInBits) + assertEquals(2, pcm.format.channels) + assertEquals(4, pcm.format.frameSize) + + val buffer = ByteArray(4_096) + assertTrue(pcm.read(buffer) > 0) + pcm.close() + } +} diff --git a/composeApp/src/desktopTest/resources/sample.mp3 b/composeApp/src/desktopTest/resources/sample.mp3 new file mode 100644 index 0000000..ddbe1c3 Binary files /dev/null and b/composeApp/src/desktopTest/resources/sample.mp3 differ