BlueGit

ict rusty-teams

Files

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

ring_tone.rs

ring_tone.rs 6.5 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 MEETING_NOTES: [f32; 3] = [659.25, 783.99, 1046.5];
18const MEETING_NOTE_SECONDS: f32 = 0.26;
19const RINGBACK_HZ: f32 = 425.;
20const RINGBACK_ON_SECONDS: f32 = 1.;
21const RINGBACK_CYCLE_SECONDS: f32 = 5.;
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
24pub enum Ringtone {
25 #[default]
26 Default,
27 Chime,
28 Pulse,
29}
30
31impl Ringtone {
32 pub const ALL: [Ringtone; 3] = [Ringtone::Default, Ringtone::Chime, Ringtone::Pulse];
33
34 pub fn label(self) -> &'static str {
35 match self {
36 Ringtone::Default => "Default",
37 Ringtone::Chime => "Chime",
38 Ringtone::Pulse => "Pulse",
39 }
40 }
41
42 pub fn key(self) -> &'static str {
43 match self {
44 Ringtone::Default => "default",
45 Ringtone::Chime => "chime",
46 Ringtone::Pulse => "pulse",
47 }
48 }
49
50 pub fn from_key(key: &str) -> Option<Ringtone> {
51 Ringtone::ALL
52 .into_iter()
53 .find(|ringtone| ringtone.key() == key)
54 }
55
56 pub fn cycle(self, sample_rate: u32) -> Vec<f32> {
57 match self {
58 Ringtone::Default => ring_cycle(sample_rate),
59 Ringtone::Chime => tone_cycle(
60 sample_rate,
61 &CHIME_NOTES,
62 CHIME_NOTE_SECONDS,
63 CHIME_CYCLE_SECONDS,
64 ),
65 Ringtone::Pulse => tone_cycle(
66 sample_rate,
67 &PULSE_NOTES,
68 PULSE_NOTE_SECONDS,
69 PULSE_CYCLE_SECONDS,
70 ),
71 }
72 }
73}
74
75pub fn ring_cycle(sample_rate: u32) -> Vec<f32> {
76 tone_cycle(sample_rate, &BEEPS, BEEP_SECONDS, CYCLE_SECONDS)
77}
78
79pub fn meeting_chime(sample_rate: u32) -> Vec<f32> {
80 let seconds = MEETING_NOTE_SECONDS * MEETING_NOTES.len() as f32;
81 tone_cycle(sample_rate, &MEETING_NOTES, MEETING_NOTE_SECONDS, seconds)
82}
83
84pub fn ringback_cycle(sample_rate: u32) -> Vec<f32> {
85 tone_cycle(
86 sample_rate,
87 &[RINGBACK_HZ],
88 RINGBACK_ON_SECONDS,
89 RINGBACK_CYCLE_SECONDS,
90 )
91}
92
93fn tone_cycle(
94 sample_rate: u32,
95 frequencies: &[f32],
96 tone_seconds: f32,
97 cycle_seconds: f32,
98) -> Vec<f32> {
99 let total = (cycle_seconds * sample_rate as f32) as usize;
100 let tone = (tone_seconds * sample_rate as f32) as usize;
101 let fade = (FADE_SECONDS * sample_rate as f32) as usize;
102 let mut samples = vec![0.; total];
103 for (index, frequency) in frequencies.iter().enumerate() {
104 let start = index * tone;
105 for position in 0..tone {
106 let envelope = (position.min(tone - 1 - position) as f32 / fade as f32).min(1.);
107 let phase = 2. * PI * frequency * position as f32 / sample_rate as f32;
108 samples[start + position] = AMPLITUDE * envelope * phase.sin();
109 }
110 }
111 samples
112}
113
114#[cfg(test)]
115mod tests {
116 use super::*;
117
118 #[test]
119 fn a_cycle_lasts_two_and_a_half_seconds_at_most_and_stays_quiet() {
120 let samples = ring_cycle(SAMPLE_RATE);
121 assert_eq!(samples.len(), (CYCLE_SECONDS * SAMPLE_RATE as f32) as usize);
122 assert!(
123 samples
124 .iter()
125 .all(|sample| sample.abs() <= AMPLITUDE + f32::EPSILON)
126 );
127 assert!(samples.iter().any(|sample| sample.abs() > AMPLITUDE * 0.9));
128 }
129
130 #[test]
131 fn beeps_fade_in_and_out_without_clicks() {
132 let samples = ring_cycle(SAMPLE_RATE);
133 assert_eq!(samples[0], 0.);
134 let beep = (BEEP_SECONDS * SAMPLE_RATE as f32) as usize;
135 assert!(samples[1].abs() < 0.05);
136 assert!(samples[beep - 1].abs() < 0.05);
137 }
138
139 #[test]
140 fn the_cycle_ends_in_silence_so_it_can_repeat() {
141 let samples = ring_cycle(SAMPLE_RATE);
142 let beeps_end = (BEEP_SECONDS * BEEPS.len() as f32 * SAMPLE_RATE as f32) as usize;
143 assert!(samples[beeps_end..].iter().all(|sample| *sample == 0.));
144 assert!(beeps_end < samples.len());
145 }
146
147 #[test]
148 fn the_cycle_is_the_same_every_time() {
149 assert_eq!(ring_cycle(SAMPLE_RATE), ring_cycle(SAMPLE_RATE));
150 }
151
152 #[test]
153 fn the_ringback_is_one_second_of_425_hz_then_four_seconds_of_silence() {
154 let samples = ringback_cycle(SAMPLE_RATE);
155 let rate = SAMPLE_RATE as usize;
156 assert_eq!(samples.len(), 5 * rate);
157 assert!(
158 samples[..rate]
159 .iter()
160 .any(|sample| sample.abs() > AMPLITUDE * 0.9)
161 );
162 assert!(samples[rate..].iter().all(|sample| *sample == 0.));
163 let crossings = samples[..rate]
164 .windows(2)
165 .filter(|pair| pair[0] < 0. && pair[1] >= 0.)
166 .count();
167 assert!((424..=426).contains(&crossings));
168 }
169
170 #[test]
171 fn every_ringtone_is_quiet_ends_in_silence_and_differs_from_the_others() {
172 for ringtone in Ringtone::ALL {
173 let samples = ringtone.cycle(SAMPLE_RATE);
174 assert!(
175 samples
176 .iter()
177 .all(|sample| sample.abs() <= AMPLITUDE + f32::EPSILON)
178 );
179 assert!(samples.iter().any(|sample| sample.abs() > AMPLITUDE * 0.9));
180 assert_eq!(samples.last().copied(), Some(0.));
181 }
182 assert_ne!(
183 Ringtone::Default.cycle(SAMPLE_RATE),
184 Ringtone::Chime.cycle(SAMPLE_RATE)
185 );
186 assert_ne!(
187 Ringtone::Chime.cycle(SAMPLE_RATE),
188 Ringtone::Pulse.cycle(SAMPLE_RATE)
189 );
190 }
191
192 #[test]
193 fn the_meeting_chime_is_short_quiet_and_audible() {
194 let samples = meeting_chime(SAMPLE_RATE);
195 assert!(samples.len() <= SAMPLE_RATE as usize);
196 assert!(
197 samples
198 .iter()
199 .all(|sample| sample.abs() <= AMPLITUDE + f32::EPSILON)
200 );
201 assert!(samples.iter().any(|sample| sample.abs() > AMPLITUDE * 0.9));
202 }
203
204 #[test]
205 fn ringtone_keys_round_trip() {
206 for ringtone in Ringtone::ALL {
207 assert_eq!(Ringtone::from_key(ringtone.key()), Some(ringtone));
208 }
209 assert_eq!(Ringtone::from_key("nope"), None);
210 }
211}