remove "empty download cache" menu entry -- clearing the cache will be in the configu...
[Rockbox.git] / apps / recorder / peakmeter.c
blobf06b19ab2daf61ec2de23d43187dc3081fc90ab1
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Philipp Pertermann
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "config.h"
20 #include "mas.h"
21 #include "thread.h"
22 #include "kernel.h"
23 #include "settings.h"
24 #include "ata.h"
25 #include "lcd.h"
26 #include "scrollbar.h"
27 #include "gwps.h"
28 #include "sprintf.h"
29 #include "button.h"
30 #include "system.h"
31 #include "font.h"
32 #include "icons.h"
33 #include "lang.h"
34 #include "peakmeter.h"
35 #include "audio.h"
36 #include "screen_access.h"
37 #ifdef HAVE_BACKLIGHT
38 #include "backlight.h"
39 #endif
40 #include "action.h"
42 #if CONFIG_CODEC == SWCODEC
43 #include "pcm_playback.h"
45 #ifdef HAVE_RECORDING
46 #include "pcm_record.h"
47 #endif
49 static bool pm_playback = true; /* selects between playback and recording peaks */
51 #endif
53 static struct meter_scales scales[NB_SCREENS];
55 #if !defined(SIMULATOR) && CONFIG_CODEC != SWCODEC
56 /* Data source */
57 static int pm_src_left = MAS_REG_DQPEAK_L;
58 static int pm_src_right = MAS_REG_DQPEAK_R;
59 #endif
61 /* Current values and cumulation */
62 static int pm_cur_left; /* current values (last peak_meter_peek) */
63 static int pm_cur_right;
64 static int pm_max_left; /* maximum values between peak meter draws */
65 static int pm_max_right;
66 #ifdef HAVE_AGC
67 static int pm_peakhold_left; /* max. peak values between peakhold calls */
68 static int pm_peakhold_right; /* used for AGC and histogram display */
69 #endif
71 /* Clip hold */
72 static bool pm_clip_left = false; /* when true a clip has occurred */
73 static bool pm_clip_right = false;
74 static long pm_clip_timeout_l; /* clip hold timeouts */
75 static long pm_clip_timeout_r;
77 /* Temporarily en- / disables peak meter. This is especially for external
78 applications to detect if the peak_meter is in use and needs drawing at all */
79 bool peak_meter_enabled = true;
81 /** Parameters **/
82 /* Range */
83 unsigned short peak_meter_range_min; /* minimum of range in samples */
84 unsigned short peak_meter_range_max; /* maximum of range in samples */
85 static unsigned short pm_range; /* range width in samples */
86 static bool pm_use_dbfs = true; /* true if peakmeter displays dBfs */
87 static bool level_check; /* true if peeked at peakmeter before drawing */
88 static unsigned short pm_db_min = 0; /* minimum of range in 1/100 dB */
89 static unsigned short pm_db_max = 9000; /* maximum of range in 1/100 dB */
90 static unsigned short pm_db_range = 9000; /* range width in 1/100 dB */
91 /* Timing behaviour */
92 static int pm_peak_hold = 1; /* peak hold timeout index */
93 static int pm_peak_release = 8; /* peak release in units per read */
94 static int pm_clip_hold = 16; /* clip hold timeout index */
95 static bool pm_clip_eternal = false; /* true if clip timeout is disabled */
97 #ifdef HAVE_RECORDING
98 static unsigned short trig_strt_threshold;
99 static long trig_strt_duration;
100 static long trig_strt_dropout;
101 static unsigned short trig_stp_threshold;
102 static long trig_stp_hold;
103 static long trig_rstrt_gap;
105 /* point in time when the threshold was exceeded */
106 static long trig_hightime;
108 /* point in time when the volume fell below the threshold*/
109 static long trig_lowtime;
111 /* The output value of the trigger. See TRIG_XXX constants for valid values */
112 static int trig_status = TRIG_OFF;
114 static void (*trigger_listener)(int) = NULL;
115 #endif
117 /* debug only */
118 #ifdef PM_DEBUG
119 static int peek_calls = 0;
121 #define PEEKS_PER_DRAW_SIZE 40
122 static unsigned int peeks_per_redraw[PEEKS_PER_DRAW_SIZE];
124 #define TICKS_PER_DRAW_SIZE 20
125 static unsigned int ticks_per_redraw[TICKS_PER_DRAW_SIZE];
126 #endif
128 /* time out values for max */
129 static const short peak_time_out[] = {
130 0 * HZ, HZ / 5, 30, HZ / 2, HZ, 2 * HZ,
131 3 * HZ, 4 * HZ, 5 * HZ, 6 * HZ, 7 * HZ, 8 * HZ,
132 9 * HZ, 10 * HZ, 15 * HZ, 20 * HZ, 30 * HZ, 60 * HZ
135 /* time out values for clip */
136 static const long clip_time_out[] = {
137 0 * HZ, 1 * HZ, 2 * HZ, 3 * HZ, 4 * HZ, 5 * HZ,
138 6 * HZ, 7 * HZ, 8 * HZ, 9 * HZ, 10 * HZ, 15 * HZ,
139 20 * HZ, 25 * HZ, 30 * HZ, 45 * HZ, 60 * HZ, 90 * HZ,
140 120 * HZ, 180 * HZ, 300 * HZ, 600L * HZ, 1200L * HZ,
141 2700L * HZ, 5400L * HZ
144 /* precalculated peak values that represent magical
145 dBfs values. Used to draw the scale */
146 static const int db_scale_src_values[DB_SCALE_SRC_VALUES_SIZE] = {
147 32736, /* 0 db */
148 22752, /* - 3 db */
149 16640, /* - 6 db */
150 11648, /* - 9 db */
151 8320, /* -12 db */
152 4364, /* -18 db */
153 2064, /* -24 db */
154 1194, /* -30 db */
155 363, /* -40 db */
156 101, /* -50 db */
157 34, /* -60 db */
158 0, /* -inf */
161 static int db_scale_count = DB_SCALE_SRC_VALUES_SIZE;
164 * Calculates dB Value for the peak meter, uses peak value as input
165 * @param int sample - The input value
166 * Make sure that 0 <= value < SAMPLE_RANGE
168 * @return int - The 2 digit fixed point result of the euation
169 * 20 * log (sample / SAMPLE_RANGE) + 90
170 * Output range is 0-9000 (that is 0.0 - 90.0 dB).
171 * Normally 0dB is full scale, here it is shifted +90dB.
172 * The calculation is based on the results of a linear
173 * approximation tool written specifically for this problem
174 * by Andreas Zwirtes (radhard@gmx.de). The result has an
175 * accurracy of better than 2%. It is highly runtime optimized,
176 * the cascading if-clauses do an successive approximation on
177 * the input value. This avoids big lookup-tables and
178 * for-loops.
179 * Improved by Jvo Studer for errors < 0.2dB for critical
180 * range of -12dB to 0dB (78.0 to 90.0dB).
183 int calc_db (int isample)
185 /* return n+m*(isample-istart)/100 */
186 int n;
187 long m;
188 int istart;
190 if (isample < 2308) { /* Range 1-5 */
192 if (isample < 115) { /* Range 1-3 */
194 if (isample < 24) {
196 if (isample < 5) {
197 istart = 1; /* Range 1 */
198 n = 98;
199 m = 34950;
201 else {
202 istart = 5; /* Range 2 */
203 n = 1496;
204 m = 7168;
207 else {
208 istart = 24; /* Range 3 */
209 n = 2858;
210 m = 1498;
213 else { /* Range 4-5 */
215 if (isample < 534) {
216 istart = 114; /* Range 4 */
217 n = 4207;
218 m = 319;
220 else {
221 istart = 588; /* Range 5 */
222 n = 5583;
223 m = 69;
228 else { /* Range 6-9 */
230 if (isample < 12932) {
232 if (isample < 6394) {
233 istart = 2608; /* Range 6 */
234 n = 6832;
235 m = 21;
237 else {
238 istart = 7000; /* Range 7 */
239 n = 7682;
240 m = 9;
243 else {
245 if (isample < 22450) {
246 istart = 13000; /* Range 8 */
247 n = 8219;
248 m = 5;
250 else {
251 istart = 22636; /* Range 9 */
252 n = 8697;
253 m = 3;
258 return n + (m * (long)(isample - istart)) / 100L;
263 * A helper function for peak_meter_db2sample. Don't call it separately but
264 * use peak_meter_db2sample. If one or both of min and max are outside the
265 * range 0 <= min (or max) < 8961 the behaviour of this function is
266 * undefined. It may not return.
267 * @param int min - The minimum of the value range that is searched.
268 * @param int max - The maximum of the value range that is searched.
269 * @param int db - The value in dBfs * (-100) for which the according
270 * minimal peak sample is searched.
271 * @return int - A linear volume value with 0 <= value < MAX_PEAK
273 static int db_to_sample_bin_search(int min, int max, int db)
275 int test = min + (max - min) / 2;
277 if (min < max) {
278 if (calc_db(test) < db) {
279 test = db_to_sample_bin_search(test, max, db);
280 } else {
281 if (calc_db(test-1) > db) {
282 test = db_to_sample_bin_search(min, test, db);
286 return test;
290 * Converts a value representing dBfs to a linear
291 * scaled volume info as it is used by the MAS.
292 * An incredibly inefficiant function which is
293 * the vague inverse of calc_db. This really
294 * should be replaced by something better soon.
296 * @param int db - A dBfs * 100 value with
297 * -9000 < value <= 0
298 * @return int - The return value is in the range of
299 * 0 <= return value < MAX_PEAK
301 int peak_meter_db2sample(int db)
303 int retval = 0;
305 /* what is the maximum pseudo db value */
306 int max_peak_db = calc_db(MAX_PEAK - 1);
308 /* range check: db value to big */
309 if (max_peak_db + db < 0) {
310 retval = 0;
313 /* range check: db value too small */
314 else if (max_peak_db + db >= max_peak_db) {
315 retval = MAX_PEAK -1;
318 /* value in range: find the matching linear value */
319 else {
320 retval = db_to_sample_bin_search(0, MAX_PEAK, max_peak_db + db);
322 /* as this is a dirty function anyway, we want to adjust the
323 full scale hit manually to avoid users complaining that when
324 they adjust maximum for 0 dBfs and display it in percent it
325 shows 99%. That is due to precision loss and this is the
326 optical fix */
329 return retval;
333 * Set the min value for restriction of the value range.
334 * @param int newmin - depending whether dBfs is used
335 * newmin is a value in dBfs * 100 or in linear percent values.
336 * for dBfs: -9000 < newmin <= 0
337 * for linear: 0 <= newmin <= 100
339 void peak_meter_set_min(int newmin)
341 if (pm_use_dbfs) {
342 peak_meter_range_min = peak_meter_db2sample(newmin);
344 } else {
345 if (newmin < peak_meter_range_max) {
346 peak_meter_range_min = newmin * MAX_PEAK / 100;
350 pm_range = peak_meter_range_max - peak_meter_range_min;
352 /* Avoid division by zero. */
353 if (pm_range == 0) {
354 pm_range = 1;
357 pm_db_min = calc_db(peak_meter_range_min);
358 pm_db_range = pm_db_max - pm_db_min;
359 int i;
360 FOR_NB_SCREENS(i)
361 scales[i].db_scale_valid = false;
365 * Returns the minimum value of the range the meter
366 * displays. If the scale is set to dBfs it returns
367 * dBfs values * 100 or linear percent values.
368 * @return: using dBfs : -9000 < value <= 0
369 * using linear scale: 0 <= value <= 100
371 int peak_meter_get_min(void)
373 int retval = 0;
374 if (pm_use_dbfs) {
375 retval = calc_db(peak_meter_range_min) - calc_db(MAX_PEAK - 1);
376 } else {
377 retval = peak_meter_range_min * 100 / MAX_PEAK;
379 return retval;
383 * Set the max value for restriction of the value range.
384 * @param int newmax - depending wether dBfs is used
385 * newmax is a value in dBfs * 100 or in linear percent values.
386 * for dBfs: -9000 < newmax <= 0
387 * for linear: 0 <= newmax <= 100
389 void peak_meter_set_max(int newmax)
391 if (pm_use_dbfs) {
392 peak_meter_range_max = peak_meter_db2sample(newmax);
393 } else {
394 if (newmax > peak_meter_range_min) {
395 peak_meter_range_max = newmax * MAX_PEAK / 100;
399 pm_range = peak_meter_range_max - peak_meter_range_min;
401 /* Avoid division by zero. */
402 if (pm_range == 0) {
403 pm_range = 1;
406 pm_db_max = calc_db(peak_meter_range_max);
407 pm_db_range = pm_db_max - pm_db_min;
408 int i;
409 FOR_NB_SCREENS(i)
410 scales[i].db_scale_valid = false;
414 * Returns the minimum value of the range the meter
415 * displays. If the scale is set to dBfs it returns
416 * dBfs values * 100 or linear percent values
417 * @return: using dBfs : -9000 < value <= 0
418 * using linear scale: 0 <= value <= 100
420 int peak_meter_get_max(void)
422 int retval = 0;
423 if (pm_use_dbfs) {
424 retval = calc_db(peak_meter_range_max) - calc_db(MAX_PEAK - 1);
425 } else {
426 retval = peak_meter_range_max * 100 / MAX_PEAK;
428 return retval;
432 * Returns whether the meter is currently displaying dBfs or percent values.
433 * @return bool - true if the meter is displaying dBfs
434 false if the meter is displaying percent values.
436 bool peak_meter_get_use_dbfs(void)
438 return pm_use_dbfs;
442 * Specifies whether the values displayed are scaled
443 * as dBfs or as linear percent values.
444 * @param use - set to true for dBfs,
445 * set to false for linear scaling in percent
447 void peak_meter_set_use_dbfs(bool use)
449 int i;
450 pm_use_dbfs = use;
451 FOR_NB_SCREENS(i)
452 scales[i].db_scale_valid = false;
456 * Initialize the range of the meter. Only values
457 * that are in the range of [range_min ... range_max]
458 * are displayed.
459 * @param bool dbfs - set to true for dBfs,
460 * set to false for linear scaling in percent
461 * @param int range_min - Specifies the lower value of the range.
462 * Pass a value dBfs * 100 when dbfs is set to true.
463 * Pass a percent value when dbfs is set to false.
464 * @param int range_max - Specifies the upper value of the range.
465 * Pass a value dBfs * 100 when dbfs is set to true.
466 * Pass a percent value when dbfs is set to false.
468 void peak_meter_init_range( bool dbfs, int range_min, int range_max)
470 pm_use_dbfs = dbfs;
471 peak_meter_set_min(range_min);
472 peak_meter_set_max(range_max);
476 * Initialize the peak meter with all relevant values concerning times.
477 * @param int release - Set the maximum amount of pixels the meter is allowed
478 * to decrease with each redraw
479 * @param int hold - Select the time preset for the time the peak indicator
480 * is reset after a peak occurred. The preset values are
481 * stored in peak_time_out.
482 * @param int clip_hold - Select the time preset for the time the peak
483 * indicator is reset after a peak occurred. The preset
484 * values are stored in clip_time_out.
486 void peak_meter_init_times(int release, int hold, int clip_hold)
488 pm_peak_hold = hold;
489 pm_peak_release = release;
490 pm_clip_hold = clip_hold;
494 * Set the source of the peak meter to playback or to
495 * record.
496 * @param: bool playback - If true playback peak meter is used.
497 * If false recording peak meter is used.
499 void peak_meter_playback(bool playback)
501 #ifdef SIMULATOR
502 (void)playback;
503 #elif CONFIG_CODEC == SWCODEC
504 pm_playback = playback;
505 #else
506 if (playback) {
507 pm_src_left = MAS_REG_DQPEAK_L;
508 pm_src_right = MAS_REG_DQPEAK_R;
509 } else {
510 pm_src_left = MAS_REG_QPEAK_L;
511 pm_src_right = MAS_REG_QPEAK_R;
513 #endif
516 #ifdef HAVE_RECORDING
517 static void set_trig_status(int new_state)
519 if (trig_status != new_state) {
520 trig_status = new_state;
521 if (trigger_listener != NULL) {
522 trigger_listener(trig_status);
526 #endif
529 * Reads peak values from the MAS, and detects clips. The
530 * values are stored in pm_max_left pm_max_right for later
531 * evauluation. Consecutive calls to peak_meter_peek detect
532 * that ocurred. This function could be used by a thread for
533 * busy reading the MAS.
535 void peak_meter_peek(void)
537 int left, right;
538 /* read current values */
539 #if CONFIG_CODEC == SWCODEC
540 if (pm_playback)
541 pcm_calculate_peaks(&pm_cur_left, &pm_cur_right);
542 #ifdef HAVE_RECORDING
543 else
544 pcm_calculate_rec_peaks(&pm_cur_left, &pm_cur_right);
545 #endif
546 left = pm_cur_left;
547 right = pm_cur_right;
548 #else
549 #ifndef SIMULATOR
550 pm_cur_left = left = mas_codec_readreg(pm_src_left);
551 pm_cur_right = right = mas_codec_readreg(pm_src_right);
552 #else
553 pm_cur_left = left = 8000;
554 pm_cur_right = right = 9000;
555 #endif
556 #endif
558 /* check for clips
559 An clip is assumed when two consecutive readouts
560 of the volume are at full scale. This is proven
561 to be inaccurate in both ways: it may detect clips
562 when no clip occurred and it may fail to detect
563 a real clip. For software codecs, the peak is already
564 the max of a bunch of samples, so use one max value
565 or you fail to detect clipping! */
566 #if CONFIG_CODEC == SWCODEC
567 if (left == MAX_PEAK - 1) {
568 #else
569 if ((left == pm_max_left) &&
570 (left == MAX_PEAK - 1)) {
571 #endif
572 pm_clip_left = true;
573 pm_clip_timeout_l =
574 current_tick + clip_time_out[pm_clip_hold];
577 #if CONFIG_CODEC == SWCODEC
578 if (right == MAX_PEAK - 1) {
579 #else
580 if ((right == pm_max_right) &&
581 (right == MAX_PEAK - 1)) {
582 #endif
583 pm_clip_right = true;
584 pm_clip_timeout_r =
585 current_tick + clip_time_out[pm_clip_hold];
588 /* peaks are searched -> we have to find the maximum. When
589 many calls of peak_meter_peek the maximum value will be
590 stored in pm_max_xxx. This maximum is reset by the
591 functions peak_meter_read_x. */
592 pm_max_left = MAX(pm_max_left, left);
593 pm_max_right = MAX(pm_max_right, right);
595 #ifdef HAVE_RECORDING
596 #if CONFIG_CODEC == SWCODEC
597 /* Ignore any unread peakmeter data */
598 #define MAX_DROP_TIME HZ/7 /* this value may need tweaking. Increase if you are
599 getting trig events when you shouldn't with
600 trig_stp_hold = 0 */
601 if (!trig_stp_hold)
602 trig_stp_hold = MAX_DROP_TIME;
603 #endif
605 switch (trig_status) {
606 case TRIG_READY:
607 /* no more changes, if trigger was activated as release trigger */
608 /* threshold exceeded? */
609 if ((left > trig_strt_threshold)
610 || (right > trig_strt_threshold)) {
611 /* reset trigger duration */
612 trig_hightime = current_tick;
614 /* reset dropout duration */
615 trig_lowtime = current_tick;
617 if (trig_strt_duration)
618 set_trig_status(TRIG_STEADY);
619 else
620 /* if trig_duration is set to 0 the user wants to start
621 recording immediately */
622 set_trig_status(TRIG_GO);
624 break;
626 case TRIG_STEADY:
627 case TRIG_RETRIG:
628 /* trigger duration exceeded */
629 if (current_tick - trig_hightime > trig_strt_duration) {
630 set_trig_status(TRIG_GO);
631 } else {
632 /* threshold exceeded? */
633 if ((left > trig_strt_threshold)
634 || (right > trig_strt_threshold)) {
635 /* reset lowtime */
636 trig_lowtime = current_tick;
638 /* volume is below threshold */
639 else {
640 /* dropout occurred? */
641 if (current_tick - trig_lowtime > trig_strt_dropout){
642 if (trig_status == TRIG_STEADY){
643 set_trig_status(TRIG_READY);
645 /* trig_status == TRIG_RETRIG */
646 else {
647 /* the gap has already expired */
648 trig_lowtime = current_tick - trig_rstrt_gap - 1;
649 set_trig_status(TRIG_POSTREC);
654 break;
656 case TRIG_GO:
657 case TRIG_CONTINUE:
658 /* threshold exceeded? */
659 if ((left > trig_stp_threshold)
660 || (right > trig_stp_threshold)) {
661 /* restart hold time countdown */
662 trig_lowtime = current_tick;
663 #if CONFIG_CODEC == SWCODEC
664 } else if (current_tick - trig_lowtime > MAX_DROP_TIME){
665 #else
666 } else {
667 #endif
668 set_trig_status(TRIG_POSTREC);
669 trig_hightime = current_tick;
671 break;
673 case TRIG_POSTREC:
674 /* gap time expired? */
675 if (current_tick - trig_lowtime > trig_rstrt_gap){
676 /* start threshold exceeded? */
677 if ((left > trig_strt_threshold)
678 || (right > trig_strt_threshold)) {
680 set_trig_status(TRIG_RETRIG);
681 trig_hightime = current_tick;
682 trig_lowtime = current_tick;
684 else
686 /* stop threshold exceeded */
687 if ((left > trig_stp_threshold)
688 || (right > trig_stp_threshold)) {
689 if (current_tick - trig_hightime > trig_stp_hold){
690 trig_lowtime = current_tick;
691 set_trig_status(TRIG_CONTINUE);
692 } else {
693 trig_lowtime = current_tick - trig_rstrt_gap - 1;
697 /* below any threshold */
698 else {
699 if (current_tick - trig_lowtime > trig_stp_hold){
700 set_trig_status(TRIG_READY);
701 } else {
702 trig_hightime = current_tick;
707 /* still within the gap time */
708 else {
709 /* stop threshold exceeded */
710 if ((left > trig_stp_threshold)
711 || (right > trig_stp_threshold)) {
712 set_trig_status(TRIG_CONTINUE);
713 trig_lowtime = current_tick;
716 /* hold time expired */
717 else if (current_tick - trig_lowtime > trig_stp_hold){
718 trig_hightime = current_tick;
719 trig_lowtime = current_tick;
720 set_trig_status(TRIG_READY);
723 break;
725 #if CONFIG_CODEC == SWCODEC
726 /* restore stop hold value */
727 if (trig_stp_hold == MAX_DROP_TIME)
728 trig_stp_hold = 0;
729 #endif
730 #endif
731 /* check levels next time peakmeter drawn */
732 level_check = true;
733 #ifdef PM_DEBUG
734 peek_calls++;
735 #endif
739 * Reads out the peak volume of the left channel.
740 * @return int - The maximum value that has been detected
741 * since the last call of peak_meter_read_l. The value
742 * is in the range 0 <= value < MAX_PEAK.
744 static int peak_meter_read_l(void)
746 /* pm_max_left contains the maximum of all peak values that were read
747 by peak_meter_peek since the last call of peak_meter_read_l */
748 int retval = pm_max_left;
750 #ifdef HAVE_AGC
751 /* store max peak value for peak_meter_get_peakhold_x readout */
752 pm_peakhold_left = MAX(pm_max_left, pm_peakhold_left);
753 #endif
754 #ifdef PM_DEBUG
755 peek_calls = 0;
756 #endif
757 /* reset pm_max_left so that subsequent calls of peak_meter_peek don't
758 get fooled by an old maximum value */
759 pm_max_left = pm_cur_left;
760 return retval;
764 * Reads out the peak volume of the right channel.
765 * @return int - The maximum value that has been detected
766 * since the last call of peak_meter_read_l. The value
767 * is in the range 0 <= value < MAX_PEAK.
769 static int peak_meter_read_r(void)
771 /* peak_meter_r contains the maximum of all peak values that were read
772 by peak_meter_peek since the last call of peak_meter_read_r */
773 int retval = pm_max_right;
775 #ifdef HAVE_AGC
776 /* store max peak value for peak_meter_get_peakhold_x readout */
777 pm_peakhold_right = MAX(pm_max_right, pm_peakhold_right);
778 #endif
779 #ifdef PM_DEBUG
780 peek_calls = 0;
781 #endif
782 /* reset pm_max_right so that subsequent calls of peak_meter_peek don't
783 get fooled by an old maximum value */
784 pm_max_right = pm_cur_right;
785 return retval;
788 #ifdef HAVE_AGC
790 * Reads out the current peak-hold values since the last call.
791 * This is used by the histogram feature in the recording screen.
792 * Values are in the range 0 <= peak_x < MAX_PEAK. MAX_PEAK is typ 32767.
794 void peak_meter_get_peakhold(int *peak_left, int *peak_right)
796 if (peak_left)
797 *peak_left = pm_peakhold_left;
798 if (peak_right)
799 *peak_right = pm_peakhold_right;
800 pm_peakhold_left = 0;
801 pm_peakhold_right = 0;
803 #endif
806 * Reset the detected clips. This method is for
807 * use by the user interface.
808 * @param int unused - This parameter was added to
809 * make the function compatible with set_int
811 void peak_meter_set_clip_hold(int time)
813 pm_clip_eternal = false;
815 if (time <= 0) {
816 pm_clip_left = false;
817 pm_clip_right = false;
818 pm_clip_eternal = true;
823 * Scales a peak value as read from the MAS to the range of meterwidth.
824 * The scaling is performed according to the scaling method (dBfs / linear)
825 * and the range (peak_meter_range_min .. peak_meter_range_max).
826 * @param unsigned short val - The volume value. Range: 0 <= val < MAX_PEAK
827 * @param int meterwidht - The widht of the meter in pixel
828 * @return unsigned short - A value 0 <= return value <= meterwidth
830 unsigned short peak_meter_scale_value(unsigned short val, int meterwidth)
832 int retval;
834 if (val <= peak_meter_range_min) {
835 return 0;
838 if (val >= peak_meter_range_max) {
839 return meterwidth;
842 retval = val;
844 /* different scaling is used for dBfs and linear percent */
845 if (pm_use_dbfs) {
847 /* scale the samples dBfs */
848 retval = (calc_db(retval) - pm_db_min) * meterwidth / pm_db_range;
851 /* Scale for linear percent display */
852 else
854 /* scale the samples */
855 retval = ((retval - peak_meter_range_min) * meterwidth)
856 / pm_range;
858 return retval;
860 void peak_meter_screen(struct screen *display, int x, int y, int height)
862 peak_meter_draw(display, &scales[display->screen_type], x, y,
863 display->width, height);
866 * Draws a peak meter in the specified size at the specified position.
867 * @param int x - The x coordinate.
868 * Make sure that 0 <= x and x + width < display->width
869 * @param int y - The y coordinate.
870 * Make sure that 0 <= y and y + height < display->height
871 * @param int width - The width of the peak meter. Note that for display
872 * of clips a 3 pixel wide area is used ->
873 * width > 3
874 * @param int height - The height of the peak meter. height > 3
876 void peak_meter_draw(struct screen *display, struct meter_scales *scales,
877 int x, int y, int width, int height)
879 static int left_level = 0, right_level = 0;
880 int left = 0, right = 0;
881 int meterwidth = width - 3;
882 int i;
884 #ifdef PM_DEBUG
885 static long pm_tick = 0;
886 int tmp = peek_calls;
887 #endif
889 /* if disabled only draw the peak meter */
890 if (peak_meter_enabled) {
893 if (level_check){
894 /* only read the volume info from MAS if peek since last read*/
895 left_level = peak_meter_read_l();
896 right_level = peak_meter_read_r();
897 level_check = false;
900 /* scale the samples dBfs */
901 left = peak_meter_scale_value(left_level, meterwidth);
902 right = peak_meter_scale_value(right_level, meterwidth);
904 /*if the scale has changed -> recalculate the scale
905 (The scale becomes invalid when the range changed.) */
906 if (!scales->db_scale_valid){
908 if (pm_use_dbfs) {
909 db_scale_count = DB_SCALE_SRC_VALUES_SIZE;
910 for (i = 0; i < db_scale_count; i++){
911 /* find the real x-coords for predefined interesting
912 dBfs values. These only are recalculated when the
913 scaling of the meter changed. */
914 scales->db_scale_lcd_coord[i] =
915 peak_meter_scale_value(
916 db_scale_src_values[i],
917 meterwidth - 1);
921 /* when scaling linear we simly make 10% steps */
922 else {
923 db_scale_count = 10;
924 for (i = 0; i < db_scale_count; i++) {
925 scales->db_scale_lcd_coord[i] =
926 (i * (MAX_PEAK / 10) - peak_meter_range_min) *
927 meterwidth / pm_range;
931 /* mark scale valid to avoid recalculating dBfs values
932 of the scale. */
933 scales->db_scale_valid = true;
936 /* apply release */
937 left = MAX(left , scales->last_left - pm_peak_release);
938 right = MAX(right, scales->last_right - pm_peak_release);
940 /* reset max values after timeout */
941 if (TIME_AFTER(current_tick, scales->pm_peak_timeout_l)){
942 scales->pm_peak_left = 0;
945 if (TIME_AFTER(current_tick, scales->pm_peak_timeout_r)){
946 scales->pm_peak_right = 0;
949 if (!pm_clip_eternal) {
950 if (pm_clip_left &&
951 TIME_AFTER(current_tick, pm_clip_timeout_l)){
952 pm_clip_left = false;
955 if (pm_clip_right &&
956 TIME_AFTER(current_tick, pm_clip_timeout_r)){
957 pm_clip_right = false;
961 /* check for new max values */
962 if (left > scales->pm_peak_left) {
963 scales->pm_peak_left = left - 1;
964 scales->pm_peak_timeout_l =
965 current_tick + peak_time_out[pm_peak_hold];
968 if (right > scales->pm_peak_right) {
969 scales->pm_peak_right = right - 1;
970 scales->pm_peak_timeout_r =
971 current_tick + peak_time_out[pm_peak_hold];
975 /* draw the peak meter */
976 display->set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
977 display->fillrect(x, y, width, height);
978 display->set_drawmode(DRMODE_SOLID);
980 /* draw left */
981 display->fillrect (x, y, left, height / 2 - 2 );
982 if (scales->pm_peak_left > 0) {
983 display->vline(x + scales->pm_peak_left, y, y + height / 2 - 2 );
985 if (pm_clip_left) {
986 display->fillrect(x + meterwidth, y, 3, height / 2 - 1);
989 /* draw right */
990 display->fillrect(x, y + height / 2 + 1, right, height / 2 - 2);
991 if (scales->pm_peak_right > 0) {
992 display->vline( x + scales->pm_peak_right, y + height / 2, y + height - 2);
994 if (pm_clip_right) {
995 display->fillrect(x + meterwidth, y + height / 2, 3, height / 2 - 1);
998 /* draw scale end */
999 display->vline(x + meterwidth, y, y + height - 2);
1001 /* draw dots for scale marks */
1002 for (i = 0; i < db_scale_count; i++) {
1003 /* The x-coordinates of interesting scale mark points
1004 have been calculated before */
1005 display->drawpixel(x + scales->db_scale_lcd_coord[i],
1006 y + height / 2 - 1);
1009 #ifdef HAVE_RECORDING
1011 #ifdef HAVE_BACKLIGHT
1012 /* cliplight */
1013 if ((pm_clip_left || pm_clip_right) &&
1014 global_settings.cliplight &&
1015 #if CONFIG_CODEC == SWCODEC
1016 !pm_playback)
1017 #else
1018 !(audio_status() & (AUDIO_STATUS_PLAY | AUDIO_STATUS_ERROR)))
1019 #endif
1021 /* if clipping, cliplight setting on and in recording screen */
1022 if (global_settings.cliplight <= 2)
1024 /* turn on main unit light if setting set to main or both*/
1025 backlight_on();
1027 #ifdef HAVE_REMOTE_LCD
1028 if (global_settings.cliplight >= 2)
1030 /* turn remote light unit on if setting set to remote or both */
1031 remote_backlight_on();
1033 #endif /* HAVE_REMOTE_LCD */
1035 #endif /* HAVE_BACKLIGHT */
1037 if (trig_status != TRIG_OFF) {
1038 int start_trigx, stop_trigx, ycenter;
1040 display->set_drawmode(DRMODE_SOLID);
1041 ycenter = y + height / 2;
1042 /* display threshold value */
1043 start_trigx = x+peak_meter_scale_value(trig_strt_threshold,meterwidth);
1044 display->vline(start_trigx, ycenter - 2, ycenter);
1045 start_trigx ++;
1046 if (start_trigx < display->width ) display->drawpixel(start_trigx, ycenter - 1);
1048 stop_trigx = x + peak_meter_scale_value(trig_stp_threshold,meterwidth);
1049 display->vline(stop_trigx, ycenter - 2, ycenter);
1050 if (stop_trigx > 0) display->drawpixel(stop_trigx - 1, ycenter - 1);
1052 #endif /*HAVE_RECORDING*/
1054 #ifdef PM_DEBUG
1055 /* display a bar to show how many calls to peak_meter_peek
1056 have ocurred since the last display */
1057 display->set_drawmode(DRMODE_COMPLEMENT);
1058 display->fillrect(x, y, tmp, 3);
1060 if (tmp < PEEKS_PER_DRAW_SIZE) {
1061 peeks_per_redraw[tmp]++;
1064 tmp = current_tick - pm_tick;
1065 if (tmp < TICKS_PER_DRAW_SIZE ){
1066 ticks_per_redraw[tmp] ++;
1069 /* display a bar to show how many ticks have passed since
1070 the last redraw */
1071 display->fillrect(x, y + height / 2, current_tick - pm_tick, 2);
1072 pm_tick = current_tick;
1073 #endif
1075 scales->last_left = left;
1076 scales->last_right = right;
1078 display->set_drawmode(DRMODE_SOLID);
1081 #ifdef HAVE_RECORDING
1083 * Defines the parameters of the trigger. After these parameters are defined
1084 * the trigger can be started either by peak_meter_attack_trigger or by
1085 * peak_meter_release_trigger. Note that you can pass either linear (%) or
1086 * logarithmic (db) values to the thresholds. Positive values are intepreted as
1087 * percent (0 is 0% .. 100 is 100%). Negative values are interpreted as db.
1088 * To avoid ambiguosity of the value 0 the negative values are shifted by -1.
1089 * Thus -75 is -74db .. -1 is 0db.
1090 * @param start_threshold - The threshold used for attack trigger. Negative
1091 * values are interpreted as db -1, positive as %.
1092 * @param start_duration - The minimum time span within which start_threshold
1093 * must be exceeded to fire the attack trigger.
1094 * @param start_dropout - The maximum time span the level may fall below
1095 * start_threshold without releasing the attack trigger.
1096 * @param stop_threshold - The threshold the volume must fall below to release
1097 * the release trigger.Negative values are
1098 * interpreted as db -1, positive as %.
1099 * @param stop_hold - The minimum time the volume must fall below the
1100 * stop_threshold to release the trigger.
1101 * @param
1103 void peak_meter_define_trigger(
1104 int start_threshold,
1105 long start_duration,
1106 long start_dropout,
1107 int stop_threshold,
1108 long stop_hold_time,
1109 long restart_gap
1112 if (start_threshold < 0) {
1113 /* db */
1114 if (start_threshold < -89) {
1115 trig_strt_threshold = 0;
1116 } else {
1117 trig_strt_threshold =peak_meter_db2sample((start_threshold+1)*100);
1119 } else {
1120 /* linear percent */
1121 trig_strt_threshold = start_threshold * MAX_PEAK / 100;
1123 trig_strt_duration = start_duration;
1124 trig_strt_dropout = start_dropout;
1125 if (stop_threshold < 0) {
1126 /* db */
1127 trig_stp_threshold = peak_meter_db2sample((stop_threshold + 1) * 100);
1128 } else {
1129 /* linear percent */
1130 trig_stp_threshold = stop_threshold * MAX_PEAK / 100;
1132 trig_stp_hold = stop_hold_time;
1133 trig_rstrt_gap = restart_gap;
1137 * Enables or disables the trigger.
1138 * @param on - If true the trigger is turned on.
1140 void peak_meter_trigger(bool on)
1142 /* don't use set_trigger here as that would fire an undesired event */
1143 trig_status = on ? TRIG_READY : TRIG_OFF;
1147 * Registers the listener function that listenes on trig_status changes.
1148 * @param listener - The function that is called with each change of
1149 * trig_status. May be set to NULL if no callback is desired.
1151 void peak_meter_set_trigger_listener(void (*listener)(int status))
1153 trigger_listener = listener;
1157 * Fetches the status of the trigger.
1158 * TRIG_OFF: the trigger is inactive
1159 * TRIG_RELEASED: The volume level is below the threshold
1160 * TRIG_ACTIVATED: The volume level has exceeded the threshold, but the trigger
1161 * hasn't been fired yet.
1162 * TRIG_FIRED: The volume exceeds the threshold
1164 * To activate the trigger call either peak_meter_attack_trigger or
1165 * peak_meter_release_trigger. To turn the trigger off call
1166 * peak_meter_trigger_off.
1168 int peak_meter_trigger_status(void)
1170 return trig_status; /* & TRIG_PIT_MASK;*/
1173 void peak_meter_draw_trig(int xpos[], int ypos[], int trig_width[], int nb_screens)
1175 int barstart[NB_SCREENS];
1176 int barend[NB_SCREENS];
1177 int icon;
1178 int ixpos[NB_SCREENS];
1179 int i;
1180 int trigbar_width[NB_SCREENS];
1182 FOR_NB_SCREENS(i)
1183 trigbar_width[i] = (trig_width[i] - (2 * (ICON_PLAY_STATE_WIDTH + 1)));
1185 switch (trig_status) {
1187 case TRIG_READY:
1188 FOR_NB_SCREENS(i){
1189 barstart[i] = 0;
1190 barend[i] = 0;
1192 icon = Icon_Stop;
1193 FOR_NB_SCREENS(i)
1194 ixpos[i] = xpos[i];
1195 break;
1197 case TRIG_STEADY:
1198 case TRIG_RETRIG:
1199 FOR_NB_SCREENS(i)
1201 barstart[i] = 0;
1202 barend[i] = (trig_strt_duration == 0) ? trigbar_width[i] :
1203 trigbar_width[i] *
1204 (current_tick - trig_hightime) / trig_strt_duration;
1206 icon = Icon_Stop;
1207 FOR_NB_SCREENS(i)
1208 ixpos[i] = xpos[i];
1209 break;
1211 case TRIG_GO:
1212 case TRIG_CONTINUE:
1213 FOR_NB_SCREENS(i)
1215 barstart[i] = trigbar_width[i];
1216 barend[i] = trigbar_width[i];
1218 icon = Icon_Record;
1219 FOR_NB_SCREENS(i)
1220 ixpos[i] = xpos[i]+ trig_width[i] - ICON_PLAY_STATE_WIDTH;
1221 break;
1223 case TRIG_POSTREC:
1224 FOR_NB_SCREENS(i)
1226 barstart[i] = (trig_stp_hold == 0) ? 0 :
1227 trigbar_width[i] - trigbar_width[i] *
1228 (current_tick - trig_lowtime) / trig_stp_hold;
1229 barend[i] = trigbar_width[i];
1231 icon = Icon_Record;
1232 FOR_NB_SCREENS(i)
1233 ixpos[i] = xpos[i] + trig_width[i] - ICON_PLAY_STATE_WIDTH;
1234 break;
1236 default:
1237 return;
1240 for(i = 0; i < nb_screens; i++)
1242 gui_scrollbar_draw(&screens[i], xpos[i] + ICON_PLAY_STATE_WIDTH + 1,
1243 ypos[i] + 1, trigbar_width[i], TRIG_HEIGHT - 2,
1244 trigbar_width[i], barstart[i], barend[i],
1245 HORIZONTAL);
1247 screens[i].mono_bitmap(bitmap_icons_7x8[icon], ixpos[i], ypos[i],
1248 ICON_PLAY_STATE_WIDTH, STATUSBAR_HEIGHT);
1251 #endif
1253 int peak_meter_draw_get_btn(int x, int y[], int height, int nb_screens)
1255 int button = BUTTON_NONE;
1256 long next_refresh = current_tick;
1257 long next_big_refresh = current_tick + HZ / 10;
1258 int i;
1259 #ifndef SIMULATOR
1260 bool highperf = !ata_disk_is_active();
1261 #else
1262 bool highperf = false;
1263 #endif
1264 bool dopeek = true;
1266 while (TIME_BEFORE(current_tick, next_big_refresh)) {
1267 button = get_action(CONTEXT_RECSCREEN, TIMEOUT_NOBLOCK);
1268 if (button != BUTTON_NONE) {
1269 break;
1271 if (dopeek) { /* Peek only once per refresh when disk is */
1272 peak_meter_peek(); /* spinning, but as often as possible */
1273 dopeek = highperf; /* otherwise. */
1274 yield();
1275 } else {
1276 sleep(0); /* Sleep until end of current tick. */
1278 if (TIME_AFTER(current_tick, next_refresh)) {
1279 for(i = 0; i < nb_screens; i++)
1281 peak_meter_screen(&screens[i], x, y[i], height);
1282 screens[i].update_rect(x, y[i], screens[i].width, height);
1284 next_refresh += HZ / PEAK_METER_FPS;
1285 dopeek = true;
1289 return button;
1292 #ifdef PM_DEBUG
1293 static void peak_meter_clear_histogram(void)
1295 int i = 0;
1296 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1297 ticks_per_redraw[i] = (unsigned int)0;
1300 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1301 peeks_per_redraw[i] = (unsigned int)0;
1305 bool peak_meter_histogram(void)
1307 int i;
1308 int btn = BUTTON_NONE;
1309 while ((btn & BUTTON_OFF) != BUTTON_OFF )
1311 unsigned int max = 0;
1312 int y = 0;
1313 int x = 0;
1314 screens[0].clear_display();
1316 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1317 max = MAX(max, peeks_per_redraw[i]);
1320 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1321 x = peeks_per_redraw[i] * (LCD_WIDTH - 1)/ max;
1322 screens[0].hline(0, x, y + i);
1325 y = PEEKS_PER_DRAW_SIZE + 1;
1326 max = 0;
1328 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1329 max = MAX(max, ticks_per_redraw[i]);
1332 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1333 x = ticks_per_redraw[i] * (LCD_WIDTH - 1)/ max;
1334 screens[0].hline(0, x, y + i);
1336 screens[0].update();
1338 btn = button_get(true);
1339 if (btn == BUTTON_PLAY) {
1340 peak_meter_clear_histogram();
1343 return false;
1345 #endif