Initial 800x480 cabbiev2 port, based on 480x800x16 one
[kugel-rb.git] / apps / plugins / pitch_detector.c
blobed3ff6413daf478517e3548d130b1dd33716f0e1
1 /**************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2008 Lechner Michael / smoking gnu
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 * ----------------------------------------------------------------------------
20 * INTRODUCTION:
21 * OK, this is an attempt to write an instrument tuner for rockbox.
22 * It uses a Schmitt trigger algorithm, which I copied from
23 * tuneit [ (c) 2004 Mario Lang <mlang@delysid.org> ], for detecting the
24 * fundamental freqency of a sound. A FFT algorithm would be more accurate
25 * but also much slower.
27 * TODO:
28 * - Adapt the Yin FFT algorithm, which would reduce complexity from O(n^2)
29 * to O(nlogn), theoretically reducing latency by a factor of ~10. -David
31 * MAJOR CHANGES:
32 * 08.03.2008 Started coding
33 * 21.03.2008 Pitch detection works more or less
34 * Button definitions for most targets added
35 * 02.04.2008 Proper GUI added
36 * Todo, Major Changes and Current Limitations added
37 * 08.19.2009 Brought the code up to date with current plugin standards
38 * Made it work more nicely with color, BW and grayscale
39 * Changed pitch detection to use the Yin algorithm (better
40 * detection, but slower -- would be ~4x faster with
41 * fixed point math, I think). Code was poached from the
42 * Aubio sound processing library (aubio.org). -David
43 * 08.31.2009 Lots of changes:
44 * Added a menu to tweak settings
45 * Converted everything to fixed point (greatly improving
46 * latency)
47 * Improved the display
48 * Improved efficiency with judicious use of cpu_boost, the
49 * backlight, and volume detection to limit unneeded
50 * calculation
51 * Fixed a problem that caused an octave-off error
52 * -David
53 * 05.14.2010 Multibuffer continuous recording with two buffers
56 * CURRENT LIMITATIONS:
57 * - No gapless recording. Strictly speaking true gappless isn't possible,
58 * since the algorithm takes longer to calculate than the length of the
59 * sample, but latency could be improved a bit with proper use of the DMA
60 * recording functions.
61 * - Due to how the Yin algorithm works, latency is higher for lower
62 * frequencies.
65 #include "plugin.h"
66 #include "lib/pluginlib_actions.h"
67 #include "lib/picture.h"
68 #include "lib/helper.h"
69 #include "pluginbitmaps/pitch_notes.h"
72 /* Some fixed point calculation stuff */
73 typedef int32_t fixed;
74 #define FIXED_PRECISION 18
75 #define FP_MAX ((fixed) {0x7fffffff})
76 #define FP_MIN ((fixed) {-0x80000000})
77 #define int2fixed(x) ((fixed)((x) << FIXED_PRECISION))
78 #define int2mantissa(x) ((fixed)(x))
79 #define fixed2int(x) ((int)((x) >> FIXED_PRECISION))
80 #define fixed2float(x) (((float)(x)) / ((float)(1 << FIXED_PRECISION)))
81 #define float2fixed(x) ((fixed)(x * (float)(1 << FIXED_PRECISION)))
83 /* I adapted these ones from the Rockbox fixed point library */
84 #define fp_mul(x, y) \
85 ((fixed)((((int64_t)((x))) * ((int64_t)((y)))) >> (FIXED_PRECISION)))
86 #define fp_div(x, y) \
87 ((fixed)((((int64_t)((x))) << (FIXED_PRECISION)) / ((int64_t)((y)))))
88 /* Operators for fixed point */
89 #define fp_add(x, y) ((fixed)((x) + (y)))
90 #define fp_sub(x, y) ((fixed)((x) - (y)))
91 #define fp_shl(x, y) ((fixed)((x) << (y)))
92 #define fp_shr(x, y) ((fixed)((x) >> (y)))
93 #define fp_neg(x) ((fixed)(-(x)))
94 #define fp_gt(x, y) ((x) > (y))
95 #define fp_gte(x, y) ((x) >= (y))
96 #define fp_lt(x, y) ((x) < (y))
97 #define fp_lte(x, y) ((x) <= (y))
98 #define fp_sqr(x) fp_mul((x), (x))
99 #define fp_equal(x, y) ((x) == (y))
100 #define fp_round(x) (fixed2int(fp_add((x), float2fixed(0.5))))
101 #define fp_data(x) (x)
102 #define fp_frac(x) (fp_sub((x), int2fixed(fixed2int(x))))
103 #define FP_ZERO ((fixed)0)
104 #define FP_LOW ((fixed)2)
106 /* Some defines for converting between period and frequency */
108 /* I introduce some divisors in this because the fixed point */
109 /* variables aren't big enough to hold higher than a certain */
110 /* value. This loses a bit of precision but it means we */
111 /* don't have to use 32.32 variables (yikes). */
112 /* With an 18-bit decimal precision, the max value in the */
113 /* integer part is 8192. Divide 44100 by 7 and it'll fit in */
114 /* that variable. */
115 #define fp_period2freq(x) fp_div(int2fixed(sample_rate / 7), \
116 fp_div((x),int2fixed(7)))
117 #define fp_freq2period(x) fp_period2freq(x)
118 #define period2freq(x) (sample_rate / (x))
119 #define freq2period(x) period2freq(x)
121 #define sqr(x) ((x)*(x))
123 /* Some constants for tuning */
124 #define A_FREQ float2fixed(440.0f)
125 #define D_NOTE float2fixed(1.059463094359f)
126 #define LOG_D_NOTE float2fixed(1.0f/12.0f)
127 #define D_NOTE_SQRT float2fixed(1.029302236643f)
128 #define LOG_2 float2fixed(1.0f)
130 /* The recording buffer size */
131 /* This is how much is sampled at a time. */
132 /* It also determines latency -- if BUFFER_SIZE == sample_rate then */
133 /* there'll be one sample per second, or a latency of one second. */
134 /* Furthermore, the lowest detectable frequency will be about twice */
135 /* the number of reads per second */
136 /* If we ever switch to Yin FFT algorithm then this needs to be
137 a power of 2 */
138 #define BUFFER_SIZE 4096
139 #define SAMPLE_SIZE 4096
140 #define SAMPLE_SIZE_MIN 1024
141 #define YIN_BUFFER_SIZE (BUFFER_SIZE / 4)
143 #define LCD_FACTOR (fp_div(int2fixed(LCD_WIDTH), int2fixed(100)))
144 /* The threshold for the YIN algorithm */
145 #define DEFAULT_YIN_THRESHOLD 5 /* 0.10 */
146 static const fixed yin_threshold_table[] IDATA_ATTR =
148 float2fixed(0.01),
149 float2fixed(0.02),
150 float2fixed(0.03),
151 float2fixed(0.04),
152 float2fixed(0.05),
153 float2fixed(0.10),
154 float2fixed(0.15),
155 float2fixed(0.20),
156 float2fixed(0.25),
157 float2fixed(0.30),
158 float2fixed(0.35),
159 float2fixed(0.40),
160 float2fixed(0.45),
161 float2fixed(0.50),
164 /* Structure for the reference frequency (frequency of A)
165 * It's used for scaling the frequency before finding out
166 * the note. The frequency is scaled in a way that the main
167 * algorithm can assume the frequency of A to be 440 Hz.
169 static const struct
171 const int frequency; /* Frequency in Hz */
172 const fixed ratio; /* 440/frequency */
173 const fixed logratio; /* log2(factor) */
174 } freq_A[] =
176 {435, float2fixed(1.011363636), float2fixed( 0.016301812)},
177 {436, float2fixed(1.009090909), float2fixed( 0.013056153)},
178 {437, float2fixed(1.006818182), float2fixed( 0.009803175)},
179 {438, float2fixed(1.004545455), float2fixed( 0.006542846)},
180 {439, float2fixed(1.002272727), float2fixed( 0.003275132)},
181 {440, float2fixed(1.000000000), float2fixed( 0.000000000)},
182 {441, float2fixed(0.997727273), float2fixed(-0.003282584)},
183 {442, float2fixed(0.995454545), float2fixed(-0.006572654)},
184 {443, float2fixed(0.993181818), float2fixed(-0.009870244)},
185 {444, float2fixed(0.990909091), float2fixed(-0.013175389)},
186 {445, float2fixed(0.988636364), float2fixed(-0.016488123)},
189 /* Index of the entry for 440 Hz in the table (default frequency for A) */
190 #define DEFAULT_FREQ_A 5
191 #define NUM_FREQ_A (sizeof(freq_A)/sizeof(freq_A[0]))
193 /* How loud the audio has to be to start displaying pitch */
194 /* Must be between 0 and 100 */
195 #define VOLUME_THRESHOLD (50)
197 /* Change to AUDIO_SRC_LINEIN if you want to record from line-in */
198 #ifdef HAVE_MIC_IN
199 #define INPUT_TYPE AUDIO_SRC_MIC
200 #else
201 #define INPUT_TYPE AUDIO_SRC_LINEIN
202 #endif
204 /* How many decimal places to display for the Hz value */
205 #define DISPLAY_HZ_PRECISION 100
207 /* Where to put the various GUI elements */
208 static int note_y;
209 static int bar_grad_y;
210 #define LCD_RES_MIN (LCD_HEIGHT < LCD_WIDTH ? LCD_HEIGHT : LCD_WIDTH)
211 #define BAR_PADDING (LCD_RES_MIN / 32)
212 #define BAR_Y (LCD_HEIGHT * 3 / 4)
213 #define BAR_HEIGHT (LCD_RES_MIN / 4 - BAR_PADDING)
214 #define BAR_HLINE_Y (BAR_Y - BAR_PADDING)
215 #define BAR_HLINE_Y2 (BAR_Y + BAR_HEIGHT + BAR_PADDING - 1)
216 #define HZ_Y 0
217 #define GRADUATION 10 /* Subdivisions of the whole 100-cent scale */
219 /* Bitmaps for drawing the note names. These need to have height
220 <= (bar_grad_y - note_y), or 15/32 * LCD_HEIGHT
222 #define NUM_NOTE_IMAGES 9
223 #define NOTE_INDEX_A 0
224 #define NOTE_INDEX_B 1
225 #define NOTE_INDEX_C 2
226 #define NOTE_INDEX_D 3
227 #define NOTE_INDEX_E 4
228 #define NOTE_INDEX_F 5
229 #define NOTE_INDEX_G 6
230 #define NOTE_INDEX_SHARP 7
231 #define NOTE_INDEX_FLAT 8
233 static const struct picture note_bitmaps =
235 pitch_notes,
236 BMPWIDTH_pitch_notes,
237 BMPHEIGHT_pitch_notes,
238 BMPHEIGHT_pitch_notes/NUM_NOTE_IMAGES
242 static unsigned int sample_rate;
243 static int audio_head = 0; /* which of the two buffers to use? */
244 static volatile int audio_tail = 0; /* which of the two buffers to record? */
245 /* It's stereo, so make the buffer twice as big */
246 #ifndef SIMULATOR
247 static int16_t audio_data[2][BUFFER_SIZE] MEM_ALIGN_ATTR;
248 static fixed yin_buffer[YIN_BUFFER_SIZE] IBSS_ATTR;
249 #ifdef PLUGIN_USE_IRAM
250 static int16_t iram_audio_data[BUFFER_SIZE] IBSS_ATTR;
251 #else
252 #define iram_audio_data audio_data[audio_head]
253 #endif
254 #endif
256 /* Notes within one (reference) scale */
257 static const struct
259 const char *name; /* Name of the note, e.g. "A#" */
260 const fixed freq; /* Note frequency, Hz */
261 const fixed logfreq; /* log2(frequency) */
262 } notes[] =
264 {"A" , float2fixed(440.0000000f), float2fixed(8.781359714f)},
265 {"A#", float2fixed(466.1637615f), float2fixed(8.864693047f)},
266 {"B" , float2fixed(493.8833013f), float2fixed(8.948026380f)},
267 {"C" , float2fixed(523.2511306f), float2fixed(9.031359714f)},
268 {"C#", float2fixed(554.3652620f), float2fixed(9.114693047f)},
269 {"D" , float2fixed(587.3295358f), float2fixed(9.198026380f)},
270 {"D#", float2fixed(622.2539674f), float2fixed(9.281359714f)},
271 {"E" , float2fixed(659.2551138f), float2fixed(9.364693047f)},
272 {"F" , float2fixed(698.4564629f), float2fixed(9.448026380f)},
273 {"F#", float2fixed(739.9888454f), float2fixed(9.531359714f)},
274 {"G" , float2fixed(783.9908720f), float2fixed(9.614693047f)},
275 {"G#", float2fixed(830.6093952f), float2fixed(9.698026380f)},
278 /* GUI */
279 #if LCD_DEPTH > 1
280 static unsigned front_color;
281 #endif
282 static int font_w,font_h;
283 static int bar_x_0;
284 static int lbl_x_minus_50, lbl_x_minus_20, lbl_x_0, lbl_x_20, lbl_x_50;
286 /* Settings for the plugin */
287 static struct tuner_settings
289 unsigned volume_threshold;
290 unsigned record_gain;
291 unsigned sample_size;
292 unsigned lowest_freq;
293 unsigned yin_threshold;
294 int freq_A; /* Index of the frequency of A */
295 bool use_sharps;
296 bool display_hz;
297 } settings;
299 /*=================================================================*/
300 /* Settings loading and saving(adapted from the clock plugin) */
301 /*=================================================================*/
303 #define SETTINGS_FILENAME PLUGIN_APPS_DIR "/.pitch_settings"
305 /* The settings as they exist on the hard disk, so that
306 * we can know at saving time if changes have been made */
307 static struct tuner_settings hdd_settings;
309 /*---------------------------------------------------------------------*/
311 static bool settings_needs_saving(void)
313 return(rb->memcmp(&settings, &hdd_settings, sizeof(settings)));
316 /*---------------------------------------------------------------------*/
318 static void tuner_settings_reset(void)
320 settings = (struct tuner_settings) {
321 .volume_threshold = VOLUME_THRESHOLD,
322 .record_gain = rb->global_settings->rec_mic_gain,
323 .sample_size = BUFFER_SIZE,
324 .lowest_freq = period2freq(BUFFER_SIZE / 4),
325 .yin_threshold = DEFAULT_YIN_THRESHOLD,
326 .freq_A = DEFAULT_FREQ_A,
327 .use_sharps = true,
328 .display_hz = false,
332 /*---------------------------------------------------------------------*/
334 static void load_settings(void)
336 int fd = rb->open(SETTINGS_FILENAME, O_RDONLY);
337 if(fd < 0){ /* file doesn't exist */
338 /* Initializes the settings with default values at least */
339 tuner_settings_reset();
340 return;
343 /* basic consistency check */
344 if(rb->filesize(fd) == sizeof(settings)){
345 rb->read(fd, &settings, sizeof(settings));
346 rb->memcpy(&hdd_settings, &settings, sizeof(settings));
348 else{
349 tuner_settings_reset();
352 rb->close(fd);
355 /*---------------------------------------------------------------------*/
357 static void save_settings(void)
359 if(!settings_needs_saving())
360 return;
362 int fd = rb->creat(SETTINGS_FILENAME, 0666);
363 if(fd >= 0){ /* does file exist? */
364 rb->write (fd, &settings, sizeof(settings));
365 rb->close(fd);
369 /*=================================================================*/
370 /* MENU */
371 /*=================================================================*/
373 /* Keymaps */
374 const struct button_mapping* plugin_contexts[]={
375 pla_main_ctx,
376 #if NB_SCREENS == 2
377 pla_remote_ctx,
378 #endif
380 #define PLA_ARRAY_COUNT sizeof(plugin_contexts)/sizeof(plugin_contexts[0])
382 /* Option strings */
384 /* This has to match yin_threshold_table */
385 static const struct opt_items yin_threshold_text[] =
387 { "0.01", -1 },
388 { "0.02", -1 },
389 { "0.03", -1 },
390 { "0.04", -1 },
391 { "0.05", -1 },
392 { "0.10", -1 },
393 { "0.15", -1 },
394 { "0.20", -1 },
395 { "0.25", -1 },
396 { "0.30", -1 },
397 { "0.35", -1 },
398 { "0.40", -1 },
399 { "0.45", -1 },
400 { "0.50", -1 },
403 static const struct opt_items accidental_text[] =
405 { "Flat", -1 },
406 { "Sharp", -1 },
409 static void set_min_freq(int new_freq)
411 settings.sample_size = freq2period(new_freq) * 4;
413 /* clamp the sample size between min and max */
414 if(settings.sample_size <= SAMPLE_SIZE_MIN)
415 settings.sample_size = SAMPLE_SIZE_MIN;
416 else if(settings.sample_size >= BUFFER_SIZE)
417 settings.sample_size = BUFFER_SIZE;
419 /* sample size must be divisible by 4 - round up */
420 settings.sample_size = (settings.sample_size + 3) & ~3;
423 static bool main_menu(void)
425 int selection = 0;
426 bool done = false;
427 bool exit_tuner = false;
428 int choice;
429 int freq_val;
430 bool reset;
432 backlight_use_settings();
433 #ifdef HAVE_SCHEDULER_BOOSTCTRL
434 rb->cancel_cpu_boost();
435 #endif
437 MENUITEM_STRINGLIST(menu,"Tuner Settings",NULL,
438 "Return to Tuner",
439 "Volume Threshold",
440 "Listening Volume",
441 "Lowest Frequency",
442 "Algorithm Pickiness",
443 "Accidentals",
444 "Display Frequency (Hz)",
445 "Frequency of A (Hz)",
446 "Reset Settings",
447 "Quit");
449 while(!done)
451 choice = rb->do_menu(&menu, &selection, NULL, false);
452 switch(choice)
454 case 1:
455 rb->set_int("Volume Threshold", "%", UNIT_INT,
456 &settings.volume_threshold,
457 NULL, 5, 5, 95, NULL);
458 break;
459 case 2:
460 rb->set_int("Listening Volume", "%", UNIT_INT,
461 &settings.record_gain,
462 NULL, 1, rb->sound_min(SOUND_MIC_GAIN),
463 rb->sound_max(SOUND_MIC_GAIN), NULL);
464 break;
465 case 3:
466 rb->set_int("Lowest Frequency", "Hz", UNIT_INT,
467 &settings.lowest_freq, set_min_freq, 1,
468 /* Range depends on the size of the buffer */
469 sample_rate / (BUFFER_SIZE / 4),
470 sample_rate / (SAMPLE_SIZE_MIN / 4), NULL);
471 break;
472 case 4:
473 rb->set_option(
474 "Algorithm Pickiness (Lower -> more discriminating)",
475 &settings.yin_threshold,
476 INT, yin_threshold_text,
477 sizeof(yin_threshold_text) / sizeof(yin_threshold_text[0]),
478 NULL);
479 break;
480 case 5:
481 rb->set_option("Display Accidentals As",
482 &settings.use_sharps,
483 BOOL, accidental_text, 2, NULL);
484 break;
485 case 6:
486 rb->set_bool("Display Frequency (Hz)",
487 &settings.display_hz);
488 break;
489 case 7:
490 freq_val = freq_A[settings.freq_A].frequency;
491 rb->set_int("Frequency of A (Hz)",
492 "Hz", UNIT_INT, &freq_val, NULL,
493 1, freq_A[0].frequency, freq_A[NUM_FREQ_A-1].frequency,
494 NULL);
495 settings.freq_A = freq_val - freq_A[0].frequency;
496 break;
497 case 8:
498 reset = false;
499 rb->set_bool("Reset Tuner Settings?", &reset);
500 if (reset)
501 tuner_settings_reset();
502 break;
503 case 9:
504 exit_tuner = true;
505 done = true;
506 break;
507 case 0:
508 default:
509 /* Return to the tuner */
510 done = true;
511 break;
515 backlight_force_on();
516 return exit_tuner;
519 /*=================================================================*/
520 /* Binary Log */
521 /*=================================================================*/
523 /* Fixed-point log base 2*/
524 /* Adapted from python code at
525 http://en.wikipedia.org/wiki/Binary_logarithm#Algorithm
527 static fixed log(fixed inp)
529 fixed x = inp;
530 fixed fp = int2fixed(1);
531 fixed res = int2fixed(0);
533 if(fp_lte(x, FP_ZERO))
535 return FP_MIN;
538 /* Integer part*/
539 /* while x<1 */
540 while(fp_lt(x, int2fixed(1)))
542 res = fp_sub(res, int2fixed(1));
543 x = fp_shl(x, 1);
545 /* while x>=2 */
546 while(fp_gte(x, int2fixed(2)))
548 res = fp_add(res, int2fixed(1));
549 x = fp_shr(x, 1);
552 /* Fractional part */
553 /* while fp > 0 */
554 while(fp_gt(fp, FP_ZERO))
556 fp = fp_shr(fp, 1);
557 x = fp_mul(x, x);
558 /* if x >= 2 */
559 if(fp_gte(x, int2fixed(2)))
561 x = fp_shr(x, 1);
562 res = fp_add(res, fp);
566 return res;
569 /*=================================================================*/
570 /* GUI Stuff */
571 /*=================================================================*/
573 /* Draw the note bitmap */
574 static void draw_note(const char *note)
576 int i;
577 int note_x = (LCD_WIDTH - BMPWIDTH_pitch_notes) / 2;
578 int accidental_index = NOTE_INDEX_SHARP;
580 i = note[0]-'A';
582 if(note[1] == '#')
584 if(!(settings.use_sharps))
586 i = (i + 1) % 7;
587 accidental_index = NOTE_INDEX_FLAT;
590 vertical_picture_draw_sprite(rb->screens[0],
591 &note_bitmaps,
592 accidental_index,
593 LCD_WIDTH / 2,
594 note_y);
595 note_x = LCD_WIDTH / 2 - BMPWIDTH_pitch_notes;
598 vertical_picture_draw_sprite(rb->screens[0], &note_bitmaps, i,
599 note_x,
600 note_y);
603 /* Draw the red bar and the white lines */
604 static void draw_bar(fixed wrong_by_cents)
606 unsigned n;
607 int x;
609 #ifdef HAVE_LCD_COLOR
610 rb->lcd_set_foreground(LCD_RGBPACK(255,255,255)); /* Color screens */
611 #elif LCD_DEPTH > 1
612 rb->lcd_set_foreground(LCD_BLACK); /* Greyscale screens */
613 #endif
615 rb->lcd_hline(0,LCD_WIDTH-1, BAR_HLINE_Y);
616 rb->lcd_hline(0,LCD_WIDTH-1, BAR_HLINE_Y2);
618 /* Draw graduation lines on the off-by readout */
619 for(n = 0; n <= GRADUATION; n++)
621 x = (LCD_WIDTH * n + GRADUATION / 2) / GRADUATION;
622 if (x >= LCD_WIDTH)
623 x = LCD_WIDTH - 1;
624 rb->lcd_vline(x, BAR_HLINE_Y, BAR_HLINE_Y2);
627 #if LCD_DEPTH > 1
628 rb->lcd_set_foreground(front_color);
629 #endif
630 rb->lcd_putsxyf(lbl_x_minus_50 ,bar_grad_y, "%d", -50);
631 rb->lcd_putsxyf(lbl_x_minus_20 ,bar_grad_y, "%d", -20);
632 rb->lcd_putsxyf(lbl_x_0 ,bar_grad_y, "%d", 0);
633 rb->lcd_putsxyf(lbl_x_20 ,bar_grad_y, "%d", 20);
634 rb->lcd_putsxyf(lbl_x_50 ,bar_grad_y, "%d", 50);
636 #ifdef HAVE_LCD_COLOR
637 rb->lcd_set_foreground(LCD_RGBPACK(255,0,0)); /* Color screens */
638 #elif LCD_DEPTH > 1
639 rb->lcd_set_foreground(LCD_DARKGRAY); /* Greyscale screens */
640 #endif
642 if (fp_gt(wrong_by_cents, FP_ZERO))
644 rb->lcd_fillrect(bar_x_0, BAR_Y,
645 fixed2int(fp_mul(wrong_by_cents, LCD_FACTOR)), BAR_HEIGHT);
647 else
649 rb->lcd_fillrect(bar_x_0 + fixed2int(fp_mul(wrong_by_cents,LCD_FACTOR)),
650 BAR_Y,
651 fixed2int(fp_mul(wrong_by_cents, LCD_FACTOR)) * -1,
652 BAR_HEIGHT);
656 /* Calculate how wrong the note is and draw the GUI */
657 static void display_frequency (fixed freq)
659 fixed ldf, mldf;
660 fixed lfreq, nfreq;
661 fixed orig_freq;
662 int i, note = 0;
664 if (fp_lt(freq, FP_LOW))
665 freq = FP_LOW;
667 /* We calculate the frequency and its log as if */
668 /* the reference frequency of A were 440 Hz. */
669 orig_freq = freq;
670 lfreq = fp_add(log(freq), freq_A[settings.freq_A].logratio);
671 freq = fp_mul(freq, freq_A[settings.freq_A].ratio);
673 /* This calculates a log freq offset for note A */
674 /* Get the frequency to within the range of our reference table, */
675 /* i.e. into the right octave. */
676 while (fp_lt(lfreq, fp_sub(notes[0].logfreq, fp_shr(LOG_D_NOTE, 1))))
677 lfreq = fp_add(lfreq, LOG_2);
678 while (fp_gte(lfreq, fp_sub(fp_add(notes[0].logfreq, LOG_2),
679 fp_shr(LOG_D_NOTE, 1))))
680 lfreq = fp_sub(lfreq, LOG_2);
681 mldf = LOG_D_NOTE;
682 for (i=0; i<12; i++)
684 ldf = fp_gt(fp_sub(lfreq,notes[i].logfreq), FP_ZERO) ?
685 fp_sub(lfreq,notes[i].logfreq) : fp_neg(fp_sub(lfreq,notes[i].logfreq));
686 if (fp_lt(ldf, mldf))
688 mldf = ldf;
689 note = i;
692 nfreq = notes[note].freq;
693 while (fp_gt(fp_div(nfreq, freq), D_NOTE_SQRT))
694 nfreq = fp_shr(nfreq, 1);
696 while (fp_gt(fp_div(freq, nfreq), D_NOTE_SQRT))
697 nfreq = fp_shl(nfreq, 1);
699 ldf = fp_mul(int2fixed(1200), log(fp_div(freq,nfreq)));
701 rb->lcd_clear_display();
702 draw_bar(ldf); /* The red bar */
703 if(fp_round(freq) != 0)
705 draw_note(notes[note].name);
706 if(settings.display_hz)
708 #if LCD_DEPTH > 1
709 rb->lcd_set_foreground(front_color);
710 #endif
711 rb->lcd_putsxyf(0, HZ_Y, "%s : %d cents (%d.%02dHz)",
712 notes[note].name, fp_round(ldf) ,fixed2int(orig_freq),
713 fp_round(fp_mul(fp_frac(orig_freq),
714 int2fixed(DISPLAY_HZ_PRECISION))));
717 rb->lcd_update();
720 #ifndef SIMULATOR
721 /*-----------------------------------------------------------------------
722 * Functions for the Yin algorithm
724 * These were all adapted from the versions in Aubio v0.3.2
725 * Here's what the Aubio documentation has to say:
727 * This algorithm was developped by A. de Cheveigne and H. Kawahara and
728 * published in:
730 * de Cheveign?, A., Kawahara, H. (2002) "YIN, a fundamental frequency
731 * estimator for speech and music", J. Acoust. Soc. Am. 111, 1917-1930.
733 * see http://recherche.ircam.fr/equipes/pcm/pub/people/cheveign.html
734 -------------------------------------------------------------------------*/
736 /* Find the index of the minimum element of an array of floats */
737 static unsigned vec_min_elem(fixed *s, unsigned buflen)
739 unsigned j, pos=0.0f;
740 fixed tmp = s[0];
741 for (j=0; j < buflen; j++)
743 if(fp_gt(tmp, s[j]))
745 pos = j;
746 tmp = s[j];
749 return pos;
753 static inline fixed aubio_quadfrac(fixed s0, fixed s1, fixed s2, fixed pf)
755 /* Original floating point version: */
756 /* tmp = s0 + (pf/2.0f) * (pf * ( s0 - 2.0f*s1 + s2 ) -
757 3.0f*s0 + 4.0f*s1 - s2);*/
758 /* Converted to explicit operator precedence: */
759 /* tmp = s0 + ((pf/2.0f) * ((((pf * ((s0 - (2*s1)) + s2)) -
760 (3*s0)) + (4*s1)) - s2)); */
762 /* I made it look like this so I could easily track the precedence and */
763 /* make sure it matched the original expression */
764 /* Oy, this is when I really wish I could do C++ operator overloading */
765 fixed tmp = fp_add
768 fp_mul
770 fp_shr(pf, 1),
771 fp_sub
773 fp_add
775 fp_sub
777 fp_mul
780 fp_add
782 fp_sub
785 fp_shl(s1, 1)
790 fp_mul
792 float2fixed(3.0f),
796 fp_shl(s1, 2)
802 return tmp;
805 #define QUADINT_STEP float2fixed(1.0f/200.0f)
807 static fixed ICODE_ATTR vec_quadint_min(fixed *x, unsigned bufsize, unsigned pos, unsigned span)
809 fixed res, frac, s0, s1, s2;
810 fixed exactpos = int2fixed(pos);
811 /* init resold to something big (in case x[pos+-span]<0)) */
812 fixed resold = FP_MAX;
814 if ((pos > span) && (pos < bufsize-span))
816 s0 = x[pos-span];
817 s1 = x[pos] ;
818 s2 = x[pos+span];
819 /* increase frac */
820 for (frac = float2fixed(0.0f);
821 fp_lt(frac, float2fixed(2.0f));
822 frac = fp_add(frac, QUADINT_STEP))
824 res = aubio_quadfrac(s0, s1, s2, frac);
825 if (fp_lt(res, resold))
827 resold = res;
829 else
831 /* exactpos += (frac-QUADINT_STEP)*span - span/2.0f; */
832 exactpos = fp_add(exactpos,
833 fp_sub(
834 fp_mul(
835 fp_sub(frac, QUADINT_STEP),
836 int2fixed(span)
838 int2fixed(span)
841 break;
845 return exactpos;
848 /* Calculate the period of the note in the
849 buffer using the YIN algorithm */
850 /* The yin pointer is just a buffer that the algorithm uses as a work
851 space. It needs to be half the length of the input buffer. */
853 static fixed ICODE_ATTR pitchyin(int16_t *input, fixed *yin)
855 fixed retval;
856 unsigned j,tau = 0;
857 int period;
858 unsigned yin_size = settings.sample_size / 4;
860 fixed tmp = FP_ZERO, tmp2 = FP_ZERO;
861 yin[0] = int2fixed(1);
862 for (tau = 1; tau < yin_size; tau++)
864 yin[tau] = FP_ZERO;
865 for (j = 0; j < yin_size; j++)
867 tmp = fp_sub(int2mantissa(input[2 * j]),
868 int2mantissa(input[2 * (j + tau)]));
869 yin[tau] = fp_add(yin[tau], fp_mul(tmp, tmp));
871 tmp2 = fp_add(tmp2, yin[tau]);
872 if(!fp_equal(tmp2, FP_ZERO))
874 yin[tau] = fp_mul(yin[tau], fp_div(int2fixed(tau), tmp2));
876 period = tau - 3;
877 if(tau > 4 && fp_lt(yin[period],
878 yin_threshold_table[settings.yin_threshold])
879 && fp_lt(yin[period], yin[period+1]))
881 retval = vec_quadint_min(yin, yin_size, period, 1);
882 return retval;
885 retval = vec_quadint_min(yin, yin_size,
886 vec_min_elem(yin, yin_size), 1);
887 return retval;
888 /*return FP_ZERO;*/
891 /*-----------------------------------------------------------------*/
893 static uint32_t ICODE_ATTR buffer_magnitude(int16_t *input)
895 unsigned n;
896 uint64_t tally = 0;
897 const unsigned size = settings.sample_size;
899 /* Operate on only one channel of the stereo signal */
900 for(n = 0; n < size; n+=2)
902 int s = input[n];
903 tally += s * s;
906 tally /= size / 2;
908 /* now tally holds the average of the squares of all the samples */
909 /* It must be between 0 and 0x7fff^2, so it fits in 32 bits */
910 return (uint32_t)tally;
913 /* Stop the recording when the buffer is full */
914 static void recording_callback(int status, void **start, size_t *size)
916 int tail = audio_tail ^ 1;
918 /* Do not overrun the reader. Reuse current buffer if full. */
919 if (tail != audio_head)
920 audio_tail = tail;
922 /* Always record full buffer, even if not required */
923 *start = audio_data[tail];
924 *size = BUFFER_SIZE * sizeof (int16_t);
926 (void)status;
928 #endif /* SIMULATOR */
930 /* Start recording */
931 static void record_data(void)
933 #ifndef SIMULATOR
934 /* Always record full buffer, even if not required */
935 rb->pcm_record_data(recording_callback, audio_data[audio_tail],
936 BUFFER_SIZE * sizeof (int16_t));
937 #endif
940 /* The main program loop */
941 static void record_and_get_pitch(void)
943 int quit=0, button;
944 bool redraw = true;
945 /* For tracking the latency */
947 long timer;
948 char debug_string[20];
950 #ifndef SIMULATOR
951 fixed period;
952 bool waiting = false;
953 #else
954 audio_tail = 1;
955 #endif
957 backlight_force_on();
959 record_data();
961 while(!quit)
963 while (audio_head == audio_tail && !quit) /* wait for the buffer to be filled */
965 button=pluginlib_getaction(HZ/100, plugin_contexts, PLA_ARRAY_COUNT);
967 switch(button)
969 case PLA_EXIT:
970 quit=true;
971 break;
973 case PLA_CANCEL:
974 rb->pcm_stop_recording();
975 quit = main_menu() != 0;
976 if(!quit)
978 redraw = true;
979 record_data();
981 break;
983 break;
987 if(!quit)
989 #ifndef SIMULATOR
990 /* Only do the heavy lifting if the volume is high enough */
991 if(buffer_magnitude(audio_data[audio_head]) >
992 sqr(settings.volume_threshold *
993 rb->sound_max(SOUND_MIC_GAIN)))
995 waiting = false;
996 redraw = false;
998 #ifdef HAVE_SCHEDULER_BOOSTCTRL
999 rb->trigger_cpu_boost();
1000 #endif
1001 #ifdef PLUGIN_USE_IRAM
1002 rb->memcpy(iram_audio_data, audio_data[audio_head],
1003 settings.sample_size * sizeof (int16_t));
1004 #endif
1005 /* This returns the period of the detected pitch in samples */
1006 period = pitchyin(iram_audio_data, yin_buffer);
1007 /* Hz = sample rate / period */
1008 if(fp_gt(period, FP_ZERO))
1010 display_frequency(fp_period2freq(period));
1012 else
1014 display_frequency(FP_ZERO);
1017 else if(redraw || !waiting)
1019 waiting = true;
1020 redraw = false;
1021 display_frequency(FP_ZERO);
1022 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
1023 rb->cancel_cpu_boost();
1024 #endif
1027 /* Move to next buffer if not empty (but empty *shouldn't* happen
1028 * here). */
1029 if (audio_head != audio_tail)
1030 audio_head ^= 1;
1031 #else /* SIMULATOR */
1032 /* Display a preselected frequency */
1033 display_frequency(int2fixed(445));
1034 #endif
1037 rb->pcm_close_recording();
1038 rb->pcm_set_frequency(REC_SAMPR_DEFAULT | SAMPR_TYPE_REC);
1039 #ifdef HAVE_SCHEDULER_BOOSTCTRL
1040 rb->cancel_cpu_boost();
1041 #endif
1043 backlight_use_settings();
1046 /* Init recording, tuning, and GUI */
1047 static void init_everything(void)
1049 /* Disable all talking before initializing IRAM */
1050 rb->talk_disable(true);
1052 load_settings();
1053 rb->storage_sleep();
1055 /* Stop all playback */
1056 rb->plugin_get_audio_buffer(NULL);
1058 /* --------- Init the audio recording ----------------- */
1059 rb->audio_set_output_source(AUDIO_SRC_PLAYBACK);
1060 rb->audio_set_input_source(INPUT_TYPE, SRCF_RECORDING);
1062 /* set to maximum gain */
1063 rb->audio_set_recording_gain(settings.record_gain,
1064 settings.record_gain,
1065 AUDIO_GAIN_MIC);
1067 /* Highest C on piano is approx 4.186 kHz, so we need just over
1068 * 8.372 kHz to pass it. */
1069 sample_rate = rb->round_value_to_list32(9000, rb->rec_freq_sampr,
1070 REC_NUM_FREQ, false);
1071 sample_rate = rb->rec_freq_sampr[sample_rate];
1072 rb->pcm_set_frequency(sample_rate | SAMPR_TYPE_REC);
1073 rb->pcm_init_recording();
1075 /* avoid divsion by zero */
1076 if(settings.lowest_freq == 0)
1077 settings.lowest_freq = period2freq(BUFFER_SIZE / 4);
1079 /* GUI */
1080 #if LCD_DEPTH > 1
1081 front_color = rb->lcd_get_foreground();
1082 #endif
1083 rb->lcd_getstringsize("X", &font_w, &font_h);
1085 bar_x_0 = LCD_WIDTH / 2;
1086 lbl_x_minus_50 = 0;
1087 lbl_x_minus_20 = (LCD_WIDTH / 2) -
1088 fixed2int(fp_mul(LCD_FACTOR, int2fixed(20))) - font_w;
1089 lbl_x_0 = (LCD_WIDTH - font_w) / 2;
1090 lbl_x_20 = (LCD_WIDTH / 2) +
1091 fixed2int(fp_mul(LCD_FACTOR, int2fixed(20))) - font_w;
1092 lbl_x_50 = LCD_WIDTH - 2 * font_w;
1094 bar_grad_y = BAR_Y - BAR_PADDING - font_h;
1095 /* Put the note right between the top and bottom text elements */
1096 note_y = ((font_h + bar_grad_y - note_bitmaps.slide_height) / 2);
1098 rb->talk_disable(false);
1101 enum plugin_status plugin_start(const void* parameter)
1103 (void)parameter;
1105 init_everything();
1106 record_and_get_pitch();
1107 save_settings();
1109 return PLUGIN_OK;