-
Notifications
You must be signed in to change notification settings - Fork 249
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
base: master
Are you sure you want to change the base?
Conversation
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.
415ecc3
to
67e759a
Compare
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.
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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
I support this for reasons of clarity and code robustness.
I agree, for performance-critical paths it's worth spending a few bytes of cache in the struct. |
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
orsample_rate
is called which should be about once per span. This could be cached to alleviate that.