2014-12-11 16:23:33 +00:00
|
|
|
#![feature(unsafe_destructor)]
|
2014-12-11 13:22:55 +00:00
|
|
|
|
|
|
|
#[cfg(all(not(windows)))]
|
|
|
|
use this_platform_is_not_supported;
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
#[path="wasapi/mod.rs"]
|
2014-12-11 15:28:26 +00:00
|
|
|
pub mod cpal_impl;
|
2014-12-11 16:23:33 +00:00
|
|
|
|
2014-12-11 17:09:48 +00:00
|
|
|
/// A `Channel` represents a sound output.
|
|
|
|
///
|
|
|
|
/// A channel must be periodically filled with new data, or the sound will
|
|
|
|
/// stop playing.
|
2014-12-11 16:23:33 +00:00
|
|
|
pub struct Channel(cpal_impl::Channel);
|
|
|
|
|
2014-12-11 18:02:04 +00:00
|
|
|
/// Number of channels.
|
|
|
|
pub type ChannelsCount = u16;
|
|
|
|
|
2014-12-11 17:09:48 +00:00
|
|
|
/// Represents a buffer that must be filled with audio data.
|
|
|
|
///
|
|
|
|
/// A `Buffer` object borrows the channel.
|
2014-12-11 18:02:04 +00:00
|
|
|
pub struct Buffer<'a>(cpal_impl::Buffer<'a>);
|
2014-12-11 16:23:33 +00:00
|
|
|
|
2014-12-11 18:07:58 +00:00
|
|
|
/// 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,
|
|
|
|
}
|
|
|
|
|
2014-12-11 16:23:33 +00:00
|
|
|
impl Channel {
|
|
|
|
pub fn new() -> Channel {
|
|
|
|
let channel = cpal_impl::Channel::new();
|
|
|
|
Channel(channel)
|
|
|
|
}
|
|
|
|
|
2014-12-11 17:09:48 +00:00
|
|
|
/// Returns the number of channels.
|
|
|
|
///
|
|
|
|
/// 1 for mono, 2 for stereo, etc.
|
2014-12-11 18:02:04 +00:00
|
|
|
pub fn get_channels(&self) -> ChannelsCount {
|
2014-12-11 16:23:33 +00:00
|
|
|
self.0.get_channels()
|
|
|
|
}
|
|
|
|
|
2014-12-11 18:07:58 +00:00
|
|
|
/// 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()
|
|
|
|
}
|
|
|
|
|
2014-12-11 17:09:48 +00:00
|
|
|
/// Adds some PCM data to the channel's buffer.
|
|
|
|
///
|
|
|
|
/// 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
|
|
|
|
/// of the backend.
|
2014-12-11 18:02:04 +00:00
|
|
|
pub fn append_data<'a>(&'a mut self) -> Buffer<'a> {
|
2014-12-11 16:23:33 +00:00
|
|
|
Buffer(self.0.append_data())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-11 18:02:04 +00:00
|
|
|
impl<'a> Deref<[u8]> for Buffer<'a> {
|
|
|
|
fn deref(&self) -> &[u8] {
|
2014-12-11 16:23:33 +00:00
|
|
|
panic!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-11 18:02:04 +00:00
|
|
|
impl<'a> DerefMut<[u8]> for Buffer<'a> {
|
|
|
|
fn deref_mut(&mut self) -> &mut [u8] {
|
2014-12-11 16:23:33 +00:00
|
|
|
self.0.get_buffer()
|
|
|
|
}
|
|
|
|
}
|