rust-embedded-community / usbd-midi

MIT License
46 stars 19 forks source link

Calculating a midi note #8

Open TeXitoi opened 3 years ago

TeXitoi commented 3 years ago

Is your feature request related to a problem? Please describe.

I need to calculate a Note, but there is no way to do any calculation as the enum is opaque. Now, I use transmute:

https://github.com/TeXitoi/midi-grid/blob/master/src/main.rs#L195-L196

Describe the solution you'd like

impl core::ops::Add<Rhs = u8> for Note;
impl core::ops::Sub<Rhs = u8> for Note;

What about overflow? Saturating? Modulo? Panic?

Describe alternatives you've considered

transmute

impl From<U7> for Note;

Additional context

Thanks for this crate, was able to make a midi keyboard within an hour! https://github.com/TeXitoi/midi-grid

When we agree on the design, I can implement it if you want.

btrepp commented 3 years ago

Hi

Glad it helps. :). From u7 makes the most sense for me. Consumer of API can build a u7 how they want.

I think it wasn't implemented just because implementation and tests may have taken some time. Which I didn't need when I initially wrote it :)

TeXitoi commented 3 years ago

OK, I'll try to do a PR when I have a bit of time. Thanks.

btrepp commented 4 months ago

For those who stumble upon this. still just was never implemented.

It would look something like

impl From<U7> for Note {
    fn from(value: U7) -> Note {
        //either massive match or transmute here
    }
}

It' only doesn't exist because it was a bit tedious to write out (eg safest is a big. match) and I didn't have the immediate need. In my use case I had a known set of midi notes, so from button presses I created the enum (safest, statically verified).

So I didn't have the use-case for calculation, hence not implemented. It's a relatively straight forward PR though.