| 1 |
// Copyright © 2011 Mozilla Foundation |
| 2 |
// |
| 3 |
// This program is made available under an ISC-style license. See the |
| 4 |
// accompanying file LICENSE for details. |
| 5 |
|
| 6 |
//! libcubeb api/function test. Plays a simple tone. |
| 7 |
extern crate cubeb; |
| 8 |
|
| 9 |
mod common; |
| 10 |
|
| 11 |
use cubeb::{MonoFrame, Sample}; |
| 12 |
use std::f32::consts::PI; |
| 13 |
use std::thread; |
| 14 |
use std::time::Duration; |
| 15 |
|
| 16 |
const SAMPLE_FREQUENCY: u32 = 48_000; |
| 17 |
const STREAM_FORMAT: cubeb::SampleFormat = cubeb::SampleFormat::S16LE; |
| 18 |
|
| 19 |
type Frame = MonoFrame<i16>; |
| 20 |
|
| 21 |
fn main() { |
| 22 |
let ctx = common::init("Cubeb tone example").expect("Failed to create cubeb context"); |
| 23 |
|
| 24 |
let params = cubeb::StreamParamsBuilder::new() |
| 25 |
.format(STREAM_FORMAT) |
| 26 |
.rate(SAMPLE_FREQUENCY) |
| 27 |
.channels(1) |
| 28 |
.layout(cubeb::ChannelLayout::MONO) |
| 29 |
.take(); |
| 30 |
|
| 31 |
let mut position = 0u32; |
| 32 |
|
| 33 |
let mut builder = cubeb::StreamBuilder::<Frame>::new(); |
| 34 |
builder |
| 35 |
.name("Cubeb tone (mono)") |
| 36 |
.default_output(¶ms) |
| 37 |
.latency(0x1000) |
| 38 |
.data_callback(move |_, output| { |
| 39 |
// generate our test tone on the fly |
| 40 |
for f in output.iter_mut() { |
| 41 |
// North American dial tone |
| 42 |
let t1 = (2.0 * PI * 350.0 * position as f32 / SAMPLE_FREQUENCY as f32).sin(); |
| 43 |
let t2 = (2.0 * PI * 440.0 * position as f32 / SAMPLE_FREQUENCY as f32).sin(); |
| 44 |
|
| 45 |
f.m = i16::from_float(0.5 * (t1 + t2)); |
| 46 |
|
| 47 |
position += 1; |
| 48 |
} |
| 49 |
output.len() as isize |
| 50 |
}) |
| 51 |
.state_callback(|state| { |
| 52 |
println!("stream {:?}", state); |
| 53 |
}); |
| 54 |
|
| 55 |
let stream = builder.init(&ctx).expect("Failed to create cubeb stream"); |
| 56 |
|
| 57 |
stream.start().unwrap(); |
| 58 |
thread::sleep(Duration::from_millis(500)); |
| 59 |
stream.stop().unwrap(); |
| 60 |
} |