Move stuff into base

This commit is contained in:
2024-08-09 19:55:15 +02:00
parent 8df6a4fff6
commit 1eed613b2a
21 changed files with 40 additions and 902 deletions

View File

@@ -7,6 +7,8 @@ import kotlin.math.round
expect fun randomDouble(): Double
const val BUFFER_MULTIPLY = 4
@ExperimentalJsExport
@JsExport
class PhysicalString(
@@ -14,9 +16,9 @@ class PhysicalString(
var damping: Double,
) {
val sampleLength = 1.0 / sampleRate.toDouble()
val maxLength = sampleRate / Note.G9.freq
val maxLength = sampleRate / Note.NO01.freq
var length = 1
val buffer = Array(maxLength.toInt() + 1) { 0.0 }
val buffer = Array((maxLength * BUFFER_MULTIPLY).toInt() + 1) { 0.0 }
var sample = 0
var index = 0
var remaining = 0.0
@@ -26,7 +28,7 @@ class PhysicalString(
fun pluck(note: Note, velocity: Double, smoothing: Int = 0) {
available = false
currentNote = note
length = round(sampleRate / note.freq).toInt()
length = round(BUFFER_MULTIPLY * sampleRate / note.freq).toInt()
sample = 0
index = 0
@@ -37,7 +39,7 @@ class PhysicalString(
buffer[i] = -randomDouble() * velocity
}
//buffer[i] = (randomDouble() - 0.5) * 2.0 * velocity
//buffer[i] = sin(PI * 2 * i/length)
//buffer[i] = sin(PI * 2 * i/length) * randomDouble() * velocity
//buffer[i] = (i/length.toDouble() * 2.0) - 1.0 //if (i < length / 2) { 1.0 } else { -1.0 }
}
repeat(smoothing) {
@@ -58,18 +60,20 @@ class PhysicalString(
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
repeat(BUFFER_MULTIPLY) {
var newValue = 0.0
newValue += getValueFromBuffer(index + 1) * 0.1
newValue += getValueFromBuffer(index + 2) * 0.15
newValue += getValueFromBuffer(index + 3) * 0.25
newValue += getValueFromBuffer(index + 4) * 0.25
newValue += getValueFromBuffer(index + 5) * 0.15
newValue += getValueFromBuffer(index + 6) * 0.1
newValue *= damping
buffer[index] = newValue
buffer[index] = newValue
index = (index + 1) % length
index = (index + 1) % length
}
return result
}