mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-11-21 20:00:49 +01:00
feat(playback): spatial audio switch for the platform spatializer
A Spatial audio switch in Playback settings, on by default, in the Audio output group of both styles: after Skip silence on Android, after exclusive output on macOS, and in its own Audio output group on iOS. Windows and Linux have no row and no search entry. The app renders nothing; the switch only tells the platform whether it may spatialize. - media3: a new `setSpatialAudio` call maps to the audio attributes' spatialization behavior (auto or never). It applies live: the sink reopens its AudioTrack and drops its buffer, so native seeks to the current position to decode that audio again instead of skipping it. - Old Android engine: the fork takes `setSpatialAudio` on its player channel (no platform interface change) and sets the behavior on API 32+ as each source loads, so a change applies from the next song. The subtitle says so while the new engine is off. - iOS and macOS: the Darwin fork sets allowedAudioSpatializationFormats (iOS 14, macOS 11) to mono, stereo and multichannel or to none, on the current item and every new one. The subtitle ends with a live status line. Android reads the Spatializer (available, enabled, head tracker, and canBeSpatialized for stereo and 5.1) over a new event channel that re-reads on spatializer, head tracker and device changes and on resume. iOS reports whether the output port has spatial audio enabled, on route and spatial capability changes. The line names the direct USB and AAudio paths and macOS exclusive output, which never reach the system's spatial audio.
This commit is contained in:
1 parent
0a9eac574d
commit
a4a476e44e
35 files changed
+1408
-16
No files matched your search
@@ -10,8 +10,10 @@ import android.media.AudioMixerAttributes
|
||||
import android.media.MediaCodecList
|
||||
import android.media.MediaExtractor
|
||||
import android.media.MediaFormat
|
||||
import android.media.Spatializer
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import androidx.annotation.RequiresApi
|
||||
|
||||
/**
|
||||
* Read-only route and stream facts for the player's audio information sheet.
|
||||
@@ -86,16 +88,38 @@ internal object AudioRouteInfo {
|
||||
return values
|
||||
}
|
||||
|
||||
private fun spatializer(manager: AudioManager): Map<String, Any?>? {
|
||||
/**
|
||||
* The platform spatializer (Android 12L and later) for the current
|
||||
* output: whether it exists and is on, and whether it would take
|
||||
* ordinary stereo music or only multichannel (5.1) audio. Null before
|
||||
* API 32, which has no spatializer.
|
||||
*/
|
||||
fun spatializer(manager: AudioManager): Map<String, Any?>? {
|
||||
if (Build.VERSION.SDK_INT < 32) return null
|
||||
val spatializer = manager.spatializer
|
||||
return mapOf(
|
||||
"available" to spatializer.isAvailable,
|
||||
"enabled" to spatializer.isEnabled,
|
||||
"headTracking" to (Build.VERSION.SDK_INT >= 33 && spatializer.isHeadTrackerAvailable),
|
||||
"stereo" to canSpatialize(spatializer, AudioFormat.CHANNEL_OUT_STEREO),
|
||||
"multichannel" to canSpatialize(spatializer, AudioFormat.CHANNEL_OUT_5POINT1),
|
||||
)
|
||||
}
|
||||
|
||||
/** Independent of the system switch; it follows the routed device. */
|
||||
@RequiresApi(32)
|
||||
private fun canSpatialize(spatializer: Spatializer, channelMask: Int): Boolean = runCatching {
|
||||
val attributes = AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA)
|
||||
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
|
||||
.setSpatializationBehavior(AudioAttributes.SPATIALIZATION_BEHAVIOR_AUTO)
|
||||
.build()
|
||||
val format = AudioFormat.Builder().setEncoding(AudioFormat.ENCODING_PCM_16BIT)
|
||||
.setSampleRate(48_000)
|
||||
.setChannelMask(channelMask)
|
||||
.build()
|
||||
spatializer.canBeSpatialized(attributes, format)
|
||||
}.getOrDefault(false)
|
||||
|
||||
/** What the platform extractor and decoder see for the playing file. */
|
||||
private fun stream(context: Context, path: String): Map<String, Any?>? {
|
||||
if (!path.startsWith("/") && !path.startsWith("content://")) return null
|
||||
|
||||
@@ -136,6 +136,21 @@ private val cappedSpeedBufferSize =
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Music attributes for the player. [spatialAudio] maps to the platform's
|
||||
* spatialization behavior on Android 12L and later: auto lets the system
|
||||
* spatialize what its spatializer supports, never keeps every song out of
|
||||
* it. Earlier releases ignore it.
|
||||
*/
|
||||
internal fun media3AudioAttributes(spatialAudio: Boolean): AudioAttributes =
|
||||
AudioAttributes.Builder()
|
||||
.setUsage(C.USAGE_MEDIA)
|
||||
.setContentType(C.AUDIO_CONTENT_TYPE_MUSIC)
|
||||
.setSpatializationBehavior(
|
||||
if (spatialAudio) C.SPATIALIZATION_BEHAVIOR_AUTO else C.SPATIALIZATION_BEHAVIOR_NEVER,
|
||||
)
|
||||
.build()
|
||||
|
||||
/**
|
||||
* One media3 player of the `com.zarz.mornye/media3_player` channel. Every
|
||||
* method runs on the plugin's playback looper, which also owns the player.
|
||||
@@ -171,17 +186,14 @@ internal class Media3Player(
|
||||
/** Read by the renderer on media3's playback thread. */
|
||||
@Volatile private var skipSilence = false
|
||||
|
||||
/** Auto until Dart sends `setSpatialAudio`. */
|
||||
private var spatialAudio = true
|
||||
|
||||
init {
|
||||
exo = ExoPlayer.Builder(appContext, media3AudioRenderers(appContext) { skipSilence })
|
||||
.setLooper(looper)
|
||||
// The app owns focus (audio_session) and becoming-noisy handling.
|
||||
.setAudioAttributes(
|
||||
AudioAttributes.Builder()
|
||||
.setUsage(C.USAGE_MEDIA)
|
||||
.setContentType(C.AUDIO_CONTENT_TYPE_MUSIC)
|
||||
.build(),
|
||||
false,
|
||||
)
|
||||
.setAudioAttributes(media3AudioAttributes(spatialAudio), false)
|
||||
.setHandleAudioBecomingNoisy(false)
|
||||
// Off until Dart sends `setSkipSilence`.
|
||||
.setSkipSilenceEnabled(false)
|
||||
@@ -250,6 +262,12 @@ internal class Media3Player(
|
||||
exo.skipSilenceEnabled = enabled
|
||||
result.success(null)
|
||||
}
|
||||
"setSpatialAudio" -> {
|
||||
val enabled = call.argument<Boolean>("enabled")
|
||||
?: return invalid(result, "enabled is required")
|
||||
setSpatialAudio(enabled)
|
||||
result.success(null)
|
||||
}
|
||||
"setEqualizer" -> {
|
||||
val configuration = call.argument<Map<String, Any?>>("configuration")
|
||||
?: return invalid(result, "configuration is required")
|
||||
@@ -308,6 +326,23 @@ internal class Media3Player(
|
||||
exo.prepare()
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies live. New attributes make the sink reopen its `AudioTrack`,
|
||||
* which the platform attaches to the spatializer or not as it opens, and
|
||||
* drop the audio it had buffered (up to 1 s, see [MAX_BUFFERED_SPEED]).
|
||||
* Seeking to the current position decodes that audio again instead of
|
||||
* skipping it, for a short pause rather than a jump.
|
||||
*/
|
||||
private fun setSpatialAudio(enabled: Boolean) {
|
||||
if (enabled == spatialAudio) return
|
||||
spatialAudio = enabled
|
||||
exo.setAudioAttributes(media3AudioAttributes(enabled), false)
|
||||
val state = exo.playbackState
|
||||
if (queue.current != null && pending == null && state != Player.STATE_IDLE && state != Player.STATE_ENDED) {
|
||||
exo.seekTo(exo.currentPosition)
|
||||
}
|
||||
}
|
||||
|
||||
private fun play() {
|
||||
stopped = false
|
||||
if (exo.playbackState == Player.STATE_ENDED) exo.seekToDefaultPosition()
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
package com.zarz.mornye
|
||||
|
||||
import android.content.Context
|
||||
import android.media.AudioDeviceCallback
|
||||
import android.media.AudioDeviceInfo
|
||||
import android.media.AudioManager
|
||||
import android.media.Spatializer
|
||||
import android.os.Build
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import androidx.annotation.RequiresApi
|
||||
import io.flutter.plugin.common.EventChannel
|
||||
|
||||
/**
|
||||
* `com.zarz.mornye/spatial_audio`: the spatializer facts of
|
||||
* [AudioRouteInfo.spatializer] on listen, then again whenever the
|
||||
* spatializer's state or head tracker changes (Android 12L and 13 listeners)
|
||||
* or an output device comes or goes. Read-only; main thread only.
|
||||
*/
|
||||
internal class SpatialAudioEvents(context: Context) : EventChannel.StreamHandler {
|
||||
private val appContext = context.applicationContext
|
||||
private val manager = appContext.getSystemService(AudioManager::class.java)
|
||||
private val main = Handler(Looper.getMainLooper())
|
||||
private var sink: EventChannel.EventSink? = null
|
||||
private var listeners: Any? = null
|
||||
|
||||
private val emitNow = Runnable { emit() }
|
||||
|
||||
private val devices = object : AudioDeviceCallback() {
|
||||
override fun onAudioDevicesAdded(added: Array<out AudioDeviceInfo>?) = routeChanged()
|
||||
override fun onAudioDevicesRemoved(removed: Array<out AudioDeviceInfo>?) = routeChanged()
|
||||
}
|
||||
|
||||
override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {
|
||||
sink = events
|
||||
if (Build.VERSION.SDK_INT >= 32 && listeners == null) {
|
||||
listeners = SpatializerListeners(manager.spatializer, appContext) { emit() }
|
||||
}
|
||||
// It also reports the devices already present, a harmless re-read.
|
||||
manager.registerAudioDeviceCallback(devices, main)
|
||||
emit()
|
||||
}
|
||||
|
||||
override fun onCancel(arguments: Any?) {
|
||||
manager.unregisterAudioDeviceCallback(devices)
|
||||
if (Build.VERSION.SDK_INT >= 32) (listeners as? SpatializerListeners)?.remove()
|
||||
listeners = null
|
||||
main.removeCallbacks(emitNow)
|
||||
sink = null
|
||||
}
|
||||
|
||||
/** The spatializer may follow a new route a moment later; read twice. */
|
||||
private fun routeChanged() {
|
||||
emit()
|
||||
main.removeCallbacks(emitNow)
|
||||
main.postDelayed(emitNow, ROUTE_SETTLE_MS)
|
||||
}
|
||||
|
||||
private fun emit() {
|
||||
val events = sink ?: return
|
||||
val spatializer = runCatching { AudioRouteInfo.spatializer(manager) }.getOrNull()
|
||||
events.success(mapOf("platform" to "android", "sdk" to Build.VERSION.SDK_INT, "spatializer" to spatializer))
|
||||
}
|
||||
|
||||
@RequiresApi(32)
|
||||
private class SpatializerListeners(
|
||||
private val spatializer: Spatializer,
|
||||
context: Context,
|
||||
changed: () -> Unit,
|
||||
) {
|
||||
private val state = object : Spatializer.OnSpatializerStateChangedListener {
|
||||
override fun onSpatializerEnabledChanged(spatializer: Spatializer, enabled: Boolean) = changed()
|
||||
override fun onSpatializerAvailableChanged(spatializer: Spatializer, available: Boolean) = changed()
|
||||
}
|
||||
private val headTracker: Any? = if (Build.VERSION.SDK_INT >= 33) {
|
||||
Spatializer.OnHeadTrackerAvailableListener { _, _ -> changed() }
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
init {
|
||||
val executor = context.mainExecutor
|
||||
spatializer.addOnSpatializerStateChangedListener(executor, state)
|
||||
if (Build.VERSION.SDK_INT >= 33) {
|
||||
spatializer.addOnHeadTrackerAvailableListener(
|
||||
executor,
|
||||
headTracker as Spatializer.OnHeadTrackerAvailableListener,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun remove() {
|
||||
spatializer.removeOnSpatializerStateChangedListener(state)
|
||||
if (Build.VERSION.SDK_INT >= 33) {
|
||||
spatializer.removeOnHeadTrackerAvailableListener(
|
||||
headTracker as Spatializer.OnHeadTrackerAvailableListener,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val ROUTE_SETTLE_MS = 750L
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,8 @@ class UsbBitPerfectPlugin : FlutterPlugin, MethodChannel.MethodCallHandler, Even
|
||||
private lateinit var methods: MethodChannel
|
||||
private lateinit var events: EventChannel
|
||||
private lateinit var route: MethodChannel
|
||||
private lateinit var spatialAudio: EventChannel
|
||||
private var spatialAudioEvents: SpatialAudioEvents? = null
|
||||
private var engine: UsbPcmPlayback? = null
|
||||
private var direct: UsbDirectPlayback? = null
|
||||
private var useDirect = false
|
||||
@@ -53,6 +55,8 @@ class UsbBitPerfectPlugin : FlutterPlugin, MethodChannel.MethodCallHandler, Even
|
||||
}
|
||||
}.start()
|
||||
}
|
||||
spatialAudio = EventChannel(binding.binaryMessenger, "com.zarz.mornye/spatial_audio")
|
||||
spatialAudio.setStreamHandler(SpatialAudioEvents(appContext).also { spatialAudioEvents = it })
|
||||
}
|
||||
|
||||
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
|
||||
@@ -112,6 +116,9 @@ class UsbBitPerfectPlugin : FlutterPlugin, MethodChannel.MethodCallHandler, Even
|
||||
methods.setMethodCallHandler(null)
|
||||
events.setStreamHandler(null)
|
||||
route.setMethodCallHandler(null)
|
||||
spatialAudio.setStreamHandler(null)
|
||||
spatialAudioEvents?.onCancel(null)
|
||||
spatialAudioEvents = null
|
||||
sink = null
|
||||
direct?.dispose()
|
||||
direct = null
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.zarz.mornye
|
||||
|
||||
import androidx.media3.common.C
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
@@ -124,4 +125,16 @@ class Media3ContractTest {
|
||||
assertNull(queue.current)
|
||||
assertTrue(queue.clear().isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun spatialAudioMapsToTheSpatializationBehavior() {
|
||||
val on = media3AudioAttributes(true)
|
||||
val off = media3AudioAttributes(false)
|
||||
assertEquals(C.SPATIALIZATION_BEHAVIOR_AUTO, on.spatializationBehavior)
|
||||
assertEquals(C.SPATIALIZATION_BEHAVIOR_NEVER, off.spatializationBehavior)
|
||||
for (attributes in listOf(on, off)) {
|
||||
assertEquals(C.USAGE_MEDIA, attributes.usage)
|
||||
assertEquals(C.AUDIO_CONTENT_TYPE_MUSIC, attributes.contentType)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,7 @@ import UniformTypeIdentifiers
|
||||
private var playbackNotification: PlaybackNotificationBridge?
|
||||
private var linkNowPlaying: LinkNowPlayingBridge?
|
||||
private var audioRouteInfo: AudioRouteInfoBridge?
|
||||
private var spatialAudio: SpatialAudioBridge?
|
||||
private var musicHaptics: MusicHapticsBridge?
|
||||
private var audioOutput: AudioOutputBridge?
|
||||
private var castOutput: CastOutputPlugin?
|
||||
@@ -174,6 +175,7 @@ import UniformTypeIdentifiers
|
||||
playbackNotification = PlaybackNotificationBridge(messenger: messenger)
|
||||
linkNowPlaying = LinkNowPlayingBridge(messenger: messenger)
|
||||
audioRouteInfo = AudioRouteInfoBridge(messenger: messenger)
|
||||
spatialAudio = SpatialAudioBridge(messenger: messenger)
|
||||
musicHaptics = MusicHapticsBridge(messenger: messenger)
|
||||
audioOutput = AudioOutputBridge(messenger: messenger) { [weak self] in self?.activeWindow }
|
||||
castOutput = CastOutputPlugin(messenger: messenger)
|
||||
|
||||
@@ -124,6 +124,54 @@ final class AudioRouteInfoBridge {
|
||||
}
|
||||
}
|
||||
|
||||
/// `com.zarz.mornye/spatial_audio`: whether the current output port has
|
||||
/// spatial audio enabled, on listen and on every route or spatial capability
|
||||
/// change (spatial audio turned on or off in Control Center). Read-only.
|
||||
final class SpatialAudioBridge: NSObject, FlutterStreamHandler {
|
||||
private let channel: FlutterEventChannel
|
||||
private var sink: FlutterEventSink?
|
||||
private var observers: [NSObjectProtocol] = []
|
||||
|
||||
init(messenger: FlutterBinaryMessenger) {
|
||||
channel = FlutterEventChannel(name: "com.zarz.mornye/spatial_audio", binaryMessenger: messenger)
|
||||
super.init()
|
||||
channel.setStreamHandler(self)
|
||||
}
|
||||
|
||||
func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
|
||||
sink = events
|
||||
let center = NotificationCenter.default
|
||||
observers = [
|
||||
AVAudioSession.routeChangeNotification,
|
||||
AVAudioSession.spatialPlaybackCapabilitiesChangedNotification,
|
||||
].map { name in
|
||||
center.addObserver(forName: name, object: nil, queue: .main) { [weak self] _ in self?.emit() }
|
||||
}
|
||||
emit()
|
||||
return nil
|
||||
}
|
||||
|
||||
func onCancel(withArguments arguments: Any?) -> FlutterError? {
|
||||
observers.forEach(NotificationCenter.default.removeObserver)
|
||||
observers = []
|
||||
sink = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
private func emit() {
|
||||
let outputs = AVAudioSession.sharedInstance().currentRoute.outputs
|
||||
sink?([
|
||||
"platform": "ios",
|
||||
"enabled": outputs.contains { $0.isSpatialAudioEnabled },
|
||||
] as [String: Any])
|
||||
}
|
||||
|
||||
deinit {
|
||||
observers.forEach(NotificationCenter.default.removeObserver)
|
||||
channel.setStreamHandler(nil)
|
||||
}
|
||||
}
|
||||
|
||||
/// `show` opens the system route picker (AirPlay, Bluetooth, this iPhone)
|
||||
/// from the app's own Audio output sheet, the counterpart of Android's
|
||||
/// output switcher on the same channel. It answers once the picker closes,
|
||||
|
||||
@@ -328,6 +328,43 @@
|
||||
"@skipSilenceAutoMix": {
|
||||
"description": "Added to the Skip silence subtitle while AutoMix is on, which suspends silence skipping"
|
||||
},
|
||||
"spatialAudio": "Spatial audio",
|
||||
"@spatialAudio": {
|
||||
"description": "Playback setting (Android, iOS, macOS) that lets the system's own spatial audio process songs"
|
||||
},
|
||||
"spatialAudioDescription": "Lets the system spatialize songs on headphones and speakers that support it. Off plays every song without it.",
|
||||
"spatialAudioNextSong": "With the old playback engine, a change applies from the next song.",
|
||||
"@spatialAudioNextSong": {
|
||||
"description": "Added to the Spatial audio subtitle on Android while the New playback engine is off"
|
||||
},
|
||||
"spatialAudioSystemOff": "Spatial audio is off in system settings.",
|
||||
"@spatialAudioSystemOff": {
|
||||
"description": "Spatial audio status: the setting is on in the app, but the phone's own spatial audio setting is off"
|
||||
},
|
||||
"spatialAudioUnavailable": "Not available on this device or output.",
|
||||
"@spatialAudioUnavailable": {
|
||||
"description": "Spatial audio status: the device or the connected output cannot spatialize"
|
||||
},
|
||||
"spatialAudioStereo": "Stereo music is spatialized.",
|
||||
"spatialAudioStereoHeadTracking": "Stereo music is spatialized, with head tracking available.",
|
||||
"spatialAudioMultichannelOnly": "Only multichannel songs (such as 5.1 or Dolby Atmos) are spatialized on this device.",
|
||||
"@spatialAudioMultichannelOnly": {
|
||||
"description": "Spatial audio status: the device spatializes only songs with more than two channels, not ordinary stereo music"
|
||||
},
|
||||
"spatialAudioDirectUsb": "Not used with direct USB output.",
|
||||
"@spatialAudioDirectUsb": {
|
||||
"description": "Spatial audio status while the hi-res direct USB output plays, which bypasses the system's spatial audio"
|
||||
},
|
||||
"spatialAudioDirectAAudio": "Not used with AAudio exclusive output.",
|
||||
"spatialAudioExclusive": "Not used with exclusive output.",
|
||||
"@spatialAudioExclusive": {
|
||||
"description": "Spatial audio status on macOS while exclusive output (hog mode) is on, which bypasses the system's spatial audio"
|
||||
},
|
||||
"spatialAudioOutputOn": "Spatial audio is on for this output.",
|
||||
"spatialAudioOutputOff": "Not available with this output, or turned off in Control Center.",
|
||||
"@spatialAudioOutputOff": {
|
||||
"description": "Spatial audio status on iOS: the current output has no spatial audio, or the user turned it off in Control Center"
|
||||
},
|
||||
"sectionPlaybackQueue": "Queue",
|
||||
"@sectionPlaybackQueue": {
|
||||
"description": "Playback settings section for how the play queue behaves"
|
||||
|
||||
@@ -221,6 +221,19 @@
|
||||
"skipSilenceDescription": "Melompati bagian lagu yang hening. Selama aktif, lagu didekode dalam 16-bit.",
|
||||
"skipSilenceNeedsEngine": "Aktifkan mesin pemutar baru untuk memakai ini.",
|
||||
"skipSilenceAutoMix": "Nonaktif selama AutoMix aktif.",
|
||||
"spatialAudio": "Audio spasial",
|
||||
"spatialAudioDescription": "Membiarkan sistem memproses lagu menjadi audio spasial di headphone dan speaker yang mendukung. Jika mati, semua lagu diputar tanpanya.",
|
||||
"spatialAudioNextSong": "Dengan mesin pemutar lama, perubahan berlaku di lagu selanjutnya.",
|
||||
"spatialAudioSystemOff": "Audio spasial dimatikan di pengaturan sistem.",
|
||||
"spatialAudioUnavailable": "Tidak tersedia di perangkat atau output ini.",
|
||||
"spatialAudioStereo": "Musik stereo diproses menjadi audio spasial.",
|
||||
"spatialAudioStereoHeadTracking": "Musik stereo diproses menjadi audio spasial, dengan pelacakan kepala tersedia.",
|
||||
"spatialAudioMultichannelOnly": "Hanya lagu multikanal (seperti 5.1 atau Dolby Atmos) yang diproses menjadi audio spasial di perangkat ini.",
|
||||
"spatialAudioDirectUsb": "Tidak dipakai dengan output USB langsung.",
|
||||
"spatialAudioDirectAAudio": "Tidak dipakai dengan output AAudio eksklusif.",
|
||||
"spatialAudioExclusive": "Tidak dipakai dengan output eksklusif.",
|
||||
"spatialAudioOutputOn": "Audio spasial aktif untuk output ini.",
|
||||
"spatialAudioOutputOff": "Tidak tersedia di output ini, atau dimatikan di Pusat Kontrol.",
|
||||
"sectionPlaybackQueue": "Antrean",
|
||||
"queueNoDuplicates": "Tanpa duplikat di antrean",
|
||||
"queueNoDuplicatesDescription": "Menambahkan lagu yang sudah ada di antrean berikutnya akan memindahkannya, bukan menambah salinan kedua. Lagu yang sedang diputar dan yang sudah diputar tetap di tempatnya.",
|
||||
|
||||
@@ -171,6 +171,11 @@ class AppSettings {
|
||||
/// decodes to 16-bit, which its silence detection needs.
|
||||
final bool skipSilence;
|
||||
|
||||
/// Android, iOS and macOS: let the system spatialize songs (Android
|
||||
/// spatialization behavior auto, Apple mono, stereo and multichannel).
|
||||
/// Off asks the system never to spatialize them. The app renders nothing.
|
||||
final bool spatialAudio;
|
||||
|
||||
/// Adding a song already coming up in the queue moves that entry instead
|
||||
/// of queueing a second copy. The current and played entries stay.
|
||||
final bool queueNoDuplicates;
|
||||
@@ -452,6 +457,7 @@ class AppSettings {
|
||||
this.dapExclusive = false,
|
||||
this.media3Engine = true,
|
||||
this.skipSilence = false,
|
||||
this.spatialAudio = true,
|
||||
this.queueNoDuplicates = false,
|
||||
this.queueLocked = false,
|
||||
this.exclusiveOutput = false,
|
||||
@@ -632,6 +638,7 @@ class AppSettings {
|
||||
bool? dapExclusive,
|
||||
bool? media3Engine,
|
||||
bool? skipSilence,
|
||||
bool? spatialAudio,
|
||||
bool? queueNoDuplicates,
|
||||
bool? queueLocked,
|
||||
bool? exclusiveOutput,
|
||||
@@ -792,6 +799,7 @@ class AppSettings {
|
||||
dapExclusive: dapExclusive ?? this.dapExclusive,
|
||||
media3Engine: media3Engine ?? this.media3Engine,
|
||||
skipSilence: skipSilence ?? this.skipSilence,
|
||||
spatialAudio: spatialAudio ?? this.spatialAudio,
|
||||
queueNoDuplicates: queueNoDuplicates ?? this.queueNoDuplicates,
|
||||
queueLocked: queueLocked ?? this.queueLocked,
|
||||
exclusiveOutput: exclusiveOutput ?? this.exclusiveOutput,
|
||||
|
||||
@@ -70,6 +70,7 @@ AppSettings _$AppSettingsFromJson(Map<String, dynamic> json) => AppSettings(
|
||||
dapExclusive: json['dapExclusive'] as bool? ?? false,
|
||||
media3Engine: json['media3Engine'] as bool? ?? true,
|
||||
skipSilence: json['skipSilence'] as bool? ?? false,
|
||||
spatialAudio: json['spatialAudio'] as bool? ?? true,
|
||||
queueNoDuplicates: json['queueNoDuplicates'] as bool? ?? false,
|
||||
queueLocked: json['queueLocked'] as bool? ?? false,
|
||||
exclusiveOutput: json['exclusiveOutput'] as bool? ?? false,
|
||||
@@ -268,6 +269,7 @@ Map<String, dynamic> _$AppSettingsToJson(
|
||||
'dapExclusive': instance.dapExclusive,
|
||||
'media3Engine': instance.media3Engine,
|
||||
'skipSilence': instance.skipSilence,
|
||||
'spatialAudio': instance.spatialAudio,
|
||||
'queueNoDuplicates': instance.queueNoDuplicates,
|
||||
'queueLocked': instance.queueLocked,
|
||||
'exclusiveOutput': instance.exclusiveOutput,
|
||||
|
||||
@@ -747,6 +747,9 @@ class SettingsNotifier extends Notifier<AppSettings> {
|
||||
void setSkipSilence(bool enabled) =>
|
||||
_commit(state.copyWith(skipSilence: enabled));
|
||||
|
||||
void setSpatialAudio(bool enabled) =>
|
||||
_commit(state.copyWith(spatialAudio: enabled));
|
||||
|
||||
void setQueueNoDuplicates(bool enabled) =>
|
||||
_commit(state.copyWith(queueNoDuplicates: enabled));
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ import 'package:mornye/services/playback_deck.dart';
|
||||
import 'package:mornye/services/music_player_service.dart'
|
||||
show resetAutoplayLearning;
|
||||
import 'package:mornye/services/song_analysis.dart';
|
||||
import 'package:mornye/services/spatial_audio.dart';
|
||||
import 'package:mornye/screens/listening_statistics_screen.dart';
|
||||
import 'package:mornye/screens/equalizer_screen.dart';
|
||||
import 'package:mornye/theme/app_tokens.dart';
|
||||
@@ -164,6 +165,7 @@ class _PlaybackSettingsPageState extends ConsumerState<PlaybackSettingsPage>
|
||||
final linuxDevice =
|
||||
settings.exclusiveOutput &&
|
||||
defaultTargetPlatform == TargetPlatform.linux;
|
||||
final macSpatialAudio = spatialAudioPlatform == SpatialAudioPlatform.macos;
|
||||
final colorScheme = Theme.of(context).colorScheme;
|
||||
final coverStyle = settings.playerCoverStyleFor(mornye: context.isMornye);
|
||||
// The charging rule applies to Android's background sweep only.
|
||||
@@ -551,12 +553,28 @@ class _PlaybackSettingsPageState extends ConsumerState<PlaybackSettingsPage>
|
||||
onChanged: (value) => ref
|
||||
.read(settingsProvider.notifier)
|
||||
.setExclusiveOutput(value),
|
||||
showDivider: linuxDevice,
|
||||
showDivider: linuxDevice || macSpatialAudio,
|
||||
);
|
||||
},
|
||||
),
|
||||
if (linuxDevice)
|
||||
_AlsaDeviceItem(device: settings.exclusiveOutputDevice),
|
||||
if (macSpatialAudio)
|
||||
const _SpatialAudioItem(
|
||||
platform: SpatialAudioPlatform.macos,
|
||||
showDivider: false,
|
||||
),
|
||||
],
|
||||
),
|
||||
// iOS has no other audio output setting.
|
||||
if (spatialAudioPlatform == SpatialAudioPlatform.ios)
|
||||
...settingsSectionSlivers(
|
||||
title: context.l10n.sectionAudioOutput,
|
||||
children: const [
|
||||
_SpatialAudioItem(
|
||||
platform: SpatialAudioPlatform.ios,
|
||||
showDivider: false,
|
||||
),
|
||||
],
|
||||
),
|
||||
if (androidAudio)
|
||||
@@ -592,6 +610,7 @@ class _PlaybackSettingsPageState extends ConsumerState<PlaybackSettingsPage>
|
||||
.read(settingsProvider.notifier)
|
||||
.setSkipSilence(value),
|
||||
),
|
||||
const _SpatialAudioItem(platform: SpatialAudioPlatform.android),
|
||||
ValueListenableBuilder<UsbAudioStatus>(
|
||||
valueListenable: usbAudioStatus,
|
||||
builder: (context, status, _) => SettingsSwitchItem(
|
||||
@@ -904,3 +923,82 @@ class _AlsaDeviceItemState extends ConsumerState<_AlsaDeviceItem> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Spatial audio, with a line for what the system does with songs on the
|
||||
/// current output. The app renders nothing; the platform spatializes.
|
||||
class _SpatialAudioItem extends ConsumerStatefulWidget {
|
||||
const _SpatialAudioItem({required this.platform, this.showDivider = true});
|
||||
|
||||
final SpatialAudioPlatform platform;
|
||||
final bool showDivider;
|
||||
|
||||
@override
|
||||
ConsumerState<_SpatialAudioItem> createState() => _SpatialAudioItemState();
|
||||
}
|
||||
|
||||
class _SpatialAudioItemState extends ConsumerState<_SpatialAudioItem>
|
||||
with WidgetsBindingObserver {
|
||||
StreamSubscription<SpatialAudioState>? _states;
|
||||
SpatialAudioState? _state;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
_listen();
|
||||
}
|
||||
|
||||
/// The native side reports the current state on listen, then each change.
|
||||
void _listen() {
|
||||
unawaited(_states?.cancel());
|
||||
_states = spatialAudioStates(widget.platform).listen((state) {
|
||||
if (mounted) setState(() => _state = state);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
// Back from the system settings, where no listener may have fired.
|
||||
if (state == AppLifecycleState.resumed) _listen();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
unawaited(_states?.cancel());
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final settings = ref.watch(settingsProvider);
|
||||
return ValueListenableBuilder<UsbAudioStatus>(
|
||||
valueListenable: usbAudioStatus,
|
||||
builder: (context, usb, _) {
|
||||
final status = spatialAudioStatus(
|
||||
enabled: settings.spatialAudio,
|
||||
platform: widget.platform,
|
||||
state: _state,
|
||||
usb: usb,
|
||||
exclusiveOutput: settings.exclusiveOutput,
|
||||
);
|
||||
return SettingsSwitchItem(
|
||||
icon: Icons.spatial_audio_outlined,
|
||||
title: context.l10n.spatialAudio,
|
||||
subtitle: [
|
||||
context.l10n.spatialAudioDescription,
|
||||
// MediaPlayer reads its attributes as each song loads.
|
||||
if (widget.platform == SpatialAudioPlatform.android &&
|
||||
!settings.media3Engine)
|
||||
context.l10n.spatialAudioNextSong,
|
||||
if (status != null) spatialAudioStatusLabel(context.l10n, status),
|
||||
].join('\n\n'),
|
||||
value: settings.spatialAudio,
|
||||
onChanged: (value) =>
|
||||
ref.read(settingsProvider.notifier).setSpatialAudio(value),
|
||||
showDivider: widget.showDivider,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -65,6 +65,7 @@ class Media3PlaybackDeck implements PlaybackDeck, QueuedPlaybackDeck {
|
||||
playbackEqualizer.addListener(_equalizerChanged);
|
||||
playbackOutputVolume.addListener(_outputVolumeChanged);
|
||||
playbackSkipSilence.addListener(_skipSilenceChanged);
|
||||
playbackSpatialAudio.addListener(_spatialAudioChanged);
|
||||
}
|
||||
|
||||
static const _channel = MethodChannel('com.zarz.mornye/media3_player');
|
||||
@@ -113,6 +114,10 @@ class Media3PlaybackDeck implements PlaybackDeck, QueuedPlaybackDeck {
|
||||
/// The native player starts with silence skipping off; only a change from
|
||||
/// that is sent.
|
||||
bool _skipSilenceSent = false;
|
||||
|
||||
/// The native player starts with spatialization allowed (auto); only a
|
||||
/// change from that is sent.
|
||||
bool _spatialAudioSent = true;
|
||||
Timer? _equalizerTimer;
|
||||
Future<void> _equalizerWrite = Future.value();
|
||||
|
||||
@@ -160,6 +165,38 @@ class Media3PlaybackDeck implements PlaybackDeck, QueuedPlaybackDeck {
|
||||
// Before the first source, so its decoder starts in the mode silence
|
||||
// skipping needs.
|
||||
if (playbackSkipSilence.value) await _sendSkipSilence();
|
||||
// Before the first source, so its output never opens spatialized.
|
||||
if (!playbackSpatialAudio.value) await _sendSpatialAudio();
|
||||
}
|
||||
|
||||
void _spatialAudioChanged() {
|
||||
final created = _created;
|
||||
if (_disposed || created == null) return;
|
||||
unawaited(
|
||||
created.then((_) async {
|
||||
if (_disposed || playbackSpatialAudio.value == _spatialAudioSent) {
|
||||
return;
|
||||
}
|
||||
await _sendSpatialAudio();
|
||||
}, onError: (Object _) {}),
|
||||
);
|
||||
}
|
||||
|
||||
/// Applies live: the native side reopens its output at the current
|
||||
/// position with the new spatialization behavior.
|
||||
Future<void> _sendSpatialAudio() async {
|
||||
final enabled = playbackSpatialAudio.value;
|
||||
_spatialAudioSent = enabled;
|
||||
try {
|
||||
await _channel.invokeMethod<void>('setSpatialAudio', {
|
||||
'playerId': playerId,
|
||||
'enabled': enabled,
|
||||
});
|
||||
} on PlatformException catch (error) {
|
||||
_log.w('Spatial audio setting unavailable: ${error.code}');
|
||||
} on MissingPluginException {
|
||||
// An older native player: the platform default (auto) stays.
|
||||
}
|
||||
}
|
||||
|
||||
void _skipSilenceChanged() {
|
||||
@@ -555,6 +592,7 @@ class Media3PlaybackDeck implements PlaybackDeck, QueuedPlaybackDeck {
|
||||
playbackEqualizer.removeListener(_equalizerChanged);
|
||||
playbackOutputVolume.removeListener(_outputVolumeChanged);
|
||||
playbackSkipSilence.removeListener(_skipSilenceChanged);
|
||||
playbackSpatialAudio.removeListener(_spatialAudioChanged);
|
||||
_equalizerTimer?.cancel();
|
||||
reportEqualizerError(playerId, null);
|
||||
_disposed = true;
|
||||
|
||||
@@ -159,6 +159,7 @@ class AudioplayersPlaybackDeck implements PlaybackDeck {
|
||||
required String playerId,
|
||||
bool? nativeAvailable,
|
||||
bool? equalizerAvailable,
|
||||
bool? spatialAudioAvailable,
|
||||
}) : _ordinary = AudioPlayer(playerId: playerId),
|
||||
// The Darwin fork's tap is shared source, so macOS has it as well.
|
||||
// isDesktopPlatform keeps host-run tests on the mobile default.
|
||||
@@ -171,9 +172,16 @@ class AudioplayersPlaybackDeck implements PlaybackDeck {
|
||||
Platform.isAndroid ||
|
||||
Platform.isIOS ||
|
||||
(isDesktopPlatform && Platform.isMacOS),
|
||||
// Both forks carry `setSpatialAudio`.
|
||||
_spatialAudioAvailable =
|
||||
spatialAudioAvailable ??
|
||||
(Platform.isAndroid ||
|
||||
Platform.isIOS ||
|
||||
(isDesktopPlatform && Platform.isMacOS)),
|
||||
_nativeAvailable = nativeAvailable ?? Platform.isAndroid {
|
||||
playbackEqualizer.addListener(_equalizerChanged);
|
||||
playbackOutputVolume.addListener(_outputVolumeChanged);
|
||||
playbackSpatialAudio.addListener(_spatialAudioChanged);
|
||||
_subscriptions.addAll([
|
||||
_ordinary.onPlayerStateChanged.listen((value) {
|
||||
if (!_direct) _states.add(_deckState(value));
|
||||
@@ -267,6 +275,39 @@ class AudioplayersPlaybackDeck implements PlaybackDeck {
|
||||
}
|
||||
}
|
||||
|
||||
final bool _spatialAudioAvailable;
|
||||
|
||||
/// Both forks start a player with spatialization allowed; only a change
|
||||
/// from that is sent.
|
||||
bool _spatialAudioSent = true;
|
||||
Future<void> _spatialAudioWrite = Future.value();
|
||||
|
||||
void _spatialAudioChanged() {
|
||||
_spatialAudioWrite = _spatialAudioWrite.then((_) => _syncSpatialAudio());
|
||||
}
|
||||
|
||||
/// Android's MediaPlayer takes it from the next source; the Darwin fork
|
||||
/// also sets it on the current item.
|
||||
Future<void> _syncSpatialAudio() async {
|
||||
if (!_spatialAudioAvailable || _disposed) return;
|
||||
final enabled = playbackSpatialAudio.value;
|
||||
if (enabled == _spatialAudioSent) return;
|
||||
_spatialAudioSent = enabled;
|
||||
try {
|
||||
// Public API waits for native player creation without changing playback.
|
||||
await _ordinary.getDuration();
|
||||
if (_disposed) return;
|
||||
await _effects
|
||||
.invokeMethod<void>('setSpatialAudio', {
|
||||
'playerId': _ordinary.playerId,
|
||||
'enabled': enabled,
|
||||
})
|
||||
.timeout(const Duration(seconds: 3));
|
||||
} catch (error) {
|
||||
_log.w('Spatial audio setting unavailable: $error');
|
||||
}
|
||||
}
|
||||
|
||||
final bool _nativeAvailable;
|
||||
final _states = StreamController<PlaybackDeckState>.broadcast();
|
||||
final _durations = StreamController<Duration>.broadcast();
|
||||
@@ -486,6 +527,9 @@ class AudioplayersPlaybackDeck implements PlaybackDeck {
|
||||
// /proc/self/fd number for a different SAF file, so URL equality must not
|
||||
// reuse the previous MediaPlayer or its pending preparation callbacks.
|
||||
await _ordinary.release();
|
||||
// Android's MediaPlayer reads its audio attributes as the source loads.
|
||||
_spatialAudioChanged();
|
||||
await _spatialAudioWrite;
|
||||
_ordinaryError = null;
|
||||
final abandoned = _ordinaryPreparation = Completer<void>();
|
||||
try {
|
||||
@@ -720,6 +764,7 @@ class AudioplayersPlaybackDeck implements PlaybackDeck {
|
||||
_equalizerDisposed = true;
|
||||
playbackEqualizer.removeListener(_equalizerChanged);
|
||||
playbackOutputVolume.removeListener(_outputVolumeChanged);
|
||||
playbackSpatialAudio.removeListener(_spatialAudioChanged);
|
||||
_equalizerTimer?.cancel();
|
||||
reportEqualizerError(_ordinary.playerId, null);
|
||||
await stop();
|
||||
|
||||
@@ -446,6 +446,7 @@ class MusicPlayerRuntime {
|
||||
settings.media3Engine &&
|
||||
!settings.autoMix &&
|
||||
dependencies.isAndroid;
|
||||
playbackSpatialAudio.value = settings.spatialAudio;
|
||||
configureLockScreenLyrics(enabled: settings.lockScreenLyrics);
|
||||
_songAnalysis?.configure(settings);
|
||||
_handler?.applySettings(previous);
|
||||
|
||||
@@ -63,6 +63,11 @@ final playbackMedia3Engine = ValueNotifier<bool>(false);
|
||||
/// audioplayers decks ignore it.
|
||||
final playbackSkipSilence = ValueNotifier<bool>(false);
|
||||
|
||||
/// The `spatialAudio` setting. True lets the platform spatialize songs (its
|
||||
/// default on Android); false asks it never to. media3 decks and the
|
||||
/// audioplayers forks apply it; libmpv decks ignore it.
|
||||
final playbackSpatialAudio = ValueNotifier<bool>(true);
|
||||
|
||||
/// Linux: the ALSA device [playbackExclusiveOutput] opens, as an mpv device
|
||||
/// name. Empty until one is known; the output stays shared meanwhile.
|
||||
final playbackExclusiveDevice = ValueNotifier<String>('');
|
||||
|
||||
@@ -0,0 +1,183 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io' show Platform;
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:mornye/l10n/app_localizations.dart';
|
||||
import 'package:mornye/services/music_playback_deck.dart';
|
||||
import 'package:mornye/utils/desktop_platform.dart';
|
||||
import 'package:mornye/utils/logger.dart';
|
||||
|
||||
final _log = AppLogger('SpatialAudio');
|
||||
|
||||
/// Where the Spatial audio setting exists. Windows and Linux leave spatial
|
||||
/// audio to the system, so they have none.
|
||||
enum SpatialAudioPlatform { android, ios, macos, none }
|
||||
|
||||
/// Overrides [spatialAudioPlatform] in tests.
|
||||
@visibleForTesting
|
||||
SpatialAudioPlatform? debugSpatialAudioPlatformOverride;
|
||||
|
||||
SpatialAudioPlatform get spatialAudioPlatform {
|
||||
final override = debugSpatialAudioPlatformOverride;
|
||||
if (override != null) return override;
|
||||
if (isDesktopPlatform) {
|
||||
return defaultTargetPlatform == TargetPlatform.macOS
|
||||
? SpatialAudioPlatform.macos
|
||||
: SpatialAudioPlatform.none;
|
||||
}
|
||||
if (Platform.isAndroid) return SpatialAudioPlatform.android;
|
||||
if (Platform.isIOS) return SpatialAudioPlatform.ios;
|
||||
return SpatialAudioPlatform.none;
|
||||
}
|
||||
|
||||
/// What the platform reports about spatial audio for the current output.
|
||||
/// Every field is optional: releases expose different subsets.
|
||||
class SpatialAudioState {
|
||||
const SpatialAudioState({
|
||||
this.platform,
|
||||
this.available,
|
||||
this.enabled,
|
||||
this.headTracking,
|
||||
this.stereo,
|
||||
this.multichannel,
|
||||
});
|
||||
|
||||
/// Android sends its `Spatializer` facts (API 32 and later, otherwise
|
||||
/// none), iOS whether the output port has spatial audio enabled.
|
||||
factory SpatialAudioState.fromMap(Map<dynamic, dynamic> map) {
|
||||
final platform = map['platform'] as String?;
|
||||
final spatializer = map['spatializer'];
|
||||
if (platform == 'android') {
|
||||
if (spatializer is! Map) {
|
||||
return const SpatialAudioState(platform: 'android', available: false);
|
||||
}
|
||||
return SpatialAudioState(
|
||||
platform: platform,
|
||||
available: spatializer['available'] as bool?,
|
||||
enabled: spatializer['enabled'] as bool?,
|
||||
headTracking: spatializer['headTracking'] as bool?,
|
||||
stereo: spatializer['stereo'] as bool?,
|
||||
multichannel: spatializer['multichannel'] as bool?,
|
||||
);
|
||||
}
|
||||
return SpatialAudioState(
|
||||
platform: platform,
|
||||
enabled: map['enabled'] as bool?,
|
||||
);
|
||||
}
|
||||
|
||||
final String? platform;
|
||||
|
||||
/// Android: the spatializer exists and the output can use it.
|
||||
final bool? available;
|
||||
|
||||
/// Android: the system setting. iOS: spatial audio on the output port.
|
||||
final bool? enabled;
|
||||
final bool? headTracking;
|
||||
|
||||
/// Android: ordinary stereo music can be spatialized on this output.
|
||||
final bool? stereo;
|
||||
|
||||
/// Android: 5.1 audio can be spatialized on this output.
|
||||
final bool? multichannel;
|
||||
}
|
||||
|
||||
/// What the system does with songs while the Spatial audio setting is on.
|
||||
enum SpatialAudioStatus {
|
||||
/// Android: the direct USB path bypasses the platform mixer.
|
||||
directUsb,
|
||||
|
||||
/// Android: AAudio exclusive bypasses the platform mixer.
|
||||
directAAudio,
|
||||
|
||||
/// macOS: hog mode plays through libmpv, never AVPlayer.
|
||||
exclusiveOutput,
|
||||
systemOff,
|
||||
unavailable,
|
||||
stereo,
|
||||
stereoHeadTracking,
|
||||
multichannelOnly,
|
||||
|
||||
/// iOS: the output port has spatial audio enabled.
|
||||
outputOn,
|
||||
|
||||
/// iOS: it does not (unsupported, or off in Control Center).
|
||||
outputOff,
|
||||
}
|
||||
|
||||
/// Null while the setting is off or nothing is known.
|
||||
SpatialAudioStatus? spatialAudioStatus({
|
||||
required bool enabled,
|
||||
required SpatialAudioPlatform platform,
|
||||
SpatialAudioState? state,
|
||||
UsbAudioStatus? usb,
|
||||
bool exclusiveOutput = false,
|
||||
}) {
|
||||
if (!enabled) return null;
|
||||
switch (platform) {
|
||||
case SpatialAudioPlatform.none:
|
||||
return null;
|
||||
case SpatialAudioPlatform.macos:
|
||||
return exclusiveOutput ? SpatialAudioStatus.exclusiveOutput : null;
|
||||
case SpatialAudioPlatform.ios:
|
||||
final on = state?.enabled;
|
||||
if (on == null) return null;
|
||||
return on ? SpatialAudioStatus.outputOn : SpatialAudioStatus.outputOff;
|
||||
case SpatialAudioPlatform.android:
|
||||
if (usb?.reason == 'active') {
|
||||
return usb?.driver == 'aaudio_exclusive'
|
||||
? SpatialAudioStatus.directAAudio
|
||||
: SpatialAudioStatus.directUsb;
|
||||
}
|
||||
if (state == null) return null;
|
||||
if (state.available != true) return SpatialAudioStatus.unavailable;
|
||||
if (state.enabled == false) return SpatialAudioStatus.systemOff;
|
||||
if (state.stereo == true) {
|
||||
return state.headTracking == true
|
||||
? SpatialAudioStatus.stereoHeadTracking
|
||||
: SpatialAudioStatus.stereo;
|
||||
}
|
||||
if (state.multichannel == true) {
|
||||
return SpatialAudioStatus.multichannelOnly;
|
||||
}
|
||||
return SpatialAudioStatus.unavailable;
|
||||
}
|
||||
}
|
||||
|
||||
String spatialAudioStatusLabel(
|
||||
AppLocalizations l10n,
|
||||
SpatialAudioStatus status,
|
||||
) => switch (status) {
|
||||
SpatialAudioStatus.directUsb => l10n.spatialAudioDirectUsb,
|
||||
SpatialAudioStatus.directAAudio => l10n.spatialAudioDirectAAudio,
|
||||
SpatialAudioStatus.exclusiveOutput => l10n.spatialAudioExclusive,
|
||||
SpatialAudioStatus.systemOff => l10n.spatialAudioSystemOff,
|
||||
SpatialAudioStatus.unavailable => l10n.spatialAudioUnavailable,
|
||||
SpatialAudioStatus.stereo => l10n.spatialAudioStereo,
|
||||
SpatialAudioStatus.stereoHeadTracking => l10n.spatialAudioStereoHeadTracking,
|
||||
SpatialAudioStatus.multichannelOnly => l10n.spatialAudioMultichannelOnly,
|
||||
SpatialAudioStatus.outputOn => l10n.spatialAudioOutputOn,
|
||||
SpatialAudioStatus.outputOff => l10n.spatialAudioOutputOff,
|
||||
};
|
||||
|
||||
const _events = EventChannel('com.zarz.mornye/spatial_audio');
|
||||
|
||||
/// The current state on listen, then each change the platform reports
|
||||
/// (spatializer state, head tracker, output devices on Android; route and
|
||||
/// spatial capability changes on iOS). Empty where nothing reports it.
|
||||
Stream<SpatialAudioState> spatialAudioStates(SpatialAudioPlatform platform) {
|
||||
if (platform != SpatialAudioPlatform.android &&
|
||||
platform != SpatialAudioPlatform.ios) {
|
||||
return const Stream.empty();
|
||||
}
|
||||
return _events
|
||||
.receiveBroadcastStream()
|
||||
.where((event) => event is Map)
|
||||
.map((event) => SpatialAudioState.fromMap(event as Map))
|
||||
.handleError((Object error) {
|
||||
if (error is! MissingPluginException) {
|
||||
_log.w('Spatial audio state unavailable: $error');
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -354,6 +354,7 @@ final Map<IconData, IconData> _outlinedSymbols = {
|
||||
Icons.share_outlined: Symbols.share_rounded,
|
||||
Icons.smart_display_outlined: Symbols.smart_display_rounded,
|
||||
Icons.source_outlined: Symbols.source_rounded,
|
||||
Icons.spatial_audio_outlined: Symbols.spatial_audio_rounded,
|
||||
Icons.speaker_group_outlined: Symbols.speaker_group_rounded,
|
||||
Icons.speed_outlined: Symbols.speed_rounded,
|
||||
Icons.storage_outlined: Symbols.storage_rounded,
|
||||
|
||||
@@ -142,6 +142,7 @@ IconData _cupertinoFor(IconData icon) {
|
||||
Icons.cloud || Icons.cloud_outlined => CupertinoIcons.cloud,
|
||||
Icons.cloud_upload_outlined => CupertinoIcons.cloud_upload,
|
||||
Icons.high_quality => CupertinoIcons.hifispeaker,
|
||||
Icons.spatial_audio_outlined => CupertinoIcons.headphones,
|
||||
Icons.four_k => CupertinoIcons.waveform,
|
||||
Icons.dynamic_feed_outlined ||
|
||||
Icons.tab_outlined => CupertinoIcons.square_on_square,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:mornye/l10n/app_localizations.dart';
|
||||
import 'package:mornye/services/spatial_audio.dart';
|
||||
import 'package:mornye/utils/android_tv.dart';
|
||||
import 'package:mornye/utils/desktop_platform.dart';
|
||||
import 'package:mornye/utils/exclusive_output_labels.dart';
|
||||
@@ -505,6 +506,21 @@ class SettingsSearchCatalog {
|
||||
targetLabel: l10n.usbBitPerfect,
|
||||
),
|
||||
],
|
||||
// Windows and Linux leave spatial audio to the system.
|
||||
if (androidAudio || spatialAudioPlatform != SpatialAudioPlatform.none)
|
||||
SettingsSearchEntry(
|
||||
icon: Icons.spatial_audio_outlined,
|
||||
title: l10n.spatialAudio,
|
||||
subtitle: l10n.spatialAudioDescription,
|
||||
keywords: const [
|
||||
'spatial',
|
||||
'surround',
|
||||
'head tracking',
|
||||
'dolby atmos',
|
||||
'3d',
|
||||
'spasial',
|
||||
],
|
||||
),
|
||||
if (isDesktopPlatform)
|
||||
SettingsSearchEntry(
|
||||
icon: Icons.speaker_outlined,
|
||||
|
||||
@@ -436,6 +436,57 @@ void main() {
|
||||
expect(native.calls, isEmpty);
|
||||
});
|
||||
|
||||
test(
|
||||
'spatial audio off reaches the player before its first source',
|
||||
() async {
|
||||
addTearDown(() => playbackSpatialAudio.value = true);
|
||||
playbackSpatialAudio.value = false;
|
||||
await deck.setFileSource('/song.flac');
|
||||
final methods = native.methods;
|
||||
expect(
|
||||
methods.indexOf('setSpatialAudio'),
|
||||
lessThan(methods.indexOf('setSource')),
|
||||
);
|
||||
expect(native.last('setSpatialAudio'), {
|
||||
'playerId': 'one',
|
||||
'enabled': false,
|
||||
});
|
||||
|
||||
playbackSpatialAudio.value = true;
|
||||
await _settle();
|
||||
expect(native.last('setSpatialAudio'), {
|
||||
'playerId': 'one',
|
||||
'enabled': true,
|
||||
});
|
||||
expect(native.count('setSpatialAudio'), 2);
|
||||
},
|
||||
);
|
||||
|
||||
test('spatial audio is sent only when it differs from auto', () async {
|
||||
addTearDown(() => playbackSpatialAudio.value = true);
|
||||
await deck.setFileSource('/song.flac');
|
||||
expect(native.methods, isNot(contains('setSpatialAudio')));
|
||||
|
||||
playbackSpatialAudio.value = false;
|
||||
await _settle();
|
||||
expect(native.last('setSpatialAudio')['enabled'], isFalse);
|
||||
|
||||
// An older native player without the method keeps playing.
|
||||
native.replies['setSpatialAudio'] = (_) async =>
|
||||
throw MissingPluginException();
|
||||
playbackSpatialAudio.value = true;
|
||||
await _settle();
|
||||
await deck.resume();
|
||||
expect(native.count('play'), 1);
|
||||
});
|
||||
|
||||
test('a deck never created sends nothing for spatial audio', () async {
|
||||
addTearDown(() => playbackSpatialAudio.value = true);
|
||||
playbackSpatialAudio.value = false;
|
||||
await _settle();
|
||||
expect(native.calls, isEmpty);
|
||||
});
|
||||
|
||||
test('a disabled equalizer is never sent', () async {
|
||||
await deck.setFileSource('/song.flac');
|
||||
playbackEqualizer.value = const EqualizerSettings(preamp: 2);
|
||||
|
||||
@@ -890,6 +890,15 @@ void main() {
|
||||
expect(playbackSkipSilence.value, isFalse);
|
||||
});
|
||||
|
||||
test('spatial audio follows the setting for the decks', () {
|
||||
addTearDown(() => playbackSpatialAudio.value = true);
|
||||
final runtime = _runtime(isAndroid: true);
|
||||
runtime.configure(runtime.settings.copyWith(spatialAudio: false));
|
||||
expect(playbackSpatialAudio.value, isFalse);
|
||||
runtime.configure(runtime.settings.copyWith(spatialAudio: true));
|
||||
expect(playbackSpatialAudio.value, isTrue);
|
||||
});
|
||||
|
||||
test('changing the engine drops a song queued on the old one', () async {
|
||||
final deck = _QueuedDeck();
|
||||
final runtime = _runtime(
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mornye/services/music_playback_deck.dart';
|
||||
import 'package:mornye/services/playback_deck.dart';
|
||||
|
||||
/// The audioplayers deck sends the Spatial audio setting to both forks
|
||||
/// (Android's MediaPlayer wrapper and the Darwin AVPlayer wrapper).
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
final messenger =
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
|
||||
final calls = <MethodCall>[];
|
||||
|
||||
List<String> methods() => [for (final call in calls) call.method];
|
||||
List<Object?> spatial() => [
|
||||
for (final call in calls)
|
||||
if (call.method == 'setSpatialAudio') call.arguments,
|
||||
];
|
||||
|
||||
setUp(() {
|
||||
calls.clear();
|
||||
for (final name in [
|
||||
'xyz.luan/audioplayers.global',
|
||||
'xyz.luan/audioplayers.global/events',
|
||||
]) {
|
||||
messenger.setMockMethodCallHandler(
|
||||
MethodChannel(name),
|
||||
(_) async => null,
|
||||
);
|
||||
}
|
||||
messenger.setMockMethodCallHandler(
|
||||
const MethodChannel('xyz.luan/audioplayers'),
|
||||
(call) async {
|
||||
calls.add(call);
|
||||
final args = call.arguments as Map<Object?, Object?>;
|
||||
final id = args['playerId']! as String;
|
||||
if (call.method == 'create') {
|
||||
messenger.setMockMethodCallHandler(
|
||||
MethodChannel('xyz.luan/audioplayers/events/$id'),
|
||||
(_) async => null,
|
||||
);
|
||||
} else if (call.method == 'setSourceUrl') {
|
||||
unawaited(
|
||||
messenger.handlePlatformMessage(
|
||||
'xyz.luan/audioplayers/events/$id',
|
||||
const StandardMethodCodec().encodeSuccessEnvelope({
|
||||
'event': 'audio.onPrepared',
|
||||
'value': true,
|
||||
}),
|
||||
(_) {},
|
||||
),
|
||||
);
|
||||
} else if (call.method == 'getDuration') {
|
||||
return 60000;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
tearDown(() => playbackSpatialAudio.value = true);
|
||||
|
||||
test('the default sends nothing', () async {
|
||||
final deck = AudioplayersPlaybackDeck(
|
||||
playerId: 'spatial-default',
|
||||
spatialAudioAvailable: true,
|
||||
);
|
||||
await deck.setFileSource('/song.flac');
|
||||
expect(methods(), isNot(contains('setSpatialAudio')));
|
||||
await deck.dispose();
|
||||
});
|
||||
|
||||
test('off reaches the player before the source it applies to', () async {
|
||||
playbackSpatialAudio.value = false;
|
||||
final deck = AudioplayersPlaybackDeck(
|
||||
playerId: 'spatial-off',
|
||||
spatialAudioAvailable: true,
|
||||
);
|
||||
await deck.setFileSource('/song.flac');
|
||||
expect(spatial(), [
|
||||
{'playerId': 'spatial-off', 'enabled': false},
|
||||
]);
|
||||
expect(
|
||||
methods().indexOf('setSpatialAudio'),
|
||||
lessThan(methods().indexOf('setSourceUrl')),
|
||||
);
|
||||
// The next song needs nothing new.
|
||||
await deck.setFileSource('/next.flac');
|
||||
expect(spatial(), hasLength(1));
|
||||
await deck.dispose();
|
||||
});
|
||||
|
||||
test('a change is sent at once, for each value', () async {
|
||||
final deck = AudioplayersPlaybackDeck(
|
||||
playerId: 'spatial-live',
|
||||
spatialAudioAvailable: true,
|
||||
);
|
||||
await deck.setFileSource('/song.flac');
|
||||
playbackSpatialAudio.value = false;
|
||||
await Future<void>.delayed(const Duration(milliseconds: 20));
|
||||
playbackSpatialAudio.value = true;
|
||||
await Future<void>.delayed(const Duration(milliseconds: 20));
|
||||
expect(spatial(), [
|
||||
{'playerId': 'spatial-live', 'enabled': false},
|
||||
{'playerId': 'spatial-live', 'enabled': true},
|
||||
]);
|
||||
await deck.dispose();
|
||||
playbackSpatialAudio.value = false;
|
||||
await Future<void>.delayed(const Duration(milliseconds: 20));
|
||||
expect(spatial(), hasLength(2));
|
||||
});
|
||||
|
||||
test('a platform without the fork method sends nothing', () async {
|
||||
playbackSpatialAudio.value = false;
|
||||
final deck = AudioplayersPlaybackDeck(
|
||||
playerId: 'spatial-none',
|
||||
spatialAudioAvailable: false,
|
||||
);
|
||||
await deck.setFileSource('/song.flac');
|
||||
expect(methods(), isNot(contains('setSpatialAudio')));
|
||||
await deck.dispose();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,451 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:mornye/l10n/app_localizations.dart';
|
||||
import 'package:mornye/models/settings.dart';
|
||||
import 'package:mornye/models/theme_settings.dart';
|
||||
import 'package:mornye/providers/settings_provider.dart';
|
||||
import 'package:mornye/screens/settings/playback_settings_page.dart';
|
||||
import 'package:mornye/services/music_playback_deck.dart';
|
||||
import 'package:mornye/services/spatial_audio.dart';
|
||||
import 'package:mornye/theme/app_theme.dart';
|
||||
import 'package:mornye/theme/mornye_theme.dart';
|
||||
import 'package:mornye/utils/desktop_platform.dart';
|
||||
import 'package:mornye/utils/settings_search_catalog.dart';
|
||||
import 'package:mornye/widgets/settings_group.dart';
|
||||
|
||||
const _title = 'Spatial audio';
|
||||
const _events = EventChannel('com.zarz.mornye/spatial_audio');
|
||||
|
||||
final _l10n = lookupAppLocalizations(const Locale('en'));
|
||||
|
||||
TestDefaultBinaryMessenger get _messenger =>
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
|
||||
|
||||
/// Answers a listen with [state], like the native stream does.
|
||||
void _mockState(Map<String, Object?> state) {
|
||||
_messenger.setMockStreamHandler(
|
||||
_events,
|
||||
MockStreamHandler.inline(onListen: (_, events) => events.success(state)),
|
||||
);
|
||||
addTearDown(() => _messenger.setMockStreamHandler(_events, null));
|
||||
}
|
||||
|
||||
Map<String, Object?> _android({
|
||||
bool available = true,
|
||||
bool enabled = true,
|
||||
bool headTracking = false,
|
||||
bool stereo = false,
|
||||
bool multichannel = false,
|
||||
}) => {
|
||||
'platform': 'android',
|
||||
'sdk': 34,
|
||||
'spatializer': {
|
||||
'available': available,
|
||||
'enabled': enabled,
|
||||
'headTracking': headTracking,
|
||||
'stereo': stereo,
|
||||
'multichannel': multichannel,
|
||||
},
|
||||
};
|
||||
|
||||
Future<ProviderContainer> _pumpPage(
|
||||
WidgetTester tester, {
|
||||
required bool mornye,
|
||||
AppSettings settings = const AppSettings(),
|
||||
}) async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
tester.view.physicalSize = const Size(1200, 1600);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
final container = ProviderContainer(
|
||||
overrides: [initialSettingsProvider.overrideWithValue(settings)],
|
||||
);
|
||||
addTearDown(container.dispose);
|
||||
await tester.pumpWidget(
|
||||
UncontrolledProviderScope(
|
||||
container: container,
|
||||
child: MaterialApp(
|
||||
theme: mornye
|
||||
? MornyeTheme.build(Brightness.dark, accent: MornyeAccent.blue)
|
||||
: AppTheme.light(),
|
||||
locale: const Locale('en'),
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
home: const PlaybackSettingsPage(),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
return container;
|
||||
}
|
||||
|
||||
Finder get _row => find.byWidgetPredicate(
|
||||
(widget) => widget is SettingsSwitchItem && widget.title == _title,
|
||||
);
|
||||
|
||||
Future<SettingsSwitchItem> _scrollToRow(WidgetTester tester) async {
|
||||
await tester.scrollUntilVisible(
|
||||
_row,
|
||||
300,
|
||||
scrollable: find.byType(Scrollable).first,
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
return tester.widget<SettingsSwitchItem>(_row);
|
||||
}
|
||||
|
||||
void main() {
|
||||
tearDown(() {
|
||||
debugAndroidAudioSettingsOverride = null;
|
||||
debugSpatialAudioPlatformOverride = null;
|
||||
debugDesktopPlatformOverride = null;
|
||||
usbAudioStatus.value = const UsbAudioStatus();
|
||||
});
|
||||
|
||||
test('the setting defaults to on and round-trips through JSON', () {
|
||||
expect(const AppSettings().spatialAudio, isTrue);
|
||||
expect(AppSettings.fromJson(const {}).spatialAudio, isTrue);
|
||||
final off = const AppSettings().copyWith(spatialAudio: false);
|
||||
expect(off.toJson()['spatialAudio'], isFalse);
|
||||
expect(AppSettings.fromJson(off.toJson()).spatialAudio, isFalse);
|
||||
expect(off.copyWith().spatialAudio, isFalse);
|
||||
});
|
||||
|
||||
group('platform', () {
|
||||
test('mobile hosts are neither platform in tests', () {
|
||||
expect(spatialAudioPlatform, SpatialAudioPlatform.none);
|
||||
});
|
||||
|
||||
for (final (platform, expected) in [
|
||||
(TargetPlatform.macOS, SpatialAudioPlatform.macos),
|
||||
(TargetPlatform.windows, SpatialAudioPlatform.none),
|
||||
(TargetPlatform.linux, SpatialAudioPlatform.none),
|
||||
]) {
|
||||
test('desktop $platform is $expected', () {
|
||||
debugDesktopPlatformOverride = true;
|
||||
debugDefaultTargetPlatformOverride = platform;
|
||||
addTearDown(() => debugDefaultTargetPlatformOverride = null);
|
||||
expect(spatialAudioPlatform, expected);
|
||||
});
|
||||
}
|
||||
|
||||
test('settings search lists it except on Windows and Linux', () {
|
||||
bool listed({bool androidAudio = false}) => SettingsSearchCatalog(
|
||||
_l10n,
|
||||
androidAudio: androidAudio,
|
||||
).playback.any((entry) => entry.title == _l10n.spatialAudio);
|
||||
expect(listed(androidAudio: true), isTrue);
|
||||
expect(listed(), isFalse);
|
||||
debugSpatialAudioPlatformOverride = SpatialAudioPlatform.ios;
|
||||
expect(listed(), isTrue);
|
||||
debugSpatialAudioPlatformOverride = null;
|
||||
debugDesktopPlatformOverride = true;
|
||||
for (final (platform, shown) in [
|
||||
(TargetPlatform.macOS, true),
|
||||
(TargetPlatform.windows, false),
|
||||
(TargetPlatform.linux, false),
|
||||
]) {
|
||||
debugDefaultTargetPlatformOverride = platform;
|
||||
expect(listed(), shown, reason: '$platform');
|
||||
}
|
||||
debugDefaultTargetPlatformOverride = null;
|
||||
});
|
||||
});
|
||||
|
||||
group('state', () {
|
||||
test('reads the Android spatializer facts', () {
|
||||
final state = SpatialAudioState.fromMap(
|
||||
_android(headTracking: true, stereo: true, multichannel: true),
|
||||
);
|
||||
expect(state.platform, 'android');
|
||||
expect(state.available, isTrue);
|
||||
expect(state.enabled, isTrue);
|
||||
expect(state.headTracking, isTrue);
|
||||
expect(state.stereo, isTrue);
|
||||
expect(state.multichannel, isTrue);
|
||||
});
|
||||
|
||||
test('an Android release without a spatializer has none', () {
|
||||
final state = SpatialAudioState.fromMap(const {
|
||||
'platform': 'android',
|
||||
'sdk': 31,
|
||||
'spatializer': null,
|
||||
});
|
||||
expect(state.available, isFalse);
|
||||
expect(
|
||||
spatialAudioStatus(
|
||||
enabled: true,
|
||||
platform: SpatialAudioPlatform.android,
|
||||
state: state,
|
||||
),
|
||||
SpatialAudioStatus.unavailable,
|
||||
);
|
||||
});
|
||||
|
||||
test('reads the iOS output port', () {
|
||||
final state = SpatialAudioState.fromMap(const {
|
||||
'platform': 'ios',
|
||||
'enabled': true,
|
||||
});
|
||||
expect(state.enabled, isTrue);
|
||||
expect(state.available, isNull);
|
||||
});
|
||||
});
|
||||
|
||||
group('status', () {
|
||||
SpatialAudioStatus? android(
|
||||
Map<String, Object?> map, {
|
||||
bool enabled = true,
|
||||
UsbAudioStatus? usb,
|
||||
}) => spatialAudioStatus(
|
||||
enabled: enabled,
|
||||
platform: SpatialAudioPlatform.android,
|
||||
state: SpatialAudioState.fromMap(map),
|
||||
usb: usb,
|
||||
);
|
||||
|
||||
test('Android maps the spatializer to what it does', () {
|
||||
expect(
|
||||
android(_android(available: false, stereo: true)),
|
||||
SpatialAudioStatus.unavailable,
|
||||
);
|
||||
expect(
|
||||
android(_android(enabled: false, stereo: true)),
|
||||
SpatialAudioStatus.systemOff,
|
||||
);
|
||||
expect(android(_android(stereo: true)), SpatialAudioStatus.stereo);
|
||||
expect(
|
||||
android(_android(stereo: true, headTracking: true)),
|
||||
SpatialAudioStatus.stereoHeadTracking,
|
||||
);
|
||||
expect(
|
||||
android(_android(multichannel: true, headTracking: true)),
|
||||
SpatialAudioStatus.multichannelOnly,
|
||||
);
|
||||
expect(android(_android()), SpatialAudioStatus.unavailable);
|
||||
});
|
||||
|
||||
test('the direct Android outputs never reach the spatializer', () {
|
||||
final state = _android(stereo: true);
|
||||
expect(
|
||||
android(
|
||||
state,
|
||||
usb: const UsbAudioStatus(reason: 'active', driver: 'usb_direct'),
|
||||
),
|
||||
SpatialAudioStatus.directUsb,
|
||||
);
|
||||
expect(
|
||||
android(state, usb: const UsbAudioStatus(reason: 'active')),
|
||||
SpatialAudioStatus.directUsb,
|
||||
);
|
||||
expect(
|
||||
android(
|
||||
state,
|
||||
usb: const UsbAudioStatus(
|
||||
reason: 'active',
|
||||
driver: 'aaudio_exclusive',
|
||||
),
|
||||
),
|
||||
SpatialAudioStatus.directAAudio,
|
||||
);
|
||||
// Only a playing direct path counts.
|
||||
expect(
|
||||
android(state, usb: const UsbAudioStatus(reason: 'unsupported')),
|
||||
SpatialAudioStatus.stereo,
|
||||
);
|
||||
});
|
||||
|
||||
test('no status while the setting is off or nothing is known', () {
|
||||
expect(android(_android(stereo: true), enabled: false), isNull);
|
||||
expect(
|
||||
spatialAudioStatus(
|
||||
enabled: true,
|
||||
platform: SpatialAudioPlatform.android,
|
||||
),
|
||||
isNull,
|
||||
);
|
||||
expect(
|
||||
spatialAudioStatus(enabled: true, platform: SpatialAudioPlatform.none),
|
||||
isNull,
|
||||
);
|
||||
});
|
||||
|
||||
test('iOS follows the output port', () {
|
||||
SpatialAudioStatus? ios(bool? on) => spatialAudioStatus(
|
||||
enabled: true,
|
||||
platform: SpatialAudioPlatform.ios,
|
||||
state: SpatialAudioState(platform: 'ios', enabled: on),
|
||||
);
|
||||
expect(ios(true), SpatialAudioStatus.outputOn);
|
||||
expect(ios(false), SpatialAudioStatus.outputOff);
|
||||
expect(ios(null), isNull);
|
||||
});
|
||||
|
||||
test('macOS names exclusive output only', () {
|
||||
SpatialAudioStatus? macos({required bool exclusive}) =>
|
||||
spatialAudioStatus(
|
||||
enabled: true,
|
||||
platform: SpatialAudioPlatform.macos,
|
||||
exclusiveOutput: exclusive,
|
||||
);
|
||||
expect(macos(exclusive: true), SpatialAudioStatus.exclusiveOutput);
|
||||
expect(macos(exclusive: false), isNull);
|
||||
});
|
||||
|
||||
test('every status has its message', () {
|
||||
expect(
|
||||
{
|
||||
for (final status in SpatialAudioStatus.values)
|
||||
status: spatialAudioStatusLabel(_l10n, status),
|
||||
},
|
||||
{
|
||||
SpatialAudioStatus.directUsb: 'Not used with direct USB output.',
|
||||
SpatialAudioStatus.directAAudio:
|
||||
'Not used with AAudio exclusive output.',
|
||||
SpatialAudioStatus.exclusiveOutput: 'Not used with exclusive output.',
|
||||
SpatialAudioStatus.systemOff:
|
||||
'Spatial audio is off in system settings.',
|
||||
SpatialAudioStatus.unavailable:
|
||||
'Not available on this device or output.',
|
||||
SpatialAudioStatus.stereo: 'Stereo music is spatialized.',
|
||||
SpatialAudioStatus.stereoHeadTracking:
|
||||
'Stereo music is spatialized, with head tracking available.',
|
||||
SpatialAudioStatus.multichannelOnly:
|
||||
'Only multichannel songs (such as 5.1 or Dolby Atmos) are '
|
||||
'spatialized on this device.',
|
||||
SpatialAudioStatus.outputOn: 'Spatial audio is on for this output.',
|
||||
SpatialAudioStatus.outputOff:
|
||||
'Not available with this output, or turned off in Control '
|
||||
'Center.',
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
for (final mornye in [false, true]) {
|
||||
testWidgets('Android shows the live status and turns it off ($mornye)', (
|
||||
tester,
|
||||
) async {
|
||||
debugAndroidAudioSettingsOverride = true;
|
||||
_mockState(_android(stereo: true, headTracking: true));
|
||||
final container = await _pumpPage(tester, mornye: mornye);
|
||||
var item = await _scrollToRow(tester);
|
||||
expect(item.value, isTrue);
|
||||
expect(item.subtitle, startsWith(_l10n.spatialAudioDescription));
|
||||
expect(
|
||||
item.subtitle,
|
||||
endsWith('Stereo music is spatialized, with head tracking available.'),
|
||||
);
|
||||
expect(item.subtitle, isNot(contains(_l10n.spatialAudioNextSong)));
|
||||
|
||||
usbAudioStatus.value = const UsbAudioStatus(
|
||||
reason: 'active',
|
||||
driver: 'usb_direct',
|
||||
);
|
||||
await tester.pump();
|
||||
item = tester.widget<SettingsSwitchItem>(_row);
|
||||
expect(item.subtitle, endsWith('Not used with direct USB output.'));
|
||||
|
||||
await tester.tap(find.text(_title));
|
||||
await tester.pumpAndSettle();
|
||||
expect(container.read(settingsProvider).spatialAudio, isFalse);
|
||||
item = tester.widget<SettingsSwitchItem>(_row);
|
||||
expect(item.value, isFalse);
|
||||
expect(item.subtitle, _l10n.spatialAudioDescription);
|
||||
expect(tester.takeException(), isNull);
|
||||
});
|
||||
}
|
||||
|
||||
testWidgets('the old Android engine applies it from the next song', (
|
||||
tester,
|
||||
) async {
|
||||
debugAndroidAudioSettingsOverride = true;
|
||||
_mockState(_android(enabled: false, stereo: true));
|
||||
await _pumpPage(
|
||||
tester,
|
||||
mornye: false,
|
||||
settings: const AppSettings(media3Engine: false),
|
||||
);
|
||||
final item = await _scrollToRow(tester);
|
||||
expect(item.enabled, isTrue);
|
||||
expect(
|
||||
item.subtitle,
|
||||
[
|
||||
_l10n.spatialAudioDescription,
|
||||
_l10n.spatialAudioNextSong,
|
||||
'Spatial audio is off in system settings.',
|
||||
].join('\n\n'),
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('a state change updates the status line', (tester) async {
|
||||
debugAndroidAudioSettingsOverride = true;
|
||||
MockStreamHandlerEventSink? sink;
|
||||
_messenger.setMockStreamHandler(
|
||||
_events,
|
||||
MockStreamHandler.inline(
|
||||
onListen: (_, events) {
|
||||
sink = events;
|
||||
events.success(_android(multichannel: true));
|
||||
},
|
||||
),
|
||||
);
|
||||
addTearDown(() => _messenger.setMockStreamHandler(_events, null));
|
||||
await _pumpPage(tester, mornye: false);
|
||||
var item = await _scrollToRow(tester);
|
||||
expect(item.subtitle, endsWith(_l10n.spatialAudioMultichannelOnly));
|
||||
sink!.success(_android(available: false));
|
||||
await tester.pumpAndSettle();
|
||||
item = tester.widget<SettingsSwitchItem>(_row);
|
||||
expect(item.subtitle, endsWith(_l10n.spatialAudioUnavailable));
|
||||
});
|
||||
|
||||
for (final mornye in [false, true]) {
|
||||
testWidgets('iOS has its own Audio output group ($mornye)', (tester) async {
|
||||
debugSpatialAudioPlatformOverride = SpatialAudioPlatform.ios;
|
||||
_mockState(const {'platform': 'ios', 'enabled': false});
|
||||
await _pumpPage(tester, mornye: mornye);
|
||||
final item = await _scrollToRow(tester);
|
||||
expect(item.value, isTrue);
|
||||
expect(item.subtitle, endsWith(_l10n.spatialAudioOutputOff));
|
||||
expect(find.text(_l10n.newPlaybackEngine), findsNothing);
|
||||
expect(tester.takeException(), isNull);
|
||||
});
|
||||
}
|
||||
|
||||
testWidgets(
|
||||
'macOS names exclusive output in the desktop group',
|
||||
variant: TargetPlatformVariant.only(TargetPlatform.macOS),
|
||||
(tester) async {
|
||||
debugDesktopPlatformOverride = true;
|
||||
await _pumpPage(
|
||||
tester,
|
||||
mornye: false,
|
||||
settings: const AppSettings(exclusiveOutput: true),
|
||||
);
|
||||
final item = await _scrollToRow(tester);
|
||||
expect(item.subtitle, endsWith('Not used with exclusive output.'));
|
||||
expect(item.showDivider, isFalse);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'Windows and Linux have no spatial audio row',
|
||||
variant: TargetPlatformVariant({
|
||||
TargetPlatform.windows,
|
||||
TargetPlatform.linux,
|
||||
}),
|
||||
(tester) async {
|
||||
debugDesktopPlatformOverride = true;
|
||||
await _pumpPage(tester, mornye: false);
|
||||
for (var i = 0; i < 10; i++) {
|
||||
await tester.drag(find.byType(Scrollable).first, const Offset(0, -600));
|
||||
await tester.pumpAndSettle();
|
||||
expect(_row, findsNothing);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -87,6 +87,17 @@ marks the next player as playing before reporting completion, so a later
|
||||
`resume` from Dart confirms its state without restarting it. A new source
|
||||
or a release drops the link; no `nextPlayerId` unlinks it.
|
||||
|
||||
### Spatial audio
|
||||
|
||||
A `setSpatialAudio` method (argument `enabled`) on the per-player method
|
||||
channel stores `WrappedPlayer.spatialAudio` (true by default). On Android
|
||||
12L (API 32) and later, `AudioContextAndroid.buildAttributes` then sets the
|
||||
spatialization behavior to auto (true) or never (false). MediaPlayer reads
|
||||
its attributes as a source loads, so `MediaPlayerWrapper.setSource` sets
|
||||
them again after its reset and a change applies from the next source. The
|
||||
pub-hosted platform interface is unchanged; the app calls the method on the
|
||||
channel directly, like `setEqualizer`.
|
||||
|
||||
### Error state
|
||||
|
||||
`WrappedPlayer.onError` now marks the player as not prepared and not playing,
|
||||
@@ -114,6 +125,7 @@ AGP 7.3.1 to 9.4.1, android-junit5 1.7.1.1 to 2.0.2, compileSdk 36 to 37,
|
||||
Compared against the pub.dev 5.3.0 package in the local pub cache:
|
||||
|
||||
- `android/build.gradle`
|
||||
- `android/src/main/kotlin/xyz/luan/audioplayers/AudioContextAndroid.kt`
|
||||
- `android/src/main/kotlin/xyz/luan/audioplayers/AudioplayersPlugin.kt`
|
||||
- `android/src/main/kotlin/xyz/luan/audioplayers/player/MediaPlayerWrapper.kt`
|
||||
- `android/src/main/kotlin/xyz/luan/audioplayers/player/MixedOutput.kt` (new)
|
||||
|
||||
+4
@@ -36,6 +36,10 @@ API reference: https://developer.android.com/reference/android/media/audiofx/Dyn
|
||||
A `setNextPlayer` method links a prepared player to start the moment another
|
||||
completes, through `MediaPlayer.setNextMediaPlayer`, for gapless playback.
|
||||
|
||||
A `setSpatialAudio` method chooses the spatialization behavior of a player's
|
||||
audio attributes on Android 12L+: auto (the default) or never. It applies from
|
||||
the player's next source.
|
||||
|
||||
While a player plays on Android 9+, an effect with no processing in use stays
|
||||
enabled on its session, so the platform mixes its audio instead of sending it
|
||||
to a compressed offload output. System features that read the mixed output,
|
||||
|
||||
+12
-5
@@ -31,9 +31,10 @@ data class AudioContextAndroid(
|
||||
audioMode = AudioManager.MODE_NORMAL,
|
||||
)
|
||||
|
||||
fun setAttributesOnPlayer(player: MediaPlayer) {
|
||||
/** [spatialAudio] false asks Android 12L and later never to spatialize. */
|
||||
fun setAttributesOnPlayer(player: MediaPlayer, spatialAudio: Boolean = true) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
player.setAudioAttributes(buildAttributes())
|
||||
player.setAudioAttributes(buildAttributes(spatialAudio))
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
player.setAudioStreamType(getStreamType())
|
||||
@@ -41,11 +42,17 @@ data class AudioContextAndroid(
|
||||
}
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
|
||||
fun buildAttributes(): AudioAttributes {
|
||||
return Builder()
|
||||
fun buildAttributes(spatialAudio: Boolean = true): AudioAttributes {
|
||||
val builder = Builder()
|
||||
.setUsage(usageType)
|
||||
.setContentType(contentType)
|
||||
.build()
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S_V2) {
|
||||
builder.setSpatializationBehavior(
|
||||
if (spatialAudio) AudioAttributes.SPATIALIZATION_BEHAVIOR_AUTO
|
||||
else AudioAttributes.SPATIALIZATION_BEHAVIOR_NEVER,
|
||||
)
|
||||
}
|
||||
return builder.build()
|
||||
}
|
||||
|
||||
@Deprecated("This is used for Android older than LOLLIPOP", replaceWith = ReplaceWith("buildAttributes"))
|
||||
|
||||
third_party/audioplayers_android/android/src/main/kotlin/xyz/luan/audioplayers/AudioplayersPlugin.kt
Vendored
+3
@@ -171,6 +171,9 @@ class AudioplayersPlugin : FlutterPlugin {
|
||||
?: error("Equalizer configuration is required")
|
||||
player.setEqualizer(configuration)
|
||||
}
|
||||
"setSpatialAudio" -> {
|
||||
player.spatialAudio = call.argument<Boolean>("enabled") ?: error("enabled is required")
|
||||
}
|
||||
"setSourceUrl" -> {
|
||||
val url = call.argument<String>("url") ?: error("url is required")
|
||||
val isLocal = call.argument<Boolean>("isLocal") ?: false
|
||||
|
||||
+4
-2
@@ -53,7 +53,7 @@ class MediaPlayerWrapper(
|
||||
if (!released) wrappedPlayer.onBuffering(percent)
|
||||
}
|
||||
}
|
||||
wrappedPlayer.context.setAttributesOnPlayer(mediaPlayer)
|
||||
wrappedPlayer.context.setAttributesOnPlayer(mediaPlayer, wrappedPlayer.spatialAudio)
|
||||
if (wrappedPlayer.context.stayAwake) {
|
||||
mediaPlayer.setWakeMode(wrappedPlayer.applicationContext, PowerManager.PARTIAL_WAKE_LOCK)
|
||||
}
|
||||
@@ -86,6 +86,8 @@ class MediaPlayerWrapper(
|
||||
|
||||
override fun setSource(source: Source) {
|
||||
reset()
|
||||
// Idle again, so the current spatialization choice applies to this source.
|
||||
wrappedPlayer.context.setAttributesOnPlayer(mediaPlayer, wrappedPlayer.spatialAudio)
|
||||
source.setForMediaPlayer(mediaPlayer)
|
||||
}
|
||||
|
||||
@@ -126,7 +128,7 @@ class MediaPlayerWrapper(
|
||||
}
|
||||
|
||||
override fun updateContext(context: AudioContextAndroid) {
|
||||
context.setAttributesOnPlayer(mediaPlayer)
|
||||
context.setAttributesOnPlayer(mediaPlayer, wrappedPlayer.spatialAudio)
|
||||
if (context.stayAwake) {
|
||||
mediaPlayer.setWakeMode(wrappedPlayer.applicationContext, PowerManager.PARTIAL_WAKE_LOCK)
|
||||
}
|
||||
|
||||
+7
@@ -30,6 +30,13 @@ class WrappedPlayer internal constructor(
|
||||
/** The player MediaPlayer starts the moment this one completes. */
|
||||
private var next: WrappedPlayer? = null
|
||||
|
||||
/**
|
||||
* Whether the platform may spatialize this player's audio (behavior auto)
|
||||
* or must not (never). MediaPlayer reads its attributes as a source loads,
|
||||
* so a change applies from the next source.
|
||||
*/
|
||||
var spatialAudio = true
|
||||
|
||||
fun setEqualizer(configuration: Map<String, Any?>) {
|
||||
val output = player as? MediaPlayerWrapper
|
||||
if (output != null) output.setEqualizer(configuration)
|
||||
|
||||
@@ -63,6 +63,16 @@ without the precise key, from the URL without the fragment, so it starts
|
||||
at once; its seeks may be approximate until it plays again from the whole
|
||||
file.
|
||||
|
||||
### Spatial audio
|
||||
|
||||
A `setSpatialAudio` method (argument `enabled`) sets
|
||||
`allowedAudioSpatializationFormats` (iOS 14, macOS 11 and later) on the
|
||||
player's current item and on every item it creates afterwards:
|
||||
`.monoStereoAndMultichannel` when true (the default), none when false.
|
||||
Upstream leaves AVFoundation's audio-only default, multichannel only. A
|
||||
missing value returns a `DarwinAudioError`. AVFoundation does not document
|
||||
whether a change on an item already playing takes effect at once.
|
||||
|
||||
### Packaging
|
||||
|
||||
- `darwin/audioplayers_darwin.podspec` adds
|
||||
|
||||
+2
@@ -9,6 +9,8 @@ The tap processes decoded PCM only, never video artwork or other apps' audio.
|
||||
EQ is bypassed when disabled. Android's native bit-perfect paths bypass EQ.
|
||||
AVPlayer external playback is disabled, so AirPlay receives the processed
|
||||
audio route instead of a full-screen AirPlay video session.
|
||||
A `setSpatialAudio` method lets the system spatialize mono, stereo and
|
||||
multichannel audio (the default) or nothing, on the current and later items.
|
||||
|
||||
The graphic EQ curve is approximate across output engines: Android uses
|
||||
DynamicsProcessing; Darwin uses cascaded RBJ peaking biquads. Preset gains and
|
||||
|
||||
+6
@@ -222,6 +222,12 @@ public class AudioplayersDarwinPlugin: NSObject, FlutterPlugin {
|
||||
result(FlutterError(code: "DarwinAudioError", message: "Equalizer is unavailable for this source", details: error.localizedDescription))
|
||||
return
|
||||
}
|
||||
} else if method == "setSpatialAudio" {
|
||||
guard let enabled = args["enabled"] as? Bool else {
|
||||
result(FlutterError(code: "DarwinAudioError", message: "Missing spatial audio value", details: nil))
|
||||
return
|
||||
}
|
||||
player.setSpatialAudio(enabled)
|
||||
} else if method == "setNextPlayer" {
|
||||
let nextId = args["nextPlayerId"] as? String
|
||||
player.next = nextId.flatMap { self.getPlayer(playerId: $0) }
|
||||
|
||||
+18
@@ -45,6 +45,23 @@ enum ReleaseMode: String {
|
||||
if player.currentItem === item { equalizerAttached = true }
|
||||
}
|
||||
}
|
||||
/// Whether the system may spatialize this player's audio: mono, stereo
|
||||
/// and multichannel when true, nothing when false. Applies to the current
|
||||
/// item and every new one.
|
||||
private var spatialAudio = true
|
||||
|
||||
func setSpatialAudio(_ enabled: Bool) {
|
||||
spatialAudio = enabled
|
||||
if let item = player.currentItem { Self.applySpatialAudio(enabled, to: item) }
|
||||
}
|
||||
|
||||
/// Audio-only items otherwise default to multichannel only.
|
||||
static func applySpatialAudio(_ enabled: Bool, to item: AVPlayerItem) {
|
||||
if #available(iOS 14.0, macOS 11.0, *) {
|
||||
item.allowedAudioSpatializationFormats = enabled ? .monoStereoAndMultichannel : []
|
||||
}
|
||||
}
|
||||
|
||||
private var playbackRate: Double
|
||||
private var volume: Double
|
||||
private var url: String?
|
||||
@@ -247,6 +264,7 @@ enum ReleaseMode: String {
|
||||
}
|
||||
let playerItem = AVPlayerItem(asset: AVURLAsset(url: assetUrl, options: options))
|
||||
playerItem.audioTimePitchAlgorithm = AVAudioTimePitchAlgorithm.timeDomain
|
||||
Self.applySpatialAudio(spatialAudio, to: playerItem)
|
||||
return playerItem
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user