Merge remote-tracking branch 'upstream/master'

merging with upstream changes
This commit is contained in:
mitchmindtree 2015-02-23 00:50:10 +11:00
commit 2b86445c9a
5 changed files with 21 additions and 18 deletions

View File

@ -1,7 +1,7 @@
[package] [package]
name = "cpal" name = "cpal"
version = "0.0.13" version = "0.0.15"
authors = ["Pierre Krieger <pierre.krieger1708@gmail.com>"] authors = ["Pierre Krieger <pierre.krieger1708@gmail.com>"]
description = "Cross-platform audio playing library in pure Rust." description = "Cross-platform audio playing library in pure Rust."
repository = "https://github.com/tomaka/cpal" repository = "https://github.com/tomaka/cpal"

View File

@ -19,7 +19,7 @@ impl Voice {
let name = ffi::CString::from_slice(b"default"); let name = ffi::CString::from_slice(b"default");
let mut playback_handle = mem::uninitialized(); let mut playback_handle = mem::uninitialized();
check_errors(alsa::snd_pcm_open(&mut playback_handle, name.as_slice_with_nul().as_ptr(), alsa::SND_PCM_STREAM_PLAYBACK, alsa::SND_PCM_NONBLOCK)).unwrap(); check_errors(alsa::snd_pcm_open(&mut playback_handle, name.as_ptr(), alsa::SND_PCM_STREAM_PLAYBACK, alsa::SND_PCM_NONBLOCK)).unwrap();
let mut hw_params = mem::uninitialized(); let mut hw_params = mem::uninitialized();
check_errors(alsa::snd_pcm_hw_params_malloc(&mut hw_params)).unwrap(); check_errors(alsa::snd_pcm_hw_params_malloc(&mut hw_params)).unwrap();

View File

@ -99,7 +99,7 @@ pub struct SamplesRate(pub u32);
/// You should destroy this object as soon as possible. Data is only committed when it /// You should destroy this object as soon as possible. Data is only committed when it
/// is destroyed. /// is destroyed.
#[must_use] #[must_use]
pub struct Buffer<'a, T> { pub struct Buffer<'a, T: 'a> {
// also contains something, taken by `Drop` // also contains something, taken by `Drop`
target: Option<cpal_impl::Buffer<'a, T>>, target: Option<cpal_impl::Buffer<'a, T>>,

View File

@ -32,11 +32,11 @@ pub trait Sample: Copy + Clone {
fn interpolate(self, other: Self) -> Self; fn interpolate(self, other: Self) -> Self;
/// Turns the data into samples of type `I16`. /// Turns the data into samples of type `I16`.
fn to_vec_i16(&[Self]) -> Cow<Vec<i16>, [i16]>; fn to_vec_i16(&[Self]) -> Cow<[i16]>;
/// Turns the data into samples of type `U16`. /// Turns the data into samples of type `U16`.
fn to_vec_u16(&[Self]) -> Cow<Vec<u16>, [u16]>; fn to_vec_u16(&[Self]) -> Cow<[u16]>;
/// Turns the data into samples of type `F32`. /// Turns the data into samples of type `F32`.
fn to_vec_f32(&[Self]) -> Cow<Vec<f32>, [f32]>; fn to_vec_f32(&[Self]) -> Cow<[f32]>;
} }
impl Sample for u16 { impl Sample for u16 {
@ -48,7 +48,7 @@ impl Sample for u16 {
(self + other) / 2 (self + other) / 2
} }
fn to_vec_i16(input: &[u16]) -> Cow<Vec<i16>, [i16]> { fn to_vec_i16(input: &[u16]) -> Cow<[i16]> {
Cow::Owned(input.iter().map(|&value| { Cow::Owned(input.iter().map(|&value| {
if value >= 32768 { if value >= 32768 {
(value - 32768) as i16 (value - 32768) as i16
@ -58,11 +58,11 @@ impl Sample for u16 {
}).collect()) }).collect())
} }
fn to_vec_u16(input: &[u16]) -> Cow<Vec<u16>, [u16]> { fn to_vec_u16(input: &[u16]) -> Cow<[u16]> {
Cow::Borrowed(input) Cow::Borrowed(input)
} }
fn to_vec_f32(input: &[u16]) -> Cow<Vec<f32>, [f32]> { fn to_vec_f32(input: &[u16]) -> Cow<[f32]> {
Cow::Owned(Sample::to_vec_f32(Sample::to_vec_i16(input).as_slice()).to_vec()) Cow::Owned(Sample::to_vec_f32(Sample::to_vec_i16(input).as_slice()).to_vec())
} }
} }
@ -76,11 +76,11 @@ impl Sample for i16 {
(self + other) / 2 (self + other) / 2
} }
fn to_vec_i16(input: &[i16]) -> Cow<Vec<i16>, [i16]> { fn to_vec_i16(input: &[i16]) -> Cow<[i16]> {
Cow::Borrowed(input) Cow::Borrowed(input)
} }
fn to_vec_u16(input: &[i16]) -> Cow<Vec<u16>, [u16]> { fn to_vec_u16(input: &[i16]) -> Cow<[u16]> {
Cow::Owned(input.iter().map(|&value| { Cow::Owned(input.iter().map(|&value| {
if value < 0 { if value < 0 {
(value + 32767) as u16 + 1 (value + 32767) as u16 + 1
@ -90,7 +90,7 @@ impl Sample for i16 {
}).collect()) }).collect())
} }
fn to_vec_f32(input: &[i16]) -> Cow<Vec<f32>, [f32]> { fn to_vec_f32(input: &[i16]) -> Cow<[f32]> {
Cow::Owned(input.iter().map(|&value| { Cow::Owned(input.iter().map(|&value| {
if value > 0 { if value > 0 {
value as f32 / 32767.0 value as f32 / 32767.0
@ -110,7 +110,7 @@ impl Sample for f32 {
(self + other) / 2.0 (self + other) / 2.0
} }
fn to_vec_i16(input: &[f32]) -> Cow<Vec<i16>, [i16]> { fn to_vec_i16(input: &[f32]) -> Cow<[i16]> {
Cow::Owned(input.iter().map(|&value| { Cow::Owned(input.iter().map(|&value| {
if value >= 0.0 { if value >= 0.0 {
(value * 32767.0) as i16 (value * 32767.0) as i16
@ -120,7 +120,7 @@ impl Sample for f32 {
}).collect()) }).collect())
} }
fn to_vec_u16(input: &[f32]) -> Cow<Vec<u16>, [u16]> { fn to_vec_u16(input: &[f32]) -> Cow<[u16]> {
Cow::Owned(input.iter().map(|&value| { Cow::Owned(input.iter().map(|&value| {
if value >= 0.0 { if value >= 0.0 {
((value * 32767.0) + 32768.0) as u16 ((value * 32767.0) + 32768.0) as u16
@ -130,7 +130,7 @@ impl Sample for f32 {
}).collect()) }).collect())
} }
fn to_vec_f32(input: &[f32]) -> Cow<Vec<f32>, [f32]> { fn to_vec_f32(input: &[f32]) -> Cow<[f32]> {
Cow::Borrowed(input) Cow::Borrowed(input)
} }
} }

View File

@ -3,6 +3,7 @@ extern crate winapi;
extern crate "ole32-sys" as ole32; extern crate "ole32-sys" as ole32;
use std::{slice, mem, ptr}; use std::{slice, mem, ptr};
use std::marker::PhantomData;
// TODO: determine if should be NoSend or not // TODO: determine if should be NoSend or not
pub struct Voice { pub struct Voice {
@ -16,11 +17,12 @@ pub struct Voice {
playing: bool, playing: bool,
} }
pub struct Buffer<'a, T> { pub struct Buffer<'a, T: 'a> {
render_client: *mut winapi::IAudioRenderClient, render_client: *mut winapi::IAudioRenderClient,
buffer_data: *mut T, buffer_data: *mut T,
buffer_len: usize, buffer_len: usize,
frames: winapi::UINT32, frames: winapi::UINT32,
marker: PhantomData<&'a mut T>,
} }
impl Voice { impl Voice {
@ -86,6 +88,7 @@ impl Voice {
buffer_data: buffer_data, buffer_data: buffer_data,
buffer_len: buffer_len, buffer_len: buffer_len,
frames: frames_available, frames: frames_available,
marker: PhantomData,
}; };
return buffer; return buffer;
@ -140,7 +143,7 @@ impl Drop for Voice {
impl<'a, T> Buffer<'a, T> { impl<'a, T> Buffer<'a, T> {
pub fn get_buffer<'b>(&'b mut self) -> &'b mut [T] { pub fn get_buffer<'b>(&'b mut self) -> &'b mut [T] {
unsafe { unsafe {
slice::from_raw_mut_buf(&self.buffer_data, self.buffer_len) slice::from_raw_parts_mut(self.buffer_data, self.buffer_len)
} }
} }
@ -264,7 +267,7 @@ fn init() -> Result<Voice, String> {
fn check_result(result: winapi::HRESULT) -> Result<(), String> { fn check_result(result: winapi::HRESULT) -> Result<(), String> {
if result < 0 { if result < 0 {
return Err(::std::os::error_string(result as usize)); // TODO: return Err(::std::os::error_string(result)); // TODO:
} }
Ok(()) Ok(())