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"
39 #include "peakmeter.h"
41 #include "screen_access.h"
43 #include "backlight.h"
47 #if CONFIG_CODEC == SWCODEC
51 #include "pcm_record.h"
54 static bool pm_playback
= true; /* selects between playback and recording peaks */
57 static struct meter_scales scales
[NB_SCREENS
];
59 #if !defined(SIMULATOR) && CONFIG_CODEC != SWCODEC
61 static int pm_src_left
= MAS_REG_DQPEAK_L
;
62 static int pm_src_right
= MAS_REG_DQPEAK_R
;
65 /* Current values and cumulation */
66 static int pm_cur_left
; /* current values (last peak_meter_peek) */
67 static int pm_cur_right
;
68 static int pm_max_left
; /* maximum values between peak meter draws */
69 static int pm_max_right
;
71 static int pm_peakhold_left
; /* max. peak values between peakhold calls */
72 static int pm_peakhold_right
; /* used for AGC and histogram display */
76 static bool pm_clip_left
= false; /* when true a clip has occurred */
77 static bool pm_clip_right
= false;
78 static long pm_clip_timeout_l
; /* clip hold timeouts */
79 static long pm_clip_timeout_r
;
81 /* Temporarily en- / disables peak meter. This is especially for external
82 applications to detect if the peak_meter is in use and needs drawing at all */
83 bool peak_meter_enabled
= true;
87 static unsigned short peak_meter_range_min
; /* minimum of range in samples */
88 static unsigned short peak_meter_range_max
; /* maximum of range in samples */
89 static unsigned short pm_range
; /* range width in samples */
90 static bool pm_use_dbfs
= true; /* true if peakmeter displays dBfs */
91 static bool level_check
; /* true if peeked at peakmeter before drawing */
92 static unsigned short pm_db_min
= 0; /* minimum of range in 1/100 dB */
93 static unsigned short pm_db_max
= 9000; /* maximum of range in 1/100 dB */
94 static unsigned short pm_db_range
= 9000; /* range width in 1/100 dB */
95 /* Timing behaviour */
96 static int pm_peak_hold
= 1; /* peak hold timeout index */
97 static int pm_peak_release
= 8; /* peak release in units per read */
98 static int pm_clip_hold
= 16; /* clip hold timeout index */
99 static bool pm_clip_eternal
= false; /* true if clip timeout is disabled */
101 #ifdef HAVE_RECORDING
102 static unsigned short trig_strt_threshold
;
103 static long trig_strt_duration
;
104 static long trig_strt_dropout
;
105 static unsigned short trig_stp_threshold
;
106 static long trig_stp_hold
;
107 static long trig_rstrt_gap
;
109 /* point in time when the threshold was exceeded */
110 static long trig_hightime
;
112 /* point in time when the volume fell below the threshold*/
113 static long trig_lowtime
;
115 /* The output value of the trigger. See TRIG_XXX constants for valid values */
116 static int trig_status
= TRIG_OFF
;
118 static void (*trigger_listener
)(int) = NULL
;
120 /* clipping counter (only used for recording) */
121 static unsigned int pm_clipcount
= 0; /* clipping count */
122 static bool pm_clipcount_active
= false; /* counting or not */
127 static int peek_calls
= 0;
129 #define PEEKS_PER_DRAW_SIZE 40
130 static unsigned int peeks_per_redraw
[PEEKS_PER_DRAW_SIZE
];
132 #define TICKS_PER_DRAW_SIZE 20
133 static unsigned int ticks_per_redraw
[TICKS_PER_DRAW_SIZE
];
136 static void peak_meter_draw(struct screen
*display
, struct meter_scales
*meter_scales
,
137 int x
, int y
, int width
, int height
);
139 /* time out values for max */
140 static const short peak_time_out
[] = {
141 0 * HZ
, HZ
/ 5, 30, HZ
/ 2, HZ
, 2 * HZ
,
142 3 * HZ
, 4 * HZ
, 5 * HZ
, 6 * HZ
, 7 * HZ
, 8 * HZ
,
143 9 * HZ
, 10 * HZ
, 15 * HZ
, 20 * HZ
, 30 * HZ
, 60 * HZ
146 /* time out values for clip */
147 static const long clip_time_out
[] = {
148 0 * HZ
, 1 * HZ
, 2 * HZ
, 3 * HZ
, 4 * HZ
, 5 * HZ
,
149 6 * HZ
, 7 * HZ
, 8 * HZ
, 9 * HZ
, 10 * HZ
, 15 * HZ
,
150 20 * HZ
, 25 * HZ
, 30 * HZ
, 45 * HZ
, 60 * HZ
, 90 * HZ
,
151 120 * HZ
, 180 * HZ
, 300 * HZ
, 600L * HZ
, 1200L * HZ
,
152 2700L * HZ
, 5400L * HZ
155 /* precalculated peak values that represent magical
156 dBfs values. Used to draw the scale */
157 static const short db_scale_src_values
[DB_SCALE_SRC_VALUES_SIZE
] = {
172 static int db_scale_count
= DB_SCALE_SRC_VALUES_SIZE
;
175 * Calculates dB Value for the peak meter, uses peak value as input
176 * @param int sample - The input value
177 * Make sure that 0 <= value < SAMPLE_RANGE
179 * @return int - The 2 digit fixed point result of the euation
180 * 20 * log (sample / SAMPLE_RANGE) + 90
181 * Output range is 0-9000 (that is 0.0 - 90.0 dB).
182 * Normally 0dB is full scale, here it is shifted +90dB.
183 * The calculation is based on the results of a linear
184 * approximation tool written specifically for this problem
185 * by Andreas Zwirtes (radhard@gmx.de). The result has an
186 * accurracy of better than 2%. It is highly runtime optimized,
187 * the cascading if-clauses do an successive approximation on
188 * the input value. This avoids big lookup-tables and
190 * Improved by Jvo Studer for errors < 0.2dB for critical
191 * range of -12dB to 0dB (78.0 to 90.0dB).
194 int calc_db (int isample
)
196 /* return n+m*(isample-istart)/100 */
201 if (isample
< 2308) { /* Range 1-5 */
203 if (isample
< 115) { /* Range 1-3 */
208 istart
= 1; /* Range 1 */
213 istart
= 5; /* Range 2 */
219 istart
= 24; /* Range 3 */
224 else { /* Range 4-5 */
227 istart
= 114; /* Range 4 */
232 istart
= 588; /* Range 5 */
239 else { /* Range 6-9 */
241 if (isample
< 12932) {
243 if (isample
< 6394) {
244 istart
= 2608; /* Range 6 */
249 istart
= 7000; /* Range 7 */
256 if (isample
< 22450) {
257 istart
= 13000; /* Range 8 */
262 istart
= 22636; /* Range 9 */
269 return n
+ (m
* (long)(isample
- istart
)) / 100L;
274 * A helper function for peak_meter_db2sample. Don't call it separately but
275 * use peak_meter_db2sample. If one or both of min and max are outside the
276 * range 0 <= min (or max) < 8961 the behaviour of this function is
277 * undefined. It may not return.
278 * @param int min - The minimum of the value range that is searched.
279 * @param int max - The maximum of the value range that is searched.
280 * @param int db - The value in dBfs * (-100) for which the according
281 * minimal peak sample is searched.
282 * @return int - A linear volume value with 0 <= value < MAX_PEAK
284 static int db_to_sample_bin_search(int min
, int max
, int db
)
286 int test
= min
+ (max
- min
) / 2;
289 if (calc_db(test
) < db
) {
290 test
= db_to_sample_bin_search(test
, max
, db
);
292 if (calc_db(test
-1) > db
) {
293 test
= db_to_sample_bin_search(min
, test
, db
);
301 * Converts a value representing dBfs to a linear
302 * scaled volume info as it is used by the MAS.
303 * An incredibly inefficiant function which is
304 * the vague inverse of calc_db. This really
305 * should be replaced by something better soon.
307 * @param int db - A dBfs * 100 value with
309 * @return int - The return value is in the range of
310 * 0 <= return value < MAX_PEAK
312 int peak_meter_db2sample(int db
)
316 /* what is the maximum pseudo db value */
317 int max_peak_db
= calc_db(MAX_PEAK
- 1);
319 /* range check: db value to big */
320 if (max_peak_db
+ db
< 0) {
324 /* range check: db value too small */
325 else if (max_peak_db
+ db
>= max_peak_db
) {
326 retval
= MAX_PEAK
-1;
329 /* value in range: find the matching linear value */
331 retval
= db_to_sample_bin_search(0, MAX_PEAK
, max_peak_db
+ db
);
333 /* as this is a dirty function anyway, we want to adjust the
334 full scale hit manually to avoid users complaining that when
335 they adjust maximum for 0 dBfs and display it in percent it
336 shows 99%. That is due to precision loss and this is the
344 * Set the min value for restriction of the value range.
345 * @param int newmin - depending whether dBfs is used
346 * newmin is a value in dBfs * 100 or in linear percent values.
347 * for dBfs: -9000 < newmin <= 0
348 * for linear: 0 <= newmin <= 100
350 static void peak_meter_set_min(int newmin
)
353 peak_meter_range_min
= peak_meter_db2sample(newmin
);
356 if (newmin
< peak_meter_range_max
) {
357 peak_meter_range_min
= newmin
* MAX_PEAK
/ 100;
361 pm_range
= peak_meter_range_max
- peak_meter_range_min
;
363 /* Avoid division by zero. */
368 pm_db_min
= calc_db(peak_meter_range_min
);
369 pm_db_range
= pm_db_max
- pm_db_min
;
372 scales
[i
].db_scale_valid
= false;
376 * Returns the minimum value of the range the meter
377 * displays. If the scale is set to dBfs it returns
378 * dBfs values * 100 or linear percent values.
379 * @return: using dBfs : -9000 < value <= 0
380 * using linear scale: 0 <= value <= 100
382 int peak_meter_get_min(void)
386 retval
= calc_db(peak_meter_range_min
) - calc_db(MAX_PEAK
- 1);
388 retval
= peak_meter_range_min
* 100 / MAX_PEAK
;
394 * Set the max value for restriction of the value range.
395 * @param int newmax - depending wether dBfs is used
396 * newmax is a value in dBfs * 100 or in linear percent values.
397 * for dBfs: -9000 < newmax <= 0
398 * for linear: 0 <= newmax <= 100
400 static void peak_meter_set_max(int newmax
)
403 peak_meter_range_max
= peak_meter_db2sample(newmax
);
405 if (newmax
> peak_meter_range_min
) {
406 peak_meter_range_max
= newmax
* MAX_PEAK
/ 100;
410 pm_range
= peak_meter_range_max
- peak_meter_range_min
;
412 /* Avoid division by zero. */
417 pm_db_max
= calc_db(peak_meter_range_max
);
418 pm_db_range
= pm_db_max
- pm_db_min
;
421 scales
[i
].db_scale_valid
= false;
425 * Returns the minimum value of the range the meter
426 * displays. If the scale is set to dBfs it returns
427 * dBfs values * 100 or linear percent values
428 * @return: using dBfs : -9000 < value <= 0
429 * using linear scale: 0 <= value <= 100
431 int peak_meter_get_max(void)
435 retval
= calc_db(peak_meter_range_max
) - calc_db(MAX_PEAK
- 1);
437 retval
= peak_meter_range_max
* 100 / MAX_PEAK
;
443 * Returns whether the meter is currently displaying dBfs or percent values.
444 * @return bool - true if the meter is displaying dBfs
445 false if the meter is displaying percent values.
447 bool peak_meter_get_use_dbfs(void)
453 * Specifies whether the values displayed are scaled
454 * as dBfs or as linear percent values.
455 * @param use - set to true for dBfs,
456 * set to false for linear scaling in percent
458 void peak_meter_set_use_dbfs(bool use
)
463 scales
[i
].db_scale_valid
= false;
467 * Initialize the range of the meter. Only values
468 * that are in the range of [range_min ... range_max]
470 * @param bool dbfs - set to true for dBfs,
471 * set to false for linear scaling in percent
472 * @param int range_min - Specifies the lower value of the range.
473 * Pass a value dBfs * 100 when dbfs is set to true.
474 * Pass a percent value when dbfs is set to false.
475 * @param int range_max - Specifies the upper value of the range.
476 * Pass a value dBfs * 100 when dbfs is set to true.
477 * Pass a percent value when dbfs is set to false.
479 void peak_meter_init_range( bool dbfs
, int range_min
, int range_max
)
482 peak_meter_set_min(range_min
);
483 peak_meter_set_max(range_max
);
487 * Initialize the peak meter with all relevant values concerning times.
488 * @param int release - Set the maximum amount of pixels the meter is allowed
489 * to decrease with each redraw
490 * @param int hold - Select the time preset for the time the peak indicator
491 * is reset after a peak occurred. The preset values are
492 * stored in peak_time_out.
493 * @param int clip_hold - Select the time preset for the time the peak
494 * indicator is reset after a peak occurred. The preset
495 * values are stored in clip_time_out.
497 void peak_meter_init_times(int release
, int hold
, int clip_hold
)
500 pm_peak_release
= release
;
501 pm_clip_hold
= clip_hold
;
504 #ifdef HAVE_RECORDING
506 * Enable/disable clip counting
508 void pm_activate_clipcount(bool active
)
510 pm_clipcount_active
= active
;
514 * Get clipping counter value
516 int pm_get_clipcount(void)
522 * Set clipping counter to zero (typically at start of recording or playback)
524 void pm_reset_clipcount(void)
531 * Set the source of the peak meter to playback or to
533 * @param: bool playback - If true playback peak meter is used.
534 * If false recording peak meter is used.
536 void peak_meter_playback(bool playback
)
541 #elif CONFIG_CODEC == SWCODEC
542 pm_playback
= playback
;
545 pm_src_left
= MAS_REG_DQPEAK_L
;
546 pm_src_right
= MAS_REG_DQPEAK_R
;
548 pm_src_left
= MAS_REG_QPEAK_L
;
549 pm_src_right
= MAS_REG_QPEAK_R
;
552 /* reset the scales just in case recording and playback
553 use different viewport sizes. Normally we should be checking viewport
554 sizes every time but this will do for now */
556 scales
[i
].db_scale_valid
= false;
559 #ifdef HAVE_RECORDING
560 static void set_trig_status(int new_state
)
562 if (trig_status
!= new_state
) {
563 trig_status
= new_state
;
564 if (trigger_listener
!= NULL
) {
565 trigger_listener(trig_status
);
573 * Reads peak values from the MAS, and detects clips. The
574 * values are stored in pm_max_left pm_max_right for later
575 * evauluation. Consecutive calls to peak_meter_peek detect
576 * that ocurred. This function could be used by a thread for
577 * busy reading the MAS.
579 void peak_meter_peek(void)
582 #ifdef HAVE_RECORDING
583 bool was_clipping
= pm_clip_left
|| pm_clip_right
;
585 /* read current values */
586 #if CONFIG_CODEC == SWCODEC
588 pcm_calculate_peaks(&pm_cur_left
, &pm_cur_right
);
589 #ifdef HAVE_RECORDING
591 pcm_calculate_rec_peaks(&pm_cur_left
, &pm_cur_right
);
594 right
= pm_cur_right
;
597 pm_cur_left
= left
= mas_codec_readreg(pm_src_left
);
598 pm_cur_right
= right
= mas_codec_readreg(pm_src_right
);
600 pm_cur_left
= left
= 8000;
601 pm_cur_right
= right
= 9000;
606 An clip is assumed when two consecutive readouts
607 of the volume are at full scale. This is proven
608 to be inaccurate in both ways: it may detect clips
609 when no clip occurred and it may fail to detect
610 a real clip. For software codecs, the peak is already
611 the max of a bunch of samples, so use one max value
612 or you fail to detect clipping! */
613 #if CONFIG_CODEC == SWCODEC
614 if (left
== MAX_PEAK
- 1) {
616 if ((left
== pm_max_left
) &&
617 (left
== MAX_PEAK
- 1)) {
621 current_tick
+ clip_time_out
[pm_clip_hold
];
624 #if CONFIG_CODEC == SWCODEC
625 if (right
== MAX_PEAK
- 1) {
627 if ((right
== pm_max_right
) &&
628 (right
== MAX_PEAK
- 1)) {
630 pm_clip_right
= true;
632 current_tick
+ clip_time_out
[pm_clip_hold
];
635 #ifdef HAVE_RECORDING
636 if(!was_clipping
&& (pm_clip_left
|| pm_clip_right
))
638 if(pm_clipcount_active
)
643 /* peaks are searched -> we have to find the maximum. When
644 many calls of peak_meter_peek the maximum value will be
645 stored in pm_max_xxx. This maximum is reset by the
646 functions peak_meter_read_x. */
647 pm_max_left
= MAX(pm_max_left
, left
);
648 pm_max_right
= MAX(pm_max_right
, right
);
650 #ifdef HAVE_RECORDING
651 #if CONFIG_CODEC == SWCODEC
652 /* Ignore any unread peakmeter data */
653 #define MAX_DROP_TIME HZ/7 /* this value may need tweaking. Increase if you are
654 getting trig events when you shouldn't with
657 trig_stp_hold
= MAX_DROP_TIME
;
660 switch (trig_status
) {
662 /* no more changes, if trigger was activated as release trigger */
663 /* threshold exceeded? */
664 if ((left
> trig_strt_threshold
)
665 || (right
> trig_strt_threshold
)) {
666 /* reset trigger duration */
667 trig_hightime
= current_tick
;
669 /* reset dropout duration */
670 trig_lowtime
= current_tick
;
672 if (trig_strt_duration
)
673 set_trig_status(TRIG_STEADY
);
675 /* if trig_duration is set to 0 the user wants to start
676 recording immediately */
677 set_trig_status(TRIG_GO
);
683 /* trigger duration exceeded */
684 if (current_tick
- trig_hightime
> trig_strt_duration
) {
685 set_trig_status(TRIG_GO
);
687 /* threshold exceeded? */
688 if ((left
> trig_strt_threshold
)
689 || (right
> trig_strt_threshold
)) {
691 trig_lowtime
= current_tick
;
693 /* volume is below threshold */
695 /* dropout occurred? */
696 if (current_tick
- trig_lowtime
> trig_strt_dropout
){
697 if (trig_status
== TRIG_STEADY
){
698 set_trig_status(TRIG_READY
);
700 /* trig_status == TRIG_RETRIG */
702 /* the gap has already expired */
703 trig_lowtime
= current_tick
- trig_rstrt_gap
- 1;
704 set_trig_status(TRIG_POSTREC
);
713 /* threshold exceeded? */
714 if ((left
> trig_stp_threshold
)
715 || (right
> trig_stp_threshold
)) {
716 /* restart hold time countdown */
717 trig_lowtime
= current_tick
;
718 #if CONFIG_CODEC == SWCODEC
719 } else if (current_tick
- trig_lowtime
> MAX_DROP_TIME
){
723 set_trig_status(TRIG_POSTREC
);
724 trig_hightime
= current_tick
;
729 /* gap time expired? */
730 if (current_tick
- trig_lowtime
> trig_rstrt_gap
){
731 /* start threshold exceeded? */
732 if ((left
> trig_strt_threshold
)
733 || (right
> trig_strt_threshold
)) {
735 set_trig_status(TRIG_RETRIG
);
736 trig_hightime
= current_tick
;
737 trig_lowtime
= current_tick
;
741 /* stop threshold exceeded */
742 if ((left
> trig_stp_threshold
)
743 || (right
> trig_stp_threshold
)) {
744 if (current_tick
- trig_hightime
> trig_stp_hold
){
745 trig_lowtime
= current_tick
;
746 set_trig_status(TRIG_CONTINUE
);
748 trig_lowtime
= current_tick
- trig_rstrt_gap
- 1;
752 /* below any threshold */
754 if (current_tick
- trig_lowtime
> trig_stp_hold
){
755 set_trig_status(TRIG_READY
);
757 trig_hightime
= current_tick
;
762 /* still within the gap time */
764 /* stop threshold exceeded */
765 if ((left
> trig_stp_threshold
)
766 || (right
> trig_stp_threshold
)) {
767 set_trig_status(TRIG_CONTINUE
);
768 trig_lowtime
= current_tick
;
771 /* hold time expired */
772 else if (current_tick
- trig_lowtime
> trig_stp_hold
){
773 trig_hightime
= current_tick
;
774 trig_lowtime
= current_tick
;
775 set_trig_status(TRIG_READY
);
780 #if CONFIG_CODEC == SWCODEC
781 /* restore stop hold value */
782 if (trig_stp_hold
== MAX_DROP_TIME
)
786 /* check levels next time peakmeter drawn */
794 * Reads out the peak volume of the left channel.
795 * @return int - The maximum value that has been detected
796 * since the last call of peak_meter_read_l. The value
797 * is in the range 0 <= value < MAX_PEAK.
799 static int peak_meter_read_l(void)
801 /* pm_max_left contains the maximum of all peak values that were read
802 by peak_meter_peek since the last call of peak_meter_read_l */
803 int retval
= pm_max_left
;
806 /* store max peak value for peak_meter_get_peakhold_x readout */
807 pm_peakhold_left
= MAX(pm_max_left
, pm_peakhold_left
);
812 /* reset pm_max_left so that subsequent calls of peak_meter_peek don't
813 get fooled by an old maximum value */
814 pm_max_left
= pm_cur_left
;
816 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
818 retval
= rand()%MAX_PEAK
;
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 */
834 int retval
= pm_max_right
;
837 /* store max peak value for peak_meter_get_peakhold_x readout */
838 pm_peakhold_right
= MAX(pm_max_right
, pm_peakhold_right
);
843 /* reset pm_max_right so that subsequent calls of peak_meter_peek don't
844 get fooled by an old maximum value */
845 pm_max_right
= pm_cur_right
;
847 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
849 retval
= rand()%MAX_PEAK
;
857 * Reads out the current peak-hold values since the last call.
858 * This is used by the histogram feature in the recording screen.
859 * Values are in the range 0 <= peak_x < MAX_PEAK. MAX_PEAK is typ 32767.
861 void peak_meter_get_peakhold(int *peak_left
, int *peak_right
)
864 *peak_left
= pm_peakhold_left
;
866 *peak_right
= pm_peakhold_right
;
867 pm_peakhold_left
= 0;
868 pm_peakhold_right
= 0;
873 * Reset the detected clips. This method is for
874 * use by the user interface.
875 * @param int unused - This parameter was added to
876 * make the function compatible with set_int
878 void peak_meter_set_clip_hold(int time
)
880 pm_clip_left
= false;
881 pm_clip_right
= false;
882 pm_clip_eternal
= (time
> 0) ? false : true;
886 * Scales a peak value as read from the MAS to the range of meterwidth.
887 * The scaling is performed according to the scaling method (dBfs / linear)
888 * and the range (peak_meter_range_min .. peak_meter_range_max).
889 * @param unsigned short val - The volume value. Range: 0 <= val < MAX_PEAK
890 * @param int meterwidht - The widht of the meter in pixel
891 * @return unsigned short - A value 0 <= return value <= meterwidth
893 unsigned short peak_meter_scale_value(unsigned short val
, int meterwidth
)
897 if (val
<= peak_meter_range_min
) {
901 if (val
>= peak_meter_range_max
) {
907 /* different scaling is used for dBfs and linear percent */
910 /* scale the samples dBfs */
911 retval
= (calc_db(retval
) - pm_db_min
) * meterwidth
/ pm_db_range
;
914 /* Scale for linear percent display */
917 /* scale the samples */
918 retval
= ((retval
- peak_meter_range_min
) * meterwidth
)
923 void peak_meter_screen(struct screen
*display
, int x
, int y
, int height
)
925 peak_meter_draw(display
, &scales
[display
->screen_type
], x
, y
,
926 display
->getwidth() - x
, height
);
929 * Draws a peak meter in the specified size at the specified position.
930 * @param int x - The x coordinate.
931 * Make sure that 0 <= x and x + width < display->getwidth()
932 * @param int y - The y coordinate.
933 * Make sure that 0 <= y and y + height < display->getheight()
934 * @param int width - The width of the peak meter. Note that for display
935 * of clips a 3 pixel wide area is used ->
937 * @param int height - The height of the peak meter. height > 3
939 static void peak_meter_draw(struct screen
*display
, struct meter_scales
*scales
,
940 int x
, int y
, int width
, int height
)
942 static int left_level
= 0, right_level
= 0;
943 int left
= 0, right
= 0;
944 int meterwidth
= width
- 3;
946 #if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
947 static long peak_release_tick
[2] = {0,0};
948 int screen_nr
= display
->screen_type
== SCREEN_MAIN
? 0 : 1;
950 static long peak_release_tick
= 0;
954 static long pm_tick
= 0;
955 int tmp
= peek_calls
;
958 /* if disabled only draw the peak meter */
959 if (peak_meter_enabled
) {
963 /* only read the volume info from MAS if peek since last read*/
964 left_level
= peak_meter_read_l();
965 right_level
= peak_meter_read_r();
969 /* scale the samples dBfs */
970 left
= peak_meter_scale_value(left_level
, meterwidth
);
971 right
= peak_meter_scale_value(right_level
, meterwidth
);
973 /*if the scale has changed -> recalculate the scale
974 (The scale becomes invalid when the range changed.) */
975 if (!scales
->db_scale_valid
){
978 db_scale_count
= DB_SCALE_SRC_VALUES_SIZE
;
979 for (i
= 0; i
< db_scale_count
; i
++){
980 /* find the real x-coords for predefined interesting
981 dBfs values. These only are recalculated when the
982 scaling of the meter changed. */
983 scales
->db_scale_lcd_coord
[i
] =
984 peak_meter_scale_value(
985 db_scale_src_values
[i
],
990 /* when scaling linear we simly make 10% steps */
993 for (i
= 0; i
< db_scale_count
; i
++) {
994 scales
->db_scale_lcd_coord
[i
] =
995 (i
* (MAX_PEAK
/ 10) - peak_meter_range_min
) *
996 meterwidth
/ pm_range
;
1000 /* mark scale valid to avoid recalculating dBfs values
1002 scales
->db_scale_valid
= true;
1006 #if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
1007 delta
= current_tick
- peak_release_tick
[screen_nr
];
1008 peak_release_tick
[screen_nr
] = current_tick
;
1010 delta
= current_tick
- peak_release_tick
;
1011 peak_release_tick
= current_tick
;
1013 left
= MAX(left
, scales
->last_left
- delta
* pm_peak_release
);
1014 right
= MAX(right
, scales
->last_right
- delta
* pm_peak_release
);
1016 /* reset max values after timeout */
1017 if (TIME_AFTER(current_tick
, scales
->pm_peak_timeout_l
)){
1018 scales
->pm_peak_left
= 0;
1021 if (TIME_AFTER(current_tick
, scales
->pm_peak_timeout_r
)){
1022 scales
->pm_peak_right
= 0;
1025 if (!pm_clip_eternal
) {
1027 TIME_AFTER(current_tick
, pm_clip_timeout_l
)){
1028 pm_clip_left
= false;
1031 if (pm_clip_right
&&
1032 TIME_AFTER(current_tick
, pm_clip_timeout_r
)){
1033 pm_clip_right
= false;
1037 /* check for new max values */
1038 if (left
> scales
->pm_peak_left
) {
1039 scales
->pm_peak_left
= left
- 1;
1040 scales
->pm_peak_timeout_l
=
1041 current_tick
+ peak_time_out
[pm_peak_hold
];
1044 if (right
> scales
->pm_peak_right
) {
1045 scales
->pm_peak_right
= right
- 1;
1046 scales
->pm_peak_timeout_r
=
1047 current_tick
+ peak_time_out
[pm_peak_hold
];
1051 /* draw the peak meter */
1052 display
->set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
1053 display
->fillrect(x
, y
, width
, height
);
1054 display
->set_drawmode(DRMODE_SOLID
);
1057 display
->fillrect (x
, y
, left
, height
/ 2 - 2 );
1058 if (scales
->pm_peak_left
> 0) {
1059 display
->vline(x
+ scales
->pm_peak_left
, y
, y
+ height
/ 2 - 2 );
1062 display
->fillrect(x
+ meterwidth
, y
, 3, height
/ 2 - 1);
1066 display
->fillrect(x
, y
+ height
/ 2 + 1, right
, height
/ 2 - 2);
1067 if (scales
->pm_peak_right
> 0) {
1068 display
->vline( x
+ scales
->pm_peak_right
, y
+ height
/ 2, y
+ height
- 2);
1070 if (pm_clip_right
) {
1071 display
->fillrect(x
+ meterwidth
, y
+ height
/ 2, 3, height
/ 2 - 1);
1074 /* draw scale end */
1075 display
->vline(x
+ meterwidth
, y
, y
+ height
- 2);
1077 /* draw dots for scale marks */
1078 for (i
= 0; i
< db_scale_count
; i
++) {
1079 /* The x-coordinates of interesting scale mark points
1080 have been calculated before */
1081 display
->drawpixel(x
+ scales
->db_scale_lcd_coord
[i
],
1082 y
+ height
/ 2 - 1);
1085 #ifdef HAVE_RECORDING
1087 #ifdef HAVE_BACKLIGHT
1089 if ((pm_clip_left
|| pm_clip_right
) &&
1090 global_settings
.cliplight
&&
1091 #if CONFIG_CODEC == SWCODEC
1094 !(audio_status() & (AUDIO_STATUS_PLAY
| AUDIO_STATUS_ERROR
)))
1097 /* if clipping, cliplight setting on and in recording screen */
1098 if (global_settings
.cliplight
<= 2)
1100 /* turn on main unit light if setting set to main or both*/
1103 #ifdef HAVE_REMOTE_LCD
1104 if (global_settings
.cliplight
>= 2)
1106 /* turn remote light unit on if setting set to remote or both */
1107 remote_backlight_on();
1109 #endif /* HAVE_REMOTE_LCD */
1111 #endif /* HAVE_BACKLIGHT */
1113 if (trig_status
!= TRIG_OFF
) {
1114 int start_trigx
, stop_trigx
, ycenter
;
1116 display
->set_drawmode(DRMODE_SOLID
);
1117 ycenter
= y
+ height
/ 2;
1118 /* display threshold value */
1119 start_trigx
= x
+peak_meter_scale_value(trig_strt_threshold
,meterwidth
);
1120 display
->vline(start_trigx
, ycenter
- 2, ycenter
);
1122 if (start_trigx
< display
->getwidth() ) display
->drawpixel(start_trigx
,
1125 stop_trigx
= x
+ peak_meter_scale_value(trig_stp_threshold
,meterwidth
);
1126 display
->vline(stop_trigx
, ycenter
- 2, ycenter
);
1127 if (stop_trigx
> 0) display
->drawpixel(stop_trigx
- 1, ycenter
- 1);
1129 #endif /*HAVE_RECORDING*/
1132 /* display a bar to show how many calls to peak_meter_peek
1133 have ocurred since the last display */
1134 display
->set_drawmode(DRMODE_COMPLEMENT
);
1135 display
->fillrect(x
, y
, tmp
, 3);
1137 if (tmp
< PEEKS_PER_DRAW_SIZE
) {
1138 peeks_per_redraw
[tmp
]++;
1141 tmp
= current_tick
- pm_tick
;
1142 if (tmp
< TICKS_PER_DRAW_SIZE
){
1143 ticks_per_redraw
[tmp
] ++;
1146 /* display a bar to show how many ticks have passed since
1148 display
->fillrect(x
, y
+ height
/ 2, current_tick
- pm_tick
, 2);
1149 pm_tick
= current_tick
;
1152 scales
->last_left
= left
;
1153 scales
->last_right
= right
;
1155 display
->set_drawmode(DRMODE_SOLID
);
1158 #ifdef HAVE_RECORDING
1160 * Defines the parameters of the trigger. After these parameters are defined
1161 * the trigger can be started either by peak_meter_attack_trigger or by
1162 * peak_meter_release_trigger. Note that you can pass either linear (%) or
1163 * logarithmic (db) values to the thresholds. Positive values are intepreted as
1164 * percent (0 is 0% .. 100 is 100%). Negative values are interpreted as db.
1165 * To avoid ambiguosity of the value 0 the negative values are shifted by -1.
1166 * Thus -75 is -74db .. -1 is 0db.
1167 * @param start_threshold - The threshold used for attack trigger. Negative
1168 * values are interpreted as db -1, positive as %.
1169 * @param start_duration - The minimum time span within which start_threshold
1170 * must be exceeded to fire the attack trigger.
1171 * @param start_dropout - The maximum time span the level may fall below
1172 * start_threshold without releasing the attack trigger.
1173 * @param stop_threshold - The threshold the volume must fall below to release
1174 * the release trigger.Negative values are
1175 * interpreted as db -1, positive as %.
1176 * @param stop_hold - The minimum time the volume must fall below the
1177 * stop_threshold to release the trigger.
1180 void peak_meter_define_trigger(
1181 int start_threshold
,
1182 long start_duration
,
1185 long stop_hold_time
,
1189 if (start_threshold
< 0) {
1191 if (start_threshold
< -89) {
1192 trig_strt_threshold
= 0;
1194 trig_strt_threshold
=peak_meter_db2sample((start_threshold
+1)*100);
1197 /* linear percent */
1198 trig_strt_threshold
= start_threshold
* MAX_PEAK
/ 100;
1200 trig_strt_duration
= start_duration
;
1201 trig_strt_dropout
= start_dropout
;
1202 if (stop_threshold
< 0) {
1204 trig_stp_threshold
= peak_meter_db2sample((stop_threshold
+ 1) * 100);
1206 /* linear percent */
1207 trig_stp_threshold
= stop_threshold
* MAX_PEAK
/ 100;
1209 trig_stp_hold
= stop_hold_time
;
1210 trig_rstrt_gap
= restart_gap
;
1214 * Enables or disables the trigger.
1215 * @param on - If true the trigger is turned on.
1217 void peak_meter_trigger(bool on
)
1219 /* don't use set_trigger here as that would fire an undesired event */
1220 trig_status
= on
? TRIG_READY
: TRIG_OFF
;
1224 * Registers the listener function that listenes on trig_status changes.
1225 * @param listener - The function that is called with each change of
1226 * trig_status. May be set to NULL if no callback is desired.
1228 void peak_meter_set_trigger_listener(void (*listener
)(int status
))
1230 trigger_listener
= listener
;
1234 * Fetches the status of the trigger.
1235 * TRIG_OFF: the trigger is inactive
1236 * TRIG_RELEASED: The volume level is below the threshold
1237 * TRIG_ACTIVATED: The volume level has exceeded the threshold, but the trigger
1238 * hasn't been fired yet.
1239 * TRIG_FIRED: The volume exceeds the threshold
1241 * To activate the trigger call either peak_meter_attack_trigger or
1242 * peak_meter_release_trigger. To turn the trigger off call
1243 * peak_meter_trigger_off.
1245 int peak_meter_trigger_status(void)
1247 return trig_status
; /* & TRIG_PIT_MASK;*/
1250 void peak_meter_draw_trig(int xpos
[], int ypos
[],
1251 int trig_width
[], int nb_screens
)
1253 int barstart
[NB_SCREENS
];
1254 int barend
[NB_SCREENS
];
1256 int ixpos
[NB_SCREENS
];
1258 int trigbar_width
[NB_SCREENS
];
1261 trigbar_width
[i
] = (trig_width
[i
] - (2 * (ICON_PLAY_STATE_WIDTH
+ 1)));
1263 switch (trig_status
) {
1280 barend
[i
] = (trig_strt_duration
== 0) ? trigbar_width
[i
] :
1282 (current_tick
- trig_hightime
) / trig_strt_duration
;
1293 barstart
[i
] = trigbar_width
[i
];
1294 barend
[i
] = trigbar_width
[i
];
1298 ixpos
[i
] = xpos
[i
]+ trig_width
[i
] - ICON_PLAY_STATE_WIDTH
;
1304 barstart
[i
] = (trig_stp_hold
== 0) ? 0 :
1305 trigbar_width
[i
] - trigbar_width
[i
] *
1306 (current_tick
- trig_lowtime
) / trig_stp_hold
;
1307 barend
[i
] = trigbar_width
[i
];
1311 ixpos
[i
] = xpos
[i
] + trig_width
[i
] - ICON_PLAY_STATE_WIDTH
;
1318 for(i
= 0; i
< nb_screens
; i
++)
1320 gui_scrollbar_draw(&screens
[i
], xpos
[i
] + ICON_PLAY_STATE_WIDTH
+ 1,
1321 ypos
[i
] + 1, trigbar_width
[i
], TRIG_HEIGHT
- 2,
1322 trigbar_width
[i
], barstart
[i
], barend
[i
],
1325 screens
[i
].mono_bitmap(bitmap_icons_7x8
[icon
], ixpos
[i
], ypos
[i
],
1326 ICON_PLAY_STATE_WIDTH
, STATUSBAR_HEIGHT
);
1331 int peak_meter_draw_get_btn(int action_context
, int x
[], int y
[],
1332 int height
[], int nb_screens
,
1333 struct viewport vps
[])
1335 int button
= BUTTON_NONE
;
1336 long next_refresh
= current_tick
;
1337 long next_big_refresh
= current_tick
+ HZ
/ 10;
1339 #if (CONFIG_CODEC == SWCODEC)
1340 bool highperf
= false;
1342 /* On MAS targets, we need to poll as often as possible in order to not
1343 * miss a peak, as the MAS does only provide a quasi-peak. When the disk
1344 * is active, it must not draw too much CPU power or a buffer overrun can
1345 * happen when saving a recording. As a compromise, poll only once per tick
1346 * when the disk is active, otherwise spin around as fast as possible. */
1347 bool highperf
= !storage_disk_is_active();
1351 while (TIME_BEFORE(current_tick
, next_big_refresh
)) {
1352 button
= get_action(action_context
, TIMEOUT_NOBLOCK
);
1353 if (button
!= BUTTON_NONE
) {
1356 if (dopeek
) { /* Peek only once per refresh when disk is */
1357 peak_meter_peek(); /* spinning, but as often as possible */
1358 dopeek
= highperf
; /* otherwise. */
1361 sleep(0); /* Sleep until end of current tick. */
1363 if (TIME_AFTER(current_tick
, next_refresh
)) {
1364 for(i
= 0; i
< nb_screens
; i
++)
1366 screens
[i
].set_viewport(&vps
[i
]);
1367 peak_meter_screen(&screens
[i
], x
[i
], y
[i
], height
[i
]);
1368 screens
[i
].update_viewport_rect(x
[i
], y
[i
],
1369 screens
[i
].getwidth() - x
[i
],
1372 next_refresh
+= HZ
/ PEAK_METER_FPS
;
1381 static void peak_meter_clear_histogram(void)
1384 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1385 ticks_per_redraw
[i
] = (unsigned int)0;
1388 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1389 peeks_per_redraw
[i
] = (unsigned int)0;
1393 bool peak_meter_histogram(void)
1396 int btn
= BUTTON_NONE
;
1397 while ((btn
& BUTTON_OFF
) != BUTTON_OFF
)
1399 unsigned int max
= 0;
1402 screens
[0].clear_display();
1404 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1405 max
= MAX(max
, peeks_per_redraw
[i
]);
1408 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1409 x
= peeks_per_redraw
[i
] * (LCD_WIDTH
- 1)/ max
;
1410 screens
[0].hline(0, x
, y
+ i
);
1413 y
= PEEKS_PER_DRAW_SIZE
+ 1;
1416 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1417 max
= MAX(max
, ticks_per_redraw
[i
]);
1420 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1421 x
= ticks_per_redraw
[i
] * (LCD_WIDTH
- 1)/ max
;
1422 screens
[0].hline(0, x
, y
+ i
);
1424 screens
[0].update();
1426 btn
= button_get(true);
1427 if (btn
== BUTTON_PLAY
) {
1428 peak_meter_clear_histogram();