Skip to content

Conversation

emuell
Copy link
Owner

@emuell emuell commented Jul 14, 2025

fixes #2

@leofidus
Copy link

leofidus commented Jul 15, 2025

That was fast. This definitely feels like the correct way to handle it.

Testing it, it almost works. By which I mean it initially failed because device.default_output_config() in preferred_output_config returns an unsupported output config. As a test I changed the function like this for manually select a fallback

fn preferred_output_config(
        device: &cpal::Device,
    ) -> Result<cpal::SupportedStreamConfig, Error> {
        for s in device.supported_output_configs()? {
            dbg!(&s);
            let rates = s.min_sample_rate()..=s.max_sample_rate();
            if s.channels() == PREFERRED_CHANNELS
                && s.sample_format() == PREFERRED_SAMPLE_FORMAT
                && rates.contains(&PREFERRED_SAMPLE_RATE)
            {
                return Ok(s.with_sample_rate(PREFERRED_SAMPLE_RATE));
            }
        }

        let fallback_config =
            device
                .supported_output_configs()?
                .next()
                .ok_or(Error::OutputDeviceError(Box::new(
                    cpal::DefaultStreamConfigError::DeviceNotAvailable,
                )))?;
        let rates = fallback_config.min_sample_rate()..=fallback_config.max_sample_rate();
        if rates.contains(&PREFERRED_SAMPLE_RATE) {
            return Ok(fallback_config.with_sample_rate(PREFERRED_SAMPLE_RATE));
        }
        return Ok(fallback_config.with_sample_rate(fallback_config.max_sample_rate()));
    }

And now it works.

Which confirms that the handling of non-f32 sample formats now works. I will file an issue with cpal about what looks like a bug in default_output_config(), as that is really a separate though related issue. Not sure if you want to work around that bug or just wait for it to be fixed by cpal

@emuell
Copy link
Owner Author

emuell commented Jul 16, 2025

Quicky looking at this, both supported_output_configs and default_output_config get the format from IAudioClient::GetMixFormat(), so both should result into a valid format. However, I can't replicate this on my machines, so I think it's better if you report this issue to the CPAL team.

We're currently using cpal 0.15, but according to https://github.com/RustAudio/cpal/blob/master/CHANGELOG.md this wasn't fixed there. I'll update to 0.16 anyway after merging this as there are other nice improvements.

Thanks for the feedback!

@emuell emuell merged commit 8956c90 into master Jul 16, 2025
1 check passed
@emuell emuell deleted the fix/cpal-sample-formats branch August 29, 2025 19:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

play-file example crashes if output device does not support f32 sample format

2 participants