nandi/jolt-nativepublic Fork 0
cfd3e3677bed92e80cfe447469a249cfdc0b1401
Commits
Clone
git clone https://git.rickub.com/nandi/jolt-native.git
git clone ssh://git@rickub.com/nandi/jolt-native.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

lib.rs · 82 lines · 2.9 KBRust Blame HistoryRaw
Lift freeq's AV media plane out of sleek 90f8b89 nandi 19d ago1#![allow(dead_code)]
2
3extern crate anyhow;
4extern crate cpal;
5
6use cpal::{
7 traits::{DeviceTrait, HostTrait, StreamTrait},
8 SizedSample, I24,
9};
10use cpal::{FromSample, Sample};
11
12#[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "full"))]
13fn main() {
14 let host = cpal::default_host();
15
16 let device = host
17 .default_output_device()
18 .expect("failed to find output device");
19
20 let config = device.default_output_config().unwrap();
21
22 match config.sample_format() {
23 cpal::SampleFormat::I8 => run::<i8>(&device, config.into()).unwrap(),
24 cpal::SampleFormat::I16 => run::<i16>(&device, config.into()).unwrap(),
25 cpal::SampleFormat::I24 => run::<I24>(&device, config.into()).unwrap(),
26 cpal::SampleFormat::I32 => run::<i32>(&device, config.into()).unwrap(),
27 // cpal::SampleFormat::I48 => run::<I48>(&device, config.into()).unwrap(),
28 cpal::SampleFormat::I64 => run::<i64>(&device, config.into()).unwrap(),
29 cpal::SampleFormat::U8 => run::<u8>(&device, config.into()).unwrap(),
30 cpal::SampleFormat::U16 => run::<u16>(&device, config.into()).unwrap(),
31 // cpal::SampleFormat::U24 => run::<U24>(&device, config.into()).unwrap(),
32 cpal::SampleFormat::U32 => run::<u32>(&device, config.into()).unwrap(),
33 // cpal::SampleFormat::U48 => run::<U48>(&device, config.into()).unwrap(),
34 cpal::SampleFormat::U64 => run::<u64>(&device, config.into()).unwrap(),
35 cpal::SampleFormat::F32 => run::<f32>(&device, config.into()).unwrap(),
36 cpal::SampleFormat::F64 => run::<f64>(&device, config.into()).unwrap(),
37 sample_format => panic!("Unsupported sample format '{sample_format}'"),
38 }
39}
40
41fn run<T>(device: &cpal::Device, config: cpal::StreamConfig) -> Result<(), anyhow::Error>
42where
43 T: SizedSample + FromSample<f32>,
44{
45 let sample_rate = config.sample_rate as f32;
46 let channels = config.channels as usize;
47
48 // Produce a sinusoid of maximum amplitude.
49 let mut sample_clock = 0f32;
50 let mut next_value = move || {
51 sample_clock = (sample_clock + 1.0) % sample_rate;
52 (sample_clock * 440.0 * 2.0 * std::f32::consts::PI / sample_rate).sin()
53 };
54
55 let err_fn = |err| eprintln!("an error occurred on stream: {}", err);
56
57 let stream = device.build_output_stream(
58 config,
59 move |data: &mut [T], _: &cpal::OutputCallbackInfo| {
60 write_data(data, channels, &mut next_value)
61 },
62 err_fn,
63 None,
64 )?;
65 stream.play()?;
66
67 std::thread::sleep(std::time::Duration::from_millis(1000));
68
69 Ok(())
70}
71
72fn write_data<T>(output: &mut [T], channels: usize, next_sample: &mut dyn FnMut() -> f32)
73where
74 T: Sample + FromSample<f32>,
75{
76 for frame in output.chunks_mut(channels) {
77 let value: T = T::from_sample(next_sample());
78 for sample in frame.iter_mut() {
79 *sample = value;
80 }
81 }
82}