summaryrefslogtreecommitdiffstats
path: root/third_party/rust/cubeb/examples
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
commit43a97878ce14b72f0981164f87f2e35e14151312 (patch)
tree620249daf56c0258faa40cbdcf9cfba06de2a846 /third_party/rust/cubeb/examples
parentInitial commit. (diff)
downloadfirefox-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')
-rw-r--r--third_party/rust/cubeb/examples/common/mod.rs32
-rw-r--r--third_party/rust/cubeb/examples/devices.rs130
-rw-r--r--third_party/rust/cubeb/examples/tone.rs60
3 files changed, 222 insertions, 0 deletions
diff --git a/third_party/rust/cubeb/examples/common/mod.rs b/third_party/rust/cubeb/examples/common/mod.rs
new file mode 100644
index 0000000000..a577a8fc06
--- /dev/null
+++ b/third_party/rust/cubeb/examples/common/mod.rs
@@ -0,0 +1,32 @@
+use cubeb::{Context, Result};
+use std::env;
+use std::ffi::CString;
+use std::io::{self, Write};
+
+pub fn init<T: Into<Vec<u8>>>(ctx_name: T) -> Result<Context> {
+ let backend = match env::var("CUBEB_BACKEND") {
+ Ok(s) => Some(s),
+ Err(_) => None,
+ };
+
+ let ctx_name = CString::new(ctx_name).unwrap();
+ let ctx = Context::init(Some(ctx_name.as_c_str()), None);
+ if let Ok(ref ctx) = ctx {
+ if let Some(ref backend) = backend {
+ let ctx_backend = ctx.backend_id();
+ if backend != ctx_backend {
+ let stderr = io::stderr();
+ let mut handle = stderr.lock();
+
+ writeln!(
+ handle,
+ "Requested backend `{}', got `{}'",
+ backend, ctx_backend
+ )
+ .unwrap();
+ }
+ }
+ }
+
+ ctx
+}
diff --git a/third_party/rust/cubeb/examples/devices.rs b/third_party/rust/cubeb/examples/devices.rs
new file mode 100644
index 0000000000..0f85762944
--- /dev/null
+++ b/third_party/rust/cubeb/examples/devices.rs
@@ -0,0 +1,130 @@
+// Copyright © 2011 Mozilla Foundation
+// Copyright © 2015 Haakon Sporsheim <haakon.sporsheim@telenordigital.com>
+//
+// This program is made available under an ISC-style license. See the
+// accompanying file LICENSE for details.
+
+//! libcubeb enumerate device test/example.
+//! Prints out a list of input/output devices connected to the system.
+extern crate cubeb;
+
+mod common;
+
+use cubeb::{DeviceFormat, DeviceType};
+
+fn print_device_info(info: &cubeb::DeviceInfo) {
+ let devtype = if info.device_type().contains(DeviceType::INPUT) {
+ "input"
+ } else if info.device_type().contains(DeviceType::OUTPUT) {
+ "output"
+ } else {
+ "unknown?"
+ };
+
+ let devstate = match info.state() {
+ cubeb::DeviceState::Disabled => "disabled",
+ cubeb::DeviceState::Unplugged => "unplugged",
+ cubeb::DeviceState::Enabled => "enabled",
+ };
+
+ let devdeffmt = match info.default_format() {
+ DeviceFormat::S16LE => "S16LE",
+ DeviceFormat::S16BE => "S16BE",
+ DeviceFormat::F32LE => "F32LE",
+ DeviceFormat::F32BE => "F32BE",
+ _ => "unknown?",
+ };
+
+ let mut devfmts = "".to_string();
+ if info.format().contains(DeviceFormat::S16LE) {
+ devfmts = format!("{} S16LE", devfmts);
+ }
+ if info.format().contains(DeviceFormat::S16BE) {
+ devfmts = format!("{} S16BE", devfmts);
+ }
+ if info.format().contains(DeviceFormat::F32LE) {
+ devfmts = format!("{} F32LE", devfmts);
+ }
+ if info.format().contains(DeviceFormat::F32BE) {
+ devfmts = format!("{} F32BE", devfmts);
+ }
+
+ if let Some(device_id) = info.device_id() {
+ let preferred = if info.preferred().is_empty() {
+ ""
+ } else {
+ " (PREFERRED)"
+ };
+ println!("dev: \"{}\"{}", device_id, preferred);
+ }
+ if let Some(friendly_name) = info.friendly_name() {
+ println!("\tName: \"{}\"", friendly_name);
+ }
+ if let Some(group_id) = info.group_id() {
+ println!("\tGroup: \"{}\"", group_id);
+ }
+ if let Some(vendor_name) = info.vendor_name() {
+ println!("\tVendor: \"{}\"", vendor_name);
+ }
+ println!("\tType: {}", devtype);
+ println!("\tState: {}", devstate);
+ println!("\tCh: {}", info.max_channels());
+ println!(
+ "\tFormat: {} (0x{:x}) (default: {})",
+ &devfmts[1..],
+ info.format(),
+ devdeffmt
+ );
+ println!(
+ "\tRate: {} - {} (default: {})",
+ info.min_rate(),
+ info.max_rate(),
+ info.default_rate()
+ );
+ println!(
+ "\tLatency: lo {} frames, hi {} frames",
+ info.latency_lo(),
+ info.latency_hi()
+ );
+}
+
+fn main() {
+ let ctx = common::init("Cubeb audio test").expect("Failed to create cubeb context");
+
+ println!("Enumerating input devices for backend {}", ctx.backend_id());
+
+ let devices = match ctx.enumerate_devices(DeviceType::INPUT) {
+ Ok(devices) => devices,
+ Err(e) if e.code() == cubeb::ErrorCode::NotSupported => {
+ println!("Device enumeration not support for this backend.");
+ return;
+ }
+ Err(e) => {
+ println!("Error enumerating devices: {}", e);
+ return;
+ }
+ };
+
+ println!("Found {} input devices", devices.len());
+ for d in devices.iter() {
+ print_device_info(d);
+ }
+
+ println!(
+ "Enumerating output devices for backend {}",
+ ctx.backend_id()
+ );
+
+ let devices = match ctx.enumerate_devices(DeviceType::OUTPUT) {
+ Ok(devices) => devices,
+ Err(e) => {
+ println!("Error enumerating devices: {}", e);
+ return;
+ }
+ };
+
+ println!("Found {} output devices", devices.len());
+ for d in devices.iter() {
+ print_device_info(d);
+ }
+}
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(&params)
+ .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();
+}