qualia_audio/features/tonal/
scale.rs1use crate::types::AudioError;
20
21const MAJOR_MASK: [bool; 12] = [
23 true, false, true, false, true, true, false, true, false, true, false, true,
24];
25const MINOR_MASK: [bool; 12] = [
27 true, false, true, true, false, true, false, true, true, false, true, false,
28];
29
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32pub enum ScaleProposal {
33 Major,
34 Minor,
35 Other,
37 Unknown,
39}
40
41#[derive(Debug, Clone, Copy, PartialEq)]
43pub struct ScaleEstimate {
44 pub tonic_pc: u8,
46 pub scale: ScaleProposal,
47 pub score: f32,
49}
50
51pub fn estimate_scale(
61 hpcp: &[f32],
62 assumes_12tet: bool,
63 min_score: f32,
64) -> Result<ScaleEstimate, AudioError> {
65 if hpcp.is_empty() {
66 return Err(AudioError::InvalidParameter);
67 }
68 let unknown = ScaleEstimate {
69 tonic_pc: 0,
70 scale: ScaleProposal::Unknown,
71 score: 0.0,
72 };
73 if !assumes_12tet || hpcp.len() < 12 {
74 return Ok(unknown);
75 }
76 let frame = &hpcp[..12];
77
78 let total: f32 = frame.iter().map(|v| v.max(0.0)).sum();
79 if total <= 0.0 {
80 return Ok(unknown);
81 }
82 let maxv = frame.iter().cloned().fold(f32::MIN, f32::max);
84 let mean = total / 12.0;
85 if maxv <= mean * 1.05 {
86 return Ok(unknown);
87 }
88
89 let mut best_tonic = 0u8;
91 let mut best_is_major = true;
92 let mut best_score = 0.0f32;
93 for tonic in 0usize..12 {
94 for (mask, is_major) in [(&MAJOR_MASK, true), (&MINOR_MASK, false)] {
95 let mut inside = 0.0f32;
96 for (pc, &v) in frame.iter().enumerate() {
97 if mask[(pc + 12 - tonic) % 12] {
98 inside += v.max(0.0);
99 }
100 }
101 let score = inside / total;
102 if score > best_score {
103 best_score = score;
104 best_tonic = tonic as u8;
105 best_is_major = is_major;
106 }
107 }
108 }
109
110 let scale = if best_score >= min_score {
111 if best_is_major {
112 ScaleProposal::Major
113 } else {
114 ScaleProposal::Minor
115 }
116 } else {
117 ScaleProposal::Other
118 };
119
120 Ok(ScaleEstimate {
121 tonic_pc: best_tonic,
122 scale,
123 score: best_score,
124 })
125}
126
127#[cfg(test)]
128mod tests {
129 use super::*;
130
131 #[test]
133 fn c_major_scale_is_major() {
134 let mut hpcp = [0.0f32; 12];
135 for pc in [0, 2, 4, 5, 7, 9, 11] {
136 hpcp[pc] = 1.0;
137 }
138 let est = estimate_scale(&hpcp, true, 0.9).expect("scale");
139 assert_eq!(est.scale, ScaleProposal::Major);
140 assert_eq!(est.tonic_pc, 0);
141 assert!(est.score > 0.99, "score = {}", est.score);
142 }
143
144 #[test]
146 fn a_minor_scale_is_minor() {
147 let mut hpcp = [0.0f32; 12];
148 for pc in [9, 11, 0, 2, 4, 5, 7] {
149 hpcp[pc] = 1.0;
150 }
151 let est = estimate_scale(&hpcp, true, 0.9).expect("scale");
155 assert!(matches!(
156 est.scale,
157 ScaleProposal::Major | ScaleProposal::Minor
158 ));
159 assert!(est.score > 0.99);
160 }
161
162 #[test]
164 fn flat_is_unknown() {
165 let hpcp = [1.0f32; 12];
166 let est = estimate_scale(&hpcp, true, 0.9).expect("scale");
167 assert_eq!(est.scale, ScaleProposal::Unknown);
168 }
169
170 #[test]
172 fn non_12tet_is_unknown() {
173 let mut hpcp = [0.0f32; 12];
174 hpcp[0] = 1.0;
175 let est = estimate_scale(&hpcp, false, 0.9).expect("scale");
176 assert_eq!(est.scale, ScaleProposal::Unknown);
177 }
178
179 #[test]
182 fn chromatic_is_other() {
183 let mut hpcp = [1.0f32; 12];
186 hpcp[0] = 3.0;
187 let est = estimate_scale(&hpcp, true, 0.95).expect("scale");
188 assert_eq!(est.scale, ScaleProposal::Other, "score = {}", est.score);
189 }
190
191 #[test]
192 fn rejects_empty() {
193 assert_eq!(
194 estimate_scale(&[], true, 0.9),
195 Err(AudioError::InvalidParameter)
196 );
197 }
198}