Simplest audio crate for total beginner

Hello community,

I am totally new to Rust with not too much general programming background, just building my first Rust learning project. For that project I would love to be able to play simple sound, be it single notes (e.g. C4) or frequencies. Searching I found a small library that appeared to do just that:
After downloading the example code did not compile, apparently because it uses linux-specific code and I am on Windows.

So my first question is: could I have seen on that this was not for Windows? Any flags to watch out for in further search of crates?

Everything else that I find seems to be "bindings to..." or "wrapper for..." things that I know to little of, to decide which route to take. It appears that I would have to delve into details of complex audio systems and probably read C++ howtos but I don't want that now. I want to focus on learning Rust first.

Thus, my second question is: Can someone please guide me which way to go to generate the most simple sounds on Windows preferably with good beginner's documentation, preferably easy to set up, preferably multiplatform, should the project turn out to be useful for others?

Thank you for your time and effort,

You can play files with rodio, which is quite simple. If I remember well, it works on Linux and Windows.

1 Like

I don't believe there was any way for you know in this case, other than looking at the source code and knowing that std::os::unix cannot be used on Windows. It's something the crate maintainer should've (in my opinion at least) documented clearly.

As for the second question, that's something I was interested in as well, so I went ahead and tried rodio.

fn main() {
    let device = rodio::default_output_device().unwrap();
    let sink = rodio::Sink::new(&device);
    sink.set_volume(0.1); // the default is pretty loud...

    let source = rodio::source::SineWave::new(256);
    sink.append(source); //beep!

    // the audio is played (forever, in this case) on a background thread, so we need to sleep to not end main too early

If you want to try running this, I recommend doing a release build, as I sometimes got crackling at the start in debug builds. You can also construct the data being played yourself, but unfortunately I'm clueless when it comes to audio and don't know how to get meaningful audio out that way.

1 Like

Thank you to both of you!
rodio installs nicely and makes sounds. The following produces four tones in a row. It is important to use a fresh sink for every tone. I first tried to call the stop method on a sink and then append a new sinewave but that does not work.

use rodio::{Sink};
use std::{thread, time};

fn main() {
    let device = rodio::default_output_device().unwrap();
    let period = time::Duration::from_millis(1000);
    for i in [932, 587, 523, 466].iter() {
        let sink = Sink::new(&device);

Those sinewaves sound terrible and later on I will have to compute waveforms on my own. Maybe after making progress in my Rust abilities and more experience reading documentation I will find an entry point for that.

Thank you very much, again!

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.