Create tuplet methods

This commit is contained in:
2020-03-16 20:57:59 +01:00
parent 5c0fe0daf3
commit 165e572203
3 changed files with 55 additions and 16 deletions

View File

@@ -17,7 +17,7 @@ data class Value(val type: DataType, val value: Any, val properties: Map<String,
get() = type.toString()
fun unwrapCollections(): Any {
return when(type) {
return when (type) {
DataType.LIST -> (value as List<Value>).map { it.unwrapCollections() }
DataType.MAP -> (value as Map<Value, Value>).map { (k, v) -> k.unwrapCollections() to v.unwrapCollections() }.toMap()
else -> this
@@ -25,7 +25,7 @@ data class Value(val type: DataType, val value: Any, val properties: Map<String,
}
fun unwrap(): Any {
return when(type) {
return when (type) {
DataType.LIST -> (value as List<Value>).map { it.unwrap() }
DataType.MAP -> (value as Map<Value, Value>).map { (k, v) -> k.unwrap() to v.unwrap() }.toMap()
else -> value
@@ -42,7 +42,7 @@ data class Value(val type: DataType, val value: Any, val properties: Map<String,
}
fun wrap(obj: Any): Value {
return when(obj) {
return when (obj) {
is Unit -> void()
is Int -> int(obj)
is Float -> float(obj)
@@ -73,7 +73,7 @@ data class Value(val type: DataType, val value: Any, val properties: Map<String,
fun string(value: String): Value {
return Value(
DataType.STRING, value, hashMapOf(
Pair("length", int(value.length))
"length" to int(value.length)
)
)
}
@@ -81,7 +81,7 @@ data class Value(val type: DataType, val value: Any, val properties: Map<String,
fun list(value: List<Value>): Value {
return Value(
DataType.LIST, value, hashMapOf(
Pair("size", int(value.size))
"size" to int(value.size)
)
)
}
@@ -89,9 +89,9 @@ data class Value(val type: DataType, val value: Any, val properties: Map<String,
fun map(value: Map<Value, Value>): Value {
return Value(
DataType.MAP, value, hashMapOf(
Pair("size", int(value.size)),
Pair("keys", list(value.keys.toList())),
Pair("values", list(value.values.toList()))
"size" to int(value.size),
"keys" to list(value.keys.toList()),
"values" to list(value.values.toList())
)
)
}
@@ -99,9 +99,14 @@ data class Value(val type: DataType, val value: Any, val properties: Map<String,
fun note(value: Note): Value {
return Value(
DataType.NOTE, value, hashMapOf(
Pair("pitch", string(value.pitch.toString())),
Pair("octave", int(value.octave)),
Pair("duration", float((value.duration.numerator / value.duration.denominator).toFloat()))
"pitch" to string(value.pitch.toString()),
"octave" to int(value.octave),
"duration" to map(
mapOf(
string("numerator") to int(value.duration.numerator),
string("denominator") to int(value.duration.denominator)
)
)
)
)
}

View File

@@ -22,5 +22,21 @@ class NoteConstructor : Function("Note") {
val note = Note(pitch, octave.value as Int, Fraction(1, duration.value as Int), dot.value as Boolean)
Value.note(note)
}
new function simple(
ofType(STRING),
ofType(INT),
ofType(INT),
ofType(INT)
) body { _, (pitchString, octave, durationNumerator, durationDenominator) ->
val pitch = Pitch.parse((pitchString.value as String).toLowerCase())
val note = Note(
pitch,
octave.value as Int,
Fraction(durationNumerator.value as Int, durationDenominator.value as Int),
false
)
Value.note(note)
}
}
}

View File

@@ -1,18 +1,18 @@
extend note as this {
function withOctave(octave: int) {
return Note(this.pitch, octave, this.duration, this.dot);
return Note(this.pitch, octave, this.duration.get("numerator"), this.duration.get("denominator"));
}
function withDuration(duration: int) {
return Note(this.pitch, this.octave, duration, this.dot);
return Note(this.pitch, this.octave, duration, false);
}
function withDot(dot: bool) {
return Note(this.pitch, this.octave, this.duration, dot);
function withDuration(numerator: int, denominator: int) {
return Note(this.pitch, this.octave, numerator, denominator)
}
function transpose(value: int) {
return noteFromInt(this.toInt() + value, this.duration, this.dot);
return noteFromInt(this.toInt() + value, this.duration.get("numerator"), this.duration.get("denominator"));
}
function toInt() {
@@ -33,6 +33,12 @@ extend note as this {
}
}
function noteFromInt(intPitch: int, numerator: int, denominator: int) {
pitch = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "H"].get(mod(intPitch, 12));
octave = Int(intPitch / 12);
return Note(pitch, octave, numerator, denominator);
}
function noteFromInt(intPitch: int, duration: int, dot: bool) {
pitch = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "H"].get(mod(intPitch, 12));
octave = Int(intPitch / 12);
@@ -72,6 +78,18 @@ function transpose(value: int, notes: list<note, int, string>) {
return output;
}
function triplet(a: note, b: note, c: note) {
return tuplet(2, a, b, c);
}
function quintuplet(a: note, b: note, c: note, d: note, e: note) {
return tuplet(4, a, b, c, d, e);
}
function tuplet(sub: int, ...notes: note) {
return notes as note ^ note.withDuration(note.duration.get("numerator") * sub, note.duration.get("denominator") * notes.size);
}
function semitones(...notes: <note, int, string>) {
return semitones(notes);
}