Cleanup and add tests for convert_channels
This commit is contained in:
parent
6e1d047320
commit
fb11478bf6
|
@ -39,9 +39,21 @@ pub fn convert_samples_rate<T>(input: &[T], from: ::SamplesRate, to: ::SamplesRa
|
|||
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`.
|
||||
pub fn convert_channels<T>(input: &[T], from: ::ChannelsCount, to: ::ChannelsCount) -> Vec<T>
|
||||
where T: Copy
|
||||
{
|
||||
assert!(from != 0);
|
||||
assert!(to != 0);
|
||||
assert!(input.len() % from as uint == 0);
|
||||
|
||||
let mut result = Vec::new();
|
||||
|
@ -53,18 +65,41 @@ pub fn convert_channels<T>(input: &[T], from: ::ChannelsCount, to: ::ChannelsCou
|
|||
}
|
||||
|
||||
// adding extra ones
|
||||
for i in range(0, ::std::cmp::max(0, to - from)) {
|
||||
result.push(element[i as uint % element.len()]);
|
||||
if to > from {
|
||||
for i in range(0, to - from) {
|
||||
result.push(element[i as uint % element.len()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
/*#[cfg(test)]
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
#[test]
|
||||
fn test_convert_channels() {
|
||||
use super::convert_channels;
|
||||
|
||||
#[test]
|
||||
fn remove_channels() {
|
||||
let result = convert_channels(&[1u16, 2, 3, 1, 2, 3], 3, 2);
|
||||
assert_eq!(result.as_slice(), [1, 2, 1, 2]);
|
||||
|
||||
let result = convert_channels(&[1u16, 2, 3, 4, 1, 2, 3, 4], 4, 1);
|
||||
assert_eq!(result.as_slice(), [1, 1]);
|
||||
}
|
||||
}*/
|
||||
|
||||
#[test]
|
||||
fn add_channels() {
|
||||
let result = convert_channels(&[1u16, 2, 1, 2], 2, 3);
|
||||
assert_eq!(result.as_slice(), [1, 2, 1, 1, 2, 1]);
|
||||
|
||||
let result = convert_channels(&[1u16, 2, 1, 2], 2, 4);
|
||||
assert_eq!(result.as_slice(), [1, 2, 1, 2, 1, 2, 1, 2]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_fail]
|
||||
fn convert_channels_wrong_data_len() {
|
||||
convert_channels(&[1u16, 2, 3], 2, 1);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue