Fix broken peakmeters: peakmeter was using viewport drawing code but absolute screen...
[kugel-rb.git] / apps / recorder / peakmeter.c
bloba55ce063dcf924a709917ad3bdd6242b92bd3c8e
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 #ifdef SIMULATOR
20 #include <stdlib.h> /* sim uses rand for peakmeter simulation */
21 #endif
22 #include "config.h"
23 #include "mas.h"
24 #include "thread.h"
25 #include "kernel.h"
26 #include "settings.h"
27 #include "ata.h"
28 #include "lcd.h"
29 #include "scrollbar.h"
30 #include "gwps.h"
31 #include "sprintf.h"
32 #include "button.h"
33 #include "system.h"
34 #include "font.h"
35 #include "icons.h"
36 #include "lang.h"
37 #include "peakmeter.h"
38 #include "audio.h"
39 #include "screen_access.h"
40 #ifdef HAVE_BACKLIGHT
41 #include "backlight.h"
42 #endif
43 #include "action.h"
45 #if CONFIG_CODEC == SWCODEC
46 #include "pcm.h"
48 #ifdef HAVE_RECORDING
49 #include "pcm_record.h"
50 #endif
52 static bool pm_playback = true; /* selects between playback and recording peaks */
53 #endif
55 static struct meter_scales scales[NB_SCREENS];
57 #if !defined(SIMULATOR) && CONFIG_CODEC != SWCODEC
58 /* Data source */
59 static int pm_src_left = MAS_REG_DQPEAK_L;
60 static int pm_src_right = MAS_REG_DQPEAK_R;
61 #endif
63 /* Current values and cumulation */
64 static int pm_cur_left; /* current values (last peak_meter_peek) */
65 static int pm_cur_right;
66 static int pm_max_left; /* maximum values between peak meter draws */
67 static int pm_max_right;
68 #ifdef HAVE_AGC
69 static int pm_peakhold_left; /* max. peak values between peakhold calls */
70 static int pm_peakhold_right; /* used for AGC and histogram display */
71 #endif
73 /* Clip hold */
74 static bool pm_clip_left = false; /* when true a clip has occurred */
75 static bool pm_clip_right = false;
76 static long pm_clip_timeout_l; /* clip hold timeouts */
77 static long pm_clip_timeout_r;
79 /* Temporarily en- / disables peak meter. This is especially for external
80 applications to detect if the peak_meter is in use and needs drawing at all */
81 bool peak_meter_enabled = true;
83 /** Parameters **/
84 /* Range */
85 static unsigned short peak_meter_range_min; /* minimum of range in samples */
86 static unsigned short peak_meter_range_max; /* maximum of range in samples */
87 static unsigned short pm_range; /* range width in samples */
88 static bool pm_use_dbfs = true; /* true if peakmeter displays dBfs */
89 static bool level_check; /* true if peeked at peakmeter before drawing */
90 static unsigned short pm_db_min = 0; /* minimum of range in 1/100 dB */
91 static unsigned short pm_db_max = 9000; /* maximum of range in 1/100 dB */
92 static unsigned short pm_db_range = 9000; /* range width in 1/100 dB */
93 /* Timing behaviour */
94 static int pm_peak_hold = 1; /* peak hold timeout index */
95 static int pm_peak_release = 8; /* peak release in units per read */
96 static int pm_clip_hold = 16; /* clip hold timeout index */
97 static bool pm_clip_eternal = false; /* true if clip timeout is disabled */
99 #ifdef HAVE_RECORDING
100 static unsigned short trig_strt_threshold;
101 static long trig_strt_duration;
102 static long trig_strt_dropout;
103 static unsigned short trig_stp_threshold;
104 static long trig_stp_hold;
105 static long trig_rstrt_gap;
107 /* point in time when the threshold was exceeded */
108 static long trig_hightime;
110 /* point in time when the volume fell below the threshold*/
111 static long trig_lowtime;
113 /* The output value of the trigger. See TRIG_XXX constants for valid values */
114 static int trig_status = TRIG_OFF;
116 static void (*trigger_listener)(int) = NULL;
118 /* clipping counter (only used for recording) */
119 static unsigned int pm_clipcount = 0; /* clipping count */
120 static bool pm_clipcount_active = false; /* counting or not */
121 #endif
123 /* debug only */
124 #ifdef PM_DEBUG
125 static int peek_calls = 0;
127 #define PEEKS_PER_DRAW_SIZE 40
128 static unsigned int peeks_per_redraw[PEEKS_PER_DRAW_SIZE];
130 #define TICKS_PER_DRAW_SIZE 20
131 static unsigned int ticks_per_redraw[TICKS_PER_DRAW_SIZE];
132 #endif
134 static void peak_meter_draw(struct screen *display, struct meter_scales *meter_scales,
135 int x, int y, int width, int height);
137 /* time out values for max */
138 static const short peak_time_out[] = {
139 0 * HZ, HZ / 5, 30, HZ / 2, HZ, 2 * HZ,
140 3 * HZ, 4 * HZ, 5 * HZ, 6 * HZ, 7 * HZ, 8 * HZ,
141 9 * HZ, 10 * HZ, 15 * HZ, 20 * HZ, 30 * HZ, 60 * HZ
144 /* time out values for clip */
145 static const long clip_time_out[] = {
146 0 * HZ, 1 * HZ, 2 * HZ, 3 * HZ, 4 * HZ, 5 * HZ,
147 6 * HZ, 7 * HZ, 8 * HZ, 9 * HZ, 10 * HZ, 15 * HZ,
148 20 * HZ, 25 * HZ, 30 * HZ, 45 * HZ, 60 * HZ, 90 * HZ,
149 120 * HZ, 180 * HZ, 300 * HZ, 600L * HZ, 1200L * HZ,
150 2700L * HZ, 5400L * HZ
153 /* precalculated peak values that represent magical
154 dBfs values. Used to draw the scale */
155 static const short db_scale_src_values[DB_SCALE_SRC_VALUES_SIZE] = {
156 32736, /* 0 db */
157 22752, /* - 3 db */
158 16640, /* - 6 db */
159 11648, /* - 9 db */
160 8320, /* -12 db */
161 4364, /* -18 db */
162 2064, /* -24 db */
163 1194, /* -30 db */
164 363, /* -40 db */
165 101, /* -50 db */
166 34, /* -60 db */
167 0, /* -inf */
170 static int db_scale_count = DB_SCALE_SRC_VALUES_SIZE;
173 * Calculates dB Value for the peak meter, uses peak value as input
174 * @param int sample - The input value
175 * Make sure that 0 <= value < SAMPLE_RANGE
177 * @return int - The 2 digit fixed point result of the euation
178 * 20 * log (sample / SAMPLE_RANGE) + 90
179 * Output range is 0-9000 (that is 0.0 - 90.0 dB).
180 * Normally 0dB is full scale, here it is shifted +90dB.
181 * The calculation is based on the results of a linear
182 * approximation tool written specifically for this problem
183 * by Andreas Zwirtes (radhard@gmx.de). The result has an
184 * accurracy of better than 2%. It is highly runtime optimized,
185 * the cascading if-clauses do an successive approximation on
186 * the input value. This avoids big lookup-tables and
187 * for-loops.
188 * Improved by Jvo Studer for errors < 0.2dB for critical
189 * range of -12dB to 0dB (78.0 to 90.0dB).
192 int calc_db (int isample)
194 /* return n+m*(isample-istart)/100 */
195 int n;
196 long m;
197 int istart;
199 if (isample < 2308) { /* Range 1-5 */
201 if (isample < 115) { /* Range 1-3 */
203 if (isample < 24) {
205 if (isample < 5) {
206 istart = 1; /* Range 1 */
207 n = 98;
208 m = 34950;
210 else {
211 istart = 5; /* Range 2 */
212 n = 1496;
213 m = 7168;
216 else {
217 istart = 24; /* Range 3 */
218 n = 2858;
219 m = 1498;
222 else { /* Range 4-5 */
224 if (isample < 534) {
225 istart = 114; /* Range 4 */
226 n = 4207;
227 m = 319;
229 else {
230 istart = 588; /* Range 5 */
231 n = 5583;
232 m = 69;
237 else { /* Range 6-9 */
239 if (isample < 12932) {
241 if (isample < 6394) {
242 istart = 2608; /* Range 6 */
243 n = 6832;
244 m = 21;
246 else {
247 istart = 7000; /* Range 7 */
248 n = 7682;
249 m = 9;
252 else {
254 if (isample < 22450) {
255 istart = 13000; /* Range 8 */
256 n = 8219;
257 m = 5;
259 else {
260 istart = 22636; /* Range 9 */
261 n = 8697;
262 m = 3;
267 return n + (m * (long)(isample - istart)) / 100L;
272 * A helper function for peak_meter_db2sample. Don't call it separately but
273 * use peak_meter_db2sample. If one or both of min and max are outside the
274 * range 0 <= min (or max) < 8961 the behaviour of this function is
275 * undefined. It may not return.
276 * @param int min - The minimum of the value range that is searched.
277 * @param int max - The maximum of the value range that is searched.
278 * @param int db - The value in dBfs * (-100) for which the according
279 * minimal peak sample is searched.
280 * @return int - A linear volume value with 0 <= value < MAX_PEAK
282 static int db_to_sample_bin_search(int min, int max, int db)
284 int test = min + (max - min) / 2;
286 if (min < max) {
287 if (calc_db(test) < db) {
288 test = db_to_sample_bin_search(test, max, db);
289 } else {
290 if (calc_db(test-1) > db) {
291 test = db_to_sample_bin_search(min, test, db);
295 return test;
299 * Converts a value representing dBfs to a linear
300 * scaled volume info as it is used by the MAS.
301 * An incredibly inefficiant function which is
302 * the vague inverse of calc_db. This really
303 * should be replaced by something better soon.
305 * @param int db - A dBfs * 100 value with
306 * -9000 < value <= 0
307 * @return int - The return value is in the range of
308 * 0 <= return value < MAX_PEAK
310 int peak_meter_db2sample(int db)
312 int retval = 0;
314 /* what is the maximum pseudo db value */
315 int max_peak_db = calc_db(MAX_PEAK - 1);
317 /* range check: db value to big */
318 if (max_peak_db + db < 0) {
319 retval = 0;
322 /* range check: db value too small */
323 else if (max_peak_db + db >= max_peak_db) {
324 retval = MAX_PEAK -1;
327 /* value in range: find the matching linear value */
328 else {
329 retval = db_to_sample_bin_search(0, MAX_PEAK, max_peak_db + db);
331 /* as this is a dirty function anyway, we want to adjust the
332 full scale hit manually to avoid users complaining that when
333 they adjust maximum for 0 dBfs and display it in percent it
334 shows 99%. That is due to precision loss and this is the
335 optical fix */
338 return retval;
342 * Set the min value for restriction of the value range.
343 * @param int newmin - depending whether dBfs is used
344 * newmin is a value in dBfs * 100 or in linear percent values.
345 * for dBfs: -9000 < newmin <= 0
346 * for linear: 0 <= newmin <= 100
348 static void peak_meter_set_min(int newmin)
350 if (pm_use_dbfs) {
351 peak_meter_range_min = peak_meter_db2sample(newmin);
353 } else {
354 if (newmin < peak_meter_range_max) {
355 peak_meter_range_min = newmin * MAX_PEAK / 100;
359 pm_range = peak_meter_range_max - peak_meter_range_min;
361 /* Avoid division by zero. */
362 if (pm_range == 0) {
363 pm_range = 1;
366 pm_db_min = calc_db(peak_meter_range_min);
367 pm_db_range = pm_db_max - pm_db_min;
368 int i;
369 FOR_NB_SCREENS(i)
370 scales[i].db_scale_valid = false;
374 * Returns the minimum value of the range the meter
375 * displays. If the scale is set to dBfs it returns
376 * dBfs values * 100 or linear percent values.
377 * @return: using dBfs : -9000 < value <= 0
378 * using linear scale: 0 <= value <= 100
380 int peak_meter_get_min(void)
382 int retval = 0;
383 if (pm_use_dbfs) {
384 retval = calc_db(peak_meter_range_min) - calc_db(MAX_PEAK - 1);
385 } else {
386 retval = peak_meter_range_min * 100 / MAX_PEAK;
388 return retval;
392 * Set the max value for restriction of the value range.
393 * @param int newmax - depending wether dBfs is used
394 * newmax is a value in dBfs * 100 or in linear percent values.
395 * for dBfs: -9000 < newmax <= 0
396 * for linear: 0 <= newmax <= 100
398 static void peak_meter_set_max(int newmax)
400 if (pm_use_dbfs) {
401 peak_meter_range_max = peak_meter_db2sample(newmax);
402 } else {
403 if (newmax > peak_meter_range_min) {
404 peak_meter_range_max = newmax * MAX_PEAK / 100;
408 pm_range = peak_meter_range_max - peak_meter_range_min;
410 /* Avoid division by zero. */
411 if (pm_range == 0) {
412 pm_range = 1;
415 pm_db_max = calc_db(peak_meter_range_max);
416 pm_db_range = pm_db_max - pm_db_min;
417 int i;
418 FOR_NB_SCREENS(i)
419 scales[i].db_scale_valid = false;
423 * Returns the minimum value of the range the meter
424 * displays. If the scale is set to dBfs it returns
425 * dBfs values * 100 or linear percent values
426 * @return: using dBfs : -9000 < value <= 0
427 * using linear scale: 0 <= value <= 100
429 int peak_meter_get_max(void)
431 int retval = 0;
432 if (pm_use_dbfs) {
433 retval = calc_db(peak_meter_range_max) - calc_db(MAX_PEAK - 1);
434 } else {
435 retval = peak_meter_range_max * 100 / MAX_PEAK;
437 return retval;
441 * Returns whether the meter is currently displaying dBfs or percent values.
442 * @return bool - true if the meter is displaying dBfs
443 false if the meter is displaying percent values.
445 bool peak_meter_get_use_dbfs(void)
447 return pm_use_dbfs;
451 * Specifies whether the values displayed are scaled
452 * as dBfs or as linear percent values.
453 * @param use - set to true for dBfs,
454 * set to false for linear scaling in percent
456 void peak_meter_set_use_dbfs(bool use)
458 int i;
459 pm_use_dbfs = use;
460 FOR_NB_SCREENS(i)
461 scales[i].db_scale_valid = false;
465 * Initialize the range of the meter. Only values
466 * that are in the range of [range_min ... range_max]
467 * are displayed.
468 * @param bool dbfs - set to true for dBfs,
469 * set to false for linear scaling in percent
470 * @param int range_min - Specifies the lower value of the range.
471 * Pass a value dBfs * 100 when dbfs is set to true.
472 * Pass a percent value when dbfs is set to false.
473 * @param int range_max - Specifies the upper value of the range.
474 * Pass a value dBfs * 100 when dbfs is set to true.
475 * Pass a percent value when dbfs is set to false.
477 void peak_meter_init_range( bool dbfs, int range_min, int range_max)
479 pm_use_dbfs = dbfs;
480 peak_meter_set_min(range_min);
481 peak_meter_set_max(range_max);
485 * Initialize the peak meter with all relevant values concerning times.
486 * @param int release - Set the maximum amount of pixels the meter is allowed
487 * to decrease with each redraw
488 * @param int hold - Select the time preset for the time the peak indicator
489 * is reset after a peak occurred. The preset values are
490 * stored in peak_time_out.
491 * @param int clip_hold - Select the time preset for the time the peak
492 * indicator is reset after a peak occurred. The preset
493 * values are stored in clip_time_out.
495 void peak_meter_init_times(int release, int hold, int clip_hold)
497 pm_peak_hold = hold;
498 pm_peak_release = release;
499 pm_clip_hold = clip_hold;
502 #ifdef HAVE_RECORDING
504 * Enable/disable clip counting
506 void pm_activate_clipcount(bool active)
508 pm_clipcount_active = active;
512 * Get clipping counter value
514 int pm_get_clipcount(void)
516 return pm_clipcount;
520 * Set clipping counter to zero (typically at start of recording or playback)
522 void pm_reset_clipcount(void)
524 pm_clipcount = 0;
526 #endif
529 * Set the source of the peak meter to playback or to
530 * record.
531 * @param: bool playback - If true playback peak meter is used.
532 * If false recording peak meter is used.
534 void peak_meter_playback(bool playback)
536 #ifdef SIMULATOR
537 (void)playback;
538 #elif CONFIG_CODEC == SWCODEC
539 pm_playback = playback;
540 #else
541 if (playback) {
542 pm_src_left = MAS_REG_DQPEAK_L;
543 pm_src_right = MAS_REG_DQPEAK_R;
544 } else {
545 pm_src_left = MAS_REG_QPEAK_L;
546 pm_src_right = MAS_REG_QPEAK_R;
548 #endif
551 #ifdef HAVE_RECORDING
552 static void set_trig_status(int new_state)
554 if (trig_status != new_state) {
555 trig_status = new_state;
556 if (trigger_listener != NULL) {
557 trigger_listener(trig_status);
562 #endif
565 * Reads peak values from the MAS, and detects clips. The
566 * values are stored in pm_max_left pm_max_right for later
567 * evauluation. Consecutive calls to peak_meter_peek detect
568 * that ocurred. This function could be used by a thread for
569 * busy reading the MAS.
571 void peak_meter_peek(void)
573 int left, right;
574 #ifdef HAVE_RECORDING
575 bool was_clipping = pm_clip_left || pm_clip_right;
576 #endif
577 /* read current values */
578 #if CONFIG_CODEC == SWCODEC
579 if (pm_playback)
580 pcm_calculate_peaks(&pm_cur_left, &pm_cur_right);
581 #ifdef HAVE_RECORDING
582 else
583 pcm_calculate_rec_peaks(&pm_cur_left, &pm_cur_right);
584 #endif
585 left = pm_cur_left;
586 right = pm_cur_right;
587 #else
588 #ifndef SIMULATOR
589 pm_cur_left = left = mas_codec_readreg(pm_src_left);
590 pm_cur_right = right = mas_codec_readreg(pm_src_right);
591 #else
592 pm_cur_left = left = 8000;
593 pm_cur_right = right = 9000;
594 #endif
595 #endif
597 /* check for clips
598 An clip is assumed when two consecutive readouts
599 of the volume are at full scale. This is proven
600 to be inaccurate in both ways: it may detect clips
601 when no clip occurred and it may fail to detect
602 a real clip. For software codecs, the peak is already
603 the max of a bunch of samples, so use one max value
604 or you fail to detect clipping! */
605 #if CONFIG_CODEC == SWCODEC
606 if (left == MAX_PEAK - 1) {
607 #else
608 if ((left == pm_max_left) &&
609 (left == MAX_PEAK - 1)) {
610 #endif
611 pm_clip_left = true;
612 pm_clip_timeout_l =
613 current_tick + clip_time_out[pm_clip_hold];
616 #if CONFIG_CODEC == SWCODEC
617 if (right == MAX_PEAK - 1) {
618 #else
619 if ((right == pm_max_right) &&
620 (right == MAX_PEAK - 1)) {
621 #endif
622 pm_clip_right = true;
623 pm_clip_timeout_r =
624 current_tick + clip_time_out[pm_clip_hold];
627 #ifdef HAVE_RECORDING
628 if(!was_clipping && (pm_clip_left || pm_clip_right))
630 if(pm_clipcount_active)
631 pm_clipcount++;
633 #endif
635 /* peaks are searched -> we have to find the maximum. When
636 many calls of peak_meter_peek the maximum value will be
637 stored in pm_max_xxx. This maximum is reset by the
638 functions peak_meter_read_x. */
639 pm_max_left = MAX(pm_max_left, left);
640 pm_max_right = MAX(pm_max_right, right);
642 #ifdef HAVE_RECORDING
643 #if CONFIG_CODEC == SWCODEC
644 /* Ignore any unread peakmeter data */
645 #define MAX_DROP_TIME HZ/7 /* this value may need tweaking. Increase if you are
646 getting trig events when you shouldn't with
647 trig_stp_hold = 0 */
648 if (!trig_stp_hold)
649 trig_stp_hold = MAX_DROP_TIME;
650 #endif
652 switch (trig_status) {
653 case TRIG_READY:
654 /* no more changes, if trigger was activated as release trigger */
655 /* threshold exceeded? */
656 if ((left > trig_strt_threshold)
657 || (right > trig_strt_threshold)) {
658 /* reset trigger duration */
659 trig_hightime = current_tick;
661 /* reset dropout duration */
662 trig_lowtime = current_tick;
664 if (trig_strt_duration)
665 set_trig_status(TRIG_STEADY);
666 else
667 /* if trig_duration is set to 0 the user wants to start
668 recording immediately */
669 set_trig_status(TRIG_GO);
671 break;
673 case TRIG_STEADY:
674 case TRIG_RETRIG:
675 /* trigger duration exceeded */
676 if (current_tick - trig_hightime > trig_strt_duration) {
677 set_trig_status(TRIG_GO);
678 } else {
679 /* threshold exceeded? */
680 if ((left > trig_strt_threshold)
681 || (right > trig_strt_threshold)) {
682 /* reset lowtime */
683 trig_lowtime = current_tick;
685 /* volume is below threshold */
686 else {
687 /* dropout occurred? */
688 if (current_tick - trig_lowtime > trig_strt_dropout){
689 if (trig_status == TRIG_STEADY){
690 set_trig_status(TRIG_READY);
692 /* trig_status == TRIG_RETRIG */
693 else {
694 /* the gap has already expired */
695 trig_lowtime = current_tick - trig_rstrt_gap - 1;
696 set_trig_status(TRIG_POSTREC);
701 break;
703 case TRIG_GO:
704 case TRIG_CONTINUE:
705 /* threshold exceeded? */
706 if ((left > trig_stp_threshold)
707 || (right > trig_stp_threshold)) {
708 /* restart hold time countdown */
709 trig_lowtime = current_tick;
710 #if CONFIG_CODEC == SWCODEC
711 } else if (current_tick - trig_lowtime > MAX_DROP_TIME){
712 #else
713 } else {
714 #endif
715 set_trig_status(TRIG_POSTREC);
716 trig_hightime = current_tick;
718 break;
720 case TRIG_POSTREC:
721 /* gap time expired? */
722 if (current_tick - trig_lowtime > trig_rstrt_gap){
723 /* start threshold exceeded? */
724 if ((left > trig_strt_threshold)
725 || (right > trig_strt_threshold)) {
727 set_trig_status(TRIG_RETRIG);
728 trig_hightime = current_tick;
729 trig_lowtime = current_tick;
731 else
733 /* stop threshold exceeded */
734 if ((left > trig_stp_threshold)
735 || (right > trig_stp_threshold)) {
736 if (current_tick - trig_hightime > trig_stp_hold){
737 trig_lowtime = current_tick;
738 set_trig_status(TRIG_CONTINUE);
739 } else {
740 trig_lowtime = current_tick - trig_rstrt_gap - 1;
744 /* below any threshold */
745 else {
746 if (current_tick - trig_lowtime > trig_stp_hold){
747 set_trig_status(TRIG_READY);
748 } else {
749 trig_hightime = current_tick;
754 /* still within the gap time */
755 else {
756 /* stop threshold exceeded */
757 if ((left > trig_stp_threshold)
758 || (right > trig_stp_threshold)) {
759 set_trig_status(TRIG_CONTINUE);
760 trig_lowtime = current_tick;
763 /* hold time expired */
764 else if (current_tick - trig_lowtime > trig_stp_hold){
765 trig_hightime = current_tick;
766 trig_lowtime = current_tick;
767 set_trig_status(TRIG_READY);
770 break;
772 #if CONFIG_CODEC == SWCODEC
773 /* restore stop hold value */
774 if (trig_stp_hold == MAX_DROP_TIME)
775 trig_stp_hold = 0;
776 #endif
777 #endif
778 /* check levels next time peakmeter drawn */
779 level_check = true;
780 #ifdef PM_DEBUG
781 peek_calls++;
782 #endif
786 * Reads out the peak volume of the left channel.
787 * @return int - The maximum value that has been detected
788 * since the last call of peak_meter_read_l. The value
789 * is in the range 0 <= value < MAX_PEAK.
791 static int peak_meter_read_l(void)
793 /* pm_max_left contains the maximum of all peak values that were read
794 by peak_meter_peek since the last call of peak_meter_read_l */
795 int retval = pm_max_left;
797 #ifdef HAVE_AGC
798 /* store max peak value for peak_meter_get_peakhold_x readout */
799 pm_peakhold_left = MAX(pm_max_left, pm_peakhold_left);
800 #endif
801 #ifdef PM_DEBUG
802 peek_calls = 0;
803 #endif
804 /* reset pm_max_left so that subsequent calls of peak_meter_peek don't
805 get fooled by an old maximum value */
806 pm_max_left = pm_cur_left;
808 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
809 srand(current_tick);
810 retval = rand()%MAX_PEAK;
811 #endif
813 return retval;
817 * Reads out the peak volume of the right channel.
818 * @return int - The maximum value that has been detected
819 * since the last call of peak_meter_read_l. The value
820 * is in the range 0 <= value < MAX_PEAK.
822 static int peak_meter_read_r(void)
824 /* peak_meter_r contains the maximum of all peak values that were read
825 by peak_meter_peek since the last call of peak_meter_read_r */
826 int retval = pm_max_right;
828 #ifdef HAVE_AGC
829 /* store max peak value for peak_meter_get_peakhold_x readout */
830 pm_peakhold_right = MAX(pm_max_right, pm_peakhold_right);
831 #endif
832 #ifdef PM_DEBUG
833 peek_calls = 0;
834 #endif
835 /* reset pm_max_right so that subsequent calls of peak_meter_peek don't
836 get fooled by an old maximum value */
837 pm_max_right = pm_cur_right;
839 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
840 srand(current_tick);
841 retval = rand()%MAX_PEAK;
842 #endif
844 return retval;
847 #ifdef HAVE_AGC
849 * Reads out the current peak-hold values since the last call.
850 * This is used by the histogram feature in the recording screen.
851 * Values are in the range 0 <= peak_x < MAX_PEAK. MAX_PEAK is typ 32767.
853 void peak_meter_get_peakhold(int *peak_left, int *peak_right)
855 if (peak_left)
856 *peak_left = pm_peakhold_left;
857 if (peak_right)
858 *peak_right = pm_peakhold_right;
859 pm_peakhold_left = 0;
860 pm_peakhold_right = 0;
862 #endif
865 * Reset the detected clips. This method is for
866 * use by the user interface.
867 * @param int unused - This parameter was added to
868 * make the function compatible with set_int
870 void peak_meter_set_clip_hold(int time)
872 pm_clip_left = false;
873 pm_clip_right = false;
874 pm_clip_eternal = (time > 0) ? false : true;
878 * Scales a peak value as read from the MAS to the range of meterwidth.
879 * The scaling is performed according to the scaling method (dBfs / linear)
880 * and the range (peak_meter_range_min .. peak_meter_range_max).
881 * @param unsigned short val - The volume value. Range: 0 <= val < MAX_PEAK
882 * @param int meterwidht - The widht of the meter in pixel
883 * @return unsigned short - A value 0 <= return value <= meterwidth
885 unsigned short peak_meter_scale_value(unsigned short val, int meterwidth)
887 int retval;
889 if (val <= peak_meter_range_min) {
890 return 0;
893 if (val >= peak_meter_range_max) {
894 return meterwidth;
897 retval = val;
899 /* different scaling is used for dBfs and linear percent */
900 if (pm_use_dbfs) {
902 /* scale the samples dBfs */
903 retval = (calc_db(retval) - pm_db_min) * meterwidth / pm_db_range;
906 /* Scale for linear percent display */
907 else
909 /* scale the samples */
910 retval = ((retval - peak_meter_range_min) * meterwidth)
911 / pm_range;
913 return retval;
915 void peak_meter_screen(struct screen *display, int x, int y, int height)
917 peak_meter_draw(display, &scales[display->screen_type], x, y,
918 display->getwidth() - x, height);
921 * Draws a peak meter in the specified size at the specified position.
922 * @param int x - The x coordinate.
923 * Make sure that 0 <= x and x + width < display->getwidth()
924 * @param int y - The y coordinate.
925 * Make sure that 0 <= y and y + height < display->getheight()
926 * @param int width - The width of the peak meter. Note that for display
927 * of clips a 3 pixel wide area is used ->
928 * width > 3
929 * @param int height - The height of the peak meter. height > 3
931 static void peak_meter_draw(struct screen *display, struct meter_scales *scales,
932 int x, int y, int width, int height)
934 static int left_level = 0, right_level = 0;
935 int left = 0, right = 0;
936 int meterwidth = width - 3;
937 int i, delta;
938 #if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
939 static long peak_release_tick[2] = {0,0};
940 int screen_nr = display->screen_type == SCREEN_MAIN ? 0 : 1;
941 #else
942 static long peak_release_tick = 0;
943 #endif
945 #ifdef PM_DEBUG
946 static long pm_tick = 0;
947 int tmp = peek_calls;
948 #endif
950 /* if disabled only draw the peak meter */
951 if (peak_meter_enabled) {
954 if (level_check){
955 /* only read the volume info from MAS if peek since last read*/
956 left_level = peak_meter_read_l();
957 right_level = peak_meter_read_r();
958 level_check = false;
961 /* scale the samples dBfs */
962 left = peak_meter_scale_value(left_level, meterwidth);
963 right = peak_meter_scale_value(right_level, meterwidth);
965 /*if the scale has changed -> recalculate the scale
966 (The scale becomes invalid when the range changed.) */
967 if (!scales->db_scale_valid){
969 if (pm_use_dbfs) {
970 db_scale_count = DB_SCALE_SRC_VALUES_SIZE;
971 for (i = 0; i < db_scale_count; i++){
972 /* find the real x-coords for predefined interesting
973 dBfs values. These only are recalculated when the
974 scaling of the meter changed. */
975 scales->db_scale_lcd_coord[i] =
976 peak_meter_scale_value(
977 db_scale_src_values[i],
978 meterwidth - 1);
982 /* when scaling linear we simly make 10% steps */
983 else {
984 db_scale_count = 10;
985 for (i = 0; i < db_scale_count; i++) {
986 scales->db_scale_lcd_coord[i] =
987 (i * (MAX_PEAK / 10) - peak_meter_range_min) *
988 meterwidth / pm_range;
992 /* mark scale valid to avoid recalculating dBfs values
993 of the scale. */
994 scales->db_scale_valid = true;
997 /* apply release */
998 #if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
999 delta = current_tick - peak_release_tick[screen_nr];
1000 peak_release_tick[screen_nr] = current_tick;
1001 #else
1002 delta = current_tick - peak_release_tick;
1003 peak_release_tick = current_tick;
1004 #endif
1005 left = MAX(left , scales->last_left - delta * pm_peak_release);
1006 right = MAX(right, scales->last_right - delta * pm_peak_release);
1008 /* reset max values after timeout */
1009 if (TIME_AFTER(current_tick, scales->pm_peak_timeout_l)){
1010 scales->pm_peak_left = 0;
1013 if (TIME_AFTER(current_tick, scales->pm_peak_timeout_r)){
1014 scales->pm_peak_right = 0;
1017 if (!pm_clip_eternal) {
1018 if (pm_clip_left &&
1019 TIME_AFTER(current_tick, pm_clip_timeout_l)){
1020 pm_clip_left = false;
1023 if (pm_clip_right &&
1024 TIME_AFTER(current_tick, pm_clip_timeout_r)){
1025 pm_clip_right = false;
1029 /* check for new max values */
1030 if (left > scales->pm_peak_left) {
1031 scales->pm_peak_left = left - 1;
1032 scales->pm_peak_timeout_l =
1033 current_tick + peak_time_out[pm_peak_hold];
1036 if (right > scales->pm_peak_right) {
1037 scales->pm_peak_right = right - 1;
1038 scales->pm_peak_timeout_r =
1039 current_tick + peak_time_out[pm_peak_hold];
1043 /* draw the peak meter */
1044 display->set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1045 display->fillrect(x, y, width, height);
1046 display->set_drawmode(DRMODE_SOLID);
1048 /* draw left */
1049 display->fillrect (x, y, left, height / 2 - 2 );
1050 if (scales->pm_peak_left > 0) {
1051 display->vline(x + scales->pm_peak_left, y, y + height / 2 - 2 );
1053 if (pm_clip_left) {
1054 display->fillrect(x + meterwidth, y, 3, height / 2 - 1);
1057 /* draw right */
1058 display->fillrect(x, y + height / 2 + 1, right, height / 2 - 2);
1059 if (scales->pm_peak_right > 0) {
1060 display->vline( x + scales->pm_peak_right, y + height / 2, y + height - 2);
1062 if (pm_clip_right) {
1063 display->fillrect(x + meterwidth, y + height / 2, 3, height / 2 - 1);
1066 /* draw scale end */
1067 display->vline(x + meterwidth, y, y + height - 2);
1069 /* draw dots for scale marks */
1070 for (i = 0; i < db_scale_count; i++) {
1071 /* The x-coordinates of interesting scale mark points
1072 have been calculated before */
1073 display->drawpixel(x + scales->db_scale_lcd_coord[i],
1074 y + height / 2 - 1);
1077 #ifdef HAVE_RECORDING
1079 #ifdef HAVE_BACKLIGHT
1080 /* cliplight */
1081 if ((pm_clip_left || pm_clip_right) &&
1082 global_settings.cliplight &&
1083 #if CONFIG_CODEC == SWCODEC
1084 !pm_playback)
1085 #else
1086 !(audio_status() & (AUDIO_STATUS_PLAY | AUDIO_STATUS_ERROR)))
1087 #endif
1089 /* if clipping, cliplight setting on and in recording screen */
1090 if (global_settings.cliplight <= 2)
1092 /* turn on main unit light if setting set to main or both*/
1093 backlight_on();
1095 #ifdef HAVE_REMOTE_LCD
1096 if (global_settings.cliplight >= 2)
1098 /* turn remote light unit on if setting set to remote or both */
1099 remote_backlight_on();
1101 #endif /* HAVE_REMOTE_LCD */
1103 #endif /* HAVE_BACKLIGHT */
1105 if (trig_status != TRIG_OFF) {
1106 int start_trigx, stop_trigx, ycenter;
1108 display->set_drawmode(DRMODE_SOLID);
1109 ycenter = y + height / 2;
1110 /* display threshold value */
1111 start_trigx = x+peak_meter_scale_value(trig_strt_threshold,meterwidth);
1112 display->vline(start_trigx, ycenter - 2, ycenter);
1113 start_trigx ++;
1114 if (start_trigx < display->getwidth() ) display->drawpixel(start_trigx, ycenter - 1);
1116 stop_trigx = x + peak_meter_scale_value(trig_stp_threshold,meterwidth);
1117 display->vline(stop_trigx, ycenter - 2, ycenter);
1118 if (stop_trigx > 0) display->drawpixel(stop_trigx - 1, ycenter - 1);
1120 #endif /*HAVE_RECORDING*/
1122 #ifdef PM_DEBUG
1123 /* display a bar to show how many calls to peak_meter_peek
1124 have ocurred since the last display */
1125 display->set_drawmode(DRMODE_COMPLEMENT);
1126 display->fillrect(x, y, tmp, 3);
1128 if (tmp < PEEKS_PER_DRAW_SIZE) {
1129 peeks_per_redraw[tmp]++;
1132 tmp = current_tick - pm_tick;
1133 if (tmp < TICKS_PER_DRAW_SIZE ){
1134 ticks_per_redraw[tmp] ++;
1137 /* display a bar to show how many ticks have passed since
1138 the last redraw */
1139 display->fillrect(x, y + height / 2, current_tick - pm_tick, 2);
1140 pm_tick = current_tick;
1141 #endif
1143 scales->last_left = left;
1144 scales->last_right = right;
1146 display->set_drawmode(DRMODE_SOLID);
1149 #ifdef HAVE_RECORDING
1151 * Defines the parameters of the trigger. After these parameters are defined
1152 * the trigger can be started either by peak_meter_attack_trigger or by
1153 * peak_meter_release_trigger. Note that you can pass either linear (%) or
1154 * logarithmic (db) values to the thresholds. Positive values are intepreted as
1155 * percent (0 is 0% .. 100 is 100%). Negative values are interpreted as db.
1156 * To avoid ambiguosity of the value 0 the negative values are shifted by -1.
1157 * Thus -75 is -74db .. -1 is 0db.
1158 * @param start_threshold - The threshold used for attack trigger. Negative
1159 * values are interpreted as db -1, positive as %.
1160 * @param start_duration - The minimum time span within which start_threshold
1161 * must be exceeded to fire the attack trigger.
1162 * @param start_dropout - The maximum time span the level may fall below
1163 * start_threshold without releasing the attack trigger.
1164 * @param stop_threshold - The threshold the volume must fall below to release
1165 * the release trigger.Negative values are
1166 * interpreted as db -1, positive as %.
1167 * @param stop_hold - The minimum time the volume must fall below the
1168 * stop_threshold to release the trigger.
1169 * @param
1171 void peak_meter_define_trigger(
1172 int start_threshold,
1173 long start_duration,
1174 long start_dropout,
1175 int stop_threshold,
1176 long stop_hold_time,
1177 long restart_gap
1180 if (start_threshold < 0) {
1181 /* db */
1182 if (start_threshold < -89) {
1183 trig_strt_threshold = 0;
1184 } else {
1185 trig_strt_threshold =peak_meter_db2sample((start_threshold+1)*100);
1187 } else {
1188 /* linear percent */
1189 trig_strt_threshold = start_threshold * MAX_PEAK / 100;
1191 trig_strt_duration = start_duration;
1192 trig_strt_dropout = start_dropout;
1193 if (stop_threshold < 0) {
1194 /* db */
1195 trig_stp_threshold = peak_meter_db2sample((stop_threshold + 1) * 100);
1196 } else {
1197 /* linear percent */
1198 trig_stp_threshold = stop_threshold * MAX_PEAK / 100;
1200 trig_stp_hold = stop_hold_time;
1201 trig_rstrt_gap = restart_gap;
1205 * Enables or disables the trigger.
1206 * @param on - If true the trigger is turned on.
1208 void peak_meter_trigger(bool on)
1210 /* don't use set_trigger here as that would fire an undesired event */
1211 trig_status = on ? TRIG_READY : TRIG_OFF;
1215 * Registers the listener function that listenes on trig_status changes.
1216 * @param listener - The function that is called with each change of
1217 * trig_status. May be set to NULL if no callback is desired.
1219 void peak_meter_set_trigger_listener(void (*listener)(int status))
1221 trigger_listener = listener;
1225 * Fetches the status of the trigger.
1226 * TRIG_OFF: the trigger is inactive
1227 * TRIG_RELEASED: The volume level is below the threshold
1228 * TRIG_ACTIVATED: The volume level has exceeded the threshold, but the trigger
1229 * hasn't been fired yet.
1230 * TRIG_FIRED: The volume exceeds the threshold
1232 * To activate the trigger call either peak_meter_attack_trigger or
1233 * peak_meter_release_trigger. To turn the trigger off call
1234 * peak_meter_trigger_off.
1236 int peak_meter_trigger_status(void)
1238 return trig_status; /* & TRIG_PIT_MASK;*/
1241 void peak_meter_draw_trig(int xpos[], int ypos[], int trig_width[], int nb_screens)
1243 int barstart[NB_SCREENS];
1244 int barend[NB_SCREENS];
1245 int icon;
1246 int ixpos[NB_SCREENS];
1247 int i;
1248 int trigbar_width[NB_SCREENS];
1250 FOR_NB_SCREENS(i)
1251 trigbar_width[i] = (trig_width[i] - (2 * (ICON_PLAY_STATE_WIDTH + 1)));
1253 switch (trig_status) {
1255 case TRIG_READY:
1256 FOR_NB_SCREENS(i){
1257 barstart[i] = 0;
1258 barend[i] = 0;
1260 icon = Icon_Stop;
1261 FOR_NB_SCREENS(i)
1262 ixpos[i] = xpos[i];
1263 break;
1265 case TRIG_STEADY:
1266 case TRIG_RETRIG:
1267 FOR_NB_SCREENS(i)
1269 barstart[i] = 0;
1270 barend[i] = (trig_strt_duration == 0) ? trigbar_width[i] :
1271 trigbar_width[i] *
1272 (current_tick - trig_hightime) / trig_strt_duration;
1274 icon = Icon_Stop;
1275 FOR_NB_SCREENS(i)
1276 ixpos[i] = xpos[i];
1277 break;
1279 case TRIG_GO:
1280 case TRIG_CONTINUE:
1281 FOR_NB_SCREENS(i)
1283 barstart[i] = trigbar_width[i];
1284 barend[i] = trigbar_width[i];
1286 icon = Icon_Record;
1287 FOR_NB_SCREENS(i)
1288 ixpos[i] = xpos[i]+ trig_width[i] - ICON_PLAY_STATE_WIDTH;
1289 break;
1291 case TRIG_POSTREC:
1292 FOR_NB_SCREENS(i)
1294 barstart[i] = (trig_stp_hold == 0) ? 0 :
1295 trigbar_width[i] - trigbar_width[i] *
1296 (current_tick - trig_lowtime) / trig_stp_hold;
1297 barend[i] = trigbar_width[i];
1299 icon = Icon_Record;
1300 FOR_NB_SCREENS(i)
1301 ixpos[i] = xpos[i] + trig_width[i] - ICON_PLAY_STATE_WIDTH;
1302 break;
1304 default:
1305 return;
1308 for(i = 0; i < nb_screens; i++)
1310 gui_scrollbar_draw(&screens[i], xpos[i] + ICON_PLAY_STATE_WIDTH + 1,
1311 ypos[i] + 1, trigbar_width[i], TRIG_HEIGHT - 2,
1312 trigbar_width[i], barstart[i], barend[i],
1313 HORIZONTAL);
1315 screens[i].mono_bitmap(bitmap_icons_7x8[icon], ixpos[i], ypos[i],
1316 ICON_PLAY_STATE_WIDTH, STATUSBAR_HEIGHT);
1319 #endif
1321 int peak_meter_draw_get_btn(int action_context, int x, int y[],
1322 int height, int nb_screens)
1324 int button = BUTTON_NONE;
1325 long next_refresh = current_tick;
1326 long next_big_refresh = current_tick + HZ / 10;
1327 int i;
1328 #if (CONFIG_CODEC == SWCODEC) || defined(SIMULATOR)
1329 bool highperf = false;
1330 #else
1331 /* On MAS targets, we need to poll as often as possible in order to not
1332 * miss a peak, as the MAS does only provide a quasi-peak. When the disk
1333 * is active, it must not draw too much CPU power or a buffer overrun can
1334 * happen when saving a recording. As a compromise, poll only once per tick
1335 * when the disk is active, otherwise spin around as fast as possible. */
1336 bool highperf = !ata_disk_is_active();
1337 #endif
1338 bool dopeek = true;
1340 while (TIME_BEFORE(current_tick, next_big_refresh)) {
1341 button = get_action(action_context, TIMEOUT_NOBLOCK);
1342 if (button != BUTTON_NONE) {
1343 break;
1345 if (dopeek) { /* Peek only once per refresh when disk is */
1346 peak_meter_peek(); /* spinning, but as often as possible */
1347 dopeek = highperf; /* otherwise. */
1348 yield();
1349 } else {
1350 sleep(0); /* Sleep until end of current tick. */
1352 if (TIME_AFTER(current_tick, next_refresh)) {
1353 for(i = 0; i < nb_screens; i++)
1355 peak_meter_screen(&screens[i], x, y[i], height);
1356 screens[i].update_viewport_rect(x, y[i],
1357 screens[i].width - x, height);
1359 next_refresh += HZ / PEAK_METER_FPS;
1360 dopeek = true;
1364 return button;
1367 #ifdef PM_DEBUG
1368 static void peak_meter_clear_histogram(void)
1370 int i = 0;
1371 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1372 ticks_per_redraw[i] = (unsigned int)0;
1375 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1376 peeks_per_redraw[i] = (unsigned int)0;
1380 bool peak_meter_histogram(void)
1382 int i;
1383 int btn = BUTTON_NONE;
1384 while ((btn & BUTTON_OFF) != BUTTON_OFF )
1386 unsigned int max = 0;
1387 int y = 0;
1388 int x = 0;
1389 screens[0].clear_display();
1391 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1392 max = MAX(max, peeks_per_redraw[i]);
1395 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1396 x = peeks_per_redraw[i] * (LCD_WIDTH - 1)/ max;
1397 screens[0].hline(0, x, y + i);
1400 y = PEEKS_PER_DRAW_SIZE + 1;
1401 max = 0;
1403 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1404 max = MAX(max, ticks_per_redraw[i]);
1407 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1408 x = ticks_per_redraw[i] * (LCD_WIDTH - 1)/ max;
1409 screens[0].hline(0, x, y + i);
1411 screens[0].update();
1413 btn = button_get(true);
1414 if (btn == BUTTON_PLAY) {
1415 peak_meter_clear_histogram();
1418 return false;
1420 #endif