Listen to midi

This commit is contained in:
2024-06-17 21:06:39 +02:00
parent 68b7ffffa8
commit 94dec1f636
6 changed files with 160 additions and 46 deletions

View File

@@ -9,6 +9,7 @@ import nl.astraeus.vst.sampleRate
import org.khronos.webgl.ArrayBuffer
import org.khronos.webgl.Float32Array
import org.khronos.webgl.Int32Array
import org.khronos.webgl.Uint8Array
import org.khronos.webgl.get
import org.khronos.webgl.set
import org.w3c.dom.MessageEvent
@@ -79,6 +80,13 @@ class VstChipProcessor : AudioWorkletProcessor() {
}
is ArrayBuffer -> {
}
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)
}
@@ -102,8 +110,12 @@ class VstChipProcessor : AudioWorkletProcessor() {
}
}
}
0x90 -> {
0x80 -> {
if (bytes.length >= 2) {
val note = bytes[1]
noteOff(note)
}
}
}
}
@@ -113,7 +125,6 @@ class VstChipProcessor : AudioWorkletProcessor() {
for (i in 0 until POLYPHONICS) {
if (notes[i].note == note) {
notes[i].retrigger(velocity)
//console.log("Note retriggered", notes[i])
return
}
}
@@ -124,7 +135,9 @@ class VstChipProcessor : AudioWorkletProcessor() {
velocity
)
notes[i].state = NoteState.ON
console.log("Playing note", notes[i])
val n = Note.fromMidi(note)
console.log("Playing note: ${n.sharp} (${n.freq})")
break
}
}
@@ -134,7 +147,6 @@ class VstChipProcessor : AudioWorkletProcessor() {
for (i in 0 until POLYPHONICS) {
if (notes[i].note == note && notes[i].state == NoteState.ON) {
notes[i].state = NoteState.RELEASED
//console.log("Released note", notes[i])
break
}
}
@@ -163,14 +175,17 @@ class VstChipProcessor : AudioWorkletProcessor() {
note.releaseSamples--
targetVolume *= (note.releaseSamples / 10000f)
}
note.actualVolume += (targetVolume - note.actualVolume) * 0.01f
note.actualVolume += (targetVolume - note.actualVolume) * 0.005f
if (note.state == NoteState.RELEASED && note.actualVolume <= 0) {
note.state = NoteState.OFF
}
left[i] = left[i] + sin(note.cycleOffset * PI2).toFloat() * note.actualVolume
right[i] = right[i] + sin(note.cycleOffset * PI2).toFloat() * note.actualVolume
left[i] = left[i] + sin(note.cycleOffset * PI2).toFloat() * note.actualVolume * 0.3f
right[i] = right[i] + sin(note.cycleOffset * PI2).toFloat() * note.actualVolume * 0.3f
//left[i] = left[i] + if (note.cycleOffset < 0.5) { 0.3f } else { -0.3f } * note.actualVolume //sin(note.cycleOffset * PI2).toFloat() * note.actualVolume * 0.3f
//right[i] = right[i] + if (note.cycleOffset < 0.5) { 0.3f } else { -0.3f } * note.actualVolume //sin(note.cycleOffset * PI2).toFloat() * note.actualVolume * 0.3f
note.cycleOffset += sampleDelta
if (note.cycleOffset > 1f) {