Looks better now

This commit is contained in:
2026-01-26 01:02:18 +01:00
parent 87fd59549d
commit 4ae8e28b2f
4 changed files with 66 additions and 45 deletions

View File

@@ -129,8 +129,7 @@ impl SequencerSnapshot {
pub struct SequencerHandle {
pub cmd_tx: Sender<SeqCommand>,
pub audio_tx: Sender<AudioCommand>,
pub audio_rx: Receiver<AudioCommand>,
pub audio_tx: Arc<ArcSwap<Sender<AudioCommand>>>,
shared_state: Arc<ArcSwap<SharedSequencerState>>,
thread: JoinHandle<()>,
}
@@ -146,6 +145,12 @@ impl SequencerHandle {
}
}
pub fn swap_audio_channel(&self) -> Receiver<AudioCommand> {
let (new_tx, new_rx) = bounded::<AudioCommand>(256);
self.audio_tx.store(Arc::new(new_tx));
new_rx
}
pub fn shutdown(self) {
let _ = self.cmd_tx.send(SeqCommand::Shutdown);
let _ = self.thread.join();
@@ -186,20 +191,21 @@ pub fn spawn_sequencer(
rng: Rng,
quantum: f64,
live_keys: Arc<LiveKeyState>,
) -> SequencerHandle {
) -> (SequencerHandle, Receiver<AudioCommand>) {
let (cmd_tx, cmd_rx) = bounded::<SeqCommand>(64);
let (audio_tx, audio_rx) = bounded::<AudioCommand>(256);
let audio_tx = Arc::new(ArcSwap::from_pointee(audio_tx));
let shared_state = Arc::new(ArcSwap::from_pointee(SharedSequencerState::default()));
let shared_state_clone = Arc::clone(&shared_state);
let audio_tx_clone = audio_tx.clone();
let audio_tx_for_thread = Arc::clone(&audio_tx);
let thread = thread::Builder::new()
.name("sequencer".into())
.spawn(move || {
sequencer_loop(
cmd_rx,
audio_tx_clone,
audio_tx_for_thread,
link,
playing,
variables,
@@ -212,13 +218,13 @@ pub fn spawn_sequencer(
})
.expect("Failed to spawn sequencer thread");
SequencerHandle {
let handle = SequencerHandle {
cmd_tx,
audio_tx,
audio_rx,
shared_state,
thread,
}
};
(handle, audio_rx)
}
struct PatternCache {
@@ -294,7 +300,7 @@ impl RunsCounter {
#[allow(clippy::too_many_arguments)]
fn sequencer_loop(
cmd_rx: Receiver<SeqCommand>,
audio_tx: Sender<AudioCommand>,
audio_tx: Arc<ArcSwap<Sender<AudioCommand>>>,
link: Arc<LinkState>,
playing: Arc<std::sync::atomic::AtomicBool>,
variables: Variables,
@@ -430,7 +436,7 @@ fn sequencer_loop(
std::mem::take(&mut trace),
);
for cmd in cmds {
match audio_tx.try_send(AudioCommand::Evaluate(cmd)) {
match audio_tx.load().try_send(AudioCommand::Evaluate(cmd)) {
Ok(()) => {
event_count += 1;
}
@@ -438,7 +444,9 @@ fn sequencer_loop(
dropped_events += 1;
}
Err(TrySendError::Disconnected(_)) => {
return;
// Channel disconnected means old stream is gone, but
// a new one will be swapped in. Don't exit - just skip.
dropped_events += 1;
}
}
}