FS#12076 - DB stats resurrection: If the filename was changed, require
[kugel-rb.git] / apps / recorder / peakmeter.c
blobca1b8c8750d878cf06a1b988c89bc09673a663d1
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 #include "config.h"
22 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
23 #include <stdlib.h> /* sim uses rand for peakmeter simulation */
24 #endif
25 #include "thread.h"
26 #include "kernel.h"
27 #include "settings.h"
28 #include "storage.h"
29 #include "lcd.h"
30 #include "scrollbar.h"
31 #include "button.h"
32 #include "system.h"
33 #include "font.h"
34 #include "icons.h"
35 #include "lang.h"
36 #include "peakmeter.h"
37 #include "audio.h"
38 #include "screen_access.h"
39 #ifdef HAVE_BACKLIGHT
40 #include "backlight.h"
41 #endif
42 #include "action.h"
44 #if CONFIG_CODEC == SWCODEC
45 #include "pcm.h"
47 #ifdef HAVE_RECORDING
48 #include "pcm_record.h"
49 #endif
51 static bool pm_playback = true; /* selects between playback and recording peaks */
52 #endif
54 static struct meter_scales scales[NB_SCREENS];
56 #if !defined(SIMULATOR) && CONFIG_CODEC != SWCODEC
57 /* Data source */
58 static int pm_src_left = MAS_REG_DQPEAK_L;
59 static int pm_src_right = MAS_REG_DQPEAK_R;
60 #endif
62 /* Current values and cumulation */
63 static int pm_cur_left; /* current values (last peak_meter_peek) */
64 static int pm_cur_right;
65 static int pm_max_left; /* maximum values between peak meter draws */
66 static int pm_max_right;
67 #if defined(HAVE_AGC) || defined(HAVE_RECORDING_HISTOGRAM)
68 static int pm_peakhold_left; /* max. peak values between peakhold calls */
69 static int pm_peakhold_right; /* used for AGC and histogram display */
70 #endif
72 /* Clip hold */
73 static bool pm_clip_left = false; /* when true a clip has occurred */
74 static bool pm_clip_right = false;
75 static long pm_clip_timeout_l; /* clip hold timeouts */
76 static long pm_clip_timeout_r;
78 /* Temporarily en- / disables peak meter. This is especially for external
79 applications to detect if the peak_meter is in use and needs drawing at all */
80 static bool peak_meter_enabled = true;
81 void peak_meter_enable(bool enable)
83 peak_meter_enabled = enable;
86 /** Parameters **/
87 /* Range */
88 static unsigned short peak_meter_range_min; /* minimum of range in samples */
89 static unsigned short peak_meter_range_max; /* maximum of range in samples */
90 static unsigned short pm_range; /* range width in samples */
91 static bool pm_use_dbfs = true; /* true if peakmeter displays dBfs */
92 static bool level_check; /* true if peeked at peakmeter before drawing */
93 static unsigned short pm_db_min = 0; /* minimum of range in 1/100 dB */
94 static unsigned short pm_db_max = 9000; /* maximum of range in 1/100 dB */
95 static unsigned short pm_db_range = 9000; /* range width in 1/100 dB */
96 /* Timing behaviour */
97 static int pm_peak_hold = 1; /* peak hold timeout index */
98 static int pm_peak_release = 8; /* peak release in units per read */
99 static int pm_clip_hold = 16; /* clip hold timeout index */
100 static bool pm_clip_eternal = false; /* true if clip timeout is disabled */
102 #ifdef HAVE_RECORDING
103 static unsigned short trig_strt_threshold;
104 static long trig_strt_duration;
105 static long trig_strt_dropout;
106 static unsigned short trig_stp_threshold;
107 static long trig_stp_hold;
108 static long trig_rstrt_gap;
110 /* point in time when the threshold was exceeded */
111 static long trig_hightime;
113 /* point in time when the volume fell below the threshold*/
114 static long trig_lowtime;
116 /* The output value of the trigger. See TRIG_XXX constants for valid values */
117 static int trig_status = TRIG_OFF;
119 static void (*trigger_listener)(int) = NULL;
121 /* clipping counter (only used for recording) */
122 static unsigned int pm_clipcount = 0; /* clipping count */
123 static bool pm_clipcount_active = false; /* counting or not */
124 #endif
126 /* debug only */
127 #ifdef PM_DEBUG
128 static int peek_calls = 0;
130 #define PEEKS_PER_DRAW_SIZE 40
131 static unsigned int peeks_per_redraw[PEEKS_PER_DRAW_SIZE];
133 #define TICKS_PER_DRAW_SIZE 20
134 static unsigned int ticks_per_redraw[TICKS_PER_DRAW_SIZE];
135 #endif
137 static void peak_meter_draw(struct screen *display, struct meter_scales *meter_scales,
138 int x, int y, int width, int height);
140 /* time out values for max */
141 static const short peak_time_out[] = {
142 0 * HZ, HZ / 5, 30, HZ / 2, HZ, 2 * HZ,
143 3 * HZ, 4 * HZ, 5 * HZ, 6 * HZ, 7 * HZ, 8 * HZ,
144 9 * HZ, 10 * HZ, 15 * HZ, 20 * HZ, 30 * HZ, 60 * HZ
147 /* time out values for clip */
148 static const long clip_time_out[] = {
149 0 * HZ, 1 * HZ, 2 * HZ, 3 * HZ, 4 * HZ, 5 * HZ,
150 6 * HZ, 7 * HZ, 8 * HZ, 9 * HZ, 10 * HZ, 15 * HZ,
151 20 * HZ, 25 * HZ, 30 * HZ, 45 * HZ, 60 * HZ, 90 * HZ,
152 120 * HZ, 180 * HZ, 300 * HZ, 600L * HZ, 1200L * HZ,
153 2700L * HZ, 5400L * HZ
156 /* precalculated peak values that represent magical
157 dBfs values. Used to draw the scale */
158 static const short db_scale_src_values[DB_SCALE_SRC_VALUES_SIZE] = {
159 32736, /* 0 db */
160 22752, /* - 3 db */
161 16640, /* - 6 db */
162 11648, /* - 9 db */
163 8320, /* -12 db */
164 4364, /* -18 db */
165 2064, /* -24 db */
166 1194, /* -30 db */
167 363, /* -40 db */
168 101, /* -50 db */
169 34, /* -60 db */
170 0, /* -inf */
173 static int db_scale_count = DB_SCALE_SRC_VALUES_SIZE;
176 * Calculates dB Value for the peak meter, uses peak value as input
177 * @param int sample - The input value
178 * Make sure that 0 <= value < SAMPLE_RANGE
180 * @return int - The 2 digit fixed point result of the euation
181 * 20 * log (sample / SAMPLE_RANGE) + 90
182 * Output range is 0-9000 (that is 0.0 - 90.0 dB).
183 * Normally 0dB is full scale, here it is shifted +90dB.
184 * The calculation is based on the results of a linear
185 * approximation tool written specifically for this problem
186 * by Andreas Zwirtes (radhard@gmx.de). The result has an
187 * accurracy of better than 2%. It is highly runtime optimized,
188 * the cascading if-clauses do an successive approximation on
189 * the input value. This avoids big lookup-tables and
190 * for-loops.
191 * Improved by Jvo Studer for errors < 0.2dB for critical
192 * range of -12dB to 0dB (78.0 to 90.0dB).
195 int calc_db (int isample)
197 /* return n+m*(isample-istart)/100 */
198 int n;
199 long m;
200 int istart;
202 if (isample < 2308) { /* Range 1-5 */
204 if (isample < 115) { /* Range 1-3 */
206 if (isample < 24) {
208 if (isample < 5) {
209 istart = 1; /* Range 1 */
210 n = 98;
211 m = 34950;
213 else {
214 istart = 5; /* Range 2 */
215 n = 1496;
216 m = 7168;
219 else {
220 istart = 24; /* Range 3 */
221 n = 2858;
222 m = 1498;
225 else { /* Range 4-5 */
227 if (isample < 534) {
228 istart = 114; /* Range 4 */
229 n = 4207;
230 m = 319;
232 else {
233 istart = 588; /* Range 5 */
234 n = 5583;
235 m = 69;
240 else { /* Range 6-9 */
242 if (isample < 12932) {
244 if (isample < 6394) {
245 istart = 2608; /* Range 6 */
246 n = 6832;
247 m = 21;
249 else {
250 istart = 7000; /* Range 7 */
251 n = 7682;
252 m = 9;
255 else {
257 if (isample < 22450) {
258 istart = 13000; /* Range 8 */
259 n = 8219;
260 m = 5;
262 else {
263 istart = 22636; /* Range 9 */
264 n = 8697;
265 m = 3;
270 return n + (m * (long)(isample - istart)) / 100L;
275 * A helper function for peak_meter_db2sample. Don't call it separately but
276 * use peak_meter_db2sample. If one or both of min and max are outside the
277 * range 0 <= min (or max) < 8961 the behaviour of this function is
278 * undefined. It may not return.
279 * @param int min - The minimum of the value range that is searched.
280 * @param int max - The maximum of the value range that is searched.
281 * @param int db - The value in dBfs * (-100) for which the according
282 * minimal peak sample is searched.
283 * @return int - A linear volume value with 0 <= value < MAX_PEAK
285 static int db_to_sample_bin_search(int min, int max, int db)
287 int test = min + (max - min) / 2;
289 if (min < max) {
290 if (calc_db(test) < db) {
291 test = db_to_sample_bin_search(test, max, db);
292 } else {
293 if (calc_db(test-1) > db) {
294 test = db_to_sample_bin_search(min, test, db);
298 return test;
302 * Converts a value representing dBfs to a linear
303 * scaled volume info as it is used by the MAS.
304 * An incredibly inefficiant function which is
305 * the vague inverse of calc_db. This really
306 * should be replaced by something better soon.
308 * @param int db - A dBfs * 100 value with
309 * -9000 < value <= 0
310 * @return int - The return value is in the range of
311 * 0 <= return value < MAX_PEAK
313 int peak_meter_db2sample(int db)
315 int retval = 0;
317 /* what is the maximum pseudo db value */
318 int max_peak_db = calc_db(MAX_PEAK - 1);
320 /* range check: db value to big */
321 if (max_peak_db + db < 0) {
322 retval = 0;
325 /* range check: db value too small */
326 else if (max_peak_db + db >= max_peak_db) {
327 retval = MAX_PEAK -1;
330 /* value in range: find the matching linear value */
331 else {
332 retval = db_to_sample_bin_search(0, MAX_PEAK, max_peak_db + db);
334 /* as this is a dirty function anyway, we want to adjust the
335 full scale hit manually to avoid users complaining that when
336 they adjust maximum for 0 dBfs and display it in percent it
337 shows 99%. That is due to precision loss and this is the
338 optical fix */
341 return retval;
345 * Set the min value for restriction of the value range.
346 * @param int newmin - depending whether dBfs is used
347 * newmin is a value in dBfs * 100 or in linear percent values.
348 * for dBfs: -9000 < newmin <= 0
349 * for linear: 0 <= newmin <= 100
351 static void peak_meter_set_min(int newmin)
353 if (pm_use_dbfs) {
354 peak_meter_range_min = peak_meter_db2sample(newmin);
356 } else {
357 if (newmin < peak_meter_range_max) {
358 peak_meter_range_min = newmin * MAX_PEAK / 100;
362 pm_range = peak_meter_range_max - peak_meter_range_min;
364 /* Avoid division by zero. */
365 if (pm_range == 0) {
366 pm_range = 1;
369 pm_db_min = calc_db(peak_meter_range_min);
370 pm_db_range = pm_db_max - pm_db_min;
371 int i;
372 FOR_NB_SCREENS(i)
373 scales[i].db_scale_valid = false;
377 * Returns the minimum value of the range the meter
378 * displays. If the scale is set to dBfs it returns
379 * dBfs values * 100 or linear percent values.
380 * @return: using dBfs : -9000 < value <= 0
381 * using linear scale: 0 <= value <= 100
383 int peak_meter_get_min(void)
385 int retval = 0;
386 if (pm_use_dbfs) {
387 retval = calc_db(peak_meter_range_min) - calc_db(MAX_PEAK - 1);
388 } else {
389 retval = peak_meter_range_min * 100 / MAX_PEAK;
391 return retval;
395 * Set the max value for restriction of the value range.
396 * @param int newmax - depending wether dBfs is used
397 * newmax is a value in dBfs * 100 or in linear percent values.
398 * for dBfs: -9000 < newmax <= 0
399 * for linear: 0 <= newmax <= 100
401 static void peak_meter_set_max(int newmax)
403 if (pm_use_dbfs) {
404 peak_meter_range_max = peak_meter_db2sample(newmax);
405 } else {
406 if (newmax > peak_meter_range_min) {
407 peak_meter_range_max = newmax * MAX_PEAK / 100;
411 pm_range = peak_meter_range_max - peak_meter_range_min;
413 /* Avoid division by zero. */
414 if (pm_range == 0) {
415 pm_range = 1;
418 pm_db_max = calc_db(peak_meter_range_max);
419 pm_db_range = pm_db_max - pm_db_min;
420 int i;
421 FOR_NB_SCREENS(i)
422 scales[i].db_scale_valid = false;
426 * Returns the minimum value of the range the meter
427 * displays. If the scale is set to dBfs it returns
428 * dBfs values * 100 or linear percent values
429 * @return: using dBfs : -9000 < value <= 0
430 * using linear scale: 0 <= value <= 100
432 int peak_meter_get_max(void)
434 int retval = 0;
435 if (pm_use_dbfs) {
436 retval = calc_db(peak_meter_range_max) - calc_db(MAX_PEAK - 1);
437 } else {
438 retval = peak_meter_range_max * 100 / MAX_PEAK;
440 return retval;
444 * Returns whether the meter is currently displaying dBfs or percent values.
445 * @return bool - true if the meter is displaying dBfs
446 false if the meter is displaying percent values.
448 bool peak_meter_get_use_dbfs(void)
450 return pm_use_dbfs;
454 * Specifies whether the values displayed are scaled
455 * as dBfs or as linear percent values.
456 * @param use - set to true for dBfs,
457 * set to false for linear scaling in percent
459 void peak_meter_set_use_dbfs(bool use)
461 int i;
462 pm_use_dbfs = use;
463 FOR_NB_SCREENS(i)
464 scales[i].db_scale_valid = false;
468 * Initialize the range of the meter. Only values
469 * that are in the range of [range_min ... range_max]
470 * are displayed.
471 * @param bool dbfs - set to true for dBfs,
472 * set to false for linear scaling in percent
473 * @param int range_min - Specifies the lower value of the range.
474 * Pass a value dBfs * 100 when dbfs is set to true.
475 * Pass a percent value when dbfs is set to false.
476 * @param int range_max - Specifies the upper value of the range.
477 * Pass a value dBfs * 100 when dbfs is set to true.
478 * Pass a percent value when dbfs is set to false.
480 void peak_meter_init_range( bool dbfs, int range_min, int range_max)
482 pm_use_dbfs = dbfs;
483 peak_meter_set_min(range_min);
484 peak_meter_set_max(range_max);
488 * Initialize the peak meter with all relevant values concerning times.
489 * @param int release - Set the maximum amount of pixels the meter is allowed
490 * to decrease with each redraw
491 * @param int hold - Select the time preset for the time the peak indicator
492 * is reset after a peak occurred. The preset values are
493 * stored in peak_time_out.
494 * @param int clip_hold - Select the time preset for the time the peak
495 * indicator is reset after a peak occurred. The preset
496 * values are stored in clip_time_out.
498 void peak_meter_init_times(int release, int hold, int clip_hold)
500 pm_peak_hold = hold;
501 pm_peak_release = release;
502 pm_clip_hold = clip_hold;
505 #ifdef HAVE_RECORDING
507 * Enable/disable clip counting
509 void pm_activate_clipcount(bool active)
511 pm_clipcount_active = active;
515 * Get clipping counter value
517 int pm_get_clipcount(void)
519 return pm_clipcount;
523 * Set clipping counter to zero (typically at start of recording or playback)
525 void pm_reset_clipcount(void)
527 pm_clipcount = 0;
529 #endif
532 * Set the source of the peak meter to playback or to
533 * record.
534 * @param: bool playback - If true playback peak meter is used.
535 * If false recording peak meter is used.
537 void peak_meter_playback(bool playback)
539 int i;
540 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
541 (void)playback;
542 #elif CONFIG_CODEC == SWCODEC
543 pm_playback = playback;
544 #else
545 if (playback) {
546 pm_src_left = MAS_REG_DQPEAK_L;
547 pm_src_right = MAS_REG_DQPEAK_R;
548 } else {
549 pm_src_left = MAS_REG_QPEAK_L;
550 pm_src_right = MAS_REG_QPEAK_R;
552 #endif
553 /* reset the scales just in case recording and playback
554 use different viewport sizes. Normally we should be checking viewport
555 sizes every time but this will do for now */
556 FOR_NB_SCREENS(i)
557 scales[i].db_scale_valid = false;
560 #ifdef HAVE_RECORDING
561 static void set_trig_status(int new_state)
563 if (trig_status != new_state) {
564 trig_status = new_state;
565 if (trigger_listener != NULL) {
566 trigger_listener(trig_status);
571 #endif
574 * Reads peak values from the MAS, and detects clips. The
575 * values are stored in pm_max_left pm_max_right for later
576 * evauluation. Consecutive calls to peak_meter_peek detect
577 * that ocurred. This function could be used by a thread for
578 * busy reading the MAS.
580 void peak_meter_peek(void)
582 int left, right;
583 #ifdef HAVE_RECORDING
584 bool was_clipping = pm_clip_left || pm_clip_right;
585 #endif
586 /* read current values */
587 #if CONFIG_CODEC == SWCODEC
588 if (pm_playback)
589 pcm_calculate_peaks(&pm_cur_left, &pm_cur_right);
590 #ifdef HAVE_RECORDING
591 else
592 pcm_calculate_rec_peaks(&pm_cur_left, &pm_cur_right);
593 #endif
594 left = pm_cur_left;
595 right = pm_cur_right;
596 #else
597 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
598 pm_cur_left = left = mas_codec_readreg(pm_src_left);
599 pm_cur_right = right = mas_codec_readreg(pm_src_right);
600 #else
601 pm_cur_left = left = 8000;
602 pm_cur_right = right = 9000;
603 #endif
604 #endif
606 /* check for clips
607 An clip is assumed when two consecutive readouts
608 of the volume are at full scale. This is proven
609 to be inaccurate in both ways: it may detect clips
610 when no clip occurred and it may fail to detect
611 a real clip. For software codecs, the peak is already
612 the max of a bunch of samples, so use one max value
613 or you fail to detect clipping! */
614 #if CONFIG_CODEC == SWCODEC
615 if (left == MAX_PEAK - 1) {
616 #else
617 if ((left == pm_max_left) &&
618 (left == MAX_PEAK - 1)) {
619 #endif
620 pm_clip_left = true;
621 pm_clip_timeout_l =
622 current_tick + clip_time_out[pm_clip_hold];
625 #if CONFIG_CODEC == SWCODEC
626 if (right == MAX_PEAK - 1) {
627 #else
628 if ((right == pm_max_right) &&
629 (right == MAX_PEAK - 1)) {
630 #endif
631 pm_clip_right = true;
632 pm_clip_timeout_r =
633 current_tick + clip_time_out[pm_clip_hold];
636 #ifdef HAVE_RECORDING
637 if(!was_clipping && (pm_clip_left || pm_clip_right))
639 if(pm_clipcount_active)
640 pm_clipcount++;
642 #endif
644 /* peaks are searched -> we have to find the maximum. When
645 many calls of peak_meter_peek the maximum value will be
646 stored in pm_max_xxx. This maximum is reset by the
647 functions peak_meter_read_x. */
648 pm_max_left = MAX(pm_max_left, left);
649 pm_max_right = MAX(pm_max_right, right);
651 #ifdef HAVE_RECORDING
652 #if CONFIG_CODEC == SWCODEC
653 /* Ignore any unread peakmeter data */
654 #define MAX_DROP_TIME HZ/7 /* this value may need tweaking. Increase if you are
655 getting trig events when you shouldn't with
656 trig_stp_hold = 0 */
657 if (!trig_stp_hold)
658 trig_stp_hold = MAX_DROP_TIME;
659 #endif
661 switch (trig_status) {
662 case TRIG_READY:
663 /* no more changes, if trigger was activated as release trigger */
664 /* threshold exceeded? */
665 if ((left > trig_strt_threshold)
666 || (right > trig_strt_threshold)) {
667 /* reset trigger duration */
668 trig_hightime = current_tick;
670 /* reset dropout duration */
671 trig_lowtime = current_tick;
673 if (trig_strt_duration)
674 set_trig_status(TRIG_STEADY);
675 else
676 /* if trig_duration is set to 0 the user wants to start
677 recording immediately */
678 set_trig_status(TRIG_GO);
680 break;
682 case TRIG_STEADY:
683 case TRIG_RETRIG:
684 /* trigger duration exceeded */
685 if (current_tick - trig_hightime > trig_strt_duration) {
686 set_trig_status(TRIG_GO);
687 } else {
688 /* threshold exceeded? */
689 if ((left > trig_strt_threshold)
690 || (right > trig_strt_threshold)) {
691 /* reset lowtime */
692 trig_lowtime = current_tick;
694 /* volume is below threshold */
695 else {
696 /* dropout occurred? */
697 if (current_tick - trig_lowtime > trig_strt_dropout){
698 if (trig_status == TRIG_STEADY){
699 set_trig_status(TRIG_READY);
701 /* trig_status == TRIG_RETRIG */
702 else {
703 /* the gap has already expired */
704 trig_lowtime = current_tick - trig_rstrt_gap - 1;
705 set_trig_status(TRIG_POSTREC);
710 break;
712 case TRIG_GO:
713 case TRIG_CONTINUE:
714 /* threshold exceeded? */
715 if ((left > trig_stp_threshold)
716 || (right > trig_stp_threshold)) {
717 /* restart hold time countdown */
718 trig_lowtime = current_tick;
719 #if CONFIG_CODEC == SWCODEC
720 } else if (current_tick - trig_lowtime > MAX_DROP_TIME){
721 #else
722 } else {
723 #endif
724 set_trig_status(TRIG_POSTREC);
725 trig_hightime = current_tick;
727 break;
729 case TRIG_POSTREC:
730 /* gap time expired? */
731 if (current_tick - trig_lowtime > trig_rstrt_gap){
732 /* start threshold exceeded? */
733 if ((left > trig_strt_threshold)
734 || (right > trig_strt_threshold)) {
736 set_trig_status(TRIG_RETRIG);
737 trig_hightime = current_tick;
738 trig_lowtime = current_tick;
740 else
742 /* stop threshold exceeded */
743 if ((left > trig_stp_threshold)
744 || (right > trig_stp_threshold)) {
745 if (current_tick - trig_hightime > trig_stp_hold){
746 trig_lowtime = current_tick;
747 set_trig_status(TRIG_CONTINUE);
748 } else {
749 trig_lowtime = current_tick - trig_rstrt_gap - 1;
753 /* below any threshold */
754 else {
755 if (current_tick - trig_lowtime > trig_stp_hold){
756 set_trig_status(TRIG_READY);
757 } else {
758 trig_hightime = current_tick;
763 /* still within the gap time */
764 else {
765 /* stop threshold exceeded */
766 if ((left > trig_stp_threshold)
767 || (right > trig_stp_threshold)) {
768 set_trig_status(TRIG_CONTINUE);
769 trig_lowtime = current_tick;
772 /* hold time expired */
773 else if (current_tick - trig_lowtime > trig_stp_hold){
774 trig_hightime = current_tick;
775 trig_lowtime = current_tick;
776 set_trig_status(TRIG_READY);
779 break;
781 #if CONFIG_CODEC == SWCODEC
782 /* restore stop hold value */
783 if (trig_stp_hold == MAX_DROP_TIME)
784 trig_stp_hold = 0;
785 #endif
786 #endif
787 /* check levels next time peakmeter drawn */
788 level_check = true;
789 #ifdef PM_DEBUG
790 peek_calls++;
791 #endif
795 * Reads out the peak volume of the left channel.
796 * @return int - The maximum value that has been detected
797 * since the last call of peak_meter_read_l. The value
798 * is in the range 0 <= value < MAX_PEAK.
800 static int peak_meter_read_l(void)
802 /* pm_max_left contains the maximum of all peak values that were read
803 by peak_meter_peek since the last call of peak_meter_read_l */
804 int retval;
806 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
807 srand(current_tick);
808 pm_max_left = rand()%MAX_PEAK;
809 #endif
811 retval = pm_max_left;
813 #if defined(HAVE_RECORDING_HISTOGRAM) || defined(HAVE_AGC)
814 /* store max peak value for peak_meter_get_peakhold_x readout */
815 pm_peakhold_left = MAX(pm_max_left, pm_peakhold_left);
816 #endif
817 #ifdef PM_DEBUG
818 peek_calls = 0;
819 #endif
820 /* reset pm_max_left so that subsequent calls of peak_meter_peek don't
821 get fooled by an old maximum value */
822 pm_max_left = pm_cur_left;
824 return retval;
828 * Reads out the peak volume of the right channel.
829 * @return int - The maximum value that has been detected
830 * since the last call of peak_meter_read_l. The value
831 * is in the range 0 <= value < MAX_PEAK.
833 static int peak_meter_read_r(void)
835 /* peak_meter_r contains the maximum of all peak values that were read
836 by peak_meter_peek since the last call of peak_meter_read_r */
837 int retval;
839 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
840 srand(current_tick);
841 pm_max_right = rand()%MAX_PEAK;
842 #endif
844 retval = pm_max_right;
846 #if defined(HAVE_RECORDING_HISTOGRAM) || defined(HAVE_AGC)
847 /* store max peak value for peak_meter_get_peakhold_x readout */
848 pm_peakhold_right = MAX(pm_max_right, pm_peakhold_right);
849 #endif
850 #ifdef PM_DEBUG
851 peek_calls = 0;
852 #endif
853 /* reset pm_max_right so that subsequent calls of peak_meter_peek don't
854 get fooled by an old maximum value */
855 pm_max_right = pm_cur_right;
857 return retval;
860 #if defined(HAVE_AGC) || defined(HAVE_RECORDING_HISTOGRAM)
862 * Reads out the current peak-hold values since the last call.
863 * This is used by the histogram feature in the recording screen.
864 * Values are in the range 0 <= peak_x < MAX_PEAK. MAX_PEAK is typ 32767.
866 void peak_meter_get_peakhold(int *peak_left, int *peak_right)
868 if (peak_left)
869 *peak_left = pm_peakhold_left;
870 if (peak_right)
871 *peak_right = pm_peakhold_right;
872 pm_peakhold_left = 0;
873 pm_peakhold_right = 0;
875 #endif
878 * Reset the detected clips. This method is for
879 * use by the user interface.
880 * @param int unused - This parameter was added to
881 * make the function compatible with set_int
883 void peak_meter_set_clip_hold(int time)
885 pm_clip_left = false;
886 pm_clip_right = false;
887 pm_clip_eternal = (time > 0) ? false : true;
891 * Scales a peak value as read from the MAS to the range of meterwidth.
892 * The scaling is performed according to the scaling method (dBfs / linear)
893 * and the range (peak_meter_range_min .. peak_meter_range_max).
894 * @param unsigned short val - The volume value. Range: 0 <= val < MAX_PEAK
895 * @param int meterwidht - The widht of the meter in pixel
896 * @return unsigned short - A value 0 <= return value <= meterwidth
898 unsigned short peak_meter_scale_value(unsigned short val, int meterwidth)
900 int retval;
902 if (val <= peak_meter_range_min) {
903 return 0;
906 if (val >= peak_meter_range_max) {
907 return meterwidth;
910 retval = val;
912 /* different scaling is used for dBfs and linear percent */
913 if (pm_use_dbfs) {
915 /* scale the samples dBfs */
916 retval = (calc_db(retval) - pm_db_min) * meterwidth / pm_db_range;
919 /* Scale for linear percent display */
920 else
922 /* scale the samples */
923 retval = ((retval - peak_meter_range_min) * meterwidth)
924 / pm_range;
926 return retval;
928 void peak_meter_screen(struct screen *display, int x, int y, int height)
930 peak_meter_draw(display, &scales[display->screen_type], x, y,
931 display->getwidth() - x, height);
934 /* sets *left and *right to the current *unscaled* values */
935 void peak_meter_current_vals(int *left, int *right)
937 static int left_level = 0, right_level = 0;
938 if (level_check){
939 /* only read the volume info from MAS if peek since last read*/
940 left_level = peak_meter_read_l();
941 right_level = peak_meter_read_r();
942 level_check = false;
944 *left = left_level;
945 *right = right_level;
949 * Draws a peak meter in the specified size at the specified position.
950 * @param int x - The x coordinate.
951 * Make sure that 0 <= x and x + width < display->getwidth()
952 * @param int y - The y coordinate.
953 * Make sure that 0 <= y and y + height < display->getheight()
954 * @param int width - The width of the peak meter. Note that for display
955 * of clips a 3 pixel wide area is used ->
956 * width > 3
957 * @param int height - The height of the peak meter. height > 3
959 static void peak_meter_draw(struct screen *display, struct meter_scales *scales,
960 int x, int y, int width, int height)
962 int left_level = 0, right_level = 0;
963 int left = 0, right = 0;
964 int meterwidth = width - 3;
965 int i, delta;
966 #if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
967 static long peak_release_tick[2] = {0,0};
968 int screen_nr = display->screen_type == SCREEN_MAIN ? 0 : 1;
969 #else
970 static long peak_release_tick = 0;
971 #endif
973 #ifdef PM_DEBUG
974 static long pm_tick = 0;
975 int tmp = peek_calls;
976 #endif
978 /* if disabled only draw the peak meter */
979 if (peak_meter_enabled) {
982 peak_meter_current_vals(&left_level, &right_level);
984 /* scale the samples dBfs */
985 left = peak_meter_scale_value(left_level, meterwidth);
986 right = peak_meter_scale_value(right_level, meterwidth);
988 /*if the scale has changed -> recalculate the scale
989 (The scale becomes invalid when the range changed.) */
990 if (!scales->db_scale_valid){
992 if (pm_use_dbfs) {
993 db_scale_count = DB_SCALE_SRC_VALUES_SIZE;
994 for (i = 0; i < db_scale_count; i++){
995 /* find the real x-coords for predefined interesting
996 dBfs values. These only are recalculated when the
997 scaling of the meter changed. */
998 scales->db_scale_lcd_coord[i] =
999 peak_meter_scale_value(
1000 db_scale_src_values[i],
1001 meterwidth - 1);
1005 /* when scaling linear we simly make 10% steps */
1006 else {
1007 db_scale_count = 10;
1008 for (i = 0; i < db_scale_count; i++) {
1009 scales->db_scale_lcd_coord[i] =
1010 (i * (MAX_PEAK / 10) - peak_meter_range_min) *
1011 meterwidth / pm_range;
1015 /* mark scale valid to avoid recalculating dBfs values
1016 of the scale. */
1017 scales->db_scale_valid = true;
1020 /* apply release */
1021 #if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
1022 delta = current_tick - peak_release_tick[screen_nr];
1023 peak_release_tick[screen_nr] = current_tick;
1024 #else
1025 delta = current_tick - peak_release_tick;
1026 peak_release_tick = current_tick;
1027 #endif
1028 left = MAX(left , scales->last_left - delta * pm_peak_release);
1029 right = MAX(right, scales->last_right - delta * pm_peak_release);
1031 /* reset max values after timeout */
1032 if (TIME_AFTER(current_tick, scales->pm_peak_timeout_l)){
1033 scales->pm_peak_left = 0;
1036 if (TIME_AFTER(current_tick, scales->pm_peak_timeout_r)){
1037 scales->pm_peak_right = 0;
1040 if (!pm_clip_eternal) {
1041 if (pm_clip_left &&
1042 TIME_AFTER(current_tick, pm_clip_timeout_l)){
1043 pm_clip_left = false;
1046 if (pm_clip_right &&
1047 TIME_AFTER(current_tick, pm_clip_timeout_r)){
1048 pm_clip_right = false;
1052 /* check for new max values */
1053 if (left > scales->pm_peak_left) {
1054 scales->pm_peak_left = left - 1;
1055 scales->pm_peak_timeout_l =
1056 current_tick + peak_time_out[pm_peak_hold];
1059 if (right > scales->pm_peak_right) {
1060 scales->pm_peak_right = right - 1;
1061 scales->pm_peak_timeout_r =
1062 current_tick + peak_time_out[pm_peak_hold];
1066 /* draw the peak meter */
1067 display->set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1068 display->fillrect(x, y, width, height);
1069 display->set_drawmode(DRMODE_SOLID);
1071 /* draw left */
1072 display->fillrect (x, y, left, height / 2 - 2 );
1073 if (scales->pm_peak_left > 0) {
1074 display->vline(x + scales->pm_peak_left, y, y + height / 2 - 2 );
1076 if (pm_clip_left) {
1077 display->fillrect(x + meterwidth, y, 3, height / 2 - 1);
1080 /* draw right */
1081 display->fillrect(x, y + height / 2 + 1, right, height / 2 - 2);
1082 if (scales->pm_peak_right > 0) {
1083 display->vline( x + scales->pm_peak_right, y + height / 2, y + height - 2);
1085 if (pm_clip_right) {
1086 display->fillrect(x + meterwidth, y + height / 2, 3, height / 2 - 1);
1089 /* draw scale end */
1090 display->vline(x + meterwidth, y, y + height - 2);
1092 /* draw dots for scale marks */
1093 for (i = 0; i < db_scale_count; i++) {
1094 /* The x-coordinates of interesting scale mark points
1095 have been calculated before */
1096 display->drawpixel(x + scales->db_scale_lcd_coord[i],
1097 y + height / 2 - 1);
1100 #ifdef HAVE_RECORDING
1102 #ifdef HAVE_BACKLIGHT
1103 /* cliplight */
1104 if ((pm_clip_left || pm_clip_right) &&
1105 global_settings.cliplight &&
1106 #if CONFIG_CODEC == SWCODEC
1107 !pm_playback)
1108 #else
1109 !(audio_status() & (AUDIO_STATUS_PLAY | AUDIO_STATUS_ERROR)))
1110 #endif
1112 /* if clipping, cliplight setting on and in recording screen */
1113 if (global_settings.cliplight <= 2)
1115 /* turn on main unit light if setting set to main or both*/
1116 backlight_on();
1118 #ifdef HAVE_REMOTE_LCD
1119 if (global_settings.cliplight >= 2)
1121 /* turn remote light unit on if setting set to remote or both */
1122 remote_backlight_on();
1124 #endif /* HAVE_REMOTE_LCD */
1126 #endif /* HAVE_BACKLIGHT */
1128 if (trig_status != TRIG_OFF) {
1129 int start_trigx, stop_trigx, ycenter;
1131 display->set_drawmode(DRMODE_SOLID);
1132 ycenter = y + height / 2;
1133 /* display threshold value */
1134 start_trigx = x+peak_meter_scale_value(trig_strt_threshold,meterwidth);
1135 display->vline(start_trigx, ycenter - 2, ycenter);
1136 start_trigx ++;
1137 if (start_trigx < display->getwidth() ) display->drawpixel(start_trigx,
1138 ycenter - 1);
1140 stop_trigx = x + peak_meter_scale_value(trig_stp_threshold,meterwidth);
1141 display->vline(stop_trigx, ycenter - 2, ycenter);
1142 if (stop_trigx > 0) display->drawpixel(stop_trigx - 1, ycenter - 1);
1144 #endif /*HAVE_RECORDING*/
1146 #ifdef PM_DEBUG
1147 /* display a bar to show how many calls to peak_meter_peek
1148 have ocurred since the last display */
1149 display->set_drawmode(DRMODE_COMPLEMENT);
1150 display->fillrect(x, y, tmp, 3);
1152 if (tmp < PEEKS_PER_DRAW_SIZE) {
1153 peeks_per_redraw[tmp]++;
1156 tmp = current_tick - pm_tick;
1157 if (tmp < TICKS_PER_DRAW_SIZE ){
1158 ticks_per_redraw[tmp] ++;
1161 /* display a bar to show how many ticks have passed since
1162 the last redraw */
1163 display->fillrect(x, y + height / 2, current_tick - pm_tick, 2);
1164 pm_tick = current_tick;
1165 #endif
1167 scales->last_left = left;
1168 scales->last_right = right;
1170 display->set_drawmode(DRMODE_SOLID);
1173 #ifdef HAVE_RECORDING
1175 * Defines the parameters of the trigger. After these parameters are defined
1176 * the trigger can be started either by peak_meter_attack_trigger or by
1177 * peak_meter_release_trigger. Note that you can pass either linear (%) or
1178 * logarithmic (db) values to the thresholds. Positive values are intepreted as
1179 * percent (0 is 0% .. 100 is 100%). Negative values are interpreted as db.
1180 * To avoid ambiguosity of the value 0 the negative values are shifted by -1.
1181 * Thus -75 is -74db .. -1 is 0db.
1182 * @param start_threshold - The threshold used for attack trigger. Negative
1183 * values are interpreted as db -1, positive as %.
1184 * @param start_duration - The minimum time span within which start_threshold
1185 * must be exceeded to fire the attack trigger.
1186 * @param start_dropout - The maximum time span the level may fall below
1187 * start_threshold without releasing the attack trigger.
1188 * @param stop_threshold - The threshold the volume must fall below to release
1189 * the release trigger.Negative values are
1190 * interpreted as db -1, positive as %.
1191 * @param stop_hold - The minimum time the volume must fall below the
1192 * stop_threshold to release the trigger.
1193 * @param
1195 void peak_meter_define_trigger(
1196 int start_threshold,
1197 long start_duration,
1198 long start_dropout,
1199 int stop_threshold,
1200 long stop_hold_time,
1201 long restart_gap
1204 if (start_threshold < 0) {
1205 /* db */
1206 if (start_threshold < -89) {
1207 trig_strt_threshold = 0;
1208 } else {
1209 trig_strt_threshold =peak_meter_db2sample((start_threshold+1)*100);
1211 } else {
1212 /* linear percent */
1213 trig_strt_threshold = start_threshold * MAX_PEAK / 100;
1215 trig_strt_duration = start_duration;
1216 trig_strt_dropout = start_dropout;
1217 if (stop_threshold < 0) {
1218 /* db */
1219 trig_stp_threshold = peak_meter_db2sample((stop_threshold + 1) * 100);
1220 } else {
1221 /* linear percent */
1222 trig_stp_threshold = stop_threshold * MAX_PEAK / 100;
1224 trig_stp_hold = stop_hold_time;
1225 trig_rstrt_gap = restart_gap;
1229 * Enables or disables the trigger.
1230 * @param on - If true the trigger is turned on.
1232 void peak_meter_trigger(bool on)
1234 /* don't use set_trigger here as that would fire an undesired event */
1235 trig_status = on ? TRIG_READY : TRIG_OFF;
1239 * Registers the listener function that listenes on trig_status changes.
1240 * @param listener - The function that is called with each change of
1241 * trig_status. May be set to NULL if no callback is desired.
1243 void peak_meter_set_trigger_listener(void (*listener)(int status))
1245 trigger_listener = listener;
1249 * Fetches the status of the trigger.
1250 * TRIG_OFF: the trigger is inactive
1251 * TRIG_RELEASED: The volume level is below the threshold
1252 * TRIG_ACTIVATED: The volume level has exceeded the threshold, but the trigger
1253 * hasn't been fired yet.
1254 * TRIG_FIRED: The volume exceeds the threshold
1256 * To activate the trigger call either peak_meter_attack_trigger or
1257 * peak_meter_release_trigger. To turn the trigger off call
1258 * peak_meter_trigger_off.
1260 int peak_meter_trigger_status(void)
1262 return trig_status; /* & TRIG_PIT_MASK;*/
1265 void peak_meter_draw_trig(int xpos[], int ypos[],
1266 int trig_width[], int nb_screens)
1268 int barstart[NB_SCREENS];
1269 int barend[NB_SCREENS];
1270 int icon;
1271 int ixpos[NB_SCREENS];
1272 int i;
1273 int trigbar_width[NB_SCREENS];
1275 FOR_NB_SCREENS(i)
1276 trigbar_width[i] = (trig_width[i] - (2 * (ICON_PLAY_STATE_WIDTH + 1)));
1278 switch (trig_status) {
1280 case TRIG_READY:
1281 FOR_NB_SCREENS(i){
1282 barstart[i] = 0;
1283 barend[i] = 0;
1285 icon = Icon_Stop;
1286 FOR_NB_SCREENS(i)
1287 ixpos[i] = xpos[i];
1288 break;
1290 case TRIG_STEADY:
1291 case TRIG_RETRIG:
1292 FOR_NB_SCREENS(i)
1294 barstart[i] = 0;
1295 barend[i] = (trig_strt_duration == 0) ? trigbar_width[i] :
1296 trigbar_width[i] *
1297 (current_tick - trig_hightime) / trig_strt_duration;
1299 icon = Icon_Stop;
1300 FOR_NB_SCREENS(i)
1301 ixpos[i] = xpos[i];
1302 break;
1304 case TRIG_GO:
1305 case TRIG_CONTINUE:
1306 FOR_NB_SCREENS(i)
1308 barstart[i] = trigbar_width[i];
1309 barend[i] = trigbar_width[i];
1311 icon = Icon_Record;
1312 FOR_NB_SCREENS(i)
1313 ixpos[i] = xpos[i]+ trig_width[i] - ICON_PLAY_STATE_WIDTH;
1314 break;
1316 case TRIG_POSTREC:
1317 FOR_NB_SCREENS(i)
1319 barstart[i] = (trig_stp_hold == 0) ? 0 :
1320 trigbar_width[i] - trigbar_width[i] *
1321 (current_tick - trig_lowtime) / trig_stp_hold;
1322 barend[i] = trigbar_width[i];
1324 icon = Icon_Record;
1325 FOR_NB_SCREENS(i)
1326 ixpos[i] = xpos[i] + trig_width[i] - ICON_PLAY_STATE_WIDTH;
1327 break;
1329 default:
1330 return;
1333 for(i = 0; i < nb_screens; i++)
1335 gui_scrollbar_draw(&screens[i], xpos[i] + ICON_PLAY_STATE_WIDTH + 1,
1336 ypos[i] + 1, trigbar_width[i], TRIG_HEIGHT - 2,
1337 trigbar_width[i], barstart[i], barend[i],
1338 HORIZONTAL);
1340 screens[i].mono_bitmap(bitmap_icons_7x8[icon], ixpos[i], ypos[i],
1341 ICON_PLAY_STATE_WIDTH, STATUSBAR_HEIGHT);
1344 #endif
1346 int peak_meter_draw_get_btn(int action_context, int x[], int y[],
1347 int height[], int nb_screens,
1348 struct viewport vps[])
1350 int button = BUTTON_NONE;
1351 long next_refresh = current_tick;
1352 long next_big_refresh = current_tick + HZ / 10;
1353 int i;
1354 #if (CONFIG_CODEC == SWCODEC)
1355 bool highperf = false;
1356 #else
1357 /* On MAS targets, we need to poll as often as possible in order to not
1358 * miss a peak, as the MAS does only provide a quasi-peak. When the disk
1359 * is active, it must not draw too much CPU power or a buffer overrun can
1360 * happen when saving a recording. As a compromise, poll only once per tick
1361 * when the disk is active, otherwise spin around as fast as possible. */
1362 bool highperf = !storage_disk_is_active();
1363 #endif
1364 bool dopeek = true;
1366 while (TIME_BEFORE(current_tick, next_big_refresh)) {
1367 button = get_action(action_context, TIMEOUT_NOBLOCK);
1368 if (button != BUTTON_NONE) {
1369 break;
1371 if (dopeek) { /* Peek only once per refresh when disk is */
1372 peak_meter_peek(); /* spinning, but as often as possible */
1373 dopeek = highperf; /* otherwise. */
1374 yield();
1375 } else {
1376 sleep(0); /* Sleep until end of current tick. */
1378 if (TIME_AFTER(current_tick, next_refresh)) {
1379 for(i = 0; i < nb_screens; i++)
1381 screens[i].set_viewport(&vps[i]);
1382 peak_meter_screen(&screens[i], x[i], y[i], height[i]);
1383 screens[i].update_viewport_rect(x[i], y[i],
1384 screens[i].getwidth() - x[i],
1385 height[i]);
1387 next_refresh += HZ / PEAK_METER_FPS;
1388 dopeek = true;
1392 return button;
1395 #ifdef PM_DEBUG
1396 static void peak_meter_clear_histogram(void)
1398 int i = 0;
1399 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1400 ticks_per_redraw[i] = (unsigned int)0;
1403 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1404 peeks_per_redraw[i] = (unsigned int)0;
1408 bool peak_meter_histogram(void)
1410 int i;
1411 int btn = BUTTON_NONE;
1412 while ((btn & BUTTON_OFF) != BUTTON_OFF )
1414 unsigned int max = 0;
1415 int y = 0;
1416 int x = 0;
1417 screens[0].clear_display();
1419 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1420 max = MAX(max, peeks_per_redraw[i]);
1423 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1424 x = peeks_per_redraw[i] * (LCD_WIDTH - 1)/ max;
1425 screens[0].hline(0, x, y + i);
1428 y = PEEKS_PER_DRAW_SIZE + 1;
1429 max = 0;
1431 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1432 max = MAX(max, ticks_per_redraw[i]);
1435 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1436 x = ticks_per_redraw[i] * (LCD_WIDTH - 1)/ max;
1437 screens[0].hline(0, x, y + i);
1439 screens[0].update();
1441 btn = button_get(true);
1442 if (btn == BUTTON_PLAY) {
1443 peak_meter_clear_histogram();
1446 return false;
1448 #endif