BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / notify / ring_tone.rs

ring_tone.rs

ring_tone.rs 5.3 KiB Download Raw
1use std::f32::consts::PI;
2
3pub const SAMPLE_RATE: u32 = 44_100;
4const FADE_SECONDS: f32 = 0.015;
5const AMPLITUDE: f32 = 0.3;
6const LOW_HZ: f32 = 784.;
7const HIGH_HZ: f32 = 988.;
8const BEEP_SECONDS: f32 = 0.18;
9const BEEPS: [f32; 4] = [LOW_HZ, HIGH_HZ, LOW_HZ, HIGH_HZ];
10const CYCLE_SECONDS: f32 = 2.4;
11const CHIME_NOTES: [f32; 4] = [523.25, 659.25, 783.99, 1046.5];
12const CHIME_NOTE_SECONDS: f32 = 0.22;
13const CHIME_CYCLE_SECONDS: f32 = 2.6;
14const PULSE_NOTES: [f32; 3] = [587.33, 587.33, 587.33];
15const PULSE_NOTE_SECONDS: f32 = 0.26;
16const PULSE_CYCLE_SECONDS: f32 = 2.2;
17const RINGBACK_HZ: f32 = 425.;
18const RINGBACK_ON_SECONDS: f32 = 1.;
19const RINGBACK_CYCLE_SECONDS: f32 = 5.;
20
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
22pub enum Ringtone {
23 #[default]
24 Default,
25 Chime,
26 Pulse,
27}
28
29impl Ringtone {
30 pub const ALL: [Ringtone; 3] = [Ringtone::Default, Ringtone::Chime, Ringtone::Pulse];
31
32 pub fn label(self) -> &'static str {
33 match self {
34 Ringtone::Default => "Default",
35 Ringtone::Chime => "Chime",
36 Ringtone::Pulse => "Pulse",
37 }
38 }
39
40 pub fn key(self) -> &'static str {
41 match self {
42 Ringtone::Default => "default",
43 Ringtone::Chime => "chime",
44 Ringtone::Pulse => "pulse",
45 }
46 }
47
48 pub fn from_key(key: &str) -> Option<Ringtone> {
49 Ringtone::ALL.into_iter().find(|ringtone| ringtone.key() == key)
50 }
51
52 pub fn cycle(self, sample_rate: u32) -> Vec<f32> {
53 match self {
54 Ringtone::Default => ring_cycle(sample_rate),
55 Ringtone::Chime => tone_cycle(sample_rate, &CHIME_NOTES, CHIME_NOTE_SECONDS, CHIME_CYCLE_SECONDS),
56 Ringtone::Pulse => tone_cycle(sample_rate, &PULSE_NOTES, PULSE_NOTE_SECONDS, PULSE_CYCLE_SECONDS),
57 }
58 }
59}
60
61pub fn ring_cycle(sample_rate: u32) -> Vec<f32> {
62 tone_cycle(sample_rate, &BEEPS, BEEP_SECONDS, CYCLE_SECONDS)
63}
64
65pub fn ringback_cycle(sample_rate: u32) -> Vec<f32> {
66 tone_cycle(sample_rate, &[RINGBACK_HZ], RINGBACK_ON_SECONDS, RINGBACK_CYCLE_SECONDS)
67}
68
69fn tone_cycle(sample_rate: u32, frequencies: &[f32], tone_seconds: f32, cycle_seconds: f32) -> Vec<f32> {
70 let total = (cycle_seconds * sample_rate as f32) as usize;
71 let tone = (tone_seconds * sample_rate as f32) as usize;
72 let fade = (FADE_SECONDS * sample_rate as f32) as usize;
73 let mut samples = vec![0.; total];
74 for (index, frequency) in frequencies.iter().enumerate() {
75 let start = index * tone;
76 for position in 0..tone {
77 let envelope = (position.min(tone - 1 - position) as f32 / fade as f32).min(1.);
78 let phase = 2. * PI * frequency * position as f32 / sample_rate as f32;
79 samples[start + position] = AMPLITUDE * envelope * phase.sin();
80 }
81 }
82 samples
83}
84
85#[cfg(test)]
86mod tests {
87 use super::*;
88
89 #[test]
90 fn a_cycle_lasts_two_and_a_half_seconds_at_most_and_stays_quiet() {
91 let samples = ring_cycle(SAMPLE_RATE);
92 assert_eq!(samples.len(), (CYCLE_SECONDS * SAMPLE_RATE as f32) as usize);
93 assert!(samples.iter().all(|sample| sample.abs() <= AMPLITUDE + f32::EPSILON));
94 assert!(samples.iter().any(|sample| sample.abs() > AMPLITUDE * 0.9));
95 }
96
97 #[test]
98 fn beeps_fade_in_and_out_without_clicks() {
99 let samples = ring_cycle(SAMPLE_RATE);
100 assert_eq!(samples[0], 0.);
101 let beep = (BEEP_SECONDS * SAMPLE_RATE as f32) as usize;
102 assert!(samples[1].abs() < 0.05);
103 assert!(samples[beep - 1].abs() < 0.05);
104 }
105
106 #[test]
107 fn the_cycle_ends_in_silence_so_it_can_repeat() {
108 let samples = ring_cycle(SAMPLE_RATE);
109 let beeps_end = (BEEP_SECONDS * BEEPS.len() as f32 * SAMPLE_RATE as f32) as usize;
110 assert!(samples[beeps_end..].iter().all(|sample| *sample == 0.));
111 assert!(beeps_end < samples.len());
112 }
113
114 #[test]
115 fn the_cycle_is_the_same_every_time() {
116 assert_eq!(ring_cycle(SAMPLE_RATE), ring_cycle(SAMPLE_RATE));
117 }
118
119 #[test]
120 fn the_ringback_is_one_second_of_425_hz_then_four_seconds_of_silence() {
121 let samples = ringback_cycle(SAMPLE_RATE);
122 let rate = SAMPLE_RATE as usize;
123 assert_eq!(samples.len(), 5 * rate);
124 assert!(samples[..rate].iter().any(|sample| sample.abs() > AMPLITUDE * 0.9));
125 assert!(samples[rate..].iter().all(|sample| *sample == 0.));
126 let crossings = samples[..rate].windows(2).filter(|pair| pair[0] < 0. && pair[1] >= 0.).count();
127 assert!((424..=426).contains(&crossings));
128 }
129
130 #[test]
131 fn every_ringtone_is_quiet_ends_in_silence_and_differs_from_the_others() {
132 for ringtone in Ringtone::ALL {
133 let samples = ringtone.cycle(SAMPLE_RATE);
134 assert!(samples.iter().all(|sample| sample.abs() <= AMPLITUDE + f32::EPSILON));
135 assert!(samples.iter().any(|sample| sample.abs() > AMPLITUDE * 0.9));
136 assert_eq!(samples.last().copied(), Some(0.));
137 }
138 assert_ne!(Ringtone::Default.cycle(SAMPLE_RATE), Ringtone::Chime.cycle(SAMPLE_RATE));
139 assert_ne!(Ringtone::Chime.cycle(SAMPLE_RATE), Ringtone::Pulse.cycle(SAMPLE_RATE));
140 }
141
142 #[test]
143 fn ringtone_keys_round_trip() {
144 for ringtone in Ringtone::ALL {
145 assert_eq!(Ringtone::from_key(ringtone.key()), Some(ringtone));
146 }
147 assert_eq!(Ringtone::from_key("nope"), None);
148 }
149}