Coder Social home page Coder Social logo

rodio's Introduction

Audio playback library

Crates.io Version Crates.io Downloads Build Status

Rust playback library.

Playback is handled by cpal. Format decoding can be handled either by Symphonia, or by format-specific decoders:

See the docs for more details on backends.

The documentation contains an introduction to the library.

License

Licensed under either of

at your option.

License of your contributions

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

rodio's People

Contributors

17cupsofcoffee avatar a1phyr avatar a2aaron avatar alexheretic avatar azriel91 avatar barzamin avatar chyvonomys avatar cormac-obrien avatar dvdsk avatar ely-uf avatar enfipy avatar est31 avatar frazar avatar germangb avatar gridbugs avatar gusted avatar halfvoxel avatar hamirmahal avatar ideka avatar mathstuf avatar mockersf avatar nbraud avatar orlp avatar senlizishi avatar thiolliere avatar tomaka avatar whentze avatar willstott101 avatar xaeroxe avatar xwillxw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rodio's Issues

Use a separate thread for decoding

Right now the engine loads samples "lazily" when the cpal backend requests them.

In other words, cpal tells rodio that samples are needed, and then only the samples are loaded from the audio file, converted, etc.

This is bad because passing samples to cpal should be the top priority in order to avoid a potential underflow.

I think we should add a PrebufferedFilter type that wraps around a Source and allows pre-buffering some of the input. The engine would automatically wrap any source sent to it around a PrebufferedFilter, and call preload(...) on the sources during downtimes.

However this design requires some modifications in cpal in order to allow executing stuff during downtimes.

Add a system to change the filters of a sound while it's playing

In a video game you usually want to change the volume, play speed (doppler effect), etc. while a sound is playing.

This could be done with an API like this:

fn apply_chunks<F, S>(self, chunks_duration: Duration, callback: F) -> ApplyChunks<Self, F>
    where F: FnMut(SourceChunk) -> S where S: Source;
let volume = Arc::new(Mutex::new(1.0));
let source = source.apply_chunks(Duration::from_millis(17), move |source| {
    source.amplify(volume.lock().unwrap().clone())
});

Inexplicable 'UnrecognizedFormat'

I went and got a sound, this particular one https://freesound.org/people/martian/sounds/403973/, however Rodio seems to panic at runtime with an UnrecognizedFormat error.

extern crate rodio;

use rodio::Sink;
use std::io::BufReader;

fn main() {
    let endpoint = rodio::get_default_endpoint().unwrap();
    let sink = Sink::new(&endpoint);

    let file = std::fs::File::open("403973__martian__beast-whoosh.wav").unwrap();
    let src = rodio::Decoder::new(BufReader::new(file)).unwrap();
    sink.append(src);
    sink.sleep_until_end();
}

Results in

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: UnrecognizedFormat', /checkout/src/libcore/result.rs:860:4
stack backtrace:
   0:     0x55e2623ce973 - std::sys::imp::backtrace::tracing::imp::unwind_backtrace::hcdf51e4c9dc54357
                               at /checkout/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1:     0x55e2623cb054 - std::sys_common::backtrace::_print::h9da91fd31a37d0f1
                               at /checkout/src/libstd/sys_common/backtrace.rs:71
   2:     0x55e2623d0a33 - std::panicking::default_hook::{{closure}}::h46820a72bf0cb624
                               at /checkout/src/libstd/sys_common/backtrace.rs:60
                               at /checkout/src/libstd/panicking.rs:380
   3:     0x55e2623d07a2 - std::panicking::default_hook::h4c1ef1cc83189c8e
                               at /checkout/src/libstd/panicking.rs:396
   4:     0x55e2623d0ef7 - std::panicking::rust_panic_with_hook::h99016f44bdcb8544
                               at /checkout/src/libstd/panicking.rs:611
   5:     0x55e2623d0dc4 - std::panicking::begin_panic_new::hae931f4b9fe56a90
                               at /checkout/src/libstd/panicking.rs:553
   6:     0x55e2623d0cc9 - std::panicking::begin_panic_fmt::h3b8db4524c33692e
                               at /checkout/src/libstd/panicking.rs:521
   7:     0x55e2623d0c5a - rust_begin_unwind
                               at /checkout/src/libstd/panicking.rs:497
   8:     0x55e2623f8d20 - core::panicking::panic_fmt::h955f7c5ec61a82d4
                               at /checkout/src/libcore/panicking.rs:92
   9:     0x55e26227d66b - core::result::unwrap_failed::h5098b88d52d0c498
                               at /checkout/src/libcore/macros.rs:41
  10:     0x55e26225f375 - <core::result::Result<T, E>>::unwrap::ha9ff8a0535b48e3f
                               at /checkout/src/libcore/result.rs:738
  11:     0x55e2622ec26a - rodio_test::main::h00a2b34090bae6e7
                               at src/main.rs:11
  12:     0x55e2623d7e1c - __rust_maybe_catch_panic
                               at /checkout/src/libpanic_unwind/lib.rs:98
  13:     0x55e2623d162a - std::rt::lang_start::h97aba2334c85f570
                               at /checkout/src/libstd/panicking.rs:458
                               at /checkout/src/libstd/panic.rs:361
                               at /checkout/src/libstd/rt.rs:59
  14:     0x55e2622ec362 - main
  15:     0x7f40840f0f69 - __libc_start_main
  16:     0x55e2622352a9 - _start
  17:                0x0 - <unknown>

The file plays fine in MPV and other WAV files work aswell.

Skipping Samples from Decoder

Is there any way to skip over audio samples using radio?
I tried to seek the BufReader forward but that results in an UnrecognizedFormat error.
The only way I've found that works requires reading the full file into memory:

let sample_rate = decoder.sample_rate();
let channels = decoder.channels();
let source = decoder.skip((sample_rate * 10) as usize).collect::<Vec<i16>>(); // decode the full song
sink.append(rodio::buffer::SamplesBuffer::new(channels, sample_rate, source));

Examples crash under Mac OS X

This is a pristine checkout on e8bb880 (latest master). No other audio programs are running right now.

$ cargo run --release --example basic
    Finished release [optimized] target(s) in 0.0 secs
     Running `target/release/examples/basic`
-> exit code 1

$ target/release/examples/basic
zsh: segmentation fault (core dumped)  target/release/examples/basic

This is a MacBookPro Retina 15 Inch, Early 2013 with Intel Core i7 running OS X El Capitan.

Build fails on OS X

$ cargo build
    Updating registry `https://github.com/rust-lang/crates.io-index`
   Compiling winapi v0.2.6
   Compiling bitflags v0.3.3
   Compiling winapi-build v0.1.1
   Compiling ole32-sys v0.1.0
   Compiling lazy_static v0.1.15
   Compiling pkg-config v0.3.8
   Compiling gcc v0.3.25
   Compiling hound v1.1.0
   Compiling libc v0.2.8
   Compiling kernel32-sys v0.2.1
   Compiling time v0.1.34
   Compiling coreaudio-sys v0.1.2
   Compiling ogg-sys v0.0.9
   Compiling vorbis-sys v0.0.8
   Compiling vorbisfile-sys v0.0.8
/Users/yupferris/.cargo/registry/src/github.com-88ac128001ac3a9a/coreaudio-sys-0.1.2/src/lib.rs:5:10: 5:28 warning: lint raw_pointer_derive has been removed: using derive with raw pointers is ok
/Users/yupferris/.cargo/registry/src/github.com-88ac128001ac3a9a/coreaudio-sys-0.1.2/src/lib.rs:5 #![allow(raw_pointer_derive)]
                                                                                                           ^~~~~~~~~~~~~~~~~~
   Compiling coreaudio-rs v0.4.0
   Compiling cpal v0.2.9
   Compiling vorbis v0.0.13
