Skip to content

Commit

Permalink
Update examples to use std Termination
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchmindtree committed Jun 21, 2019
1 parent a733bdb commit 0b6e66d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 30 deletions.
11 changes: 6 additions & 5 deletions examples/beep.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
extern crate cpal;
extern crate failure;

fn main() {
let device = cpal::default_output_device().expect("Failed to get default output device");
let format = device.default_output_format().expect("Failed to get default output format");
fn main() -> Result<(), failure::Error> {
let device = cpal::default_output_device().expect("failed to find a default output device");
let format = device.default_output_format()?;
let event_loop = cpal::EventLoop::new();
let stream_id = event_loop.build_output_stream(&device, &format).unwrap();
event_loop.play_stream(stream_id.clone()).unwrap();
let stream_id = event_loop.build_output_stream(&device, &format)?;
event_loop.play_stream(stream_id.clone())?;

let sample_rate = format.sample_rate.0 as f32;
let mut sample_clock = 0f32;
Expand Down
17 changes: 10 additions & 7 deletions examples/enumerate.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
extern crate cpal;
extern crate failure;

fn main() {
println!("Default Input Device:\n {:?}", cpal::default_input_device().map(|e| e.name().unwrap()));
println!("Default Output Device:\n {:?}", cpal::default_output_device().map(|e| e.name().unwrap()));
fn main() -> Result<(), failure::Error> {
let default_in = cpal::default_input_device().map(|e| e.name().unwrap());
let default_out = cpal::default_output_device().map(|e| e.name().unwrap());
println!("Default Input Device:\n {:?}", default_in);
println!("Default Output Device:\n {:?}", default_out);

let devices = cpal::devices().expect("failed to enumerate devices");
let devices = cpal::devices()?;
println!("Devices: ");
for (device_index, device) in devices.enumerate() {
println!("{}. \"{}\"",
device_index + 1,
device.name().unwrap());
println!("{}. \"{}\"", device_index + 1, device.name()?);

// Input formats
if let Ok(fmt) = device.default_input_format() {
Expand Down Expand Up @@ -47,4 +48,6 @@ fn main() {
}
}
}

Ok(())
}
24 changes: 13 additions & 11 deletions examples/feedback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,27 @@
//! precisely synchronised.
extern crate cpal;
extern crate failure;

const LATENCY_MS: f32 = 150.0;

fn main() {
fn main() -> Result<(), failure::Error> {
let event_loop = cpal::EventLoop::new();

// Default devices.
let input_device = cpal::default_input_device().expect("Failed to get default input device");
let output_device = cpal::default_output_device().expect("Failed to get default output device");
println!("Using default input device: \"{}\"", input_device.name().unwrap());
println!("Using default output device: \"{}\"", output_device.name().unwrap());
let input_device = cpal::default_input_device().expect("failed to get default input device");
let output_device = cpal::default_output_device().expect("failed to get default output device");
println!("Using default input device: \"{}\"", input_device.name()?);
println!("Using default output device: \"{}\"", output_device.name()?);

// We'll try and use the same format between streams to keep it simple
let mut format = input_device.default_input_format().expect("Failed to get default format");
let mut format = input_device.default_input_format()?;
format.data_type = cpal::SampleFormat::F32;

// Build streams.
println!("Attempting to build both streams with `{:?}`.", format);
let input_stream_id = event_loop.build_input_stream(&input_device, &format).unwrap();
let output_stream_id = event_loop.build_output_stream(&output_device, &format).unwrap();
let input_stream_id = event_loop.build_input_stream(&input_device, &format)?;
let output_stream_id = event_loop.build_output_stream(&output_device, &format)?;
println!("Successfully built streams.");

// Create a delay in case the input and output devices aren't synced.
Expand All @@ -38,13 +39,13 @@ fn main() {

// Fill the samples with 0.0 equal to the length of the delay.
for _ in 0..latency_samples {
tx.send(0.0).unwrap();
tx.send(0.0)?;
}

// Play the streams.
println!("Starting the input and output streams with `{}` milliseconds of latency.", LATENCY_MS);
event_loop.play_stream(input_stream_id.clone()).unwrap();
event_loop.play_stream(output_stream_id.clone()).unwrap();
event_loop.play_stream(input_stream_id.clone())?;
event_loop.play_stream(output_stream_id.clone())?;

// Run the event loop on a separate thread.
std::thread::spawn(move || {
Expand Down Expand Up @@ -87,4 +88,5 @@ fn main() {
println!("Playing for 3 seconds... ");
std::thread::sleep(std::time::Duration::from_secs(3));
println!("Done!");
Ok(())
}
15 changes: 8 additions & 7 deletions examples/record_wav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
//! The input data is recorded to "$CARGO_MANIFEST_DIR/recorded.wav".
extern crate cpal;
extern crate failure;
extern crate hound;

fn main() {
fn main() -> Result<(), failure::Error> {
// Setup the default input device and stream with the default input format.
let device = cpal::default_input_device().expect("Failed to get default input device");
println!("Default input device: {}", device.name().unwrap());
println!("Default input device: {}", device.name()?);
let format = device.default_input_format().expect("Failed to get default input format");
println!("Default input format: {:?}", format);
let event_loop = cpal::EventLoop::new();
let stream_id = event_loop.build_input_stream(&device, &format)
.expect("Failed to build input stream");
event_loop.play_stream(stream_id).unwrap();
let stream_id = event_loop.build_input_stream(&device, &format)?;
event_loop.play_stream(stream_id)?;

// The WAV file we're recording to.
const PATH: &'static str = concat!(env!("CARGO_MANIFEST_DIR"), "/recorded.wav");
let spec = wav_spec_from_format(&format);
let writer = hound::WavWriter::create(PATH, spec).unwrap();
let writer = hound::WavWriter::create(PATH, spec)?;
let writer = std::sync::Arc::new(std::sync::Mutex::new(Some(writer)));

// A flag to indicate that recording is in progress.
Expand Down Expand Up @@ -73,8 +73,9 @@ fn main() {
// Let recording go for roughly three seconds.
std::thread::sleep(std::time::Duration::from_secs(3));
recording.store(false, std::sync::atomic::Ordering::Relaxed);
writer.lock().unwrap().take().unwrap().finalize().unwrap();
writer.lock().unwrap().take().unwrap().finalize()?;
println!("Recording {} complete!", PATH);
Ok(())
}

fn sample_format(format: cpal::SampleFormat) -> hound::SampleFormat {
Expand Down

0 comments on commit 0b6e66d

Please sign in to comment.