Also search on name when setting midi port
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package nl.astraeus.vst
|
||||
|
||||
import kotlin.js.ExperimentalJsExport
|
||||
import kotlin.js.JsExport
|
||||
import kotlin.math.max
|
||||
import kotlin.math.min
|
||||
import kotlin.math.pow
|
||||
@@ -11,6 +13,8 @@ import kotlin.math.round
|
||||
* Time: 11:50
|
||||
*/
|
||||
|
||||
@ExperimentalJsExport
|
||||
@JsExport
|
||||
enum class Note(
|
||||
val sharp: String,
|
||||
val flat: String
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
package nl.astraeus.vst.string
|
||||
|
||||
import nl.astraeus.vst.Note
|
||||
import kotlin.js.ExperimentalJsExport
|
||||
import kotlin.js.JsExport
|
||||
import kotlin.math.round
|
||||
|
||||
expect fun randomDouble(): Double
|
||||
|
||||
@ExperimentalJsExport
|
||||
@JsExport
|
||||
class PhysicalString(
|
||||
val sampleRate: Int,
|
||||
var damping: Double,
|
||||
) {
|
||||
val sampleLength = 1.0 / sampleRate.toDouble()
|
||||
val maxLength = sampleRate / Note.G9.freq
|
||||
var length = 1
|
||||
val buffer = Array(maxLength.toInt() + 1) { 0.0 }
|
||||
var sample = 0
|
||||
var index = 0
|
||||
var remaining = 0.0
|
||||
var currentNote = Note.C4
|
||||
var available = true
|
||||
|
||||
fun pluck(note: Note, velocity: Double, smoothing: Int = 0) {
|
||||
available = false
|
||||
currentNote = note
|
||||
length = round(sampleRate / note.freq).toInt()
|
||||
sample = 0
|
||||
index = 0
|
||||
|
||||
for (i in 0..<length) {
|
||||
if (i < length / 2) {
|
||||
buffer[i] = randomDouble() * velocity
|
||||
} else {
|
||||
buffer[i] = -randomDouble() * velocity
|
||||
}
|
||||
//buffer[i] = (randomDouble() - 0.5) * 2.0 * velocity
|
||||
//buffer[i] = sin(PI * 2 * i/length)
|
||||
//buffer[i] = (i/length.toDouble() * 2.0) - 1.0 //if (i < length / 2) { 1.0 } else { -1.0 }
|
||||
}
|
||||
repeat(smoothing) {
|
||||
for (i in 0..<length) {
|
||||
tick()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun update(time: Double) {
|
||||
remaining += (time / 1000.0)
|
||||
while (remaining > sampleLength) {
|
||||
remaining -= sampleLength
|
||||
tick()
|
||||
}
|
||||
}
|
||||
|
||||
fun tick(): Double {
|
||||
val result = buffer[index]
|
||||
|
||||
var newValue = 0.0
|
||||
newValue += getValueFromBuffer(index + 1) * 0.2
|
||||
newValue += getValueFromBuffer(index + 2) * 0.3
|
||||
newValue += getValueFromBuffer(index + 3) * 0.3
|
||||
newValue += getValueFromBuffer(index + 4) * 0.2
|
||||
// newValue += getValueFromBuffer(index + 5) * 0.2
|
||||
// newValue += getValueFromBuffer(index + 6) * 0.3
|
||||
newValue *= damping
|
||||
|
||||
buffer[index] = newValue
|
||||
|
||||
index = (index + 1) % length
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
private fun getValueFromBuffer(index: Int): Double {
|
||||
return buffer[(index + length) % length]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package nl.astraeus.vst.string
|
||||
|
||||
import kotlin.random.Random
|
||||
|
||||
actual fun randomDouble(): Double = Random.nextDouble()
|
||||
@@ -0,0 +1,5 @@
|
||||
package nl.astraeus.vst.string
|
||||
|
||||
actual fun randomDouble(): Double {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
Reference in New Issue
Block a user