diff options
Diffstat (limited to 'apps/plugins/pitch_detector.c')
-rw-r--r-- | apps/plugins/pitch_detector.c | 69 |
1 files changed, 28 insertions, 41 deletions
diff --git a/apps/plugins/pitch_detector.c b/apps/plugins/pitch_detector.c index c1d0cff50a..461b8562d8 100644 --- a/apps/plugins/pitch_detector.c +++ b/apps/plugins/pitch_detector.c | |||
@@ -258,37 +258,29 @@ audio_sample_type audio_data[BUFFER_SIZE]; | |||
258 | fixed yin_buffer[YIN_BUFFER_SIZE]; | 258 | fixed yin_buffer[YIN_BUFFER_SIZE]; |
259 | static int recording=0; | 259 | static int recording=0; |
260 | 260 | ||
261 | /* Frequencies of all the notes of the scale */ | 261 | /* Description of a note of scale */ |
262 | static const fixed freqs[12] = | 262 | struct note_entry |
263 | { | 263 | { |
264 | float2fixed(440.0f), /* A */ | 264 | const char *name; /* Name of the note, e.g. "A#" */ |
265 | float2fixed(466.1637615f), /* A# */ | 265 | const fixed freq; /* Note frequency */ |
266 | float2fixed(493.8833013f), /* etc... */ | 266 | const fixed logfreq; /* log2(frequency) */ |
267 | float2fixed(523.2511306f), | ||
268 | float2fixed(554.3652620f), | ||
269 | float2fixed(587.3295358f), | ||
270 | float2fixed(622.2539674f), | ||
271 | float2fixed(659.2551138f), | ||
272 | float2fixed(698.4564629f), | ||
273 | float2fixed(739.9888454f), | ||
274 | float2fixed(783.9908720f), | ||
275 | float2fixed(830.6093952f), | ||
276 | }; | 267 | }; |
277 | /* logarithm of all the notes of the scale */ | 268 | |
278 | static const fixed lfreqs[12] = | 269 | /* Notes within one (reference) scale */ |
270 | static const struct note_entry notes[] = | ||
279 | { | 271 | { |
280 | float2fixed(8.781359714f), | 272 | {"A" , float2fixed(440.0000000f), float2fixed(8.781359714f)}, |
281 | float2fixed(8.864693047f), | 273 | {"A#", float2fixed(466.1637615f), float2fixed(8.864693047f)}, |
282 | float2fixed(8.948026380f), | 274 | {"B" , float2fixed(493.8833013f), float2fixed(8.948026380f)}, |
283 | float2fixed(9.031359714f), | 275 | {"C" , float2fixed(523.2511306f), float2fixed(9.031359714f)}, |
284 | float2fixed(9.114693047f), | 276 | {"C#", float2fixed(554.3652620f), float2fixed(9.114693047f)}, |
285 | float2fixed(9.198026380f), | 277 | {"D" , float2fixed(587.3295358f), float2fixed(9.198026380f)}, |
286 | float2fixed(9.281359714f), | 278 | {"D#", float2fixed(622.2539674f), float2fixed(9.281359714f)}, |
287 | float2fixed(9.364693047f), | 279 | {"E" , float2fixed(659.2551138f), float2fixed(9.364693047f)}, |
288 | float2fixed(9.448026380f), | 280 | {"F" , float2fixed(698.4564629f), float2fixed(9.448026380f)}, |
289 | float2fixed(9.531359714f), | 281 | {"F#", float2fixed(739.9888454f), float2fixed(9.531359714f)}, |
290 | float2fixed(9.614693047f), | 282 | {"G" , float2fixed(783.9908720f), float2fixed(9.614693047f)}, |
291 | float2fixed(9.698026380f), | 283 | {"G#", float2fixed(830.6093952f), float2fixed(9.698026380f)}, |
292 | }; | 284 | }; |
293 | 285 | ||
294 | /* GUI */ | 286 | /* GUI */ |
@@ -297,12 +289,7 @@ static int font_w,font_h; | |||
297 | static int bar_x_0; | 289 | static int bar_x_0; |
298 | static int lbl_x_minus_50, lbl_x_minus_20, lbl_x_0, lbl_x_20, lbl_x_50; | 290 | static int lbl_x_minus_50, lbl_x_minus_20, lbl_x_0, lbl_x_20, lbl_x_50; |
299 | 291 | ||
300 | static const char *english_notes[] = {"A","A#","B","C","C#","D","D#","E", | 292 | /* Settings for the plugin */ |
301 | "F","F#","G","G#"}; | ||
302 | static const char **notes = english_notes; | ||
303 | |||
304 | /*Settings for the plugin */ | ||
305 | |||
306 | struct tuner_settings | 293 | struct tuner_settings |
307 | { | 294 | { |
308 | unsigned volume_threshold; | 295 | unsigned volume_threshold; |
@@ -747,23 +734,23 @@ void display_frequency (fixed freq) | |||
747 | /* This calculates a log freq offset for note A */ | 734 | /* This calculates a log freq offset for note A */ |
748 | /* Get the frequency to within the range of our reference table, */ | 735 | /* Get the frequency to within the range of our reference table, */ |
749 | /* i.e. into the right octave. */ | 736 | /* i.e. into the right octave. */ |
750 | while (fp_lt(lfreq, fp_sub(lfreqs[0], fp_shr(LOG_D_NOTE, 1)))) | 737 | while (fp_lt(lfreq, fp_sub(notes[0].logfreq, fp_shr(LOG_D_NOTE, 1)))) |
751 | lfreq = fp_add(lfreq, LOG_2); | 738 | lfreq = fp_add(lfreq, LOG_2); |
752 | while (fp_gte(lfreq, fp_sub(fp_add(lfreqs[0], LOG_2), | 739 | while (fp_gte(lfreq, fp_sub(fp_add(notes[0].logfreq, LOG_2), |
753 | fp_shr(LOG_D_NOTE, 1)))) | 740 | fp_shr(LOG_D_NOTE, 1)))) |
754 | lfreq = fp_sub(lfreq, LOG_2); | 741 | lfreq = fp_sub(lfreq, LOG_2); |
755 | mldf = LOG_D_NOTE; | 742 | mldf = LOG_D_NOTE; |
756 | for (i=0; i<12; i++) | 743 | for (i=0; i<12; i++) |
757 | { | 744 | { |
758 | ldf = fp_gt(fp_sub(lfreq,lfreqs[i]), FP_ZERO) ? | 745 | ldf = fp_gt(fp_sub(lfreq,notes[i].logfreq), FP_ZERO) ? |
759 | fp_sub(lfreq,lfreqs[i]) : fp_neg(fp_sub(lfreq,lfreqs[i])); | 746 | fp_sub(lfreq,notes[i].logfreq) : fp_neg(fp_sub(lfreq,notes[i].logfreq)); |
760 | if (fp_lt(ldf, mldf)) | 747 | if (fp_lt(ldf, mldf)) |
761 | { | 748 | { |
762 | mldf = ldf; | 749 | mldf = ldf; |
763 | note = i; | 750 | note = i; |
764 | } | 751 | } |
765 | } | 752 | } |
766 | nfreq = freqs[note]; | 753 | nfreq = notes[note].freq; |
767 | while (fp_gt(fp_div(nfreq, freq), D_NOTE_SQRT)) | 754 | while (fp_gt(fp_div(nfreq, freq), D_NOTE_SQRT)) |
768 | nfreq = fp_shr(nfreq, 1); | 755 | nfreq = fp_shr(nfreq, 1); |
769 | while (fp_gt(fp_div(freq, nfreq), D_NOTE_SQRT)) | 756 | while (fp_gt(fp_div(freq, nfreq), D_NOTE_SQRT)) |
@@ -777,11 +764,11 @@ void display_frequency (fixed freq) | |||
777 | draw_bar(ldf); /* The red bar */ | 764 | draw_bar(ldf); /* The red bar */ |
778 | if(fp_round(freq) != 0) | 765 | if(fp_round(freq) != 0) |
779 | { | 766 | { |
780 | draw_note(notes[note]); | 767 | draw_note(notes[note].name); |
781 | if(tuner_settings.display_hz) | 768 | if(tuner_settings.display_hz) |
782 | { | 769 | { |
783 | rb->snprintf(str_buf,30, "%s : %d cents (%d.%02dHz)", | 770 | rb->snprintf(str_buf,30, "%s : %d cents (%d.%02dHz)", |
784 | notes[note], fp_round(ldf) ,fixed2int(orig_freq), | 771 | notes[note].name, fp_round(ldf) ,fixed2int(orig_freq), |
785 | fp_round(fp_mul(fp_frac(orig_freq), | 772 | fp_round(fp_mul(fp_frac(orig_freq), |
786 | int2fixed(DISPLAY_HZ_PRECISION)))); | 773 | int2fixed(DISPLAY_HZ_PRECISION)))); |
787 | print_str(str_buf); | 774 | print_str(str_buf); |