Forgot the #
[kugel-rb.git] / apps / recorder / peakmeter.c
blob8f32a837a76f8808e6c12f1c9ae313bb50c006e1
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Philipp Pertermann
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #ifdef SIMULATOR
22 #include <stdlib.h> /* sim uses rand for peakmeter simulation */
23 #endif
24 #include "config.h"
25 #include "mas.h"
26 #include "thread.h"
27 #include "kernel.h"
28 #include "settings.h"
29 #include "storage.h"
30 #include "lcd.h"
31 #include "scrollbar.h"
32 #include "sprintf.h"
33 #include "button.h"
34 #include "system.h"
35 #include "font.h"
36 #include "icons.h"
37 #include "lang.h"
38 #include "peakmeter.h"
39 #include "audio.h"
40 #include "screen_access.h"
41 #ifdef HAVE_BACKLIGHT
42 #include "backlight.h"
43 #endif
44 #include "action.h"
46 #if CONFIG_CODEC == SWCODEC
47 #include "pcm.h"
49 #ifdef HAVE_RECORDING
50 #include "pcm_record.h"
51 #endif
53 static bool pm_playback = true; /* selects between playback and recording peaks */
54 #endif
56 static struct meter_scales scales[NB_SCREENS];
58 #if !defined(SIMULATOR) && CONFIG_CODEC != SWCODEC
59 /* Data source */
60 static int pm_src_left = MAS_REG_DQPEAK_L;
61 static int pm_src_right = MAS_REG_DQPEAK_R;
62 #endif
64 /* Current values and cumulation */
65 static int pm_cur_left; /* current values (last peak_meter_peek) */
66 static int pm_cur_right;
67 static int pm_max_left; /* maximum values between peak meter draws */
68 static int pm_max_right;
69 #if defined(HAVE_AGC) || defined(HAVE_RECORDING_HISTOGRAM)
70 static int pm_peakhold_left; /* max. peak values between peakhold calls */
71 static int pm_peakhold_right; /* used for AGC and histogram display */
72 #endif
74 /* Clip hold */
75 static bool pm_clip_left = false; /* when true a clip has occurred */
76 static bool pm_clip_right = false;
77 static long pm_clip_timeout_l; /* clip hold timeouts */
78 static long pm_clip_timeout_r;
80 /* Temporarily en- / disables peak meter. This is especially for external
81 applications to detect if the peak_meter is in use and needs drawing at all */
82 bool peak_meter_enabled = true;
84 /** Parameters **/
85 /* Range */
86 static unsigned short peak_meter_range_min; /* minimum of range in samples */
87 static unsigned short peak_meter_range_max; /* maximum of range in samples */
88 static unsigned short pm_range; /* range width in samples */
89 static bool pm_use_dbfs = true; /* true if peakmeter displays dBfs */
90 static bool level_check; /* true if peeked at peakmeter before drawing */
91 static unsigned short pm_db_min = 0; /* minimum of range in 1/100 dB */
92 static unsigned short pm_db_max = 9000; /* maximum of range in 1/100 dB */
93 static unsigned short pm_db_range = 9000; /* range width in 1/100 dB */
94 /* Timing behaviour */
95 static int pm_peak_hold = 1; /* peak hold timeout index */
96 static int pm_peak_release = 8; /* peak release in units per read */
97 static int pm_clip_hold = 16; /* clip hold timeout index */
98 static bool pm_clip_eternal = false; /* true if clip timeout is disabled */
100 #ifdef HAVE_RECORDING
101 static unsigned short trig_strt_threshold;
102 static long trig_strt_duration;
103 static long trig_strt_dropout;
104 static unsigned short trig_stp_threshold;
105 static long trig_stp_hold;
106 static long trig_rstrt_gap;
108 /* point in time when the threshold was exceeded */
109 static long trig_hightime;
111 /* point in time when the volume fell below the threshold*/
112 static long trig_lowtime;
114 /* The output value of the trigger. See TRIG_XXX constants for valid values */
115 static int trig_status = TRIG_OFF;
117 static void (*trigger_listener)(int) = NULL;
119 /* clipping counter (only used for recording) */
120 static unsigned int pm_clipcount = 0; /* clipping count */
121 static bool pm_clipcount_active = false; /* counting or not */
122 #endif
124 /* debug only */
125 #ifdef PM_DEBUG
126 static int peek_calls = 0;
128 #define PEEKS_PER_DRAW_SIZE 40
129 static unsigned int peeks_per_redraw[PEEKS_PER_DRAW_SIZE];
131 #define TICKS_PER_DRAW_SIZE 20
132 static unsigned int ticks_per_redraw[TICKS_PER_DRAW_SIZE];
133 #endif
135 static void peak_meter_draw(struct screen *display, struct meter_scales *meter_scales,
136 int x, int y, int width, int height);
138 /* time out values for max */
139 static const short peak_time_out[] = {
140 0 * HZ, HZ / 5, 30, HZ / 2, HZ, 2 * HZ,
141 3 * HZ, 4 * HZ, 5 * HZ, 6 * HZ, 7 * HZ, 8 * HZ,
142 9 * HZ, 10 * HZ, 15 * HZ, 20 * HZ, 30 * HZ, 60 * HZ
145 /* time out values for clip */
146 static const long clip_time_out[] = {
147 0 * HZ, 1 * HZ, 2 * HZ, 3 * HZ, 4 * HZ, 5 * HZ,
148 6 * HZ, 7 * HZ, 8 * HZ, 9 * HZ, 10 * HZ, 15 * HZ,
149 20 * HZ, 25 * HZ, 30 * HZ, 45 * HZ, 60 * HZ, 90 * HZ,
150 120 * HZ, 180 * HZ, 300 * HZ, 600L * HZ, 1200L * HZ,
151 2700L * HZ, 5400L * HZ
154 /* precalculated peak values that represent magical
155 dBfs values. Used to draw the scale */
156 static const short db_scale_src_values[DB_SCALE_SRC_VALUES_SIZE] = {
157 32736, /* 0 db */
158 22752, /* - 3 db */
159 16640, /* - 6 db */
160 11648, /* - 9 db */
161 8320, /* -12 db */
162 4364, /* -18 db */
163 2064, /* -24 db */
164 1194, /* -30 db */
165 363, /* -40 db */
166 101, /* -50 db */
167 34, /* -60 db */
168 0, /* -inf */
171 static int db_scale_count = DB_SCALE_SRC_VALUES_SIZE;
174 * Calculates dB Value for the peak meter, uses peak value as input
175 * @param int sample - The input value
176 * Make sure that 0 <= value < SAMPLE_RANGE
178 * @return int - The 2 digit fixed point result of the euation
179 * 20 * log (sample / SAMPLE_RANGE) + 90
180 * Output range is 0-9000 (that is 0.0 - 90.0 dB).
181 * Normally 0dB is full scale, here it is shifted +90dB.
182 * The calculation is based on the results of a linear
183 * approximation tool written specifically for this problem
184 * by Andreas Zwirtes (radhard@gmx.de). The result has an
185 * accurracy of better than 2%. It is highly runtime optimized,
186 * the cascading if-clauses do an successive approximation on
187 * the input value. This avoids big lookup-tables and
188 * for-loops.
189 * Improved by Jvo Studer for errors < 0.2dB for critical
190 * range of -12dB to 0dB (78.0 to 90.0dB).
193 int calc_db (int isample)
195 /* return n+m*(isample-istart)/100 */
196 int n;
197 long m;
198 int istart;
200 if (isample < 2308) { /* Range 1-5 */
202 if (isample < 115) { /* Range 1-3 */
204 if (isample < 24) {
206 if (isample < 5) {
207 istart = 1; /* Range 1 */
208 n = 98;
209 m = 34950;
211 else {
212 istart = 5; /* Range 2 */
213 n = 1496;
214 m = 7168;
217 else {
218 istart = 24; /* Range 3 */
219 n = 2858;
220 m = 1498;
223 else { /* Range 4-5 */
225 if (isample < 534) {
226 istart = 114; /* Range 4 */
227 n = 4207;
228 m = 319;
230 else {
231 istart = 588; /* Range 5 */
232 n = 5583;
233 m = 69;
238 else { /* Range 6-9 */
240 if (isample < 12932) {
242 if (isample < 6394) {
243 istart = 2608; /* Range 6 */
244 n = 6832;
245 m = 21;
247 else {
248 istart = 7000; /* Range 7 */
249 n = 7682;
250 m = 9;
253 else {
255 if (isample < 22450) {
256 istart = 13000; /* Range 8 */
257 n = 8219;
258 m = 5;
260 else {
261 istart = 22636; /* Range 9 */
262 n = 8697;
263 m = 3;
268 return n + (m * (long)(isample - istart)) / 100L;
273 * A helper function for peak_meter_db2sample. Don't call it separately but
274 * use peak_meter_db2sample. If one or both of min and max are outside the
275 * range 0 <= min (or max) < 8961 the behaviour of this function is
276 * undefined. It may not return.
277 * @param int min - The minimum of the value range that is searched.
278 * @param int max - The maximum of the value range that is searched.
279 * @param int db - The value in dBfs * (-100) for which the according
280 * minimal peak sample is searched.
281 * @return int - A linear volume value with 0 <= value < MAX_PEAK
283 static int db_to_sample_bin_search(int min, int max, int db)
285 int test = min + (max - min) / 2;
287 if (min < max) {
288 if (calc_db(test) < db) {
289 test = db_to_sample_bin_search(test, max, db);
290 } else {
291 if (calc_db(test-1) > db) {
292 test = db_to_sample_bin_search(min, test, db);
296 return test;
300 * Converts a value representing dBfs to a linear
301 * scaled volume info as it is used by the MAS.
302 * An incredibly inefficiant function which is
303 * the vague inverse of calc_db. This really
304 * should be replaced by something better soon.
306 * @param int db - A dBfs * 100 value with
307 * -9000 < value <= 0
308 * @return int - The return value is in the range of
309 * 0 <= return value < MAX_PEAK
311 int peak_meter_db2sample(int db)
313 int retval = 0;
315 /* what is the maximum pseudo db value */
316 int max_peak_db = calc_db(MAX_PEAK - 1);
318 /* range check: db value to big */
319 if (max_peak_db + db < 0) {
320 retval = 0;
323 /* range check: db value too small */
324 else if (max_peak_db + db >= max_peak_db) {
325 retval = MAX_PEAK -1;
328 /* value in range: find the matching linear value */
329 else {
330 retval = db_to_sample_bin_search(0, MAX_PEAK, max_peak_db + db);
332 /* as this is a dirty function anyway, we want to adjust the
333 full scale hit manually to avoid users complaining that when
334 they adjust maximum for 0 dBfs and display it in percent it
335 shows 99%. That is due to precision loss and this is the
336 optical fix */
339 return retval;
343 * Set the min value for restriction of the value range.
344 * @param int newmin - depending whether dBfs is used
345 * newmin is a value in dBfs * 100 or in linear percent values.
346 * for dBfs: -9000 < newmin <= 0
347 * for linear: 0 <= newmin <= 100
349 static void peak_meter_set_min(int newmin)
351 if (pm_use_dbfs) {
352 peak_meter_range_min = peak_meter_db2sample(newmin);
354 } else {
355 if (newmin < peak_meter_range_max) {
356 peak_meter_range_min = newmin * MAX_PEAK / 100;
360 pm_range = peak_meter_range_max - peak_meter_range_min;
362 /* Avoid division by zero. */
363 if (pm_range == 0) {
364 pm_range = 1;
367 pm_db_min = calc_db(peak_meter_range_min);
368 pm_db_range = pm_db_max - pm_db_min;
369 int i;
370 FOR_NB_SCREENS(i)
371 scales[i].db_scale_valid = false;
375 * Returns the minimum value of the range the meter
376 * displays. If the scale is set to dBfs it returns
377 * dBfs values * 100 or linear percent values.
378 * @return: using dBfs : -9000 < value <= 0
379 * using linear scale: 0 <= value <= 100
381 int peak_meter_get_min(void)
383 int retval = 0;
384 if (pm_use_dbfs) {
385 retval = calc_db(peak_meter_range_min) - calc_db(MAX_PEAK - 1);
386 } else {
387 retval = peak_meter_range_min * 100 / MAX_PEAK;
389 return retval;
393 * Set the max value for restriction of the value range.
394 * @param int newmax - depending wether dBfs is used
395 * newmax is a value in dBfs * 100 or in linear percent values.
396 * for dBfs: -9000 < newmax <= 0
397 * for linear: 0 <= newmax <= 100
399 static void peak_meter_set_max(int newmax)
401 if (pm_use_dbfs) {
402 peak_meter_range_max = peak_meter_db2sample(newmax);
403 } else {
404 if (newmax > peak_meter_range_min) {
405 peak_meter_range_max = newmax * MAX_PEAK / 100;
409 pm_range = peak_meter_range_max - peak_meter_range_min;
411 /* Avoid division by zero. */
412 if (pm_range == 0) {
413 pm_range = 1;
416 pm_db_max = calc_db(peak_meter_range_max);
417 pm_db_range = pm_db_max - pm_db_min;
418 int i;
419 FOR_NB_SCREENS(i)
420 scales[i].db_scale_valid = false;
424 * Returns the minimum value of the range the meter
425 * displays. If the scale is set to dBfs it returns
426 * dBfs values * 100 or linear percent values
427 * @return: using dBfs : -9000 < value <= 0
428 * using linear scale: 0 <= value <= 100
430 int peak_meter_get_max(void)
432 int retval = 0;
433 if (pm_use_dbfs) {
434 retval = calc_db(peak_meter_range_max) - calc_db(MAX_PEAK - 1);
435 } else {
436 retval = peak_meter_range_max * 100 / MAX_PEAK;
438 return retval;
442 * Returns whether the meter is currently displaying dBfs or percent values.
443 * @return bool - true if the meter is displaying dBfs
444 false if the meter is displaying percent values.
446 bool peak_meter_get_use_dbfs(void)
448 return pm_use_dbfs;
452 * Specifies whether the values displayed are scaled
453 * as dBfs or as linear percent values.
454 * @param use - set to true for dBfs,
455 * set to false for linear scaling in percent
457 void peak_meter_set_use_dbfs(bool use)
459 int i;
460 pm_use_dbfs = use;
461 FOR_NB_SCREENS(i)
462 scales[i].db_scale_valid = false;
466 * Initialize the range of the meter. Only values
467 * that are in the range of [range_min ... range_max]
468 * are displayed.
469 * @param bool dbfs - set to true for dBfs,
470 * set to false for linear scaling in percent
471 * @param int range_min - Specifies the lower value of the range.
472 * Pass a value dBfs * 100 when dbfs is set to true.
473 * Pass a percent value when dbfs is set to false.
474 * @param int range_max - Specifies the upper value of the range.
475 * Pass a value dBfs * 100 when dbfs is set to true.
476 * Pass a percent value when dbfs is set to false.
478 void peak_meter_init_range( bool dbfs, int range_min, int range_max)
480 pm_use_dbfs = dbfs;
481 peak_meter_set_min(range_min);
482 peak_meter_set_max(range_max);
486 * Initialize the peak meter with all relevant values concerning times.
487 * @param int release - Set the maximum amount of pixels the meter is allowed
488 * to decrease with each redraw
489 * @param int hold - Select the time preset for the time the peak indicator
490 * is reset after a peak occurred. The preset values are
491 * stored in peak_time_out.
492 * @param int clip_hold - Select the time preset for the time the peak
493 * indicator is reset after a peak occurred. The preset
494 * values are stored in clip_time_out.
496 void peak_meter_init_times(int release, int hold, int clip_hold)
498 pm_peak_hold = hold;
499 pm_peak_release = release;
500 pm_clip_hold = clip_hold;
503 #ifdef HAVE_RECORDING
505 * Enable/disable clip counting
507 void pm_activate_clipcount(bool active)
509 pm_clipcount_active = active;
513 * Get clipping counter value
515 int pm_get_clipcount(void)
517 return pm_clipcount;
521 * Set clipping counter to zero (typically at start of recording or playback)
523 void pm_reset_clipcount(void)
525 pm_clipcount = 0;
527 #endif
530 * Set the source of the peak meter to playback or to
531 * record.
532 * @param: bool playback - If true playback peak meter is used.
533 * If false recording peak meter is used.
535 void peak_meter_playback(bool playback)
537 int i;
538 #ifdef SIMULATOR
539 (void)playback;
540 #elif CONFIG_CODEC == SWCODEC
541 pm_playback = playback;
542 #else
543 if (playback) {
544 pm_src_left = MAS_REG_DQPEAK_L;
545 pm_src_right = MAS_REG_DQPEAK_R;
546 } else {
547 pm_src_left = MAS_REG_QPEAK_L;
548 pm_src_right = MAS_REG_QPEAK_R;
550 #endif
551 /* reset the scales just in case recording and playback
552 use different viewport sizes. Normally we should be checking viewport
553 sizes every time but this will do for now */
554 FOR_NB_SCREENS(i)
555 scales[i].db_scale_valid = false;
558 #ifdef HAVE_RECORDING
559 static void set_trig_status(int new_state)
561 if (trig_status != new_state) {
562 trig_status = new_state;
563 if (trigger_listener != NULL) {
564 trigger_listener(trig_status);
569 #endif
572 * Reads peak values from the MAS, and detects clips. The
573 * values are stored in pm_max_left pm_max_right for later
574 * evauluation. Consecutive calls to peak_meter_peek detect
575 * that ocurred. This function could be used by a thread for
576 * busy reading the MAS.
578 void peak_meter_peek(void)
580 int left, right;
581 #ifdef HAVE_RECORDING
582 bool was_clipping = pm_clip_left || pm_clip_right;
583 #endif
584 /* read current values */
585 #if CONFIG_CODEC == SWCODEC
586 if (pm_playback)
587 pcm_calculate_peaks(&pm_cur_left, &pm_cur_right);
588 #ifdef HAVE_RECORDING
589 else
590 pcm_calculate_rec_peaks(&pm_cur_left, &pm_cur_right);
591 #endif
592 left = pm_cur_left;
593 right = pm_cur_right;
594 #else
595 #ifndef SIMULATOR
596 pm_cur_left = left = mas_codec_readreg(pm_src_left);
597 pm_cur_right = right = mas_codec_readreg(pm_src_right);
598 #else
599 pm_cur_left = left = 8000;
600 pm_cur_right = right = 9000;
601 #endif
602 #endif
604 /* check for clips
605 An clip is assumed when two consecutive readouts
606 of the volume are at full scale. This is proven
607 to be inaccurate in both ways: it may detect clips
608 when no clip occurred and it may fail to detect
609 a real clip. For software codecs, the peak is already
610 the max of a bunch of samples, so use one max value
611 or you fail to detect clipping! */
612 #if CONFIG_CODEC == SWCODEC
613 if (left == MAX_PEAK - 1) {
614 #else
615 if ((left == pm_max_left) &&
616 (left == MAX_PEAK - 1)) {
617 #endif
618 pm_clip_left = true;
619 pm_clip_timeout_l =
620 current_tick + clip_time_out[pm_clip_hold];
623 #if CONFIG_CODEC == SWCODEC
624 if (right == MAX_PEAK - 1) {
625 #else
626 if ((right == pm_max_right) &&
627 (right == MAX_PEAK - 1)) {
628 #endif
629 pm_clip_right = true;
630 pm_clip_timeout_r =
631 current_tick + clip_time_out[pm_clip_hold];
634 #ifdef HAVE_RECORDING
635 if(!was_clipping && (pm_clip_left || pm_clip_right))
637 if(pm_clipcount_active)
638 pm_clipcount++;
640 #endif
642 /* peaks are searched -> we have to find the maximum. When
643 many calls of peak_meter_peek the maximum value will be
644 stored in pm_max_xxx. This maximum is reset by the
645 functions peak_meter_read_x. */
646 pm_max_left = MAX(pm_max_left, left);
647 pm_max_right = MAX(pm_max_right, right);
649 #ifdef HAVE_RECORDING
650 #if CONFIG_CODEC == SWCODEC
651 /* Ignore any unread peakmeter data */
652 #define MAX_DROP_TIME HZ/7 /* this value may need tweaking. Increase if you are
653 getting trig events when you shouldn't with
654 trig_stp_hold = 0 */
655 if (!trig_stp_hold)
656 trig_stp_hold = MAX_DROP_TIME;
657 #endif
659 switch (trig_status) {
660 case TRIG_READY:
661 /* no more changes, if trigger was activated as release trigger */
662 /* threshold exceeded? */
663 if ((left > trig_strt_threshold)
664 || (right > trig_strt_threshold)) {
665 /* reset trigger duration */
666 trig_hightime = current_tick;
668 /* reset dropout duration */
669 trig_lowtime = current_tick;
671 if (trig_strt_duration)
672 set_trig_status(TRIG_STEADY);
673 else
674 /* if trig_duration is set to 0 the user wants to start
675 recording immediately */
676 set_trig_status(TRIG_GO);
678 break;
680 case TRIG_STEADY:
681 case TRIG_RETRIG:
682 /* trigger duration exceeded */
683 if (current_tick - trig_hightime > trig_strt_duration) {
684 set_trig_status(TRIG_GO);
685 } else {
686 /* threshold exceeded? */
687 if ((left > trig_strt_threshold)
688 || (right > trig_strt_threshold)) {
689 /* reset lowtime */
690 trig_lowtime = current_tick;
692 /* volume is below threshold */
693 else {
694 /* dropout occurred? */
695 if (current_tick - trig_lowtime > trig_strt_dropout){
696 if (trig_status == TRIG_STEADY){
697 set_trig_status(TRIG_READY);
699 /* trig_status == TRIG_RETRIG */
700 else {
701 /* the gap has already expired */
702 trig_lowtime = current_tick - trig_rstrt_gap - 1;
703 set_trig_status(TRIG_POSTREC);
708 break;
710 case TRIG_GO:
711 case TRIG_CONTINUE:
712 /* threshold exceeded? */
713 if ((left > trig_stp_threshold)
714 || (right > trig_stp_threshold)) {
715 /* restart hold time countdown */
716 trig_lowtime = current_tick;
717 #if CONFIG_CODEC == SWCODEC
718 } else if (current_tick - trig_lowtime > MAX_DROP_TIME){
719 #else
720 } else {
721 #endif
722 set_trig_status(TRIG_POSTREC);
723 trig_hightime = current_tick;
725 break;
727 case TRIG_POSTREC:
728 /* gap time expired? */
729 if (current_tick - trig_lowtime > trig_rstrt_gap){
730 /* start threshold exceeded? */
731 if ((left > trig_strt_threshold)
732 || (right > trig_strt_threshold)) {
734 set_trig_status(TRIG_RETRIG);
735 trig_hightime = current_tick;
736 trig_lowtime = current_tick;
738 else
740 /* stop threshold exceeded */
741 if ((left > trig_stp_threshold)
742 || (right > trig_stp_threshold)) {
743 if (current_tick - trig_hightime > trig_stp_hold){
744 trig_lowtime = current_tick;
745 set_trig_status(TRIG_CONTINUE);
746 } else {
747 trig_lowtime = current_tick - trig_rstrt_gap - 1;
751 /* below any threshold */
752 else {
753 if (current_tick - trig_lowtime > trig_stp_hold){
754 set_trig_status(TRIG_READY);
755 } else {
756 trig_hightime = current_tick;
761 /* still within the gap time */
762 else {
763 /* stop threshold exceeded */
764 if ((left > trig_stp_threshold)
765 || (right > trig_stp_threshold)) {
766 set_trig_status(TRIG_CONTINUE);
767 trig_lowtime = current_tick;
770 /* hold time expired */
771 else if (current_tick - trig_lowtime > trig_stp_hold){
772 trig_hightime = current_tick;
773 trig_lowtime = current_tick;
774 set_trig_status(TRIG_READY);
777 break;
779 #if CONFIG_CODEC == SWCODEC
780 /* restore stop hold value */
781 if (trig_stp_hold == MAX_DROP_TIME)
782 trig_stp_hold = 0;
783 #endif
784 #endif
785 /* check levels next time peakmeter drawn */
786 level_check = true;
787 #ifdef PM_DEBUG
788 peek_calls++;
789 #endif
793 * Reads out the peak volume of the left channel.
794 * @return int - The maximum value that has been detected
795 * since the last call of peak_meter_read_l. The value
796 * is in the range 0 <= value < MAX_PEAK.
798 static int peak_meter_read_l(void)
800 /* pm_max_left contains the maximum of all peak values that were read
801 by peak_meter_peek since the last call of peak_meter_read_l */
802 int retval;
804 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
805 srand(current_tick);
806 pm_max_left = rand()%MAX_PEAK;
807 #endif
809 retval = pm_max_left;
811 #if defined(HAVE_RECORDING_HISTOGRAM) || defined(HAVE_AGC)
812 /* store max peak value for peak_meter_get_peakhold_x readout */
813 pm_peakhold_left = MAX(pm_max_left, pm_peakhold_left);
814 #endif
815 #ifdef PM_DEBUG
816 peek_calls = 0;
817 #endif
818 /* reset pm_max_left so that subsequent calls of peak_meter_peek don't
819 get fooled by an old maximum value */
820 pm_max_left = pm_cur_left;
822 return retval;
826 * Reads out the peak volume of the right channel.
827 * @return int - The maximum value that has been detected
828 * since the last call of peak_meter_read_l. The value
829 * is in the range 0 <= value < MAX_PEAK.
831 static int peak_meter_read_r(void)
833 /* peak_meter_r contains the maximum of all peak values that were read
834 by peak_meter_peek since the last call of peak_meter_read_r */
835 int retval;
837 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
838 srand(current_tick);
839 pm_max_right = rand()%MAX_PEAK;
840 #endif
842 retval = pm_max_right;
844 #if defined(HAVE_RECORDING_HISTOGRAM) || defined(HAVE_AGC)
845 /* store max peak value for peak_meter_get_peakhold_x readout */
846 pm_peakhold_right = MAX(pm_max_right, pm_peakhold_right);
847 #endif
848 #ifdef PM_DEBUG
849 peek_calls = 0;
850 #endif
851 /* reset pm_max_right so that subsequent calls of peak_meter_peek don't
852 get fooled by an old maximum value */
853 pm_max_right = pm_cur_right;
855 return retval;
858 #if defined(HAVE_AGC) || defined(HAVE_RECORDING_HISTOGRAM)
860 * Reads out the current peak-hold values since the last call.
861 * This is used by the histogram feature in the recording screen.
862 * Values are in the range 0 <= peak_x < MAX_PEAK. MAX_PEAK is typ 32767.
864 void peak_meter_get_peakhold(int *peak_left, int *peak_right)
866 if (peak_left)
867 *peak_left = pm_peakhold_left;
868 if (peak_right)
869 *peak_right = pm_peakhold_right;
870 pm_peakhold_left = 0;
871 pm_peakhold_right = 0;
873 #endif
876 * Reset the detected clips. This method is for
877 * use by the user interface.
878 * @param int unused - This parameter was added to
879 * make the function compatible with set_int
881 void peak_meter_set_clip_hold(int time)
883 pm_clip_left = false;
884 pm_clip_right = false;
885 pm_clip_eternal = (time > 0) ? false : true;
889 * Scales a peak value as read from the MAS to the range of meterwidth.
890 * The scaling is performed according to the scaling method (dBfs / linear)
891 * and the range (peak_meter_range_min .. peak_meter_range_max).
892 * @param unsigned short val - The volume value. Range: 0 <= val < MAX_PEAK
893 * @param int meterwidht - The widht of the meter in pixel
894 * @return unsigned short - A value 0 <= return value <= meterwidth
896 unsigned short peak_meter_scale_value(unsigned short val, int meterwidth)
898 int retval;
900 if (val <= peak_meter_range_min) {
901 return 0;
904 if (val >= peak_meter_range_max) {
905 return meterwidth;
908 retval = val;
910 /* different scaling is used for dBfs and linear percent */
911 if (pm_use_dbfs) {
913 /* scale the samples dBfs */
914 retval = (calc_db(retval) - pm_db_min) * meterwidth / pm_db_range;
917 /* Scale for linear percent display */
918 else
920 /* scale the samples */
921 retval = ((retval - peak_meter_range_min) * meterwidth)
922 / pm_range;
924 return retval;
926 void peak_meter_screen(struct screen *display, int x, int y, int height)
928 peak_meter_draw(display, &scales[display->screen_type], x, y,
929 display->getwidth() - x, height);
932 * Draws a peak meter in the specified size at the specified position.
933 * @param int x - The x coordinate.
934 * Make sure that 0 <= x and x + width < display->getwidth()
935 * @param int y - The y coordinate.
936 * Make sure that 0 <= y and y + height < display->getheight()
937 * @param int width - The width of the peak meter. Note that for display
938 * of clips a 3 pixel wide area is used ->
939 * width > 3
940 * @param int height - The height of the peak meter. height > 3
942 static void peak_meter_draw(struct screen *display, struct meter_scales *scales,
943 int x, int y, int width, int height)
945 static int left_level = 0, right_level = 0;
946 int left = 0, right = 0;
947 int meterwidth = width - 3;
948 int i, delta;
949 #if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
950 static long peak_release_tick[2] = {0,0};
951 int screen_nr = display->screen_type == SCREEN_MAIN ? 0 : 1;
952 #else
953 static long peak_release_tick = 0;
954 #endif
956 #ifdef PM_DEBUG
957 static long pm_tick = 0;
958 int tmp = peek_calls;
959 #endif
961 /* if disabled only draw the peak meter */
962 if (peak_meter_enabled) {
965 if (level_check){
966 /* only read the volume info from MAS if peek since last read*/
967 left_level = peak_meter_read_l();
968 right_level = peak_meter_read_r();
969 level_check = false;
972 /* scale the samples dBfs */
973 left = peak_meter_scale_value(left_level, meterwidth);
974 right = peak_meter_scale_value(right_level, meterwidth);
976 /*if the scale has changed -> recalculate the scale
977 (The scale becomes invalid when the range changed.) */
978 if (!scales->db_scale_valid){
980 if (pm_use_dbfs) {
981 db_scale_count = DB_SCALE_SRC_VALUES_SIZE;
982 for (i = 0; i < db_scale_count; i++){
983 /* find the real x-coords for predefined interesting
984 dBfs values. These only are recalculated when the
985 scaling of the meter changed. */
986 scales->db_scale_lcd_coord[i] =
987 peak_meter_scale_value(
988 db_scale_src_values[i],
989 meterwidth - 1);
993 /* when scaling linear we simly make 10% steps */
994 else {
995 db_scale_count = 10;
996 for (i = 0; i < db_scale_count; i++) {
997 scales->db_scale_lcd_coord[i] =
998 (i * (MAX_PEAK / 10) - peak_meter_range_min) *
999 meterwidth / pm_range;
1003 /* mark scale valid to avoid recalculating dBfs values
1004 of the scale. */
1005 scales->db_scale_valid = true;
1008 /* apply release */
1009 #if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
1010 delta = current_tick - peak_release_tick[screen_nr];
1011 peak_release_tick[screen_nr] = current_tick;
1012 #else
1013 delta = current_tick - peak_release_tick;
1014 peak_release_tick = current_tick;
1015 #endif
1016 left = MAX(left , scales->last_left - delta * pm_peak_release);
1017 right = MAX(right, scales->last_right - delta * pm_peak_release);
1019 /* reset max values after timeout */
1020 if (TIME_AFTER(current_tick, scales->pm_peak_timeout_l)){
1021 scales->pm_peak_left = 0;
1024 if (TIME_AFTER(current_tick, scales->pm_peak_timeout_r)){
1025 scales->pm_peak_right = 0;
1028 if (!pm_clip_eternal) {
1029 if (pm_clip_left &&
1030 TIME_AFTER(current_tick, pm_clip_timeout_l)){
1031 pm_clip_left = false;
1034 if (pm_clip_right &&
1035 TIME_AFTER(current_tick, pm_clip_timeout_r)){
1036 pm_clip_right = false;
1040 /* check for new max values */
1041 if (left > scales->pm_peak_left) {
1042 scales->pm_peak_left = left - 1;
1043 scales->pm_peak_timeout_l =
1044 current_tick + peak_time_out[pm_peak_hold];
1047 if (right > scales->pm_peak_right) {
1048 scales->pm_peak_right = right - 1;
1049 scales->pm_peak_timeout_r =
1050 current_tick + peak_time_out[pm_peak_hold];
1054 /* draw the peak meter */
1055 display->set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1056 display->fillrect(x, y, width, height);
1057 display->set_drawmode(DRMODE_SOLID);
1059 /* draw left */
1060 display->fillrect (x, y, left, height / 2 - 2 );
1061 if (scales->pm_peak_left > 0) {
1062 display->vline(x + scales->pm_peak_left, y, y + height / 2 - 2 );
1064 if (pm_clip_left) {
1065 display->fillrect(x + meterwidth, y, 3, height / 2 - 1);
1068 /* draw right */
1069 display->fillrect(x, y + height / 2 + 1, right, height / 2 - 2);
1070 if (scales->pm_peak_right > 0) {
1071 display->vline( x + scales->pm_peak_right, y + height / 2, y + height - 2);
1073 if (pm_clip_right) {
1074 display->fillrect(x + meterwidth, y + height / 2, 3, height / 2 - 1);
1077 /* draw scale end */
1078 display->vline(x + meterwidth, y, y + height - 2);
1080 /* draw dots for scale marks */
1081 for (i = 0; i < db_scale_count; i++) {
1082 /* The x-coordinates of interesting scale mark points
1083 have been calculated before */
1084 display->drawpixel(x + scales->db_scale_lcd_coord[i],
1085 y + height / 2 - 1);
1088 #ifdef HAVE_RECORDING
1090 #ifdef HAVE_BACKLIGHT
1091 /* cliplight */
1092 if ((pm_clip_left || pm_clip_right) &&
1093 global_settings.cliplight &&
1094 #if CONFIG_CODEC == SWCODEC
1095 !pm_playback)
1096 #else
1097 !(audio_status() & (AUDIO_STATUS_PLAY | AUDIO_STATUS_ERROR)))
1098 #endif
1100 /* if clipping, cliplight setting on and in recording screen */
1101 if (global_settings.cliplight <= 2)
1103 /* turn on main unit light if setting set to main or both*/
1104 backlight_on();
1106 #ifdef HAVE_REMOTE_LCD
1107 if (global_settings.cliplight >= 2)
1109 /* turn remote light unit on if setting set to remote or both */
1110 remote_backlight_on();
1112 #endif /* HAVE_REMOTE_LCD */
1114 #endif /* HAVE_BACKLIGHT */
1116 if (trig_status != TRIG_OFF) {
1117 int start_trigx, stop_trigx, ycenter;
1119 display->set_drawmode(DRMODE_SOLID);
1120 ycenter = y + height / 2;
1121 /* display threshold value */
1122 start_trigx = x+peak_meter_scale_value(trig_strt_threshold,meterwidth);
1123 display->vline(start_trigx, ycenter - 2, ycenter);
1124 start_trigx ++;
1125 if (start_trigx < display->getwidth() ) display->drawpixel(start_trigx,
1126 ycenter - 1);
1128 stop_trigx = x + peak_meter_scale_value(trig_stp_threshold,meterwidth);
1129 display->vline(stop_trigx, ycenter - 2, ycenter);
1130 if (stop_trigx > 0) display->drawpixel(stop_trigx - 1, ycenter - 1);
1132 #endif /*HAVE_RECORDING*/
1134 #ifdef PM_DEBUG
1135 /* display a bar to show how many calls to peak_meter_peek
1136 have ocurred since the last display */
1137 display->set_drawmode(DRMODE_COMPLEMENT);
1138 display->fillrect(x, y, tmp, 3);
1140 if (tmp < PEEKS_PER_DRAW_SIZE) {
1141 peeks_per_redraw[tmp]++;
1144 tmp = current_tick - pm_tick;
1145 if (tmp < TICKS_PER_DRAW_SIZE ){
1146 ticks_per_redraw[tmp] ++;
1149 /* display a bar to show how many ticks have passed since
1150 the last redraw */
1151 display->fillrect(x, y + height / 2, current_tick - pm_tick, 2);
1152 pm_tick = current_tick;
1153 #endif
1155 scales->last_left = left;
1156 scales->last_right = right;
1158 display->set_drawmode(DRMODE_SOLID);
1161 #ifdef HAVE_RECORDING
1163 * Defines the parameters of the trigger. After these parameters are defined
1164 * the trigger can be started either by peak_meter_attack_trigger or by
1165 * peak_meter_release_trigger. Note that you can pass either linear (%) or
1166 * logarithmic (db) values to the thresholds. Positive values are intepreted as
1167 * percent (0 is 0% .. 100 is 100%). Negative values are interpreted as db.
1168 * To avoid ambiguosity of the value 0 the negative values are shifted by -1.
1169 * Thus -75 is -74db .. -1 is 0db.
1170 * @param start_threshold - The threshold used for attack trigger. Negative
1171 * values are interpreted as db -1, positive as %.
1172 * @param start_duration - The minimum time span within which start_threshold
1173 * must be exceeded to fire the attack trigger.
1174 * @param start_dropout - The maximum time span the level may fall below
1175 * start_threshold without releasing the attack trigger.
1176 * @param stop_threshold - The threshold the volume must fall below to release
1177 * the release trigger.Negative values are
1178 * interpreted as db -1, positive as %.
1179 * @param stop_hold - The minimum time the volume must fall below the
1180 * stop_threshold to release the trigger.
1181 * @param
1183 void peak_meter_define_trigger(
1184 int start_threshold,
1185 long start_duration,
1186 long start_dropout,
1187 int stop_threshold,
1188 long stop_hold_time,
1189 long restart_gap
1192 if (start_threshold < 0) {
1193 /* db */
1194 if (start_threshold < -89) {
1195 trig_strt_threshold = 0;
1196 } else {
1197 trig_strt_threshold =peak_meter_db2sample((start_threshold+1)*100);
1199 } else {
1200 /* linear percent */
1201 trig_strt_threshold = start_threshold * MAX_PEAK / 100;
1203 trig_strt_duration = start_duration;
1204 trig_strt_dropout = start_dropout;
1205 if (stop_threshold < 0) {
1206 /* db */
1207 trig_stp_threshold = peak_meter_db2sample((stop_threshold + 1) * 100);
1208 } else {
1209 /* linear percent */
1210 trig_stp_threshold = stop_threshold * MAX_PEAK / 100;
1212 trig_stp_hold = stop_hold_time;
1213 trig_rstrt_gap = restart_gap;
1217 * Enables or disables the trigger.
1218 * @param on - If true the trigger is turned on.
1220 void peak_meter_trigger(bool on)
1222 /* don't use set_trigger here as that would fire an undesired event */
1223 trig_status = on ? TRIG_READY : TRIG_OFF;
1227 * Registers the listener function that listenes on trig_status changes.
1228 * @param listener - The function that is called with each change of
1229 * trig_status. May be set to NULL if no callback is desired.
1231 void peak_meter_set_trigger_listener(void (*listener)(int status))
1233 trigger_listener = listener;
1237 * Fetches the status of the trigger.
1238 * TRIG_OFF: the trigger is inactive
1239 * TRIG_RELEASED: The volume level is below the threshold
1240 * TRIG_ACTIVATED: The volume level has exceeded the threshold, but the trigger
1241 * hasn't been fired yet.
1242 * TRIG_FIRED: The volume exceeds the threshold
1244 * To activate the trigger call either peak_meter_attack_trigger or
1245 * peak_meter_release_trigger. To turn the trigger off call
1246 * peak_meter_trigger_off.
1248 int peak_meter_trigger_status(void)
1250 return trig_status; /* & TRIG_PIT_MASK;*/
1253 void peak_meter_draw_trig(int xpos[], int ypos[],
1254 int trig_width[], int nb_screens)
1256 int barstart[NB_SCREENS];
1257 int barend[NB_SCREENS];
1258 int icon;
1259 int ixpos[NB_SCREENS];
1260 int i;
1261 int trigbar_width[NB_SCREENS];
1263 FOR_NB_SCREENS(i)
1264 trigbar_width[i] = (trig_width[i] - (2 * (ICON_PLAY_STATE_WIDTH + 1)));
1266 switch (trig_status) {
1268 case TRIG_READY:
1269 FOR_NB_SCREENS(i){
1270 barstart[i] = 0;
1271 barend[i] = 0;
1273 icon = Icon_Stop;
1274 FOR_NB_SCREENS(i)
1275 ixpos[i] = xpos[i];
1276 break;
1278 case TRIG_STEADY:
1279 case TRIG_RETRIG:
1280 FOR_NB_SCREENS(i)
1282 barstart[i] = 0;
1283 barend[i] = (trig_strt_duration == 0) ? trigbar_width[i] :
1284 trigbar_width[i] *
1285 (current_tick - trig_hightime) / trig_strt_duration;
1287 icon = Icon_Stop;
1288 FOR_NB_SCREENS(i)
1289 ixpos[i] = xpos[i];
1290 break;
1292 case TRIG_GO:
1293 case TRIG_CONTINUE:
1294 FOR_NB_SCREENS(i)
1296 barstart[i] = trigbar_width[i];
1297 barend[i] = trigbar_width[i];
1299 icon = Icon_Record;
1300 FOR_NB_SCREENS(i)
1301 ixpos[i] = xpos[i]+ trig_width[i] - ICON_PLAY_STATE_WIDTH;
1302 break;
1304 case TRIG_POSTREC:
1305 FOR_NB_SCREENS(i)
1307 barstart[i] = (trig_stp_hold == 0) ? 0 :
1308 trigbar_width[i] - trigbar_width[i] *
1309 (current_tick - trig_lowtime) / trig_stp_hold;
1310 barend[i] = trigbar_width[i];
1312 icon = Icon_Record;
1313 FOR_NB_SCREENS(i)
1314 ixpos[i] = xpos[i] + trig_width[i] - ICON_PLAY_STATE_WIDTH;
1315 break;
1317 default:
1318 return;
1321 for(i = 0; i < nb_screens; i++)
1323 gui_scrollbar_draw(&screens[i], xpos[i] + ICON_PLAY_STATE_WIDTH + 1,
1324 ypos[i] + 1, trigbar_width[i], TRIG_HEIGHT - 2,
1325 trigbar_width[i], barstart[i], barend[i],
1326 HORIZONTAL);
1328 screens[i].mono_bitmap(bitmap_icons_7x8[icon], ixpos[i], ypos[i],
1329 ICON_PLAY_STATE_WIDTH, STATUSBAR_HEIGHT);
1332 #endif
1334 int peak_meter_draw_get_btn(int action_context, int x[], int y[],
1335 int height[], int nb_screens,
1336 struct viewport vps[])
1338 int button = BUTTON_NONE;
1339 long next_refresh = current_tick;
1340 long next_big_refresh = current_tick + HZ / 10;
1341 int i;
1342 #if (CONFIG_CODEC == SWCODEC)
1343 bool highperf = false;
1344 #else
1345 /* On MAS targets, we need to poll as often as possible in order to not
1346 * miss a peak, as the MAS does only provide a quasi-peak. When the disk
1347 * is active, it must not draw too much CPU power or a buffer overrun can
1348 * happen when saving a recording. As a compromise, poll only once per tick
1349 * when the disk is active, otherwise spin around as fast as possible. */
1350 bool highperf = !storage_disk_is_active();
1351 #endif
1352 bool dopeek = true;
1354 while (TIME_BEFORE(current_tick, next_big_refresh)) {
1355 button = get_action(action_context, TIMEOUT_NOBLOCK);
1356 if (button != BUTTON_NONE) {
1357 break;
1359 if (dopeek) { /* Peek only once per refresh when disk is */
1360 peak_meter_peek(); /* spinning, but as often as possible */
1361 dopeek = highperf; /* otherwise. */
1362 yield();
1363 } else {
1364 sleep(0); /* Sleep until end of current tick. */
1366 if (TIME_AFTER(current_tick, next_refresh)) {
1367 for(i = 0; i < nb_screens; i++)
1369 screens[i].set_viewport(&vps[i]);
1370 peak_meter_screen(&screens[i], x[i], y[i], height[i]);
1371 screens[i].update_viewport_rect(x[i], y[i],
1372 screens[i].getwidth() - x[i],
1373 height[i]);
1375 next_refresh += HZ / PEAK_METER_FPS;
1376 dopeek = true;
1380 return button;
1383 #ifdef PM_DEBUG
1384 static void peak_meter_clear_histogram(void)
1386 int i = 0;
1387 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1388 ticks_per_redraw[i] = (unsigned int)0;
1391 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1392 peeks_per_redraw[i] = (unsigned int)0;
1396 bool peak_meter_histogram(void)
1398 int i;
1399 int btn = BUTTON_NONE;
1400 while ((btn & BUTTON_OFF) != BUTTON_OFF )
1402 unsigned int max = 0;
1403 int y = 0;
1404 int x = 0;
1405 screens[0].clear_display();
1407 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1408 max = MAX(max, peeks_per_redraw[i]);
1411 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1412 x = peeks_per_redraw[i] * (LCD_WIDTH - 1)/ max;
1413 screens[0].hline(0, x, y + i);
1416 y = PEEKS_PER_DRAW_SIZE + 1;
1417 max = 0;
1419 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1420 max = MAX(max, ticks_per_redraw[i]);
1423 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1424 x = ticks_per_redraw[i] * (LCD_WIDTH - 1)/ max;
1425 screens[0].hline(0, x, y + i);
1427 screens[0].update();
1429 btn = button_get(true);
1430 if (btn == BUTTON_PLAY) {
1431 peak_meter_clear_histogram();
1434 return false;
1436 #endif