Rename `SamplesRate` to `SampleRate` and `ChannelsCount` to `ChannelCount` (#199)
* Rename SamplesRate to SampleRate and samples_rate to sample_rate * Rename ChannelsCount to ChannelCount * Update CHANGELOG for SamplesRate and ChannelsCount renaming
This commit is contained in:
parent
f2728f6bdf
commit
fcc75f4566
|
@ -1,5 +1,7 @@
|
||||||
# Unreleased
|
# Unreleased
|
||||||
|
|
||||||
|
- Rename ChannelsCount to ChannelCount.
|
||||||
|
- Rename SamplesRate to SampleRate.
|
||||||
- Remove unused ChannelPosition API.
|
- Remove unused ChannelPosition API.
|
||||||
- Implement Endpoint and Format Enumeration for macos.
|
- Implement Endpoint and Format Enumeration for macos.
|
||||||
- Implement format handling for macos `build_voice` method.
|
- Implement format handling for macos `build_voice` method.
|
||||||
|
|
|
@ -7,19 +7,19 @@ fn main() {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.next()
|
.next()
|
||||||
.expect("Failed to get endpoint format")
|
.expect("Failed to get endpoint format")
|
||||||
.with_max_samples_rate();
|
.with_max_sample_rate();
|
||||||
|
|
||||||
let event_loop = cpal::EventLoop::new();
|
let event_loop = cpal::EventLoop::new();
|
||||||
let voice_id = event_loop.build_voice(&endpoint, &format).unwrap();
|
let voice_id = event_loop.build_voice(&endpoint, &format).unwrap();
|
||||||
event_loop.play(voice_id);
|
event_loop.play(voice_id);
|
||||||
|
|
||||||
let samples_rate = format.samples_rate.0 as f32;
|
let sample_rate = format.sample_rate.0 as f32;
|
||||||
let mut sample_clock = 0f32;
|
let mut sample_clock = 0f32;
|
||||||
|
|
||||||
// Produce a sinusoid of maximum amplitude.
|
// Produce a sinusoid of maximum amplitude.
|
||||||
let mut next_value = || {
|
let mut next_value = || {
|
||||||
sample_clock = (sample_clock + 1.0) % samples_rate;
|
sample_clock = (sample_clock + 1.0) % sample_rate;
|
||||||
(sample_clock * 440.0 * 2.0 * 3.141592 / samples_rate).sin()
|
(sample_clock * 440.0 * 2.0 * 3.141592 / sample_rate).sin()
|
||||||
};
|
};
|
||||||
|
|
||||||
event_loop.run(move |_, buffer| {
|
event_loop.run(move |_, buffer| {
|
||||||
|
|
|
@ -3,12 +3,12 @@ extern crate libc;
|
||||||
|
|
||||||
pub use self::enumerate::{EndpointsIterator, default_endpoint};
|
pub use self::enumerate::{EndpointsIterator, default_endpoint};
|
||||||
|
|
||||||
use ChannelsCount;
|
use ChannelCount;
|
||||||
use CreationError;
|
use CreationError;
|
||||||
use Format;
|
use Format;
|
||||||
use FormatsEnumerationError;
|
use FormatsEnumerationError;
|
||||||
use SampleFormat;
|
use SampleFormat;
|
||||||
use SamplesRate;
|
use SampleRate;
|
||||||
use SupportedFormat;
|
use SupportedFormat;
|
||||||
use UnknownTypeBuffer;
|
use UnknownTypeBuffer;
|
||||||
|
|
||||||
|
@ -149,7 +149,7 @@ impl Endpoint {
|
||||||
ptr::null_mut()))
|
ptr::null_mut()))
|
||||||
.expect("unable to get maximum supported rate");
|
.expect("unable to get maximum supported rate");
|
||||||
|
|
||||||
let samples_rates = if min_rate == max_rate {
|
let sample_rates = if min_rate == max_rate {
|
||||||
vec![(min_rate, max_rate)]
|
vec![(min_rate, max_rate)]
|
||||||
} else if alsa::snd_pcm_hw_params_test_rate(playback_handle,
|
} else if alsa::snd_pcm_hw_params_test_rate(playback_handle,
|
||||||
hw_params.0,
|
hw_params.0,
|
||||||
|
@ -206,21 +206,21 @@ impl Endpoint {
|
||||||
num,
|
num,
|
||||||
) == 0
|
) == 0
|
||||||
{
|
{
|
||||||
Some(num as ChannelsCount)
|
Some(num as ChannelCount)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let mut output = Vec::with_capacity(supported_formats.len() * supported_channels.len() *
|
let mut output = Vec::with_capacity(supported_formats.len() * supported_channels.len() *
|
||||||
samples_rates.len());
|
sample_rates.len());
|
||||||
for &data_type in supported_formats.iter() {
|
for &data_type in supported_formats.iter() {
|
||||||
for channels in supported_channels.iter() {
|
for channels in supported_channels.iter() {
|
||||||
for &(min_rate, max_rate) in samples_rates.iter() {
|
for &(min_rate, max_rate) in sample_rates.iter() {
|
||||||
output.push(SupportedFormat {
|
output.push(SupportedFormat {
|
||||||
channels: channels.clone(),
|
channels: channels.clone(),
|
||||||
min_samples_rate: SamplesRate(min_rate as u32),
|
min_sample_rate: SampleRate(min_rate as u32),
|
||||||
max_samples_rate: SamplesRate(max_rate as u32),
|
max_sample_rate: SampleRate(max_rate as u32),
|
||||||
data_type: data_type,
|
data_type: data_type,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -552,7 +552,7 @@ impl EventLoop {
|
||||||
.expect("format could not be set");
|
.expect("format could not be set");
|
||||||
check_errors(alsa::snd_pcm_hw_params_set_rate(playback_handle,
|
check_errors(alsa::snd_pcm_hw_params_set_rate(playback_handle,
|
||||||
hw_params.0,
|
hw_params.0,
|
||||||
format.samples_rate.0 as libc::c_uint,
|
format.sample_rate.0 as libc::c_uint,
|
||||||
0))
|
0))
|
||||||
.expect("sample rate could not be set");
|
.expect("sample rate could not be set");
|
||||||
check_errors(alsa::snd_pcm_hw_params_set_channels(playback_handle,
|
check_errors(alsa::snd_pcm_hw_params_set_channels(playback_handle,
|
||||||
|
@ -560,7 +560,7 @@ impl EventLoop {
|
||||||
format.channels as
|
format.channels as
|
||||||
libc::c_uint))
|
libc::c_uint))
|
||||||
.expect("channel count could not be set");
|
.expect("channel count could not be set");
|
||||||
let mut max_buffer_size = format.samples_rate.0 as alsa::snd_pcm_uframes_t /
|
let mut max_buffer_size = format.sample_rate.0 as alsa::snd_pcm_uframes_t /
|
||||||
format.channels as alsa::snd_pcm_uframes_t /
|
format.channels as alsa::snd_pcm_uframes_t /
|
||||||
5; // 200ms of buffer
|
5; // 200ms of buffer
|
||||||
check_errors(alsa::snd_pcm_hw_params_set_buffer_size_max(playback_handle,
|
check_errors(alsa::snd_pcm_hw_params_set_buffer_size_max(playback_handle,
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
extern crate coreaudio;
|
extern crate coreaudio;
|
||||||
extern crate core_foundation_sys;
|
extern crate core_foundation_sys;
|
||||||
|
|
||||||
use ChannelsCount;
|
use ChannelCount;
|
||||||
use CreationError;
|
use CreationError;
|
||||||
use Format;
|
use Format;
|
||||||
use FormatsEnumerationError;
|
use FormatsEnumerationError;
|
||||||
use Sample;
|
use Sample;
|
||||||
use SampleFormat;
|
use SampleFormat;
|
||||||
use SamplesRate;
|
use SampleRate;
|
||||||
use SupportedFormat;
|
use SupportedFormat;
|
||||||
use UnknownTypeBuffer;
|
use UnknownTypeBuffer;
|
||||||
|
|
||||||
|
@ -151,9 +151,9 @@ impl Endpoint {
|
||||||
let mut fmts = vec![];
|
let mut fmts = vec![];
|
||||||
for range in ranges {
|
for range in ranges {
|
||||||
let fmt = SupportedFormat {
|
let fmt = SupportedFormat {
|
||||||
channels: n_channels as ChannelsCount,
|
channels: n_channels as ChannelCount,
|
||||||
min_samples_rate: SamplesRate(range.mMinimum as _),
|
min_sample_rate: SampleRate(range.mMinimum as _),
|
||||||
max_samples_rate: SamplesRate(range.mMaximum as _),
|
max_sample_rate: SampleRate(range.mMaximum as _),
|
||||||
data_type: sample_format,
|
data_type: sample_format,
|
||||||
};
|
};
|
||||||
fmts.push(fmt);
|
fmts.push(fmt);
|
||||||
|
@ -282,7 +282,7 @@ impl EventLoop {
|
||||||
|
|
||||||
// Set the stream in interleaved mode.
|
// Set the stream in interleaved mode.
|
||||||
let n_channels = format.channels as usize;
|
let n_channels = format.channels as usize;
|
||||||
let sample_rate = format.samples_rate.0;
|
let sample_rate = format.sample_rate.0;
|
||||||
let bytes_per_channel = format.data_type.sample_size();
|
let bytes_per_channel = format.data_type.sample_size();
|
||||||
let bits_per_channel = bytes_per_channel * 8;
|
let bits_per_channel = bytes_per_channel * 8;
|
||||||
let bytes_per_frame = n_channels * bytes_per_channel;
|
let bytes_per_frame = n_channels * bytes_per_channel;
|
||||||
|
|
|
@ -184,8 +184,8 @@ impl Endpoint {
|
||||||
vec![
|
vec![
|
||||||
SupportedFormat {
|
SupportedFormat {
|
||||||
channels: 2,
|
channels: 2,
|
||||||
min_samples_rate: ::SamplesRate(44100),
|
min_sample_rate: ::SampleRate(44100),
|
||||||
max_samples_rate: ::SamplesRate(44100),
|
max_sample_rate: ::SampleRate(44100),
|
||||||
data_type: ::SampleFormat::F32,
|
data_type: ::SampleFormat::F32,
|
||||||
},
|
},
|
||||||
].into_iter(),
|
].into_iter(),
|
||||||
|
|
26
src/lib.rs
26
src/lib.rs
|
@ -40,14 +40,14 @@
|
||||||
//! let mut supported_formats_range = endpoint.supported_formats()
|
//! let mut supported_formats_range = endpoint.supported_formats()
|
||||||
//! .expect("error while querying formats");
|
//! .expect("error while querying formats");
|
||||||
//! let format = supported_formats_range.next().expect("no supported format?!")
|
//! let format = supported_formats_range.next().expect("no supported format?!")
|
||||||
//! .with_max_samples_rate();
|
//! .with_max_sample_rate();
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! Now that we have everything, we can create a voice from that event loop:
|
//! Now that we have everything, we can create a voice from that event loop:
|
||||||
//!
|
//!
|
||||||
//! ```no_run
|
//! ```no_run
|
||||||
//! # let endpoint = cpal::default_endpoint().unwrap();
|
//! # let endpoint = cpal::default_endpoint().unwrap();
|
||||||
//! # let format = endpoint.supported_formats().unwrap().next().unwrap().with_max_samples_rate();
|
//! # let format = endpoint.supported_formats().unwrap().next().unwrap().with_max_sample_rate();
|
||||||
//! # let event_loop = cpal::EventLoop::new();
|
//! # let event_loop = cpal::EventLoop::new();
|
||||||
//! let voice_id = event_loop.build_voice(&endpoint, &format).unwrap();
|
//! let voice_id = event_loop.build_voice(&endpoint, &format).unwrap();
|
||||||
//! ```
|
//! ```
|
||||||
|
@ -238,17 +238,17 @@ impl Endpoint {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Number of channels.
|
/// Number of channels.
|
||||||
pub type ChannelsCount = u16;
|
pub type ChannelCount = u16;
|
||||||
|
|
||||||
///
|
///
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub struct SamplesRate(pub u32);
|
pub struct SampleRate(pub u32);
|
||||||
|
|
||||||
/// Describes a format.
|
/// Describes a format.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct Format {
|
pub struct Format {
|
||||||
pub channels: ChannelsCount,
|
pub channels: ChannelCount,
|
||||||
pub samples_rate: SamplesRate,
|
pub sample_rate: SampleRate,
|
||||||
pub data_type: SampleFormat,
|
pub data_type: SampleFormat,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -274,11 +274,11 @@ impl Iterator for SupportedFormatsIterator {
|
||||||
/// Describes a range of supported formats.
|
/// Describes a range of supported formats.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct SupportedFormat {
|
pub struct SupportedFormat {
|
||||||
pub channels: ChannelsCount,
|
pub channels: ChannelCount,
|
||||||
/// Minimum value for the samples rate of the supported formats.
|
/// Minimum value for the samples rate of the supported formats.
|
||||||
pub min_samples_rate: SamplesRate,
|
pub min_sample_rate: SampleRate,
|
||||||
/// Maximum value for the samples rate of the supported formats.
|
/// Maximum value for the samples rate of the supported formats.
|
||||||
pub max_samples_rate: SamplesRate,
|
pub max_sample_rate: SampleRate,
|
||||||
/// Type of data expected by the endpoint.
|
/// Type of data expected by the endpoint.
|
||||||
pub data_type: SampleFormat,
|
pub data_type: SampleFormat,
|
||||||
}
|
}
|
||||||
|
@ -286,10 +286,10 @@ pub struct SupportedFormat {
|
||||||
impl SupportedFormat {
|
impl SupportedFormat {
|
||||||
/// Turns this `SupportedFormat` into a `Format` corresponding to the maximum samples rate.
|
/// Turns this `SupportedFormat` into a `Format` corresponding to the maximum samples rate.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn with_max_samples_rate(self) -> Format {
|
pub fn with_max_sample_rate(self) -> Format {
|
||||||
Format {
|
Format {
|
||||||
channels: self.channels,
|
channels: self.channels,
|
||||||
samples_rate: self.max_samples_rate,
|
sample_rate: self.max_sample_rate,
|
||||||
data_type: self.data_type,
|
data_type: self.data_type,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -300,8 +300,8 @@ impl From<Format> for SupportedFormat {
|
||||||
fn from(format: Format) -> SupportedFormat {
|
fn from(format: Format) -> SupportedFormat {
|
||||||
SupportedFormat {
|
SupportedFormat {
|
||||||
channels: format.channels,
|
channels: format.channels,
|
||||||
min_samples_rate: format.samples_rate,
|
min_sample_rate: format.sample_rate,
|
||||||
max_samples_rate: format.samples_rate,
|
max_sample_rate: format.sample_rate,
|
||||||
data_type: format.data_type,
|
data_type: format.data_type,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,10 +7,10 @@ use std::ptr;
|
||||||
use std::slice;
|
use std::slice;
|
||||||
use std::sync::{Arc, Mutex, MutexGuard};
|
use std::sync::{Arc, Mutex, MutexGuard};
|
||||||
|
|
||||||
use ChannelsCount;
|
use ChannelCount;
|
||||||
use FormatsEnumerationError;
|
use FormatsEnumerationError;
|
||||||
use SampleFormat;
|
use SampleFormat;
|
||||||
use SamplesRate;
|
use SampleRate;
|
||||||
use SupportedFormat;
|
use SupportedFormat;
|
||||||
|
|
||||||
use super::check_result;
|
use super::check_result;
|
||||||
|
@ -144,7 +144,7 @@ impl Endpoint {
|
||||||
},
|
},
|
||||||
winapi::WAVE_FORMAT_EXTENSIBLE => {
|
winapi::WAVE_FORMAT_EXTENSIBLE => {
|
||||||
let format_ptr = format_ptr as *const winapi::WAVEFORMATEXTENSIBLE;
|
let format_ptr = format_ptr as *const winapi::WAVEFORMATEXTENSIBLE;
|
||||||
let channels = (*format_ptr).Format.nChannels as ChannelsCount;
|
let channels = (*format_ptr).Format.nChannels as ChannelCount;
|
||||||
let format = {
|
let format = {
|
||||||
fn cmp_guid(a: &winapi::GUID, b: &winapi::GUID) -> bool {
|
fn cmp_guid(a: &winapi::GUID, b: &winapi::GUID) -> bool {
|
||||||
a.Data1 == b.Data1 && a.Data2 == b.Data2 && a.Data3 == b.Data3 &&
|
a.Data1 == b.Data1 && a.Data2 == b.Data2 && a.Data3 == b.Data3 &&
|
||||||
|
@ -172,8 +172,8 @@ impl Endpoint {
|
||||||
|
|
||||||
SupportedFormat {
|
SupportedFormat {
|
||||||
channels: channels,
|
channels: channels,
|
||||||
min_samples_rate: SamplesRate((*format_ptr).nSamplesPerSec),
|
min_sample_rate: SampleRate((*format_ptr).nSamplesPerSec),
|
||||||
max_samples_rate: SamplesRate((*format_ptr).nSamplesPerSec),
|
max_sample_rate: SampleRate((*format_ptr).nSamplesPerSec),
|
||||||
data_type: data_type,
|
data_type: data_type,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -472,9 +472,9 @@ fn format_to_waveformatextensible(format: &Format)
|
||||||
SampleFormat::U16 => return Err(CreationError::FormatNotSupported),
|
SampleFormat::U16 => return Err(CreationError::FormatNotSupported),
|
||||||
},
|
},
|
||||||
nChannels: format.channels as winapi::WORD,
|
nChannels: format.channels as winapi::WORD,
|
||||||
nSamplesPerSec: format.samples_rate.0 as winapi::DWORD,
|
nSamplesPerSec: format.sample_rate.0 as winapi::DWORD,
|
||||||
nAvgBytesPerSec: format.channels as winapi::DWORD *
|
nAvgBytesPerSec: format.channels as winapi::DWORD *
|
||||||
format.samples_rate.0 as winapi::DWORD *
|
format.sample_rate.0 as winapi::DWORD *
|
||||||
format.data_type.sample_size() as winapi::DWORD,
|
format.data_type.sample_size() as winapi::DWORD,
|
||||||
nBlockAlign: format.channels as winapi::WORD *
|
nBlockAlign: format.channels as winapi::WORD *
|
||||||
format.data_type.sample_size() as winapi::WORD,
|
format.data_type.sample_size() as winapi::WORD,
|
||||||
|
|
Loading…
Reference in New Issue