use crate::app::persistent_state::PersistentState;
use egui::{Button, Color32, CornerRadius, Stroke, TextFormat, Ui, Vec2, text::LayoutJob};
use rusty_link::{AblLink, SessionState};
use std::{
sync::{
Arc,
atomic::{AtomicU64, Ordering::Relaxed},
},
time::{Duration, Instant},
};
pub static CONNECTED_PEERS: AtomicU64 = AtomicU64::new(0);
pub static LINK_ACTIVE_COLOR: Color32 = Color32::from_rgb(41, 116, 145);
const BEAT_PROGRESSION_OFFSET: f32 = 40.0;
const LINK_QUANTUM: f64 = 4.0;
#[derive(Debug, Clone, Copy, Default)]
pub struct BeatState {
pub beat_progression: f32,
pub beats_per_minute: f32,
pub framerate: f32,
}
impl BeatState {
pub fn sample(link: &AblLink, framerate: f32) -> Self {
let mut session_state = SessionState::default();
link.capture_audio_session_state(&mut session_state);
let now = link.clock_micros();
Self {
beat_progression: BEAT_PROGRESSION_OFFSET
+ session_state.beat_at_time(now, LINK_QUANTUM) as f32,
beats_per_minute: session_state.tempo() as f32,
framerate,
}
}
}
pub struct Timing {
link: Arc<AblLink>,
beats_per_minute: f32,
previous_change_beats_per_minute: f32,
pub change_beats_per_minute: f32,
beat_progression: f32,
framerate: Option<f32>,
taps: [Option<Instant>; 4],
tap_count: usize,
}
impl std::fmt::Debug for Timing {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Timing")
.field("beats_per_minute", &self.beats_per_minute)
.field("change_beats_per_minute", &self.change_beats_per_minute)
.finish_non_exhaustive()
}
}
impl Default for Timing {
fn default() -> Self {
let link = AblLink::new(120.0);
link.enable(true);
Self {
link: Arc::new(link),
beats_per_minute: 120.0,
previous_change_beats_per_minute: 120.0,
change_beats_per_minute: 120.0,
beat_progression: 0.0,
framerate: None,
taps: [None; 4],
tap_count: 0,
}
}
}
impl Timing {
pub fn beat_progression(&self) -> f32 {
self.beat_progression
}
pub fn beats_per_minute(&self) -> f32 {
self.beats_per_minute
}
pub fn framerate(&self) -> Option<f32> {
self.framerate
}
pub fn set_framerate(&mut self, framerate: f32) {
self.framerate = Some(framerate);
}
pub fn beat_state(&self) -> BeatState {
BeatState {
beat_progression: self.beat_progression,
beats_per_minute: self.beats_per_minute,
framerate: self.framerate.unwrap_or_default(),
}
}
pub fn link(&self) -> Arc<AblLink> {
Arc::clone(&self.link)
}
#[cfg_attr(feature = "profiling", profiling::function)]
pub fn tick(&mut self, persistent_state: &PersistentState) {
self.set_link_values(persistent_state);
self.get_link_values();
self.remove_old_taps();
}
#[cfg_attr(feature = "profiling", profiling::function)]
fn get_link_values(&mut self) {
CONNECTED_PEERS.store(self.link.num_peers(), Relaxed);
let mut session_state = SessionState::default();
self.link.capture_app_session_state(&mut session_state);
let now = self.link.clock_micros();
self.beat_progression =
BEAT_PROGRESSION_OFFSET + session_state.beat_at_time(now, LINK_QUANTUM) as f32;
self.beats_per_minute = session_state.tempo() as f32;
}
#[cfg_attr(feature = "profiling", profiling::function)]
fn set_link_values(&mut self, persistent_state: &PersistentState) {
if persistent_state.ableton_link_read_only() {
self.previous_change_beats_per_minute = self.beats_per_minute;
self.change_beats_per_minute = self.beats_per_minute;
return;
}
if self.previous_change_beats_per_minute == self.change_beats_per_minute {
self.previous_change_beats_per_minute = self.beats_per_minute;
self.change_beats_per_minute = self.beats_per_minute;
return;
}
let mut session_state = SessionState::default();
self.link.capture_app_session_state(&mut session_state);
let now = self.link.clock_micros();
session_state.set_tempo(self.change_beats_per_minute as f64, now);
self.link.commit_app_session_state(&session_state);
self.previous_change_beats_per_minute = self.change_beats_per_minute;
}
fn remove_old_taps(&mut self) {
let mut reset_tap_count = true;
for tap in self.taps.iter_mut() {
if let Some(date) = tap {
if date.elapsed() > Duration::from_secs(10) {
tap.take();
} else {
reset_tap_count = false;
}
}
}
if reset_tap_count {
self.tap_count = 0;
}
}
pub fn half_button(
&mut self,
ui: &mut Ui,
tap_input: bool,
persistent_state: &PersistentState,
) {
let ableton_link_read_only = persistent_state.ableton_link_read_only();
if ui
.add_enabled(!ableton_link_read_only, Button::new("x½"))
.clicked()
|| (tap_input && !ableton_link_read_only)
{
self.multiply_speed(0.5, persistent_state);
}
}
pub fn double_button(
&mut self,
ui: &mut Ui,
tap_input: bool,
persistent_state: &PersistentState,
) {
let ableton_link_read_only = persistent_state.ableton_link_read_only();
if ui
.add_enabled(!ableton_link_read_only, Button::new("x2"))
.clicked()
|| (tap_input && !ableton_link_read_only)
{
self.multiply_speed(2.0, persistent_state);
}
}
pub fn multiply_speed(&mut self, multiplier: f32, persistent_state: &PersistentState) {
if persistent_state.ableton_link_read_only() {
return;
}
self.change_beats_per_minute *= multiplier;
}
pub fn add_speed(&mut self, delta: f32, persistent_state: &PersistentState) {
if persistent_state.ableton_link_read_only() {
return;
}
self.change_beats_per_minute += delta;
}
pub fn tap_button(
&mut self,
ui: &mut Ui,
menu_button_size: Vec2,
tap_input: bool,
persistent_state: &PersistentState,
) {
let ableton_link_read_only = persistent_state.ableton_link_read_only();
ui.scope(|ui| {
ui.style_mut().visuals.widgets.inactive.bg_stroke =
ui.style().visuals.widgets.noninteractive.bg_stroke;
let underlined = TextFormat {
underline: Stroke::new(1.0, Color32::GRAY),
..Default::default()
};
let mut tap_text = LayoutJob::default();
tap_text.append("T", 0.0, underlined);
tap_text.append(
&format!(
"ap{}",
match (self.tap_count, self.tap_count % 4) {
(0, _) => "",
(_, 0) => "/",
(_, 1) => "–",
(_, 2) => "\\",
_ => "|",
}
),
0.0,
TextFormat::default(),
);
let response = ui.add_enabled(
!ableton_link_read_only,
Button::new(tap_text).min_size(menu_button_size),
);
let mut alpha = None;
let bar_progression = self.beat_progression % 1.0;
if bar_progression < 0.10 {
alpha = Some(30.0);
} else if bar_progression < 0.20 {
alpha = Some(20.0 - ((bar_progression - 0.1) * 200.0));
} else if bar_progression > 0.9 {
alpha = Some((bar_progression - 0.9) * 200.0);
}
if let Some(alpha) = alpha {
ui.painter().rect_filled(
match self.beat_flank() {
0 =>
{
response
.rect
.split_left_right_at_fraction(0.5)
.0
.split_top_bottom_at_fraction(0.5)
.0
}
1 =>
{
response
.rect
.split_left_right_at_fraction(0.5)
.1
.split_top_bottom_at_fraction(0.5)
.0
}
2 =>
{
response
.rect
.split_left_right_at_fraction(0.5)
.0
.split_top_bottom_at_fraction(0.5)
.1
}
3 =>
{
response
.rect
.split_left_right_at_fraction(0.5)
.1
.split_top_bottom_at_fraction(0.5)
.1
}
_ => unreachable!(),
}
.shrink(1.0),
CornerRadius::default(),
Color32::from_white_alpha(alpha as u8),
);
}
let tapped = response.clicked() || (tap_input && !ableton_link_read_only);
if tapped {
self.tap(persistent_state);
}
});
}
pub fn beat_flank(&self) -> u8 {
match self.beat_progression % 4.0 {
0.5..1.5 => 1,
1.5..2.5 => 2,
2.5..3.5 => 3,
_ => 0,
}
}
pub fn tap(&mut self, persistent_state: &PersistentState) {
if persistent_state.ableton_link_read_only() {
return;
}
let link_now = self.link.clock_micros();
self.tap_count += 1;
let now = Instant::now();
self.taps[0] = Some(now);
self.taps.sort();
let first = self
.taps
.iter()
.filter_map(|tap| *tap)
.next()
.expect("There must be at least one entry");
if first == now {
return;
}
let beat_time_first = now.duration_since(first);
let avg_beat_time = (beat_time_first.as_nanos())
/ (self.taps.iter().filter(|tap| tap.is_some()).count() as u128 - 1);
let new_beats_per_minute = (60e+9f64 / f64::from(avg_beat_time as u32)) as f32;
let offset = self.beat_progression % 4.0;
let goal_offset = (self.tap_count - 1) as f32 % 4.0;
let mut session_state = SessionState::default();
self.link.capture_app_session_state(&mut session_state);
session_state.set_tempo(new_beats_per_minute as f64, link_now);
session_state.request_beat_at_time(
(self.beat_progression + goal_offset - offset) as f64,
link_now,
4.0,
);
self.link.commit_app_session_state(&session_state);
}
}