diff --git a/examples/beep.rs b/examples/beep.rs index c43d1a7..adbc750 100644 --- a/examples/beep.rs +++ b/examples/beep.rs @@ -3,6 +3,8 @@ extern crate cpal; fn main() { let mut channel = cpal::Channel::new(); + assert!(channel.get_samples_format() == cpal::SampleFormat::U16); + // producing a sinusoid let mut data_source = std::iter::iterate(0.0f32, |f| f + 0.03) diff --git a/src/lib.rs b/src/lib.rs index 8d9fcb6..28df48d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,6 +21,15 @@ pub type ChannelsCount = u16; /// A `Buffer` object borrows the channel. pub struct Buffer<'a>(cpal_impl::Buffer<'a>); +/// Format that each sample has. +#[deriving(Clone, Show, PartialEq, Eq)] +pub enum SampleFormat { + /// The value 0 corresponds to 0. + I16, + /// The value 0 corresponds to 32768. + U16, +} + impl Channel { pub fn new() -> Channel { let channel = cpal_impl::Channel::new(); @@ -34,6 +43,20 @@ impl Channel { self.0.get_channels() } + /// Returns the number of samples that are played per second. + /// + /// Common values are 22050 Hz or 44100 Hz. + pub fn get_samples_per_second(&self) -> u32 { + self.0.get_samples_per_second() + } + + /// Returns the number of samples that are played per second. + /// + /// Common values are 22050 Hz or 44100 Hz. + pub fn get_samples_format(&self) -> SampleFormat { + self.0.get_samples_format() + } + /// Adds some PCM data to the channel's buffer. /// /// This function returns a `Buffer` object that must be filled with the audio data. diff --git a/src/wasapi/mod.rs b/src/wasapi/mod.rs index c146804..bd75655 100644 --- a/src/wasapi/mod.rs +++ b/src/wasapi/mod.rs @@ -10,6 +10,8 @@ pub struct Channel { max_frames_in_buffer: winapi::UINT32, num_channels: winapi::WORD, bytes_per_frame: winapi::WORD, + samples_per_second: winapi::DWORD, + bits_per_sample: winapi::WORD, started: bool, } @@ -30,6 +32,17 @@ impl Channel { self.num_channels as ::ChannelsCount } + pub fn get_samples_per_second(&self) -> u32 { + self.samples_per_second as u32 + } + + pub fn get_samples_format(&self) -> ::SampleFormat { + match self.bits_per_sample { + 16 => ::SampleFormat::U16, + _ => unimplemented!(), + } + } + pub fn append_data<'a>(&'a mut self) -> Buffer<'a> { unsafe { loop { @@ -202,6 +215,8 @@ fn init() -> Result { max_frames_in_buffer: max_frames_in_buffer, num_channels: format.nChannels, bytes_per_frame: format.nBlockAlign, + samples_per_second: format.nSamplesPerSec, + bits_per_sample: format.wBitsPerSample, started: false, }) }