-
Notifications
You must be signed in to change notification settings - Fork 503
Expand file tree
/
Copy pathrecord_wav.rs
More file actions
206 lines (181 loc) · 6.54 KB
/
record_wav.rs
File metadata and controls
206 lines (181 loc) · 6.54 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//! Records a WAV file (roughly 3 seconds long) using the default input device and config.
//!
//! The input data is recorded to "$CARGO_MANIFEST_DIR/recorded.wav".
use std::{
fs::File,
io::BufWriter,
sync::{Arc, Mutex},
};
use clap::Parser;
use cpal::{
traits::{DeviceTrait, HostTrait, StreamTrait},
Error, ErrorKind, FromSample, HostId, Sample, SampleFormat, SupportedStreamConfig,
};
#[derive(Parser, Debug)]
#[command(version, about = "CPAL record_wav example", long_about = None)]
struct Opt {
/// The audio device to use.
#[arg(short, long)]
device: Option<String>,
/// How long to record, in seconds
#[arg(long, default_value_t = 3)]
duration: u64,
/// Use the JACK host. Requires `--features jack`.
#[arg(long, default_value_t = false)]
jack: bool,
/// Use the PulseAudio host. Requires `--features pulseaudio`.
#[arg(long, default_value_t = false)]
pulseaudio: bool,
/// Use the Pipewire host. Requires `--features pipewire`
#[arg(long, default_value_t = false)]
pipewire: bool,
}
fn main() -> Result<(), anyhow::Error> {
let opt = Opt::parse();
// Jack/PulseAudio support must be enabled at compile time, and is
// only available on some platforms.
#[allow(unused_mut, unused_assignments)]
let mut jack_host_id: Result<HostId, Error> = Err(ErrorKind::HostUnavailable.into());
#[allow(unused_mut, unused_assignments)]
let mut pulseaudio_host_id: Result<HostId, Error> = Err(ErrorKind::HostUnavailable.into());
#[allow(unused_mut, unused_assignments)]
let mut pipewire_host_id: Result<HostId, Error> = Err(ErrorKind::HostUnavailable.into());
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd"
))]
{
#[cfg(feature = "jack")]
{
jack_host_id = Ok(HostId::Jack);
}
#[cfg(feature = "pulseaudio")]
{
pulseaudio_host_id = Ok(HostId::PulseAudio);
}
#[cfg(feature = "pipewire")]
{
pipewire_host_id = Ok(HostId::PipeWire);
}
}
// Manually check for flags. Can be passed through cargo with -- e.g.
// cargo run --release --example record_wav --features jack -- --jack
let host = if opt.jack {
jack_host_id
.and_then(cpal::host_from_id)
.expect("make sure `--features jack` is specified, and the platform is supported")
} else if opt.pulseaudio {
pulseaudio_host_id
.and_then(cpal::host_from_id)
.expect("make sure `--features pulseaudio` is specified, and the platform is supported")
} else if opt.pipewire {
pipewire_host_id
.and_then(cpal::host_from_id)
.expect("make sure `--features pipewire` is specified, and the platform is supported")
} else {
cpal::default_host()
};
// Set up the input device and stream with the default input config.
let device = if let Some(device) = opt.device {
let id = &device.parse().expect("failed to parse input device id");
host.device_by_id(id)
} else {
host.default_input_device()
}
.expect("failed to find input device");
println!("Input device: {}", device.id()?);
let config = if device.supports_input() {
device.default_input_config()
} else {
device.default_output_config()
}
.expect("Failed to get default input/output config");
println!("Default input/output config: {config:?}");
// The WAV file we're recording to.
const PATH: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/recorded.wav");
let spec = wav_spec_from_config(&config);
let writer = hound::WavWriter::create(PATH, spec)?;
let writer = Arc::new(Mutex::new(Some(writer)));
// A flag to indicate that recording is in progress.
println!("Begin recording...");
// Run the input stream on a separate thread.
let writer_2 = writer.clone();
let err_fn = move |err: Error| match err.kind() {
ErrorKind::DeviceChanged | ErrorKind::Xrun | ErrorKind::RealtimeDenied => {
eprintln!("{err}")
}
_ => eprintln!("Stream error: {err}"),
};
let stream = match config.sample_format() {
SampleFormat::I8 => device.build_input_stream(
config.into(),
move |data, _: &_| write_input_data::<i8, i8>(data, &writer_2),
err_fn,
None,
)?,
SampleFormat::I16 => device.build_input_stream(
config.into(),
move |data, _: &_| write_input_data::<i16, i16>(data, &writer_2),
err_fn,
None,
)?,
SampleFormat::I32 => device.build_input_stream(
config.into(),
move |data, _: &_| write_input_data::<i32, i32>(data, &writer_2),
err_fn,
None,
)?,
SampleFormat::F32 => device.build_input_stream(
config.into(),
move |data, _: &_| write_input_data::<f32, f32>(data, &writer_2),
err_fn,
None,
)?,
sample_format => {
return Err(anyhow::Error::msg(format!(
"Unsupported sample format '{sample_format}'"
)))
}
};
stream.play()?;
// Let recording go for roughly three seconds.
std::thread::sleep(std::time::Duration::from_secs(opt.duration));
drop(stream);
writer.lock().unwrap().take().unwrap().finalize()?;
println!("Recording {PATH} complete!");
Ok(())
}
fn sample_format(format: SampleFormat) -> hound::SampleFormat {
if format.is_dsd() {
panic!("DSD formats cannot be written to WAV files");
} else if format.is_float() {
hound::SampleFormat::Float
} else {
hound::SampleFormat::Int
}
}
fn wav_spec_from_config(config: &SupportedStreamConfig) -> hound::WavSpec {
hound::WavSpec {
channels: config.channels() as _,
sample_rate: config.sample_rate() as _,
bits_per_sample: (config.sample_format().sample_size() * 8) as _,
sample_format: sample_format(config.sample_format()),
}
}
type WavWriterHandle = Arc<Mutex<Option<hound::WavWriter<BufWriter<File>>>>>;
fn write_input_data<T, U>(input: &[T], writer: &WavWriterHandle)
where
T: Sample,
U: Sample + hound::Sample + FromSample<T>,
{
if let Ok(mut guard) = writer.try_lock() {
if let Some(writer) = guard.as_mut() {
for &sample in input.iter() {
let sample: U = U::from_sample(sample);
writer.write_sample(sample).ok();
}
}
}
}