diff --git a/audio-worklet/src/jsMain/kotlin/nl/astraeus/vst/chip/ChipProcessor.kt b/audio-worklet/src/jsMain/kotlin/nl/astraeus/vst/chip/ChipProcessor.kt index a76f2a8..a65de46 100644 --- a/audio-worklet/src/jsMain/kotlin/nl/astraeus/vst/chip/ChipProcessor.kt +++ b/audio-worklet/src/jsMain/kotlin/nl/astraeus/vst/chip/ChipProcessor.kt @@ -59,6 +59,7 @@ enum class Waveform { @ExperimentalJsExport @JsExport class VstChipProcessor : AudioWorkletProcessor() { + var midiChannel = 0 val notes = Array(POLYPHONICS) { PlayingNote( 0 @@ -78,41 +79,58 @@ class VstChipProcessor : AudioWorkletProcessor() { } private fun handleMessage(message: MessageEvent) { - //console.log("VstChipProcessor: Received message", message) + console.log("VstChipProcessor: Received message:", message.data) val data = message.data - when (data) { - "test_on" -> { - playMidi(Int32Array(arrayOf(0x90, 60, 64))) - - } - "test_off" -> { - playMidi(Int32Array(arrayOf(0x90, 60, 0))) - } - is String -> { - } - is ArrayBuffer -> { - } - is Uint8Array -> { - val data32 = Int32Array(data.length) - for (i in 0 until data.length) { - data32[i] = (data[i].toInt() and 0xff) + try { + when (data) { + is String -> { + if (data.startsWith("set_channel")) { + val parts = data.split('\n') + if (parts.size == 2) { + midiChannel = parts[1].toInt() + println("Setting channel: $midiChannel") + } + } } - playMidi(data32) + + is Uint8Array -> { + val data32 = Int32Array(data.length) + for (i in 0 until data.length) { + data32[i] = (data[i].toInt() and 0xff) + } + playMidi(data32) + } + + is Int32Array -> { + playMidi(data) + } + + else -> + console.error("Don't kow how to handle message", message) } - is Int32Array -> { - playMidi(data) - } - else -> - console.error("Don't kow how to handle message", message) + } catch(e: Exception) { + console.log(e.message, e) } } private fun playMidi(bytes: Int32Array) { + console.log("playMidi", bytes) if (bytes.length > 0) { + var cmdByte = bytes[0] + val channelCmd = ((cmdByte shr 4) and 0xf) in 0x8 .. 0xe + val channel = cmdByte and 0xf + println("Channel cmd: $channelCmd") + if (channelCmd && channel != midiChannel) { + console.log("Wrong channel", midiChannel, bytes) + return + } + + cmdByte = cmdByte and 0xf0 + //console.log("Received", bytes) - when(bytes[0]) { + when(cmdByte) { 0x90 -> { if (bytes.length == 3) { val note = bytes[1] diff --git a/build.gradle.kts b/build.gradle.kts index 1754011..85d618d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -6,7 +6,6 @@ buildscript { plugins { kotlin("multiplatform") - kotlin("plugin.serialization") id("maven-publish") application } diff --git a/settings.common.gradle.kts b/settings.common.gradle.kts index 08e8c93..4230eaf 100644 --- a/settings.common.gradle.kts +++ b/settings.common.gradle.kts @@ -1,7 +1,6 @@ pluginManagement { plugins { kotlin("multiplatform") version "2.0.0" - kotlin("plugin.serialization") version "2.0.0" id("org.gradle.toolchains.foojay-resolver-convention") version "0.5.0" } repositories { diff --git a/src/jsMain/kotlin/nl/astraeus/vst/chip/audio/AudioModule.kt b/src/jsMain/kotlin/nl/astraeus/vst/chip/audio/AudioModule.kt index 82f582a..f5e835a 100644 --- a/src/jsMain/kotlin/nl/astraeus/vst/chip/audio/AudioModule.kt +++ b/src/jsMain/kotlin/nl/astraeus/vst/chip/audio/AudioModule.kt @@ -54,6 +54,9 @@ abstract class AudioNode( abstract fun onMessage(message: MessageEvent) open fun postMessage(msg: Any) { + if (port == null) { + console.log("postMessage port is NULL!") + } port?.postMessage(msg) } diff --git a/src/jsMain/kotlin/nl/astraeus/vst/chip/view/MainView.kt b/src/jsMain/kotlin/nl/astraeus/vst/chip/view/MainView.kt index 523bed5..5809c74 100644 --- a/src/jsMain/kotlin/nl/astraeus/vst/chip/view/MainView.kt +++ b/src/jsMain/kotlin/nl/astraeus/vst/chip/view/MainView.kt @@ -13,6 +13,7 @@ import kotlinx.html.h1 import kotlinx.html.input import kotlinx.html.js.onChangeFunction import kotlinx.html.js.onClickFunction +import kotlinx.html.js.onInputFunction import kotlinx.html.option import kotlinx.html.select import kotlinx.html.span @@ -111,9 +112,11 @@ object MainView : Komponent() { input { type = InputType.number value = Midi.inputChannel.toString() - onChangeFunction = { event -> + onInputFunction = { event -> val target = event.target as HTMLInputElement Midi.inputChannel = target.value.toInt() + println("onInput channel: ${Midi.inputChannel}") + VstChipWorklet.postMessage("set_channel\n${Midi.inputChannel}") } } } @@ -155,7 +158,7 @@ object MainView : Komponent() { input { type = InputType.number value = Midi.outputChannel.toString() - onChangeFunction = { event -> + onInputFunction = { event -> val target = event.target as HTMLInputElement Midi.outputChannel = target.value.toInt() } @@ -230,6 +233,8 @@ object MainView : Komponent() { margin(1.rem) padding(1.rem) backgroundColor(Css.currentStyle.buttonBackgroundColor) + borderColor(Css.currentStyle.buttonBorderColor) + borderWidth(Css.currentStyle.buttonBorderWidth) color(Css.currentStyle.mainFontColor) hover { diff --git a/src/jsMain/kotlin/nl/astraeus/vst/chip/view/css/Css.kt b/src/jsMain/kotlin/nl/astraeus/vst/chip/view/css/Css.kt index 8d3ab64..40b9709 100644 --- a/src/jsMain/kotlin/nl/astraeus/vst/chip/view/css/Css.kt +++ b/src/jsMain/kotlin/nl/astraeus/vst/chip/view/css/Css.kt @@ -13,7 +13,7 @@ class StyleDefinition( val inputBackgroundColor : Color = mainBackgroundColor.lighten(15), val buttonBackgroundColor : Color = mainBackgroundColor.lighten(15), val buttonBorderColor : Color = mainFontColor.changeAlpha(0.25), - val buttonBorderWidth : Measurement = 2.px, + val buttonBorderWidth : Measurement = 1.px, ) object NoTextSelectCls : CssName("no-text-select")