Overload synth() and wave() functions

This commit is contained in:
2020-03-20 19:41:03 +01:00
parent 900d3849f1
commit ff45e7e479
10 changed files with 54 additions and 25 deletions

View File

@@ -2,19 +2,41 @@ package io.smnp.ext.function
import io.smnp.callable.function.Function
import io.smnp.callable.function.FunctionDefinitionTool
import io.smnp.callable.signature.Signature
import io.smnp.callable.signature.Signature.Companion.simple
import io.smnp.ext.synth.Synthesizer
import io.smnp.ext.synth.Wave
import io.smnp.ext.synth.WaveCompiler
import io.smnp.type.enumeration.DataType
import io.smnp.type.enumeration.DataType.INT
import io.smnp.type.matcher.Matcher
import io.smnp.type.matcher.Matcher.Companion.listOf
import io.smnp.type.model.Value
class SynthFunction : Function("synth") {
override fun define(new: FunctionDefinitionTool) {
new function simple(listOf(INT)) body { _, (wave) ->
val bytes = (wave.value as List<Value>).map { (it.value as Int).toByte() }.toByteArray()
Synthesizer.synth(Wave(bytes))
Value.void()
}
new function Signature.vararg(
listOf(DataType.NOTE, INT, DataType.STRING),
Matcher.mapOfMatchers(Matcher.ofType(DataType.STRING), Matcher.anyType())
) body { _, (config, vararg) ->
val compiler = WaveCompiler(config, Synthesizer.SAMPLING_RATE)
val wave = compiler.compileLines(vararg.unwrapCollections() as List<List<Value>>)
Synthesizer.synth(wave)
Value.void()
}
new function Signature.vararg(listOf(DataType.NOTE, INT, DataType.STRING)) body { _, (vararg) ->
val compiler = WaveCompiler(Value.map(emptyMap()), Synthesizer.SAMPLING_RATE)
val wave = compiler.compileLines(vararg.unwrapCollections() as List<List<Value>>)
Synthesizer.synth(wave)
Value.void()
}
}
}

View File

@@ -14,7 +14,6 @@ import io.smnp.type.model.Value
class WaveFunction : Function("wave") {
override fun define(new: FunctionDefinitionTool) {
new function Signature.vararg(
listOf(NOTE, INT, STRING),
@@ -26,5 +25,13 @@ class WaveFunction : Function("wave") {
Value.list(wave.bytes.map { Value.int(it.toInt()) }.toList())
}
new function Signature.vararg(listOf(NOTE, INT, STRING)) body { _, (vararg) ->
val compiler = WaveCompiler(Value.map(emptyMap()), Synthesizer.SAMPLING_RATE)
val wave = compiler.compileLines(vararg.unwrapCollections() as List<List<Value>>)
Value.list(wave.bytes.map { Value.int(it.toInt()) }.toList())
}
}
}

View File

@@ -3,10 +3,10 @@ package io.smnp.ext.synth
import io.smnp.type.enumeration.DataType.FLOAT
import io.smnp.type.matcher.Matcher.Companion.ofType
import io.smnp.type.model.Value
import io.smnp.util.config.MapConfigSchema
import io.smnp.util.config.ConfigMapSchema
object AdsrEnvelopeFactory : EnvelopeFactory {
private val schema = MapConfigSchema()
private val schema = ConfigMapSchema()
.required("p1", ofType(FLOAT))
.required("p2", ofType(FLOAT))
.required("p3", ofType(FLOAT))

View File

@@ -4,13 +4,13 @@ import io.smnp.error.CustomException
import io.smnp.type.enumeration.DataType
import io.smnp.type.matcher.Matcher
import io.smnp.type.model.Value
import io.smnp.util.config.MapConfigSchema
import io.smnp.util.config.ConfigMapSchema
interface EnvelopeFactory {
fun createEnvelope(config: Value): Envelope
companion object {
private val schema = MapConfigSchema()
private val schema = ConfigMapSchema()
.required("name", Matcher.ofType(DataType.STRING))
private val factories = mapOf(

View File

@@ -7,12 +7,12 @@ import io.smnp.math.Fraction
import io.smnp.type.enumeration.DataType
import io.smnp.type.matcher.Matcher
import io.smnp.type.model.Value
import io.smnp.util.config.MapConfigSchema
import io.smnp.util.config.ConfigMapSchema
import kotlin.math.pow
class WaveCompiler(config: Value, private val samplingRate: Double) {
private val semitone = 2.0.pow(1.0 / 12.0)
private val schema = MapConfigSchema()
private val schema = ConfigMapSchema()
.optional("bpm", Matcher.ofType(DataType.INT), Value.int(120))
.optional(
"overtones", Matcher.listOf(DataType.FLOAT), Value.list(