Skip to content

Commit

Permalink
working pattern recall
Browse files Browse the repository at this point in the history
  • Loading branch information
magnetophon committed Nov 15, 2024
1 parent 6136696 commit b04c0f7
Showing 1 changed file with 51 additions and 78 deletions.
129 changes: 51 additions & 78 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,14 +601,8 @@ impl Plugin for Del2 {
&mut self,
_audio_io_layout: &AudioIOLayout,
buffer_config: &BufferConfig,
context: &mut impl InitContext<Self>,
_context: &mut impl InitContext<Self>,
) -> bool {
println!(
"initialize taps: {}",
self.params
.current_tap
.load(std::sync::atomic::Ordering::SeqCst)
);
// Set the sample rate from the buffer configuration
self.sample_rate = buffer_config.sample_rate;

Expand All @@ -620,32 +614,29 @@ impl Plugin for Del2 {
// Calculate and set the delay buffer size
self.set_delay_buffer_size(buffer_config);

for tap_index in 0..self
.params
.current_tap
.load(std::sync::atomic::Ordering::SeqCst)
{
self.load_and_configure_tap(self.sample_rate, 0, 0, None, tap_index);
}
// Initialize filter parameters for each tap
self.initialize_filter_parameters();

true
}

fn reset(&mut self) {
println!(
"reset taps: {}",
self.params
.current_tap
.load(std::sync::atomic::Ordering::SeqCst)
);
for tap in 0..NUM_TAPS {
self.ladders[tap].s = [f32x4::splat(0.); 4];
self.amp_envelopes[tap].reset(0.0);
}
self.delay_taps.fill(None);

for tap_index in 0..self
.params
.current_tap
.load(std::sync::atomic::Ordering::SeqCst)
{
self.load_and_configure_tap(self.sample_rate, 0, None, tap_index);
}

// Indicate filter update needed
self.should_update_filter.store(true, Ordering::Release);
// Reset buffers and envelopes here. This can be called from the audio thread and may not
// allocate. You can remove this function if you do not need it.
}
Expand All @@ -667,11 +658,6 @@ impl Plugin for Del2 {
let mut block_start: usize = 0;
let mut block_end: usize = MAX_BLOCK_SIZE.min(num_samples);

// for now, we assume one note or less per block
let mut live_timing = 0;
let mut live_channel = 0;
let mut live_voice_id = None;

// write the audio buffer into the delay
self.write_into_delay(buffer);

Expand Down Expand Up @@ -703,9 +689,25 @@ impl Plugin for Del2 {
velocity,
} => {
self.store_note_on_in_delay_data(timing, note, velocity);
let live_timing = timing;
let live_channel = channel;
let live_voice_id = voice_id;
if self
.params
.current_tap
.load(std::sync::atomic::Ordering::SeqCst)
> old_nr_taps
{
let tap_index = self
.params
.current_tap
.load(std::sync::atomic::Ordering::SeqCst)
- 1;
self.load_and_configure_tap(
sample_rate,
// timing,
channel,
voice_id,
tap_index,
);
}
}
NoteEvent::NoteOff {
timing: _,
Expand All @@ -715,15 +717,6 @@ impl Plugin for Del2 {
velocity: _,
} => {
self.store_note_off_in_delay_data(note);
// if we are in instrument mode
// if !self.params.global.mute_is_toggle.value() {
// self.start_mute_for_delay_tap(
// sample_rate,
// voice_id,
// channel,
// note,
// )
// }
}
NoteEvent::Choke {
timing,
Expand Down Expand Up @@ -843,26 +836,6 @@ impl Plugin for Del2 {
}
}
}

if self
.params
.current_tap
.load(std::sync::atomic::Ordering::SeqCst)
> old_nr_taps
{
let tap_index = self
.params
.current_tap
.load(std::sync::atomic::Ordering::SeqCst)
- 1;
self.load_and_configure_tap(
sample_rate,
live_timing,
live_channel,
live_voice_id,
tap_index,
);
}
self.prepare_for_delay(block_end - block_start);

if self
Expand Down Expand Up @@ -1260,25 +1233,18 @@ impl Del2 {
&mut self,
sample_rate: f32,
// context: &mut (impl ProcessContext<Self> + nih_plug::prelude::ProcessContext<Self>),
timing: u32,
// timing: u32,
channel: u8,
voice_id: Option<i32>,
tap_index: usize,
) {
// Determine tap index
println!("loading {tap_index} + 1 taps");
println!(
"voice id: {:#?}, channel: {channel}, timing: {timing}",
voice_id
);

// Load atomic values corresponding to the tap index
let delay_time = self.params.delay_times[tap_index].load(Ordering::SeqCst);
let note = self.params.notes[tap_index].load(Ordering::SeqCst);
let velocity = self.params.velocities[tap_index].load(Ordering::SeqCst);

// Create amplitude envelope
let mut amp_envelope =
let amp_envelope =
Smoother::new(SmoothingStyle::Linear(self.params.global.attack_ms.value()));

// Conditionally reset and configure the amplitude envelope
Expand All @@ -1288,7 +1254,7 @@ impl Del2 {
}

// Start delay tap and configure its properties
let mut delay_tap = self.start_delay_tap(timing, channel, note);
let delay_tap = self.start_delay_tap(channel, note);
delay_tap.velocity = velocity;
delay_tap.amp_envelope = amp_envelope;
delay_tap.tap_index = tap_index;
Expand Down Expand Up @@ -1590,7 +1556,7 @@ impl Del2 {
fn start_delay_tap(
&mut self,
// context: &mut impl ProcessContext<Self>,
sample_offset: u32,
// sample_offset: u32,
channel: u8,
note: u8,
) -> &mut DelayTap {
Expand Down Expand Up @@ -1636,15 +1602,15 @@ impl Del2 {

// The stolen delay tap needs to be terminated so the host can reuse its modulation
// resources
{
let oldest_delay_tap = oldest_delay_tap.as_ref().unwrap();
// context.send_event(NoteEvent::VoiceTerminated {
// timing: sample_offset,
// voice_id: Some(oldest_delay_tap.id),
// channel: oldest_delay_tap.channel,
// note: oldest_delay_tap.note,
// });
}
// {
// let oldest_delay_tap = oldest_delay_tap.as_ref().unwrap();
// context.send_event(NoteEvent::VoiceTerminated {
// timing: sample_offset,
// voice_id: Some(oldest_delay_tap.id),
// channel: oldest_delay_tap.channel,
// note: oldest_delay_tap.note,
// });
// }

*oldest_delay_tap = Some(new_delay_tap);
oldest_delay_tap.as_mut().unwrap()
Expand Down Expand Up @@ -1987,7 +1953,6 @@ impl PersistentField<'_, u8> for ArcAtomicBoolArray {
f(&value)
}
}

struct AtomicU8Array([Arc<AtomicU8>; NUM_TAPS]);
struct AtomicU32Array([Arc<AtomicU32>; NUM_TAPS]);
struct AtomicF32Array([Arc<AtomicF32>; NUM_TAPS]);
Expand Down Expand Up @@ -2059,6 +2024,14 @@ impl PersistentField<'_, [f32; 8]> for AtomicF32Array {
}
}

// TODO: use this:
// impl<T> Index<usize> for AtomicArray<T> {
// type Output = Arc<T>;
// fn index(&self, index: usize) -> &Self::Output {
// &self.0[index]
// }
// }

// Implement the Index trait to allow for array-style access
impl Index<usize> for AtomicU8Array {
type Output = Arc<AtomicU8>;
Expand Down

0 comments on commit b04c0f7

Please sign in to comment.