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 #include <stdlib.h> /* sim uses rand for peakmeter simulation */
31 #include "scrollbar.h"
37 #include "peakmeter.h"
39 #include "screen_access.h"
41 #include "backlight.h"
45 #if CONFIG_CODEC == SWCODEC
49 #include "pcm_record.h"
52 static bool pm_playback
= true; /* selects between playback and recording peaks */
55 static struct meter_scales scales
[NB_SCREENS
];
57 #if !defined(SIMULATOR) && CONFIG_CODEC != SWCODEC
59 static int pm_src_left
= MAS_REG_DQPEAK_L
;
60 static int pm_src_right
= MAS_REG_DQPEAK_R
;
63 /* Current values and cumulation */
64 static int pm_cur_left
; /* current values (last peak_meter_peek) */
65 static int pm_cur_right
;
66 static int pm_max_left
; /* maximum values between peak meter draws */
67 static int pm_max_right
;
68 #if defined(HAVE_AGC) || defined(HAVE_RECORDING_HISTOGRAM)
69 static int pm_peakhold_left
; /* max. peak values between peakhold calls */
70 static int pm_peakhold_right
; /* used for AGC and histogram display */
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;
85 static unsigned short peak_meter_range_min
; /* minimum of range in samples */
86 static unsigned short peak_meter_range_max
; /* maximum of range in samples */
87 static unsigned short pm_range
; /* range width in samples */
88 static bool pm_use_dbfs
= true; /* true if peakmeter displays dBfs */
89 static bool level_check
; /* true if peeked at peakmeter before drawing */
90 static unsigned short pm_db_min
= 0; /* minimum of range in 1/100 dB */
91 static unsigned short pm_db_max
= 9000; /* maximum of range in 1/100 dB */
92 static unsigned short pm_db_range
= 9000; /* range width in 1/100 dB */
93 /* Timing behaviour */
94 static int pm_peak_hold
= 1; /* peak hold timeout index */
95 static int pm_peak_release
= 8; /* peak release in units per read */
96 static int pm_clip_hold
= 16; /* clip hold timeout index */
97 static bool pm_clip_eternal
= false; /* true if clip timeout is disabled */
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 */
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
];
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
] = {
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
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 */
199 if (isample
< 2308) { /* Range 1-5 */
201 if (isample
< 115) { /* Range 1-3 */
206 istart
= 1; /* Range 1 */
211 istart
= 5; /* Range 2 */
217 istart
= 24; /* Range 3 */
222 else { /* Range 4-5 */
225 istart
= 114; /* Range 4 */
230 istart
= 588; /* Range 5 */
237 else { /* Range 6-9 */
239 if (isample
< 12932) {
241 if (isample
< 6394) {
242 istart
= 2608; /* Range 6 */
247 istart
= 7000; /* Range 7 */
254 if (isample
< 22450) {
255 istart
= 13000; /* Range 8 */
260 istart
= 22636; /* Range 9 */
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;
287 if (calc_db(test
) < db
) {
288 test
= db_to_sample_bin_search(test
, max
, db
);
290 if (calc_db(test
-1) > db
) {
291 test
= db_to_sample_bin_search(min
, test
, db
);
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
307 * @return int - The return value is in the range of
308 * 0 <= return value < MAX_PEAK
310 int peak_meter_db2sample(int db
)
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) {
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 */
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
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
)
351 peak_meter_range_min
= peak_meter_db2sample(newmin
);
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. */
366 pm_db_min
= calc_db(peak_meter_range_min
);
367 pm_db_range
= pm_db_max
- pm_db_min
;
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)
384 retval
= calc_db(peak_meter_range_min
) - calc_db(MAX_PEAK
- 1);
386 retval
= peak_meter_range_min
* 100 / MAX_PEAK
;
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
)
401 peak_meter_range_max
= peak_meter_db2sample(newmax
);
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. */
415 pm_db_max
= calc_db(peak_meter_range_max
);
416 pm_db_range
= pm_db_max
- pm_db_min
;
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)
433 retval
= calc_db(peak_meter_range_max
) - calc_db(MAX_PEAK
- 1);
435 retval
= peak_meter_range_max
* 100 / MAX_PEAK
;
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)
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
)
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]
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
)
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
)
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)
520 * Set clipping counter to zero (typically at start of recording or playback)
522 void pm_reset_clipcount(void)
529 * Set the source of the peak meter to playback or to
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
)
539 #elif CONFIG_CODEC == SWCODEC
540 pm_playback
= playback
;
543 pm_src_left
= MAS_REG_DQPEAK_L
;
544 pm_src_right
= MAS_REG_DQPEAK_R
;
546 pm_src_left
= MAS_REG_QPEAK_L
;
547 pm_src_right
= MAS_REG_QPEAK_R
;
550 /* reset the scales just in case recording and playback
551 use different viewport sizes. Normally we should be checking viewport
552 sizes every time but this will do for now */
554 scales
[i
].db_scale_valid
= false;
557 #ifdef HAVE_RECORDING
558 static void set_trig_status(int new_state
)
560 if (trig_status
!= new_state
) {
561 trig_status
= new_state
;
562 if (trigger_listener
!= NULL
) {
563 trigger_listener(trig_status
);
571 * Reads peak values from the MAS, and detects clips. The
572 * values are stored in pm_max_left pm_max_right for later
573 * evauluation. Consecutive calls to peak_meter_peek detect
574 * that ocurred. This function could be used by a thread for
575 * busy reading the MAS.
577 void peak_meter_peek(void)
580 #ifdef HAVE_RECORDING
581 bool was_clipping
= pm_clip_left
|| pm_clip_right
;
583 /* read current values */
584 #if CONFIG_CODEC == SWCODEC
586 pcm_calculate_peaks(&pm_cur_left
, &pm_cur_right
);
587 #ifdef HAVE_RECORDING
589 pcm_calculate_rec_peaks(&pm_cur_left
, &pm_cur_right
);
592 right
= pm_cur_right
;
595 pm_cur_left
= left
= mas_codec_readreg(pm_src_left
);
596 pm_cur_right
= right
= mas_codec_readreg(pm_src_right
);
598 pm_cur_left
= left
= 8000;
599 pm_cur_right
= right
= 9000;
604 An clip is assumed when two consecutive readouts
605 of the volume are at full scale. This is proven
606 to be inaccurate in both ways: it may detect clips
607 when no clip occurred and it may fail to detect
608 a real clip. For software codecs, the peak is already
609 the max of a bunch of samples, so use one max value
610 or you fail to detect clipping! */
611 #if CONFIG_CODEC == SWCODEC
612 if (left
== MAX_PEAK
- 1) {
614 if ((left
== pm_max_left
) &&
615 (left
== MAX_PEAK
- 1)) {
619 current_tick
+ clip_time_out
[pm_clip_hold
];
622 #if CONFIG_CODEC == SWCODEC
623 if (right
== MAX_PEAK
- 1) {
625 if ((right
== pm_max_right
) &&
626 (right
== MAX_PEAK
- 1)) {
628 pm_clip_right
= true;
630 current_tick
+ clip_time_out
[pm_clip_hold
];
633 #ifdef HAVE_RECORDING
634 if(!was_clipping
&& (pm_clip_left
|| pm_clip_right
))
636 if(pm_clipcount_active
)
641 /* peaks are searched -> we have to find the maximum. When
642 many calls of peak_meter_peek the maximum value will be
643 stored in pm_max_xxx. This maximum is reset by the
644 functions peak_meter_read_x. */
645 pm_max_left
= MAX(pm_max_left
, left
);
646 pm_max_right
= MAX(pm_max_right
, right
);
648 #ifdef HAVE_RECORDING
649 #if CONFIG_CODEC == SWCODEC
650 /* Ignore any unread peakmeter data */
651 #define MAX_DROP_TIME HZ/7 /* this value may need tweaking. Increase if you are
652 getting trig events when you shouldn't with
655 trig_stp_hold
= MAX_DROP_TIME
;
658 switch (trig_status
) {
660 /* no more changes, if trigger was activated as release trigger */
661 /* threshold exceeded? */
662 if ((left
> trig_strt_threshold
)
663 || (right
> trig_strt_threshold
)) {
664 /* reset trigger duration */
665 trig_hightime
= current_tick
;
667 /* reset dropout duration */
668 trig_lowtime
= current_tick
;
670 if (trig_strt_duration
)
671 set_trig_status(TRIG_STEADY
);
673 /* if trig_duration is set to 0 the user wants to start
674 recording immediately */
675 set_trig_status(TRIG_GO
);
681 /* trigger duration exceeded */
682 if (current_tick
- trig_hightime
> trig_strt_duration
) {
683 set_trig_status(TRIG_GO
);
685 /* threshold exceeded? */
686 if ((left
> trig_strt_threshold
)
687 || (right
> trig_strt_threshold
)) {
689 trig_lowtime
= current_tick
;
691 /* volume is below threshold */
693 /* dropout occurred? */
694 if (current_tick
- trig_lowtime
> trig_strt_dropout
){
695 if (trig_status
== TRIG_STEADY
){
696 set_trig_status(TRIG_READY
);
698 /* trig_status == TRIG_RETRIG */
700 /* the gap has already expired */
701 trig_lowtime
= current_tick
- trig_rstrt_gap
- 1;
702 set_trig_status(TRIG_POSTREC
);
711 /* threshold exceeded? */
712 if ((left
> trig_stp_threshold
)
713 || (right
> trig_stp_threshold
)) {
714 /* restart hold time countdown */
715 trig_lowtime
= current_tick
;
716 #if CONFIG_CODEC == SWCODEC
717 } else if (current_tick
- trig_lowtime
> MAX_DROP_TIME
){
721 set_trig_status(TRIG_POSTREC
);
722 trig_hightime
= current_tick
;
727 /* gap time expired? */
728 if (current_tick
- trig_lowtime
> trig_rstrt_gap
){
729 /* start threshold exceeded? */
730 if ((left
> trig_strt_threshold
)
731 || (right
> trig_strt_threshold
)) {
733 set_trig_status(TRIG_RETRIG
);
734 trig_hightime
= current_tick
;
735 trig_lowtime
= current_tick
;
739 /* stop threshold exceeded */
740 if ((left
> trig_stp_threshold
)
741 || (right
> trig_stp_threshold
)) {
742 if (current_tick
- trig_hightime
> trig_stp_hold
){
743 trig_lowtime
= current_tick
;
744 set_trig_status(TRIG_CONTINUE
);
746 trig_lowtime
= current_tick
- trig_rstrt_gap
- 1;
750 /* below any threshold */
752 if (current_tick
- trig_lowtime
> trig_stp_hold
){
753 set_trig_status(TRIG_READY
);
755 trig_hightime
= current_tick
;
760 /* still within the gap time */
762 /* stop threshold exceeded */
763 if ((left
> trig_stp_threshold
)
764 || (right
> trig_stp_threshold
)) {
765 set_trig_status(TRIG_CONTINUE
);
766 trig_lowtime
= current_tick
;
769 /* hold time expired */
770 else if (current_tick
- trig_lowtime
> trig_stp_hold
){
771 trig_hightime
= current_tick
;
772 trig_lowtime
= current_tick
;
773 set_trig_status(TRIG_READY
);
778 #if CONFIG_CODEC == SWCODEC
779 /* restore stop hold value */
780 if (trig_stp_hold
== MAX_DROP_TIME
)
784 /* check levels next time peakmeter drawn */
792 * Reads out the peak volume of the left channel.
793 * @return int - The maximum value that has been detected
794 * since the last call of peak_meter_read_l. The value
795 * is in the range 0 <= value < MAX_PEAK.
797 static int peak_meter_read_l(void)
799 /* pm_max_left contains the maximum of all peak values that were read
800 by peak_meter_peek since the last call of peak_meter_read_l */
803 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
805 pm_max_left
= rand()%MAX_PEAK
;
808 retval
= pm_max_left
;
810 #if defined(HAVE_RECORDING_HISTOGRAM) || defined(HAVE_AGC)
811 /* store max peak value for peak_meter_get_peakhold_x readout */
812 pm_peakhold_left
= MAX(pm_max_left
, pm_peakhold_left
);
817 /* reset pm_max_left so that subsequent calls of peak_meter_peek don't
818 get fooled by an old maximum value */
819 pm_max_left
= pm_cur_left
;
825 * Reads out the peak volume of the right channel.
826 * @return int - The maximum value that has been detected
827 * since the last call of peak_meter_read_l. The value
828 * is in the range 0 <= value < MAX_PEAK.
830 static int peak_meter_read_r(void)
832 /* peak_meter_r contains the maximum of all peak values that were read
833 by peak_meter_peek since the last call of peak_meter_read_r */
836 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
838 pm_max_right
= rand()%MAX_PEAK
;
841 retval
= pm_max_right
;
843 #if defined(HAVE_RECORDING_HISTOGRAM) || defined(HAVE_AGC)
844 /* store max peak value for peak_meter_get_peakhold_x readout */
845 pm_peakhold_right
= MAX(pm_max_right
, pm_peakhold_right
);
850 /* reset pm_max_right so that subsequent calls of peak_meter_peek don't
851 get fooled by an old maximum value */
852 pm_max_right
= pm_cur_right
;
857 #if defined(HAVE_AGC) || defined(HAVE_RECORDING_HISTOGRAM)
859 * Reads out the current peak-hold values since the last call.
860 * This is used by the histogram feature in the recording screen.
861 * Values are in the range 0 <= peak_x < MAX_PEAK. MAX_PEAK is typ 32767.
863 void peak_meter_get_peakhold(int *peak_left
, int *peak_right
)
866 *peak_left
= pm_peakhold_left
;
868 *peak_right
= pm_peakhold_right
;
869 pm_peakhold_left
= 0;
870 pm_peakhold_right
= 0;
875 * Reset the detected clips. This method is for
876 * use by the user interface.
877 * @param int unused - This parameter was added to
878 * make the function compatible with set_int
880 void peak_meter_set_clip_hold(int time
)
882 pm_clip_left
= false;
883 pm_clip_right
= false;
884 pm_clip_eternal
= (time
> 0) ? false : true;
888 * Scales a peak value as read from the MAS to the range of meterwidth.
889 * The scaling is performed according to the scaling method (dBfs / linear)
890 * and the range (peak_meter_range_min .. peak_meter_range_max).
891 * @param unsigned short val - The volume value. Range: 0 <= val < MAX_PEAK
892 * @param int meterwidht - The widht of the meter in pixel
893 * @return unsigned short - A value 0 <= return value <= meterwidth
895 unsigned short peak_meter_scale_value(unsigned short val
, int meterwidth
)
899 if (val
<= peak_meter_range_min
) {
903 if (val
>= peak_meter_range_max
) {
909 /* different scaling is used for dBfs and linear percent */
912 /* scale the samples dBfs */
913 retval
= (calc_db(retval
) - pm_db_min
) * meterwidth
/ pm_db_range
;
916 /* Scale for linear percent display */
919 /* scale the samples */
920 retval
= ((retval
- peak_meter_range_min
) * meterwidth
)
925 void peak_meter_screen(struct screen
*display
, int x
, int y
, int height
)
927 peak_meter_draw(display
, &scales
[display
->screen_type
], x
, y
,
928 display
->getwidth() - x
, height
);
931 * Draws a peak meter in the specified size at the specified position.
932 * @param int x - The x coordinate.
933 * Make sure that 0 <= x and x + width < display->getwidth()
934 * @param int y - The y coordinate.
935 * Make sure that 0 <= y and y + height < display->getheight()
936 * @param int width - The width of the peak meter. Note that for display
937 * of clips a 3 pixel wide area is used ->
939 * @param int height - The height of the peak meter. height > 3
941 static void peak_meter_draw(struct screen
*display
, struct meter_scales
*scales
,
942 int x
, int y
, int width
, int height
)
944 static int left_level
= 0, right_level
= 0;
945 int left
= 0, right
= 0;
946 int meterwidth
= width
- 3;
948 #if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
949 static long peak_release_tick
[2] = {0,0};
950 int screen_nr
= display
->screen_type
== SCREEN_MAIN
? 0 : 1;
952 static long peak_release_tick
= 0;
956 static long pm_tick
= 0;
957 int tmp
= peek_calls
;
960 /* if disabled only draw the peak meter */
961 if (peak_meter_enabled
) {
965 /* only read the volume info from MAS if peek since last read*/
966 left_level
= peak_meter_read_l();
967 right_level
= peak_meter_read_r();
971 /* scale the samples dBfs */
972 left
= peak_meter_scale_value(left_level
, meterwidth
);
973 right
= peak_meter_scale_value(right_level
, meterwidth
);
975 /*if the scale has changed -> recalculate the scale
976 (The scale becomes invalid when the range changed.) */
977 if (!scales
->db_scale_valid
){
980 db_scale_count
= DB_SCALE_SRC_VALUES_SIZE
;
981 for (i
= 0; i
< db_scale_count
; i
++){
982 /* find the real x-coords for predefined interesting
983 dBfs values. These only are recalculated when the
984 scaling of the meter changed. */
985 scales
->db_scale_lcd_coord
[i
] =
986 peak_meter_scale_value(
987 db_scale_src_values
[i
],
992 /* when scaling linear we simly make 10% steps */
995 for (i
= 0; i
< db_scale_count
; i
++) {
996 scales
->db_scale_lcd_coord
[i
] =
997 (i
* (MAX_PEAK
/ 10) - peak_meter_range_min
) *
998 meterwidth
/ pm_range
;
1002 /* mark scale valid to avoid recalculating dBfs values
1004 scales
->db_scale_valid
= true;
1008 #if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
1009 delta
= current_tick
- peak_release_tick
[screen_nr
];
1010 peak_release_tick
[screen_nr
] = current_tick
;
1012 delta
= current_tick
- peak_release_tick
;
1013 peak_release_tick
= current_tick
;
1015 left
= MAX(left
, scales
->last_left
- delta
* pm_peak_release
);
1016 right
= MAX(right
, scales
->last_right
- delta
* pm_peak_release
);
1018 /* reset max values after timeout */
1019 if (TIME_AFTER(current_tick
, scales
->pm_peak_timeout_l
)){
1020 scales
->pm_peak_left
= 0;
1023 if (TIME_AFTER(current_tick
, scales
->pm_peak_timeout_r
)){
1024 scales
->pm_peak_right
= 0;
1027 if (!pm_clip_eternal
) {
1029 TIME_AFTER(current_tick
, pm_clip_timeout_l
)){
1030 pm_clip_left
= false;
1033 if (pm_clip_right
&&
1034 TIME_AFTER(current_tick
, pm_clip_timeout_r
)){
1035 pm_clip_right
= false;
1039 /* check for new max values */
1040 if (left
> scales
->pm_peak_left
) {
1041 scales
->pm_peak_left
= left
- 1;
1042 scales
->pm_peak_timeout_l
=
1043 current_tick
+ peak_time_out
[pm_peak_hold
];
1046 if (right
> scales
->pm_peak_right
) {
1047 scales
->pm_peak_right
= right
- 1;
1048 scales
->pm_peak_timeout_r
=
1049 current_tick
+ peak_time_out
[pm_peak_hold
];
1053 /* draw the peak meter */
1054 display
->set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
1055 display
->fillrect(x
, y
, width
, height
);
1056 display
->set_drawmode(DRMODE_SOLID
);
1059 display
->fillrect (x
, y
, left
, height
/ 2 - 2 );
1060 if (scales
->pm_peak_left
> 0) {
1061 display
->vline(x
+ scales
->pm_peak_left
, y
, y
+ height
/ 2 - 2 );
1064 display
->fillrect(x
+ meterwidth
, y
, 3, height
/ 2 - 1);
1068 display
->fillrect(x
, y
+ height
/ 2 + 1, right
, height
/ 2 - 2);
1069 if (scales
->pm_peak_right
> 0) {
1070 display
->vline( x
+ scales
->pm_peak_right
, y
+ height
/ 2, y
+ height
- 2);
1072 if (pm_clip_right
) {
1073 display
->fillrect(x
+ meterwidth
, y
+ height
/ 2, 3, height
/ 2 - 1);
1076 /* draw scale end */
1077 display
->vline(x
+ meterwidth
, y
, y
+ height
- 2);
1079 /* draw dots for scale marks */
1080 for (i
= 0; i
< db_scale_count
; i
++) {
1081 /* The x-coordinates of interesting scale mark points
1082 have been calculated before */
1083 display
->drawpixel(x
+ scales
->db_scale_lcd_coord
[i
],
1084 y
+ height
/ 2 - 1);
1087 #ifdef HAVE_RECORDING
1089 #ifdef HAVE_BACKLIGHT
1091 if ((pm_clip_left
|| pm_clip_right
) &&
1092 global_settings
.cliplight
&&
1093 #if CONFIG_CODEC == SWCODEC
1096 !(audio_status() & (AUDIO_STATUS_PLAY
| AUDIO_STATUS_ERROR
)))
1099 /* if clipping, cliplight setting on and in recording screen */
1100 if (global_settings
.cliplight
<= 2)
1102 /* turn on main unit light if setting set to main or both*/
1105 #ifdef HAVE_REMOTE_LCD
1106 if (global_settings
.cliplight
>= 2)
1108 /* turn remote light unit on if setting set to remote or both */
1109 remote_backlight_on();
1111 #endif /* HAVE_REMOTE_LCD */
1113 #endif /* HAVE_BACKLIGHT */
1115 if (trig_status
!= TRIG_OFF
) {
1116 int start_trigx
, stop_trigx
, ycenter
;
1118 display
->set_drawmode(DRMODE_SOLID
);
1119 ycenter
= y
+ height
/ 2;
1120 /* display threshold value */
1121 start_trigx
= x
+peak_meter_scale_value(trig_strt_threshold
,meterwidth
);
1122 display
->vline(start_trigx
, ycenter
- 2, ycenter
);
1124 if (start_trigx
< display
->getwidth() ) display
->drawpixel(start_trigx
,
1127 stop_trigx
= x
+ peak_meter_scale_value(trig_stp_threshold
,meterwidth
);
1128 display
->vline(stop_trigx
, ycenter
- 2, ycenter
);
1129 if (stop_trigx
> 0) display
->drawpixel(stop_trigx
- 1, ycenter
- 1);
1131 #endif /*HAVE_RECORDING*/
1134 /* display a bar to show how many calls to peak_meter_peek
1135 have ocurred since the last display */
1136 display
->set_drawmode(DRMODE_COMPLEMENT
);
1137 display
->fillrect(x
, y
, tmp
, 3);
1139 if (tmp
< PEEKS_PER_DRAW_SIZE
) {
1140 peeks_per_redraw
[tmp
]++;
1143 tmp
= current_tick
- pm_tick
;
1144 if (tmp
< TICKS_PER_DRAW_SIZE
){
1145 ticks_per_redraw
[tmp
] ++;
1148 /* display a bar to show how many ticks have passed since
1150 display
->fillrect(x
, y
+ height
/ 2, current_tick
- pm_tick
, 2);
1151 pm_tick
= current_tick
;
1154 scales
->last_left
= left
;
1155 scales
->last_right
= right
;
1157 display
->set_drawmode(DRMODE_SOLID
);
1160 #ifdef HAVE_RECORDING
1162 * Defines the parameters of the trigger. After these parameters are defined
1163 * the trigger can be started either by peak_meter_attack_trigger or by
1164 * peak_meter_release_trigger. Note that you can pass either linear (%) or
1165 * logarithmic (db) values to the thresholds. Positive values are intepreted as
1166 * percent (0 is 0% .. 100 is 100%). Negative values are interpreted as db.
1167 * To avoid ambiguosity of the value 0 the negative values are shifted by -1.
1168 * Thus -75 is -74db .. -1 is 0db.
1169 * @param start_threshold - The threshold used for attack trigger. Negative
1170 * values are interpreted as db -1, positive as %.
1171 * @param start_duration - The minimum time span within which start_threshold
1172 * must be exceeded to fire the attack trigger.
1173 * @param start_dropout - The maximum time span the level may fall below
1174 * start_threshold without releasing the attack trigger.
1175 * @param stop_threshold - The threshold the volume must fall below to release
1176 * the release trigger.Negative values are
1177 * interpreted as db -1, positive as %.
1178 * @param stop_hold - The minimum time the volume must fall below the
1179 * stop_threshold to release the trigger.
1182 void peak_meter_define_trigger(
1183 int start_threshold
,
1184 long start_duration
,
1187 long stop_hold_time
,
1191 if (start_threshold
< 0) {
1193 if (start_threshold
< -89) {
1194 trig_strt_threshold
= 0;
1196 trig_strt_threshold
=peak_meter_db2sample((start_threshold
+1)*100);
1199 /* linear percent */
1200 trig_strt_threshold
= start_threshold
* MAX_PEAK
/ 100;
1202 trig_strt_duration
= start_duration
;
1203 trig_strt_dropout
= start_dropout
;
1204 if (stop_threshold
< 0) {
1206 trig_stp_threshold
= peak_meter_db2sample((stop_threshold
+ 1) * 100);
1208 /* linear percent */
1209 trig_stp_threshold
= stop_threshold
* MAX_PEAK
/ 100;
1211 trig_stp_hold
= stop_hold_time
;
1212 trig_rstrt_gap
= restart_gap
;
1216 * Enables or disables the trigger.
1217 * @param on - If true the trigger is turned on.
1219 void peak_meter_trigger(bool on
)
1221 /* don't use set_trigger here as that would fire an undesired event */
1222 trig_status
= on
? TRIG_READY
: TRIG_OFF
;
1226 * Registers the listener function that listenes on trig_status changes.
1227 * @param listener - The function that is called with each change of
1228 * trig_status. May be set to NULL if no callback is desired.
1230 void peak_meter_set_trigger_listener(void (*listener
)(int status
))
1232 trigger_listener
= listener
;
1236 * Fetches the status of the trigger.
1237 * TRIG_OFF: the trigger is inactive
1238 * TRIG_RELEASED: The volume level is below the threshold
1239 * TRIG_ACTIVATED: The volume level has exceeded the threshold, but the trigger
1240 * hasn't been fired yet.
1241 * TRIG_FIRED: The volume exceeds the threshold
1243 * To activate the trigger call either peak_meter_attack_trigger or
1244 * peak_meter_release_trigger. To turn the trigger off call
1245 * peak_meter_trigger_off.
1247 int peak_meter_trigger_status(void)
1249 return trig_status
; /* & TRIG_PIT_MASK;*/
1252 void peak_meter_draw_trig(int xpos
[], int ypos
[],
1253 int trig_width
[], int nb_screens
)
1255 int barstart
[NB_SCREENS
];
1256 int barend
[NB_SCREENS
];
1258 int ixpos
[NB_SCREENS
];
1260 int trigbar_width
[NB_SCREENS
];
1263 trigbar_width
[i
] = (trig_width
[i
] - (2 * (ICON_PLAY_STATE_WIDTH
+ 1)));
1265 switch (trig_status
) {
1282 barend
[i
] = (trig_strt_duration
== 0) ? trigbar_width
[i
] :
1284 (current_tick
- trig_hightime
) / trig_strt_duration
;
1295 barstart
[i
] = trigbar_width
[i
];
1296 barend
[i
] = trigbar_width
[i
];
1300 ixpos
[i
] = xpos
[i
]+ trig_width
[i
] - ICON_PLAY_STATE_WIDTH
;
1306 barstart
[i
] = (trig_stp_hold
== 0) ? 0 :
1307 trigbar_width
[i
] - trigbar_width
[i
] *
1308 (current_tick
- trig_lowtime
) / trig_stp_hold
;
1309 barend
[i
] = trigbar_width
[i
];
1313 ixpos
[i
] = xpos
[i
] + trig_width
[i
] - ICON_PLAY_STATE_WIDTH
;
1320 for(i
= 0; i
< nb_screens
; i
++)
1322 gui_scrollbar_draw(&screens
[i
], xpos
[i
] + ICON_PLAY_STATE_WIDTH
+ 1,
1323 ypos
[i
] + 1, trigbar_width
[i
], TRIG_HEIGHT
- 2,
1324 trigbar_width
[i
], barstart
[i
], barend
[i
],
1327 screens
[i
].mono_bitmap(bitmap_icons_7x8
[icon
], ixpos
[i
], ypos
[i
],
1328 ICON_PLAY_STATE_WIDTH
, STATUSBAR_HEIGHT
);
1333 int peak_meter_draw_get_btn(int action_context
, int x
[], int y
[],
1334 int height
[], int nb_screens
,
1335 struct viewport vps
[])
1337 int button
= BUTTON_NONE
;
1338 long next_refresh
= current_tick
;
1339 long next_big_refresh
= current_tick
+ HZ
/ 10;
1341 #if (CONFIG_CODEC == SWCODEC)
1342 bool highperf
= false;
1344 /* On MAS targets, we need to poll as often as possible in order to not
1345 * miss a peak, as the MAS does only provide a quasi-peak. When the disk
1346 * is active, it must not draw too much CPU power or a buffer overrun can
1347 * happen when saving a recording. As a compromise, poll only once per tick
1348 * when the disk is active, otherwise spin around as fast as possible. */
1349 bool highperf
= !storage_disk_is_active();
1353 while (TIME_BEFORE(current_tick
, next_big_refresh
)) {
1354 button
= get_action(action_context
, TIMEOUT_NOBLOCK
);
1355 if (button
!= BUTTON_NONE
) {
1358 if (dopeek
) { /* Peek only once per refresh when disk is */
1359 peak_meter_peek(); /* spinning, but as often as possible */
1360 dopeek
= highperf
; /* otherwise. */
1363 sleep(0); /* Sleep until end of current tick. */
1365 if (TIME_AFTER(current_tick
, next_refresh
)) {
1366 for(i
= 0; i
< nb_screens
; i
++)
1368 screens
[i
].set_viewport(&vps
[i
]);
1369 peak_meter_screen(&screens
[i
], x
[i
], y
[i
], height
[i
]);
1370 screens
[i
].update_viewport_rect(x
[i
], y
[i
],
1371 screens
[i
].getwidth() - x
[i
],
1374 next_refresh
+= HZ
/ PEAK_METER_FPS
;
1383 static void peak_meter_clear_histogram(void)
1386 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1387 ticks_per_redraw
[i
] = (unsigned int)0;
1390 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1391 peeks_per_redraw
[i
] = (unsigned int)0;
1395 bool peak_meter_histogram(void)
1398 int btn
= BUTTON_NONE
;
1399 while ((btn
& BUTTON_OFF
) != BUTTON_OFF
)
1401 unsigned int max
= 0;
1404 screens
[0].clear_display();
1406 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1407 max
= MAX(max
, peeks_per_redraw
[i
]);
1410 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1411 x
= peeks_per_redraw
[i
] * (LCD_WIDTH
- 1)/ max
;
1412 screens
[0].hline(0, x
, y
+ i
);
1415 y
= PEEKS_PER_DRAW_SIZE
+ 1;
1418 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1419 max
= MAX(max
, ticks_per_redraw
[i
]);
1422 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1423 x
= ticks_per_redraw
[i
] * (LCD_WIDTH
- 1)/ max
;
1424 screens
[0].hline(0, x
, y
+ i
);
1426 screens
[0].update();
1428 btn
= button_get(true);
1429 if (btn
== BUTTON_PLAY
) {
1430 peak_meter_clear_histogram();