Blackjack: small improvement in the c200 buttonmap - use the volume buttons instead...
[Rockbox.git] / apps / recorder / peakmeter.c
blobfe97bd83d2dd27e411339887eff17cb27679dcb4
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 unsigned short peak_meter_range_min; /* minimum of range in samples */
86 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_eternal = false;
874 if (time <= 0) {
875 pm_clip_left = false;
876 pm_clip_right = false;
877 pm_clip_eternal = true;
882 * Scales a peak value as read from the MAS to the range of meterwidth.
883 * The scaling is performed according to the scaling method (dBfs / linear)
884 * and the range (peak_meter_range_min .. peak_meter_range_max).
885 * @param unsigned short val - The volume value. Range: 0 <= val < MAX_PEAK
886 * @param int meterwidht - The widht of the meter in pixel
887 * @return unsigned short - A value 0 <= return value <= meterwidth
889 unsigned short peak_meter_scale_value(unsigned short val, int meterwidth)
891 int retval;
893 if (val <= peak_meter_range_min) {
894 return 0;
897 if (val >= peak_meter_range_max) {
898 return meterwidth;
901 retval = val;
903 /* different scaling is used for dBfs and linear percent */
904 if (pm_use_dbfs) {
906 /* scale the samples dBfs */
907 retval = (calc_db(retval) - pm_db_min) * meterwidth / pm_db_range;
910 /* Scale for linear percent display */
911 else
913 /* scale the samples */
914 retval = ((retval - peak_meter_range_min) * meterwidth)
915 / pm_range;
917 return retval;
919 void peak_meter_screen(struct screen *display, int x, int y, int height)
921 peak_meter_draw(display, &scales[display->screen_type], x, y,
922 display->width - x, height);
925 * Draws a peak meter in the specified size at the specified position.
926 * @param int x - The x coordinate.
927 * Make sure that 0 <= x and x + width < display->width
928 * @param int y - The y coordinate.
929 * Make sure that 0 <= y and y + height < display->height
930 * @param int width - The width of the peak meter. Note that for display
931 * of clips a 3 pixel wide area is used ->
932 * width > 3
933 * @param int height - The height of the peak meter. height > 3
935 static void peak_meter_draw(struct screen *display, struct meter_scales *scales,
936 int x, int y, int width, int height)
938 static int left_level = 0, right_level = 0;
939 int left = 0, right = 0;
940 int meterwidth = width - 3;
941 int i, delta;
942 static long peak_release_tick = 0;
944 #ifdef PM_DEBUG
945 static long pm_tick = 0;
946 int tmp = peek_calls;
947 #endif
949 /* if disabled only draw the peak meter */
950 if (peak_meter_enabled) {
953 if (level_check){
954 /* only read the volume info from MAS if peek since last read*/
955 left_level = peak_meter_read_l();
956 right_level = peak_meter_read_r();
957 level_check = false;
960 /* scale the samples dBfs */
961 left = peak_meter_scale_value(left_level, meterwidth);
962 right = peak_meter_scale_value(right_level, meterwidth);
964 /*if the scale has changed -> recalculate the scale
965 (The scale becomes invalid when the range changed.) */
966 if (!scales->db_scale_valid){
968 if (pm_use_dbfs) {
969 db_scale_count = DB_SCALE_SRC_VALUES_SIZE;
970 for (i = 0; i < db_scale_count; i++){
971 /* find the real x-coords for predefined interesting
972 dBfs values. These only are recalculated when the
973 scaling of the meter changed. */
974 scales->db_scale_lcd_coord[i] =
975 peak_meter_scale_value(
976 db_scale_src_values[i],
977 meterwidth - 1);
981 /* when scaling linear we simly make 10% steps */
982 else {
983 db_scale_count = 10;
984 for (i = 0; i < db_scale_count; i++) {
985 scales->db_scale_lcd_coord[i] =
986 (i * (MAX_PEAK / 10) - peak_meter_range_min) *
987 meterwidth / pm_range;
991 /* mark scale valid to avoid recalculating dBfs values
992 of the scale. */
993 scales->db_scale_valid = true;
996 /* apply release */
997 delta = current_tick - peak_release_tick;
998 peak_release_tick = current_tick;
999 left = MAX(left , scales->last_left - delta * pm_peak_release);
1000 right = MAX(right, scales->last_right - delta * pm_peak_release);
1002 /* reset max values after timeout */
1003 if (TIME_AFTER(current_tick, scales->pm_peak_timeout_l)){
1004 scales->pm_peak_left = 0;
1007 if (TIME_AFTER(current_tick, scales->pm_peak_timeout_r)){
1008 scales->pm_peak_right = 0;
1011 if (!pm_clip_eternal) {
1012 if (pm_clip_left &&
1013 TIME_AFTER(current_tick, pm_clip_timeout_l)){
1014 pm_clip_left = false;
1017 if (pm_clip_right &&
1018 TIME_AFTER(current_tick, pm_clip_timeout_r)){
1019 pm_clip_right = false;
1023 /* check for new max values */
1024 if (left > scales->pm_peak_left) {
1025 scales->pm_peak_left = left - 1;
1026 scales->pm_peak_timeout_l =
1027 current_tick + peak_time_out[pm_peak_hold];
1030 if (right > scales->pm_peak_right) {
1031 scales->pm_peak_right = right - 1;
1032 scales->pm_peak_timeout_r =
1033 current_tick + peak_time_out[pm_peak_hold];
1037 /* draw the peak meter */
1038 display->set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1039 display->fillrect(x, y, width, height);
1040 display->set_drawmode(DRMODE_SOLID);
1042 /* draw left */
1043 display->fillrect (x, y, left, height / 2 - 2 );
1044 if (scales->pm_peak_left > 0) {
1045 display->vline(x + scales->pm_peak_left, y, y + height / 2 - 2 );
1047 if (pm_clip_left) {
1048 display->fillrect(x + meterwidth, y, 3, height / 2 - 1);
1051 /* draw right */
1052 display->fillrect(x, y + height / 2 + 1, right, height / 2 - 2);
1053 if (scales->pm_peak_right > 0) {
1054 display->vline( x + scales->pm_peak_right, y + height / 2, y + height - 2);
1056 if (pm_clip_right) {
1057 display->fillrect(x + meterwidth, y + height / 2, 3, height / 2 - 1);
1060 /* draw scale end */
1061 display->vline(x + meterwidth, y, y + height - 2);
1063 /* draw dots for scale marks */
1064 for (i = 0; i < db_scale_count; i++) {
1065 /* The x-coordinates of interesting scale mark points
1066 have been calculated before */
1067 display->drawpixel(x + scales->db_scale_lcd_coord[i],
1068 y + height / 2 - 1);
1071 #ifdef HAVE_RECORDING
1073 #ifdef HAVE_BACKLIGHT
1074 /* cliplight */
1075 if ((pm_clip_left || pm_clip_right) &&
1076 global_settings.cliplight &&
1077 #if CONFIG_CODEC == SWCODEC
1078 !pm_playback)
1079 #else
1080 !(audio_status() & (AUDIO_STATUS_PLAY | AUDIO_STATUS_ERROR)))
1081 #endif
1083 /* if clipping, cliplight setting on and in recording screen */
1084 if (global_settings.cliplight <= 2)
1086 /* turn on main unit light if setting set to main or both*/
1087 backlight_on();
1089 #ifdef HAVE_REMOTE_LCD
1090 if (global_settings.cliplight >= 2)
1092 /* turn remote light unit on if setting set to remote or both */
1093 remote_backlight_on();
1095 #endif /* HAVE_REMOTE_LCD */
1097 #endif /* HAVE_BACKLIGHT */
1099 if (trig_status != TRIG_OFF) {
1100 int start_trigx, stop_trigx, ycenter;
1102 display->set_drawmode(DRMODE_SOLID);
1103 ycenter = y + height / 2;
1104 /* display threshold value */
1105 start_trigx = x+peak_meter_scale_value(trig_strt_threshold,meterwidth);
1106 display->vline(start_trigx, ycenter - 2, ycenter);
1107 start_trigx ++;
1108 if (start_trigx < display->width ) display->drawpixel(start_trigx, ycenter - 1);
1110 stop_trigx = x + peak_meter_scale_value(trig_stp_threshold,meterwidth);
1111 display->vline(stop_trigx, ycenter - 2, ycenter);
1112 if (stop_trigx > 0) display->drawpixel(stop_trigx - 1, ycenter - 1);
1114 #endif /*HAVE_RECORDING*/
1116 #ifdef PM_DEBUG
1117 /* display a bar to show how many calls to peak_meter_peek
1118 have ocurred since the last display */
1119 display->set_drawmode(DRMODE_COMPLEMENT);
1120 display->fillrect(x, y, tmp, 3);
1122 if (tmp < PEEKS_PER_DRAW_SIZE) {
1123 peeks_per_redraw[tmp]++;
1126 tmp = current_tick - pm_tick;
1127 if (tmp < TICKS_PER_DRAW_SIZE ){
1128 ticks_per_redraw[tmp] ++;
1131 /* display a bar to show how many ticks have passed since
1132 the last redraw */
1133 display->fillrect(x, y + height / 2, current_tick - pm_tick, 2);
1134 pm_tick = current_tick;
1135 #endif
1137 scales->last_left = left;
1138 scales->last_right = right;
1140 display->set_drawmode(DRMODE_SOLID);
1143 #ifdef HAVE_RECORDING
1145 * Defines the parameters of the trigger. After these parameters are defined
1146 * the trigger can be started either by peak_meter_attack_trigger or by
1147 * peak_meter_release_trigger. Note that you can pass either linear (%) or
1148 * logarithmic (db) values to the thresholds. Positive values are intepreted as
1149 * percent (0 is 0% .. 100 is 100%). Negative values are interpreted as db.
1150 * To avoid ambiguosity of the value 0 the negative values are shifted by -1.
1151 * Thus -75 is -74db .. -1 is 0db.
1152 * @param start_threshold - The threshold used for attack trigger. Negative
1153 * values are interpreted as db -1, positive as %.
1154 * @param start_duration - The minimum time span within which start_threshold
1155 * must be exceeded to fire the attack trigger.
1156 * @param start_dropout - The maximum time span the level may fall below
1157 * start_threshold without releasing the attack trigger.
1158 * @param stop_threshold - The threshold the volume must fall below to release
1159 * the release trigger.Negative values are
1160 * interpreted as db -1, positive as %.
1161 * @param stop_hold - The minimum time the volume must fall below the
1162 * stop_threshold to release the trigger.
1163 * @param
1165 void peak_meter_define_trigger(
1166 int start_threshold,
1167 long start_duration,
1168 long start_dropout,
1169 int stop_threshold,
1170 long stop_hold_time,
1171 long restart_gap
1174 if (start_threshold < 0) {
1175 /* db */
1176 if (start_threshold < -89) {
1177 trig_strt_threshold = 0;
1178 } else {
1179 trig_strt_threshold =peak_meter_db2sample((start_threshold+1)*100);
1181 } else {
1182 /* linear percent */
1183 trig_strt_threshold = start_threshold * MAX_PEAK / 100;
1185 trig_strt_duration = start_duration;
1186 trig_strt_dropout = start_dropout;
1187 if (stop_threshold < 0) {
1188 /* db */
1189 trig_stp_threshold = peak_meter_db2sample((stop_threshold + 1) * 100);
1190 } else {
1191 /* linear percent */
1192 trig_stp_threshold = stop_threshold * MAX_PEAK / 100;
1194 trig_stp_hold = stop_hold_time;
1195 trig_rstrt_gap = restart_gap;
1199 * Enables or disables the trigger.
1200 * @param on - If true the trigger is turned on.
1202 void peak_meter_trigger(bool on)
1204 /* don't use set_trigger here as that would fire an undesired event */
1205 trig_status = on ? TRIG_READY : TRIG_OFF;
1209 * Registers the listener function that listenes on trig_status changes.
1210 * @param listener - The function that is called with each change of
1211 * trig_status. May be set to NULL if no callback is desired.
1213 void peak_meter_set_trigger_listener(void (*listener)(int status))
1215 trigger_listener = listener;
1219 * Fetches the status of the trigger.
1220 * TRIG_OFF: the trigger is inactive
1221 * TRIG_RELEASED: The volume level is below the threshold
1222 * TRIG_ACTIVATED: The volume level has exceeded the threshold, but the trigger
1223 * hasn't been fired yet.
1224 * TRIG_FIRED: The volume exceeds the threshold
1226 * To activate the trigger call either peak_meter_attack_trigger or
1227 * peak_meter_release_trigger. To turn the trigger off call
1228 * peak_meter_trigger_off.
1230 int peak_meter_trigger_status(void)
1232 return trig_status; /* & TRIG_PIT_MASK;*/
1235 void peak_meter_draw_trig(int xpos[], int ypos[], int trig_width[], int nb_screens)
1237 int barstart[NB_SCREENS];
1238 int barend[NB_SCREENS];
1239 int icon;
1240 int ixpos[NB_SCREENS];
1241 int i;
1242 int trigbar_width[NB_SCREENS];
1244 FOR_NB_SCREENS(i)
1245 trigbar_width[i] = (trig_width[i] - (2 * (ICON_PLAY_STATE_WIDTH + 1)));
1247 switch (trig_status) {
1249 case TRIG_READY:
1250 FOR_NB_SCREENS(i){
1251 barstart[i] = 0;
1252 barend[i] = 0;
1254 icon = Icon_Stop;
1255 FOR_NB_SCREENS(i)
1256 ixpos[i] = xpos[i];
1257 break;
1259 case TRIG_STEADY:
1260 case TRIG_RETRIG:
1261 FOR_NB_SCREENS(i)
1263 barstart[i] = 0;
1264 barend[i] = (trig_strt_duration == 0) ? trigbar_width[i] :
1265 trigbar_width[i] *
1266 (current_tick - trig_hightime) / trig_strt_duration;
1268 icon = Icon_Stop;
1269 FOR_NB_SCREENS(i)
1270 ixpos[i] = xpos[i];
1271 break;
1273 case TRIG_GO:
1274 case TRIG_CONTINUE:
1275 FOR_NB_SCREENS(i)
1277 barstart[i] = trigbar_width[i];
1278 barend[i] = trigbar_width[i];
1280 icon = Icon_Record;
1281 FOR_NB_SCREENS(i)
1282 ixpos[i] = xpos[i]+ trig_width[i] - ICON_PLAY_STATE_WIDTH;
1283 break;
1285 case TRIG_POSTREC:
1286 FOR_NB_SCREENS(i)
1288 barstart[i] = (trig_stp_hold == 0) ? 0 :
1289 trigbar_width[i] - trigbar_width[i] *
1290 (current_tick - trig_lowtime) / trig_stp_hold;
1291 barend[i] = trigbar_width[i];
1293 icon = Icon_Record;
1294 FOR_NB_SCREENS(i)
1295 ixpos[i] = xpos[i] + trig_width[i] - ICON_PLAY_STATE_WIDTH;
1296 break;
1298 default:
1299 return;
1302 for(i = 0; i < nb_screens; i++)
1304 gui_scrollbar_draw(&screens[i], xpos[i] + ICON_PLAY_STATE_WIDTH + 1,
1305 ypos[i] + 1, trigbar_width[i], TRIG_HEIGHT - 2,
1306 trigbar_width[i], barstart[i], barend[i],
1307 HORIZONTAL);
1309 screens[i].mono_bitmap(bitmap_icons_7x8[icon], ixpos[i], ypos[i],
1310 ICON_PLAY_STATE_WIDTH, STATUSBAR_HEIGHT);
1313 #endif
1315 int peak_meter_draw_get_btn(int x, int y[], int height, int nb_screens)
1317 int button = BUTTON_NONE;
1318 long next_refresh = current_tick;
1319 long next_big_refresh = current_tick + HZ / 10;
1320 int i;
1321 #if (CONFIG_CODEC == SWCODEC) || defined(SIMULATOR)
1322 bool highperf = false;
1323 #else
1324 /* On MAS targets, we need to poll as often as possible in order to not
1325 * miss a peak, as the MAS does only provide a quasi-peak. When the disk
1326 * is active, it must not draw too much CPU power or a buffer overrun can
1327 * happen when saving a recording. As a compromise, poll only once per tick
1328 * when the disk is active, otherwise spin around as fast as possible. */
1329 bool highperf = !ata_disk_is_active();
1330 #endif
1331 bool dopeek = true;
1333 while (TIME_BEFORE(current_tick, next_big_refresh)) {
1334 button = get_action(CONTEXT_RECSCREEN, TIMEOUT_NOBLOCK);
1335 if (button != BUTTON_NONE) {
1336 break;
1338 if (dopeek) { /* Peek only once per refresh when disk is */
1339 peak_meter_peek(); /* spinning, but as often as possible */
1340 dopeek = highperf; /* otherwise. */
1341 yield();
1342 } else {
1343 sleep(0); /* Sleep until end of current tick. */
1345 if (TIME_AFTER(current_tick, next_refresh)) {
1346 for(i = 0; i < nb_screens; i++)
1348 peak_meter_screen(&screens[i], x, y[i], height);
1349 screens[i].update_rect(x, y[i], screens[i].width - x, height);
1351 next_refresh += HZ / PEAK_METER_FPS;
1352 dopeek = true;
1356 return button;
1359 #ifdef PM_DEBUG
1360 static void peak_meter_clear_histogram(void)
1362 int i = 0;
1363 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1364 ticks_per_redraw[i] = (unsigned int)0;
1367 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1368 peeks_per_redraw[i] = (unsigned int)0;
1372 bool peak_meter_histogram(void)
1374 int i;
1375 int btn = BUTTON_NONE;
1376 while ((btn & BUTTON_OFF) != BUTTON_OFF )
1378 unsigned int max = 0;
1379 int y = 0;
1380 int x = 0;
1381 screens[0].clear_display();
1383 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1384 max = MAX(max, peeks_per_redraw[i]);
1387 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1388 x = peeks_per_redraw[i] * (LCD_WIDTH - 1)/ max;
1389 screens[0].hline(0, x, y + i);
1392 y = PEEKS_PER_DRAW_SIZE + 1;
1393 max = 0;
1395 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1396 max = MAX(max, ticks_per_redraw[i]);
1399 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1400 x = ticks_per_redraw[i] * (LCD_WIDTH - 1)/ max;
1401 screens[0].hline(0, x, y + i);
1403 screens[0].update();
1405 btn = button_get(true);
1406 if (btn == BUTTON_PLAY) {
1407 peak_meter_clear_histogram();
1410 return false;
1412 #endif