Skip to content

Commit 8d4988d

Browse files
authored
Merge pull request #15 from ardura/beat-bar-timing-sync
Beat bar timing sync
2 parents 34342d5 + 5591e03 commit 8d4988d

File tree

1 file changed

+60
-11
lines changed

1 file changed

+60
-11
lines changed

src/lib.rs

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ const CYAN: Color32 = Color32::from_rgb(14,177,210);
1515
const YELLOW: Color32 = Color32::from_rgb(248, 255, 31);
1616
const DARK: Color32 = Color32::from_rgb(40, 40, 40);
1717

18+
#[derive(Enum, Clone, PartialEq)]
19+
pub enum BeatSync {
20+
Beat,
21+
Bar
22+
}
23+
1824
pub struct Gain {
1925
params: Arc<GainParams>,
2026

@@ -57,6 +63,10 @@ struct GainParams {
5763
/// Horizontal Scaling
5864
#[id = "scaling"]
5965
pub h_scale: IntParam,
66+
67+
/// Sync Timing
68+
#[id = "Sync Timing"]
69+
pub sync_timing: EnumParam<BeatSync>,
6070
}
6171

6272
impl Default for Gain {
@@ -114,6 +124,9 @@ impl Default for GainParams {
114124
24,
115125
IntRange::Linear {min: 1, max: 150 },
116126
).with_unit(" Skip"),
127+
128+
// Sync timing parameter
129+
sync_timing: EnumParam::new("Timing", BeatSync::Beat),
117130
}
118131
}
119132
}
@@ -225,17 +238,29 @@ impl Plugin for Gain {
225238
ui.add_space(4.0);
226239
let _swap_response = ui.checkbox(&mut ontop.lock(), "Swap").on_hover_text("Change the drawing order of waveforms");
227240

228-
let sync_response = ui.checkbox(&mut sync_var.lock(), "Sync Beat").on_hover_text("Lock drawing to beat");
241+
let sync_response = ui.checkbox(&mut sync_var.lock(), "Sync").on_hover_text("Lock drawing to timing");
229242
let alt_sync = ui.checkbox(&mut alt_sync.lock(), "Alt. Sync").on_hover_text("Try this if Sync doesn't work");
243+
let timing = ui.add(widgets::ParamSlider::for_param(&params.sync_timing, setter).with_width(60.0)).on_hover_text("Refresh interval when sync enabled");
230244

231245
let dir_response = ui.checkbox(&mut dir_var.lock(), "Flip").on_hover_text("Flip direction of oscilloscope");
232246

233247
if gain_handle.changed() {
234248
sum_line = Line::new(PlotPoints::default());
235249
}
236250
// Reset our line on change
237-
if sync_response.clicked() || dir_response.clicked() || alt_sync.clicked()
251+
if sync_response.clicked() || dir_response.clicked() || alt_sync.clicked() || timing.changed()
238252
{
253+
// Keep same direction when syncing (Issue #12)
254+
if sync_response.clicked(){
255+
// If flip selected already, it should be deselected on this click
256+
if *dir_var.lock() {
257+
*dir_var.lock() = false;
258+
}
259+
// If flip not selected, it should now be selected
260+
else {
261+
*dir_var.lock() = true;
262+
}
263+
}
239264
sum_line = Line::new(PlotPoints::default());
240265
aux_line = Line::new(PlotPoints::default());
241266
line = Line::new(PlotPoints::default());
@@ -380,19 +405,43 @@ impl Plugin for Gain {
380405
// This should still play well with other DAWs using this timing
381406
current_beat = ((current_beat + 0.036) * 100.0 as f64).round() / 100.0 as f64;
382407
let current_bar = current_beat as i64;
383-
// Tracks based off beat number for other daws - this is a mutex instead of atomic for locking
384-
if *self.alt_sync_beat.lock() != current_bar {
385-
self.in_place_index = Arc::new(AtomicI32::new(0));
386-
self.skip_counter = 0;
387-
*self.alt_sync_beat.lock() = current_bar;
408+
// Added in Issue #11
409+
match self.params.sync_timing.value() {
410+
BeatSync::Bar => {
411+
// Tracks based off beat number for other daws - this is a mutex instead of atomic for locking
412+
if *self.alt_sync_beat.lock() != current_bar && current_bar % 4 == 0 {
413+
self.in_place_index = Arc::new(AtomicI32::new(0));
414+
self.skip_counter = 0;
415+
*self.alt_sync_beat.lock() = current_bar;
416+
}
417+
},
418+
BeatSync::Beat => {
419+
// Tracks based off beat number for other daws - this is a mutex instead of atomic for locking
420+
if *self.alt_sync_beat.lock() != current_bar {
421+
self.in_place_index = Arc::new(AtomicI32::new(0));
422+
self.skip_counter = 0;
423+
*self.alt_sync_beat.lock() = current_bar;
424+
}
425+
},
388426
}
389427
} else {
390428
// Works in FL Studio but not other daws, hence the previous couple of lines
391429
current_beat = (current_beat * 10000.0 as f64).round() / 10000.0 as f64;
392-
if current_beat % 1.0 == 0.0 {
393-
// Reset our index to the sample vecdeques
394-
self.in_place_index = Arc::new(AtomicI32::new(0));
395-
self.skip_counter = 0;
430+
match self.params.sync_timing.value() {
431+
BeatSync::Bar => {
432+
if current_beat % 4.0 == 0.0 {
433+
// Reset our index to the sample vecdeques
434+
self.in_place_index = Arc::new(AtomicI32::new(0));
435+
self.skip_counter = 0;
436+
}
437+
},
438+
BeatSync::Beat => {
439+
if current_beat % 1.0 == 0.0 {
440+
// Reset our index to the sample vecdeques
441+
self.in_place_index = Arc::new(AtomicI32::new(0));
442+
self.skip_counter = 0;
443+
}
444+
},
396445
}
397446
}
398447
}

0 commit comments

Comments
 (0)