Skip to content

Commit

Permalink
Run rustfmt on the code (RustAudio#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaka authored Oct 11, 2017
1 parent cdcef96 commit 2028d59
Show file tree
Hide file tree
Showing 13 changed files with 624 additions and 401 deletions.
18 changes: 18 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
fn_args_density = "Compressed"
fn_args_layout = "Visual"
fn_brace_style = "SameLineWhere"
fn_call_style = "Visual"
fn_empty_single_line = false
format_strings = true
generics_indent = "Visual"
impl_empty_single_line = false
match_block_trailing_comma = true
reorder_imported_names = true
reorder_imports = true
reorder_imports_in_group = true
spaces_around_ranges = true
use_try_shorthand = true
where_density = "Tall"
where_style = "Legacy"
wrap_match_arms = false
write_mode = "Overwrite"
50 changes: 34 additions & 16 deletions examples/beep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ impl Executor for MyExecutor {

fn main() {
let endpoint = cpal::default_endpoint().expect("Failed to get default endpoint");
let format = endpoint.supported_formats().unwrap().next().expect("Failed to get endpoint format");
let format = endpoint
.supported_formats()
.unwrap()
.next()
.expect("Failed to get endpoint format");

let event_loop = cpal::EventLoop::new();
let executor = Arc::new(MyExecutor);

let (mut voice, stream) = cpal::Voice::new(&endpoint, &format, &event_loop).expect("Failed to create a voice");
let (mut voice, stream) = cpal::Voice::new(&endpoint, &format, &event_loop)
.expect("Failed to create a voice");

// Produce a sinusoid of maximum amplitude.
let samples_rate = format.samples_rate.0 as f32;
Expand All @@ -36,37 +41,50 @@ fn main() {
task::spawn(stream.for_each(move |buffer| -> Result<_, ()> {
match buffer {
cpal::UnknownTypeBuffer::U16(mut buffer) => {
for (sample, value) in buffer.chunks_mut(format.channels.len()).zip(&mut data_source) {
for (sample, value) in buffer
.chunks_mut(format.channels.len())
.zip(&mut data_source)
{
let value = ((value * 0.5 + 0.5) * std::u16::MAX as f32) as u16;
for out in sample.iter_mut() { *out = value; }
for out in sample.iter_mut() {
*out = value;
}
}
},

cpal::UnknownTypeBuffer::I16(mut buffer) => {
for (sample, value) in buffer.chunks_mut(format.channels.len()).zip(&mut data_source) {
for (sample, value) in buffer
.chunks_mut(format.channels.len())
.zip(&mut data_source)
{
let value = (value * std::i16::MAX as f32) as i16;
for out in sample.iter_mut() { *out = value; }
for out in sample.iter_mut() {
*out = value;
}
}
},

cpal::UnknownTypeBuffer::F32(mut buffer) => {
for (sample, value) in buffer.chunks_mut(format.channels.len()).zip(&mut data_source) {
for out in sample.iter_mut() { *out = value; }
for (sample, value) in buffer
.chunks_mut(format.channels.len())
.zip(&mut data_source)
{
for out in sample.iter_mut() {
*out = value;
}
}
},
};

Ok(())
})).execute(executor);

thread::spawn(move || {
loop {
thread::sleep(Duration::from_millis(500));
voice.pause();
thread::sleep(Duration::from_millis(500));
voice.play();
}
});
thread::spawn(move || loop {
thread::sleep(Duration::from_millis(500));
voice.pause();
thread::sleep(Duration::from_millis(500));
voice.play();
});

event_loop.run();
}
11 changes: 8 additions & 3 deletions examples/enumerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ extern crate cpal;

fn main() {
let endpoints = cpal::endpoints();

println!("Endpoints: ");
for (endpoint_index, endpoint) in endpoints.enumerate() {
println!("{}. Endpoint \"{}\" Audio formats: ", endpoint_index + 1, endpoint.name());
println!("{}. Endpoint \"{}\" Audio formats: ",
endpoint_index + 1,
endpoint.name());

let formats = match endpoint.supported_formats() {
Ok(f) => f,
Err(e) => { println!("Error: {:?}", e); continue; }
Err(e) => {
println!("Error: {:?}", e);
continue;
},
};

for (format_index, format) in formats.enumerate() {
Expand Down
19 changes: 12 additions & 7 deletions src/alsa/enumerate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

use super::Endpoint;
use super::alsa;
use super::check_errors;
use super::Endpoint;

use std::ffi::CStr;
use std::ffi::CString;
Expand All @@ -17,8 +18,10 @@ pub struct EndpointsIterator {
next_str: *const *const u8,
}

unsafe impl Send for EndpointsIterator {}
unsafe impl Sync for EndpointsIterator {}
unsafe impl Send for EndpointsIterator {
}
unsafe impl Sync for EndpointsIterator {
}

impl Drop for EndpointsIterator {
#[inline]
Expand All @@ -34,8 +37,8 @@ impl Default for EndpointsIterator {
unsafe {
let mut hints = mem::uninitialized();
// TODO: check in which situation this can fail
check_errors(alsa::snd_device_name_hint(-1, b"pcm\0".as_ptr() as *const _,
&mut hints)).unwrap();
check_errors(alsa::snd_device_name_hint(-1, b"pcm\0".as_ptr() as *const _, &mut hints))
.unwrap();

let hints = hints as *const *const u8;

Expand Down Expand Up @@ -95,8 +98,10 @@ impl Iterator for EndpointsIterator {
// trying to open the PCM device to see if it can be opened
let name_zeroed = CString::new(name.clone()).unwrap();
let mut playback_handle = mem::uninitialized();
if alsa::snd_pcm_open(&mut playback_handle, name_zeroed.as_ptr() as *const _,
alsa::SND_PCM_STREAM_PLAYBACK, alsa::SND_PCM_NONBLOCK) == 0
if alsa::snd_pcm_open(&mut playback_handle,
name_zeroed.as_ptr() as *const _,
alsa::SND_PCM_STREAM_PLAYBACK,
alsa::SND_PCM_NONBLOCK) == 0
{
alsa::snd_pcm_close(playback_handle);
} else {
Expand Down
Loading

0 comments on commit 2028d59

Please sign in to comment.