cpal/src/conversions.rs

137 lines
4.0 KiB
Rust
Raw Normal View History

2014-12-15 10:45:38 +00:00
/*!
This module contains function that will convert from one PCM format to another.
This includes conversion between samples formats, channels or sample rates.
*/
use samples_formats::Sample;
/// Converts between samples rates while preserving the pitch.
pub fn convert_samples_rate<T>(input: &[T], from: ::SamplesRate, to: ::SamplesRate,
channels: ::ChannelsCount) -> Vec<T>
where T: Sample
2014-12-15 10:45:38 +00:00
{
let from = from.0;
let to = to.0;
// if `from` is a multiple of `to` (for example `from` is 44100 and `to` is 22050),
// then we simply skip some samples
if from % to == 0 {
let mut result = Vec::new();
2015-01-09 20:25:51 +00:00
for element in input.chunks(channels as usize * (from / to) as usize) {
2015-03-25 10:21:10 +00:00
for i in (0 .. channels) {
2015-01-09 20:25:51 +00:00
result.push(element[i as usize]);
}
}
return result;
}
// if `to` is twice `from` (for example `to` is 44100 and `from` is 22050)
// TODO: more generic
if to == from * 2 {
let mut result = Vec::new();
let mut previous: Option<Vec<T>> = None;
2015-01-09 20:25:51 +00:00
for element in input.chunks(channels as usize) {
if let Some(previous) = previous.take() {
for (prev, curr) in previous.into_iter().zip(element.iter()) {
result.push(prev.interpolate(*curr));
}
for curr in element.iter() {
result.push(*curr);
}
} else {
for e in element.iter() {
result.push(*e);
}
}
previous = Some(element.to_vec());
}
return result;
}
2014-12-15 10:45:38 +00:00
unimplemented!()
}
/// Converts between a certain number of channels.
///
/// If the target number is inferior to the source number, additional channels are removed.
///
/// If the target number is superior to the source number, the value of channel `N` is equal
/// to the value of channel `N % source_channels`.
///
/// ## Panic
///
/// Panics if `from` is 0, `to` is 0, or if the data length is not a multiple of `from`.
2014-12-15 10:45:38 +00:00
pub fn convert_channels<T>(input: &[T], from: ::ChannelsCount, to: ::ChannelsCount) -> Vec<T>
where T: Sample
2014-12-15 10:45:38 +00:00
{
assert!(from != 0);
assert!(to != 0);
2015-01-09 20:25:51 +00:00
assert!(input.len() % from as usize == 0);
2014-12-15 10:45:38 +00:00
let mut result = Vec::new();
2015-01-09 20:25:51 +00:00
for element in input.chunks(from as usize) {
2014-12-15 10:45:38 +00:00
// copying the common channels
2015-03-25 10:21:10 +00:00
for i in (0 .. ::std::cmp::min(from, to)) {
2015-01-09 20:25:51 +00:00
result.push(element[i as usize]);
2014-12-15 10:45:38 +00:00
}
// adding extra ones
if to > from {
2015-03-25 10:21:10 +00:00
for i in (0 .. to - from) {
2015-01-09 20:25:51 +00:00
result.push(element[i as usize % element.len()]);
}
2014-12-15 10:45:38 +00:00
}
}
result
}
#[cfg(test)]
2014-12-15 10:45:38 +00:00
mod test {
use super::convert_channels;
use super::convert_samples_rate;
#[test]
fn remove_channels() {
let result = convert_channels(&[1u16, 2, 3, 1, 2, 3], 3, 2);
2015-04-04 07:06:46 +00:00
assert_eq!(result, [1, 2, 1, 2]);
let result = convert_channels(&[1u16, 2, 3, 4, 1, 2, 3, 4], 4, 1);
2015-04-04 07:06:46 +00:00
assert_eq!(result, [1, 1]);
}
2014-12-15 10:45:38 +00:00
#[test]
fn add_channels() {
let result = convert_channels(&[1u16, 2, 1, 2], 2, 3);
2015-04-04 07:06:46 +00:00
assert_eq!(result, [1, 2, 1, 1, 2, 1]);
2014-12-15 10:45:38 +00:00
let result = convert_channels(&[1u16, 2, 1, 2], 2, 4);
2015-04-04 07:06:46 +00:00
assert_eq!(result, [1, 2, 1, 2, 1, 2, 1, 2]);
2014-12-15 10:45:38 +00:00
}
#[test]
2015-03-25 10:21:10 +00:00
#[should_panic]
fn convert_channels_wrong_data_len() {
convert_channels(&[1u16, 2, 3], 2, 1);
}
#[test]
fn half_samples_rate() {
let result = convert_samples_rate(&[1u16, 16, 2, 17, 3, 18, 4, 19],
::SamplesRate(44100), ::SamplesRate(22050), 2);
2015-04-04 07:06:46 +00:00
assert_eq!(result, [1, 16, 3, 18]);
}
#[test]
fn double_samples_rate() {
let result = convert_samples_rate(&[2u16, 16, 4, 18, 6, 20, 8, 22],
::SamplesRate(22050), ::SamplesRate(44100), 2);
2015-04-04 07:06:46 +00:00
assert_eq!(result, [2, 16, 3, 17, 4, 18, 5, 19, 6, 20, 7, 21, 8, 22]);
}
}