freesig / cpal

Audio player in pure Rust
0 stars 0 forks source link

Buffer Conversion #37

Open freesig opened 5 years ago

freesig commented 5 years ago

I'm pretty unhappy with the state of conversions. It's all done with a really messy macro. We could rewrite it using enums to represent which is from and to types of conversion. ie.

convert(sample, Type::Float, Type::Int)

This still doesnt help if you are going to a smaller type ie i16 to i32 It also introduces a runtime cost. Not sure how bad it would be but it is happening in the audio callback.

Another way could be to impl from for each possible conversion. Then we can just call into() I think this could be a good way to go. Although there might be some code duplication we could always make some helper functions that are shared between eg. i8::from(i16) and i16::from(i64) and conversions that have the same logic for there conversion. I think it could be worth pulling this into it's own module or even it's own crate as I recon it could be cool to have a crate that's tested and fast for sample conversion

freesig commented 5 years ago

Actually is this a use case for sample @mitchmindtree ?

mitchmindtree commented 5 years ago

Yep the sample crate has a conversion for just about every sample type under the sun and hundreds of tests for them!

mitchmindtree commented 5 years ago

This method is probably the easiest way to do the conversions. E.g.

my_sample.to_sample::<f32>()
freesig commented 5 years ago

Would it be ok to add this as a cpal dependency?

freesig commented 5 years ago

I feel like we'd just be reinventing your work otherwise