Implement some samples rate conversions
This commit is contained in:
parent
32bca93cc9
commit
081912c5fb
|
@ -20,11 +20,9 @@ fn main() {
|
||||||
loop {
|
loop {
|
||||||
let mut buffer = channel.append_data(1, cpal::SamplesRate(44100));
|
let mut buffer = channel.append_data(1, cpal::SamplesRate(44100));
|
||||||
|
|
||||||
for sample in buffer.chunks_mut(1) {
|
for sample in buffer.iter_mut() {
|
||||||
let value = data_source.next().unwrap();
|
let value = data_source.next().unwrap();
|
||||||
|
*sample = value;
|
||||||
sample[0] = value;
|
|
||||||
//sample[1] = value;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,35 @@ This includes conversion between samples formats, channels or sample rates.
|
||||||
pub fn convert_samples_rate<T>(input: &[T], from: ::SamplesRate, to: ::SamplesRate) -> Vec<T>
|
pub fn convert_samples_rate<T>(input: &[T], from: ::SamplesRate, to: ::SamplesRate) -> Vec<T>
|
||||||
where T: Copy
|
where T: Copy
|
||||||
{
|
{
|
||||||
|
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();
|
||||||
|
for element in input.chunks((from / to) as uint) {
|
||||||
|
result.push(element[0]);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if `to` is a multiple of `from` (for example `to` is 44100 and `from` is 22050)
|
||||||
|
// TODO: dumb algorithm
|
||||||
|
// FIXME: doesn't take channels into account
|
||||||
|
if to % from == 0 {
|
||||||
|
let mut result = Vec::new();
|
||||||
|
for element in input.windows(2) {
|
||||||
|
for _ in range(0, (to / from) as uint) {
|
||||||
|
result.push(element[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _ in range(0, (to / from) as uint) {
|
||||||
|
result.push(*input.last().unwrap());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,10 +61,10 @@ pub fn convert_channels<T>(input: &[T], from: ::ChannelsCount, to: ::ChannelsCou
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
/*#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_convert_channels() {
|
fn test_convert_channels() {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
|
|
|
@ -20,7 +20,7 @@ pub type ChannelsCount = u16;
|
||||||
|
|
||||||
///
|
///
|
||||||
#[deriving(Show, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
#[deriving(Show, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub struct SamplesRate(pub u16);
|
pub struct SamplesRate(pub u32);
|
||||||
|
|
||||||
/// Represents a buffer that must be filled with audio data.
|
/// Represents a buffer that must be filled with audio data.
|
||||||
///
|
///
|
||||||
|
|
|
@ -33,7 +33,7 @@ impl Channel {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_samples_rate(&self) -> ::SamplesRate {
|
pub fn get_samples_rate(&self) -> ::SamplesRate {
|
||||||
::SamplesRate(self.samples_per_second as u16)
|
::SamplesRate(self.samples_per_second as u32)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_samples_format(&self) -> ::SampleFormat {
|
pub fn get_samples_format(&self) -> ::SampleFormat {
|
||||||
|
|
Loading…
Reference in New Issue