dn-m / Music

Structures for the creation, analysis, and performance of music in Swift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Remove Duration runtime constructor errors

bwetherfield opened this issue · comments

At present let badDur: Duration = 1/>31 causes an error, because 31 is not a power of 2.

Proposed eradication of these errors: A constructor of the following form.

init(_ numerator: Beats, _ power: Power) {
    self.beats = numerator
    self.denominator = pow(2, power)
}

where Power may be implemented as Int as Subdivision is now.

This has the added bonus that technically a breve/double-whole-note is now possible with power = -1...

I really like this. I used the other way as it is more immediately intuitive (I personally don't think of rhythmic subdivision values by their power of two). Perhaps for now, we could keep both.

Wanna open a PR which adds:

extension Duration {
    init(_ numerator: Beats, power: Power) { ... }
}

Note: power argument label extant.

You will have to add a Power struct which is a wrapper for Int, with a failable (or crashing) initializer. Maybe look into using the NewType protocol in Structure/DataStructures.

If only Swift had dependent types!

Nevermind, misunderstood. This would be an excellent idea! And no new types needed.

Re: dependent types: very interesting project: https://github.com/silt-lang/silt

It might actually make things easier down the line if the power was the actual stored value. See ProportionTree, etc. where a bunch of decoding of powers has to take place. Perhaps that could be simplified with this approach.

Interesting. Yes trying to make sense of it at the moment!

ProportionTree that is