1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
22 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
23 #include <stdlib.h> /* sim uses rand for peakmeter simulation */
30 #include "scrollbar.h"
36 #include "peakmeter.h"
38 #include "screen_access.h"
40 #include "backlight.h"
44 #if CONFIG_CODEC == SWCODEC
48 #include "pcm_record.h"
51 static bool pm_playback
= true; /* selects between playback and recording peaks */
54 static struct meter_scales scales
[NB_SCREENS
];
56 #if !defined(SIMULATOR) && CONFIG_CODEC != SWCODEC
58 static int pm_src_left
= MAS_REG_DQPEAK_L
;
59 static int pm_src_right
= MAS_REG_DQPEAK_R
;
62 /* Current values and cumulation */
63 static int pm_cur_left
; /* current values (last peak_meter_peek) */
64 static int pm_cur_right
;
65 static int pm_max_left
; /* maximum values between peak meter draws */
66 static int pm_max_right
;
67 #if defined(HAVE_AGC) || defined(HAVE_RECORDING_HISTOGRAM)
68 static int pm_peakhold_left
; /* max. peak values between peakhold calls */
69 static int pm_peakhold_right
; /* used for AGC and histogram display */
73 static bool pm_clip_left
= false; /* when true a clip has occurred */
74 static bool pm_clip_right
= false;
75 static long pm_clip_timeout_l
; /* clip hold timeouts */
76 static long pm_clip_timeout_r
;
78 /* Temporarily en- / disables peak meter. This is especially for external
79 applications to detect if the peak_meter is in use and needs drawing at all */
80 static bool peak_meter_enabled
= true;
81 void peak_meter_enable(bool enable
)
83 peak_meter_enabled
= enable
;
88 static unsigned short peak_meter_range_min
; /* minimum of range in samples */
89 static unsigned short peak_meter_range_max
; /* maximum of range in samples */
90 static unsigned short pm_range
; /* range width in samples */
91 static bool pm_use_dbfs
= true; /* true if peakmeter displays dBfs */
92 static bool level_check
; /* true if peeked at peakmeter before drawing */
93 static unsigned short pm_db_min
= 0; /* minimum of range in 1/100 dB */
94 static unsigned short pm_db_max
= 9000; /* maximum of range in 1/100 dB */
95 static unsigned short pm_db_range
= 9000; /* range width in 1/100 dB */
96 /* Timing behaviour */
97 static int pm_peak_hold
= 1; /* peak hold timeout index */
98 static int pm_peak_release
= 8; /* peak release in units per read */
99 static int pm_clip_hold
= 16; /* clip hold timeout index */
100 static bool pm_clip_eternal
= false; /* true if clip timeout is disabled */
102 #ifdef HAVE_RECORDING
103 static unsigned short trig_strt_threshold
;
104 static long trig_strt_duration
;
105 static long trig_strt_dropout
;
106 static unsigned short trig_stp_threshold
;
107 static long trig_stp_hold
;
108 static long trig_rstrt_gap
;
110 /* point in time when the threshold was exceeded */
111 static long trig_hightime
;
113 /* point in time when the volume fell below the threshold*/
114 static long trig_lowtime
;
116 /* The output value of the trigger. See TRIG_XXX constants for valid values */
117 static int trig_status
= TRIG_OFF
;
119 static void (*trigger_listener
)(int) = NULL
;
121 /* clipping counter (only used for recording) */
122 static unsigned int pm_clipcount
= 0; /* clipping count */
123 static bool pm_clipcount_active
= false; /* counting or not */
128 static int peek_calls
= 0;
130 #define PEEKS_PER_DRAW_SIZE 40
131 static unsigned int peeks_per_redraw
[PEEKS_PER_DRAW_SIZE
];
133 #define TICKS_PER_DRAW_SIZE 20
134 static unsigned int ticks_per_redraw
[TICKS_PER_DRAW_SIZE
];
137 static void peak_meter_draw(struct screen
*display
, struct meter_scales
*meter_scales
,
138 int x
, int y
, int width
, int height
);
140 /* time out values for max */
141 static const short peak_time_out
[] = {
142 0 * HZ
, HZ
/ 5, 30, HZ
/ 2, HZ
, 2 * HZ
,
143 3 * HZ
, 4 * HZ
, 5 * HZ
, 6 * HZ
, 7 * HZ
, 8 * HZ
,
144 9 * HZ
, 10 * HZ
, 15 * HZ
, 20 * HZ
, 30 * HZ
, 60 * HZ
147 /* time out values for clip */
148 static const long clip_time_out
[] = {
149 0 * HZ
, 1 * HZ
, 2 * HZ
, 3 * HZ
, 4 * HZ
, 5 * HZ
,
150 6 * HZ
, 7 * HZ
, 8 * HZ
, 9 * HZ
, 10 * HZ
, 15 * HZ
,
151 20 * HZ
, 25 * HZ
, 30 * HZ
, 45 * HZ
, 60 * HZ
, 90 * HZ
,
152 120 * HZ
, 180 * HZ
, 300 * HZ
, 600L * HZ
, 1200L * HZ
,
153 2700L * HZ
, 5400L * HZ
156 /* precalculated peak values that represent magical
157 dBfs values. Used to draw the scale */
158 static const short db_scale_src_values
[DB_SCALE_SRC_VALUES_SIZE
] = {
173 static int db_scale_count
= DB_SCALE_SRC_VALUES_SIZE
;
176 * Calculates dB Value for the peak meter, uses peak value as input
177 * @param int sample - The input value
178 * Make sure that 0 <= value < SAMPLE_RANGE
180 * @return int - The 2 digit fixed point result of the euation
181 * 20 * log (sample / SAMPLE_RANGE) + 90
182 * Output range is 0-9000 (that is 0.0 - 90.0 dB).
183 * Normally 0dB is full scale, here it is shifted +90dB.
184 * The calculation is based on the results of a linear
185 * approximation tool written specifically for this problem
186 * by Andreas Zwirtes (radhard@gmx.de). The result has an
187 * accurracy of better than 2%. It is highly runtime optimized,
188 * the cascading if-clauses do an successive approximation on
189 * the input value. This avoids big lookup-tables and
191 * Improved by Jvo Studer for errors < 0.2dB for critical
192 * range of -12dB to 0dB (78.0 to 90.0dB).
195 int calc_db (int isample
)
197 /* return n+m*(isample-istart)/100 */
202 if (isample
< 2308) { /* Range 1-5 */
204 if (isample
< 115) { /* Range 1-3 */
209 istart
= 1; /* Range 1 */
214 istart
= 5; /* Range 2 */
220 istart
= 24; /* Range 3 */
225 else { /* Range 4-5 */
228 istart
= 114; /* Range 4 */
233 istart
= 588; /* Range 5 */
240 else { /* Range 6-9 */
242 if (isample
< 12932) {
244 if (isample
< 6394) {
245 istart
= 2608; /* Range 6 */
250 istart
= 7000; /* Range 7 */
257 if (isample
< 22450) {
258 istart
= 13000; /* Range 8 */
263 istart
= 22636; /* Range 9 */
270 return n
+ (m
* (long)(isample
- istart
)) / 100L;
275 * A helper function for peak_meter_db2sample. Don't call it separately but
276 * use peak_meter_db2sample. If one or both of min and max are outside the
277 * range 0 <= min (or max) < 8961 the behaviour of this function is
278 * undefined. It may not return.
279 * @param int min - The minimum of the value range that is searched.
280 * @param int max - The maximum of the value range that is searched.
281 * @param int db - The value in dBfs * (-100) for which the according
282 * minimal peak sample is searched.
283 * @return int - A linear volume value with 0 <= value < MAX_PEAK
285 static int db_to_sample_bin_search(int min
, int max
, int db
)
287 int test
= min
+ (max
- min
) / 2;
290 if (calc_db(test
) < db
) {
291 test
= db_to_sample_bin_search(test
, max
, db
);
293 if (calc_db(test
-1) > db
) {
294 test
= db_to_sample_bin_search(min
, test
, db
);
302 * Converts a value representing dBfs to a linear
303 * scaled volume info as it is used by the MAS.
304 * An incredibly inefficiant function which is
305 * the vague inverse of calc_db. This really
306 * should be replaced by something better soon.
308 * @param int db - A dBfs * 100 value with
310 * @return int - The return value is in the range of
311 * 0 <= return value < MAX_PEAK
313 int peak_meter_db2sample(int db
)
317 /* what is the maximum pseudo db value */
318 int max_peak_db
= calc_db(MAX_PEAK
- 1);
320 /* range check: db value to big */
321 if (max_peak_db
+ db
< 0) {
325 /* range check: db value too small */
326 else if (max_peak_db
+ db
>= max_peak_db
) {
327 retval
= MAX_PEAK
-1;
330 /* value in range: find the matching linear value */
332 retval
= db_to_sample_bin_search(0, MAX_PEAK
, max_peak_db
+ db
);
334 /* as this is a dirty function anyway, we want to adjust the
335 full scale hit manually to avoid users complaining that when
336 they adjust maximum for 0 dBfs and display it in percent it
337 shows 99%. That is due to precision loss and this is the
345 * Set the min value for restriction of the value range.
346 * @param int newmin - depending whether dBfs is used
347 * newmin is a value in dBfs * 100 or in linear percent values.
348 * for dBfs: -9000 < newmin <= 0
349 * for linear: 0 <= newmin <= 100
351 static void peak_meter_set_min(int newmin
)
354 peak_meter_range_min
= peak_meter_db2sample(newmin
);
357 if (newmin
< peak_meter_range_max
) {
358 peak_meter_range_min
= newmin
* MAX_PEAK
/ 100;
362 pm_range
= peak_meter_range_max
- peak_meter_range_min
;
364 /* Avoid division by zero. */
369 pm_db_min
= calc_db(peak_meter_range_min
);
370 pm_db_range
= pm_db_max
- pm_db_min
;
373 scales
[i
].db_scale_valid
= false;
377 * Returns the minimum value of the range the meter
378 * displays. If the scale is set to dBfs it returns
379 * dBfs values * 100 or linear percent values.
380 * @return: using dBfs : -9000 < value <= 0
381 * using linear scale: 0 <= value <= 100
383 int peak_meter_get_min(void)
387 retval
= calc_db(peak_meter_range_min
) - calc_db(MAX_PEAK
- 1);
389 retval
= peak_meter_range_min
* 100 / MAX_PEAK
;
395 * Set the max value for restriction of the value range.
396 * @param int newmax - depending wether dBfs is used
397 * newmax is a value in dBfs * 100 or in linear percent values.
398 * for dBfs: -9000 < newmax <= 0
399 * for linear: 0 <= newmax <= 100
401 static void peak_meter_set_max(int newmax
)
404 peak_meter_range_max
= peak_meter_db2sample(newmax
);
406 if (newmax
> peak_meter_range_min
) {
407 peak_meter_range_max
= newmax
* MAX_PEAK
/ 100;
411 pm_range
= peak_meter_range_max
- peak_meter_range_min
;
413 /* Avoid division by zero. */
418 pm_db_max
= calc_db(peak_meter_range_max
);
419 pm_db_range
= pm_db_max
- pm_db_min
;
422 scales
[i
].db_scale_valid
= false;
426 * Returns the minimum value of the range the meter
427 * displays. If the scale is set to dBfs it returns
428 * dBfs values * 100 or linear percent values
429 * @return: using dBfs : -9000 < value <= 0
430 * using linear scale: 0 <= value <= 100
432 int peak_meter_get_max(void)
436 retval
= calc_db(peak_meter_range_max
) - calc_db(MAX_PEAK
- 1);
438 retval
= peak_meter_range_max
* 100 / MAX_PEAK
;
444 * Returns whether the meter is currently displaying dBfs or percent values.
445 * @return bool - true if the meter is displaying dBfs
446 false if the meter is displaying percent values.
448 bool peak_meter_get_use_dbfs(void)
454 * Specifies whether the values displayed are scaled
455 * as dBfs or as linear percent values.
456 * @param use - set to true for dBfs,
457 * set to false for linear scaling in percent
459 void peak_meter_set_use_dbfs(bool use
)
464 scales
[i
].db_scale_valid
= false;
468 * Initialize the range of the meter. Only values
469 * that are in the range of [range_min ... range_max]
471 * @param bool dbfs - set to true for dBfs,
472 * set to false for linear scaling in percent
473 * @param int range_min - Specifies the lower 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.
476 * @param int range_max - Specifies the upper value of the range.
477 * Pass a value dBfs * 100 when dbfs is set to true.
478 * Pass a percent value when dbfs is set to false.
480 void peak_meter_init_range( bool dbfs
, int range_min
, int range_max
)
483 peak_meter_set_min(range_min
);
484 peak_meter_set_max(range_max
);
488 * Initialize the peak meter with all relevant values concerning times.
489 * @param int release - Set the maximum amount of pixels the meter is allowed
490 * to decrease with each redraw
491 * @param int hold - Select the time preset for the time the peak indicator
492 * is reset after a peak occurred. The preset values are
493 * stored in peak_time_out.
494 * @param int clip_hold - Select the time preset for the time the peak
495 * indicator is reset after a peak occurred. The preset
496 * values are stored in clip_time_out.
498 void peak_meter_init_times(int release
, int hold
, int clip_hold
)
501 pm_peak_release
= release
;
502 pm_clip_hold
= clip_hold
;
505 #ifdef HAVE_RECORDING
507 * Enable/disable clip counting
509 void pm_activate_clipcount(bool active
)
511 pm_clipcount_active
= active
;
515 * Get clipping counter value
517 int pm_get_clipcount(void)
523 * Set clipping counter to zero (typically at start of recording or playback)
525 void pm_reset_clipcount(void)
532 * Set the source of the peak meter to playback or to
534 * @param: bool playback - If true playback peak meter is used.
535 * If false recording peak meter is used.
537 void peak_meter_playback(bool playback
)
540 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
542 #elif CONFIG_CODEC == SWCODEC
543 pm_playback
= playback
;
546 pm_src_left
= MAS_REG_DQPEAK_L
;
547 pm_src_right
= MAS_REG_DQPEAK_R
;
549 pm_src_left
= MAS_REG_QPEAK_L
;
550 pm_src_right
= MAS_REG_QPEAK_R
;
553 /* reset the scales just in case recording and playback
554 use different viewport sizes. Normally we should be checking viewport
555 sizes every time but this will do for now */
557 scales
[i
].db_scale_valid
= false;
560 #ifdef HAVE_RECORDING
561 static void set_trig_status(int new_state
)
563 if (trig_status
!= new_state
) {
564 trig_status
= new_state
;
565 if (trigger_listener
!= NULL
) {
566 trigger_listener(trig_status
);
574 * Reads peak values from the MAS, and detects clips. The
575 * values are stored in pm_max_left pm_max_right for later
576 * evauluation. Consecutive calls to peak_meter_peek detect
577 * that ocurred. This function could be used by a thread for
578 * busy reading the MAS.
580 void peak_meter_peek(void)
583 #ifdef HAVE_RECORDING
584 bool was_clipping
= pm_clip_left
|| pm_clip_right
;
586 /* read current values */
587 #if CONFIG_CODEC == SWCODEC
589 pcm_calculate_peaks(&pm_cur_left
, &pm_cur_right
);
590 #ifdef HAVE_RECORDING
592 pcm_calculate_rec_peaks(&pm_cur_left
, &pm_cur_right
);
595 right
= pm_cur_right
;
597 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
598 pm_cur_left
= left
= mas_codec_readreg(pm_src_left
);
599 pm_cur_right
= right
= mas_codec_readreg(pm_src_right
);
601 pm_cur_left
= left
= 8000;
602 pm_cur_right
= right
= 9000;
607 An clip is assumed when two consecutive readouts
608 of the volume are at full scale. This is proven
609 to be inaccurate in both ways: it may detect clips
610 when no clip occurred and it may fail to detect
611 a real clip. For software codecs, the peak is already
612 the max of a bunch of samples, so use one max value
613 or you fail to detect clipping! */
614 #if CONFIG_CODEC == SWCODEC
615 if (left
== MAX_PEAK
- 1) {
617 if ((left
== pm_max_left
) &&
618 (left
== MAX_PEAK
- 1)) {
622 current_tick
+ clip_time_out
[pm_clip_hold
];
625 #if CONFIG_CODEC == SWCODEC
626 if (right
== MAX_PEAK
- 1) {
628 if ((right
== pm_max_right
) &&
629 (right
== MAX_PEAK
- 1)) {
631 pm_clip_right
= true;
633 current_tick
+ clip_time_out
[pm_clip_hold
];
636 #ifdef HAVE_RECORDING
637 if(!was_clipping
&& (pm_clip_left
|| pm_clip_right
))
639 if(pm_clipcount_active
)
644 /* peaks are searched -> we have to find the maximum. When
645 many calls of peak_meter_peek the maximum value will be
646 stored in pm_max_xxx. This maximum is reset by the
647 functions peak_meter_read_x. */
648 pm_max_left
= MAX(pm_max_left
, left
);
649 pm_max_right
= MAX(pm_max_right
, right
);
651 #ifdef HAVE_RECORDING
652 #if CONFIG_CODEC == SWCODEC
653 /* Ignore any unread peakmeter data */
654 #define MAX_DROP_TIME HZ/7 /* this value may need tweaking. Increase if you are
655 getting trig events when you shouldn't with
658 trig_stp_hold
= MAX_DROP_TIME
;
661 switch (trig_status
) {
663 /* no more changes, if trigger was activated as release trigger */
664 /* threshold exceeded? */
665 if ((left
> trig_strt_threshold
)
666 || (right
> trig_strt_threshold
)) {
667 /* reset trigger duration */
668 trig_hightime
= current_tick
;
670 /* reset dropout duration */
671 trig_lowtime
= current_tick
;
673 if (trig_strt_duration
)
674 set_trig_status(TRIG_STEADY
);
676 /* if trig_duration is set to 0 the user wants to start
677 recording immediately */
678 set_trig_status(TRIG_GO
);
684 /* trigger duration exceeded */
685 if (current_tick
- trig_hightime
> trig_strt_duration
) {
686 set_trig_status(TRIG_GO
);
688 /* threshold exceeded? */
689 if ((left
> trig_strt_threshold
)
690 || (right
> trig_strt_threshold
)) {
692 trig_lowtime
= current_tick
;
694 /* volume is below threshold */
696 /* dropout occurred? */
697 if (current_tick
- trig_lowtime
> trig_strt_dropout
){
698 if (trig_status
== TRIG_STEADY
){
699 set_trig_status(TRIG_READY
);
701 /* trig_status == TRIG_RETRIG */
703 /* the gap has already expired */
704 trig_lowtime
= current_tick
- trig_rstrt_gap
- 1;
705 set_trig_status(TRIG_POSTREC
);
714 /* threshold exceeded? */
715 if ((left
> trig_stp_threshold
)
716 || (right
> trig_stp_threshold
)) {
717 /* restart hold time countdown */
718 trig_lowtime
= current_tick
;
719 #if CONFIG_CODEC == SWCODEC
720 } else if (current_tick
- trig_lowtime
> MAX_DROP_TIME
){
724 set_trig_status(TRIG_POSTREC
);
725 trig_hightime
= current_tick
;
730 /* gap time expired? */
731 if (current_tick
- trig_lowtime
> trig_rstrt_gap
){
732 /* start threshold exceeded? */
733 if ((left
> trig_strt_threshold
)
734 || (right
> trig_strt_threshold
)) {
736 set_trig_status(TRIG_RETRIG
);
737 trig_hightime
= current_tick
;
738 trig_lowtime
= current_tick
;
742 /* stop threshold exceeded */
743 if ((left
> trig_stp_threshold
)
744 || (right
> trig_stp_threshold
)) {
745 if (current_tick
- trig_hightime
> trig_stp_hold
){
746 trig_lowtime
= current_tick
;
747 set_trig_status(TRIG_CONTINUE
);
749 trig_lowtime
= current_tick
- trig_rstrt_gap
- 1;
753 /* below any threshold */
755 if (current_tick
- trig_lowtime
> trig_stp_hold
){
756 set_trig_status(TRIG_READY
);
758 trig_hightime
= current_tick
;
763 /* still within the gap time */
765 /* stop threshold exceeded */
766 if ((left
> trig_stp_threshold
)
767 || (right
> trig_stp_threshold
)) {
768 set_trig_status(TRIG_CONTINUE
);
769 trig_lowtime
= current_tick
;
772 /* hold time expired */
773 else if (current_tick
- trig_lowtime
> trig_stp_hold
){
774 trig_hightime
= current_tick
;
775 trig_lowtime
= current_tick
;
776 set_trig_status(TRIG_READY
);
781 #if CONFIG_CODEC == SWCODEC
782 /* restore stop hold value */
783 if (trig_stp_hold
== MAX_DROP_TIME
)
787 /* check levels next time peakmeter drawn */
795 * Reads out the peak volume of the left channel.
796 * @return int - The maximum value that has been detected
797 * since the last call of peak_meter_read_l. The value
798 * is in the range 0 <= value < MAX_PEAK.
800 static int peak_meter_read_l(void)
802 /* pm_max_left contains the maximum of all peak values that were read
803 by peak_meter_peek since the last call of peak_meter_read_l */
806 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
808 pm_max_left
= rand()%MAX_PEAK
;
811 retval
= pm_max_left
;
813 #if defined(HAVE_RECORDING_HISTOGRAM) || defined(HAVE_AGC)
814 /* store max peak value for peak_meter_get_peakhold_x readout */
815 pm_peakhold_left
= MAX(pm_max_left
, pm_peakhold_left
);
820 /* reset pm_max_left so that subsequent calls of peak_meter_peek don't
821 get fooled by an old maximum value */
822 pm_max_left
= pm_cur_left
;
828 * Reads out the peak volume of the right channel.
829 * @return int - The maximum value that has been detected
830 * since the last call of peak_meter_read_l. The value
831 * is in the range 0 <= value < MAX_PEAK.
833 static int peak_meter_read_r(void)
835 /* peak_meter_r contains the maximum of all peak values that were read
836 by peak_meter_peek since the last call of peak_meter_read_r */
839 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
841 pm_max_right
= rand()%MAX_PEAK
;
844 retval
= pm_max_right
;
846 #if defined(HAVE_RECORDING_HISTOGRAM) || defined(HAVE_AGC)
847 /* store max peak value for peak_meter_get_peakhold_x readout */
848 pm_peakhold_right
= MAX(pm_max_right
, pm_peakhold_right
);
853 /* reset pm_max_right so that subsequent calls of peak_meter_peek don't
854 get fooled by an old maximum value */
855 pm_max_right
= pm_cur_right
;
860 #if defined(HAVE_AGC) || defined(HAVE_RECORDING_HISTOGRAM)
862 * Reads out the current peak-hold values since the last call.
863 * This is used by the histogram feature in the recording screen.
864 * Values are in the range 0 <= peak_x < MAX_PEAK. MAX_PEAK is typ 32767.
866 void peak_meter_get_peakhold(int *peak_left
, int *peak_right
)
869 *peak_left
= pm_peakhold_left
;
871 *peak_right
= pm_peakhold_right
;
872 pm_peakhold_left
= 0;
873 pm_peakhold_right
= 0;
878 * Reset the detected clips. This method is for
879 * use by the user interface.
880 * @param int unused - This parameter was added to
881 * make the function compatible with set_int
883 void peak_meter_set_clip_hold(int time
)
885 pm_clip_left
= false;
886 pm_clip_right
= false;
887 pm_clip_eternal
= (time
> 0) ? false : true;
891 * Scales a peak value as read from the MAS to the range of meterwidth.
892 * The scaling is performed according to the scaling method (dBfs / linear)
893 * and the range (peak_meter_range_min .. peak_meter_range_max).
894 * @param unsigned short val - The volume value. Range: 0 <= val < MAX_PEAK
895 * @param int meterwidht - The widht of the meter in pixel
896 * @return unsigned short - A value 0 <= return value <= meterwidth
898 unsigned short peak_meter_scale_value(unsigned short val
, int meterwidth
)
902 if (val
<= peak_meter_range_min
) {
906 if (val
>= peak_meter_range_max
) {
912 /* different scaling is used for dBfs and linear percent */
915 /* scale the samples dBfs */
916 retval
= (calc_db(retval
) - pm_db_min
) * meterwidth
/ pm_db_range
;
919 /* Scale for linear percent display */
922 /* scale the samples */
923 retval
= ((retval
- peak_meter_range_min
) * meterwidth
)
928 void peak_meter_screen(struct screen
*display
, int x
, int y
, int height
)
930 peak_meter_draw(display
, &scales
[display
->screen_type
], x
, y
,
931 display
->getwidth() - x
, height
);
934 * Draws a peak meter in the specified size at the specified position.
935 * @param int x - The x coordinate.
936 * Make sure that 0 <= x and x + width < display->getwidth()
937 * @param int y - The y coordinate.
938 * Make sure that 0 <= y and y + height < display->getheight()
939 * @param int width - The width of the peak meter. Note that for display
940 * of clips a 3 pixel wide area is used ->
942 * @param int height - The height of the peak meter. height > 3
944 static void peak_meter_draw(struct screen
*display
, struct meter_scales
*scales
,
945 int x
, int y
, int width
, int height
)
947 static int left_level
= 0, right_level
= 0;
948 int left
= 0, right
= 0;
949 int meterwidth
= width
- 3;
951 #if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
952 static long peak_release_tick
[2] = {0,0};
953 int screen_nr
= display
->screen_type
== SCREEN_MAIN
? 0 : 1;
955 static long peak_release_tick
= 0;
959 static long pm_tick
= 0;
960 int tmp
= peek_calls
;
963 /* if disabled only draw the peak meter */
964 if (peak_meter_enabled
) {
968 /* only read the volume info from MAS if peek since last read*/
969 left_level
= peak_meter_read_l();
970 right_level
= peak_meter_read_r();
974 /* scale the samples dBfs */
975 left
= peak_meter_scale_value(left_level
, meterwidth
);
976 right
= peak_meter_scale_value(right_level
, meterwidth
);
978 /*if the scale has changed -> recalculate the scale
979 (The scale becomes invalid when the range changed.) */
980 if (!scales
->db_scale_valid
){
983 db_scale_count
= DB_SCALE_SRC_VALUES_SIZE
;
984 for (i
= 0; i
< db_scale_count
; i
++){
985 /* find the real x-coords for predefined interesting
986 dBfs values. These only are recalculated when the
987 scaling of the meter changed. */
988 scales
->db_scale_lcd_coord
[i
] =
989 peak_meter_scale_value(
990 db_scale_src_values
[i
],
995 /* when scaling linear we simly make 10% steps */
998 for (i
= 0; i
< db_scale_count
; i
++) {
999 scales
->db_scale_lcd_coord
[i
] =
1000 (i
* (MAX_PEAK
/ 10) - peak_meter_range_min
) *
1001 meterwidth
/ pm_range
;
1005 /* mark scale valid to avoid recalculating dBfs values
1007 scales
->db_scale_valid
= true;
1011 #if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
1012 delta
= current_tick
- peak_release_tick
[screen_nr
];
1013 peak_release_tick
[screen_nr
] = current_tick
;
1015 delta
= current_tick
- peak_release_tick
;
1016 peak_release_tick
= current_tick
;
1018 left
= MAX(left
, scales
->last_left
- delta
* pm_peak_release
);
1019 right
= MAX(right
, scales
->last_right
- delta
* pm_peak_release
);
1021 /* reset max values after timeout */
1022 if (TIME_AFTER(current_tick
, scales
->pm_peak_timeout_l
)){
1023 scales
->pm_peak_left
= 0;
1026 if (TIME_AFTER(current_tick
, scales
->pm_peak_timeout_r
)){
1027 scales
->pm_peak_right
= 0;
1030 if (!pm_clip_eternal
) {
1032 TIME_AFTER(current_tick
, pm_clip_timeout_l
)){
1033 pm_clip_left
= false;
1036 if (pm_clip_right
&&
1037 TIME_AFTER(current_tick
, pm_clip_timeout_r
)){
1038 pm_clip_right
= false;
1042 /* check for new max values */
1043 if (left
> scales
->pm_peak_left
) {
1044 scales
->pm_peak_left
= left
- 1;
1045 scales
->pm_peak_timeout_l
=
1046 current_tick
+ peak_time_out
[pm_peak_hold
];
1049 if (right
> scales
->pm_peak_right
) {
1050 scales
->pm_peak_right
= right
- 1;
1051 scales
->pm_peak_timeout_r
=
1052 current_tick
+ peak_time_out
[pm_peak_hold
];
1056 /* draw the peak meter */
1057 display
->set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
1058 display
->fillrect(x
, y
, width
, height
);
1059 display
->set_drawmode(DRMODE_SOLID
);
1062 display
->fillrect (x
, y
, left
, height
/ 2 - 2 );
1063 if (scales
->pm_peak_left
> 0) {
1064 display
->vline(x
+ scales
->pm_peak_left
, y
, y
+ height
/ 2 - 2 );
1067 display
->fillrect(x
+ meterwidth
, y
, 3, height
/ 2 - 1);
1071 display
->fillrect(x
, y
+ height
/ 2 + 1, right
, height
/ 2 - 2);
1072 if (scales
->pm_peak_right
> 0) {
1073 display
->vline( x
+ scales
->pm_peak_right
, y
+ height
/ 2, y
+ height
- 2);
1075 if (pm_clip_right
) {
1076 display
->fillrect(x
+ meterwidth
, y
+ height
/ 2, 3, height
/ 2 - 1);
1079 /* draw scale end */
1080 display
->vline(x
+ meterwidth
, y
, y
+ height
- 2);
1082 /* draw dots for scale marks */
1083 for (i
= 0; i
< db_scale_count
; i
++) {
1084 /* The x-coordinates of interesting scale mark points
1085 have been calculated before */
1086 display
->drawpixel(x
+ scales
->db_scale_lcd_coord
[i
],
1087 y
+ height
/ 2 - 1);
1090 #ifdef HAVE_RECORDING
1092 #ifdef HAVE_BACKLIGHT
1094 if ((pm_clip_left
|| pm_clip_right
) &&
1095 global_settings
.cliplight
&&
1096 #if CONFIG_CODEC == SWCODEC
1099 !(audio_status() & (AUDIO_STATUS_PLAY
| AUDIO_STATUS_ERROR
)))
1102 /* if clipping, cliplight setting on and in recording screen */
1103 if (global_settings
.cliplight
<= 2)
1105 /* turn on main unit light if setting set to main or both*/
1108 #ifdef HAVE_REMOTE_LCD
1109 if (global_settings
.cliplight
>= 2)
1111 /* turn remote light unit on if setting set to remote or both */
1112 remote_backlight_on();
1114 #endif /* HAVE_REMOTE_LCD */
1116 #endif /* HAVE_BACKLIGHT */
1118 if (trig_status
!= TRIG_OFF
) {
1119 int start_trigx
, stop_trigx
, ycenter
;
1121 display
->set_drawmode(DRMODE_SOLID
);
1122 ycenter
= y
+ height
/ 2;
1123 /* display threshold value */
1124 start_trigx
= x
+peak_meter_scale_value(trig_strt_threshold
,meterwidth
);
1125 display
->vline(start_trigx
, ycenter
- 2, ycenter
);
1127 if (start_trigx
< display
->getwidth() ) display
->drawpixel(start_trigx
,
1130 stop_trigx
= x
+ peak_meter_scale_value(trig_stp_threshold
,meterwidth
);
1131 display
->vline(stop_trigx
, ycenter
- 2, ycenter
);
1132 if (stop_trigx
> 0) display
->drawpixel(stop_trigx
- 1, ycenter
- 1);
1134 #endif /*HAVE_RECORDING*/
1137 /* display a bar to show how many calls to peak_meter_peek
1138 have ocurred since the last display */
1139 display
->set_drawmode(DRMODE_COMPLEMENT
);
1140 display
->fillrect(x
, y
, tmp
, 3);
1142 if (tmp
< PEEKS_PER_DRAW_SIZE
) {
1143 peeks_per_redraw
[tmp
]++;
1146 tmp
= current_tick
- pm_tick
;
1147 if (tmp
< TICKS_PER_DRAW_SIZE
){
1148 ticks_per_redraw
[tmp
] ++;
1151 /* display a bar to show how many ticks have passed since
1153 display
->fillrect(x
, y
+ height
/ 2, current_tick
- pm_tick
, 2);
1154 pm_tick
= current_tick
;
1157 scales
->last_left
= left
;
1158 scales
->last_right
= right
;
1160 display
->set_drawmode(DRMODE_SOLID
);
1163 #ifdef HAVE_RECORDING
1165 * Defines the parameters of the trigger. After these parameters are defined
1166 * the trigger can be started either by peak_meter_attack_trigger or by
1167 * peak_meter_release_trigger. Note that you can pass either linear (%) or
1168 * logarithmic (db) values to the thresholds. Positive values are intepreted as
1169 * percent (0 is 0% .. 100 is 100%). Negative values are interpreted as db.
1170 * To avoid ambiguosity of the value 0 the negative values are shifted by -1.
1171 * Thus -75 is -74db .. -1 is 0db.
1172 * @param start_threshold - The threshold used for attack trigger. Negative
1173 * values are interpreted as db -1, positive as %.
1174 * @param start_duration - The minimum time span within which start_threshold
1175 * must be exceeded to fire the attack trigger.
1176 * @param start_dropout - The maximum time span the level may fall below
1177 * start_threshold without releasing the attack trigger.
1178 * @param stop_threshold - The threshold the volume must fall below to release
1179 * the release trigger.Negative values are
1180 * interpreted as db -1, positive as %.
1181 * @param stop_hold - The minimum time the volume must fall below the
1182 * stop_threshold to release the trigger.
1185 void peak_meter_define_trigger(
1186 int start_threshold
,
1187 long start_duration
,
1190 long stop_hold_time
,
1194 if (start_threshold
< 0) {
1196 if (start_threshold
< -89) {
1197 trig_strt_threshold
= 0;
1199 trig_strt_threshold
=peak_meter_db2sample((start_threshold
+1)*100);
1202 /* linear percent */
1203 trig_strt_threshold
= start_threshold
* MAX_PEAK
/ 100;
1205 trig_strt_duration
= start_duration
;
1206 trig_strt_dropout
= start_dropout
;
1207 if (stop_threshold
< 0) {
1209 trig_stp_threshold
= peak_meter_db2sample((stop_threshold
+ 1) * 100);
1211 /* linear percent */
1212 trig_stp_threshold
= stop_threshold
* MAX_PEAK
/ 100;
1214 trig_stp_hold
= stop_hold_time
;
1215 trig_rstrt_gap
= restart_gap
;
1219 * Enables or disables the trigger.
1220 * @param on - If true the trigger is turned on.
1222 void peak_meter_trigger(bool on
)
1224 /* don't use set_trigger here as that would fire an undesired event */
1225 trig_status
= on
? TRIG_READY
: TRIG_OFF
;
1229 * Registers the listener function that listenes on trig_status changes.
1230 * @param listener - The function that is called with each change of
1231 * trig_status. May be set to NULL if no callback is desired.
1233 void peak_meter_set_trigger_listener(void (*listener
)(int status
))
1235 trigger_listener
= listener
;
1239 * Fetches the status of the trigger.
1240 * TRIG_OFF: the trigger is inactive
1241 * TRIG_RELEASED: The volume level is below the threshold
1242 * TRIG_ACTIVATED: The volume level has exceeded the threshold, but the trigger
1243 * hasn't been fired yet.
1244 * TRIG_FIRED: The volume exceeds the threshold
1246 * To activate the trigger call either peak_meter_attack_trigger or
1247 * peak_meter_release_trigger. To turn the trigger off call
1248 * peak_meter_trigger_off.
1250 int peak_meter_trigger_status(void)
1252 return trig_status
; /* & TRIG_PIT_MASK;*/
1255 void peak_meter_draw_trig(int xpos
[], int ypos
[],
1256 int trig_width
[], int nb_screens
)
1258 int barstart
[NB_SCREENS
];
1259 int barend
[NB_SCREENS
];
1261 int ixpos
[NB_SCREENS
];
1263 int trigbar_width
[NB_SCREENS
];
1266 trigbar_width
[i
] = (trig_width
[i
] - (2 * (ICON_PLAY_STATE_WIDTH
+ 1)));
1268 switch (trig_status
) {
1285 barend
[i
] = (trig_strt_duration
== 0) ? trigbar_width
[i
] :
1287 (current_tick
- trig_hightime
) / trig_strt_duration
;
1298 barstart
[i
] = trigbar_width
[i
];
1299 barend
[i
] = trigbar_width
[i
];
1303 ixpos
[i
] = xpos
[i
]+ trig_width
[i
] - ICON_PLAY_STATE_WIDTH
;
1309 barstart
[i
] = (trig_stp_hold
== 0) ? 0 :
1310 trigbar_width
[i
] - trigbar_width
[i
] *
1311 (current_tick
- trig_lowtime
) / trig_stp_hold
;
1312 barend
[i
] = trigbar_width
[i
];
1316 ixpos
[i
] = xpos
[i
] + trig_width
[i
] - ICON_PLAY_STATE_WIDTH
;
1323 for(i
= 0; i
< nb_screens
; i
++)
1325 gui_scrollbar_draw(&screens
[i
], xpos
[i
] + ICON_PLAY_STATE_WIDTH
+ 1,
1326 ypos
[i
] + 1, trigbar_width
[i
], TRIG_HEIGHT
- 2,
1327 trigbar_width
[i
], barstart
[i
], barend
[i
],
1330 screens
[i
].mono_bitmap(bitmap_icons_7x8
[icon
], ixpos
[i
], ypos
[i
],
1331 ICON_PLAY_STATE_WIDTH
, STATUSBAR_HEIGHT
);
1336 int peak_meter_draw_get_btn(int action_context
, int x
[], int y
[],
1337 int height
[], int nb_screens
,
1338 struct viewport vps
[])
1340 int button
= BUTTON_NONE
;
1341 long next_refresh
= current_tick
;
1342 long next_big_refresh
= current_tick
+ HZ
/ 10;
1344 #if (CONFIG_CODEC == SWCODEC)
1345 bool highperf
= false;
1347 /* On MAS targets, we need to poll as often as possible in order to not
1348 * miss a peak, as the MAS does only provide a quasi-peak. When the disk
1349 * is active, it must not draw too much CPU power or a buffer overrun can
1350 * happen when saving a recording. As a compromise, poll only once per tick
1351 * when the disk is active, otherwise spin around as fast as possible. */
1352 bool highperf
= !storage_disk_is_active();
1356 while (TIME_BEFORE(current_tick
, next_big_refresh
)) {
1357 button
= get_action(action_context
, TIMEOUT_NOBLOCK
);
1358 if (button
!= BUTTON_NONE
) {
1361 if (dopeek
) { /* Peek only once per refresh when disk is */
1362 peak_meter_peek(); /* spinning, but as often as possible */
1363 dopeek
= highperf
; /* otherwise. */
1366 sleep(0); /* Sleep until end of current tick. */
1368 if (TIME_AFTER(current_tick
, next_refresh
)) {
1369 for(i
= 0; i
< nb_screens
; i
++)
1371 screens
[i
].set_viewport(&vps
[i
]);
1372 peak_meter_screen(&screens
[i
], x
[i
], y
[i
], height
[i
]);
1373 screens
[i
].update_viewport_rect(x
[i
], y
[i
],
1374 screens
[i
].getwidth() - x
[i
],
1377 next_refresh
+= HZ
/ PEAK_METER_FPS
;
1386 static void peak_meter_clear_histogram(void)
1389 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1390 ticks_per_redraw
[i
] = (unsigned int)0;
1393 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1394 peeks_per_redraw
[i
] = (unsigned int)0;
1398 bool peak_meter_histogram(void)
1401 int btn
= BUTTON_NONE
;
1402 while ((btn
& BUTTON_OFF
) != BUTTON_OFF
)
1404 unsigned int max
= 0;
1407 screens
[0].clear_display();
1409 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1410 max
= MAX(max
, peeks_per_redraw
[i
]);
1413 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1414 x
= peeks_per_redraw
[i
] * (LCD_WIDTH
- 1)/ max
;
1415 screens
[0].hline(0, x
, y
+ i
);
1418 y
= PEEKS_PER_DRAW_SIZE
+ 1;
1421 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1422 max
= MAX(max
, ticks_per_redraw
[i
]);
1425 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1426 x
= ticks_per_redraw
[i
] * (LCD_WIDTH
- 1)/ max
;
1427 screens
[0].hline(0, x
, y
+ i
);
1429 screens
[0].update();
1431 btn
= button_get(true);
1432 if (btn
== BUTTON_PLAY
) {
1433 peak_meter_clear_histogram();