Refactor Value and EvaluatorOutput models in order to get rid of optionals(?)

This commit is contained in:
2020-03-14 13:25:42 +01:00
parent d8744670ed
commit 5b03f55cd4
30 changed files with 60 additions and 78 deletions

View File

@@ -22,8 +22,8 @@ class MidiFunction : Function("midi") {
mapOfMatchers(ofType(INT), allTypes())
) body { _, (config, lines) ->
val lines = (lines.value!! as List<Value>).map { it.value!! as List<Value> }
val parameters = configParametersMap(config.value!!)
val lines = (lines.value as List<Value>).map { it.value as List<Value> }
val parameters = configParametersMap(config.value)
MidiSequencer.playLines(lines, parameters)
Value.void()
}
@@ -32,11 +32,11 @@ class MidiFunction : Function("midi") {
mapOfMatchers(allTypes(), allTypes()),
mapOfMatchers(ofType(INT), listOfMatchers(listOf(NOTE, INT, STRING)))
) body { _, (config, channels) ->
val channels = (channels.value!! as Map<Value, Value>).map { (key, value) ->
key.value!! as Int to ((value.value!! as List<Value>).map { it.value!! as List<Value> })
val channels = (channels.value as Map<Value, Value>).map { (key, value) ->
key.value as Int to ((value.value as List<Value>).map { it.value as List<Value> })
}.toMap()
val parameters = configParametersMap(config.value!!)
val parameters = configParametersMap(config.value)
MidiSequencer.playChannels(channels, parameters)
Value.void()
@@ -45,10 +45,10 @@ class MidiFunction : Function("midi") {
private fun configParametersMap(config: Any): Map<String, Any> {
return (config as Map<Value, Value>)
.map { (key, value) -> key.value!! as String to value }
.map { (key, value) -> key.value as String to value }
.map { (key, value) ->
key to when (key) {
"bpm" -> if (value.type == INT) value.value!! else throw EvaluationException("Invalid parameter type: 'bpm' is supposed to be of int type")
"bpm" -> if (value.type == INT) value.value else throw EvaluationException("Invalid parameter type: 'bpm' is supposed to be of int type")
else -> value
}
}

View File

@@ -48,7 +48,7 @@ object MidiSequencer {
NOTE -> {
note(item, channel, noteOnTick, track)
}
INT -> noteOnTick + 4L * PPQ / (item.value!! as Int)
INT -> noteOnTick + 4L * PPQ / (item.value as Int)
STRING -> command(item, channel, noteOnTick, track)
else -> throw ShouldNeverReachThisLineException()
}
@@ -56,7 +56,7 @@ object MidiSequencer {
}
private fun command(item: Value, channel: Int, beginTick: Long, track: Track): Long {
val instruction = item.value!! as String
val instruction = item.value as String
if(instruction.isBlank()) {
throw EvaluationException("Empty strings are not allowed here")
}
@@ -73,7 +73,7 @@ object MidiSequencer {
}
private fun note(item: Value, channel: Int, noteOnTick: Long, track: Track): Long {
val note = item.value!! as Note
val note = item.value as Note
val noteDuration = ((if (note.dot) 1.5 else 1.0) * 4L * PPQ / note.duration).toLong()
val noteOffTick = noteOnTick + noteDuration
track.add(noteOn(note, channel, noteOnTick))