uom icon indicating copy to clipboard operation
uom copied to clipboard

Would there be interest in an example of making a "music time" system?

Open anchpop opened this issue 3 years ago • 1 comments

For a project of mine I'm working on making units to represent what I'm calling "music time", things like a breve, a whole note, a semiquaver, a beat, etc. These don't represent any specific amount of time on their own, a whole note could last a second or a tenth of second depending on the speed the music is supposed to be played at.

This is what I've got so far:

#[macro_use]
pub mod music_time {
    quantity! {
        quantity: MusicTime; "music time";
        dimension: Q<P1>; 
        units {
            @demisemihemidemisemiquaver: 1.0; "d", "demisemihemidemisemiquaver", "demisemihemidemisemiquavers";
            @beat: 64.0; "be", "beat", "beats";
            @note: 256.0; "n", "note", "notes";
        }
    }
}

system! {
    quantities: Q {
        music_time: demisemihemidemisemiquaver, T;
    }

    units: U {
        mod music_time::MusicTime,
    }
}

pub mod f64 {
    mod mks {
        pub use super::super::*;
    }

    Q!(self::mks, f64);
}

I don't know if it makes sense to include this in the library because they aren't SI units. I do think it could be useful as an example though for people who want to make their own systems of quantities.

The one thing that's confusing to me is the typenum dimensions. I have dimension: Q<P1> because it compiles, but P1 was basically chosen at random. So before I made a PR to add an example I would like that clarified.

Also, it would be cool if one could write MusicTime::new::<beat>(120.0) / Time::new::<minute>(1.0) to represent 120 beats per minute. I think this interacts with the typenum dimension but I don't understand how.

Thoughts?

anchpop avatar Mar 19 '21 19:03 anchpop

This article about the SI base units may help to understand how dimensions work. In short a unit like meter has the dimensions length^1. A unit like meter / second has the dimensions length^1 time^-1. The typenum values are the exponents in the dimension.

To have something like beats / minute you have a couple options. Add time to the music time system or implement a whole new type that is essentially struct X { mt: music_time::MusicTime, t: uom::si::time::Time, }.

I'll need to do some more reading to better understand music time to see how it might be included in uom. If you have any references that would be great. Both about the fundamentals of music time and how it would be used in a program.

iliekturtles avatar Mar 20 '21 13:03 iliekturtles