Generate sound
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
@file:OptIn(ExperimentalJsExport::class)
|
@file:OptIn(ExperimentalJsExport::class)
|
||||||
|
|
||||||
import org.khronos.webgl.Float64Array
|
import org.khronos.webgl.Float64Array
|
||||||
|
import org.khronos.webgl.get
|
||||||
import org.khronos.webgl.set
|
import org.khronos.webgl.set
|
||||||
import org.w3c.dom.MessageEvent
|
import org.w3c.dom.MessageEvent
|
||||||
import org.w3c.dom.MessagePort
|
import org.w3c.dom.MessagePort
|
||||||
@@ -11,10 +12,20 @@ import kotlin.math.sin
|
|||||||
@JsExport
|
@JsExport
|
||||||
object WorkletProcessor {
|
object WorkletProcessor {
|
||||||
var port: MessagePort? = null
|
var port: MessagePort? = null
|
||||||
var offset: Double = 0.0
|
|
||||||
|
var offsets = Array(5) { 0.0 }
|
||||||
var counter: Int = 0
|
var counter: Int = 0
|
||||||
var note = Note.C0
|
var baseNote = Note.C2
|
||||||
var directionUp = true
|
val CARRAY = arrayOf(
|
||||||
|
Note.C1,
|
||||||
|
Note.C2,
|
||||||
|
Note.C3,
|
||||||
|
Note.C4,
|
||||||
|
Note.C5,
|
||||||
|
Note.C6,
|
||||||
|
Note.C7,
|
||||||
|
Note.C8,
|
||||||
|
)
|
||||||
|
|
||||||
@JsName("setPort")
|
@JsName("setPort")
|
||||||
fun setPort(port: MessagePort) {
|
fun setPort(port: MessagePort) {
|
||||||
@@ -30,9 +41,11 @@ object WorkletProcessor {
|
|||||||
"start" -> {
|
"start" -> {
|
||||||
println("Start worklet!")
|
println("Start worklet!")
|
||||||
}
|
}
|
||||||
|
|
||||||
"stop" -> {
|
"stop" -> {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else ->
|
else ->
|
||||||
console.error("Don't kow how to handle message", message)
|
console.error("Don't kow how to handle message", message)
|
||||||
}
|
}
|
||||||
@@ -40,42 +53,96 @@ object WorkletProcessor {
|
|||||||
|
|
||||||
@JsName("process")
|
@JsName("process")
|
||||||
fun process(samples: Int, left: Float64Array, right: Float64Array) {
|
fun process(samples: Int, left: Float64Array, right: Float64Array) {
|
||||||
|
var first = true
|
||||||
|
for (index in offsets.indices) {
|
||||||
|
var offset = offsets[index]
|
||||||
|
var tmpCounter = counter
|
||||||
|
var offsetNote = baseNote.transpose(index * 12)
|
||||||
|
var delta = offsetNote.sampleDelta
|
||||||
|
|
||||||
for (sample in 0 until samples) {
|
for (sample in 0 until samples) {
|
||||||
|
var value = sin(offset * 2 * PI)
|
||||||
|
offset += delta
|
||||||
|
|
||||||
|
val noteProgress = tmpCounter % 5000
|
||||||
|
if (noteProgress == 0) {
|
||||||
|
offsetNote = offsetNote.transpose(1)
|
||||||
|
if (offsetNote in CARRAY) {
|
||||||
|
offsetNote = offsetNote.transpose(-12)
|
||||||
|
}
|
||||||
|
delta = offsetNote.sampleDelta
|
||||||
|
}
|
||||||
|
value *= (1.0 - noteProgress / 5000.0)
|
||||||
|
|
||||||
|
if (first) {
|
||||||
|
left[sample] = value
|
||||||
|
right[sample] = value
|
||||||
|
} else {
|
||||||
|
left[sample] = left[sample] + value
|
||||||
|
right[sample] = right[sample] + value
|
||||||
|
}
|
||||||
|
tmpCounter++
|
||||||
|
}
|
||||||
|
|
||||||
|
offsets[index] = offset
|
||||||
|
first = false
|
||||||
|
}
|
||||||
|
|
||||||
|
var tmpCounter = counter
|
||||||
|
for (sample in 0 until samples) {
|
||||||
|
if (tmpCounter % 5000 == 0) {
|
||||||
|
baseNote = baseNote.transpose(1)
|
||||||
|
if (baseNote in CARRAY) {
|
||||||
|
baseNote = baseNote.transpose(-12)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tmpCounter++
|
||||||
|
}
|
||||||
|
counter += samples
|
||||||
|
|
||||||
|
val nrOffsets = offsets.size
|
||||||
|
for (sample in 0 until samples) {
|
||||||
|
left[sample] = left[sample] / nrOffsets
|
||||||
|
right[sample] = right[sample] / nrOffsets
|
||||||
|
}
|
||||||
|
|
||||||
|
/* for (sample in 0 until samples) {
|
||||||
val noteProgress = counter % 5000
|
val noteProgress = counter % 5000
|
||||||
if (noteProgress == 0) {
|
if (noteProgress == 0) {
|
||||||
note = note.transpose(
|
baseNote = baseNote.transpose(1)
|
||||||
if (directionUp) {
|
if (baseNote == Note.C3s) {
|
||||||
1
|
baseNote = Note.C2
|
||||||
} else {
|
|
||||||
-1
|
|
||||||
}
|
|
||||||
)
|
|
||||||
if (note == Note.B8) {
|
|
||||||
directionUp = false
|
|
||||||
}
|
|
||||||
if (note == Note.C0) {
|
|
||||||
directionUp = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
offset = (offset + note.sampleDelta) % 1.0
|
var value = 0.0
|
||||||
|
var volumeAdjust = (Note.C3.ordinal - baseNote.ordinal) / 12.0
|
||||||
|
for (index in 0 until offsets.size) {
|
||||||
|
offsets[index] = (offsets[index] + (baseNote.transpose(index * 12).sampleDelta)) % 1.0
|
||||||
|
|
||||||
var value = sin(offset * 2 * PI) +
|
var partValue = sin(offsets[index] * 2 * PI)
|
||||||
sin(offset * 4 * PI) * 0.6 +
|
if (index == 0) {
|
||||||
sin(offset * 6 * PI) * 0.35 +
|
partValue *= volumeAdjust
|
||||||
sin(offset * 8 * PI) * 0.2
|
} else if (index == offsets.size - 1) {
|
||||||
|
partValue *= (1.0 - volumeAdjust)
|
||||||
|
}
|
||||||
|
|
||||||
|
value += partValue
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
value /= offsets.size
|
||||||
// simple amplitude
|
// simple amplitude
|
||||||
value *= if (noteProgress < 0.1) {
|
value *= if (noteProgress < 0.1) {
|
||||||
(noteProgress / 500.0)
|
(noteProgress / 500.0)
|
||||||
} else {
|
} else {
|
||||||
(1.0 - (noteProgress - 0.1) / 4500.0)
|
(1.0 - (noteProgress - 0.1) / 4500.0)
|
||||||
}
|
}
|
||||||
//val value = if (offset < 0.5) { 1.0 } else { -1.0 }
|
|
||||||
|
|
||||||
left[sample] = value
|
left[sample] = value
|
||||||
right[sample] = value
|
right[sample] = value
|
||||||
|
|
||||||
counter++
|
counter++
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user