Add some formats-related functions

This commit is contained in:
Pierre Krieger 2014-12-11 19:07:58 +01:00
parent 0bdafdab9b
commit 99c23327d8
3 changed files with 40 additions and 0 deletions

View File

@ -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)

View File

@ -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.

View File

@ -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<Channel, String> {
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,
})
}