diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /third_party/rust/cubeb/examples/tone.rs | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/cubeb/examples/tone.rs')
-rw-r--r-- | third_party/rust/cubeb/examples/tone.rs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/third_party/rust/cubeb/examples/tone.rs b/third_party/rust/cubeb/examples/tone.rs new file mode 100644 index 0000000000..97e940bc94 --- /dev/null +++ b/third_party/rust/cubeb/examples/tone.rs @@ -0,0 +1,60 @@ +// Copyright © 2011 Mozilla Foundation +// +// This program is made available under an ISC-style license. See the +// accompanying file LICENSE for details. + +//! libcubeb api/function test. Plays a simple tone. +extern crate cubeb; + +mod common; + +use cubeb::{MonoFrame, Sample}; +use std::f32::consts::PI; +use std::thread; +use std::time::Duration; + +const SAMPLE_FREQUENCY: u32 = 48_000; +const STREAM_FORMAT: cubeb::SampleFormat = cubeb::SampleFormat::S16LE; + +type Frame = MonoFrame<i16>; + +fn main() { + let ctx = common::init("Cubeb tone example").expect("Failed to create cubeb context"); + + let params = cubeb::StreamParamsBuilder::new() + .format(STREAM_FORMAT) + .rate(SAMPLE_FREQUENCY) + .channels(1) + .layout(cubeb::ChannelLayout::MONO) + .take(); + + let mut position = 0u32; + + let mut builder = cubeb::StreamBuilder::<Frame>::new(); + builder + .name("Cubeb tone (mono)") + .default_output(¶ms) + .latency(0x1000) + .data_callback(move |_, output| { + // generate our test tone on the fly + for f in output.iter_mut() { + // North American dial tone + let t1 = (2.0 * PI * 350.0 * position as f32 / SAMPLE_FREQUENCY as f32).sin(); + let t2 = (2.0 * PI * 440.0 * position as f32 / SAMPLE_FREQUENCY as f32).sin(); + + f.m = i16::from_float(0.5 * (t1 + t2)); + + position += 1; + } + output.len() as isize + }) + .state_callback(|state| { + println!("stream {:?}", state); + }); + + let stream = builder.init(&ctx).expect("Failed to create cubeb stream"); + + stream.start().unwrap(); + thread::sleep(Duration::from_millis(500)); + stream.stop().unwrap(); +} |