udoprog / audio

A crate for working with audio in Rust
Apache License 2.0
78 stars 11 forks source link

Conversions cause distortion #7

Closed HEnquist closed 1 year ago

HEnquist commented 1 year ago

The conversions currently scale with a different constant for positive and negative sample values. https://github.com/udoprog/audio/blob/main/audio-core/src/translate.rs#L37 This creates distortion. Not very much, but it could and should be distortion-free.

Cpal does the same mistake, see my old comment here: https://github.com/RustAudio/cpal/issues/512#issuecomment-752237751 In short, it's better to just divide by -MIN. For i8 it then works like this:

udoprog commented 1 year ago

Good call! Can't remember now where I borrowed those algos from but they clearly should be fixed (with some tests to sanity check different extremes).

HEnquist commented 1 year ago

I needed to do these conversions plus a few more in my DSP project. I ended up splitting that part off as a separate crate. Maybe that could be useful here? https://github.com/HEnquist/rawsample It would be neat if it was possible to create an audio buffer directly from a buffer of raw bytes, for all the common sample formats. Especially the 24-bit ones are a handful.

udoprog commented 1 year ago

🤔 Hm... proper wrapping and abstracting over raw audio buffers is on my (personal) todo list to try out. And it has crossed my mind that it might be the "more appropriate" way to build robust audio abstractions. While statically typing everything is really nice when everything is using the same format everywhere it can get quite awkward when you have dynamic components. Most notably I/O devices where they either don't support resampling internally, the API doesn't (WASAPI), or you don't want them to.

But for now we should at least ensure the translation is more correctly implemented as-is. But I'd love to talk more about the broader problem space involving dynamic I/O buffers. And thanks for linking the project!