Add channel selection

This commit is contained in:
2024-06-26 14:10:03 +02:00
parent 2bcd1e0b71
commit f7e088bb67
6 changed files with 53 additions and 29 deletions

View File

@@ -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]