Add support for velocity command to smnp.audio.synth module

This commit is contained in:
2020-04-06 14:06:20 +02:00
parent fbf559d6bd
commit 5dbaed2edf
3 changed files with 4 additions and 2 deletions

View File

@@ -59,7 +59,7 @@ abstract class SequenceCompiler {
command["velocity"]?.let {
val value = it as? Float ?: throw CustomException("Invalid parameter type: 'velocity' is supposed to be of float type")
velocity = (127.0 * value).toInt()
velocity = (127.0 * value.coerceIn(0.0F, 1.0F)).toInt()
}
noteOnTick

View File

@@ -8,6 +8,7 @@ import kotlin.math.pow
private val SEMITONE = 2.0.pow(1.0 / 12.0)
class CompilationParameters(config: ConfigMap) {
val velocity by lazy { (config["velocity"].value as Float).coerceIn(0.0F, 1.0F) }
val envelope by lazy { EnvelopeFactory.provideEnvelope(config["envelope"]) }
val overtones by lazy { (config["overtones"].unwrap() as List<Float>).map { it.toDouble() } }
val bpm by lazy { config["bpm"].value as Int }

View File

@@ -13,6 +13,7 @@ import kotlin.math.pow
class WaveCompiler(config: Value, private val samplingRate: Double) {
private val schema = ConfigMapSchema()
.optional("bpm", Matcher.ofType(DataType.INT), Value.int(120))
.optional("velocity", Matcher.ofType(DataType.FLOAT), Value.float(1.0F))
.optional(
"overtones", Matcher.listOf(DataType.FLOAT), Value.list(
listOf(
@@ -76,6 +77,6 @@ class WaveCompiler(config: Value, private val samplingRate: Double) {
Wave.sine(frequency * (overtone + 1), duration, samplingRate) * ratio
}.toTypedArray())
return parameters.envelope.apply(wave)
return parameters.envelope.apply(wave) * parameters.velocity.toDouble()
}
}