Trying to output sound (cpal)

I'm trying to output a simple sound (a square waveform). All seems to work except no sound is going to my speakers. I found cpal as a lib to manage sound, but maybe there is a better way to handle streams. Here is my code

use cpal::{Sample};
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};

fn main() {
    let err_fn = |err| eprintln!("an error occurred on the output audio stream: {}", err);
    let host = cpal::default_host();
    let device = host.default_output_device().expect("no output device available");
    let supported_config = device.default_output_config().unwrap();

    println!("Device: {}, Using config: {:?}","flute"), supported_config);

    let config = supported_config.into();

    let stream = device.build_output_stream(&config, write_silence, err_fn).unwrap();;


fn write_silence(data: &mut [f32], _: &cpal::OutputCallbackInfo) {
    let mut counter = 0;
    for sample in data.iter_mut() {
        let s = if (counter / 20) % 2 == 0 { &1.0 } else { &0.0 };
        counter = counter + 1;
        *sample = Sample::from(s);
    println!("{:?}", data);

line 27 (println!("{:?}", data)) does output the samples of a square waveform.
I'm running this in the linux/crostini thing of chromebooks. It can be the problem.

Does anyone knows why it's not working ? And is cpal a good start for audio processing ?

According to Chrome OS devices/Crostini - ArchWiki sound only works in crostini starting from Chrome OS 74 with the crostini guest tools installed inside the linux system. On ArchLinux that would for example be cros-container-guest-tools-git in the AUR. If your linux distro doesn't ship with them I think you can build it from source (chromiumos/containers/cros-container-guest-tools - Git at Google)

I finally found a solution here : alsa - No sound from speakers using Chromebook - Ask Ubuntu

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.