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

@@ -3,6 +3,7 @@ package nl.astraeus.vst
import kotlin.math.max
import kotlin.math.min
import kotlin.math.pow
import kotlin.math.round
/**
* User: rnentjes
@@ -15,17 +16,17 @@ enum class Note(
val flat: String
) {
NONE("---", "---"),
NO02("",""),
NO03("",""),
NO04("",""),
NO05("",""),
NO06("",""),
NO07("",""),
NO08("",""),
NO09("",""),
NO10("",""),
NO11("",""),
NO12("",""),
No02("C--","C--"),
NO03("C#-","Db-"),
NO04("D--","D--"),
NO05("D#-","Eb-"),
NO06("E--","E--"),
NO07("F--","F--"),
NO08("F#-","Gb-"),
NO09("G--","G--"),
NO10("G#-","Ab-"),
NO11("A#-","Bb-"),
NO12("B--","B--"),
C0("C-0","C-0"),
C0s("C#0","Db0"),
D0("D-0","D-0"),
@@ -152,7 +153,7 @@ enum class Note(
;
// 69 = A4.ordinal
val freq: Double = 440.0 * 2.0.pow((ordinal - 69)/12.0)
val freq: Double = round(440.0 * 2.0.pow((ordinal - 69)/12.0) * 100.0) / 100.0
val cycleLength: Double = 1.0 / freq
var sampleDelta: Double = 0.0
@@ -162,14 +163,12 @@ enum class Note(
result = min(result, G9.ordinal)
result = max(result, C0.ordinal)
entries.firstOrNull { it.ordinal == result } ?: this
fromMidi(result)
} else {
this
}
companion object {
var sampleRate: Int = 44100
fun fromMidi(midi: Int): Note {
// todo: add check
return entries[midi]
@@ -177,10 +176,16 @@ enum class Note(
fun updateSampleRate(rate: Int) {
println("Setting sample rate to $rate")
sampleRate = rate
for (note in Note.entries) {
note.sampleDelta = (1.0 / sampleRate.toDouble()) / note.cycleLength
note.sampleDelta = (1.0 / rate.toDouble()) / note.cycleLength
}
}
}
}
// freq = 10Hz
// cycleLength = 0.1
// sampleRate = 48000
// sampleDelta = 4800
// (1.0 / freq) * sampleRate