This guide will help you update your code when upgrading from older versions of rodio. While we did our best, we might have missed things. PRs that improve this guide are very welcome!
The list below only contains required code changes. For a complete list of changes and new features, see CHANGELOG.md.
Donenow calls a callback instead of decrementing anArc<AtomicUsize>.- To retain old behavior replace the
Arc<AtomicUsize>argument inDone::newwithmove |_| { number.fetch_sub(1, std::sync::atomic::Ordering::Relaxed) }. Donehas now two generics instead of one:<I: Source, F: FnMut(&mut I)>.
- To retain old behavior replace the
Zero::new_samples()now returnsResult<Zero, ZeroError>instead ofZero. Previously the function accepted anynum_samplesvalue; passing one that is not a multiple ofchannelsnow returns anErrinstead of producing a mis-aligned source.
-
Sink terms are replaced with Player and Stream terms replaced with Sink. This is a simple rename, functionality is identical.
We recommend a search replace for the following terms, in no particular order:
OutputStreamis nowMixerDeviceSink(in anticipation of futureQueueDeviceSink)OutputStreamBuilderis nowDeviceSinkBuilderopen_stream_or_fallbackis nowopen_sink_or_fallbackopen_default_streamis nowopen_default_sinkopen_streamis nowopen_mixer(in anticipation of futureopen_queue)Sinkis nowPlayerSpatialSinkis nowSpatialPlayerStreamErroris nowOsSinkError
-
output_to_wavrenamed towav_to_fileand now takes ownership of theSource. -
SamplesBuffer::newnow takes ChannelCount and SampleRate as arguments. Use thenz!macro to easily create those from literals like this:SamplesBuffer::new(nz!(1), nz!(44100))or construct them at runtime like this:SamplesBuffer::new(NonZero::new(channel_count)?, NonZero::new(sample_rate)?);
- Playback logic has been turned into a feature that is enabled by default.
If you have
default_features = falsein yourCargo.tomland want audio playback, you need to also setfeatures = ["playback"]. - The default decoders have changed to Symphonia, which itself is licensed
under MPL. If you want to revert to the old decoders, you need to set
default_features = falseand enable theclaxon,houndandlewtonfeatures inCargo.tomlfor respectively FLAC, WAV and Ogg Vorbis.
OutputStreamHandleno longer exists, you can remove it from your code.OutputStreamHandle::play_rawhas been removed, instead useOutputStream.mixer().add().- The output stream is now more configurable. Where you used
OutputStream::try_default(), you need to change to either:- (recommended)
OutputStreamBuilder::open_default_stream()?which tries to open a new output stream for the default output device with its default configuration. Failing that it attempt to open an output stream with alternative configuration and/or non default output devices. Returns the stream for the first configurations tried that succeeds. If all attempts fail returns the initial error. - (org behavior)
open_stream_or_fallback()?which is used as follows:That tries to opening a output stream with the default configuration on the default device. Failing that attempt to open a stream with other available configurations supported by the default device. If all attempts fail returns initial error.let default_device = cpal::default_host() .default_output_device() .ok_or("No default audio output device is found.")?; rodio::OutputStreamBuilder::from_device(default_device)? .open_stream_or_fallback()?;
- (recommended)
- The output stream now prints to stderr or logs a message on drop, if that breaks your
CLI/UI use
stream.log_on_drop(false).
- Replace
Sink::try_newwithSink::connect_new, which takes an&Mixerinstead of aOutputStreamHandle. You get an&Mixerby callingmixer()onOutputStream. - Replace
Sink::new_idlewithSink::new.
The following Rodio 0.20 code:
let (_stream, handle) = rodio::OutputStream::try_default()?;
let player = rodio::Player::try_new(&handle)?;Should be written like this in Rodio 0.21:
let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?;
let player = rodio::Player::connect_new(stream_handle.mixer());The SpatialSink changes mirror those in Sink described above.
Decoder::new_mp4no longer takes anMp4Typeas hint. Remove the hint.- The Symphonia decoders no longer assumes all sources are seekable. Use
DecoderBuilder::with_seekableortry_fromon aFile. You do not need to wrap it into aBufReaderanymore.
The following Rodio 0.20 code
let file = File::open("music.ogg")?;
let reader = BufReader::new(file);
let source = Decoder::new(reader);Should be written like this in Rodio 0.21:
let file = File::open("music.ogg")?;
let source = Decoder::try_from(file)?;- Replace
DynamicMixerControllerwithMixerandDynamicMixerwithMixerSource.
- The
Source::whiteandSource::pinkmethods have been deprecated. UseWhiteUniform::newandPink::newinstead.
- The
Sourcetrait had a required methodcurrent_frame_len, which has been renamed tocurrent_span_len. Rename every occurrence. Sourcewas generic over sample typesf32,u16, andi16. It no longer is; rodio now works withf32everywhere. This means:- Remove any generics (
::<f32>,::<u16>or::<i16>) that cause errors. - Remove any use of
SampleConvertor. - Remove any calls to
convert_samples.
- Remove any generics (