diff options
-rw-r--r-- | apps/plugins/pitch_detector.c | 123 |
1 files changed, 58 insertions, 65 deletions
diff --git a/apps/plugins/pitch_detector.c b/apps/plugins/pitch_detector.c index 45184031b2..be47823e0d 100644 --- a/apps/plugins/pitch_detector.c +++ b/apps/plugins/pitch_detector.c | |||
@@ -70,45 +70,38 @@ | |||
70 | 70 | ||
71 | 71 | ||
72 | /* Some fixed point calculation stuff */ | 72 | /* Some fixed point calculation stuff */ |
73 | typedef int32_t fixed_data; | 73 | typedef int32_t fixed; |
74 | typedef struct | ||
75 | { | ||
76 | fixed_data a; | ||
77 | } fixed; | ||
78 | #define FIXED_PRECISION 18 | 74 | #define FIXED_PRECISION 18 |
79 | #define FP_MAX ((fixed) {0x7fffffff}) | 75 | #define FP_MAX ((fixed) {0x7fffffff}) |
80 | #define FP_MIN ((fixed) {-0x80000000}) | 76 | #define FP_MIN ((fixed) {-0x80000000}) |
81 | #define int2fixed(x) ((fixed){(x) << FIXED_PRECISION}) | 77 | #define int2fixed(x) ((fixed)((x) << FIXED_PRECISION)) |
82 | #define int2mantissa(x) ((fixed){x}) | 78 | #define int2mantissa(x) ((fixed)(x)) |
83 | #define fixed2int(x) ((int)((x).a >> FIXED_PRECISION)) | 79 | #define fixed2int(x) ((int)((x) >> FIXED_PRECISION)) |
84 | #define fixed2float(x) (((float)(x).a) / ((float)(1 << FIXED_PRECISION))) | 80 | #define fixed2float(x) (((float)(x)) / ((float)(1 << FIXED_PRECISION))) |
85 | 81 | #define float2fixed(x) ((fixed)(x * (float)(1 << FIXED_PRECISION))) | |
86 | /* cast in tables confuse gcc -std=gnu99 */ | ||
87 | #define float2fixed_decl(x) {(fixed_data)(x * (float)(1 << FIXED_PRECISION))} | ||
88 | #define float2fixed(x) ((fixed)float2fixed_decl(x)) | ||
89 | 82 | ||
90 | /* I adapted these ones from the Rockbox fixed point library */ | 83 | /* I adapted these ones from the Rockbox fixed point library */ |
91 | #define fp_mul(x, y) \ | 84 | #define fp_mul(x, y) \ |
92 | ((fixed){(((int64_t)((x).a)) * ((int64_t)((y).a))) >> (FIXED_PRECISION)}) | 85 | ((fixed)((((int64_t)((x))) * ((int64_t)((y)))) >> (FIXED_PRECISION))) |
93 | #define fp_div(x, y) \ | 86 | #define fp_div(x, y) \ |
94 | ((fixed){(((int64_t)((x).a)) << (FIXED_PRECISION)) / ((int64_t)((y).a))}) | 87 | ((fixed)((((int64_t)((x))) << (FIXED_PRECISION)) / ((int64_t)((y))))) |
95 | /* Operators for fixed point */ | 88 | /* Operators for fixed point */ |
96 | #define fp_add(x, y) ((fixed){(x).a + (y).a}) | 89 | #define fp_add(x, y) ((fixed)((x) + (y))) |
97 | #define fp_sub(x, y) ((fixed){(x).a - (y).a}) | 90 | #define fp_sub(x, y) ((fixed)((x) - (y))) |
98 | #define fp_shl(x, y) ((fixed){(x).a << y}) | 91 | #define fp_shl(x, y) ((fixed)((x) << (y))) |
99 | #define fp_shr(x, y) ((fixed){(x).a >> y}) | 92 | #define fp_shr(x, y) ((fixed)((x) >> (y))) |
100 | #define fp_neg(x) ((fixed){-(x).a}) | 93 | #define fp_neg(x) ((fixed)(-(x))) |
101 | #define fp_gt(x, y) ((x).a > (y).a) | 94 | #define fp_gt(x, y) ((x) > (y)) |
102 | #define fp_gte(x, y) ((x).a >= (y).a) | 95 | #define fp_gte(x, y) ((x) >= (y)) |
103 | #define fp_lt(x, y) ((x).a < (y).a) | 96 | #define fp_lt(x, y) ((x) < (y)) |
104 | #define fp_lte(x, y) ((x).a <= (y).a) | 97 | #define fp_lte(x, y) ((x) <= (y)) |
105 | #define fp_sqr(x) fp_mul((x), (x)) | 98 | #define fp_sqr(x) fp_mul((x), (x)) |
106 | #define fp_equal(x, y) ((x).a == (y).a) | 99 | #define fp_equal(x, y) ((x) == (y)) |
107 | #define fp_round(x) (fixed2int(fp_add((x), float2fixed(0.5)))) | 100 | #define fp_round(x) (fixed2int(fp_add((x), float2fixed(0.5)))) |
108 | #define fp_data(x) ((x).a) | 101 | #define fp_data(x) (x) |
109 | #define fp_frac(x) (fp_sub((x), int2fixed(fixed2int(x)))) | 102 | #define fp_frac(x) (fp_sub((x), int2fixed(fixed2int(x)))) |
110 | #define FP_ZERO ((fixed){0}) | 103 | #define FP_ZERO ((fixed)0) |
111 | #define FP_LOW ((fixed){2}) | 104 | #define FP_LOW ((fixed)2) |
112 | 105 | ||
113 | /* Some defines for converting between period and frequency */ | 106 | /* Some defines for converting between period and frequency */ |
114 | 107 | ||
@@ -152,20 +145,20 @@ typedef struct | |||
152 | #define DEFAULT_YIN_THRESHOLD 5 /* 0.10 */ | 145 | #define DEFAULT_YIN_THRESHOLD 5 /* 0.10 */ |
153 | static const fixed yin_threshold_table[] IDATA_ATTR = | 146 | static const fixed yin_threshold_table[] IDATA_ATTR = |
154 | { | 147 | { |
155 | float2fixed_decl(0.01), | 148 | float2fixed(0.01), |
156 | float2fixed_decl(0.02), | 149 | float2fixed(0.02), |
157 | float2fixed_decl(0.03), | 150 | float2fixed(0.03), |
158 | float2fixed_decl(0.04), | 151 | float2fixed(0.04), |
159 | float2fixed_decl(0.05), | 152 | float2fixed(0.05), |
160 | float2fixed_decl(0.10), | 153 | float2fixed(0.10), |
161 | float2fixed_decl(0.15), | 154 | float2fixed(0.15), |
162 | float2fixed_decl(0.20), | 155 | float2fixed(0.20), |
163 | float2fixed_decl(0.25), | 156 | float2fixed(0.25), |
164 | float2fixed_decl(0.30), | 157 | float2fixed(0.30), |
165 | float2fixed_decl(0.35), | 158 | float2fixed(0.35), |
166 | float2fixed_decl(0.40), | 159 | float2fixed(0.40), |
167 | float2fixed_decl(0.45), | 160 | float2fixed(0.45), |
168 | float2fixed_decl(0.50), | 161 | float2fixed(0.50), |
169 | }; | 162 | }; |
170 | 163 | ||
171 | /* Structure for the reference frequency (frequency of A) | 164 | /* Structure for the reference frequency (frequency of A) |
@@ -180,17 +173,17 @@ static const struct | |||
180 | const fixed logratio; /* log2(factor) */ | 173 | const fixed logratio; /* log2(factor) */ |
181 | } freq_A[] = | 174 | } freq_A[] = |
182 | { | 175 | { |
183 | {435, float2fixed_decl(1.011363636), float2fixed_decl( 0.016301812)}, | 176 | {435, float2fixed(1.011363636), float2fixed( 0.016301812)}, |
184 | {436, float2fixed_decl(1.009090909), float2fixed_decl( 0.013056153)}, | 177 | {436, float2fixed(1.009090909), float2fixed( 0.013056153)}, |
185 | {437, float2fixed_decl(1.006818182), float2fixed_decl( 0.009803175)}, | 178 | {437, float2fixed(1.006818182), float2fixed( 0.009803175)}, |
186 | {438, float2fixed_decl(1.004545455), float2fixed_decl( 0.006542846)}, | 179 | {438, float2fixed(1.004545455), float2fixed( 0.006542846)}, |
187 | {439, float2fixed_decl(1.002272727), float2fixed_decl( 0.003275132)}, | 180 | {439, float2fixed(1.002272727), float2fixed( 0.003275132)}, |
188 | {440, float2fixed_decl(1.000000000), float2fixed_decl( 0.000000000)}, | 181 | {440, float2fixed(1.000000000), float2fixed( 0.000000000)}, |
189 | {441, float2fixed_decl(0.997727273), float2fixed_decl(-0.003282584)}, | 182 | {441, float2fixed(0.997727273), float2fixed(-0.003282584)}, |
190 | {442, float2fixed_decl(0.995454545), float2fixed_decl(-0.006572654)}, | 183 | {442, float2fixed(0.995454545), float2fixed(-0.006572654)}, |
191 | {443, float2fixed_decl(0.993181818), float2fixed_decl(-0.009870244)}, | 184 | {443, float2fixed(0.993181818), float2fixed(-0.009870244)}, |
192 | {444, float2fixed_decl(0.990909091), float2fixed_decl(-0.013175389)}, | 185 | {444, float2fixed(0.990909091), float2fixed(-0.013175389)}, |
193 | {445, float2fixed_decl(0.988636364), float2fixed_decl(-0.016488123)}, | 186 | {445, float2fixed(0.988636364), float2fixed(-0.016488123)}, |
194 | }; | 187 | }; |
195 | 188 | ||
196 | /* Index of the entry for 440 Hz in the table (default frequency for A) */ | 189 | /* Index of the entry for 440 Hz in the table (default frequency for A) */ |
@@ -268,18 +261,18 @@ static const struct | |||
268 | const fixed logfreq; /* log2(frequency) */ | 261 | const fixed logfreq; /* log2(frequency) */ |
269 | } notes[] = | 262 | } notes[] = |
270 | { | 263 | { |
271 | {"A" , float2fixed_decl(440.0000000f), float2fixed_decl(8.781359714f)}, | 264 | {"A" , float2fixed(440.0000000f), float2fixed(8.781359714f)}, |
272 | {"A#", float2fixed_decl(466.1637615f), float2fixed_decl(8.864693047f)}, | 265 | {"A#", float2fixed(466.1637615f), float2fixed(8.864693047f)}, |
273 | {"B" , float2fixed_decl(493.8833013f), float2fixed_decl(8.948026380f)}, | 266 | {"B" , float2fixed(493.8833013f), float2fixed(8.948026380f)}, |
274 | {"C" , float2fixed_decl(523.2511306f), float2fixed_decl(9.031359714f)}, | 267 | {"C" , float2fixed(523.2511306f), float2fixed(9.031359714f)}, |
275 | {"C#", float2fixed_decl(554.3652620f), float2fixed_decl(9.114693047f)}, | 268 | {"C#", float2fixed(554.3652620f), float2fixed(9.114693047f)}, |
276 | {"D" , float2fixed_decl(587.3295358f), float2fixed_decl(9.198026380f)}, | 269 | {"D" , float2fixed(587.3295358f), float2fixed(9.198026380f)}, |
277 | {"D#", float2fixed_decl(622.2539674f), float2fixed_decl(9.281359714f)}, | 270 | {"D#", float2fixed(622.2539674f), float2fixed(9.281359714f)}, |
278 | {"E" , float2fixed_decl(659.2551138f), float2fixed_decl(9.364693047f)}, | 271 | {"E" , float2fixed(659.2551138f), float2fixed(9.364693047f)}, |
279 | {"F" , float2fixed_decl(698.4564629f), float2fixed_decl(9.448026380f)}, | 272 | {"F" , float2fixed(698.4564629f), float2fixed(9.448026380f)}, |
280 | {"F#", float2fixed_decl(739.9888454f), float2fixed_decl(9.531359714f)}, | 273 | {"F#", float2fixed(739.9888454f), float2fixed(9.531359714f)}, |
281 | {"G" , float2fixed_decl(783.9908720f), float2fixed_decl(9.614693047f)}, | 274 | {"G" , float2fixed(783.9908720f), float2fixed(9.614693047f)}, |
282 | {"G#", float2fixed_decl(830.6093952f), float2fixed_decl(9.698026380f)}, | 275 | {"G#", float2fixed(830.6093952f), float2fixed(9.698026380f)}, |
283 | }; | 276 | }; |
284 | 277 | ||
285 | /* GUI */ | 278 | /* GUI */ |