-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
66 lines (59 loc) · 2.19 KB
/
lib.rs
File metadata and controls
66 lines (59 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#![deny(clippy::all)]
use napi::Result;
use napi_derive::napi;
/// Gathers the human readable device name of each output device detected
#[napi]
pub fn get_sound_devices() -> Result<Vec<String>, &'static str> {
cpvc::safe::get_sound_devices().map_err(to_napi_error)
}
/// Get whether the system volume is muted
#[napi]
pub fn get_mute() -> Result<bool, &'static str> {
cpvc::safe::get_mute().map_err(to_napi_error)
}
/// Gathers the current volume in percent of the default output device
#[napi]
pub fn get_system_volume() -> Result<u8, &'static str> {
cpvc::safe::get_system_volume().map_err(to_napi_error)
}
/// Mute or unmute the system volume
#[napi]
pub fn set_mute(mute: bool) -> Result<(), &'static str> {
cpvc::safe::set_mute(mute).map_err(to_napi_error)
}
/// Sets the current volume in percent of the default output device.
/// The `percent` argument should be a number between 0-100.
///
/// # On macOS
/// `cpvc` needs to mute and unmute the audio device to get the hardware device volume to sync
#[napi]
pub fn set_system_volume(percent: u8) -> Result<(), &'static str> {
if percent > 100 {
Err(napi::Error::new(
"BAD_ARGUMENT",
"percent should be a percentage (0-100)",
))
} else {
cpvc::safe::set_system_volume(percent).map_err(to_napi_error)
}
}
fn to_napi_error(err: cpvc::error::Error) -> napi::Error<&'static str> {
match err {
cpvc::error::Error::DeviceNotFound => napi::Error::new("DEVICE_NOT_FOUND", "device not found"),
cpvc::error::Error::DeviceAccessFailed(reason) => {
napi::Error::new("DEVICE_ACCESS_FAILED", reason)
}
cpvc::error::Error::DeviceEnumerationFailed(reason) => {
napi::Error::new("DEVICE_ENUMERATION_FAILED", reason)
}
cpvc::error::Error::VolumeCaptureFailed(reason) => {
napi::Error::new("VOLUME_CAPTURE_FAILED", reason)
}
cpvc::error::Error::VolumeSetFailed(reason) => napi::Error::new("VOLUME_SET_FAILED", reason),
cpvc::error::Error::MuteSetFailed(reason) => napi::Error::new("MUTE_SET_FAILED", reason),
cpvc::error::Error::PlatformUnsupported => {
napi::Error::new("PLATFORM_UNSUPPORTED", "this platform is not supported")
}
_ => napi::Error::new("UNKNOWN", "unknown error"),
}
}