Index: apps/plugins/pitch_detector.c =================================================================== --- apps/plugins/pitch_detector.c (Revision 22700) +++ apps/plugins/pitch_detector.c (Arbeitskopie) @@ -177,6 +177,43 @@ float2fixed(0.45), float2fixed(0.50), }; + +/* Index of the entry for 440 Hz in the tables (default frequency for A) */ +#define DEFAULT_REFERENCE 5 /* 440 */ + +/* Frequency of A normalised to 440Hz, i.e. xxx/440 */ +const fixed freq_A [] = +{ + float2fixed(0.988636364), /* 435 Hz */ + float2fixed(0.990909091), + float2fixed(0.993181818), + float2fixed(0.995454545), + float2fixed(0.997727273), + float2fixed(1), /* 440 Hz */ + float2fixed(1.002272727), + float2fixed(1.004545455), + float2fixed(1.006818182), + float2fixed(1.009090909), + float2fixed(1.011363636), /* 445 Hz */ +}; + +/* Log (to base 2) of Frequency of A normalised to 440Hz, + * i.e. log2(xxx/440) + */ +const fixed lfreq_A [] = +{ + float2fixed(-0.016488123), /* 435 Hz */ + float2fixed(-0.013175389), + float2fixed(-0.009870244), + float2fixed(-0.006572654), + float2fixed(-0.003282584), + float2fixed( 0), /* 440 Hz reference standard A */ + float2fixed( 0.003275132), + float2fixed( 0.006542846), + float2fixed( 0.009803175), + float2fixed( 0.013056153), + float2fixed( 0.016301812), /* 445 Hz */ +}; /* How loud the audio has to be to start displaying pitch */ /* Must be between 0 and 100 */ @@ -279,6 +316,7 @@ unsigned sample_size; unsigned lowest_freq; unsigned yin_threshold; + unsigned freq_A; /* Index of the frequency of A */ bool use_sharps; bool display_hz; } tuner_settings; @@ -325,6 +363,7 @@ settings->sample_size = BUFFER_SIZE; settings->lowest_freq = period2freq(BUFFER_SIZE / 4); settings->yin_threshold = DEFAULT_YIN_THRESHOLD; + settings->freq_A = DEFAULT_REFERENCE; settings->use_sharps = true; settings->display_hz = false; } @@ -426,6 +465,23 @@ { "0.50", -1 }, }; +/* These are the display frequency options for note A */ + +static const struct opt_items freq_A_text[] = +{ + { "435", -1 }, + { "436", -1 }, + { "437", -1 }, + { "438", -1 }, + { "439", -1 }, + { "440", -1 }, + { "441", -1 }, + { "442", -1 }, + { "443", -1 }, + { "444", -1 }, + { "445", -1 }, +}; + static const struct opt_items accidental_text[] = { { "Flat", -1 }, @@ -469,6 +525,7 @@ "Accidentals", "Display Frequency (Hz)", "Reset Settings", + "Frequency of A (Hz)", "Quit"); while(!done) @@ -519,13 +576,20 @@ BOOL, noyes_text, 2, tuner_settings_reset_query); break; case 8: + rb->set_option( + "Frequency of A (Hz)", + &tuner_settings.freq_A, + INT, freq_A_text, + sizeof(freq_A_text) / sizeof(freq_A_text[0]), + NULL); + break; + case 9: exit_tuner = true; case 0: default: /* Return to the tuner */ done = true; break; - } } return(exit_tuner); @@ -650,11 +714,11 @@ int x; #ifdef HAVE_LCD_COLOR - rb->lcd_set_foreground(LCD_RGBPACK(255,255,255)); /* Color screens */ + rb->lcd_set_foreground(LCD_RGBPACK(255,255,255)); /* Color screens */ #elif LCD_DEPTH > 1 rb->lcd_set_foreground(LCD_BLACK); /* Greyscale screens */ #else - rb->lcd_set_foreground(LCD_BLACK); /* Black and white screens */ + rb->lcd_set_foreground(LCD_BLACK); /* Black and white screens */ #endif rb->lcd_hline(0,LCD_WIDTH-1, BAR_HLINE_Y); @@ -708,25 +772,29 @@ if (fp_lt(freq, FP_LOW)) freq = FP_LOW; lfreq = log(freq); - - /* Get the frequency to within the range of our reference table */ - while (fp_lt(lfreq, fp_sub(lfreqs[0], fp_shr(LOG_D_NOTE, 1)))) + fixed lfreq_A_offset; + /* This calculates a log freq offset for note A */ + lfreq_A_offset = lfreq_A[tuner_settings.freq_A]; + /* Get the frequency to within the range of our freq_A table */ + while (fp_lt(lfreq, fp_sub(fp_add(lfreqs[0],lfreq_A_offset), fp_shr(LOG_D_NOTE, 1)))) lfreq = fp_add(lfreq, LOG_2); - while (fp_gte(lfreq, fp_sub(fp_add(lfreqs[0], LOG_2), + while (fp_gte(lfreq, fp_sub(fp_add(fp_add(lfreqs[0],lfreq_A_offset), LOG_2), fp_shr(LOG_D_NOTE, 1)))) lfreq = fp_sub(lfreq, LOG_2); mldf = LOG_D_NOTE; - for (i=0; i<12; i++) + for (i=0; i<12; i++) { - ldf = fp_gt(fp_sub(lfreq,lfreqs[i]), FP_ZERO) ? - fp_sub(lfreq,lfreqs[i]) : fp_neg(fp_sub(lfreq,lfreqs[i])); - if (fp_lt(ldf, mldf)) + ldf = fp_gt(fp_sub(lfreq,fp_add(lfreqs[i],lfreq_A_offset)), FP_ZERO) ? + fp_sub(lfreq,fp_add(lfreqs[i],lfreq_A_offset)) : + fp_neg(fp_sub(lfreq,fp_add(lfreqs[i],lfreq_A_offset))); + if (fp_lt(ldf, mldf)) { - mldf = ldf; - note = i; + mldf = ldf; + note = i; } } - nfreq = freqs[note]; + /* this scales the calulated note by the freq offset for note A */ + nfreq = (fp_mul(freqs[note],freq_A[tuner_settings.freq_A])); while (fp_gt(fp_div(nfreq, freq), D_NOTE_SQRT)) nfreq = fp_shr(nfreq, 1); while (fp_gt(fp_div(freq, nfreq), D_NOTE_SQRT)) @@ -734,7 +802,7 @@ nfreq = fp_shl(nfreq, 1); } - ldf=fp_mul(int2fixed(1200), log(fp_div(freq,nfreq))); + ldf = fp_mul(int2fixed(1200), log(fp_div(freq,nfreq))); rb->lcd_clear_display(); draw_bar(ldf); /* The red bar */