diff --git a/examples/beep.rs b/examples/beep.rs index 15ec10e..10fcc09 100644 --- a/examples/beep.rs +++ b/examples/beep.rs @@ -20,7 +20,7 @@ fn main() { loop { let mut buffer = channel.append_data(1, cpal::SamplesRate(44100), 32768); - for sample in buffer.samples() { + for sample in buffer.iter_mut() { let value = data_source.next().unwrap(); *sample = value; } diff --git a/examples/music.rs b/examples/music.rs index 57905b0..c9fd041 100644 --- a/examples/music.rs +++ b/examples/music.rs @@ -21,7 +21,7 @@ fn main() { } let mut buffer = channel.append_data(channels, cpal::SamplesRate(rate as u32), data.len()); - let mut buffer = buffer.samples(); + let mut buffer = buffer.iter_mut(); loop { let next_sample = match data.get(0) { diff --git a/src/lib.rs b/src/lib.rs index 2185128..1d89349 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,13 +31,8 @@ pub struct Buffer<'a, T> { // if this is non-none, then the data will be written to `conversion.intermediate_buffer` // instead of `target`, and the conversion will be done in buffer's destructor conversion: Option>, - // number of elements that have been written - elements_written: uint, } -/// Iterator over the samples of the buffer. -pub struct SamplesIter<'a, 'b, T: 'b>(&'b mut uint, std::iter::Skip>); - struct RequiredConversion { intermediate_buffer: Vec, from_sample_rate: SamplesRate, @@ -164,43 +159,26 @@ impl Channel { from_channels: channels, to_channels: target_channels, }), - elements_written: 0, } } else { Buffer { target: Some(self.0.append_data(max_elements)), conversion: None, - elements_written: 0, } } } } -impl<'a, T> Buffer<'a, T> { - pub fn samples<'b>(&'b mut self) -> SamplesIter<'a, 'b, T> { - let iter = if let Some(ref mut conversion) = self.conversion { - conversion.intermediate_buffer.as_mut_slice().iter_mut() - } else { - self.target.as_mut().unwrap().get_buffer().iter_mut() - }; - - let iter = iter.skip(self.elements_written); - - SamplesIter(&mut self.elements_written, iter) +impl<'a, T> Deref<[T]> for Buffer<'a, T> { + fn deref(&self) -> &[T] { + panic!("It is forbidden to read from the audio buffer"); } } -/// Iterator over the samples of the buffer. -impl<'a, 'b, T> Iterator<&'b mut T> for SamplesIter<'a, 'b, T> { - fn next(&mut self) -> Option<&'b mut T> { - match self.1.next() { - Some(v) => { - *self.0 += 1; - Some(v) - }, - None => None - } +impl<'a, T> DerefMut<[T]> for Buffer<'a, T> { + fn deref_mut(&mut self) -> &mut [T] { + self.target.as_mut().unwrap().get_buffer() } } @@ -236,12 +214,11 @@ impl<'a, T> Drop for Buffer<'a, T> where T: Sample { let output = self.target.as_mut().unwrap().get_buffer(); assert!(buffer.len() == output.len(), "Buffers length mismatch: {} vs {}", buffer.len(), output.len()); - self.elements_written += buffer.len(); for (i, o) in buffer.into_iter().zip(output.iter_mut()) { *o = i; } } - self.target.take().unwrap().finish(self.elements_written); + self.target.take().unwrap().finish(); } } diff --git a/src/wasapi/mod.rs b/src/wasapi/mod.rs index 7f269c4..8ee1530 100644 --- a/src/wasapi/mod.rs +++ b/src/wasapi/mod.rs @@ -21,7 +21,6 @@ pub struct Buffer<'a, T> { render_client: *mut winapi::IAudioRenderClient, buffer: CVec, frames: winapi::UINT32, - channels: winapi::WORD, start_on_drop: bool, } @@ -87,7 +86,6 @@ impl Channel { audio_client: self.audio_client, render_client: self.render_client, buffer: buffer, - channels: self.num_channels, frames: frames_available, start_on_drop: !self.started, }; @@ -120,13 +118,11 @@ impl<'a, T> Buffer<'a, T> { self.buffer.as_mut_slice() } - pub fn finish(self, elements_written: uint) { - let elements_written = elements_written / self.channels as uint; - + pub fn finish(self) { // releasing buffer unsafe { let f = self.render_client.as_mut().unwrap().lpVtbl.as_ref().unwrap().ReleaseBuffer; - let hresult = f(self.render_client, elements_written as u32, 0); + let hresult = f(self.render_client, self.frames as u32, 0); check_result(hresult).unwrap(); if self.start_on_drop {