Improved error handling
This commit is contained in:
60
src/main.rs
60
src/main.rs
@@ -2,6 +2,7 @@ mod allocator;
|
||||
mod audio_chunk;
|
||||
mod chunk_factory;
|
||||
mod looper_error;
|
||||
mod notification_handler;
|
||||
mod process_handler;
|
||||
mod track;
|
||||
|
||||
@@ -12,38 +13,61 @@ use audio_chunk::AudioChunk;
|
||||
use chunk_factory::ChunkFactory;
|
||||
use looper_error::LooperError;
|
||||
use looper_error::Result;
|
||||
use notification_handler::JackNotification;
|
||||
use notification_handler::NotificationHandler;
|
||||
use process_handler::ProcessHandler;
|
||||
use track::Track;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let (allocator, factory_handle) = Allocator::spawn(5000, 100);
|
||||
simple_logger::SimpleLogger::new().init().expect("Could not initialize logger");
|
||||
|
||||
let (jack_client, audio_in, audio_out) = setup_jack();
|
||||
|
||||
let (allocator, factory_handle) = Allocator::spawn(jack_client.buffer_size() as usize * 10, 100);
|
||||
|
||||
let process_handler = ProcessHandler::new(audio_in, audio_out, allocator)
|
||||
.expect("Could not create process handler");
|
||||
|
||||
let notification_handler = NotificationHandler::new();
|
||||
let mut notification_channel = notification_handler.subscribe();
|
||||
|
||||
let _async_client = jack_client
|
||||
.activate_async(notification_handler, process_handler)
|
||||
.expect("Could not activate Jack");
|
||||
|
||||
tokio::select! {
|
||||
notification = notification_channel.recv() => {
|
||||
match notification {
|
||||
Ok(JackNotification::Shutdown {reason, status}) => {
|
||||
log::error!("Jack shutdown: {reason} {status:?}");
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("NotificationHandler stopped: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = factory_handle => {
|
||||
log::error!("Allocator was dropped");
|
||||
}
|
||||
_ = tokio::signal::ctrl_c() => {
|
||||
log::info!("Stopping");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn setup_jack() -> (jack::Client, jack::Port<jack::AudioIn>, jack::Port<jack::AudioOut>) {
|
||||
let (jack_client, jack_status) =
|
||||
jack::Client::new("looper", jack::ClientOptions::NO_START_SERVER)
|
||||
.expect("Could not create Jack client");
|
||||
if !jack_status.is_empty() {
|
||||
println!("Could not start jack client: {jack_status:?}");
|
||||
return;
|
||||
panic!("Could not start jack client: {jack_status:?}");
|
||||
}
|
||||
|
||||
let audio_in = jack_client
|
||||
.register_port("audio_in", jack::AudioIn::default())
|
||||
.expect("Could not create audio_in port");
|
||||
let audio_out = jack_client
|
||||
.register_port("audio_out", jack::AudioOut::default())
|
||||
.expect("Could not create audio_out port");
|
||||
|
||||
let process_handler = ProcessHandler::new(audio_in, audio_out, allocator)
|
||||
.expect("Could not create process handler");
|
||||
|
||||
let async_client = jack_client
|
||||
.activate_async((), process_handler)
|
||||
.expect("Could not activate Jack");
|
||||
|
||||
factory_handle.await.unwrap();
|
||||
|
||||
async_client
|
||||
.deactivate()
|
||||
.expect("Could not deactivate Jack");
|
||||
}
|
||||
(jack_client, audio_in, audio_out)
|
||||
}
|
||||
31
src/notification_handler.rs
Normal file
31
src/notification_handler.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
pub struct NotificationHandler {
|
||||
channel: tokio::sync::broadcast::Sender<JackNotification>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum JackNotification {
|
||||
Shutdown{
|
||||
status: jack::ClientStatus,
|
||||
reason: String
|
||||
},
|
||||
}
|
||||
|
||||
impl NotificationHandler {
|
||||
pub fn new() -> Self {
|
||||
let (channel, _) = tokio::sync::broadcast::channel(1);
|
||||
Self {
|
||||
channel,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn subscribe(&self) -> tokio::sync::broadcast::Receiver<JackNotification> {
|
||||
self.channel.subscribe()
|
||||
}
|
||||
}
|
||||
|
||||
impl jack::NotificationHandler for NotificationHandler {
|
||||
unsafe fn shutdown(&mut self, status: jack::ClientStatus, reason: &str) {
|
||||
let reason = reason.to_string();
|
||||
self.channel.send(JackNotification::Shutdown { status, reason }).expect("Could not send shutdown notification");
|
||||
}
|
||||
}
|
||||
@@ -41,11 +41,11 @@ impl<F: ChunkFactory> jack::ProcessHandler for ProcessHandler<F> {
|
||||
let sample_count_to_append =
|
||||
sample_count_to_append.min(recording_samples - self.track.len());
|
||||
let samples_to_append = &input_buffer[index..index + sample_count_to_append];
|
||||
if self
|
||||
if let Err(e) = self
|
||||
.track
|
||||
.append_samples(samples_to_append, &mut self.chunk_factory)
|
||||
.is_err()
|
||||
{
|
||||
log::error!("Could not append samples: {e}");
|
||||
return jack::Control::Quit;
|
||||
};
|
||||
output_buffer[index..(index + sample_count_to_append)].fill(0.0);
|
||||
@@ -55,21 +55,22 @@ impl<F: ChunkFactory> jack::ProcessHandler for ProcessHandler<F> {
|
||||
let sample_count_to_play = jack_buffer_size - index;
|
||||
let sample_count_to_play =
|
||||
sample_count_to_play.min(self.track.len() - self.playback_position);
|
||||
if self
|
||||
if let Err(e) = self
|
||||
.track
|
||||
.copy_samples(
|
||||
&mut output_buffer[index..(index + sample_count_to_play)],
|
||||
self.playback_position,
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
log::error!("Could not copy samples: {e}");
|
||||
return jack::Control::Quit;
|
||||
}
|
||||
index += sample_count_to_play;
|
||||
self.playback_position += sample_count_to_play;
|
||||
if self.playback_position >= self.track.len() {
|
||||
self.playback_position = 0;
|
||||
if self.track.clear(&mut self.chunk_factory).is_err() {
|
||||
if let Err(e) = self.track.clear(&mut self.chunk_factory) {
|
||||
log::error!("Could not clear track: {e}");
|
||||
return jack::Control::Quit;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user