Last active
September 27, 2015 15:10
-
-
Save yupferris/d1502d1f3f76938c3d48 to your computer and use it in GitHub Desktop.
coreaudio-rs memleak drop test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! 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 = Iter { value: 0.0 } | |
.map(|phase| (phase * PI * 2.0).sin() as f32 * 0.15); | |
// Construct an Output audio unit. | |
let drop_val = DropVal::new(String::from("derp")); | |
let audio_unit = AudioUnit::new(Type::Output, SubType::HalOutput) | |
.render_callback(Box::new(move |buffer, num_frames| { | |
let _ = drop_val; | |
for frame in (0..num_frames) { | |
let sample = samples.next().unwrap(); | |
for channel in buffer.iter_mut() { | |
channel[frame] = sample; | |
} | |
} | |
Ok(()) | |
})) | |
.start() | |
.unwrap(); | |
::std::thread::sleep_ms(3000); | |
audio_unit.close(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~/dev/projects/coreaudio-rs-test $ cargo run --release | |
Running `target/release/coreaudio-rs-test` | |
Creating derp | |
--- sound plays --- | |
Destroying derp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment