| 1 | use std::f32::consts::PI; |
| 2 | |
| 3 | pub const SAMPLE_RATE: u32 = 44_100; |
| 4 | const FADE_SECONDS: f32 = 0.015; |
| 5 | const AMPLITUDE: f32 = 0.3; |
| 6 | const LOW_HZ: f32 = 784.; |
| 7 | const HIGH_HZ: f32 = 988.; |
| 8 | const BEEP_SECONDS: f32 = 0.18; |
| 9 | const BEEPS: [f32; 4] = [LOW_HZ, HIGH_HZ, LOW_HZ, HIGH_HZ]; |
| 10 | const CYCLE_SECONDS: f32 = 2.4; |
| 11 | const CHIME_NOTES: [f32; 4] = [523.25, 659.25, 783.99, 1046.5]; |
| 12 | const CHIME_NOTE_SECONDS: f32 = 0.22; |
| 13 | const CHIME_CYCLE_SECONDS: f32 = 2.6; |
| 14 | const PULSE_NOTES: [f32; 3] = [587.33, 587.33, 587.33]; |
| 15 | const PULSE_NOTE_SECONDS: f32 = 0.26; |
| 16 | const PULSE_CYCLE_SECONDS: f32 = 2.2; |
| 17 | const RINGBACK_HZ: f32 = 425.; |
| 18 | const RINGBACK_ON_SECONDS: f32 = 1.; |
| 19 | const RINGBACK_CYCLE_SECONDS: f32 = 5.; |
| 20 | |
| 21 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 22 | pub enum Ringtone { |
| 23 | #[default] |
| 24 | Default, |
| 25 | Chime, |
| 26 | Pulse, |
| 27 | } |
| 28 | |
| 29 | impl 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 |
| 50 | .into_iter() |
| 51 | .find(|ringtone| ringtone.key() == key) |
| 52 | } |
| 53 | |
| 54 | pub fn cycle(self, sample_rate: u32) -> Vec<f32> { |
| 55 | match self { |
| 56 | Ringtone::Default => ring_cycle(sample_rate), |
| 57 | Ringtone::Chime => tone_cycle( |
| 58 | sample_rate, |
| 59 | &CHIME_NOTES, |
| 60 | CHIME_NOTE_SECONDS, |
| 61 | CHIME_CYCLE_SECONDS, |
| 62 | ), |
| 63 | Ringtone::Pulse => tone_cycle( |
| 64 | sample_rate, |
| 65 | &PULSE_NOTES, |
| 66 | PULSE_NOTE_SECONDS, |
| 67 | PULSE_CYCLE_SECONDS, |
| 68 | ), |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | pub fn ring_cycle(sample_rate: u32) -> Vec<f32> { |
| 74 | tone_cycle(sample_rate, &BEEPS, BEEP_SECONDS, CYCLE_SECONDS) |
| 75 | } |
| 76 | |
| 77 | pub fn ringback_cycle(sample_rate: u32) -> Vec<f32> { |
| 78 | tone_cycle( |
| 79 | sample_rate, |
| 80 | &[RINGBACK_HZ], |
| 81 | RINGBACK_ON_SECONDS, |
| 82 | RINGBACK_CYCLE_SECONDS, |
| 83 | ) |
| 84 | } |
| 85 | |
| 86 | fn tone_cycle( |
| 87 | sample_rate: u32, |
| 88 | frequencies: &[f32], |
| 89 | tone_seconds: f32, |
| 90 | cycle_seconds: f32, |
| 91 | ) -> Vec<f32> { |
| 92 | let total = (cycle_seconds * sample_rate as f32) as usize; |
| 93 | let tone = (tone_seconds * sample_rate as f32) as usize; |
| 94 | let fade = (FADE_SECONDS * sample_rate as f32) as usize; |
| 95 | let mut samples = vec![0.; total]; |
| 96 | for (index, frequency) in frequencies.iter().enumerate() { |
| 97 | let start = index * tone; |
| 98 | for position in 0..tone { |
| 99 | let envelope = (position.min(tone - 1 - position) as f32 / fade as f32).min(1.); |
| 100 | let phase = 2. * PI * frequency * position as f32 / sample_rate as f32; |
| 101 | samples[start + position] = AMPLITUDE * envelope * phase.sin(); |
| 102 | } |
| 103 | } |
| 104 | samples |
| 105 | } |
| 106 | |
| 107 | #[cfg(test)] |
| 108 | mod tests { |
| 109 | use super::*; |
| 110 | |
| 111 | #[test] |
| 112 | fn a_cycle_lasts_two_and_a_half_seconds_at_most_and_stays_quiet() { |
| 113 | let samples = ring_cycle(SAMPLE_RATE); |
| 114 | assert_eq!(samples.len(), (CYCLE_SECONDS * SAMPLE_RATE as f32) as usize); |
| 115 | assert!( |
| 116 | samples |
| 117 | .iter() |
| 118 | .all(|sample| sample.abs() <= AMPLITUDE + f32::EPSILON) |
| 119 | ); |
| 120 | assert!(samples.iter().any(|sample| sample.abs() > AMPLITUDE * 0.9)); |
| 121 | } |
| 122 | |
| 123 | #[test] |
| 124 | fn beeps_fade_in_and_out_without_clicks() { |
| 125 | let samples = ring_cycle(SAMPLE_RATE); |
| 126 | assert_eq!(samples[0], 0.); |
| 127 | let beep = (BEEP_SECONDS * SAMPLE_RATE as f32) as usize; |
| 128 | assert!(samples[1].abs() < 0.05); |
| 129 | assert!(samples[beep - 1].abs() < 0.05); |
| 130 | } |
| 131 | |
| 132 | #[test] |
| 133 | fn the_cycle_ends_in_silence_so_it_can_repeat() { |
| 134 | let samples = ring_cycle(SAMPLE_RATE); |
| 135 | let beeps_end = (BEEP_SECONDS * BEEPS.len() as f32 * SAMPLE_RATE as f32) as usize; |
| 136 | assert!(samples[beeps_end..].iter().all(|sample| *sample == 0.)); |
| 137 | assert!(beeps_end < samples.len()); |
| 138 | } |
| 139 | |
| 140 | #[test] |
| 141 | fn the_cycle_is_the_same_every_time() { |
| 142 | assert_eq!(ring_cycle(SAMPLE_RATE), ring_cycle(SAMPLE_RATE)); |
| 143 | } |
| 144 | |
| 145 | #[test] |
| 146 | fn the_ringback_is_one_second_of_425_hz_then_four_seconds_of_silence() { |
| 147 | let samples = ringback_cycle(SAMPLE_RATE); |
| 148 | let rate = SAMPLE_RATE as usize; |
| 149 | assert_eq!(samples.len(), 5 * rate); |
| 150 | assert!( |
| 151 | samples[..rate] |
| 152 | .iter() |
| 153 | .any(|sample| sample.abs() > AMPLITUDE * 0.9) |
| 154 | ); |
| 155 | assert!(samples[rate..].iter().all(|sample| *sample == 0.)); |
| 156 | let crossings = samples[..rate] |
| 157 | .windows(2) |
| 158 | .filter(|pair| pair[0] < 0. && pair[1] >= 0.) |
| 159 | .count(); |
| 160 | assert!((424..=426).contains(&crossings)); |
| 161 | } |
| 162 | |
| 163 | #[test] |
| 164 | fn every_ringtone_is_quiet_ends_in_silence_and_differs_from_the_others() { |
| 165 | for ringtone in Ringtone::ALL { |
| 166 | let samples = ringtone.cycle(SAMPLE_RATE); |
| 167 | assert!( |
| 168 | samples |
| 169 | .iter() |
| 170 | .all(|sample| sample.abs() <= AMPLITUDE + f32::EPSILON) |
| 171 | ); |
| 172 | assert!(samples.iter().any(|sample| sample.abs() > AMPLITUDE * 0.9)); |
| 173 | assert_eq!(samples.last().copied(), Some(0.)); |
| 174 | } |
| 175 | assert_ne!( |
| 176 | Ringtone::Default.cycle(SAMPLE_RATE), |
| 177 | Ringtone::Chime.cycle(SAMPLE_RATE) |
| 178 | ); |
| 179 | assert_ne!( |
| 180 | Ringtone::Chime.cycle(SAMPLE_RATE), |
| 181 | Ringtone::Pulse.cycle(SAMPLE_RATE) |
| 182 | ); |
| 183 | } |
| 184 | |
| 185 | #[test] |
| 186 | fn ringtone_keys_round_trip() { |
| 187 | for ringtone in Ringtone::ALL { |
| 188 | assert_eq!(Ringtone::from_key(ringtone.key()), Some(ringtone)); |
| 189 | } |
| 190 | assert_eq!(Ringtone::from_key("nope"), None); |
| 191 | } |
| 192 | } |