/Users/yupferris/.cargo/registry/src/github.com-88ac128001ac3a9a/cpal-0.2.9/src/lib.rs:334:20: 334:30 error: no method named `get_period` found for type `cpal_impl::Voice` in the current scope
/Users/yupferris/.cargo/registry/src/github.com-88ac128001ac3a9a/cpal-0.2.9/src/lib.rs:334         self.voice.get_period()
                                                                                                              ^~~~~~~~~~
error: aborting due to previous error
Build failed, waiting for other jobs to finish...
Could not compile `cpal`.

To learn more, run the command again with --verbose.

This applies to both the latest version of this repo and also the published version on crates.io (0.3.1).

Update hound dependency to 3.3.1

The current version of hound used by rodio (1.0.0) fails when reading certain valid WAV files (see ruuda/hound#20). All the tests still pass with the dependency in Cargo.toml updated to 3.3.1. Is there anything preventing rodio's version of hound from being updated?

Latest release breaks semver

Version 0.5.3 upgraded cpal to 0.6. This breaks semver since the cpal EndpointsIterator structure is exposed in the rodio API.

When using ALSA backend Voice doesn't call drop after termination

I have added a println in Voice::drop like this:

impl Drop for Voice {
    #[inline]
    fn drop(&mut self) {
        println!("Voice droped.");
        unsafe {
            alsa::snd_pcm_close(*self.channel.lock().expect("drop for voice"));
        }
    }
}

And used modified cpal version in rodio like this:

[dependencies.cpal]
path = "../cpal/"
version = "0.2.8"

When running any rodio example line "Voice droped." isn't printed, and the next time I run the example it panics with:

RUST_BACKTRACE=1 cargo run --example basic
     Running `target/debug/examples/basic`
ALSA lib pcm_dmix.c:1079:(snd_pcm_dmix_open) unable to open slave
thread '<main>' panicked at 'device not available: "File descriptor in bad state"', ../src/libcore/result.rs:785
stack backtrace:
   1:     0x55d38f4528ef - std::sys::backtrace::tracing::imp::write::h3800f45f421043b8
   2:     0x55d38f45607b - std::panicking::default_hook::_$u7b$$u7b$closure$u7d$$u7d$::h0ef6c8db532f55dc
   3:     0x55d38f455d03 - std::panicking::default_hook::hf3839060ccbb8764
   4:     0x55d38f44c44d - std::panicking::rust_panic_with_hook::h5dd7da6bb3d06020
   5:     0x55d38f4562c1 - std::panicking::begin_panic::h9bf160aee246b9f6
   6:     0x55d38f44d31a - std::panicking::begin_panic_fmt::haf08a9a70a097ee1
   7:     0x55d38f45625e - rust_begin_unwind
   8:     0x55d38f48b4cf - core::panicking::panic_fmt::h93df64e7370b5253
   9:     0x55d38f444039 - core::result::unwrap_failed::hfa264942aa0c849a
                        at ../src/libcore/macros.rs:29
  10:     0x55d38f443e4d - _<std..result..Result<T, E>>::expect::h4d9839a56813da38
                        at ../src/libcore/result.rs:747
  11:     0x55d38f44023f - cpal::cpal_impl::Endpoint::get_supported_formats_list::h75c5d241492cc62d
                        at /home/user/C/cpal/src/alsa/mod.rs:37
  12:     0x55d38f421efa - cpal::Endpoint::get_supported_formats_list::hb577f5a458b49c0a
                        at /home/user/C/cpal/src/lib.rs:107
  13:     0x55d38f41ad5e - rodio::engine::Engine::start::_$u7b$$u7b$closure$u7d$$u7d$::h88818cfd009576fc
                        at /home/user/C/rodio/src/engine.rs:76
  14:     0x55d38f418e88 - _<std..collections..hash_map..Entry<'a, K, V>>::or_insert_with::h21890678fa9fa9ae
                        at ../src/libstd/collections/hash/map.rs:1534
  15:     0x55d38f41226d - rodio::engine::Engine::start::h87deb3d983372762
                        at /home/user/C/rodio/src/engine.rs:73
  16:     0x55d38f3ea741 - rodio::Sink::new::h808662d15dce8280
                        at /home/user/C/rodio/examples/lib.rs:94
  17:     0x55d38f3daa87 - rodio::play_once::h13ef39eaad2afdcf
                        at /home/user/C/rodio/examples/lib.rs:153
  18:     0x55d38f3cb5bb - basic::main::h8b9602a214009541
                        at /home/user/C/rodio/examples/basic.rs:9
  19:     0x55d38f455918 - std::panicking::try::call::hbbf4746cba890ca7
  20:     0x55d38f45f6db - __rust_try
  21:     0x55d38f45f67e - __rust_maybe_catch_panic
  22:     0x55d38f4553be - std::rt::lang_start::hbcefdc316c2fbd45
  23:     0x55d38f406f29 - main
  24:     0x7fe316299740 - __libc_start_main
  25:     0x55d38f3cb3b8 - _start
  26:                0x0 - <unknown>
error: Process didn't exit successfully: `target/debug/examples/basic` (exit code: 101)

If Voice is properly dropped it doesn't panic and also it doesn't panic when some other process is playing sound.

I have encountered the same problem with cpal/examples/beep.rs example.

Cannot restart stopped sink

Hi there! Thank you for this awesome library. While exploring the API I stumbled over the following behavior, which I believe is a bug:

A Sink no longer plays newly appended sounds after .stop() has been called on the Sink.

Looking at the sources, it seems that .stop() sets Controls.stopped to true, but I could not find anything that would set it back to false other than constructing a new Sink.

Clarify Source.amplify

When reading over the documentation, I naively assumed that Source.amplify would amplify a source by a value in decibels, similar to how amplifying a track in Audacity works, but instead it sets the volume for the source the same way that Sink.set_volume does (additionally, passing a negative values functions the same as passing a positive values, which didn't help).

I think it would be useful to clarify this so that people don't make the same mistake!

No sound output when using default Endpoint (ALSA)

So this is a bit strange: when running the cpal's "beep" example (which uses get_default_endpoint) everything works as expected but running rodio's "basic" example (which also uses the re-exported get_default_endpoint) no sound is playing.

Using the first endpoint from get_endpoints_list works as expected (in both examples).

I tried changing the format selection to use the first format like in the cpal example but that didn't help.

Apparently something's not right with the event loop because after I inserted a println here it's only printed exactly once.

edit: (In both examples the default endpoint is named "default" and the first endpoint from get_endpoints_list is "pulse".)

Audio glitches on CPU starved systems in debug mode, on Unix based systems

So in Amethyst we've been chasing a bug with audio glitching that we've only been able to reproduce on Linux/OSX in debug mode if the system is very starved for CPU. Interestingly we can't reproduce this under release mode on Unix based, or debug on Windows, or release on Windows, even if the CPU is starved in those instances. Consider this low priority as we're not really terribly concerned about this, but we did want to mention to you there may be problems with thread scheduling in debug mode on Unix based systems.

thread '<main>' panicked at 'called `Result::unwrap()` on an `Err` value: "PoisonError"...

Greetings,

When trying to use this library, I end up with an error.

Here is the code that's causing the panic, inspired by one of your examples (https://github.com/tomaka/rodio/blob/master/examples/music_wav.rs)...


    use std::io::BufReader;
    let endpoint = rodio::get_default_endpoint().expect("Failed to retrieve default endpoint.");
    let sink = rodio::Sink::new(&endpoint);

    let file = std::fs::File::open(filepath).expect("file missing!");
    let source = rodio::Decoder::new(BufReader::new(file)).unwrap();
    sink.append(source);
    sink.sleep_until_end();


This is currently residing in a trigger system, so it currently occurs when the player steps on a particular tile. When I step on that tile, I get the following error:


thread '<main>' panicked at 'Unknown SubFormat GUID returned by GetMixFormat: GUID { Data1: 1048576, Data2: 128, Data3: 43520, Data4: [0, 56, 155, 113, 164, 120, 36, 176] }', C:\Users\<USERNAME>\.cargo\registry\src\github.com-88ac128001ac3a9a\cpal-0.2.11\src\wasapi/mod.rs:191
note: Run with `RUST_BACKTRACE=1` for a backtrace.
thread '<main>' panicked at 'called `Result::unwrap()` on an `Err` value: "PoisonError { inner: .. }"', ../src/libcore\result.rs:746
stack backtrace:
   0:     0x7ff7be789b5b - std::rt::lang_start::h5b0863080165c75e
   1:     0x7ff7be788f5b - std::rt::lang_start::h5b0863080165c75e
   2:     0x7ff7be77d34f - std::sys_common::unwind::begin_unwind_inner::h39d40f52add53ef7
   3:     0x7ff7be77e33d - std::sys_common::unwind::begin_unwind_fmt::h64c0ff793199cc1b
   4:     0x7ff7be78456b - rust_begin_unwind
   5:     0x7ff7be78f1b5 - core::panicking::panic_fmt::h73bf9d7e8e891a73
   6:     0x7ff7be66ae6f - main
   7:     0x7ff7be6676a6 - main
   8:     0x7ff7be64f47d - __ImageBase
   9:     0x7ffb3afbc6bb - _C_specific_handler
  10:     0x7ffb50859b7c - _chkstk
  11:     0x7ffb507e595b - RtlUnwindEx
  12:     0x7ffb3afbc604 - _C_specific_handler
  13:     0x7ffb50859afc - _chkstk
  14:     0x7ffb507e4fe8 - RtlImageNtHeaderEx
  15:     0x7ffb507e6c93 - RtlRaiseException
  16:     0x7ffb4cf61f27 - RaiseException
  17:     0x7ff7be782517 - std::io::stdio::_print::h03730948b3f63a9b
  18:     0x7ff7be77d4a9 - std::sys_common::unwind::begin_unwind_inner::h39d40f52add53ef7
  19:     0x7ff7be77e33d - std::sys_common::unwind::begin_unwind_fmt::h64c0ff793199cc1b
  20:     0x7ff7be777b3c - cpal::cpal_impl::Endpoint::get_supported_formats_list::h49aba8504a03d688
  21:     0x7ff7be7673b7 - rodio::engine::Engine::start::hb7ef03fed4b01782
  22:     0x7ff7be647c3c - __ImageBase
  23:     0x7ff7be788928 - std::rt::lang_start::h5b0863080165c75e
  24:     0x7ff7be7844d8 - std::sys_common::unwind::inner_try::h9eebd8dc83f388a6
  25:     0x7ff7be7886d7 - std::rt::lang_start::h5b0863080165c75e
  26:     0x7ff7be7af24f - __scrt_common_main_seh
                        at f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl:255
  27:     0x7ffb4dc08101 - BaseThreadInitThunk
thread panicked while panicking. aborting.
error: Process didn't exit successfully: `target\release\project_zed.exe` (exit code: 3221225477)

This panic is occuring lower than the caller level, so I'm not sure there is anything I can do with it at the user level.

Thanks,
Plasticcaz

EDIT: I should probably give some system info too:
I am running Windows 10 64-bit. If you need anything else, please ask.

Relicense under dual MIT/Apache-2.0

Why?

The MIT license requires reproducing countless copies of the same copyright
header with different names in the copyright field, for every MIT library in
use. The Apache license does not have this drawback, and has protections from
patent trolls and an explicit contribution licensing clause. However, the
Apache license is incompatible with GPLv2. This is why Rust is dual-licensed as
MIT/Apache (the "primary" license being Apache, MIT only for GPLv2 compat), and
doing so would be wise for this project. This also makes this crate suitable
for inclusion in the Rust standard distribution and other project using dual
MIT/Apache.

How?

To do this, get explicit approval from each contributor of copyrightable work
(as not all contributions qualify for copyright) and then add the following to
your README:

## License

Licensed under either of
 * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
 * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.

### Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you shall be dual licensed as above, without any
additional terms or conditions.

and in your license headers, use the following boilerplate (based on that used in Rust):

// Copyright (c) 2015 t developers
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.

And don't forget to update the license metadata in your Cargo.toml!

Contributor checkoff

Sharing playback state

Hi,

First thanks for a great create.

I have one question: I would like the rest of the application to be able to get a ref/copy of the current buffer of data that is being played. What I want to do is for example doing a oscilloscope or something like that.

I looked at how the FLAC decoder works and it decodes a block of data and then it has a index into that block. I likely want to do something similar in a custom Source but I also want to share it with other parts of the application.

Any suggestions on what the best approach for this would be?

Cheers!

Alsa crashes rodio on device not available

As said in title. I think the default behaviour should be to warn that no sound is going to be played as the device is unavailable. Current behaviour is panic.

The device not available error is normal. Alsa is configured to play on my headphones, which are not plugged in. I believe I should be able to run any app, even if the sound is not present.

Backtrace:

ALSA lib pulse.c:243:(pulse_connect) PulseAudio: Unable to connect: Connection refused

thread 'main' panicked at 'device not available: "Connection refused"', /checkout/src/libcore/result.rs:906:4
stack backtrace:
   0: std::sys::imp::backtrace::tracing::imp::unwind_backtrace
             at /checkout/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1: std::sys_common::backtrace::_print
             at /checkout/src/libstd/sys_common/backtrace.rs:68
   2: std::panicking::default_hook::{{closure}}
             at /checkout/src/libstd/sys_common/backtrace.rs:57
             at /checkout/src/libstd/panicking.rs:381
   3: std::panicking::default_hook
             at /checkout/src/libstd/panicking.rs:397
   4: std::panicking::rust_panic_with_hook
             at /checkout/src/libstd/panicking.rs:577
   5: std::panicking::begin_panic
             at /checkout/src/libstd/panicking.rs:538
   6: std::panicking::begin_panic_fmt
             at /checkout/src/libstd/panicking.rs:522
   7: rust_begin_unwind
             at /checkout/src/libstd/panicking.rs:498
   8: core::panicking::panic_fmt
             at /checkout/src/libcore/panicking.rs:71
   9: core::result::unwrap_failed
             at /checkout/src/libcore/macros.rs:23
  10: <core::result::Result<T, E>>::expect
             at /checkout/src/libcore/result.rs:799
  11: cpal::cpal_impl::Endpoint::get_supported_formats_list
             at /home/jojolepro/.cargo/registry/src/github.com-1ecc6299db9ec823/cpal-0.4.6/src/alsa/mod.rs:87
  12: cpal::Endpoint::supported_formats
             at /home/jojolepro/.cargo/registry/src/github.com-1ecc6299db9ec823/cpal-0.4.6/src/lib.rs:156
  13: cpal::Endpoint::get_supported_formats_list
             at /home/jojolepro/.cargo/registry/src/github.com-1ecc6299db9ec823/cpal-0.4.6/src/lib.rs:165
  14: rodio::engine::new_voice
             at /home/jojolepro/.cargo/registry/src/github.com-1ecc6299db9ec823/rodio-0.5.2/src/engine.rs:106
  15: rodio::engine::Engine::start
             at /home/jojolepro/.cargo/registry/src/github.com-1ecc6299db9ec823/rodio-0.5.2/src/engine.rs:76
  16: rodio::engine::play_raw
             at /home/jojolepro/.cargo/registry/src/github.com-1ecc6299db9ec823/rodio-0.5.2/src/engine.rs:50
  17: rodio::sink::Sink::new
             at /home/jojolepro/.cargo/registry/src/github.com-1ecc6299db9ec823/rodio-0.5.2/src/sink.rs:37
  18: amethyst_audio::sink::AudioSink::new
             at amethyst_audio/src/sink.rs:18

Sound stops playing at seemingly random locations

I'm on linux.
When I run the basic example, it never plays all the beeps. It just stops playback at random times (after ~100ms to ~4 seconds).
To confirm I loaded a .wav song from my music collection and tried to play it. Same results.

I do not understand low level sound playback very well, so any pointers on how I can give better debug information would be appreciated.

Memory leak

It looks like Sink::detach will leak the associated QueueIterator (even when no sounds are, or can be played anymore) until the stream for the associated endpoint is dropped.

This means if you create and detach too many sinks it degrades sound quality.

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: DeviceNotAvailable'

I am totally new to rust development and started to learn by picking up this game at https://github.com/thiolliere/ruga and couldnt run it because of this error. Thought this can be a good thing to solve or get to understand rust. I have read the src and i can see the file engine has TODO comment for the fn new_voice so i guess its better to post it here.

ALSA lib pcm_dmix.c:1099:(snd_pcm_dmix_open) unable to open slave
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: DeviceNotAvailable', /checkout/src/libcore/result.rs:860
stack backtrace:
   0: std::sys::imp::backtrace::tracing::imp::unwind_backtrace
             at /checkout/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1: std::sys_common::backtrace::_print
             at /checkout/src/libstd/sys_common/backtrace.rs:71
   2: std::panicking::default_hook::{{closure}}
             at /checkout/src/libstd/sys_common/backtrace.rs:60
             at /checkout/src/libstd/panicking.rs:355
   3: std::panicking::default_hook
             at /checkout/src/libstd/panicking.rs:371
   4: std::panicking::rust_panic_with_hook
             at /checkout/src/libstd/panicking.rs:549
   5: std::panicking::begin_panic
             at /checkout/src/libstd/panicking.rs:511
   6: std::panicking::begin_panic_fmt
             at /checkout/src/libstd/panicking.rs:495
   7: rust_begin_unwind
             at /checkout/src/libstd/panicking.rs:471
   8: core::panicking::panic_fmt
             at /checkout/src/libcore/panicking.rs:69
   9: core::result::unwrap_failed
  10: rodio::engine::new_voice
  11: rodio::sink::Sink::new
  12: baal::effect::State::init
  13: baal::init
  14: ruga::init
  15: ruga::main
  16: __rust_maybe_catch_panic
             at /checkout/src/libpanic_unwind/lib.rs:98
  17: std::rt::lang_start
             at /checkout/src/libstd/panicking.rs:433
             at /checkout/src/libstd/panic.rs:361
             at /checkout/src/libstd/rt.rs:59
  18: __libc_start_main
  19: _start
```

High CPU usage

Running the basic.rs example using cargo run --example basic uses a lot of CPU power in my case and cause the beeps to stutter.

$ top

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                                             
11034 ritiek    20   0  221772  16936   6080 S  95.4  0.3   0:04.57 basic                                                                                               
 2888 ritiek    20   0 2261352 266224  63592 S  11.3  4.4  24:34.86 cinnamon                                                                                            
 1134 root      20   0  356784  63600  51844 S   4.6  1.1  12:13.30 Xorg                                                                                                
 2725 ritiek     9 -11  474084  11880   8616 S   4.6  0.2   9:34.37 pulseaudio                                                                                          
28895 ritiek    20   0  493916  43916  28824 S   3.0  0.7   0:27.94 gnome-terminal-                                                                                     
 2503 ritiek    20   0 2626936 629232 131356 S   1.0 10.5   5:10.36 firefox       

Is such high CPU usage expected or is something wrong?

Zero/SineWave + take_duration + buffered panic.

Zero/SineWave source and a combination of take_duration(...) and buffered() filters panics.

Setup:

extern crate rodio;

fn main() {
    use rodio::Source;
    //let _ = rodio::source::SineWave::new(147)
    let _ = rodio::source::Zero::<f32>::new(2, 44100)
        .take_duration(std::time::Duration::from_secs(1))
        .buffered()
        .count();
}

Backtrace:

thread 'main' panicked at 'index out of bounds: the len is 0 but the index is 0', src/libcollections/vec.rs:1422
stack backtrace:
   0: std::sys::imp::backtrace::tracing::imp::unwind_backtrace
   1: std::panicking::default_hook::{{closure}}
   2: std::panicking::default_hook
   3: std::panicking::rust_panic_with_hook
   4: std::panicking::begin_panic
   5: std::panicking::begin_panic_fmt
   6: rust_begin_unwind
   7: core::panicking::panic_fmt
   8: core::panicking::panic_bounds_check
   9: <collections::vec::Vec<T> as core::ops::Index<usize>>::index
  10: <rodio::source::buffered::Buffered<I> as core::iter::iterator::Iterator>::next
  11: core::iter::iterator::Iterator::fold
  12: core::iter::iterator::Iterator::count
  13: rodio_sandbox::main
  14: std::panicking::try::do_call
  15: __rust_maybe_catch_panic
  16: std::rt::lang_start
  17: main

"basic" example crashes with PulseAudio on LInux

Running the "basic" example produces a crash on my machine with the following output.

$ cargo run --example basic
    Finished dev [unoptimized + debuginfo] target(s) in 0.23 secs
     Running `target/debug/examples/basic`
thread 'main' panicked at 'sample rate could not be set: "Invalid argument"', libcore/result.rs:945:5
note: Run with `RUST_BACKTRACE=1` for a backtrace.

I think this is related to an issue in cpal, where the maximum sample rate reported by ALSA is "garbage" (4294967295) when using PulseAudio.

The issue has has been solved in cpal >= 0.8 by using a better default, so updating the dependency version might be sufficient.

Basic example build fails on Windows

Trying to build the basic example on Windows using Rust 1.4.0, I get the following errors:

note: C:\msys64\home\ferris\dev\projects\rodio\target\debug\deps\libvorbisfile_sys-4a255b9627c465d7.rlib(vorbisfile.o): In function `_get_data':
/home/ferris/.cargo/registry/src/github.com-0a35038f75765ae4/vorbisfile-sys-0.0.8/libvorbisfile/vorbisfile.c:68: undefined reference to `__errno'
/home/ferris/.cargo/registry/src/github.com-0a35038f75765ae4/vorbisfile-sys-0.0.8/libvorbisfile/vorbisfile.c:74: undefined reference to `__errno'

Full output:

$ cargo run --example basic --verbose
    Updating registry `https://github.com/rust-lang/crates.io-index`
   Compiling ole32-sys v0.1.0
     Running `rustc C:\\msys64\\home\\ferris\\.cargo\\registry\\src\\github.com-0a35038f75765ae4\\ole32-sys-0.1.0\\build.rs --crate-name build_script_build --crate-type bin -g --out-dir C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\build\\ole32-sys-5219fe2002394e46 --emit=dep-info,link -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --cap-lints allow`
   Compiling pkg-config v0.3.6
     Running `rustc C:\\msys64\\home\\ferris\\.cargo\\registry\\src\\github.com-0a35038f75765ae4\\pkg-config-0.3.6\\src\\lib.rs --crate-name pkg_config --crate-type lib -g -C metadata=376a9590956a17f5 -C extra-filename=-376a9590956a17f5 --out-dir C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --emit=dep-info,link -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --cap-lints allow`
   Compiling winapi v0.2.5
     Running `rustc C:\\msys64\\home\\ferris\\.cargo\\registry\\src\\github.com-0a35038f75765ae4\\winapi-0.2.5\\src\\lib.rs --crate-name winapi --crate-type lib -g -C metadata=d88a8c018c340227 -C extra-filename=-d88a8c018c340227 --out-dir C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --emit=dep-info,link -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --cap-lints allow`
   Compiling lazy_static v0.1.15
     Running `rustc C:\\msys64\\home\\ferris\\.cargo\\registry\\src\\github.com-0a35038f75765ae4\\lazy_static-0.1.15\\src\\lib.rs --crate-name lazy_static --crate-type lib -g -C metadata=f3aa6dfcc7c157cc -C extra-filename=-f3aa6dfcc7c157cc --out-dir C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --emit=dep-info,link -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --cap-lints allow`
   Compiling libc v0.2.2
     Running `rustc C:\\msys64\\home\\ferris\\.cargo\\registry\\src\\github.com-0a35038f75765ae4\\libc-0.2.2\\src\\lib.rs --crate-name libc --crate-type lib -g --cfg feature=\"default\" -C metadata=540159808ccfa9ab -C extra-filename=-540159808ccfa9ab --out-dir C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --emit=dep-info,link -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --cap-lints allow`
   Compiling winapi-build v0.1.1
     Running `rustc C:\\msys64\\home\\ferris\\.cargo\\registry\\src\\github.com-0a35038f75765ae4\\winapi-build-0.1.1\\src\\lib.rs --crate-name build --crate-type lib -g -C metadata=304afb6bdff23d72 -C extra-filename=-304afb6bdff23d72 --out-dir C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --emit=dep-info,link -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --cap-lints allow`
   Compiling hound v1.1.0
     Running `rustc C:\\msys64\\home\\ferris\\.cargo\\registry\\src\\github.com-0a35038f75765ae4\\hound-1.1.0\\src\\lib.rs --crate-name hound --crate-type lib -g -C metadata=10ecc825f7963132 -C extra-filename=-10ecc825f7963132 --out-dir C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --emit=dep-info,link -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --cap-lints allow`
       Fresh bitflags v0.3.3
     Running `C:\msys64\home\ferris\dev\projects\rodio\target\debug\build\ole32-sys-5219fe2002394e46\build-script-build`
   Compiling advapi32-sys v0.1.2
     Running `rustc C:\\msys64\\home\\ferris\\.cargo\\registry\\src\\github.com-0a35038f75765ae4\\advapi32-sys-0.1.2\\build.rs --crate-name build_script_build --crate-type bin -g --out-dir C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\build\\advapi32-sys-cfef7a1f30f1e5f6 --emit=dep-info,link -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --extern build=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libbuild-304afb6bdff23d72.rlib --cap-lints allow`
   Compiling kernel32-sys v0.2.1
     Running `rustc C:\\msys64\\home\\ferris\\.cargo\\registry\\src\\github.com-0a35038f75765ae4\\kernel32-sys-0.2.1\\build.rs --crate-name build_script_build --crate-type bin -g --out-dir C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\build\\kernel32-sys-35c1a745c861d23a --emit=dep-info,link -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --extern build=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libbuild-304afb6bdff23d72.rlib --cap-lints allow`
     Running `C:\msys64\home\ferris\dev\projects\rodio\target\debug\build\advapi32-sys-cfef7a1f30f1e5f6\build-script-build`
     Running `C:\msys64\home\ferris\dev\projects\rodio\target\debug\build\kernel32-sys-35c1a745c861d23a\build-script-build`
     Running `rustc C:\\msys64\\home\\ferris\\.cargo\\registry\\src\\github.com-0a35038f75765ae4\\advapi32-sys-0.1.2\\src\\lib.rs --crate-name advapi32 --crate-type lib -g -C metadata=cfef7a1f30f1e5f6 -C extra-filename=-cfef7a1f30f1e5f6 --out-dir C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --emit=dep-info,link -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --extern winapi=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libwinapi-d88a8c018c340227.rlib --cap-lints allow -l dylib=advapi32`
     Running `rustc C:\\msys64\\home\\ferris\\.cargo\\registry\\src\\github.com-0a35038f75765ae4\\ole32-sys-0.1.0\\src\\lib.rs --crate-name ole32 --crate-type lib -g -C metadata=5219fe2002394e46 -C extra-filename=-5219fe2002394e46 --out-dir C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --emit=dep-info,link -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --extern winapi=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libwinapi-d88a8c018c340227.rlib --cap-lints allow -l dylib=ole32`
     Running `rustc C:\\msys64\\home\\ferris\\.cargo\\registry\\src\\github.com-0a35038f75765ae4\\kernel32-sys-0.2.1\\src\\lib.rs --crate-name kernel32 --crate-type lib -g -C metadata=35c1a745c861d23a -C extra-filename=-35c1a745c861d23a --out-dir C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --emit=dep-info,link -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --extern winapi=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libwinapi-d88a8c018c340227.rlib --cap-lints allow -l dylib=kernel32`
   Compiling cpal v0.2.8
     Running `rustc C:\\msys64\\home\\ferris\\.cargo\\registry\\src\\github.com-0a35038f75765ae4\\cpal-0.2.8\\src\\lib.rs --crate-name cpal --crate-type lib -g -C metadata=239e4349acb60439 -C extra-filename=-239e4349acb60439 --out-dir C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --emit=dep-info,link -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --extern libc=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\liblibc-540159808ccfa9ab.rlib --extern lazy_static=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\liblazy_static-f3aa6dfcc7c157cc.rlib --extern winapi=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libwinapi-d88a8c018c340227.rlib --extern ole32=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libole32-5219fe2002394e46.rlib --cap-lints allow`
   Compiling gcc v0.3.20
     Running `rustc C:\\msys64\\home\\ferris\\.cargo\\registry\\src\\github.com-0a35038f75765ae4\\gcc-0.3.20\\src\\lib.rs --crate-name gcc --crate-type lib -g -C metadata=4ced25fee15775b3 -C extra-filename=-4ced25fee15775b3 --out-dir C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --emit=dep-info,link -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --extern winapi=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libwinapi-d88a8c018c340227.rlib --extern advapi32=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libadvapi32-cfef7a1f30f1e5f6.rlib --cap-lints allow`
   Compiling time v0.1.34
     Running `rustc C:\\msys64\\home\\ferris\\.cargo\\registry\\src\\github.com-0a35038f75765ae4\\time-0.1.34\\src\\lib.rs --crate-name time --crate-type lib -g -C metadata=b8d9c9e7ca5bced7 -C extra-filename=-b8d9c9e7ca5bced7 --out-dir C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --emit=dep-info,link -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --extern libc=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\liblibc-540159808ccfa9ab.rlib --extern winapi=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libwinapi-d88a8c018c340227.rlib --extern kernel32=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libkernel32-35c1a745c861d23a.rlib --cap-lints allow`
   Compiling vorbis-sys v0.0.8
     Running `rustc C:\\msys64\\home\\ferris\\.cargo\\registry\\src\\github.com-0a35038f75765ae4\\vorbis-sys-0.0.8\\libvorbis/build.rs --crate-name build_script_build --crate-type bin -g --out-dir C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\build\\vorbis-sys-4431916cf211c242 --emit=dep-info,link -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --extern pkg_config=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libpkg_config-376a9590956a17f5.rlib --extern gcc=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libgcc-4ced25fee15775b3.rlib --cap-lints allow`
   Compiling ogg-sys v0.0.9
     Running `rustc C:\\msys64\\home\\ferris\\.cargo\\registry\\src\\github.com-0a35038f75765ae4\\ogg-sys-0.0.9\\libogg/build.rs --crate-name build_script_build --crate-type bin -g --out-dir C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\build\\ogg-sys-7a1f7c9e45945cde --emit=dep-info,link -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --extern pkg_config=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libpkg_config-376a9590956a17f5.rlib --extern gcc=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libgcc-4ced25fee15775b3.rlib --cap-lints allow`
   Compiling vorbisfile-sys v0.0.8
     Running `rustc C:\\msys64\\home\\ferris\\.cargo\\registry\\src\\github.com-0a35038f75765ae4\\vorbisfile-sys-0.0.8\\libvorbisfile/build.rs --crate-name build_script_build --crate-type bin -g --out-dir C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\build\\vorbisfile-sys-4a255b9627c465d7 --emit=dep-info,link -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --extern gcc=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libgcc-4ced25fee15775b3.rlib --extern pkg_config=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libpkg_config-376a9590956a17f5.rlib --cap-lints allow`
     Running `C:\msys64\home\ferris\dev\projects\rodio\target\debug\build\ogg-sys-7a1f7c9e45945cde\build-script-build`
     Running `rustc C:\\msys64\\home\\ferris\\.cargo\\registry\\src\\github.com-0a35038f75765ae4\\ogg-sys-0.0.9\\src\\lib.rs --crate-name ogg_sys --crate-type lib -g -C metadata=7a1f7c9e45945cde -C extra-filename=-7a1f7c9e45945cde --out-dir C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --emit=dep-info,link -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --extern libc=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\liblibc-540159808ccfa9ab.rlib --cap-lints allow -L native=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\build\\ogg-sys-7a1f7c9e45945cde\\out -l static=ogg`
     Running `C:\msys64\home\ferris\dev\projects\rodio\target\debug\build\vorbis-sys-4431916cf211c242\build-script-build`
     Running `rustc C:\\msys64\\home\\ferris\\.cargo\\registry\\src\\github.com-0a35038f75765ae4\\vorbis-sys-0.0.8\\src\\lib.rs --crate-name vorbis_sys --crate-type lib -g -C metadata=4431916cf211c242 -C extra-filename=-4431916cf211c242 --out-dir C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --emit=dep-info,link -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --extern libc=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\liblibc-540159808ccfa9ab.rlib --extern ogg_sys=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libogg_sys-7a1f7c9e45945cde.rlib --cap-lints allow -L native=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\build\\vorbis-sys-4431916cf211c242\\out -l static=vorbis -L native=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\build\\ogg-sys-7a1f7c9e45945cde\\out`
     Running `C:\msys64\home\ferris\dev\projects\rodio\target\debug\build\vorbisfile-sys-4a255b9627c465d7\build-script-build`
     Running `rustc C:\\msys64\\home\\ferris\\.cargo\\registry\\src\\github.com-0a35038f75765ae4\\vorbisfile-sys-0.0.8\\src\\lib.rs --crate-name vorbisfile_sys --crate-type lib -g -C metadata=4a255b9627c465d7 -C extra-filename=-4a255b9627c465d7 --out-dir C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --emit=dep-info,link -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --extern ogg_sys=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libogg_sys-7a1f7c9e45945cde.rlib --extern libc=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\liblibc-540159808ccfa9ab.rlib --extern vorbis_sys=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libvorbis_sys-4431916cf211c242.rlib --cap-lints allow -L native=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\build\\vorbisfile-sys-4a255b9627c465d7\\out -l static=vorbisfile -L native=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\build\\ogg-sys-7a1f7c9e45945cde\\out -L native=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\build\\vorbis-sys-4431916cf211c242\\out`
   Compiling vorbis v0.0.13
     Running `rustc C:\\msys64\\home\\ferris\\.cargo\\registry\\src\\github.com-0a35038f75765ae4\\vorbis-0.0.13\\src\\lib.rs --crate-name vorbis --crate-type lib -g -C metadata=65edf5b33d9211e4 -C extra-filename=-65edf5b33d9211e4 --out-dir C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --emit=dep-info,link -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --extern libc=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\liblibc-540159808ccfa9ab.rlib --extern ogg_sys=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libogg_sys-7a1f7c9e45945cde.rlib --extern vorbis_sys=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libvorbis_sys-4431916cf211c242.rlib --extern vorbisfile_sys=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libvorbisfile_sys-4a255b9627c465d7.rlib --cap-lints allow -L native=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\build\\ogg-sys-7a1f7c9e45945cde\\out -L native=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\build\\vorbis-sys-4431916cf211c242\\out -L native=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\build\\vorbisfile-sys-4a255b9627c465d7\\out`
   Compiling rodio v0.3.1 (file:///C:/msys64/home/ferris/dev/projects/rodio)
     Running `rustc src\\lib.rs --crate-name rodio --crate-type lib -g --out-dir C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug --emit=dep-info,link -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --extern lazy_static=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\liblazy_static-f3aa6dfcc7c157cc.rlib --extern hound=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libhound-10ecc825f7963132.rlib --extern time=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libtime-b8d9c9e7ca5bced7.rlib --extern vorbis=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libvorbis-65edf5b33d9211e4.rlib --extern cpal=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libcpal-239e4349acb60439.rlib -L native=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\build\\ogg-sys-7a1f7c9e45945cde\\out -L native=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\build\\vorbis-sys-4431916cf211c242\\out -L native=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\build\\vorbisfile-sys-4a255b9627c465d7\\out`
     Running `rustc examples\\basic.rs --crate-name basic --crate-type bin -g --out-dir C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\examples --emit=dep-info,link -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug -L dependency=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps --extern lazy_static=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\liblazy_static-f3aa6dfcc7c157cc.rlib --extern hound=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libhound-10ecc825f7963132.rlib --extern time=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libtime-b8d9c9e7ca5bced7.rlib --extern vorbis=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libvorbis-65edf5b33d9211e4.rlib --extern cpal=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libcpal-239e4349acb60439.rlib --extern rodio=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\librodio.rlib -L native=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\build\\ogg-sys-7a1f7c9e45945cde\\out -L native=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\build\\vorbis-sys-4431916cf211c242\\out -L native=C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\build\\vorbisfile-sys-4a255b9627c465d7\\out`
error: linking with `gcc` failed: exit code: 1
note: "gcc" "-Wl,--enable-long-section-names" "-fno-use-linker-plugin" "-Wl,--nxcompat" "-static-libgcc" "-m64" "-L" "C:\\Program Files\\Rust stable 1.4\\bin\\rustlib\\x86_64-pc-windows-gnu\\lib" "C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\examples\\basic.0.o" "-o" "C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\examples\\basic.exe" "-Wl,--gc-sections" "C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\librodio.rlib" "C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libcpal-239e4349acb60439.rlib" "C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\liblazy_static-f3aa6dfcc7c157cc.rlib" "C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libole32-5219fe2002394e46.rlib" "C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libhound-10ecc825f7963132.rlib" "C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libvorbis-65edf5b33d9211e4.rlib" "C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libvorbisfile_sys-4a255b9627c465d7.rlib" "C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libtime-b8d9c9e7ca5bced7.rlib" "C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libkernel32-35c1a745c861d23a.rlib" "C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libwinapi-d88a8c018c340227.rlib" "C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libvorbis_sys-4431916cf211c242.rlib" "C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\libogg_sys-7a1f7c9e45945cde.rlib" "C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps\\liblibc-540159808ccfa9ab.rlib" "C:\\Program Files\\Rust stable 1.4\\bin\\rustlib\\x86_64-pc-windows-gnu\\lib\\libstd-1bf6e69c.rlib" "C:\\Program Files\\Rust stable 1.4\\bin\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcollections-1bf6e69c.rlib" "C:\\Program Files\\Rust stable 1.4\\bin\\rustlib\\x86_64-pc-windows-gnu\\lib\\librustc_unicode-1bf6e69c.rlib" "C:\\Program Files\\Rust stable 1.4\\bin\\rustlib\\x86_64-pc-windows-gnu\\lib\\librand-1bf6e69c.rlib" "C:\\Program Files\\Rust stable 1.4\\bin\\rustlib\\x86_64-pc-windows-gnu\\lib\\liballoc-1bf6e69c.rlib" "C:\\Program Files\\Rust stable 1.4\\bin\\rustlib\\x86_64-pc-windows-gnu\\lib\\liballoc_jemalloc-1bf6e69c.rlib" "C:\\Program Files\\Rust stable 1.4\\bin\\rustlib\\x86_64-pc-windows-gnu\\lib\\liblibc-1bf6e69c.rlib" "C:\\Program Files\\Rust stable 1.4\\bin\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcore-1bf6e69c.rlib" "-L" "C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug" "-L" "C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\deps" "-L" "C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\build\\ogg-sys-7a1f7c9e45945cde\\out" "-L" "C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\build\\vorbis-sys-4431916cf211c242\\out" "-L" "C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\target\\debug\\build\\vorbisfile-sys-4a255b9627c465d7\\out" "-L" "C:\\Program Files\\Rust stable 1.4\\bin\\rustlib\\x86_64-pc-windows-gnu\\lib" "-L" "C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\.rust\\bin\\x86_64-pc-windows-gnu" "-L" "C:\\msys64\\home\\ferris\\dev\\projects\\rodio\\bin\\x86_64-pc-windows-gnu" "-Wl,-Bstatic" "-Wl,-Bdynamic" "-l" "ole32" "-l" "kernel32" "-l" "ws2_32" "-l" "userenv" "-l" "advapi32" "-l" "compiler-rt"
note: C:\msys64\home\ferris\dev\projects\rodio\target\debug\deps\libvorbisfile_sys-4a255b9627c465d7.rlib(vorbisfile.o): In function `_get_data':
/home/ferris/.cargo/registry/src/github.com-0a35038f75765ae4/vorbisfile-sys-0.0.8/libvorbisfile/vorbisfile.c:68: undefined reference to `__errno'
/home/ferris/.cargo/registry/src/github.com-0a35038f75765ae4/vorbisfile-sys-0.0.8/libvorbisfile/vorbisfile.c:74: undefined reference to `__errno'

error: aborting due to previous error
Could not compile `rodio`.

Caused by:
  Process didn't exit successfully: `rustc examples\basic.rs --crate-name basic --crate-type bin -g --out-dir C:\msys64\home\ferris\dev\projects\rodio\target\debug\examples --emit=dep-info,link -L dependency=C:\msys64\home\ferris\dev\projects\rodio\target\debug -L dependency=C:\msys64\home\ferris\dev\projects\rodio\target\debug\deps --extern lazy_static=C:\msys64\home\ferris\dev\projects\rodio\target\debug\deps\liblazy_static-f3aa6dfcc7c157cc.rlib --extern hound=C:\msys64\home\ferris\dev\projects\rodio\target\debug\deps\libhound-10ecc825f7963132.rlib --extern time=C:\msys64\home\ferris\dev\projects\rodio\target\debug\deps\libtime-b8d9c9e7ca5bced7.rlib --extern vorbis=C:\msys64\home\ferris\dev\projects\rodio\target\debug\deps\libvorbis-65edf5b33d9211e4.rlib --extern cpal=C:\msys64\home\ferris\dev\projects\rodio\target\debug\deps\libcpal-239e4349acb60439.rlib --extern rodio=C:\msys64\home\ferris\dev\projects\rodio\target\debug\librodio.rlib -L native=C:\msys64\home\ferris\dev\projects\rodio\target\debug\build\ogg-sys-7a1f7c9e45945cde\out -L native=C:\msys64\home\ferris\dev\projects\rodio\target\debug\build\vorbis-sys-4431916cf211c242\out -L native=C:\msys64\home\ferris\dev\projects\rodio\target\debug\build\vorbisfile-sys-4a255b9627c465d7\out` (exit code: 101)

I haven't dug that deeply yet, but I did notice the dependencies for vorbis-rs don't point to specific versions; they all use "*", so it could be that they're out of sync.

By the way, any reason that project isn't on crates.io?

Examples not building for OSX

osx: v10.11.6 El Capitan
rust: stable v1.21 (doesn't build with nightly-2017-10-22 either)

Problem

  • Examples failing to build/run on my Mac
  • All the examples build/run fine on my Windows 10 machine with the same rust version(s).
  • I can build/run cpal examples fine on both Windows and OSX.

Error

error[E0277]: the trait bound `for<'r> std::ops::FnMut(cpal::cpal_impl::VoiceId, cpal::UnknownTypeBuffer<'r>) + 'static: std::marker::Send` is not satisfied
  --> src/engine.rs:35:18
   |
35 |                 .spawn({
   |                  ^^^^^ `for<'r> std::ops::FnMut(cpal::cpal_impl::VoiceId, cpal::UnknownTypeBuffer<'r>) + 'static` cannot be sent between threads safely
   |
   = help: the trait `std::marker::Send` is not implemented for `for<'r> std::ops::FnMut(cpal::cpal_impl::VoiceId, cpal::UnknownTypeBuffer<'r>) + 'static`
   = note: required because of the requirements on the impl of `std::marker::Send` for `&'static mut for<'r> std::ops::FnMut(cpal::cpal_impl::VoiceId, cpal::UnknownTypeBuffer<'r>) +
 'static`
   = note: required because of the requirements on the impl of `std::marker::Send` for `std::ptr::Unique<&'static mut for<'r> std::ops::FnMut(cpal::cpal_impl::VoiceId, cpal::Unknown
TypeBuffer<'r>) + 'static>`

// ... more stack trace

error[E0277]: the trait bound `for<'r> std::ops::FnMut(cpal::cpal_impl::VoiceId, cpal::UnknownTypeBuffer<'r>) + 'static: std::marker::Send` is not satisfied
  --> src/engine.rs:23:5
   |
23 | /     lazy_static! {
24 | |         static ref ENGINE: Arc<Engine> = {
25 | |             let engine = Arc::new(Engine {
26 | |                 events_loop: EventLoop::new(),
...  |
47 | |         };
48 | |     }
   | |_____^ `for<'r> std::ops::FnMut(cpal::cpal_impl::VoiceId, cpal::UnknownTypeBuffer<'r>) + 'static` cannot be sent between threads safely
   |
   = help: the trait `std::marker::Send` is not implemented for `for<'r> std::ops::FnMut(cpal::cpal_impl::VoiceId, cpal::UnknownTypeBuffer<'r>) + 'static`

// ... more stack trace

Please release a 0.5.3 with recent commits

In 8eaba65
default_endpoint() was exported, and usage examples recommend this pattern in the examples as seen by a potential user.
But this is not yet released, making user (me) confused.

With rodio 0.5.2:

warning: use of deprecated item 'rodio::get_default_endpoint'
  --> src\main.rs:10:20
   |
10 |     let endpoint = rodio::get_default_endpoint().unwrap();
   |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^

changing:

error[E0425]: cannot find function `default_endpoint` in module `rodio`
  --> src\main.rs:10:27
   |
10 |     let endpoint = rodio::default_endpoint().unwrap();
   |                           ^^^^^^^^^^^^^^^^ did you mean `get_default_endpoint`?

ogg sound is incorrect if using sink

the beginning of the sound is not correct.

full example can be found there: https://github.com/thiolliere/rodio_debug

I'm on linux

extern crate rodio;
use std::thread::sleep;
use std::time::Duration;
use std::io;
use std::io::BufReader;
use std::fs::File;
use std::io::Read;
use rodio::Source;

fn main() {
    let endpoint = rodio::get_default_endpoint().unwrap();


    // this sound is correct
    let source = rodio::Decoder::new(File::open("audio.ogg").unwrap()).unwrap();
    rodio::play_raw(&endpoint, source.convert_samples());
    sleep(Duration::from_secs(1));

    // this sound is not, the beginning is lower than it should
    let source = rodio::Decoder::new(File::open("audio.ogg").unwrap()).unwrap();
    let sink = rodio::Sink::new(&endpoint);
    sink.append(source);
    sink.detach();

    sleep(Duration::from_secs(10));
}

Is there support for streaming audio?

Hi,

I have tried integrating rodio with a websocket, as and when 4096 bytes of u8 data comes in, I have tried to pass it as i16 for play_raw api. The speaker is too noisy.

let mut converted: Vec<i16> = data.into_iter().map(|i|i as i16).collect();
rodio::play_raw(&endpoint, SamplesBuffer::new(1,44100, converted).convert_samples());

The same binary stream is sent to a node js server to write to a wav file plays just fine. So the binary stream seems to be proper.

  var fileWriter = new wav.FileWriter(outFile, {
    channels: 1,
    sampleRate: 48000,
    bitDepth: 16
  });

  client.on('stream', function(stream, meta) {
    stream.pipe(fileWriter);

Is there something I am missing here to make it work with rodio?

No way to check if sink is empty

While designing my game engine I needed to know when a Sink had finished playing its sounds without requiring a new thread always borrowing my Sink to do so. I created a custom version of rodio in my own repository that implements this feature, I'm willing to submit a PR for this if that's something you want in your API.

Playback artifacts in dynamic_mixer when using --release

There seems to be some kind of timing related issue that only manifests itself when building with --release.

Once https://github.com/tomaka/rodio/blob/master/src/dynamic_mixer.rs#L112 evaluates to true and casues next to return None the underlying audio buffer seems to still contain some non-zero samples and thus generates some very audible clicking noise which stays even when new inputs are added to the mixer. Even worse, the more sounds are played the more artifacts accumulate.

A "quick fix" seems to be to return Some(S::zero_value()) on line 113. However, I suspect that this might not be very performant so another potential "solution" seems to return just self.samples_rate() of these S::zero_value() frames, which seems to flush out the artifacts.

I haven't opened a PR for this yet since I don't think that I have profound enough knowledge of the library or audio programming in general to tell whether this fixes the actual underlying issue, or if I'm just applying band-aids here :)

Update: Further testing revealed that this still doesn't help all the time.

System: Linux 64 bit Ubuntu 14.10, Pulseaudio.

PS: I've uploaded a zip with my test sounds and a recording of the artifcats they cause:

https://www.dropbox.com/s/qttxa1taqi72poz/sounds.zip?dl=0

Certain ogg files don't play correctly but don't appear obviously corrupt.

ggez decodes a particular ogg or two incorrectly, giving corrupt output. Using release mode and debug mode both have the same results, so it's not my first thought, stuttering due to being too slow to keep the audio buffer full. vlc and audacious play the file perfectly fine. Re-encoding it with sox makes a file that ggez plays fine, but gives no decoding errors in the process. ogginfo shows similar info for both files, also gives no errors.

Repository that has a program that reproduces this, with included audio files: https://github.com/ggez/ggez-soundtest

Full report, with reproduction, here: ggez/ggez#207

I'm stumped, so I'm kicking this upstream.

To do: make a reproduction that only uses rodio, not ggez.

Playing the same sound several times requires allocating a new buffer each time

I've been looking at the various Rodio sources for a while and I'm unable to find any sources that allow reusing a buffer of sound.

The closest solution appears to be using SamplesBuffer and cloning the vector each time, but this is inefficient as it requires an allocation and deallocation for each played sound.

I believe this crate should contain a source that wraps a vector of samples in an Arc, allowing the allocated memory to be reused.

I've attached a sample implementation of such a source: main.rs.

Sound repeats forever when detaching sink (on Windows)

On Linux, if I detach the sink, it keeps playing the sound clip until it finishes. On Windows, a short segment (<100 ms) of the sound clip I'm playing starts repeating forever when I detach the sink. I've even tried stopping the sink before detaching it, but it has no effect. The only workaround I've found is to sleep for some time (e.g. 100 ms) after stopping it, before detaching it, but that's a bit inelegant.

extern crate rodio;

use std::fs::File;
use std::io::BufReader;
use std::time::Duration;

fn main() {
	let endpoint = rodio::get_default_endpoint().unwrap();
	let mut sink = rodio::Sink::new(&endpoint);
	sink.set_volume(0.1);

        // Using beep.wav from rodio/examples
	let source = File::open("res/beep.wav").ok()
		.and_then(|file| rodio::Decoder::new(BufReader::new(file)).ok()).unwrap();

	sink.append(source);
	std::thread::sleep(Duration::from_millis(100));

	// A stop, followed by a short sleep, prevents the bug from occuring.
	// sink.stop();
	// std::thread::sleep(Duration::from_millis(100));

	sink.detach();
	std::thread::sleep(Duration::from_secs(10)); // Sleep so we can hear the annoying sound for a while.
}

Stop playing the current song

How do I stop a sound from playing? Ex. I have a start menu screen with start menu music. When the user clicks a button, I switch to the game, now I want to stop playing the start menu music and start playing the game music. How do I stop the start menu music from playing? I can start new songs, but I can't stop old ones. Why can't I stop songs? Once I call rodio::play_raw (which is, to my knowledge, the only way to start playing), that's it. There's no way to stop playing the song once it's started: rodio::play_raw() consumes the song, so I can't pause for it in a loop.

Am I missing something obvious here? Why does rodio::play_raw not simply return a struct that lets me stop the song?

Here is sort-of what I have:

        let mut currently_played_song = "";

        while let Ok(signal) = rx.recv() {
            if signal == currently_played_song { continue; }
            currently_played_song = signal;

            let mut title_screen_song = static_decoder!(::assets::TITLE_SCREEN_SONG_DATA.clone())
                .repeat_infinite()
                .fade_in(Duration::from_secs(2))
                .pausable(false);

            match currently_played_song {
                ::assets::AUDIO_MSG_PLAY_TITLE_SCREEN_SONG |
                ::assets::AUDIO_MSG_PLAY_TITLE_SCREEN_SONG_STOP => {

                    if currently_played_song == ::assets::AUDIO_MSG_PLAY_TITLE_SCREEN_SONG_STOP {
                        title_screen_song.set_paused(true);
                    }

                    // I can't stop the damn song! 
                    ::rodio::play_raw(&endpoint, title_screen_song.convert_samples());

                },
                ::assets::AUDIO_MSG_PLAY_GAME_SONG => {
                    let game_screen_song_1 = static_decoder!(::assets::GAME_SONG_1_DATA)
                        .fade_in(Duration::from_secs(2));

                    let game_screen_song_2 = static_decoder!(::assets::GAME_SONG_2_DATA)
                        .fade_in(Duration::from_secs(2));

                    let game_screen_song_3 = static_decoder!(::assets::GAME_SONG_3_DATA)
                        .fade_in(Duration::from_secs(2));

                    // this just plays the second song on top of the first one, why???
                    ::rodio::play_raw(&endpoint, game_screen_song_1.convert_samples());
                },
                _ => {
                    println!("unknown sound message: {:?}", signal);
                }
            }
        }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.