Update the discussion of themeing in the manual, and put a note in the wps tags appen...
[kugel-rb.git] / apps / recorder / peakmeter.c
blob164b14b15ce58d13e384f1ab7b60308980e677bd
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 "storage.h"
30 #include "lcd.h"
31 #include "scrollbar.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 #if defined(HAVE_AGC) || defined(HAVE_RECORDING_HISTOGRAM)
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 static bool peak_meter_enabled = true;
82 void peak_meter_enable(bool enable)
84 peak_meter_enabled = enable;
87 /** Parameters **/
88 /* Range */
89 static unsigned short peak_meter_range_min; /* minimum of range in samples */
90 static unsigned short peak_meter_range_max; /* maximum of range in samples */
91 static unsigned short pm_range; /* range width in samples */
92 static bool pm_use_dbfs = true; /* true if peakmeter displays dBfs */
93 static bool level_check; /* true if peeked at peakmeter before drawing */
94 static unsigned short pm_db_min = 0; /* minimum of range in 1/100 dB */
95 static unsigned short pm_db_max = 9000; /* maximum of range in 1/100 dB */
96 static unsigned short pm_db_range = 9000; /* range width in 1/100 dB */
97 /* Timing behaviour */
98 static int pm_peak_hold = 1; /* peak hold timeout index */
99 static int pm_peak_release = 8; /* peak release in units per read */
100 static int pm_clip_hold = 16; /* clip hold timeout index */
101 static bool pm_clip_eternal = false; /* true if clip timeout is disabled */
103 #ifdef HAVE_RECORDING
104 static unsigned short trig_strt_threshold;
105 static long trig_strt_duration;
106 static long trig_strt_dropout;
107 static unsigned short trig_stp_threshold;
108 static long trig_stp_hold;
109 static long trig_rstrt_gap;
111 /* point in time when the threshold was exceeded */
112 static long trig_hightime;
114 /* point in time when the volume fell below the threshold*/
115 static long trig_lowtime;
117 /* The output value of the trigger. See TRIG_XXX constants for valid values */
118 static int trig_status = TRIG_OFF;
120 static void (*trigger_listener)(int) = NULL;
122 /* clipping counter (only used for recording) */
123 static unsigned int pm_clipcount = 0; /* clipping count */
124 static bool pm_clipcount_active = false; /* counting or not */
125 #endif
127 /* debug only */
128 #ifdef PM_DEBUG
129 static int peek_calls = 0;
131 #define PEEKS_PER_DRAW_SIZE 40
132 static unsigned int peeks_per_redraw[PEEKS_PER_DRAW_SIZE];
134 #define TICKS_PER_DRAW_SIZE 20
135 static unsigned int ticks_per_redraw[TICKS_PER_DRAW_SIZE];
136 #endif
138 static void peak_meter_draw(struct screen *display, struct meter_scales *meter_scales,
139 int x, int y, int width, int height);
141 /* time out values for max */
142 static const short peak_time_out[] = {
143 0 * HZ, HZ / 5, 30, HZ / 2, HZ, 2 * HZ,
144 3 * HZ, 4 * HZ, 5 * HZ, 6 * HZ, 7 * HZ, 8 * HZ,
145 9 * HZ, 10 * HZ, 15 * HZ, 20 * HZ, 30 * HZ, 60 * HZ
148 /* time out values for clip */
149 static const long clip_time_out[] = {
150 0 * HZ, 1 * HZ, 2 * HZ, 3 * HZ, 4 * HZ, 5 * HZ,
151 6 * HZ, 7 * HZ, 8 * HZ, 9 * HZ, 10 * HZ, 15 * HZ,
152 20 * HZ, 25 * HZ, 30 * HZ, 45 * HZ, 60 * HZ, 90 * HZ,
153 120 * HZ, 180 * HZ, 300 * HZ, 600L * HZ, 1200L * HZ,
154 2700L * HZ, 5400L * HZ
157 /* precalculated peak values that represent magical
158 dBfs values. Used to draw the scale */
159 static const short db_scale_src_values[DB_SCALE_SRC_VALUES_SIZE] = {
160 32736, /* 0 db */
161 22752, /* - 3 db */
162 16640, /* - 6 db */
163 11648, /* - 9 db */
164 8320, /* -12 db */
165 4364, /* -18 db */
166 2064, /* -24 db */
167 1194, /* -30 db */
168 363, /* -40 db */
169 101, /* -50 db */
170 34, /* -60 db */
171 0, /* -inf */
174 static int db_scale_count = DB_SCALE_SRC_VALUES_SIZE;
177 * Calculates dB Value for the peak meter, uses peak value as input
178 * @param int sample - The input value
179 * Make sure that 0 <= value < SAMPLE_RANGE
181 * @return int - The 2 digit fixed point result of the euation
182 * 20 * log (sample / SAMPLE_RANGE) + 90
183 * Output range is 0-9000 (that is 0.0 - 90.0 dB).
184 * Normally 0dB is full scale, here it is shifted +90dB.
185 * The calculation is based on the results of a linear
186 * approximation tool written specifically for this problem
187 * by Andreas Zwirtes (radhard@gmx.de). The result has an
188 * accurracy of better than 2%. It is highly runtime optimized,
189 * the cascading if-clauses do an successive approximation on
190 * the input value. This avoids big lookup-tables and
191 * for-loops.
192 * Improved by Jvo Studer for errors < 0.2dB for critical
193 * range of -12dB to 0dB (78.0 to 90.0dB).
196 int calc_db (int isample)
198 /* return n+m*(isample-istart)/100 */
199 int n;
200 long m;
201 int istart;
203 if (isample < 2308) { /* Range 1-5 */
205 if (isample < 115) { /* Range 1-3 */
207 if (isample < 24) {
209 if (isample < 5) {
210 istart = 1; /* Range 1 */
211 n = 98;
212 m = 34950;
214 else {
215 istart = 5; /* Range 2 */
216 n = 1496;
217 m = 7168;
220 else {
221 istart = 24; /* Range 3 */
222 n = 2858;
223 m = 1498;
226 else { /* Range 4-5 */
228 if (isample < 534) {
229 istart = 114; /* Range 4 */
230 n = 4207;
231 m = 319;
233 else {
234 istart = 588; /* Range 5 */
235 n = 5583;
236 m = 69;
241 else { /* Range 6-9 */
243 if (isample < 12932) {
245 if (isample < 6394) {
246 istart = 2608; /* Range 6 */
247 n = 6832;
248 m = 21;
250 else {
251 istart = 7000; /* Range 7 */
252 n = 7682;
253 m = 9;
256 else {
258 if (isample < 22450) {
259 istart = 13000; /* Range 8 */
260 n = 8219;
261 m = 5;
263 else {
264 istart = 22636; /* Range 9 */
265 n = 8697;
266 m = 3;
271 return n + (m * (long)(isample - istart)) / 100L;
276 * A helper function for peak_meter_db2sample. Don't call it separately but
277 * use peak_meter_db2sample. If one or both of min and max are outside the
278 * range 0 <= min (or max) < 8961 the behaviour of this function is
279 * undefined. It may not return.
280 * @param int min - The minimum of the value range that is searched.
281 * @param int max - The maximum of the value range that is searched.
282 * @param int db - The value in dBfs * (-100) for which the according
283 * minimal peak sample is searched.
284 * @return int - A linear volume value with 0 <= value < MAX_PEAK
286 static int db_to_sample_bin_search(int min, int max, int db)
288 int test = min + (max - min) / 2;
290 if (min < max) {
291 if (calc_db(test) < db) {
292 test = db_to_sample_bin_search(test, max, db);
293 } else {
294 if (calc_db(test-1) > db) {
295 test = db_to_sample_bin_search(min, test, db);
299 return test;
303 * Converts a value representing dBfs to a linear
304 * scaled volume info as it is used by the MAS.
305 * An incredibly inefficiant function which is
306 * the vague inverse of calc_db. This really
307 * should be replaced by something better soon.
309 * @param int db - A dBfs * 100 value with
310 * -9000 < value <= 0
311 * @return int - The return value is in the range of
312 * 0 <= return value < MAX_PEAK
314 int peak_meter_db2sample(int db)
316 int retval = 0;
318 /* what is the maximum pseudo db value */
319 int max_peak_db = calc_db(MAX_PEAK - 1);
321 /* range check: db value to big */
322 if (max_peak_db + db < 0) {
323 retval = 0;
326 /* range check: db value too small */
327 else if (max_peak_db + db >= max_peak_db) {
328 retval = MAX_PEAK -1;
331 /* value in range: find the matching linear value */
332 else {
333 retval = db_to_sample_bin_search(0, MAX_PEAK, max_peak_db + db);
335 /* as this is a dirty function anyway, we want to adjust the
336 full scale hit manually to avoid users complaining that when
337 they adjust maximum for 0 dBfs and display it in percent it
338 shows 99%. That is due to precision loss and this is the
339 optical fix */
342 return retval;
346 * Set the min value for restriction of the value range.
347 * @param int newmin - depending whether dBfs is used
348 * newmin is a value in dBfs * 100 or in linear percent values.
349 * for dBfs: -9000 < newmin <= 0
350 * for linear: 0 <= newmin <= 100
352 static void peak_meter_set_min(int newmin)
354 if (pm_use_dbfs) {
355 peak_meter_range_min = peak_meter_db2sample(newmin);
357 } else {
358 if (newmin < peak_meter_range_max) {
359 peak_meter_range_min = newmin * MAX_PEAK / 100;
363 pm_range = peak_meter_range_max - peak_meter_range_min;
365 /* Avoid division by zero. */
366 if (pm_range == 0) {
367 pm_range = 1;
370 pm_db_min = calc_db(peak_meter_range_min);
371 pm_db_range = pm_db_max - pm_db_min;
372 int i;
373 FOR_NB_SCREENS(i)
374 scales[i].db_scale_valid = false;
378 * Returns the minimum value of the range the meter
379 * displays. If the scale is set to dBfs it returns
380 * dBfs values * 100 or linear percent values.
381 * @return: using dBfs : -9000 < value <= 0
382 * using linear scale: 0 <= value <= 100
384 int peak_meter_get_min(void)
386 int retval = 0;
387 if (pm_use_dbfs) {
388 retval = calc_db(peak_meter_range_min) - calc_db(MAX_PEAK - 1);
389 } else {
390 retval = peak_meter_range_min * 100 / MAX_PEAK;
392 return retval;
396 * Set the max value for restriction of the value range.
397 * @param int newmax - depending wether dBfs is used
398 * newmax is a value in dBfs * 100 or in linear percent values.
399 * for dBfs: -9000 < newmax <= 0
400 * for linear: 0 <= newmax <= 100
402 static void peak_meter_set_max(int newmax)
404 if (pm_use_dbfs) {
405 peak_meter_range_max = peak_meter_db2sample(newmax);
406 } else {
407 if (newmax > peak_meter_range_min) {
408 peak_meter_range_max = newmax * MAX_PEAK / 100;
412 pm_range = peak_meter_range_max - peak_meter_range_min;
414 /* Avoid division by zero. */
415 if (pm_range == 0) {
416 pm_range = 1;
419 pm_db_max = calc_db(peak_meter_range_max);
420 pm_db_range = pm_db_max - pm_db_min;
421 int i;
422 FOR_NB_SCREENS(i)
423 scales[i].db_scale_valid = false;
427 * Returns the minimum value of the range the meter
428 * displays. If the scale is set to dBfs it returns
429 * dBfs values * 100 or linear percent values
430 * @return: using dBfs : -9000 < value <= 0
431 * using linear scale: 0 <= value <= 100
433 int peak_meter_get_max(void)
435 int retval = 0;
436 if (pm_use_dbfs) {
437 retval = calc_db(peak_meter_range_max) - calc_db(MAX_PEAK - 1);
438 } else {
439 retval = peak_meter_range_max * 100 / MAX_PEAK;
441 return retval;
445 * Returns whether the meter is currently displaying dBfs or percent values.
446 * @return bool - true if the meter is displaying dBfs
447 false if the meter is displaying percent values.
449 bool peak_meter_get_use_dbfs(void)
451 return pm_use_dbfs;
455 * Specifies whether the values displayed are scaled
456 * as dBfs or as linear percent values.
457 * @param use - set to true for dBfs,
458 * set to false for linear scaling in percent
460 void peak_meter_set_use_dbfs(bool use)
462 int i;
463 pm_use_dbfs = use;
464 FOR_NB_SCREENS(i)
465 scales[i].db_scale_valid = false;
469 * Initialize the range of the meter. Only values
470 * that are in the range of [range_min ... range_max]
471 * are displayed.
472 * @param bool dbfs - set to true for dBfs,
473 * set to false for linear scaling in percent
474 * @param int range_min - Specifies the lower value of the range.
475 * Pass a value dBfs * 100 when dbfs is set to true.
476 * Pass a percent value when dbfs is set to false.
477 * @param int range_max - Specifies the upper value of the range.
478 * Pass a value dBfs * 100 when dbfs is set to true.
479 * Pass a percent value when dbfs is set to false.
481 void peak_meter_init_range( bool dbfs, int range_min, int range_max)
483 pm_use_dbfs = dbfs;
484 peak_meter_set_min(range_min);
485 peak_meter_set_max(range_max);
489 * Initialize the peak meter with all relevant values concerning times.
490 * @param int release - Set the maximum amount of pixels the meter is allowed
491 * to decrease with each redraw
492 * @param int hold - Select the time preset for the time the peak indicator
493 * is reset after a peak occurred. The preset values are
494 * stored in peak_time_out.
495 * @param int clip_hold - Select the time preset for the time the peak
496 * indicator is reset after a peak occurred. The preset
497 * values are stored in clip_time_out.
499 void peak_meter_init_times(int release, int hold, int clip_hold)
501 pm_peak_hold = hold;
502 pm_peak_release = release;
503 pm_clip_hold = clip_hold;
506 #ifdef HAVE_RECORDING
508 * Enable/disable clip counting
510 void pm_activate_clipcount(bool active)
512 pm_clipcount_active = active;
516 * Get clipping counter value
518 int pm_get_clipcount(void)
520 return pm_clipcount;
524 * Set clipping counter to zero (typically at start of recording or playback)
526 void pm_reset_clipcount(void)
528 pm_clipcount = 0;
530 #endif
533 * Set the source of the peak meter to playback or to
534 * record.
535 * @param: bool playback - If true playback peak meter is used.
536 * If false recording peak meter is used.
538 void peak_meter_playback(bool playback)
540 int i;
541 #ifdef SIMULATOR
542 (void)playback;
543 #elif CONFIG_CODEC == SWCODEC
544 pm_playback = playback;
545 #else
546 if (playback) {
547 pm_src_left = MAS_REG_DQPEAK_L;
548 pm_src_right = MAS_REG_DQPEAK_R;
549 } else {
550 pm_src_left = MAS_REG_QPEAK_L;
551 pm_src_right = MAS_REG_QPEAK_R;
553 #endif
554 /* reset the scales just in case recording and playback
555 use different viewport sizes. Normally we should be checking viewport
556 sizes every time but this will do for now */
557 FOR_NB_SCREENS(i)
558 scales[i].db_scale_valid = false;
561 #ifdef HAVE_RECORDING
562 static void set_trig_status(int new_state)
564 if (trig_status != new_state) {
565 trig_status = new_state;
566 if (trigger_listener != NULL) {
567 trigger_listener(trig_status);
572 #endif
575 * Reads peak values from the MAS, and detects clips. The
576 * values are stored in pm_max_left pm_max_right for later
577 * evauluation. Consecutive calls to peak_meter_peek detect
578 * that ocurred. This function could be used by a thread for
579 * busy reading the MAS.
581 void peak_meter_peek(void)
583 int left, right;
584 #ifdef HAVE_RECORDING
585 bool was_clipping = pm_clip_left || pm_clip_right;
586 #endif
587 /* read current values */
588 #if CONFIG_CODEC == SWCODEC
589 if (pm_playback)
590 pcm_calculate_peaks(&pm_cur_left, &pm_cur_right);
591 #ifdef HAVE_RECORDING
592 else
593 pcm_calculate_rec_peaks(&pm_cur_left, &pm_cur_right);
594 #endif
595 left = pm_cur_left;
596 right = pm_cur_right;
597 #else
598 #ifndef SIMULATOR
599 pm_cur_left = left = mas_codec_readreg(pm_src_left);
600 pm_cur_right = right = mas_codec_readreg(pm_src_right);
601 #else
602 pm_cur_left = left = 8000;
603 pm_cur_right = right = 9000;
604 #endif
605 #endif
607 /* check for clips
608 An clip is assumed when two consecutive readouts
609 of the volume are at full scale. This is proven
610 to be inaccurate in both ways: it may detect clips
611 when no clip occurred and it may fail to detect
612 a real clip. For software codecs, the peak is already
613 the max of a bunch of samples, so use one max value
614 or you fail to detect clipping! */
615 #if CONFIG_CODEC == SWCODEC
616 if (left == MAX_PEAK - 1) {
617 #else
618 if ((left == pm_max_left) &&
619 (left == MAX_PEAK - 1)) {
620 #endif
621 pm_clip_left = true;
622 pm_clip_timeout_l =
623 current_tick + clip_time_out[pm_clip_hold];
626 #if CONFIG_CODEC == SWCODEC
627 if (right == MAX_PEAK - 1) {
628 #else
629 if ((right == pm_max_right) &&
630 (right == MAX_PEAK - 1)) {
631 #endif
632 pm_clip_right = true;
633 pm_clip_timeout_r =
634 current_tick + clip_time_out[pm_clip_hold];
637 #ifdef HAVE_RECORDING
638 if(!was_clipping && (pm_clip_left || pm_clip_right))
640 if(pm_clipcount_active)
641 pm_clipcount++;
643 #endif
645 /* peaks are searched -> we have to find the maximum. When
646 many calls of peak_meter_peek the maximum value will be
647 stored in pm_max_xxx. This maximum is reset by the
648 functions peak_meter_read_x. */
649 pm_max_left = MAX(pm_max_left, left);
650 pm_max_right = MAX(pm_max_right, right);
652 #ifdef HAVE_RECORDING
653 #if CONFIG_CODEC == SWCODEC
654 /* Ignore any unread peakmeter data */
655 #define MAX_DROP_TIME HZ/7 /* this value may need tweaking. Increase if you are
656 getting trig events when you shouldn't with
657 trig_stp_hold = 0 */
658 if (!trig_stp_hold)
659 trig_stp_hold = MAX_DROP_TIME;
660 #endif
662 switch (trig_status) {
663 case TRIG_READY:
664 /* no more changes, if trigger was activated as release trigger */
665 /* threshold exceeded? */
666 if ((left > trig_strt_threshold)
667 || (right > trig_strt_threshold)) {
668 /* reset trigger duration */
669 trig_hightime = current_tick;
671 /* reset dropout duration */
672 trig_lowtime = current_tick;
674 if (trig_strt_duration)
675 set_trig_status(TRIG_STEADY);
676 else
677 /* if trig_duration is set to 0 the user wants to start
678 recording immediately */
679 set_trig_status(TRIG_GO);
681 break;
683 case TRIG_STEADY:
684 case TRIG_RETRIG:
685 /* trigger duration exceeded */
686 if (current_tick - trig_hightime > trig_strt_duration) {
687 set_trig_status(TRIG_GO);
688 } else {
689 /* threshold exceeded? */
690 if ((left > trig_strt_threshold)
691 || (right > trig_strt_threshold)) {
692 /* reset lowtime */
693 trig_lowtime = current_tick;
695 /* volume is below threshold */
696 else {
697 /* dropout occurred? */
698 if (current_tick - trig_lowtime > trig_strt_dropout){
699 if (trig_status == TRIG_STEADY){
700 set_trig_status(TRIG_READY);
702 /* trig_status == TRIG_RETRIG */
703 else {
704 /* the gap has already expired */
705 trig_lowtime = current_tick - trig_rstrt_gap - 1;
706 set_trig_status(TRIG_POSTREC);
711 break;
713 case TRIG_GO:
714 case TRIG_CONTINUE:
715 /* threshold exceeded? */
716 if ((left > trig_stp_threshold)
717 || (right > trig_stp_threshold)) {
718 /* restart hold time countdown */
719 trig_lowtime = current_tick;
720 #if CONFIG_CODEC == SWCODEC
721 } else if (current_tick - trig_lowtime > MAX_DROP_TIME){
722 #else
723 } else {
724 #endif
725 set_trig_status(TRIG_POSTREC);
726 trig_hightime = current_tick;
728 break;
730 case TRIG_POSTREC:
731 /* gap time expired? */
732 if (current_tick - trig_lowtime > trig_rstrt_gap){
733 /* start threshold exceeded? */
734 if ((left > trig_strt_threshold)
735 || (right > trig_strt_threshold)) {
737 set_trig_status(TRIG_RETRIG);
738 trig_hightime = current_tick;
739 trig_lowtime = current_tick;
741 else
743 /* stop threshold exceeded */
744 if ((left > trig_stp_threshold)
745 || (right > trig_stp_threshold)) {
746 if (current_tick - trig_hightime > trig_stp_hold){
747 trig_lowtime = current_tick;
748 set_trig_status(TRIG_CONTINUE);
749 } else {
750 trig_lowtime = current_tick - trig_rstrt_gap - 1;
754 /* below any threshold */
755 else {
756 if (current_tick - trig_lowtime > trig_stp_hold){
757 set_trig_status(TRIG_READY);
758 } else {
759 trig_hightime = current_tick;
764 /* still within the gap time */
765 else {
766 /* stop threshold exceeded */
767 if ((left > trig_stp_threshold)
768 || (right > trig_stp_threshold)) {
769 set_trig_status(TRIG_CONTINUE);
770 trig_lowtime = current_tick;
773 /* hold time expired */
774 else if (current_tick - trig_lowtime > trig_stp_hold){
775 trig_hightime = current_tick;
776 trig_lowtime = current_tick;
777 set_trig_status(TRIG_READY);
780 break;
782 #if CONFIG_CODEC == SWCODEC
783 /* restore stop hold value */
784 if (trig_stp_hold == MAX_DROP_TIME)
785 trig_stp_hold = 0;
786 #endif
787 #endif
788 /* check levels next time peakmeter drawn */
789 level_check = true;
790 #ifdef PM_DEBUG
791 peek_calls++;
792 #endif
796 * Reads out the peak volume of the left channel.
797 * @return int - The maximum value that has been detected
798 * since the last call of peak_meter_read_l. The value
799 * is in the range 0 <= value < MAX_PEAK.
801 static int peak_meter_read_l(void)
803 /* pm_max_left contains the maximum of all peak values that were read
804 by peak_meter_peek since the last call of peak_meter_read_l */
805 int retval;
807 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
808 srand(current_tick);
809 pm_max_left = rand()%MAX_PEAK;
810 #endif
812 retval = pm_max_left;
814 #if defined(HAVE_RECORDING_HISTOGRAM) || defined(HAVE_AGC)
815 /* store max peak value for peak_meter_get_peakhold_x readout */
816 pm_peakhold_left = MAX(pm_max_left, pm_peakhold_left);
817 #endif
818 #ifdef PM_DEBUG
819 peek_calls = 0;
820 #endif
821 /* reset pm_max_left so that subsequent calls of peak_meter_peek don't
822 get fooled by an old maximum value */
823 pm_max_left = pm_cur_left;
825 return retval;
829 * Reads out the peak volume of the right channel.
830 * @return int - The maximum value that has been detected
831 * since the last call of peak_meter_read_l. The value
832 * is in the range 0 <= value < MAX_PEAK.
834 static int peak_meter_read_r(void)
836 /* peak_meter_r contains the maximum of all peak values that were read
837 by peak_meter_peek since the last call of peak_meter_read_r */
838 int retval;
840 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
841 srand(current_tick);
842 pm_max_right = rand()%MAX_PEAK;
843 #endif
845 retval = pm_max_right;
847 #if defined(HAVE_RECORDING_HISTOGRAM) || defined(HAVE_AGC)
848 /* store max peak value for peak_meter_get_peakhold_x readout */
849 pm_peakhold_right = MAX(pm_max_right, pm_peakhold_right);
850 #endif
851 #ifdef PM_DEBUG
852 peek_calls = 0;
853 #endif
854 /* reset pm_max_right so that subsequent calls of peak_meter_peek don't
855 get fooled by an old maximum value */
856 pm_max_right = pm_cur_right;
858 return retval;
861 #if defined(HAVE_AGC) || defined(HAVE_RECORDING_HISTOGRAM)
863 * Reads out the current peak-hold values since the last call.
864 * This is used by the histogram feature in the recording screen.
865 * Values are in the range 0 <= peak_x < MAX_PEAK. MAX_PEAK is typ 32767.
867 void peak_meter_get_peakhold(int *peak_left, int *peak_right)
869 if (peak_left)
870 *peak_left = pm_peakhold_left;
871 if (peak_right)
872 *peak_right = pm_peakhold_right;
873 pm_peakhold_left = 0;
874 pm_peakhold_right = 0;
876 #endif
879 * Reset the detected clips. This method is for
880 * use by the user interface.
881 * @param int unused - This parameter was added to
882 * make the function compatible with set_int
884 void peak_meter_set_clip_hold(int time)
886 pm_clip_left = false;
887 pm_clip_right = false;
888 pm_clip_eternal = (time > 0) ? false : true;
892 * Scales a peak value as read from the MAS to the range of meterwidth.
893 * The scaling is performed according to the scaling method (dBfs / linear)
894 * and the range (peak_meter_range_min .. peak_meter_range_max).
895 * @param unsigned short val - The volume value. Range: 0 <= val < MAX_PEAK
896 * @param int meterwidht - The widht of the meter in pixel
897 * @return unsigned short - A value 0 <= return value <= meterwidth
899 unsigned short peak_meter_scale_value(unsigned short val, int meterwidth)
901 int retval;
903 if (val <= peak_meter_range_min) {
904 return 0;
907 if (val >= peak_meter_range_max) {
908 return meterwidth;
911 retval = val;
913 /* different scaling is used for dBfs and linear percent */
914 if (pm_use_dbfs) {
916 /* scale the samples dBfs */
917 retval = (calc_db(retval) - pm_db_min) * meterwidth / pm_db_range;
920 /* Scale for linear percent display */
921 else
923 /* scale the samples */
924 retval = ((retval - peak_meter_range_min) * meterwidth)
925 / pm_range;
927 return retval;
929 void peak_meter_screen(struct screen *display, int x, int y, int height)
931 peak_meter_draw(display, &scales[display->screen_type], x, y,
932 display->getwidth() - x, height);
935 * Draws a peak meter in the specified size at the specified position.
936 * @param int x - The x coordinate.
937 * Make sure that 0 <= x and x + width < display->getwidth()
938 * @param int y - The y coordinate.
939 * Make sure that 0 <= y and y + height < display->getheight()
940 * @param int width - The width of the peak meter. Note that for display
941 * of clips a 3 pixel wide area is used ->
942 * width > 3
943 * @param int height - The height of the peak meter. height > 3
945 static void peak_meter_draw(struct screen *display, struct meter_scales *scales,
946 int x, int y, int width, int height)
948 static int left_level = 0, right_level = 0;
949 int left = 0, right = 0;
950 int meterwidth = width - 3;
951 int i, delta;
952 #if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
953 static long peak_release_tick[2] = {0,0};
954 int screen_nr = display->screen_type == SCREEN_MAIN ? 0 : 1;
955 #else
956 static long peak_release_tick = 0;
957 #endif
959 #ifdef PM_DEBUG
960 static long pm_tick = 0;
961 int tmp = peek_calls;
962 #endif
964 /* if disabled only draw the peak meter */
965 if (peak_meter_enabled) {
968 if (level_check){
969 /* only read the volume info from MAS if peek since last read*/
970 left_level = peak_meter_read_l();
971 right_level = peak_meter_read_r();
972 level_check = false;
975 /* scale the samples dBfs */
976 left = peak_meter_scale_value(left_level, meterwidth);
977 right = peak_meter_scale_value(right_level, meterwidth);
979 /*if the scale has changed -> recalculate the scale
980 (The scale becomes invalid when the range changed.) */
981 if (!scales->db_scale_valid){
983 if (pm_use_dbfs) {
984 db_scale_count = DB_SCALE_SRC_VALUES_SIZE;
985 for (i = 0; i < db_scale_count; i++){
986 /* find the real x-coords for predefined interesting
987 dBfs values. These only are recalculated when the
988 scaling of the meter changed. */
989 scales->db_scale_lcd_coord[i] =
990 peak_meter_scale_value(
991 db_scale_src_values[i],
992 meterwidth - 1);
996 /* when scaling linear we simly make 10% steps */
997 else {
998 db_scale_count = 10;
999 for (i = 0; i < db_scale_count; i++) {
1000 scales->db_scale_lcd_coord[i] =
1001 (i * (MAX_PEAK / 10) - peak_meter_range_min) *
1002 meterwidth / pm_range;
1006 /* mark scale valid to avoid recalculating dBfs values
1007 of the scale. */
1008 scales->db_scale_valid = true;
1011 /* apply release */
1012 #if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
1013 delta = current_tick - peak_release_tick[screen_nr];
1014 peak_release_tick[screen_nr] = current_tick;
1015 #else
1016 delta = current_tick - peak_release_tick;
1017 peak_release_tick = current_tick;
1018 #endif
1019 left = MAX(left , scales->last_left - delta * pm_peak_release);
1020 right = MAX(right, scales->last_right - delta * pm_peak_release);
1022 /* reset max values after timeout */
1023 if (TIME_AFTER(current_tick, scales->pm_peak_timeout_l)){
1024 scales->pm_peak_left = 0;
1027 if (TIME_AFTER(current_tick, scales->pm_peak_timeout_r)){
1028 scales->pm_peak_right = 0;
1031 if (!pm_clip_eternal) {
1032 if (pm_clip_left &&
1033 TIME_AFTER(current_tick, pm_clip_timeout_l)){
1034 pm_clip_left = false;
1037 if (pm_clip_right &&
1038 TIME_AFTER(current_tick, pm_clip_timeout_r)){
1039 pm_clip_right = false;
1043 /* check for new max values */
1044 if (left > scales->pm_peak_left) {
1045 scales->pm_peak_left = left - 1;
1046 scales->pm_peak_timeout_l =
1047 current_tick + peak_time_out[pm_peak_hold];
1050 if (right > scales->pm_peak_right) {
1051 scales->pm_peak_right = right - 1;
1052 scales->pm_peak_timeout_r =
1053 current_tick + peak_time_out[pm_peak_hold];
1057 /* draw the peak meter */
1058 display->set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1059 display->fillrect(x, y, width, height);
1060 display->set_drawmode(DRMODE_SOLID);
1062 /* draw left */
1063 display->fillrect (x, y, left, height / 2 - 2 );
1064 if (scales->pm_peak_left > 0) {
1065 display->vline(x + scales->pm_peak_left, y, y + height / 2 - 2 );
1067 if (pm_clip_left) {
1068 display->fillrect(x + meterwidth, y, 3, height / 2 - 1);
1071 /* draw right */
1072 display->fillrect(x, y + height / 2 + 1, right, height / 2 - 2);
1073 if (scales->pm_peak_right > 0) {
1074 display->vline( x + scales->pm_peak_right, y + height / 2, y + height - 2);
1076 if (pm_clip_right) {
1077 display->fillrect(x + meterwidth, y + height / 2, 3, height / 2 - 1);
1080 /* draw scale end */
1081 display->vline(x + meterwidth, y, y + height - 2);
1083 /* draw dots for scale marks */
1084 for (i = 0; i < db_scale_count; i++) {
1085 /* The x-coordinates of interesting scale mark points
1086 have been calculated before */
1087 display->drawpixel(x + scales->db_scale_lcd_coord[i],
1088 y + height / 2 - 1);
1091 #ifdef HAVE_RECORDING
1093 #ifdef HAVE_BACKLIGHT
1094 /* cliplight */
1095 if ((pm_clip_left || pm_clip_right) &&
1096 global_settings.cliplight &&
1097 #if CONFIG_CODEC == SWCODEC
1098 !pm_playback)
1099 #else
1100 !(audio_status() & (AUDIO_STATUS_PLAY | AUDIO_STATUS_ERROR)))
1101 #endif
1103 /* if clipping, cliplight setting on and in recording screen */
1104 if (global_settings.cliplight <= 2)
1106 /* turn on main unit light if setting set to main or both*/
1107 backlight_on();
1109 #ifdef HAVE_REMOTE_LCD
1110 if (global_settings.cliplight >= 2)
1112 /* turn remote light unit on if setting set to remote or both */
1113 remote_backlight_on();
1115 #endif /* HAVE_REMOTE_LCD */
1117 #endif /* HAVE_BACKLIGHT */
1119 if (trig_status != TRIG_OFF) {
1120 int start_trigx, stop_trigx, ycenter;
1122 display->set_drawmode(DRMODE_SOLID);
1123 ycenter = y + height / 2;
1124 /* display threshold value */
1125 start_trigx = x+peak_meter_scale_value(trig_strt_threshold,meterwidth);
1126 display->vline(start_trigx, ycenter - 2, ycenter);
1127 start_trigx ++;
1128 if (start_trigx < display->getwidth() ) display->drawpixel(start_trigx,
1129 ycenter - 1);
1131 stop_trigx = x + peak_meter_scale_value(trig_stp_threshold,meterwidth);
1132 display->vline(stop_trigx, ycenter - 2, ycenter);
1133 if (stop_trigx > 0) display->drawpixel(stop_trigx - 1, ycenter - 1);
1135 #endif /*HAVE_RECORDING*/
1137 #ifdef PM_DEBUG
1138 /* display a bar to show how many calls to peak_meter_peek
1139 have ocurred since the last display */
1140 display->set_drawmode(DRMODE_COMPLEMENT);
1141 display->fillrect(x, y, tmp, 3);
1143 if (tmp < PEEKS_PER_DRAW_SIZE) {
1144 peeks_per_redraw[tmp]++;
1147 tmp = current_tick - pm_tick;
1148 if (tmp < TICKS_PER_DRAW_SIZE ){
1149 ticks_per_redraw[tmp] ++;
1152 /* display a bar to show how many ticks have passed since
1153 the last redraw */
1154 display->fillrect(x, y + height / 2, current_tick - pm_tick, 2);
1155 pm_tick = current_tick;
1156 #endif
1158 scales->last_left = left;
1159 scales->last_right = right;
1161 display->set_drawmode(DRMODE_SOLID);
1164 #ifdef HAVE_RECORDING
1166 * Defines the parameters of the trigger. After these parameters are defined
1167 * the trigger can be started either by peak_meter_attack_trigger or by
1168 * peak_meter_release_trigger. Note that you can pass either linear (%) or
1169 * logarithmic (db) values to the thresholds. Positive values are intepreted as
1170 * percent (0 is 0% .. 100 is 100%). Negative values are interpreted as db.
1171 * To avoid ambiguosity of the value 0 the negative values are shifted by -1.
1172 * Thus -75 is -74db .. -1 is 0db.
1173 * @param start_threshold - The threshold used for attack trigger. Negative
1174 * values are interpreted as db -1, positive as %.
1175 * @param start_duration - The minimum time span within which start_threshold
1176 * must be exceeded to fire the attack trigger.
1177 * @param start_dropout - The maximum time span the level may fall below
1178 * start_threshold without releasing the attack trigger.
1179 * @param stop_threshold - The threshold the volume must fall below to release
1180 * the release trigger.Negative values are
1181 * interpreted as db -1, positive as %.
1182 * @param stop_hold - The minimum time the volume must fall below the
1183 * stop_threshold to release the trigger.
1184 * @param
1186 void peak_meter_define_trigger(
1187 int start_threshold,
1188 long start_duration,
1189 long start_dropout,
1190 int stop_threshold,
1191 long stop_hold_time,
1192 long restart_gap
1195 if (start_threshold < 0) {
1196 /* db */
1197 if (start_threshold < -89) {
1198 trig_strt_threshold = 0;
1199 } else {
1200 trig_strt_threshold =peak_meter_db2sample((start_threshold+1)*100);
1202 } else {
1203 /* linear percent */
1204 trig_strt_threshold = start_threshold * MAX_PEAK / 100;
1206 trig_strt_duration = start_duration;
1207 trig_strt_dropout = start_dropout;
1208 if (stop_threshold < 0) {
1209 /* db */
1210 trig_stp_threshold = peak_meter_db2sample((stop_threshold + 1) * 100);
1211 } else {
1212 /* linear percent */
1213 trig_stp_threshold = stop_threshold * MAX_PEAK / 100;
1215 trig_stp_hold = stop_hold_time;
1216 trig_rstrt_gap = restart_gap;
1220 * Enables or disables the trigger.
1221 * @param on - If true the trigger is turned on.
1223 void peak_meter_trigger(bool on)
1225 /* don't use set_trigger here as that would fire an undesired event */
1226 trig_status = on ? TRIG_READY : TRIG_OFF;
1230 * Registers the listener function that listenes on trig_status changes.
1231 * @param listener - The function that is called with each change of
1232 * trig_status. May be set to NULL if no callback is desired.
1234 void peak_meter_set_trigger_listener(void (*listener)(int status))
1236 trigger_listener = listener;
1240 * Fetches the status of the trigger.
1241 * TRIG_OFF: the trigger is inactive
1242 * TRIG_RELEASED: The volume level is below the threshold
1243 * TRIG_ACTIVATED: The volume level has exceeded the threshold, but the trigger
1244 * hasn't been fired yet.
1245 * TRIG_FIRED: The volume exceeds the threshold
1247 * To activate the trigger call either peak_meter_attack_trigger or
1248 * peak_meter_release_trigger. To turn the trigger off call
1249 * peak_meter_trigger_off.
1251 int peak_meter_trigger_status(void)
1253 return trig_status; /* & TRIG_PIT_MASK;*/
1256 void peak_meter_draw_trig(int xpos[], int ypos[],
1257 int trig_width[], int nb_screens)
1259 int barstart[NB_SCREENS];
1260 int barend[NB_SCREENS];
1261 int icon;
1262 int ixpos[NB_SCREENS];
1263 int i;
1264 int trigbar_width[NB_SCREENS];
1266 FOR_NB_SCREENS(i)
1267 trigbar_width[i] = (trig_width[i] - (2 * (ICON_PLAY_STATE_WIDTH + 1)));
1269 switch (trig_status) {
1271 case TRIG_READY:
1272 FOR_NB_SCREENS(i){
1273 barstart[i] = 0;
1274 barend[i] = 0;
1276 icon = Icon_Stop;
1277 FOR_NB_SCREENS(i)
1278 ixpos[i] = xpos[i];
1279 break;
1281 case TRIG_STEADY:
1282 case TRIG_RETRIG:
1283 FOR_NB_SCREENS(i)
1285 barstart[i] = 0;
1286 barend[i] = (trig_strt_duration == 0) ? trigbar_width[i] :
1287 trigbar_width[i] *
1288 (current_tick - trig_hightime) / trig_strt_duration;
1290 icon = Icon_Stop;
1291 FOR_NB_SCREENS(i)
1292 ixpos[i] = xpos[i];
1293 break;
1295 case TRIG_GO:
1296 case TRIG_CONTINUE:
1297 FOR_NB_SCREENS(i)
1299 barstart[i] = trigbar_width[i];
1300 barend[i] = trigbar_width[i];
1302 icon = Icon_Record;
1303 FOR_NB_SCREENS(i)
1304 ixpos[i] = xpos[i]+ trig_width[i] - ICON_PLAY_STATE_WIDTH;
1305 break;
1307 case TRIG_POSTREC:
1308 FOR_NB_SCREENS(i)
1310 barstart[i] = (trig_stp_hold == 0) ? 0 :
1311 trigbar_width[i] - trigbar_width[i] *
1312 (current_tick - trig_lowtime) / trig_stp_hold;
1313 barend[i] = trigbar_width[i];
1315 icon = Icon_Record;
1316 FOR_NB_SCREENS(i)
1317 ixpos[i] = xpos[i] + trig_width[i] - ICON_PLAY_STATE_WIDTH;
1318 break;
1320 default:
1321 return;
1324 for(i = 0; i < nb_screens; i++)
1326 gui_scrollbar_draw(&screens[i], xpos[i] + ICON_PLAY_STATE_WIDTH + 1,
1327 ypos[i] + 1, trigbar_width[i], TRIG_HEIGHT - 2,
1328 trigbar_width[i], barstart[i], barend[i],
1329 HORIZONTAL);
1331 screens[i].mono_bitmap(bitmap_icons_7x8[icon], ixpos[i], ypos[i],
1332 ICON_PLAY_STATE_WIDTH, STATUSBAR_HEIGHT);
1335 #endif
1337 int peak_meter_draw_get_btn(int action_context, int x[], int y[],
1338 int height[], int nb_screens,
1339 struct viewport vps[])
1341 int button = BUTTON_NONE;
1342 long next_refresh = current_tick;
1343 long next_big_refresh = current_tick + HZ / 10;
1344 int i;
1345 #if (CONFIG_CODEC == SWCODEC)
1346 bool highperf = false;
1347 #else
1348 /* On MAS targets, we need to poll as often as possible in order to not
1349 * miss a peak, as the MAS does only provide a quasi-peak. When the disk
1350 * is active, it must not draw too much CPU power or a buffer overrun can
1351 * happen when saving a recording. As a compromise, poll only once per tick
1352 * when the disk is active, otherwise spin around as fast as possible. */
1353 bool highperf = !storage_disk_is_active();
1354 #endif
1355 bool dopeek = true;
1357 while (TIME_BEFORE(current_tick, next_big_refresh)) {
1358 button = get_action(action_context, TIMEOUT_NOBLOCK);
1359 if (button != BUTTON_NONE) {
1360 break;
1362 if (dopeek) { /* Peek only once per refresh when disk is */
1363 peak_meter_peek(); /* spinning, but as often as possible */
1364 dopeek = highperf; /* otherwise. */
1365 yield();
1366 } else {
1367 sleep(0); /* Sleep until end of current tick. */
1369 if (TIME_AFTER(current_tick, next_refresh)) {
1370 for(i = 0; i < nb_screens; i++)
1372 screens[i].set_viewport(&vps[i]);
1373 peak_meter_screen(&screens[i], x[i], y[i], height[i]);
1374 screens[i].update_viewport_rect(x[i], y[i],
1375 screens[i].getwidth() - x[i],
1376 height[i]);
1378 next_refresh += HZ / PEAK_METER_FPS;
1379 dopeek = true;
1383 return button;
1386 #ifdef PM_DEBUG
1387 static void peak_meter_clear_histogram(void)
1389 int i = 0;
1390 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1391 ticks_per_redraw[i] = (unsigned int)0;
1394 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1395 peeks_per_redraw[i] = (unsigned int)0;
1399 bool peak_meter_histogram(void)
1401 int i;
1402 int btn = BUTTON_NONE;
1403 while ((btn & BUTTON_OFF) != BUTTON_OFF )
1405 unsigned int max = 0;
1406 int y = 0;
1407 int x = 0;
1408 screens[0].clear_display();
1410 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1411 max = MAX(max, peeks_per_redraw[i]);
1414 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1415 x = peeks_per_redraw[i] * (LCD_WIDTH - 1)/ max;
1416 screens[0].hline(0, x, y + i);
1419 y = PEEKS_PER_DRAW_SIZE + 1;
1420 max = 0;
1422 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1423 max = MAX(max, ticks_per_redraw[i]);
1426 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1427 x = ticks_per_redraw[i] * (LCD_WIDTH - 1)/ max;
1428 screens[0].hline(0, x, y + i);
1430 screens[0].update();
1432 btn = button_get(true);
1433 if (btn == BUTTON_PLAY) {
1434 peak_meter_clear_histogram();
1437 return false;
1439 #endif