Fix FS #9128: invalidate the peakmeter scales when switching between playback and...
[Rockbox.git] / apps / recorder / peakmeter.c
blob29be704f0cc8325df0553c6b4c6d5e56a6c68011
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 "ata.h"
30 #include "lcd.h"
31 #include "scrollbar.h"
32 #include "gwps.h"
33 #include "sprintf.h"
34 #include "button.h"
35 #include "system.h"
36 #include "font.h"
37 #include "icons.h"
38 #include "lang.h"
39 #include "peakmeter.h"
40 #include "audio.h"
41 #include "screen_access.h"
42 #ifdef HAVE_BACKLIGHT
43 #include "backlight.h"
44 #endif
45 #include "action.h"
47 #if CONFIG_CODEC == SWCODEC
48 #include "pcm.h"
50 #ifdef HAVE_RECORDING
51 #include "pcm_record.h"
52 #endif
54 static bool pm_playback = true; /* selects between playback and recording peaks */
55 #endif
57 static struct meter_scales scales[NB_SCREENS];
59 #if !defined(SIMULATOR) && CONFIG_CODEC != SWCODEC
60 /* Data source */
61 static int pm_src_left = MAS_REG_DQPEAK_L;
62 static int pm_src_right = MAS_REG_DQPEAK_R;
63 #endif
65 /* Current values and cumulation */
66 static int pm_cur_left; /* current values (last peak_meter_peek) */
67 static int pm_cur_right;
68 static int pm_max_left; /* maximum values between peak meter draws */
69 static int pm_max_right;
70 #ifdef HAVE_AGC
71 static int pm_peakhold_left; /* max. peak values between peakhold calls */
72 static int pm_peakhold_right; /* used for AGC and histogram display */
73 #endif
75 /* Clip hold */
76 static bool pm_clip_left = false; /* when true a clip has occurred */
77 static bool pm_clip_right = false;
78 static long pm_clip_timeout_l; /* clip hold timeouts */
79 static long pm_clip_timeout_r;
81 /* Temporarily en- / disables peak meter. This is especially for external
82 applications to detect if the peak_meter is in use and needs drawing at all */
83 bool peak_meter_enabled = true;
85 /** Parameters **/
86 /* Range */
87 static unsigned short peak_meter_range_min; /* minimum of range in samples */
88 static unsigned short peak_meter_range_max; /* maximum of range in samples */
89 static unsigned short pm_range; /* range width in samples */
90 static bool pm_use_dbfs = true; /* true if peakmeter displays dBfs */
91 static bool level_check; /* true if peeked at peakmeter before drawing */
92 static unsigned short pm_db_min = 0; /* minimum of range in 1/100 dB */
93 static unsigned short pm_db_max = 9000; /* maximum of range in 1/100 dB */
94 static unsigned short pm_db_range = 9000; /* range width in 1/100 dB */
95 /* Timing behaviour */
96 static int pm_peak_hold = 1; /* peak hold timeout index */
97 static int pm_peak_release = 8; /* peak release in units per read */
98 static int pm_clip_hold = 16; /* clip hold timeout index */
99 static bool pm_clip_eternal = false; /* true if clip timeout is disabled */
101 #ifdef HAVE_RECORDING
102 static unsigned short trig_strt_threshold;
103 static long trig_strt_duration;
104 static long trig_strt_dropout;
105 static unsigned short trig_stp_threshold;
106 static long trig_stp_hold;
107 static long trig_rstrt_gap;
109 /* point in time when the threshold was exceeded */
110 static long trig_hightime;
112 /* point in time when the volume fell below the threshold*/
113 static long trig_lowtime;
115 /* The output value of the trigger. See TRIG_XXX constants for valid values */
116 static int trig_status = TRIG_OFF;
118 static void (*trigger_listener)(int) = NULL;
120 /* clipping counter (only used for recording) */
121 static unsigned int pm_clipcount = 0; /* clipping count */
122 static bool pm_clipcount_active = false; /* counting or not */
123 #endif
125 /* debug only */
126 #ifdef PM_DEBUG
127 static int peek_calls = 0;
129 #define PEEKS_PER_DRAW_SIZE 40
130 static unsigned int peeks_per_redraw[PEEKS_PER_DRAW_SIZE];
132 #define TICKS_PER_DRAW_SIZE 20
133 static unsigned int ticks_per_redraw[TICKS_PER_DRAW_SIZE];
134 #endif
136 static void peak_meter_draw(struct screen *display, struct meter_scales *meter_scales,
137 int x, int y, int width, int height);
139 /* time out values for max */
140 static const short peak_time_out[] = {
141 0 * HZ, HZ / 5, 30, HZ / 2, HZ, 2 * HZ,
142 3 * HZ, 4 * HZ, 5 * HZ, 6 * HZ, 7 * HZ, 8 * HZ,
143 9 * HZ, 10 * HZ, 15 * HZ, 20 * HZ, 30 * HZ, 60 * HZ
146 /* time out values for clip */
147 static const long clip_time_out[] = {
148 0 * HZ, 1 * HZ, 2 * HZ, 3 * HZ, 4 * HZ, 5 * HZ,
149 6 * HZ, 7 * HZ, 8 * HZ, 9 * HZ, 10 * HZ, 15 * HZ,
150 20 * HZ, 25 * HZ, 30 * HZ, 45 * HZ, 60 * HZ, 90 * HZ,
151 120 * HZ, 180 * HZ, 300 * HZ, 600L * HZ, 1200L * HZ,
152 2700L * HZ, 5400L * HZ
155 /* precalculated peak values that represent magical
156 dBfs values. Used to draw the scale */
157 static const short db_scale_src_values[DB_SCALE_SRC_VALUES_SIZE] = {
158 32736, /* 0 db */
159 22752, /* - 3 db */
160 16640, /* - 6 db */
161 11648, /* - 9 db */
162 8320, /* -12 db */
163 4364, /* -18 db */
164 2064, /* -24 db */
165 1194, /* -30 db */
166 363, /* -40 db */
167 101, /* -50 db */
168 34, /* -60 db */
169 0, /* -inf */
172 static int db_scale_count = DB_SCALE_SRC_VALUES_SIZE;
175 * Calculates dB Value for the peak meter, uses peak value as input
176 * @param int sample - The input value
177 * Make sure that 0 <= value < SAMPLE_RANGE
179 * @return int - The 2 digit fixed point result of the euation
180 * 20 * log (sample / SAMPLE_RANGE) + 90
181 * Output range is 0-9000 (that is 0.0 - 90.0 dB).
182 * Normally 0dB is full scale, here it is shifted +90dB.
183 * The calculation is based on the results of a linear
184 * approximation tool written specifically for this problem
185 * by Andreas Zwirtes (radhard@gmx.de). The result has an
186 * accurracy of better than 2%. It is highly runtime optimized,
187 * the cascading if-clauses do an successive approximation on
188 * the input value. This avoids big lookup-tables and
189 * for-loops.
190 * Improved by Jvo Studer for errors < 0.2dB for critical
191 * range of -12dB to 0dB (78.0 to 90.0dB).
194 int calc_db (int isample)
196 /* return n+m*(isample-istart)/100 */
197 int n;
198 long m;
199 int istart;
201 if (isample < 2308) { /* Range 1-5 */
203 if (isample < 115) { /* Range 1-3 */
205 if (isample < 24) {
207 if (isample < 5) {
208 istart = 1; /* Range 1 */
209 n = 98;
210 m = 34950;
212 else {
213 istart = 5; /* Range 2 */
214 n = 1496;
215 m = 7168;
218 else {
219 istart = 24; /* Range 3 */
220 n = 2858;
221 m = 1498;
224 else { /* Range 4-5 */
226 if (isample < 534) {
227 istart = 114; /* Range 4 */
228 n = 4207;
229 m = 319;
231 else {
232 istart = 588; /* Range 5 */
233 n = 5583;
234 m = 69;
239 else { /* Range 6-9 */
241 if (isample < 12932) {
243 if (isample < 6394) {
244 istart = 2608; /* Range 6 */
245 n = 6832;
246 m = 21;
248 else {
249 istart = 7000; /* Range 7 */
250 n = 7682;
251 m = 9;
254 else {
256 if (isample < 22450) {
257 istart = 13000; /* Range 8 */
258 n = 8219;
259 m = 5;
261 else {
262 istart = 22636; /* Range 9 */
263 n = 8697;
264 m = 3;
269 return n + (m * (long)(isample - istart)) / 100L;
274 * A helper function for peak_meter_db2sample. Don't call it separately but
275 * use peak_meter_db2sample. If one or both of min and max are outside the
276 * range 0 <= min (or max) < 8961 the behaviour of this function is
277 * undefined. It may not return.
278 * @param int min - The minimum of the value range that is searched.
279 * @param int max - The maximum of the value range that is searched.
280 * @param int db - The value in dBfs * (-100) for which the according
281 * minimal peak sample is searched.
282 * @return int - A linear volume value with 0 <= value < MAX_PEAK
284 static int db_to_sample_bin_search(int min, int max, int db)
286 int test = min + (max - min) / 2;
288 if (min < max) {
289 if (calc_db(test) < db) {
290 test = db_to_sample_bin_search(test, max, db);
291 } else {
292 if (calc_db(test-1) > db) {
293 test = db_to_sample_bin_search(min, test, db);
297 return test;
301 * Converts a value representing dBfs to a linear
302 * scaled volume info as it is used by the MAS.
303 * An incredibly inefficiant function which is
304 * the vague inverse of calc_db. This really
305 * should be replaced by something better soon.
307 * @param int db - A dBfs * 100 value with
308 * -9000 < value <= 0
309 * @return int - The return value is in the range of
310 * 0 <= return value < MAX_PEAK
312 int peak_meter_db2sample(int db)
314 int retval = 0;
316 /* what is the maximum pseudo db value */
317 int max_peak_db = calc_db(MAX_PEAK - 1);
319 /* range check: db value to big */
320 if (max_peak_db + db < 0) {
321 retval = 0;
324 /* range check: db value too small */
325 else if (max_peak_db + db >= max_peak_db) {
326 retval = MAX_PEAK -1;
329 /* value in range: find the matching linear value */
330 else {
331 retval = db_to_sample_bin_search(0, MAX_PEAK, max_peak_db + db);
333 /* as this is a dirty function anyway, we want to adjust the
334 full scale hit manually to avoid users complaining that when
335 they adjust maximum for 0 dBfs and display it in percent it
336 shows 99%. That is due to precision loss and this is the
337 optical fix */
340 return retval;
344 * Set the min value for restriction of the value range.
345 * @param int newmin - depending whether dBfs is used
346 * newmin is a value in dBfs * 100 or in linear percent values.
347 * for dBfs: -9000 < newmin <= 0
348 * for linear: 0 <= newmin <= 100
350 static void peak_meter_set_min(int newmin)
352 if (pm_use_dbfs) {
353 peak_meter_range_min = peak_meter_db2sample(newmin);
355 } else {
356 if (newmin < peak_meter_range_max) {
357 peak_meter_range_min = newmin * MAX_PEAK / 100;
361 pm_range = peak_meter_range_max - peak_meter_range_min;
363 /* Avoid division by zero. */
364 if (pm_range == 0) {
365 pm_range = 1;
368 pm_db_min = calc_db(peak_meter_range_min);
369 pm_db_range = pm_db_max - pm_db_min;
370 int i;
371 FOR_NB_SCREENS(i)
372 scales[i].db_scale_valid = false;
376 * Returns the minimum value of the range the meter
377 * displays. If the scale is set to dBfs it returns
378 * dBfs values * 100 or linear percent values.
379 * @return: using dBfs : -9000 < value <= 0
380 * using linear scale: 0 <= value <= 100
382 int peak_meter_get_min(void)
384 int retval = 0;
385 if (pm_use_dbfs) {
386 retval = calc_db(peak_meter_range_min) - calc_db(MAX_PEAK - 1);
387 } else {
388 retval = peak_meter_range_min * 100 / MAX_PEAK;
390 return retval;
394 * Set the max value for restriction of the value range.
395 * @param int newmax - depending wether dBfs is used
396 * newmax is a value in dBfs * 100 or in linear percent values.
397 * for dBfs: -9000 < newmax <= 0
398 * for linear: 0 <= newmax <= 100
400 static void peak_meter_set_max(int newmax)
402 if (pm_use_dbfs) {
403 peak_meter_range_max = peak_meter_db2sample(newmax);
404 } else {
405 if (newmax > peak_meter_range_min) {
406 peak_meter_range_max = newmax * MAX_PEAK / 100;
410 pm_range = peak_meter_range_max - peak_meter_range_min;
412 /* Avoid division by zero. */
413 if (pm_range == 0) {
414 pm_range = 1;
417 pm_db_max = calc_db(peak_meter_range_max);
418 pm_db_range = pm_db_max - pm_db_min;
419 int i;
420 FOR_NB_SCREENS(i)
421 scales[i].db_scale_valid = false;
425 * Returns the minimum value of the range the meter
426 * displays. If the scale is set to dBfs it returns
427 * dBfs values * 100 or linear percent values
428 * @return: using dBfs : -9000 < value <= 0
429 * using linear scale: 0 <= value <= 100
431 int peak_meter_get_max(void)
433 int retval = 0;
434 if (pm_use_dbfs) {
435 retval = calc_db(peak_meter_range_max) - calc_db(MAX_PEAK - 1);
436 } else {
437 retval = peak_meter_range_max * 100 / MAX_PEAK;
439 return retval;
443 * Returns whether the meter is currently displaying dBfs or percent values.
444 * @return bool - true if the meter is displaying dBfs
445 false if the meter is displaying percent values.
447 bool peak_meter_get_use_dbfs(void)
449 return pm_use_dbfs;
453 * Specifies whether the values displayed are scaled
454 * as dBfs or as linear percent values.
455 * @param use - set to true for dBfs,
456 * set to false for linear scaling in percent
458 void peak_meter_set_use_dbfs(bool use)
460 int i;
461 pm_use_dbfs = use;
462 FOR_NB_SCREENS(i)
463 scales[i].db_scale_valid = false;
467 * Initialize the range of the meter. Only values
468 * that are in the range of [range_min ... range_max]
469 * are displayed.
470 * @param bool dbfs - set to true for dBfs,
471 * set to false for linear scaling in percent
472 * @param int range_min - Specifies the lower value of the range.
473 * Pass a value dBfs * 100 when dbfs is set to true.
474 * Pass a percent value when dbfs is set to false.
475 * @param int range_max - Specifies the upper value of the range.
476 * Pass a value dBfs * 100 when dbfs is set to true.
477 * Pass a percent value when dbfs is set to false.
479 void peak_meter_init_range( bool dbfs, int range_min, int range_max)
481 pm_use_dbfs = dbfs;
482 peak_meter_set_min(range_min);
483 peak_meter_set_max(range_max);
487 * Initialize the peak meter with all relevant values concerning times.
488 * @param int release - Set the maximum amount of pixels the meter is allowed
489 * to decrease with each redraw
490 * @param int hold - Select the time preset for the time the peak indicator
491 * is reset after a peak occurred. The preset values are
492 * stored in peak_time_out.
493 * @param int clip_hold - Select the time preset for the time the peak
494 * indicator is reset after a peak occurred. The preset
495 * values are stored in clip_time_out.
497 void peak_meter_init_times(int release, int hold, int clip_hold)
499 pm_peak_hold = hold;
500 pm_peak_release = release;
501 pm_clip_hold = clip_hold;
504 #ifdef HAVE_RECORDING
506 * Enable/disable clip counting
508 void pm_activate_clipcount(bool active)
510 pm_clipcount_active = active;
514 * Get clipping counter value
516 int pm_get_clipcount(void)
518 return pm_clipcount;
522 * Set clipping counter to zero (typically at start of recording or playback)
524 void pm_reset_clipcount(void)
526 pm_clipcount = 0;
528 #endif
531 * Set the source of the peak meter to playback or to
532 * record.
533 * @param: bool playback - If true playback peak meter is used.
534 * If false recording peak meter is used.
536 void peak_meter_playback(bool playback)
538 int i;
539 #ifdef SIMULATOR
540 (void)playback;
541 #elif CONFIG_CODEC == SWCODEC
542 pm_playback = playback;
543 #else
544 if (playback) {
545 pm_src_left = MAS_REG_DQPEAK_L;
546 pm_src_right = MAS_REG_DQPEAK_R;
547 } else {
548 pm_src_left = MAS_REG_QPEAK_L;
549 pm_src_right = MAS_REG_QPEAK_R;
551 #endif
552 /* reset the scales just in case recording and playback
553 use different viewport sizes. Normally we should be checking viewport
554 sizes every time but this will do for now */
555 FOR_NB_SCREENS(i)
556 scales[i].db_scale_valid = false;
559 #ifdef HAVE_RECORDING
560 static void set_trig_status(int new_state)
562 if (trig_status != new_state) {
563 trig_status = new_state;
564 if (trigger_listener != NULL) {
565 trigger_listener(trig_status);
570 #endif
573 * Reads peak values from the MAS, and detects clips. The
574 * values are stored in pm_max_left pm_max_right for later
575 * evauluation. Consecutive calls to peak_meter_peek detect
576 * that ocurred. This function could be used by a thread for
577 * busy reading the MAS.
579 void peak_meter_peek(void)
581 int left, right;
582 #ifdef HAVE_RECORDING
583 bool was_clipping = pm_clip_left || pm_clip_right;
584 #endif
585 /* read current values */
586 #if CONFIG_CODEC == SWCODEC
587 if (pm_playback)
588 pcm_calculate_peaks(&pm_cur_left, &pm_cur_right);
589 #ifdef HAVE_RECORDING
590 else
591 pcm_calculate_rec_peaks(&pm_cur_left, &pm_cur_right);
592 #endif
593 left = pm_cur_left;
594 right = pm_cur_right;
595 #else
596 #ifndef SIMULATOR
597 pm_cur_left = left = mas_codec_readreg(pm_src_left);
598 pm_cur_right = right = mas_codec_readreg(pm_src_right);
599 #else
600 pm_cur_left = left = 8000;
601 pm_cur_right = right = 9000;
602 #endif
603 #endif
605 /* check for clips
606 An clip is assumed when two consecutive readouts
607 of the volume are at full scale. This is proven
608 to be inaccurate in both ways: it may detect clips
609 when no clip occurred and it may fail to detect
610 a real clip. For software codecs, the peak is already
611 the max of a bunch of samples, so use one max value
612 or you fail to detect clipping! */
613 #if CONFIG_CODEC == SWCODEC
614 if (left == MAX_PEAK - 1) {
615 #else
616 if ((left == pm_max_left) &&
617 (left == MAX_PEAK - 1)) {
618 #endif
619 pm_clip_left = true;
620 pm_clip_timeout_l =
621 current_tick + clip_time_out[pm_clip_hold];
624 #if CONFIG_CODEC == SWCODEC
625 if (right == MAX_PEAK - 1) {
626 #else
627 if ((right == pm_max_right) &&
628 (right == MAX_PEAK - 1)) {
629 #endif
630 pm_clip_right = true;
631 pm_clip_timeout_r =
632 current_tick + clip_time_out[pm_clip_hold];
635 #ifdef HAVE_RECORDING
636 if(!was_clipping && (pm_clip_left || pm_clip_right))
638 if(pm_clipcount_active)
639 pm_clipcount++;
641 #endif
643 /* peaks are searched -> we have to find the maximum. When
644 many calls of peak_meter_peek the maximum value will be
645 stored in pm_max_xxx. This maximum is reset by the
646 functions peak_meter_read_x. */
647 pm_max_left = MAX(pm_max_left, left);
648 pm_max_right = MAX(pm_max_right, right);
650 #ifdef HAVE_RECORDING
651 #if CONFIG_CODEC == SWCODEC
652 /* Ignore any unread peakmeter data */
653 #define MAX_DROP_TIME HZ/7 /* this value may need tweaking. Increase if you are
654 getting trig events when you shouldn't with
655 trig_stp_hold = 0 */
656 if (!trig_stp_hold)
657 trig_stp_hold = MAX_DROP_TIME;
658 #endif
660 switch (trig_status) {
661 case TRIG_READY:
662 /* no more changes, if trigger was activated as release trigger */
663 /* threshold exceeded? */
664 if ((left > trig_strt_threshold)
665 || (right > trig_strt_threshold)) {
666 /* reset trigger duration */
667 trig_hightime = current_tick;
669 /* reset dropout duration */
670 trig_lowtime = current_tick;
672 if (trig_strt_duration)
673 set_trig_status(TRIG_STEADY);
674 else
675 /* if trig_duration is set to 0 the user wants to start
676 recording immediately */
677 set_trig_status(TRIG_GO);
679 break;
681 case TRIG_STEADY:
682 case TRIG_RETRIG:
683 /* trigger duration exceeded */
684 if (current_tick - trig_hightime > trig_strt_duration) {
685 set_trig_status(TRIG_GO);
686 } else {
687 /* threshold exceeded? */
688 if ((left > trig_strt_threshold)
689 || (right > trig_strt_threshold)) {
690 /* reset lowtime */
691 trig_lowtime = current_tick;
693 /* volume is below threshold */
694 else {
695 /* dropout occurred? */
696 if (current_tick - trig_lowtime > trig_strt_dropout){
697 if (trig_status == TRIG_STEADY){
698 set_trig_status(TRIG_READY);
700 /* trig_status == TRIG_RETRIG */
701 else {
702 /* the gap has already expired */
703 trig_lowtime = current_tick - trig_rstrt_gap - 1;
704 set_trig_status(TRIG_POSTREC);
709 break;
711 case TRIG_GO:
712 case TRIG_CONTINUE:
713 /* threshold exceeded? */
714 if ((left > trig_stp_threshold)
715 || (right > trig_stp_threshold)) {
716 /* restart hold time countdown */
717 trig_lowtime = current_tick;
718 #if CONFIG_CODEC == SWCODEC
719 } else if (current_tick - trig_lowtime > MAX_DROP_TIME){
720 #else
721 } else {
722 #endif
723 set_trig_status(TRIG_POSTREC);
724 trig_hightime = current_tick;
726 break;
728 case TRIG_POSTREC:
729 /* gap time expired? */
730 if (current_tick - trig_lowtime > trig_rstrt_gap){
731 /* start threshold exceeded? */
732 if ((left > trig_strt_threshold)
733 || (right > trig_strt_threshold)) {
735 set_trig_status(TRIG_RETRIG);
736 trig_hightime = current_tick;
737 trig_lowtime = current_tick;
739 else
741 /* stop threshold exceeded */
742 if ((left > trig_stp_threshold)
743 || (right > trig_stp_threshold)) {
744 if (current_tick - trig_hightime > trig_stp_hold){
745 trig_lowtime = current_tick;
746 set_trig_status(TRIG_CONTINUE);
747 } else {
748 trig_lowtime = current_tick - trig_rstrt_gap - 1;
752 /* below any threshold */
753 else {
754 if (current_tick - trig_lowtime > trig_stp_hold){
755 set_trig_status(TRIG_READY);
756 } else {
757 trig_hightime = current_tick;
762 /* still within the gap time */
763 else {
764 /* stop threshold exceeded */
765 if ((left > trig_stp_threshold)
766 || (right > trig_stp_threshold)) {
767 set_trig_status(TRIG_CONTINUE);
768 trig_lowtime = current_tick;
771 /* hold time expired */
772 else if (current_tick - trig_lowtime > trig_stp_hold){
773 trig_hightime = current_tick;
774 trig_lowtime = current_tick;
775 set_trig_status(TRIG_READY);
778 break;
780 #if CONFIG_CODEC == SWCODEC
781 /* restore stop hold value */
782 if (trig_stp_hold == MAX_DROP_TIME)
783 trig_stp_hold = 0;
784 #endif
785 #endif
786 /* check levels next time peakmeter drawn */
787 level_check = true;
788 #ifdef PM_DEBUG
789 peek_calls++;
790 #endif
794 * Reads out the peak volume of the left channel.
795 * @return int - The maximum value that has been detected
796 * since the last call of peak_meter_read_l. The value
797 * is in the range 0 <= value < MAX_PEAK.
799 static int peak_meter_read_l(void)
801 /* pm_max_left contains the maximum of all peak values that were read
802 by peak_meter_peek since the last call of peak_meter_read_l */
803 int retval = pm_max_left;
805 #ifdef HAVE_AGC
806 /* store max peak value for peak_meter_get_peakhold_x readout */
807 pm_peakhold_left = MAX(pm_max_left, pm_peakhold_left);
808 #endif
809 #ifdef PM_DEBUG
810 peek_calls = 0;
811 #endif
812 /* reset pm_max_left so that subsequent calls of peak_meter_peek don't
813 get fooled by an old maximum value */
814 pm_max_left = pm_cur_left;
816 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
817 srand(current_tick);
818 retval = rand()%MAX_PEAK;
819 #endif
821 return retval;
825 * Reads out the peak volume of the right channel.
826 * @return int - The maximum value that has been detected
827 * since the last call of peak_meter_read_l. The value
828 * is in the range 0 <= value < MAX_PEAK.
830 static int peak_meter_read_r(void)
832 /* peak_meter_r contains the maximum of all peak values that were read
833 by peak_meter_peek since the last call of peak_meter_read_r */
834 int retval = pm_max_right;
836 #ifdef HAVE_AGC
837 /* store max peak value for peak_meter_get_peakhold_x readout */
838 pm_peakhold_right = MAX(pm_max_right, pm_peakhold_right);
839 #endif
840 #ifdef PM_DEBUG
841 peek_calls = 0;
842 #endif
843 /* reset pm_max_right so that subsequent calls of peak_meter_peek don't
844 get fooled by an old maximum value */
845 pm_max_right = pm_cur_right;
847 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
848 srand(current_tick);
849 retval = rand()%MAX_PEAK;
850 #endif
852 return retval;
855 #ifdef HAVE_AGC
857 * Reads out the current peak-hold values since the last call.
858 * This is used by the histogram feature in the recording screen.
859 * Values are in the range 0 <= peak_x < MAX_PEAK. MAX_PEAK is typ 32767.
861 void peak_meter_get_peakhold(int *peak_left, int *peak_right)
863 if (peak_left)
864 *peak_left = pm_peakhold_left;
865 if (peak_right)
866 *peak_right = pm_peakhold_right;
867 pm_peakhold_left = 0;
868 pm_peakhold_right = 0;
870 #endif
873 * Reset the detected clips. This method is for
874 * use by the user interface.
875 * @param int unused - This parameter was added to
876 * make the function compatible with set_int
878 void peak_meter_set_clip_hold(int time)
880 pm_clip_left = false;
881 pm_clip_right = false;
882 pm_clip_eternal = (time > 0) ? false : true;
886 * Scales a peak value as read from the MAS to the range of meterwidth.
887 * The scaling is performed according to the scaling method (dBfs / linear)
888 * and the range (peak_meter_range_min .. peak_meter_range_max).
889 * @param unsigned short val - The volume value. Range: 0 <= val < MAX_PEAK
890 * @param int meterwidht - The widht of the meter in pixel
891 * @return unsigned short - A value 0 <= return value <= meterwidth
893 unsigned short peak_meter_scale_value(unsigned short val, int meterwidth)
895 int retval;
897 if (val <= peak_meter_range_min) {
898 return 0;
901 if (val >= peak_meter_range_max) {
902 return meterwidth;
905 retval = val;
907 /* different scaling is used for dBfs and linear percent */
908 if (pm_use_dbfs) {
910 /* scale the samples dBfs */
911 retval = (calc_db(retval) - pm_db_min) * meterwidth / pm_db_range;
914 /* Scale for linear percent display */
915 else
917 /* scale the samples */
918 retval = ((retval - peak_meter_range_min) * meterwidth)
919 / pm_range;
921 return retval;
923 void peak_meter_screen(struct screen *display, int x, int y, int height)
925 peak_meter_draw(display, &scales[display->screen_type], x, y,
926 display->getwidth() - x, height);
929 * Draws a peak meter in the specified size at the specified position.
930 * @param int x - The x coordinate.
931 * Make sure that 0 <= x and x + width < display->getwidth()
932 * @param int y - The y coordinate.
933 * Make sure that 0 <= y and y + height < display->getheight()
934 * @param int width - The width of the peak meter. Note that for display
935 * of clips a 3 pixel wide area is used ->
936 * width > 3
937 * @param int height - The height of the peak meter. height > 3
939 static void peak_meter_draw(struct screen *display, struct meter_scales *scales,
940 int x, int y, int width, int height)
942 static int left_level = 0, right_level = 0;
943 int left = 0, right = 0;
944 int meterwidth = width - 3;
945 int i, delta;
946 #if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
947 static long peak_release_tick[2] = {0,0};
948 int screen_nr = display->screen_type == SCREEN_MAIN ? 0 : 1;
949 #else
950 static long peak_release_tick = 0;
951 #endif
953 #ifdef PM_DEBUG
954 static long pm_tick = 0;
955 int tmp = peek_calls;
956 #endif
958 /* if disabled only draw the peak meter */
959 if (peak_meter_enabled) {
962 if (level_check){
963 /* only read the volume info from MAS if peek since last read*/
964 left_level = peak_meter_read_l();
965 right_level = peak_meter_read_r();
966 level_check = false;
969 /* scale the samples dBfs */
970 left = peak_meter_scale_value(left_level, meterwidth);
971 right = peak_meter_scale_value(right_level, meterwidth);
973 /*if the scale has changed -> recalculate the scale
974 (The scale becomes invalid when the range changed.) */
975 if (!scales->db_scale_valid){
977 if (pm_use_dbfs) {
978 db_scale_count = DB_SCALE_SRC_VALUES_SIZE;
979 for (i = 0; i < db_scale_count; i++){
980 /* find the real x-coords for predefined interesting
981 dBfs values. These only are recalculated when the
982 scaling of the meter changed. */
983 scales->db_scale_lcd_coord[i] =
984 peak_meter_scale_value(
985 db_scale_src_values[i],
986 meterwidth - 1);
990 /* when scaling linear we simly make 10% steps */
991 else {
992 db_scale_count = 10;
993 for (i = 0; i < db_scale_count; i++) {
994 scales->db_scale_lcd_coord[i] =
995 (i * (MAX_PEAK / 10) - peak_meter_range_min) *
996 meterwidth / pm_range;
1000 /* mark scale valid to avoid recalculating dBfs values
1001 of the scale. */
1002 scales->db_scale_valid = true;
1005 /* apply release */
1006 #if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
1007 delta = current_tick - peak_release_tick[screen_nr];
1008 peak_release_tick[screen_nr] = current_tick;
1009 #else
1010 delta = current_tick - peak_release_tick;
1011 peak_release_tick = current_tick;
1012 #endif
1013 left = MAX(left , scales->last_left - delta * pm_peak_release);
1014 right = MAX(right, scales->last_right - delta * pm_peak_release);
1016 /* reset max values after timeout */
1017 if (TIME_AFTER(current_tick, scales->pm_peak_timeout_l)){
1018 scales->pm_peak_left = 0;
1021 if (TIME_AFTER(current_tick, scales->pm_peak_timeout_r)){
1022 scales->pm_peak_right = 0;
1025 if (!pm_clip_eternal) {
1026 if (pm_clip_left &&
1027 TIME_AFTER(current_tick, pm_clip_timeout_l)){
1028 pm_clip_left = false;
1031 if (pm_clip_right &&
1032 TIME_AFTER(current_tick, pm_clip_timeout_r)){
1033 pm_clip_right = false;
1037 /* check for new max values */
1038 if (left > scales->pm_peak_left) {
1039 scales->pm_peak_left = left - 1;
1040 scales->pm_peak_timeout_l =
1041 current_tick + peak_time_out[pm_peak_hold];
1044 if (right > scales->pm_peak_right) {
1045 scales->pm_peak_right = right - 1;
1046 scales->pm_peak_timeout_r =
1047 current_tick + peak_time_out[pm_peak_hold];
1051 /* draw the peak meter */
1052 display->set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1053 display->fillrect(x, y, width, height);
1054 display->set_drawmode(DRMODE_SOLID);
1056 /* draw left */
1057 display->fillrect (x, y, left, height / 2 - 2 );
1058 if (scales->pm_peak_left > 0) {
1059 display->vline(x + scales->pm_peak_left, y, y + height / 2 - 2 );
1061 if (pm_clip_left) {
1062 display->fillrect(x + meterwidth, y, 3, height / 2 - 1);
1065 /* draw right */
1066 display->fillrect(x, y + height / 2 + 1, right, height / 2 - 2);
1067 if (scales->pm_peak_right > 0) {
1068 display->vline( x + scales->pm_peak_right, y + height / 2, y + height - 2);
1070 if (pm_clip_right) {
1071 display->fillrect(x + meterwidth, y + height / 2, 3, height / 2 - 1);
1074 /* draw scale end */
1075 display->vline(x + meterwidth, y, y + height - 2);
1077 /* draw dots for scale marks */
1078 for (i = 0; i < db_scale_count; i++) {
1079 /* The x-coordinates of interesting scale mark points
1080 have been calculated before */
1081 display->drawpixel(x + scales->db_scale_lcd_coord[i],
1082 y + height / 2 - 1);
1085 #ifdef HAVE_RECORDING
1087 #ifdef HAVE_BACKLIGHT
1088 /* cliplight */
1089 if ((pm_clip_left || pm_clip_right) &&
1090 global_settings.cliplight &&
1091 #if CONFIG_CODEC == SWCODEC
1092 !pm_playback)
1093 #else
1094 !(audio_status() & (AUDIO_STATUS_PLAY | AUDIO_STATUS_ERROR)))
1095 #endif
1097 /* if clipping, cliplight setting on and in recording screen */
1098 if (global_settings.cliplight <= 2)
1100 /* turn on main unit light if setting set to main or both*/
1101 backlight_on();
1103 #ifdef HAVE_REMOTE_LCD
1104 if (global_settings.cliplight >= 2)
1106 /* turn remote light unit on if setting set to remote or both */
1107 remote_backlight_on();
1109 #endif /* HAVE_REMOTE_LCD */
1111 #endif /* HAVE_BACKLIGHT */
1113 if (trig_status != TRIG_OFF) {
1114 int start_trigx, stop_trigx, ycenter;
1116 display->set_drawmode(DRMODE_SOLID);
1117 ycenter = y + height / 2;
1118 /* display threshold value */
1119 start_trigx = x+peak_meter_scale_value(trig_strt_threshold,meterwidth);
1120 display->vline(start_trigx, ycenter - 2, ycenter);
1121 start_trigx ++;
1122 if (start_trigx < display->getwidth() ) display->drawpixel(start_trigx,
1123 ycenter - 1);
1125 stop_trigx = x + peak_meter_scale_value(trig_stp_threshold,meterwidth);
1126 display->vline(stop_trigx, ycenter - 2, ycenter);
1127 if (stop_trigx > 0) display->drawpixel(stop_trigx - 1, ycenter - 1);
1129 #endif /*HAVE_RECORDING*/
1131 #ifdef PM_DEBUG
1132 /* display a bar to show how many calls to peak_meter_peek
1133 have ocurred since the last display */
1134 display->set_drawmode(DRMODE_COMPLEMENT);
1135 display->fillrect(x, y, tmp, 3);
1137 if (tmp < PEEKS_PER_DRAW_SIZE) {
1138 peeks_per_redraw[tmp]++;
1141 tmp = current_tick - pm_tick;
1142 if (tmp < TICKS_PER_DRAW_SIZE ){
1143 ticks_per_redraw[tmp] ++;
1146 /* display a bar to show how many ticks have passed since
1147 the last redraw */
1148 display->fillrect(x, y + height / 2, current_tick - pm_tick, 2);
1149 pm_tick = current_tick;
1150 #endif
1152 scales->last_left = left;
1153 scales->last_right = right;
1155 display->set_drawmode(DRMODE_SOLID);
1158 #ifdef HAVE_RECORDING
1160 * Defines the parameters of the trigger. After these parameters are defined
1161 * the trigger can be started either by peak_meter_attack_trigger or by
1162 * peak_meter_release_trigger. Note that you can pass either linear (%) or
1163 * logarithmic (db) values to the thresholds. Positive values are intepreted as
1164 * percent (0 is 0% .. 100 is 100%). Negative values are interpreted as db.
1165 * To avoid ambiguosity of the value 0 the negative values are shifted by -1.
1166 * Thus -75 is -74db .. -1 is 0db.
1167 * @param start_threshold - The threshold used for attack trigger. Negative
1168 * values are interpreted as db -1, positive as %.
1169 * @param start_duration - The minimum time span within which start_threshold
1170 * must be exceeded to fire the attack trigger.
1171 * @param start_dropout - The maximum time span the level may fall below
1172 * start_threshold without releasing the attack trigger.
1173 * @param stop_threshold - The threshold the volume must fall below to release
1174 * the release trigger.Negative values are
1175 * interpreted as db -1, positive as %.
1176 * @param stop_hold - The minimum time the volume must fall below the
1177 * stop_threshold to release the trigger.
1178 * @param
1180 void peak_meter_define_trigger(
1181 int start_threshold,
1182 long start_duration,
1183 long start_dropout,
1184 int stop_threshold,
1185 long stop_hold_time,
1186 long restart_gap
1189 if (start_threshold < 0) {
1190 /* db */
1191 if (start_threshold < -89) {
1192 trig_strt_threshold = 0;
1193 } else {
1194 trig_strt_threshold =peak_meter_db2sample((start_threshold+1)*100);
1196 } else {
1197 /* linear percent */
1198 trig_strt_threshold = start_threshold * MAX_PEAK / 100;
1200 trig_strt_duration = start_duration;
1201 trig_strt_dropout = start_dropout;
1202 if (stop_threshold < 0) {
1203 /* db */
1204 trig_stp_threshold = peak_meter_db2sample((stop_threshold + 1) * 100);
1205 } else {
1206 /* linear percent */
1207 trig_stp_threshold = stop_threshold * MAX_PEAK / 100;
1209 trig_stp_hold = stop_hold_time;
1210 trig_rstrt_gap = restart_gap;
1214 * Enables or disables the trigger.
1215 * @param on - If true the trigger is turned on.
1217 void peak_meter_trigger(bool on)
1219 /* don't use set_trigger here as that would fire an undesired event */
1220 trig_status = on ? TRIG_READY : TRIG_OFF;
1224 * Registers the listener function that listenes on trig_status changes.
1225 * @param listener - The function that is called with each change of
1226 * trig_status. May be set to NULL if no callback is desired.
1228 void peak_meter_set_trigger_listener(void (*listener)(int status))
1230 trigger_listener = listener;
1234 * Fetches the status of the trigger.
1235 * TRIG_OFF: the trigger is inactive
1236 * TRIG_RELEASED: The volume level is below the threshold
1237 * TRIG_ACTIVATED: The volume level has exceeded the threshold, but the trigger
1238 * hasn't been fired yet.
1239 * TRIG_FIRED: The volume exceeds the threshold
1241 * To activate the trigger call either peak_meter_attack_trigger or
1242 * peak_meter_release_trigger. To turn the trigger off call
1243 * peak_meter_trigger_off.
1245 int peak_meter_trigger_status(void)
1247 return trig_status; /* & TRIG_PIT_MASK;*/
1250 void peak_meter_draw_trig(int xpos[], int ypos[],
1251 int trig_width[], int nb_screens)
1253 int barstart[NB_SCREENS];
1254 int barend[NB_SCREENS];
1255 int icon;
1256 int ixpos[NB_SCREENS];
1257 int i;
1258 int trigbar_width[NB_SCREENS];
1260 FOR_NB_SCREENS(i)
1261 trigbar_width[i] = (trig_width[i] - (2 * (ICON_PLAY_STATE_WIDTH + 1)));
1263 switch (trig_status) {
1265 case TRIG_READY:
1266 FOR_NB_SCREENS(i){
1267 barstart[i] = 0;
1268 barend[i] = 0;
1270 icon = Icon_Stop;
1271 FOR_NB_SCREENS(i)
1272 ixpos[i] = xpos[i];
1273 break;
1275 case TRIG_STEADY:
1276 case TRIG_RETRIG:
1277 FOR_NB_SCREENS(i)
1279 barstart[i] = 0;
1280 barend[i] = (trig_strt_duration == 0) ? trigbar_width[i] :
1281 trigbar_width[i] *
1282 (current_tick - trig_hightime) / trig_strt_duration;
1284 icon = Icon_Stop;
1285 FOR_NB_SCREENS(i)
1286 ixpos[i] = xpos[i];
1287 break;
1289 case TRIG_GO:
1290 case TRIG_CONTINUE:
1291 FOR_NB_SCREENS(i)
1293 barstart[i] = trigbar_width[i];
1294 barend[i] = trigbar_width[i];
1296 icon = Icon_Record;
1297 FOR_NB_SCREENS(i)
1298 ixpos[i] = xpos[i]+ trig_width[i] - ICON_PLAY_STATE_WIDTH;
1299 break;
1301 case TRIG_POSTREC:
1302 FOR_NB_SCREENS(i)
1304 barstart[i] = (trig_stp_hold == 0) ? 0 :
1305 trigbar_width[i] - trigbar_width[i] *
1306 (current_tick - trig_lowtime) / trig_stp_hold;
1307 barend[i] = trigbar_width[i];
1309 icon = Icon_Record;
1310 FOR_NB_SCREENS(i)
1311 ixpos[i] = xpos[i] + trig_width[i] - ICON_PLAY_STATE_WIDTH;
1312 break;
1314 default:
1315 return;
1318 for(i = 0; i < nb_screens; i++)
1320 gui_scrollbar_draw(&screens[i], xpos[i] + ICON_PLAY_STATE_WIDTH + 1,
1321 ypos[i] + 1, trigbar_width[i], TRIG_HEIGHT - 2,
1322 trigbar_width[i], barstart[i], barend[i],
1323 HORIZONTAL);
1325 screens[i].mono_bitmap(bitmap_icons_7x8[icon], ixpos[i], ypos[i],
1326 ICON_PLAY_STATE_WIDTH, STATUSBAR_HEIGHT);
1329 #endif
1331 int peak_meter_draw_get_btn(int action_context, int x, int y[],
1332 int height, int nb_screens)
1334 int button = BUTTON_NONE;
1335 long next_refresh = current_tick;
1336 long next_big_refresh = current_tick + HZ / 10;
1337 int i;
1338 #if (CONFIG_CODEC == SWCODEC) || defined(SIMULATOR)
1339 bool highperf = false;
1340 #else
1341 /* On MAS targets, we need to poll as often as possible in order to not
1342 * miss a peak, as the MAS does only provide a quasi-peak. When the disk
1343 * is active, it must not draw too much CPU power or a buffer overrun can
1344 * happen when saving a recording. As a compromise, poll only once per tick
1345 * when the disk is active, otherwise spin around as fast as possible. */
1346 bool highperf = !ata_disk_is_active();
1347 #endif
1348 bool dopeek = true;
1350 while (TIME_BEFORE(current_tick, next_big_refresh)) {
1351 button = get_action(action_context, TIMEOUT_NOBLOCK);
1352 if (button != BUTTON_NONE) {
1353 break;
1355 if (dopeek) { /* Peek only once per refresh when disk is */
1356 peak_meter_peek(); /* spinning, but as often as possible */
1357 dopeek = highperf; /* otherwise. */
1358 yield();
1359 } else {
1360 sleep(0); /* Sleep until end of current tick. */
1362 if (TIME_AFTER(current_tick, next_refresh)) {
1363 for(i = 0; i < nb_screens; i++)
1365 peak_meter_screen(&screens[i], x, y[i], height);
1366 screens[i].update_viewport_rect(x, y[i],
1367 screens[i].getwidth() - x,
1368 height);
1370 next_refresh += HZ / PEAK_METER_FPS;
1371 dopeek = true;
1375 return button;
1378 #ifdef PM_DEBUG
1379 static void peak_meter_clear_histogram(void)
1381 int i = 0;
1382 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1383 ticks_per_redraw[i] = (unsigned int)0;
1386 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1387 peeks_per_redraw[i] = (unsigned int)0;
1391 bool peak_meter_histogram(void)
1393 int i;
1394 int btn = BUTTON_NONE;
1395 while ((btn & BUTTON_OFF) != BUTTON_OFF )
1397 unsigned int max = 0;
1398 int y = 0;
1399 int x = 0;
1400 screens[0].clear_display();
1402 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1403 max = MAX(max, peeks_per_redraw[i]);
1406 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1407 x = peeks_per_redraw[i] * (LCD_WIDTH - 1)/ max;
1408 screens[0].hline(0, x, y + i);
1411 y = PEEKS_PER_DRAW_SIZE + 1;
1412 max = 0;
1414 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1415 max = MAX(max, ticks_per_redraw[i]);
1418 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1419 x = ticks_per_redraw[i] * (LCD_WIDTH - 1)/ max;
1420 screens[0].hline(0, x, y + i);
1422 screens[0].update();
1424 btn = button_get(true);
1425 if (btn == BUTTON_PLAY) {
1426 peak_meter_clear_histogram();
1429 return false;
1431 #endif