diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/websocket/Messages.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/websocket/Messages.kt index bf95baa50..a2d3eb6c0 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/websocket/Messages.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/websocket/Messages.kt @@ -33,20 +33,20 @@ class Messages { AppendToPlaylist("append_to_playlist"), RemoveTracksFromPlaylist("remove_tracks_from_playlist"), ListOutputDrivers("list_output_drivers"), - SetDefaultOutputDriver("set_default_output_driver"); + SetDefaultOutputDriver("set_default_output_driver"), + GetGainSettings("get_gain_settings"), + UpdateGainSettings("update_gain_settings"); override fun toString(): String = rawValue fun matches(name: String): Boolean = (rawValue == name) companion object { - fun from(rawValue: String): Request? { - for (value in Request.values()) { - if (value.toString() == rawValue) { - return value + Request.values().forEach { + if (it.toString() == rawValue) { + return it } } - return null } } @@ -61,12 +61,11 @@ class Messages { companion object { fun from(rawValue: String): Broadcast? { - for (value in Broadcast.values()) { - if (value.toString() == rawValue) { - return value + Broadcast.values().forEach { + if (it.toString() == rawValue) { + return it } } - return null } } @@ -102,6 +101,8 @@ class Messages { val SORT_ORDERS = "sort_orders" val DRIVER_NAME = "driver_name" val DEVICE_ID = "device_id" + val REPLAYGAIN_MODE = "replaygain_mode" + val PREAMP_GAIN = "preamp_gain" } } diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/websocket/SocketMessage.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/websocket/SocketMessage.kt index 29ab22928..e592b05de 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/websocket/SocketMessage.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/websocket/SocketMessage.kt @@ -111,6 +111,10 @@ class SocketMessage private constructor(val name: String, val id: String, val ty return defaultValue } + fun getJsonObject(): JSONObject { + return JSONObject(options.toString()) + } + @JvmOverloads fun getJsonObjectOption(key: String, defaultValue: JSONObject? = null): JSONObject? { if (options.has(key)) { try { diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/websocket/model/IDataProvider.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/websocket/model/IDataProvider.kt index 04f80976e..a15a96864 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/websocket/model/IDataProvider.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/websocket/model/IDataProvider.kt @@ -51,5 +51,8 @@ interface IDataProvider { fun listOutputDrivers(): Observable> fun setDefaultOutputDriver(driverName: String, deviceId: String = "default"): Observable + fun getGainSettings(): Observable + fun updateGainSettings(replayGainMode: IGainSettings.ReplayGainMode, preampGain: Float): Observable + val state: State } diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/websocket/model/IGainSettings.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/websocket/model/IGainSettings.kt new file mode 100644 index 000000000..ce8def1ce --- /dev/null +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/websocket/model/IGainSettings.kt @@ -0,0 +1,23 @@ +package io.casey.musikcube.remote.service.websocket.model + +interface IGainSettings { + enum class ReplayGainMode(val rawValue: String) { + Disabled("disabled"), + Album("album"), + Track("track"); + + companion object { + fun find(raw: String): ReplayGainMode { + values().forEach { + if (it.rawValue == raw) { + return it + } + } + return Disabled + } + } + } + + val replayGainMode: ReplayGainMode + val preampGain: Float +} \ No newline at end of file diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/websocket/model/impl/remote/RemoteDataProvider.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/websocket/model/impl/remote/RemoteDataProvider.kt index 98dd220dc..dec760f6e 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/websocket/model/impl/remote/RemoteDataProvider.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/websocket/model/impl/remote/RemoteDataProvider.kt @@ -423,6 +423,30 @@ class RemoteDataProvider(private val service: WebSocketService) : IDataProvider .observeOn(AndroidSchedulers.mainThread()) } + override fun getGainSettings(): Observable { + val message = SocketMessage.Builder + .request(Messages.Request.GetGainSettings) + .build() + + return service.observe(message, client) + .flatMap { socketMessage -> + Observable.just(RemoteGainSettings(socketMessage.getJsonObject())) + } + .observeOn(AndroidSchedulers.mainThread()) + } + + override fun updateGainSettings(replayGainMode: IGainSettings.ReplayGainMode, preampGain: Float): Observable { + val message = SocketMessage.Builder + .request(Messages.Request.UpdateGainSettings) + .addOption(Messages.Key.REPLAYGAIN_MODE, replayGainMode.rawValue) + .addOption(Messages.Key.PREAMP_GAIN, preampGain) + .build() + + return service.observe(message, client) + .flatMap { socketMessage -> isSuccessful(socketMessage) } + .observeOn(AndroidSchedulers.mainThread()) + } + override fun observeState(): Observable> = connectionStatePublisher.observeOn(AndroidSchedulers.mainThread()) diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/websocket/model/impl/remote/RemoteGainSettings.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/websocket/model/impl/remote/RemoteGainSettings.kt new file mode 100644 index 000000000..b4acdbf7a --- /dev/null +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/websocket/model/impl/remote/RemoteGainSettings.kt @@ -0,0 +1,12 @@ +package io.casey.musikcube.remote.service.websocket.model.impl.remote + +import io.casey.musikcube.remote.service.websocket.Messages +import io.casey.musikcube.remote.service.websocket.model.IGainSettings +import org.json.JSONObject + +class RemoteGainSettings(val json: JSONObject): IGainSettings { + override val replayGainMode: IGainSettings.ReplayGainMode + get() = IGainSettings.ReplayGainMode.find(json.optString(Messages.Key.REPLAYGAIN_MODE, "disabled")) + override val preampGain: Float + get() = json.optDouble(Messages.Key.PREAMP_GAIN, 0.0).toFloat() +} \ No newline at end of file