Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ChannelCount and SampleRate NonZero #709

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Make ChannelCount and SampleRate NonZero #709

wants to merge 2 commits into from

Conversation

dvdsk
Copy link
Collaborator

@dvdsk dvdsk commented Feb 28, 2025

I ran into a lot of bugs while adding tests that had to do with channel being set to zero somewhere. While this change makes the API slightly less easy to use it prevents very hard to debug crashes/underflows etc.

Similar to the channels situation a lot of bugs seem to be caused by zero sample_rate. Since audio can literally not play at zero speed it makes no sense to have zero sample_rate's.

Neither channel = 0 nor sample_rate = 0 are used as a sentinel value anywhere

Performance might drop in decoders, the current implementation makes the bound check every time channels or sample_rate is called which should be about once per span. This could be cached to alleviate that.

I ran into a lot of bugs while adding tests that had to do with channel
being set to zero somewhere. While this change makes the API slightly
less easy to use it prevents very hard to debug crashes/underflows etc.

Performance might drop in decoders, the current implementation makes the
bound check every time `channels` is called which is once per span. This
could be cached to alleviate that.
@dvdsk dvdsk force-pushed the NonZero branch 3 times, most recently from 415ecc3 to 67e759a Compare February 28, 2025 17:51
Similar to the channels situation a lot of bugs seem to be caused by
zero sample_rate. Since audio can literally not play at zero speed it
makes no sense to have zero sample_rate's.
@dvdsk dvdsk changed the title This makes ChannelCount NonZero<u16> and channels not zero asserts Make ChannelCount and SampleRate NonZero Feb 28, 2025
@PetrGlad
Copy link
Collaborator

PetrGlad commented Mar 2, 2025

Yes, maybe.

/// short macro to generate a `NonZero`. It panics during compile if the
/// passed in literal is zero. Used for `ChannelCount` and `Samplerate`
/// constants
macro_rules! nz {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A similar macro nonzero is provided by nonzero_ext. I understand it may be too heavy-weight to pull in a dependency just for that, but maybe we could use the same name for recognizability and ease of switching if we ever want to use more of nonzero_ext in the future.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a look its not a transitive dependencies of rodio so for now lets keep it out. (Another argument is that nz! is nice in short, though that might be bad taste on my end).

If you ever need something from nonzero_ext do not hesitate to include it though!

@roderickvd
Copy link
Collaborator

I support this for reasons of clarity and code robustness.

Performance might drop in decoders, the current implementation makes the bound check every time channels or sample_rate is called which should be about once per span. This could be cached to alleviate that.

I agree, for performance-critical paths it's worth spending a few bytes of cache in the struct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants