Buffer now always has the u8 format
This commit is contained in:
parent
d6f49a5176
commit
0bdafdab9b
|
@ -15,14 +15,17 @@ fn main() {
|
||||||
let max: u16 = Int::max_value();
|
let max: u16 = Int::max_value();
|
||||||
let value = (max as f32 / 2.0) + (angle * (max as f32 / 2.0));
|
let value = (max as f32 / 2.0) + (angle * (max as f32 / 2.0));
|
||||||
value as u16
|
value as u16
|
||||||
})
|
});
|
||||||
.map(|v| (v, v));
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let mut buffer = channel.append_data::<(u16, u16)>();
|
let mut buffer = channel.append_data();
|
||||||
|
|
||||||
for value in buffer.iter_mut() {
|
for sample in buffer.chunks_mut(4) {
|
||||||
*value = data_source.next().unwrap();
|
let value = data_source.next().unwrap();
|
||||||
|
|
||||||
|
let mut writer = std::io::BufWriter::new(sample);
|
||||||
|
writer.write_le_u16(value).unwrap();
|
||||||
|
writer.write_le_u16(value).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
17
src/lib.rs
17
src/lib.rs
|
@ -13,10 +13,13 @@ pub mod cpal_impl;
|
||||||
/// stop playing.
|
/// stop playing.
|
||||||
pub struct Channel(cpal_impl::Channel);
|
pub struct Channel(cpal_impl::Channel);
|
||||||
|
|
||||||
|
/// Number of channels.
|
||||||
|
pub type ChannelsCount = u16;
|
||||||
|
|
||||||
/// Represents a buffer that must be filled with audio data.
|
/// Represents a buffer that must be filled with audio data.
|
||||||
///
|
///
|
||||||
/// A `Buffer` object borrows the channel.
|
/// A `Buffer` object borrows the channel.
|
||||||
pub struct Buffer<'a, T>(cpal_impl::Buffer<'a, T>);
|
pub struct Buffer<'a>(cpal_impl::Buffer<'a>);
|
||||||
|
|
||||||
impl Channel {
|
impl Channel {
|
||||||
pub fn new() -> Channel {
|
pub fn new() -> Channel {
|
||||||
|
@ -27,7 +30,7 @@ impl Channel {
|
||||||
/// Returns the number of channels.
|
/// Returns the number of channels.
|
||||||
///
|
///
|
||||||
/// 1 for mono, 2 for stereo, etc.
|
/// 1 for mono, 2 for stereo, etc.
|
||||||
pub fn get_channels(&self) -> u16 {
|
pub fn get_channels(&self) -> ChannelsCount {
|
||||||
self.0.get_channels()
|
self.0.get_channels()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,19 +39,19 @@ impl Channel {
|
||||||
/// This function returns a `Buffer` object that must be filled with the audio data.
|
/// This function returns a `Buffer` object that must be filled with the audio data.
|
||||||
/// You can't know in advance the size of the buffer, as it depends on the current state
|
/// You can't know in advance the size of the buffer, as it depends on the current state
|
||||||
/// of the backend.
|
/// of the backend.
|
||||||
pub fn append_data<'a, T>(&'a mut self) -> Buffer<'a, T> {
|
pub fn append_data<'a>(&'a mut self) -> Buffer<'a> {
|
||||||
Buffer(self.0.append_data())
|
Buffer(self.0.append_data())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T> Deref<[T]> for Buffer<'a, T> {
|
impl<'a> Deref<[u8]> for Buffer<'a> {
|
||||||
fn deref(&self) -> &[T] {
|
fn deref(&self) -> &[u8] {
|
||||||
panic!()
|
panic!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T> DerefMut<[T]> for Buffer<'a, T> {
|
impl<'a> DerefMut<[u8]> for Buffer<'a> {
|
||||||
fn deref_mut(&mut self) -> &mut [T] {
|
fn deref_mut(&mut self) -> &mut [u8] {
|
||||||
self.0.get_buffer()
|
self.0.get_buffer()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,10 +13,10 @@ pub struct Channel {
|
||||||
started: bool,
|
started: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Buffer<'a, T> {
|
pub struct Buffer<'a> {
|
||||||
audio_client: *mut winapi::IAudioClient,
|
audio_client: *mut winapi::IAudioClient,
|
||||||
render_client: *mut winapi::IAudioRenderClient,
|
render_client: *mut winapi::IAudioRenderClient,
|
||||||
buffer: CVec<T>,
|
buffer: CVec<u8>,
|
||||||
frames: winapi::UINT32,
|
frames: winapi::UINT32,
|
||||||
start_on_drop: bool,
|
start_on_drop: bool,
|
||||||
}
|
}
|
||||||
|
@ -26,13 +26,11 @@ impl Channel {
|
||||||
init().unwrap()
|
init().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_channels(&self) -> u16 {
|
pub fn get_channels(&self) -> ::ChannelsCount {
|
||||||
self.num_channels as u16
|
self.num_channels as ::ChannelsCount
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn append_data<'a, T>(&'a mut self) -> Buffer<'a, T> {
|
pub fn append_data<'a>(&'a mut self) -> Buffer<'a> {
|
||||||
assert!(mem::size_of::<T>() == self.bytes_per_frame as uint);
|
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
loop {
|
loop {
|
||||||
//
|
//
|
||||||
|
@ -52,14 +50,16 @@ impl Channel {
|
||||||
}
|
}
|
||||||
|
|
||||||
// loading buffer
|
// loading buffer
|
||||||
let buffer: CVec<T> = {
|
let buffer: CVec<u8> = {
|
||||||
let mut buffer: *mut winapi::BYTE = mem::uninitialized();
|
let mut buffer: *mut winapi::BYTE = mem::uninitialized();
|
||||||
let f = self.render_client.as_mut().unwrap().lpVtbl.as_ref().unwrap().GetBuffer;
|
let f = self.render_client.as_mut().unwrap().lpVtbl.as_ref().unwrap().GetBuffer;
|
||||||
let hresult = f(self.render_client, frames_available,
|
let hresult = f(self.render_client, frames_available,
|
||||||
&mut buffer as *mut *mut libc::c_uchar);
|
&mut buffer as *mut *mut libc::c_uchar);
|
||||||
check_result(hresult).unwrap();
|
check_result(hresult).unwrap();
|
||||||
assert!(!buffer.is_null());
|
assert!(!buffer.is_null());
|
||||||
CVec::new(buffer as *mut T, frames_available as uint)
|
|
||||||
|
CVec::new(buffer as *mut u8,
|
||||||
|
frames_available as uint * self.bytes_per_frame as uint)
|
||||||
};
|
};
|
||||||
|
|
||||||
let buffer = Buffer {
|
let buffer = Buffer {
|
||||||
|
@ -77,14 +77,14 @@ impl Channel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T> Buffer<'a, T> {
|
impl<'a> Buffer<'a> {
|
||||||
pub fn get_buffer(&mut self) -> &mut [T] {
|
pub fn get_buffer(&mut self) -> &mut [u8] {
|
||||||
self.buffer.as_mut_slice()
|
self.buffer.as_mut_slice()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unsafe_destructor]
|
#[unsafe_destructor]
|
||||||
impl<'a, T> Drop for Buffer<'a, T> {
|
impl<'a> Drop for Buffer<'a> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
// releasing buffer
|
// releasing buffer
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
Loading…
Reference in New Issue