be7fa3828415f600df702d65d0c68d0b49b5f4d5
[Rockbox.git] / apps / recorder / peakmeter.c
blobbe7fa3828415f600df702d65d0c68d0b49b5f4d5
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 #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
553 #ifdef HAVE_RECORDING
554 static void set_trig_status(int new_state)
556 if (trig_status != new_state) {
557 trig_status = new_state;
558 if (trigger_listener != NULL) {
559 trigger_listener(trig_status);
564 #endif
567 * Reads peak values from the MAS, and detects clips. The
568 * values are stored in pm_max_left pm_max_right for later
569 * evauluation. Consecutive calls to peak_meter_peek detect
570 * that ocurred. This function could be used by a thread for
571 * busy reading the MAS.
573 void peak_meter_peek(void)
575 int left, right;
576 #ifdef HAVE_RECORDING
577 bool was_clipping = pm_clip_left || pm_clip_right;
578 #endif
579 /* read current values */
580 #if CONFIG_CODEC == SWCODEC
581 if (pm_playback)
582 pcm_calculate_peaks(&pm_cur_left, &pm_cur_right);
583 #ifdef HAVE_RECORDING
584 else
585 pcm_calculate_rec_peaks(&pm_cur_left, &pm_cur_right);
586 #endif
587 left = pm_cur_left;
588 right = pm_cur_right;
589 #else
590 #ifndef SIMULATOR
591 pm_cur_left = left = mas_codec_readreg(pm_src_left);
592 pm_cur_right = right = mas_codec_readreg(pm_src_right);
593 #else
594 pm_cur_left = left = 8000;
595 pm_cur_right = right = 9000;
596 #endif
597 #endif
599 /* check for clips
600 An clip is assumed when two consecutive readouts
601 of the volume are at full scale. This is proven
602 to be inaccurate in both ways: it may detect clips
603 when no clip occurred and it may fail to detect
604 a real clip. For software codecs, the peak is already
605 the max of a bunch of samples, so use one max value
606 or you fail to detect clipping! */
607 #if CONFIG_CODEC == SWCODEC
608 if (left == MAX_PEAK - 1) {
609 #else
610 if ((left == pm_max_left) &&
611 (left == MAX_PEAK - 1)) {
612 #endif
613 pm_clip_left = true;
614 pm_clip_timeout_l =
615 current_tick + clip_time_out[pm_clip_hold];
618 #if CONFIG_CODEC == SWCODEC
619 if (right == MAX_PEAK - 1) {
620 #else
621 if ((right == pm_max_right) &&
622 (right == MAX_PEAK - 1)) {
623 #endif
624 pm_clip_right = true;
625 pm_clip_timeout_r =
626 current_tick + clip_time_out[pm_clip_hold];
629 #ifdef HAVE_RECORDING
630 if(!was_clipping && (pm_clip_left || pm_clip_right))
632 if(pm_clipcount_active)
633 pm_clipcount++;
635 #endif
637 /* peaks are searched -> we have to find the maximum. When
638 many calls of peak_meter_peek the maximum value will be
639 stored in pm_max_xxx. This maximum is reset by the
640 functions peak_meter_read_x. */
641 pm_max_left = MAX(pm_max_left, left);
642 pm_max_right = MAX(pm_max_right, right);
644 #ifdef HAVE_RECORDING
645 #if CONFIG_CODEC == SWCODEC
646 /* Ignore any unread peakmeter data */
647 #define MAX_DROP_TIME HZ/7 /* this value may need tweaking. Increase if you are
648 getting trig events when you shouldn't with
649 trig_stp_hold = 0 */
650 if (!trig_stp_hold)
651 trig_stp_hold = MAX_DROP_TIME;
652 #endif
654 switch (trig_status) {
655 case TRIG_READY:
656 /* no more changes, if trigger was activated as release trigger */
657 /* threshold exceeded? */
658 if ((left > trig_strt_threshold)
659 || (right > trig_strt_threshold)) {
660 /* reset trigger duration */
661 trig_hightime = current_tick;
663 /* reset dropout duration */
664 trig_lowtime = current_tick;
666 if (trig_strt_duration)
667 set_trig_status(TRIG_STEADY);
668 else
669 /* if trig_duration is set to 0 the user wants to start
670 recording immediately */
671 set_trig_status(TRIG_GO);
673 break;
675 case TRIG_STEADY:
676 case TRIG_RETRIG:
677 /* trigger duration exceeded */
678 if (current_tick - trig_hightime > trig_strt_duration) {
679 set_trig_status(TRIG_GO);
680 } else {
681 /* threshold exceeded? */
682 if ((left > trig_strt_threshold)
683 || (right > trig_strt_threshold)) {
684 /* reset lowtime */
685 trig_lowtime = current_tick;
687 /* volume is below threshold */
688 else {
689 /* dropout occurred? */
690 if (current_tick - trig_lowtime > trig_strt_dropout){
691 if (trig_status == TRIG_STEADY){
692 set_trig_status(TRIG_READY);
694 /* trig_status == TRIG_RETRIG */
695 else {
696 /* the gap has already expired */
697 trig_lowtime = current_tick - trig_rstrt_gap - 1;
698 set_trig_status(TRIG_POSTREC);
703 break;
705 case TRIG_GO:
706 case TRIG_CONTINUE:
707 /* threshold exceeded? */
708 if ((left > trig_stp_threshold)
709 || (right > trig_stp_threshold)) {
710 /* restart hold time countdown */
711 trig_lowtime = current_tick;
712 #if CONFIG_CODEC == SWCODEC
713 } else if (current_tick - trig_lowtime > MAX_DROP_TIME){
714 #else
715 } else {
716 #endif
717 set_trig_status(TRIG_POSTREC);
718 trig_hightime = current_tick;
720 break;
722 case TRIG_POSTREC:
723 /* gap time expired? */
724 if (current_tick - trig_lowtime > trig_rstrt_gap){
725 /* start threshold exceeded? */
726 if ((left > trig_strt_threshold)
727 || (right > trig_strt_threshold)) {
729 set_trig_status(TRIG_RETRIG);
730 trig_hightime = current_tick;
731 trig_lowtime = current_tick;
733 else
735 /* stop threshold exceeded */
736 if ((left > trig_stp_threshold)
737 || (right > trig_stp_threshold)) {
738 if (current_tick - trig_hightime > trig_stp_hold){
739 trig_lowtime = current_tick;
740 set_trig_status(TRIG_CONTINUE);
741 } else {
742 trig_lowtime = current_tick - trig_rstrt_gap - 1;
746 /* below any threshold */
747 else {
748 if (current_tick - trig_lowtime > trig_stp_hold){
749 set_trig_status(TRIG_READY);
750 } else {
751 trig_hightime = current_tick;
756 /* still within the gap time */
757 else {
758 /* stop threshold exceeded */
759 if ((left > trig_stp_threshold)
760 || (right > trig_stp_threshold)) {
761 set_trig_status(TRIG_CONTINUE);
762 trig_lowtime = current_tick;
765 /* hold time expired */
766 else if (current_tick - trig_lowtime > trig_stp_hold){
767 trig_hightime = current_tick;
768 trig_lowtime = current_tick;
769 set_trig_status(TRIG_READY);
772 break;
774 #if CONFIG_CODEC == SWCODEC
775 /* restore stop hold value */
776 if (trig_stp_hold == MAX_DROP_TIME)
777 trig_stp_hold = 0;
778 #endif
779 #endif
780 /* check levels next time peakmeter drawn */
781 level_check = true;
782 #ifdef PM_DEBUG
783 peek_calls++;
784 #endif
788 * Reads out the peak volume of the left channel.
789 * @return int - The maximum value that has been detected
790 * since the last call of peak_meter_read_l. The value
791 * is in the range 0 <= value < MAX_PEAK.
793 static int peak_meter_read_l(void)
795 /* pm_max_left contains the maximum of all peak values that were read
796 by peak_meter_peek since the last call of peak_meter_read_l */
797 int retval = pm_max_left;
799 #ifdef HAVE_AGC
800 /* store max peak value for peak_meter_get_peakhold_x readout */
801 pm_peakhold_left = MAX(pm_max_left, pm_peakhold_left);
802 #endif
803 #ifdef PM_DEBUG
804 peek_calls = 0;
805 #endif
806 /* reset pm_max_left so that subsequent calls of peak_meter_peek don't
807 get fooled by an old maximum value */
808 pm_max_left = pm_cur_left;
810 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
811 srand(current_tick);
812 retval = rand()%MAX_PEAK;
813 #endif
815 return retval;
819 * Reads out the peak volume of the right channel.
820 * @return int - The maximum value that has been detected
821 * since the last call of peak_meter_read_l. The value
822 * is in the range 0 <= value < MAX_PEAK.
824 static int peak_meter_read_r(void)
826 /* peak_meter_r contains the maximum of all peak values that were read
827 by peak_meter_peek since the last call of peak_meter_read_r */
828 int retval = pm_max_right;
830 #ifdef HAVE_AGC
831 /* store max peak value for peak_meter_get_peakhold_x readout */
832 pm_peakhold_right = MAX(pm_max_right, pm_peakhold_right);
833 #endif
834 #ifdef PM_DEBUG
835 peek_calls = 0;
836 #endif
837 /* reset pm_max_right so that subsequent calls of peak_meter_peek don't
838 get fooled by an old maximum value */
839 pm_max_right = pm_cur_right;
841 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
842 srand(current_tick);
843 retval = rand()%MAX_PEAK;
844 #endif
846 return retval;
849 #ifdef HAVE_AGC
851 * Reads out the current peak-hold values since the last call.
852 * This is used by the histogram feature in the recording screen.
853 * Values are in the range 0 <= peak_x < MAX_PEAK. MAX_PEAK is typ 32767.
855 void peak_meter_get_peakhold(int *peak_left, int *peak_right)
857 if (peak_left)
858 *peak_left = pm_peakhold_left;
859 if (peak_right)
860 *peak_right = pm_peakhold_right;
861 pm_peakhold_left = 0;
862 pm_peakhold_right = 0;
864 #endif
867 * Reset the detected clips. This method is for
868 * use by the user interface.
869 * @param int unused - This parameter was added to
870 * make the function compatible with set_int
872 void peak_meter_set_clip_hold(int time)
874 pm_clip_left = false;
875 pm_clip_right = false;
876 pm_clip_eternal = (time > 0) ? false : true;
880 * Scales a peak value as read from the MAS to the range of meterwidth.
881 * The scaling is performed according to the scaling method (dBfs / linear)
882 * and the range (peak_meter_range_min .. peak_meter_range_max).
883 * @param unsigned short val - The volume value. Range: 0 <= val < MAX_PEAK
884 * @param int meterwidht - The widht of the meter in pixel
885 * @return unsigned short - A value 0 <= return value <= meterwidth
887 unsigned short peak_meter_scale_value(unsigned short val, int meterwidth)
889 int retval;
891 if (val <= peak_meter_range_min) {
892 return 0;
895 if (val >= peak_meter_range_max) {
896 return meterwidth;
899 retval = val;
901 /* different scaling is used for dBfs and linear percent */
902 if (pm_use_dbfs) {
904 /* scale the samples dBfs */
905 retval = (calc_db(retval) - pm_db_min) * meterwidth / pm_db_range;
908 /* Scale for linear percent display */
909 else
911 /* scale the samples */
912 retval = ((retval - peak_meter_range_min) * meterwidth)
913 / pm_range;
915 return retval;
917 void peak_meter_screen(struct screen *display, int x, int y, int height)
919 peak_meter_draw(display, &scales[display->screen_type], x, y,
920 display->getwidth() - x, height);
923 * Draws a peak meter in the specified size at the specified position.
924 * @param int x - The x coordinate.
925 * Make sure that 0 <= x and x + width < display->getwidth()
926 * @param int y - The y coordinate.
927 * Make sure that 0 <= y and y + height < display->getheight()
928 * @param int width - The width of the peak meter. Note that for display
929 * of clips a 3 pixel wide area is used ->
930 * width > 3
931 * @param int height - The height of the peak meter. height > 3
933 static void peak_meter_draw(struct screen *display, struct meter_scales *scales,
934 int x, int y, int width, int height)
936 static int left_level = 0, right_level = 0;
937 int left = 0, right = 0;
938 int meterwidth = width - 3;
939 int i, delta;
940 #if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
941 static long peak_release_tick[2] = {0,0};
942 int screen_nr = display->screen_type == SCREEN_MAIN ? 0 : 1;
943 #else
944 static long peak_release_tick = 0;
945 #endif
947 #ifdef PM_DEBUG
948 static long pm_tick = 0;
949 int tmp = peek_calls;
950 #endif
952 /* if disabled only draw the peak meter */
953 if (peak_meter_enabled) {
956 if (level_check){
957 /* only read the volume info from MAS if peek since last read*/
958 left_level = peak_meter_read_l();
959 right_level = peak_meter_read_r();
960 level_check = false;
963 /* scale the samples dBfs */
964 left = peak_meter_scale_value(left_level, meterwidth);
965 right = peak_meter_scale_value(right_level, meterwidth);
967 /*if the scale has changed -> recalculate the scale
968 (The scale becomes invalid when the range changed.) */
969 if (!scales->db_scale_valid){
971 if (pm_use_dbfs) {
972 db_scale_count = DB_SCALE_SRC_VALUES_SIZE;
973 for (i = 0; i < db_scale_count; i++){
974 /* find the real x-coords for predefined interesting
975 dBfs values. These only are recalculated when the
976 scaling of the meter changed. */
977 scales->db_scale_lcd_coord[i] =
978 peak_meter_scale_value(
979 db_scale_src_values[i],
980 meterwidth - 1);
984 /* when scaling linear we simly make 10% steps */
985 else {
986 db_scale_count = 10;
987 for (i = 0; i < db_scale_count; i++) {
988 scales->db_scale_lcd_coord[i] =
989 (i * (MAX_PEAK / 10) - peak_meter_range_min) *
990 meterwidth / pm_range;
994 /* mark scale valid to avoid recalculating dBfs values
995 of the scale. */
996 scales->db_scale_valid = true;
999 /* apply release */
1000 #if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
1001 delta = current_tick - peak_release_tick[screen_nr];
1002 peak_release_tick[screen_nr] = current_tick;
1003 #else
1004 delta = current_tick - peak_release_tick;
1005 peak_release_tick = current_tick;
1006 #endif
1007 left = MAX(left , scales->last_left - delta * pm_peak_release);
1008 right = MAX(right, scales->last_right - delta * pm_peak_release);
1010 /* reset max values after timeout */
1011 if (TIME_AFTER(current_tick, scales->pm_peak_timeout_l)){
1012 scales->pm_peak_left = 0;
1015 if (TIME_AFTER(current_tick, scales->pm_peak_timeout_r)){
1016 scales->pm_peak_right = 0;
1019 if (!pm_clip_eternal) {
1020 if (pm_clip_left &&
1021 TIME_AFTER(current_tick, pm_clip_timeout_l)){
1022 pm_clip_left = false;
1025 if (pm_clip_right &&
1026 TIME_AFTER(current_tick, pm_clip_timeout_r)){
1027 pm_clip_right = false;
1031 /* check for new max values */
1032 if (left > scales->pm_peak_left) {
1033 scales->pm_peak_left = left - 1;
1034 scales->pm_peak_timeout_l =
1035 current_tick + peak_time_out[pm_peak_hold];
1038 if (right > scales->pm_peak_right) {
1039 scales->pm_peak_right = right - 1;
1040 scales->pm_peak_timeout_r =
1041 current_tick + peak_time_out[pm_peak_hold];
1045 /* draw the peak meter */
1046 display->set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1047 display->fillrect(x, y, width, height);
1048 display->set_drawmode(DRMODE_SOLID);
1050 /* draw left */
1051 display->fillrect (x, y, left, height / 2 - 2 );
1052 if (scales->pm_peak_left > 0) {
1053 display->vline(x + scales->pm_peak_left, y, y + height / 2 - 2 );
1055 if (pm_clip_left) {
1056 display->fillrect(x + meterwidth, y, 3, height / 2 - 1);
1059 /* draw right */
1060 display->fillrect(x, y + height / 2 + 1, right, height / 2 - 2);
1061 if (scales->pm_peak_right > 0) {
1062 display->vline( x + scales->pm_peak_right, y + height / 2, y + height - 2);
1064 if (pm_clip_right) {
1065 display->fillrect(x + meterwidth, y + height / 2, 3, height / 2 - 1);
1068 /* draw scale end */
1069 display->vline(x + meterwidth, y, y + height - 2);
1071 /* draw dots for scale marks */
1072 for (i = 0; i < db_scale_count; i++) {
1073 /* The x-coordinates of interesting scale mark points
1074 have been calculated before */
1075 display->drawpixel(x + scales->db_scale_lcd_coord[i],
1076 y + height / 2 - 1);
1079 #ifdef HAVE_RECORDING
1081 #ifdef HAVE_BACKLIGHT
1082 /* cliplight */
1083 if ((pm_clip_left || pm_clip_right) &&
1084 global_settings.cliplight &&
1085 #if CONFIG_CODEC == SWCODEC
1086 !pm_playback)
1087 #else
1088 !(audio_status() & (AUDIO_STATUS_PLAY | AUDIO_STATUS_ERROR)))
1089 #endif
1091 /* if clipping, cliplight setting on and in recording screen */
1092 if (global_settings.cliplight <= 2)
1094 /* turn on main unit light if setting set to main or both*/
1095 backlight_on();
1097 #ifdef HAVE_REMOTE_LCD
1098 if (global_settings.cliplight >= 2)
1100 /* turn remote light unit on if setting set to remote or both */
1101 remote_backlight_on();
1103 #endif /* HAVE_REMOTE_LCD */
1105 #endif /* HAVE_BACKLIGHT */
1107 if (trig_status != TRIG_OFF) {
1108 int start_trigx, stop_trigx, ycenter;
1110 display->set_drawmode(DRMODE_SOLID);
1111 ycenter = y + height / 2;
1112 /* display threshold value */
1113 start_trigx = x+peak_meter_scale_value(trig_strt_threshold,meterwidth);
1114 display->vline(start_trigx, ycenter - 2, ycenter);
1115 start_trigx ++;
1116 if (start_trigx < display->getwidth() ) display->drawpixel(start_trigx,
1117 ycenter - 1);
1119 stop_trigx = x + peak_meter_scale_value(trig_stp_threshold,meterwidth);
1120 display->vline(stop_trigx, ycenter - 2, ycenter);
1121 if (stop_trigx > 0) display->drawpixel(stop_trigx - 1, ycenter - 1);
1123 #endif /*HAVE_RECORDING*/
1125 #ifdef PM_DEBUG
1126 /* display a bar to show how many calls to peak_meter_peek
1127 have ocurred since the last display */
1128 display->set_drawmode(DRMODE_COMPLEMENT);
1129 display->fillrect(x, y, tmp, 3);
1131 if (tmp < PEEKS_PER_DRAW_SIZE) {
1132 peeks_per_redraw[tmp]++;
1135 tmp = current_tick - pm_tick;
1136 if (tmp < TICKS_PER_DRAW_SIZE ){
1137 ticks_per_redraw[tmp] ++;
1140 /* display a bar to show how many ticks have passed since
1141 the last redraw */
1142 display->fillrect(x, y + height / 2, current_tick - pm_tick, 2);
1143 pm_tick = current_tick;
1144 #endif
1146 scales->last_left = left;
1147 scales->last_right = right;
1149 display->set_drawmode(DRMODE_SOLID);
1152 #ifdef HAVE_RECORDING
1154 * Defines the parameters of the trigger. After these parameters are defined
1155 * the trigger can be started either by peak_meter_attack_trigger or by
1156 * peak_meter_release_trigger. Note that you can pass either linear (%) or
1157 * logarithmic (db) values to the thresholds. Positive values are intepreted as
1158 * percent (0 is 0% .. 100 is 100%). Negative values are interpreted as db.
1159 * To avoid ambiguosity of the value 0 the negative values are shifted by -1.
1160 * Thus -75 is -74db .. -1 is 0db.
1161 * @param start_threshold - The threshold used for attack trigger. Negative
1162 * values are interpreted as db -1, positive as %.
1163 * @param start_duration - The minimum time span within which start_threshold
1164 * must be exceeded to fire the attack trigger.
1165 * @param start_dropout - The maximum time span the level may fall below
1166 * start_threshold without releasing the attack trigger.
1167 * @param stop_threshold - The threshold the volume must fall below to release
1168 * the release trigger.Negative values are
1169 * interpreted as db -1, positive as %.
1170 * @param stop_hold - The minimum time the volume must fall below the
1171 * stop_threshold to release the trigger.
1172 * @param
1174 void peak_meter_define_trigger(
1175 int start_threshold,
1176 long start_duration,
1177 long start_dropout,
1178 int stop_threshold,
1179 long stop_hold_time,
1180 long restart_gap
1183 if (start_threshold < 0) {
1184 /* db */
1185 if (start_threshold < -89) {
1186 trig_strt_threshold = 0;
1187 } else {
1188 trig_strt_threshold =peak_meter_db2sample((start_threshold+1)*100);
1190 } else {
1191 /* linear percent */
1192 trig_strt_threshold = start_threshold * MAX_PEAK / 100;
1194 trig_strt_duration = start_duration;
1195 trig_strt_dropout = start_dropout;
1196 if (stop_threshold < 0) {
1197 /* db */
1198 trig_stp_threshold = peak_meter_db2sample((stop_threshold + 1) * 100);
1199 } else {
1200 /* linear percent */
1201 trig_stp_threshold = stop_threshold * MAX_PEAK / 100;
1203 trig_stp_hold = stop_hold_time;
1204 trig_rstrt_gap = restart_gap;
1208 * Enables or disables the trigger.
1209 * @param on - If true the trigger is turned on.
1211 void peak_meter_trigger(bool on)
1213 /* don't use set_trigger here as that would fire an undesired event */
1214 trig_status = on ? TRIG_READY : TRIG_OFF;
1218 * Registers the listener function that listenes on trig_status changes.
1219 * @param listener - The function that is called with each change of
1220 * trig_status. May be set to NULL if no callback is desired.
1222 void peak_meter_set_trigger_listener(void (*listener)(int status))
1224 trigger_listener = listener;
1228 * Fetches the status of the trigger.
1229 * TRIG_OFF: the trigger is inactive
1230 * TRIG_RELEASED: The volume level is below the threshold
1231 * TRIG_ACTIVATED: The volume level has exceeded the threshold, but the trigger
1232 * hasn't been fired yet.
1233 * TRIG_FIRED: The volume exceeds the threshold
1235 * To activate the trigger call either peak_meter_attack_trigger or
1236 * peak_meter_release_trigger. To turn the trigger off call
1237 * peak_meter_trigger_off.
1239 int peak_meter_trigger_status(void)
1241 return trig_status; /* & TRIG_PIT_MASK;*/
1244 void peak_meter_draw_trig(int xpos[], int ypos[],
1245 int trig_width[], int nb_screens)
1247 int barstart[NB_SCREENS];
1248 int barend[NB_SCREENS];
1249 int icon;
1250 int ixpos[NB_SCREENS];
1251 int i;
1252 int trigbar_width[NB_SCREENS];
1254 FOR_NB_SCREENS(i)
1255 trigbar_width[i] = (trig_width[i] - (2 * (ICON_PLAY_STATE_WIDTH + 1)));
1257 switch (trig_status) {
1259 case TRIG_READY:
1260 FOR_NB_SCREENS(i){
1261 barstart[i] = 0;
1262 barend[i] = 0;
1264 icon = Icon_Stop;
1265 FOR_NB_SCREENS(i)
1266 ixpos[i] = xpos[i];
1267 break;
1269 case TRIG_STEADY:
1270 case TRIG_RETRIG:
1271 FOR_NB_SCREENS(i)
1273 barstart[i] = 0;
1274 barend[i] = (trig_strt_duration == 0) ? trigbar_width[i] :
1275 trigbar_width[i] *
1276 (current_tick - trig_hightime) / trig_strt_duration;
1278 icon = Icon_Stop;
1279 FOR_NB_SCREENS(i)
1280 ixpos[i] = xpos[i];
1281 break;
1283 case TRIG_GO:
1284 case TRIG_CONTINUE:
1285 FOR_NB_SCREENS(i)
1287 barstart[i] = trigbar_width[i];
1288 barend[i] = trigbar_width[i];
1290 icon = Icon_Record;
1291 FOR_NB_SCREENS(i)
1292 ixpos[i] = xpos[i]+ trig_width[i] - ICON_PLAY_STATE_WIDTH;
1293 break;
1295 case TRIG_POSTREC:
1296 FOR_NB_SCREENS(i)
1298 barstart[i] = (trig_stp_hold == 0) ? 0 :
1299 trigbar_width[i] - trigbar_width[i] *
1300 (current_tick - trig_lowtime) / trig_stp_hold;
1301 barend[i] = trigbar_width[i];
1303 icon = Icon_Record;
1304 FOR_NB_SCREENS(i)
1305 ixpos[i] = xpos[i] + trig_width[i] - ICON_PLAY_STATE_WIDTH;
1306 break;
1308 default:
1309 return;
1312 for(i = 0; i < nb_screens; i++)
1314 gui_scrollbar_draw(&screens[i], xpos[i] + ICON_PLAY_STATE_WIDTH + 1,
1315 ypos[i] + 1, trigbar_width[i], TRIG_HEIGHT - 2,
1316 trigbar_width[i], barstart[i], barend[i],
1317 HORIZONTAL);
1319 screens[i].mono_bitmap(bitmap_icons_7x8[icon], ixpos[i], ypos[i],
1320 ICON_PLAY_STATE_WIDTH, STATUSBAR_HEIGHT);
1323 #endif
1325 int peak_meter_draw_get_btn(int action_context, int x, int y[],
1326 int height, int nb_screens)
1328 int button = BUTTON_NONE;
1329 long next_refresh = current_tick;
1330 long next_big_refresh = current_tick + HZ / 10;
1331 int i;
1332 #if (CONFIG_CODEC == SWCODEC) || defined(SIMULATOR)
1333 bool highperf = false;
1334 #else
1335 /* On MAS targets, we need to poll as often as possible in order to not
1336 * miss a peak, as the MAS does only provide a quasi-peak. When the disk
1337 * is active, it must not draw too much CPU power or a buffer overrun can
1338 * happen when saving a recording. As a compromise, poll only once per tick
1339 * when the disk is active, otherwise spin around as fast as possible. */
1340 bool highperf = !ata_disk_is_active();
1341 #endif
1342 bool dopeek = true;
1344 while (TIME_BEFORE(current_tick, next_big_refresh)) {
1345 button = get_action(action_context, TIMEOUT_NOBLOCK);
1346 if (button != BUTTON_NONE) {
1347 break;
1349 if (dopeek) { /* Peek only once per refresh when disk is */
1350 peak_meter_peek(); /* spinning, but as often as possible */
1351 dopeek = highperf; /* otherwise. */
1352 yield();
1353 } else {
1354 sleep(0); /* Sleep until end of current tick. */
1356 if (TIME_AFTER(current_tick, next_refresh)) {
1357 for(i = 0; i < nb_screens; i++)
1359 peak_meter_screen(&screens[i], x, y[i], height);
1360 screens[i].update_viewport_rect(x, y[i],
1361 screens[i].getwidth() - x,
1362 height);
1364 next_refresh += HZ / PEAK_METER_FPS;
1365 dopeek = true;
1369 return button;
1372 #ifdef PM_DEBUG
1373 static void peak_meter_clear_histogram(void)
1375 int i = 0;
1376 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1377 ticks_per_redraw[i] = (unsigned int)0;
1380 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1381 peeks_per_redraw[i] = (unsigned int)0;
1385 bool peak_meter_histogram(void)
1387 int i;
1388 int btn = BUTTON_NONE;
1389 while ((btn & BUTTON_OFF) != BUTTON_OFF )
1391 unsigned int max = 0;
1392 int y = 0;
1393 int x = 0;
1394 screens[0].clear_display();
1396 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1397 max = MAX(max, peeks_per_redraw[i]);
1400 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1401 x = peeks_per_redraw[i] * (LCD_WIDTH - 1)/ max;
1402 screens[0].hline(0, x, y + i);
1405 y = PEEKS_PER_DRAW_SIZE + 1;
1406 max = 0;
1408 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1409 max = MAX(max, ticks_per_redraw[i]);
1412 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1413 x = ticks_per_redraw[i] * (LCD_WIDTH - 1)/ max;
1414 screens[0].hline(0, x, y + i);
1416 screens[0].update();
1418 btn = button_get(true);
1419 if (btn == BUTTON_PLAY) {
1420 peak_meter_clear_histogram();
1423 return false;
1425 #endif