Skip to content

Instantly share code, notes, and snippets.

@yupferris
Last active September 27, 2015 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yupferris/036c798c15411695faff to your computer and use it in GitHub Desktop.
Save yupferris/036c798c15411695faff to your computer and use it in GitHub Desktop.
coreaudio-rs memleak drop test 2
//! A basic output stream example, using an Output AudioUnit to generate a sine wave.
extern crate coreaudio_rs as coreaudio;
extern crate num;
use coreaudio::audio_unit::{AudioUnit, Type, SubType};
use num::Float;
use std::f64::consts::PI;
// NOTE: temporary replacement for unstable `std::iter::iterate`
struct Iter {
value: f64,
}
impl Iterator for Iter {
type Item = f64;
fn next(&mut self) -> Option<f64> {
self.value += 440.0 / 44_100.0;
Some(self.value)
}
}
struct DropVal {
name: String
}
impl DropVal {
fn new(name: String) -> DropVal {
println!("Creating {}", name);
DropVal { name: name }
}
}
impl Drop for DropVal {
fn drop(&mut self) {
println!("Destroying {}", self.name);
}
}
fn main() {
// 440hz sine wave generator.
let mut samples_a = Iter { value: 0.0 }
.map(|phase| (phase * PI * 2.0).sin() as f32 * 0.15);
let drop_val_a = DropVal::new(String::from("a"));
// Second data set to ensure dropping happens correctly
let mut samples_b = Iter { value: 0.0 }
.map(|phase| (phase * PI * 2.0).sin() as f32 * 0.15);
let drop_val_b = DropVal::new(String::from("b"));
// Construct an Output audio unit.
let audio_unit = AudioUnit::new(Type::Output, SubType::HalOutput)
.render_callback(Box::new(move |buffer, num_frames| {
let _ = drop_val_a;
for frame in (0..num_frames) {
let sample = samples_a.next().unwrap();
for channel in buffer.iter_mut() {
channel[frame] = sample;
}
}
Ok(())
}))
.render_callback(Box::new(move |buffer, num_frames| {
let _ = drop_val_b;
for frame in (0..num_frames) {
let sample = samples_b.next().unwrap();
for channel in buffer.iter_mut() {
channel[frame] = sample;
}
}
Ok(())
}))
.start()
.unwrap();
::std::thread::sleep_ms(3000);
audio_unit.close();
}
~/dev/projects/coreaudio-rs-test $ cargo run --release
Compiling coreaudio-rs-test v0.1.0 (file:///Users/yupferris/dev/projects/coreaudio-rs-test)
Running `target/release/coreaudio-rs-test`
Creating a
Creating b
Destroying a
--- sound plays ---
Destroying b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment