#![cfg_attr(
not(bela_device),
allow(
dead_code,
reason = "only the fallback main is reachable off-device; the application code should still compile and lint"
)
)]
use std::process::ExitCode;
use bela::{
BelaApplication, BlockContext, CleanupContext, RenderContext, SetupContext, ThreadInfo,
rt_println,
};
#[derive(Default)]
struct Report {
blocks: u64,
}
impl BelaApplication for Report {
type RenderState = ();
fn setup(&mut self, context: &SetupContext) -> bool {
rt_println!(
"setup: {} Hz, {} frames per block, analog {} Hz, digital {} Hz",
context.audio_sample_rate(),
context.audio_frames(),
context.analog_sample_rate(),
context.digital_sample_rate()
);
true
}
fn create_render_state(&mut self, _thread: ThreadInfo, _context: &SetupContext) {}
fn render(&self, _state: &mut (), _context: &mut RenderContext) {}
fn render_post(&mut self, _states: &mut [()], _context: &mut BlockContext) {
self.blocks += 1;
}
fn cleanup(&mut self, _states: &mut [()], context: &CleanupContext) {
rt_println!(
"cleanup: {} blocks, {} frames elapsed, {} underruns",
self.blocks,
context.audio_frames_elapsed(),
context.underrun_count()
);
}
}
#[cfg(bela_device)]
fn main() -> ExitCode {
use core::num::NonZeroU32;
use std::env::args;
let Some(hz) = args().nth(1) else {
eprintln!("usage: sample_rate <hz>");
return ExitCode::FAILURE;
};
let Ok(hz) = hz.parse::<u32>().map(NonZeroU32::try_from) else {
eprintln!("sample_rate takes a rate in Hz, not {hz:?}");
return ExitCode::FAILURE;
};
let Ok(hz) = hz else {
eprintln!("the requested rate must not be 0");
return ExitCode::FAILURE;
};
let settings = bela::Settings::new().audio_sample_rate(hz);
match bela::Bela::run(Report::default(), &settings) {
Ok(()) => ExitCode::SUCCESS,
Err(error) => {
eprintln!("Error: {error}");
ExitCode::FAILURE
}
}
}
#[cfg(not(bela_device))]
fn main() -> ExitCode {
eprintln!("This example must be cross-compiled for Bela Gem (aarch64-unknown-linux-gnu).");
ExitCode::FAILURE
}