Created
March 20, 2025 16:05
-
-
Save MBoui/5f65ae744531c87859ca5092981b3400 to your computer and use it in GitHub Desktop.
Cidre experiment
This file contains hidden or 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
#[cfg(target_os = "macos")] | |
mod macos { | |
use cidre::{arc, av, cat, cf, cm, core_audio as ca, ns}; | |
use cidre::{ | |
at::{ | |
self, au, | |
audio::component::{InitializedState, UninitializedState}, | |
}, | |
core_audio, os, vdsp, | |
}; | |
struct Ctx { | |
file: arc::R<av::AudioFile>, | |
format: arc::R<av::AudioFormat>, | |
output: Option<au::Output<InitializedState>>, | |
data: Vec<f32>, | |
} | |
impl Ctx { | |
fn start(&mut self, mut output: au::Output<UninitializedState>) -> os::Result<()> { | |
output.set_io_enabled(au::Scope::INPUT, 1, true)?; | |
output.set_io_enabled(au::Scope::OUTPUT, 0, false)?; | |
output.set_should_allocate_input_buf(false)?; | |
output.set_should_allocate_output_buf(false)?; | |
output.set_input_cb(Ctx::input_cb, self as *mut Self)?; | |
let output = output.allocate_resources().unwrap(); | |
let frames_per_slice = output.unit().max_frames_per_slice()? as usize; | |
self.data = vec![0f32; frames_per_slice * 2]; | |
self.output = Some(output); | |
let output = unsafe { self.output.as_mut().unwrap_unchecked() }; | |
output.start() | |
} | |
extern "C-unwind" fn input_cb( | |
ctx: *mut Ctx, | |
_io_action_flags: &mut au::RenderActionFlags, | |
_in_timestamp: &at::AudioTimeStamp, | |
_in_bus_num: u32, | |
in_number_frames: u32, | |
_io_data: *mut at::AudioBufList<1>, | |
) -> os::Status { | |
if ctx.is_null() { | |
return au::err::NO_CONNECTION.into(); | |
} | |
let ctx = unsafe { &mut *ctx }; | |
if ctx.output.is_none() { | |
return au::err::NO_CONNECTION.into(); | |
} | |
let output = unsafe { ctx.output.as_mut().unwrap_unchecked() }; | |
let mut buf_list = at::AudioBufList::<1>::new(); | |
buf_list.buffers[0] = at::AudioBuf { | |
number_channels: 2, | |
data_bytes_size: std::mem::size_of_val(&ctx.data[..]) as u32, | |
data: ctx.data.as_mut_ptr() as *mut _, | |
}; | |
if let Err(e) = output.render(in_number_frames, &mut buf_list, 1) { | |
return e.status(); | |
} | |
// let min_left = vdsp::min_stride_f32(&ctx.data[..in_number_frames as usize * 2], 2); | |
// let min_right = vdsp::min_stride_f32(&ctx.data[1..in_number_frames as usize * 2], 2); | |
// let max_left = vdsp::max_stride_f32(&ctx.data[..in_number_frames as usize * 2], 2); | |
// let max_right = vdsp::max_stride_f32(&ctx.data[1..in_number_frames as usize * 2], 2); | |
// println!("{min_left} {min_right} {max_left} {max_right}"); | |
let buf = | |
av::AudioPcmBuf::with_buf_list_no_copy(&ctx.format, &buf_list, None).unwrap(); | |
ctx.file.write(&buf).unwrap(); | |
os::Status::NO_ERR | |
} | |
} | |
#[tokio::main] | |
pub async fn main() { | |
let _input = core_audio::System::default_input_device().unwrap(); | |
let output = au::Output::new_apple_vp().unwrap(); | |
let input_device = output.input_device().unwrap(); | |
let asbd = output | |
.input_stream_format(0) | |
.expect("Failed to get output stream format"); | |
println!("format {:#?}", asbd); | |
let name = input_device.name().expect("Failed to device name property"); | |
println!("input device: {name}"); | |
let buf_list = input_device.input_stream_cfg().unwrap(); | |
println!("stream cfg: {:?}", buf_list); | |
let format = av::AudioFormat::with_asbd(&asbd).unwrap(); | |
// let format = av::AudioFormat::standard_with_sample_rate_and_channels(44100.0, 2).unwrap(); | |
println!("format.settings() cfg: {:?}", format.settings()); | |
let url = ns::Url::with_fs_path_string(ns::str!(c"/Users/killix/tmp/record_bb_3.wav"), false); | |
let file = av::AudioFile::open_write_common_format( | |
&url, | |
&format.settings(), | |
av::audio::CommonFormat::PcmF32, | |
format.is_interleaved(), | |
) | |
.unwrap(); | |
let mut ctx = Box::new(Ctx { | |
data: vec![], | |
output: None, | |
file, | |
format | |
}); | |
let _s = "_⎽⎼—⎻⎺‾"; | |
let s = "▁▂▃▄▅▆▇█"; | |
ctx.start(output).unwrap(); | |
println!("{s}"); | |
tokio::signal::ctrl_c().await.unwrap(); | |
} | |
} | |
#[cfg(target_os = "macos")] | |
pub use macos::main; | |
#[cfg(not(target_os = "macos"))] | |
fn main() { | |
todo!() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment