cpal icon indicating copy to clipboard operation
cpal copied to clipboard

FR: Add volume and mute controls to Device

Open micolous opened this issue 4 years ago • 25 comments

I want to be able to use this library to control the volume and mute state of input and output Devices, ie:

let input_device = host.default_input_device().unwrap();
println!("microphone mute state: {}", input_device.get_mute());
input_device.set_mute(true);

let output_device = host.default_output_device().unwrap();
println!("output device volume: {} dB, {}%",
    output_device.get_volume_db(),
    output_device.get_volume_scalar() * 100);

// Set output volume to -10dB
output_device.set_volume_db(-10.);
// Set output volume to "scalar" 50% of maximum
output_device.set_volume_scalar(0.5);

micolous avatar Mar 15 '21 05:03 micolous

I'm not sure this should be cpal's responsibility. For ideal gain staging, volume should be set in hardware. I doubt it is possible to do this in a portable way. Of course cpal could simply apply a gain reduction to the samples as a fallback, but that would likely get confusing for the user if it does not interact with the OS or sound server's volume control.

Be-ing avatar Mar 18 '21 07:03 Be-ing

Duplicate of #436?

Be-ing avatar Mar 18 '21 07:03 Be-ing

I'm interested in very simple, device-level controls. I think #436 is asking about than mixing audio sources from different applications.

Device-level mixer controls is fairly straight forward to implement, and I've had a crack at doing this for CoreAudio (macOS, based on coreaudio-rs) and Windows APIs. Doing Linux wouldn't be too difficult either, but I'd need to get a Linux box running that has audio devices. 😄

Moving this functionality "upstream" as it were into a library like this means that I don't need to worry so much about writing unsafe platform-specific code, and that everyone in the Rust ecosystem can benefit from it.

micolous avatar May 09 '21 02:05 micolous