only build the clipcounter for targets with recording
[Rockbox.git] / apps / recorder / peakmeter.c
blobc7e351f8989c5aedf3f9b8d8ee493cc7f70e318f
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_playback.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 /* time out values for max */
135 static const short peak_time_out[] = {
136 0 * HZ, HZ / 5, 30, HZ / 2, HZ, 2 * HZ,
137 3 * HZ, 4 * HZ, 5 * HZ, 6 * HZ, 7 * HZ, 8 * HZ,
138 9 * HZ, 10 * HZ, 15 * HZ, 20 * HZ, 30 * HZ, 60 * HZ
141 /* time out values for clip */
142 static const long clip_time_out[] = {
143 0 * HZ, 1 * HZ, 2 * HZ, 3 * HZ, 4 * HZ, 5 * HZ,
144 6 * HZ, 7 * HZ, 8 * HZ, 9 * HZ, 10 * HZ, 15 * HZ,
145 20 * HZ, 25 * HZ, 30 * HZ, 45 * HZ, 60 * HZ, 90 * HZ,
146 120 * HZ, 180 * HZ, 300 * HZ, 600L * HZ, 1200L * HZ,
147 2700L * HZ, 5400L * HZ
150 /* precalculated peak values that represent magical
151 dBfs values. Used to draw the scale */
152 static const int db_scale_src_values[DB_SCALE_SRC_VALUES_SIZE] = {
153 32736, /* 0 db */
154 22752, /* - 3 db */
155 16640, /* - 6 db */
156 11648, /* - 9 db */
157 8320, /* -12 db */
158 4364, /* -18 db */
159 2064, /* -24 db */
160 1194, /* -30 db */
161 363, /* -40 db */
162 101, /* -50 db */
163 34, /* -60 db */
164 0, /* -inf */
167 static int db_scale_count = DB_SCALE_SRC_VALUES_SIZE;
170 * Calculates dB Value for the peak meter, uses peak value as input
171 * @param int sample - The input value
172 * Make sure that 0 <= value < SAMPLE_RANGE
174 * @return int - The 2 digit fixed point result of the euation
175 * 20 * log (sample / SAMPLE_RANGE) + 90
176 * Output range is 0-9000 (that is 0.0 - 90.0 dB).
177 * Normally 0dB is full scale, here it is shifted +90dB.
178 * The calculation is based on the results of a linear
179 * approximation tool written specifically for this problem
180 * by Andreas Zwirtes (radhard@gmx.de). The result has an
181 * accurracy of better than 2%. It is highly runtime optimized,
182 * the cascading if-clauses do an successive approximation on
183 * the input value. This avoids big lookup-tables and
184 * for-loops.
185 * Improved by Jvo Studer for errors < 0.2dB for critical
186 * range of -12dB to 0dB (78.0 to 90.0dB).
189 int calc_db (int isample)
191 /* return n+m*(isample-istart)/100 */
192 int n;
193 long m;
194 int istart;
196 if (isample < 2308) { /* Range 1-5 */
198 if (isample < 115) { /* Range 1-3 */
200 if (isample < 24) {
202 if (isample < 5) {
203 istart = 1; /* Range 1 */
204 n = 98;
205 m = 34950;
207 else {
208 istart = 5; /* Range 2 */
209 n = 1496;
210 m = 7168;
213 else {
214 istart = 24; /* Range 3 */
215 n = 2858;
216 m = 1498;
219 else { /* Range 4-5 */
221 if (isample < 534) {
222 istart = 114; /* Range 4 */
223 n = 4207;
224 m = 319;
226 else {
227 istart = 588; /* Range 5 */
228 n = 5583;
229 m = 69;
234 else { /* Range 6-9 */
236 if (isample < 12932) {
238 if (isample < 6394) {
239 istart = 2608; /* Range 6 */
240 n = 6832;
241 m = 21;
243 else {
244 istart = 7000; /* Range 7 */
245 n = 7682;
246 m = 9;
249 else {
251 if (isample < 22450) {
252 istart = 13000; /* Range 8 */
253 n = 8219;
254 m = 5;
256 else {
257 istart = 22636; /* Range 9 */
258 n = 8697;
259 m = 3;
264 return n + (m * (long)(isample - istart)) / 100L;
269 * A helper function for peak_meter_db2sample. Don't call it separately but
270 * use peak_meter_db2sample. If one or both of min and max are outside the
271 * range 0 <= min (or max) < 8961 the behaviour of this function is
272 * undefined. It may not return.
273 * @param int min - The minimum of the value range that is searched.
274 * @param int max - The maximum of the value range that is searched.
275 * @param int db - The value in dBfs * (-100) for which the according
276 * minimal peak sample is searched.
277 * @return int - A linear volume value with 0 <= value < MAX_PEAK
279 static int db_to_sample_bin_search(int min, int max, int db)
281 int test = min + (max - min) / 2;
283 if (min < max) {
284 if (calc_db(test) < db) {
285 test = db_to_sample_bin_search(test, max, db);
286 } else {
287 if (calc_db(test-1) > db) {
288 test = db_to_sample_bin_search(min, test, db);
292 return test;
296 * Converts a value representing dBfs to a linear
297 * scaled volume info as it is used by the MAS.
298 * An incredibly inefficiant function which is
299 * the vague inverse of calc_db. This really
300 * should be replaced by something better soon.
302 * @param int db - A dBfs * 100 value with
303 * -9000 < value <= 0
304 * @return int - The return value is in the range of
305 * 0 <= return value < MAX_PEAK
307 int peak_meter_db2sample(int db)
309 int retval = 0;
311 /* what is the maximum pseudo db value */
312 int max_peak_db = calc_db(MAX_PEAK - 1);
314 /* range check: db value to big */
315 if (max_peak_db + db < 0) {
316 retval = 0;
319 /* range check: db value too small */
320 else if (max_peak_db + db >= max_peak_db) {
321 retval = MAX_PEAK -1;
324 /* value in range: find the matching linear value */
325 else {
326 retval = db_to_sample_bin_search(0, MAX_PEAK, max_peak_db + db);
328 /* as this is a dirty function anyway, we want to adjust the
329 full scale hit manually to avoid users complaining that when
330 they adjust maximum for 0 dBfs and display it in percent it
331 shows 99%. That is due to precision loss and this is the
332 optical fix */
335 return retval;
339 * Set the min value for restriction of the value range.
340 * @param int newmin - depending whether dBfs is used
341 * newmin is a value in dBfs * 100 or in linear percent values.
342 * for dBfs: -9000 < newmin <= 0
343 * for linear: 0 <= newmin <= 100
345 void peak_meter_set_min(int newmin)
347 if (pm_use_dbfs) {
348 peak_meter_range_min = peak_meter_db2sample(newmin);
350 } else {
351 if (newmin < peak_meter_range_max) {
352 peak_meter_range_min = newmin * MAX_PEAK / 100;
356 pm_range = peak_meter_range_max - peak_meter_range_min;
358 /* Avoid division by zero. */
359 if (pm_range == 0) {
360 pm_range = 1;
363 pm_db_min = calc_db(peak_meter_range_min);
364 pm_db_range = pm_db_max - pm_db_min;
365 int i;
366 FOR_NB_SCREENS(i)
367 scales[i].db_scale_valid = false;
371 * Returns the minimum value of the range the meter
372 * displays. If the scale is set to dBfs it returns
373 * dBfs values * 100 or linear percent values.
374 * @return: using dBfs : -9000 < value <= 0
375 * using linear scale: 0 <= value <= 100
377 int peak_meter_get_min(void)
379 int retval = 0;
380 if (pm_use_dbfs) {
381 retval = calc_db(peak_meter_range_min) - calc_db(MAX_PEAK - 1);
382 } else {
383 retval = peak_meter_range_min * 100 / MAX_PEAK;
385 return retval;
389 * Set the max value for restriction of the value range.
390 * @param int newmax - depending wether dBfs is used
391 * newmax is a value in dBfs * 100 or in linear percent values.
392 * for dBfs: -9000 < newmax <= 0
393 * for linear: 0 <= newmax <= 100
395 void peak_meter_set_max(int newmax)
397 if (pm_use_dbfs) {
398 peak_meter_range_max = peak_meter_db2sample(newmax);
399 } else {
400 if (newmax > peak_meter_range_min) {
401 peak_meter_range_max = newmax * MAX_PEAK / 100;
405 pm_range = peak_meter_range_max - peak_meter_range_min;
407 /* Avoid division by zero. */
408 if (pm_range == 0) {
409 pm_range = 1;
412 pm_db_max = calc_db(peak_meter_range_max);
413 pm_db_range = pm_db_max - pm_db_min;
414 int i;
415 FOR_NB_SCREENS(i)
416 scales[i].db_scale_valid = false;
420 * Returns the minimum value of the range the meter
421 * displays. If the scale is set to dBfs it returns
422 * dBfs values * 100 or linear percent values
423 * @return: using dBfs : -9000 < value <= 0
424 * using linear scale: 0 <= value <= 100
426 int peak_meter_get_max(void)
428 int retval = 0;
429 if (pm_use_dbfs) {
430 retval = calc_db(peak_meter_range_max) - calc_db(MAX_PEAK - 1);
431 } else {
432 retval = peak_meter_range_max * 100 / MAX_PEAK;
434 return retval;
438 * Returns whether the meter is currently displaying dBfs or percent values.
439 * @return bool - true if the meter is displaying dBfs
440 false if the meter is displaying percent values.
442 bool peak_meter_get_use_dbfs(void)
444 return pm_use_dbfs;
448 * Specifies whether the values displayed are scaled
449 * as dBfs or as linear percent values.
450 * @param use - set to true for dBfs,
451 * set to false for linear scaling in percent
453 void peak_meter_set_use_dbfs(bool use)
455 int i;
456 pm_use_dbfs = use;
457 FOR_NB_SCREENS(i)
458 scales[i].db_scale_valid = false;
462 * Initialize the range of the meter. Only values
463 * that are in the range of [range_min ... range_max]
464 * are displayed.
465 * @param bool dbfs - set to true for dBfs,
466 * set to false for linear scaling in percent
467 * @param int range_min - Specifies the lower value of the range.
468 * Pass a value dBfs * 100 when dbfs is set to true.
469 * Pass a percent value when dbfs is set to false.
470 * @param int range_max - Specifies the upper 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.
474 void peak_meter_init_range( bool dbfs, int range_min, int range_max)
476 pm_use_dbfs = dbfs;
477 peak_meter_set_min(range_min);
478 peak_meter_set_max(range_max);
482 * Initialize the peak meter with all relevant values concerning times.
483 * @param int release - Set the maximum amount of pixels the meter is allowed
484 * to decrease with each redraw
485 * @param int hold - Select the time preset for the time the peak indicator
486 * is reset after a peak occurred. The preset values are
487 * stored in peak_time_out.
488 * @param int clip_hold - Select the time preset for the time the peak
489 * indicator is reset after a peak occurred. The preset
490 * values are stored in clip_time_out.
492 void peak_meter_init_times(int release, int hold, int clip_hold)
494 pm_peak_hold = hold;
495 pm_peak_release = release;
496 pm_clip_hold = clip_hold;
499 #ifdef HAVE_RECORDING
501 * Enable/disable clip counting
503 void pm_activate_clipcount(bool active)
505 pm_clipcount_active = active;
509 * Get clipping counter value
511 int pm_get_clipcount(void)
513 return pm_clipcount;
517 * Set clipping counter to zero (typically at start of recording or playback)
519 void pm_reset_clipcount(void)
521 pm_clipcount = 0;
523 #endif
526 * Set the source of the peak meter to playback or to
527 * record.
528 * @param: bool playback - If true playback peak meter is used.
529 * If false recording peak meter is used.
531 void peak_meter_playback(bool playback)
533 #ifdef SIMULATOR
534 (void)playback;
535 #elif CONFIG_CODEC == SWCODEC
536 pm_playback = playback;
537 #else
538 if (playback) {
539 pm_src_left = MAS_REG_DQPEAK_L;
540 pm_src_right = MAS_REG_DQPEAK_R;
541 } else {
542 pm_src_left = MAS_REG_QPEAK_L;
543 pm_src_right = MAS_REG_QPEAK_R;
545 #endif
548 #ifdef HAVE_RECORDING
549 static void set_trig_status(int new_state)
551 if (trig_status != new_state) {
552 trig_status = new_state;
553 if (trigger_listener != NULL) {
554 trigger_listener(trig_status);
559 #endif
562 * Reads peak values from the MAS, and detects clips. The
563 * values are stored in pm_max_left pm_max_right for later
564 * evauluation. Consecutive calls to peak_meter_peek detect
565 * that ocurred. This function could be used by a thread for
566 * busy reading the MAS.
568 void peak_meter_peek(void)
570 int left, right;
571 #ifdef HAVE_RECORDING
572 bool was_clipping = pm_clip_left || pm_clip_right;
573 #endif
574 /* read current values */
575 #if CONFIG_CODEC == SWCODEC
576 if (pm_playback)
577 pcm_calculate_peaks(&pm_cur_left, &pm_cur_right);
578 #ifdef HAVE_RECORDING
579 else
580 pcm_calculate_rec_peaks(&pm_cur_left, &pm_cur_right);
581 #endif
582 left = pm_cur_left;
583 right = pm_cur_right;
584 #else
585 #ifndef SIMULATOR
586 pm_cur_left = left = mas_codec_readreg(pm_src_left);
587 pm_cur_right = right = mas_codec_readreg(pm_src_right);
588 #else
589 pm_cur_left = left = 8000;
590 pm_cur_right = right = 9000;
591 #endif
592 #endif
594 /* check for clips
595 An clip is assumed when two consecutive readouts
596 of the volume are at full scale. This is proven
597 to be inaccurate in both ways: it may detect clips
598 when no clip occurred and it may fail to detect
599 a real clip. For software codecs, the peak is already
600 the max of a bunch of samples, so use one max value
601 or you fail to detect clipping! */
602 #if CONFIG_CODEC == SWCODEC
603 if (left == MAX_PEAK - 1) {
604 #else
605 if ((left == pm_max_left) &&
606 (left == MAX_PEAK - 1)) {
607 #endif
608 pm_clip_left = true;
609 pm_clip_timeout_l =
610 current_tick + clip_time_out[pm_clip_hold];
613 #if CONFIG_CODEC == SWCODEC
614 if (right == MAX_PEAK - 1) {
615 #else
616 if ((right == pm_max_right) &&
617 (right == MAX_PEAK - 1)) {
618 #endif
619 pm_clip_right = true;
620 pm_clip_timeout_r =
621 current_tick + clip_time_out[pm_clip_hold];
624 #ifdef HAVE_RECORDING
625 if(!was_clipping && (pm_clip_left || pm_clip_right))
627 if(pm_clipcount_active)
628 pm_clipcount++;
630 #endif
632 /* peaks are searched -> we have to find the maximum. When
633 many calls of peak_meter_peek the maximum value will be
634 stored in pm_max_xxx. This maximum is reset by the
635 functions peak_meter_read_x. */
636 pm_max_left = MAX(pm_max_left, left);
637 pm_max_right = MAX(pm_max_right, right);
639 #ifdef HAVE_RECORDING
640 #if CONFIG_CODEC == SWCODEC
641 /* Ignore any unread peakmeter data */
642 #define MAX_DROP_TIME HZ/7 /* this value may need tweaking. Increase if you are
643 getting trig events when you shouldn't with
644 trig_stp_hold = 0 */
645 if (!trig_stp_hold)
646 trig_stp_hold = MAX_DROP_TIME;
647 #endif
649 switch (trig_status) {
650 case TRIG_READY:
651 /* no more changes, if trigger was activated as release trigger */
652 /* threshold exceeded? */
653 if ((left > trig_strt_threshold)
654 || (right > trig_strt_threshold)) {
655 /* reset trigger duration */
656 trig_hightime = current_tick;
658 /* reset dropout duration */
659 trig_lowtime = current_tick;
661 if (trig_strt_duration)
662 set_trig_status(TRIG_STEADY);
663 else
664 /* if trig_duration is set to 0 the user wants to start
665 recording immediately */
666 set_trig_status(TRIG_GO);
668 break;
670 case TRIG_STEADY:
671 case TRIG_RETRIG:
672 /* trigger duration exceeded */
673 if (current_tick - trig_hightime > trig_strt_duration) {
674 set_trig_status(TRIG_GO);
675 } else {
676 /* threshold exceeded? */
677 if ((left > trig_strt_threshold)
678 || (right > trig_strt_threshold)) {
679 /* reset lowtime */
680 trig_lowtime = current_tick;
682 /* volume is below threshold */
683 else {
684 /* dropout occurred? */
685 if (current_tick - trig_lowtime > trig_strt_dropout){
686 if (trig_status == TRIG_STEADY){
687 set_trig_status(TRIG_READY);
689 /* trig_status == TRIG_RETRIG */
690 else {
691 /* the gap has already expired */
692 trig_lowtime = current_tick - trig_rstrt_gap - 1;
693 set_trig_status(TRIG_POSTREC);
698 break;
700 case TRIG_GO:
701 case TRIG_CONTINUE:
702 /* threshold exceeded? */
703 if ((left > trig_stp_threshold)
704 || (right > trig_stp_threshold)) {
705 /* restart hold time countdown */
706 trig_lowtime = current_tick;
707 #if CONFIG_CODEC == SWCODEC
708 } else if (current_tick - trig_lowtime > MAX_DROP_TIME){
709 #else
710 } else {
711 #endif
712 set_trig_status(TRIG_POSTREC);
713 trig_hightime = current_tick;
715 break;
717 case TRIG_POSTREC:
718 /* gap time expired? */
719 if (current_tick - trig_lowtime > trig_rstrt_gap){
720 /* start threshold exceeded? */
721 if ((left > trig_strt_threshold)
722 || (right > trig_strt_threshold)) {
724 set_trig_status(TRIG_RETRIG);
725 trig_hightime = current_tick;
726 trig_lowtime = current_tick;
728 else
730 /* stop threshold exceeded */
731 if ((left > trig_stp_threshold)
732 || (right > trig_stp_threshold)) {
733 if (current_tick - trig_hightime > trig_stp_hold){
734 trig_lowtime = current_tick;
735 set_trig_status(TRIG_CONTINUE);
736 } else {
737 trig_lowtime = current_tick - trig_rstrt_gap - 1;
741 /* below any threshold */
742 else {
743 if (current_tick - trig_lowtime > trig_stp_hold){
744 set_trig_status(TRIG_READY);
745 } else {
746 trig_hightime = current_tick;
751 /* still within the gap time */
752 else {
753 /* stop threshold exceeded */
754 if ((left > trig_stp_threshold)
755 || (right > trig_stp_threshold)) {
756 set_trig_status(TRIG_CONTINUE);
757 trig_lowtime = current_tick;
760 /* hold time expired */
761 else if (current_tick - trig_lowtime > trig_stp_hold){
762 trig_hightime = current_tick;
763 trig_lowtime = current_tick;
764 set_trig_status(TRIG_READY);
767 break;
769 #if CONFIG_CODEC == SWCODEC
770 /* restore stop hold value */
771 if (trig_stp_hold == MAX_DROP_TIME)
772 trig_stp_hold = 0;
773 #endif
774 #endif
775 /* check levels next time peakmeter drawn */
776 level_check = true;
777 #ifdef PM_DEBUG
778 peek_calls++;
779 #endif
783 * Reads out the peak volume of the left channel.
784 * @return int - The maximum value that has been detected
785 * since the last call of peak_meter_read_l. The value
786 * is in the range 0 <= value < MAX_PEAK.
788 static int peak_meter_read_l(void)
790 /* pm_max_left contains the maximum of all peak values that were read
791 by peak_meter_peek since the last call of peak_meter_read_l */
792 int retval = pm_max_left;
794 #ifdef HAVE_AGC
795 /* store max peak value for peak_meter_get_peakhold_x readout */
796 pm_peakhold_left = MAX(pm_max_left, pm_peakhold_left);
797 #endif
798 #ifdef PM_DEBUG
799 peek_calls = 0;
800 #endif
801 /* reset pm_max_left so that subsequent calls of peak_meter_peek don't
802 get fooled by an old maximum value */
803 pm_max_left = pm_cur_left;
805 #ifdef SIMULATOR
806 srand(current_tick);
807 retval = rand()%MAX_PEAK;
808 #endif
810 return retval;
814 * Reads out the peak volume of the right channel.
815 * @return int - The maximum value that has been detected
816 * since the last call of peak_meter_read_l. The value
817 * is in the range 0 <= value < MAX_PEAK.
819 static int peak_meter_read_r(void)
821 /* peak_meter_r contains the maximum of all peak values that were read
822 by peak_meter_peek since the last call of peak_meter_read_r */
823 int retval = pm_max_right;
825 #ifdef HAVE_AGC
826 /* store max peak value for peak_meter_get_peakhold_x readout */
827 pm_peakhold_right = MAX(pm_max_right, pm_peakhold_right);
828 #endif
829 #ifdef PM_DEBUG
830 peek_calls = 0;
831 #endif
832 /* reset pm_max_right so that subsequent calls of peak_meter_peek don't
833 get fooled by an old maximum value */
834 pm_max_right = pm_cur_right;
836 #ifdef SIMULATOR
837 srand(current_tick);
838 retval = rand()%MAX_PEAK;
839 #endif
841 return retval;
844 #ifdef HAVE_AGC
846 * Reads out the current peak-hold values since the last call.
847 * This is used by the histogram feature in the recording screen.
848 * Values are in the range 0 <= peak_x < MAX_PEAK. MAX_PEAK is typ 32767.
850 void peak_meter_get_peakhold(int *peak_left, int *peak_right)
852 if (peak_left)
853 *peak_left = pm_peakhold_left;
854 if (peak_right)
855 *peak_right = pm_peakhold_right;
856 pm_peakhold_left = 0;
857 pm_peakhold_right = 0;
859 #endif
862 * Reset the detected clips. This method is for
863 * use by the user interface.
864 * @param int unused - This parameter was added to
865 * make the function compatible with set_int
867 void peak_meter_set_clip_hold(int time)
869 pm_clip_eternal = false;
871 if (time <= 0) {
872 pm_clip_left = false;
873 pm_clip_right = false;
874 pm_clip_eternal = true;
879 * Scales a peak value as read from the MAS to the range of meterwidth.
880 * The scaling is performed according to the scaling method (dBfs / linear)
881 * and the range (peak_meter_range_min .. peak_meter_range_max).
882 * @param unsigned short val - The volume value. Range: 0 <= val < MAX_PEAK
883 * @param int meterwidht - The widht of the meter in pixel
884 * @return unsigned short - A value 0 <= return value <= meterwidth
886 unsigned short peak_meter_scale_value(unsigned short val, int meterwidth)
888 int retval;
890 if (val <= peak_meter_range_min) {
891 return 0;
894 if (val >= peak_meter_range_max) {
895 return meterwidth;
898 retval = val;
900 /* different scaling is used for dBfs and linear percent */
901 if (pm_use_dbfs) {
903 /* scale the samples dBfs */
904 retval = (calc_db(retval) - pm_db_min) * meterwidth / pm_db_range;
907 /* Scale for linear percent display */
908 else
910 /* scale the samples */
911 retval = ((retval - peak_meter_range_min) * meterwidth)
912 / pm_range;
914 return retval;
916 void peak_meter_screen(struct screen *display, int x, int y, int height)
918 peak_meter_draw(display, &scales[display->screen_type], x, y,
919 display->width - x, height);
922 * Draws a peak meter in the specified size at the specified position.
923 * @param int x - The x coordinate.
924 * Make sure that 0 <= x and x + width < display->width
925 * @param int y - The y coordinate.
926 * Make sure that 0 <= y and y + height < display->height
927 * @param int width - The width of the peak meter. Note that for display
928 * of clips a 3 pixel wide area is used ->
929 * width > 3
930 * @param int height - The height of the peak meter. height > 3
932 void peak_meter_draw(struct screen *display, struct meter_scales *scales,
933 int x, int y, int width, int height)
935 static int left_level = 0, right_level = 0;
936 int left = 0, right = 0;
937 int meterwidth = width - 3;
938 int i, delta;
939 static long peak_release_tick = 0;
941 #ifdef PM_DEBUG
942 static long pm_tick = 0;
943 int tmp = peek_calls;
944 #endif
946 /* if disabled only draw the peak meter */
947 if (peak_meter_enabled) {
950 if (level_check){
951 /* only read the volume info from MAS if peek since last read*/
952 left_level = peak_meter_read_l();
953 right_level = peak_meter_read_r();
954 level_check = false;
957 /* scale the samples dBfs */
958 left = peak_meter_scale_value(left_level, meterwidth);
959 right = peak_meter_scale_value(right_level, meterwidth);
961 /*if the scale has changed -> recalculate the scale
962 (The scale becomes invalid when the range changed.) */
963 if (!scales->db_scale_valid){
965 if (pm_use_dbfs) {
966 db_scale_count = DB_SCALE_SRC_VALUES_SIZE;
967 for (i = 0; i < db_scale_count; i++){
968 /* find the real x-coords for predefined interesting
969 dBfs values. These only are recalculated when the
970 scaling of the meter changed. */
971 scales->db_scale_lcd_coord[i] =
972 peak_meter_scale_value(
973 db_scale_src_values[i],
974 meterwidth - 1);
978 /* when scaling linear we simly make 10% steps */
979 else {
980 db_scale_count = 10;
981 for (i = 0; i < db_scale_count; i++) {
982 scales->db_scale_lcd_coord[i] =
983 (i * (MAX_PEAK / 10) - peak_meter_range_min) *
984 meterwidth / pm_range;
988 /* mark scale valid to avoid recalculating dBfs values
989 of the scale. */
990 scales->db_scale_valid = true;
993 /* apply release */
994 delta = current_tick - peak_release_tick;
995 peak_release_tick = current_tick;
996 left = MAX(left , scales->last_left - delta * pm_peak_release);
997 right = MAX(right, scales->last_right - delta * pm_peak_release);
999 /* reset max values after timeout */
1000 if (TIME_AFTER(current_tick, scales->pm_peak_timeout_l)){
1001 scales->pm_peak_left = 0;
1004 if (TIME_AFTER(current_tick, scales->pm_peak_timeout_r)){
1005 scales->pm_peak_right = 0;
1008 if (!pm_clip_eternal) {
1009 if (pm_clip_left &&
1010 TIME_AFTER(current_tick, pm_clip_timeout_l)){
1011 pm_clip_left = false;
1014 if (pm_clip_right &&
1015 TIME_AFTER(current_tick, pm_clip_timeout_r)){
1016 pm_clip_right = false;
1020 /* check for new max values */
1021 if (left > scales->pm_peak_left) {
1022 scales->pm_peak_left = left - 1;
1023 scales->pm_peak_timeout_l =
1024 current_tick + peak_time_out[pm_peak_hold];
1027 if (right > scales->pm_peak_right) {
1028 scales->pm_peak_right = right - 1;
1029 scales->pm_peak_timeout_r =
1030 current_tick + peak_time_out[pm_peak_hold];
1034 /* draw the peak meter */
1035 display->set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1036 display->fillrect(x, y, width, height);
1037 display->set_drawmode(DRMODE_SOLID);
1039 /* draw left */
1040 display->fillrect (x, y, left, height / 2 - 2 );
1041 if (scales->pm_peak_left > 0) {
1042 display->vline(x + scales->pm_peak_left, y, y + height / 2 - 2 );
1044 if (pm_clip_left) {
1045 display->fillrect(x + meterwidth, y, 3, height / 2 - 1);
1048 /* draw right */
1049 display->fillrect(x, y + height / 2 + 1, right, height / 2 - 2);
1050 if (scales->pm_peak_right > 0) {
1051 display->vline( x + scales->pm_peak_right, y + height / 2, y + height - 2);
1053 if (pm_clip_right) {
1054 display->fillrect(x + meterwidth, y + height / 2, 3, height / 2 - 1);
1057 /* draw scale end */
1058 display->vline(x + meterwidth, y, y + height - 2);
1060 /* draw dots for scale marks */
1061 for (i = 0; i < db_scale_count; i++) {
1062 /* The x-coordinates of interesting scale mark points
1063 have been calculated before */
1064 display->drawpixel(x + scales->db_scale_lcd_coord[i],
1065 y + height / 2 - 1);
1068 #ifdef HAVE_RECORDING
1070 #ifdef HAVE_BACKLIGHT
1071 /* cliplight */
1072 if ((pm_clip_left || pm_clip_right) &&
1073 global_settings.cliplight &&
1074 #if CONFIG_CODEC == SWCODEC
1075 !pm_playback)
1076 #else
1077 !(audio_status() & (AUDIO_STATUS_PLAY | AUDIO_STATUS_ERROR)))
1078 #endif
1080 /* if clipping, cliplight setting on and in recording screen */
1081 if (global_settings.cliplight <= 2)
1083 /* turn on main unit light if setting set to main or both*/
1084 backlight_on();
1086 #ifdef HAVE_REMOTE_LCD
1087 if (global_settings.cliplight >= 2)
1089 /* turn remote light unit on if setting set to remote or both */
1090 remote_backlight_on();
1092 #endif /* HAVE_REMOTE_LCD */
1094 #endif /* HAVE_BACKLIGHT */
1096 if (trig_status != TRIG_OFF) {
1097 int start_trigx, stop_trigx, ycenter;
1099 display->set_drawmode(DRMODE_SOLID);
1100 ycenter = y + height / 2;
1101 /* display threshold value */
1102 start_trigx = x+peak_meter_scale_value(trig_strt_threshold,meterwidth);
1103 display->vline(start_trigx, ycenter - 2, ycenter);
1104 start_trigx ++;
1105 if (start_trigx < display->width ) display->drawpixel(start_trigx, ycenter - 1);
1107 stop_trigx = x + peak_meter_scale_value(trig_stp_threshold,meterwidth);
1108 display->vline(stop_trigx, ycenter - 2, ycenter);
1109 if (stop_trigx > 0) display->drawpixel(stop_trigx - 1, ycenter - 1);
1111 #endif /*HAVE_RECORDING*/
1113 #ifdef PM_DEBUG
1114 /* display a bar to show how many calls to peak_meter_peek
1115 have ocurred since the last display */
1116 display->set_drawmode(DRMODE_COMPLEMENT);
1117 display->fillrect(x, y, tmp, 3);
1119 if (tmp < PEEKS_PER_DRAW_SIZE) {
1120 peeks_per_redraw[tmp]++;
1123 tmp = current_tick - pm_tick;
1124 if (tmp < TICKS_PER_DRAW_SIZE ){
1125 ticks_per_redraw[tmp] ++;
1128 /* display a bar to show how many ticks have passed since
1129 the last redraw */
1130 display->fillrect(x, y + height / 2, current_tick - pm_tick, 2);
1131 pm_tick = current_tick;
1132 #endif
1134 scales->last_left = left;
1135 scales->last_right = right;
1137 display->set_drawmode(DRMODE_SOLID);
1140 #ifdef HAVE_RECORDING
1142 * Defines the parameters of the trigger. After these parameters are defined
1143 * the trigger can be started either by peak_meter_attack_trigger or by
1144 * peak_meter_release_trigger. Note that you can pass either linear (%) or
1145 * logarithmic (db) values to the thresholds. Positive values are intepreted as
1146 * percent (0 is 0% .. 100 is 100%). Negative values are interpreted as db.
1147 * To avoid ambiguosity of the value 0 the negative values are shifted by -1.
1148 * Thus -75 is -74db .. -1 is 0db.
1149 * @param start_threshold - The threshold used for attack trigger. Negative
1150 * values are interpreted as db -1, positive as %.
1151 * @param start_duration - The minimum time span within which start_threshold
1152 * must be exceeded to fire the attack trigger.
1153 * @param start_dropout - The maximum time span the level may fall below
1154 * start_threshold without releasing the attack trigger.
1155 * @param stop_threshold - The threshold the volume must fall below to release
1156 * the release trigger.Negative values are
1157 * interpreted as db -1, positive as %.
1158 * @param stop_hold - The minimum time the volume must fall below the
1159 * stop_threshold to release the trigger.
1160 * @param
1162 void peak_meter_define_trigger(
1163 int start_threshold,
1164 long start_duration,
1165 long start_dropout,
1166 int stop_threshold,
1167 long stop_hold_time,
1168 long restart_gap
1171 if (start_threshold < 0) {
1172 /* db */
1173 if (start_threshold < -89) {
1174 trig_strt_threshold = 0;
1175 } else {
1176 trig_strt_threshold =peak_meter_db2sample((start_threshold+1)*100);
1178 } else {
1179 /* linear percent */
1180 trig_strt_threshold = start_threshold * MAX_PEAK / 100;
1182 trig_strt_duration = start_duration;
1183 trig_strt_dropout = start_dropout;
1184 if (stop_threshold < 0) {
1185 /* db */
1186 trig_stp_threshold = peak_meter_db2sample((stop_threshold + 1) * 100);
1187 } else {
1188 /* linear percent */
1189 trig_stp_threshold = stop_threshold * MAX_PEAK / 100;
1191 trig_stp_hold = stop_hold_time;
1192 trig_rstrt_gap = restart_gap;
1196 * Enables or disables the trigger.
1197 * @param on - If true the trigger is turned on.
1199 void peak_meter_trigger(bool on)
1201 /* don't use set_trigger here as that would fire an undesired event */
1202 trig_status = on ? TRIG_READY : TRIG_OFF;
1206 * Registers the listener function that listenes on trig_status changes.
1207 * @param listener - The function that is called with each change of
1208 * trig_status. May be set to NULL if no callback is desired.
1210 void peak_meter_set_trigger_listener(void (*listener)(int status))
1212 trigger_listener = listener;
1216 * Fetches the status of the trigger.
1217 * TRIG_OFF: the trigger is inactive
1218 * TRIG_RELEASED: The volume level is below the threshold
1219 * TRIG_ACTIVATED: The volume level has exceeded the threshold, but the trigger
1220 * hasn't been fired yet.
1221 * TRIG_FIRED: The volume exceeds the threshold
1223 * To activate the trigger call either peak_meter_attack_trigger or
1224 * peak_meter_release_trigger. To turn the trigger off call
1225 * peak_meter_trigger_off.
1227 int peak_meter_trigger_status(void)
1229 return trig_status; /* & TRIG_PIT_MASK;*/
1232 void peak_meter_draw_trig(int xpos[], int ypos[], int trig_width[], int nb_screens)
1234 int barstart[NB_SCREENS];
1235 int barend[NB_SCREENS];
1236 int icon;
1237 int ixpos[NB_SCREENS];
1238 int i;
1239 int trigbar_width[NB_SCREENS];
1241 FOR_NB_SCREENS(i)
1242 trigbar_width[i] = (trig_width[i] - (2 * (ICON_PLAY_STATE_WIDTH + 1)));
1244 switch (trig_status) {
1246 case TRIG_READY:
1247 FOR_NB_SCREENS(i){
1248 barstart[i] = 0;
1249 barend[i] = 0;
1251 icon = Icon_Stop;
1252 FOR_NB_SCREENS(i)
1253 ixpos[i] = xpos[i];
1254 break;
1256 case TRIG_STEADY:
1257 case TRIG_RETRIG:
1258 FOR_NB_SCREENS(i)
1260 barstart[i] = 0;
1261 barend[i] = (trig_strt_duration == 0) ? trigbar_width[i] :
1262 trigbar_width[i] *
1263 (current_tick - trig_hightime) / trig_strt_duration;
1265 icon = Icon_Stop;
1266 FOR_NB_SCREENS(i)
1267 ixpos[i] = xpos[i];
1268 break;
1270 case TRIG_GO:
1271 case TRIG_CONTINUE:
1272 FOR_NB_SCREENS(i)
1274 barstart[i] = trigbar_width[i];
1275 barend[i] = trigbar_width[i];
1277 icon = Icon_Record;
1278 FOR_NB_SCREENS(i)
1279 ixpos[i] = xpos[i]+ trig_width[i] - ICON_PLAY_STATE_WIDTH;
1280 break;
1282 case TRIG_POSTREC:
1283 FOR_NB_SCREENS(i)
1285 barstart[i] = (trig_stp_hold == 0) ? 0 :
1286 trigbar_width[i] - trigbar_width[i] *
1287 (current_tick - trig_lowtime) / trig_stp_hold;
1288 barend[i] = trigbar_width[i];
1290 icon = Icon_Record;
1291 FOR_NB_SCREENS(i)
1292 ixpos[i] = xpos[i] + trig_width[i] - ICON_PLAY_STATE_WIDTH;
1293 break;
1295 default:
1296 return;
1299 for(i = 0; i < nb_screens; i++)
1301 gui_scrollbar_draw(&screens[i], xpos[i] + ICON_PLAY_STATE_WIDTH + 1,
1302 ypos[i] + 1, trigbar_width[i], TRIG_HEIGHT - 2,
1303 trigbar_width[i], barstart[i], barend[i],
1304 HORIZONTAL);
1306 screens[i].mono_bitmap(bitmap_icons_7x8[icon], ixpos[i], ypos[i],
1307 ICON_PLAY_STATE_WIDTH, STATUSBAR_HEIGHT);
1310 #endif
1312 int peak_meter_draw_get_btn(int x, int y[], int height, int nb_screens)
1314 int button = BUTTON_NONE;
1315 long next_refresh = current_tick;
1316 long next_big_refresh = current_tick + HZ / 10;
1317 int i;
1318 #ifndef SIMULATOR
1319 bool highperf = !ata_disk_is_active();
1320 #else
1321 bool highperf = false;
1322 #endif
1323 bool dopeek = true;
1325 while (TIME_BEFORE(current_tick, next_big_refresh)) {
1326 button = get_action(CONTEXT_RECSCREEN, TIMEOUT_NOBLOCK);
1327 if (button != BUTTON_NONE) {
1328 break;
1330 if (dopeek) { /* Peek only once per refresh when disk is */
1331 peak_meter_peek(); /* spinning, but as often as possible */
1332 dopeek = highperf; /* otherwise. */
1333 yield();
1334 } else {
1335 sleep(0); /* Sleep until end of current tick. */
1337 if (TIME_AFTER(current_tick, next_refresh)) {
1338 for(i = 0; i < nb_screens; i++)
1340 peak_meter_screen(&screens[i], x, y[i], height);
1341 screens[i].update_rect(x, y[i], screens[i].width - x, height);
1343 next_refresh += HZ / PEAK_METER_FPS;
1344 dopeek = true;
1348 return button;
1351 #ifdef PM_DEBUG
1352 static void peak_meter_clear_histogram(void)
1354 int i = 0;
1355 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1356 ticks_per_redraw[i] = (unsigned int)0;
1359 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1360 peeks_per_redraw[i] = (unsigned int)0;
1364 bool peak_meter_histogram(void)
1366 int i;
1367 int btn = BUTTON_NONE;
1368 while ((btn & BUTTON_OFF) != BUTTON_OFF )
1370 unsigned int max = 0;
1371 int y = 0;
1372 int x = 0;
1373 screens[0].clear_display();
1375 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1376 max = MAX(max, peeks_per_redraw[i]);
1379 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1380 x = peeks_per_redraw[i] * (LCD_WIDTH - 1)/ max;
1381 screens[0].hline(0, x, y + i);
1384 y = PEEKS_PER_DRAW_SIZE + 1;
1385 max = 0;
1387 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1388 max = MAX(max, ticks_per_redraw[i]);
1391 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1392 x = ticks_per_redraw[i] * (LCD_WIDTH - 1)/ max;
1393 screens[0].hline(0, x, y + i);
1395 screens[0].update();
1397 btn = button_get(true);
1398 if (btn == BUTTON_PLAY) {
1399 peak_meter_clear_histogram();
1402 return false;
1404 #endif