Browse Source

Add sample-rate setter for `OutStream`

master
Andreas Linz 5 years ago
parent
commit
84a4d17a13
2 changed files with 12 additions and 2 deletions
  1. +8
    -0
      src/stream.rs
  2. +4
    -2
      tests/stream.rs

+ 8
- 0
src/stream.rs View File

@ -378,6 +378,14 @@ impl<'a> OutStream<'a> {
unsafe { (*self.stream).sample_rate as u32 }
}
/// Sets the stream sample rate.
/// Make sure that the device supports the given sample rate to avoid
/// sample rate conversions. A `Device` provides `supports_sample_rate` and
/// `nearest_sample_rate` methods for this purpose.
pub fn set_sample_rate(&mut self, sample_rate: u32) {
unsafe { (*self.stream).sample_rate = sample_rate as c_int }
}
/// Returns the underlying device of the output stream.
pub fn device(&self) -> Device {
let dev = Device::new(unsafe { (*self.stream).device });

+ 4
- 2
tests/stream.rs View File

@ -20,13 +20,15 @@ fn test_outstream() {
let dev = sio.output_device(dev_idx).unwrap();
let mut stream = dev.create_outstream().unwrap();
stream.open().unwrap();
assert!(stream.sample_rate() > 0);
let sample_rate: u32 = if dev.supports_sample_rate(96_000) { 96_000 } else { 48_000 };
stream.set_sample_rate(sample_rate);
assert_eq!(stream.sample_rate(), sample_rate);
let layout = stream.layout();
assert_eq!(layout.channel_count(), 2);
let cb = move |out: rsoundio::OutStream, min_frame_count: u32, max_frame_count: u32| {
let l: Vec<f32> = (0..max_frame_count as usize)
.map(|i| {
(i as f32 * ((2.0 * ::std::f32::consts::PI * 440.0) / 48_000f32))
(i as f32 * ((2.0 * ::std::f32::consts::PI * 440.0) / sample_rate as f32))
.sin()
})
.collect();

Loading…
Cancel
Save