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
)
540 #elif CONFIG_CODEC == SWCODEC
541 pm_playback
= playback
;
544 pm_src_left
= MAS_REG_DQPEAK_L
;
545 pm_src_right
= MAS_REG_DQPEAK_R
;
547 pm_src_left
= MAS_REG_QPEAK_L
;
548 pm_src_right
= MAS_REG_QPEAK_R
;
553 #ifdef HAVE_RECORDING
554 static void set_trig_status(int new_state
)
556 if (trig_status
!= new_state
) {
557 trig_status
= new_state
;
558 if (trigger_listener
!= NULL
) {
559 trigger_listener(trig_status
);
567 * Reads peak values from the MAS, and detects clips. The
568 * values are stored in pm_max_left pm_max_right for later
569 * evauluation. Consecutive calls to peak_meter_peek detect
570 * that ocurred. This function could be used by a thread for
571 * busy reading the MAS.
573 void peak_meter_peek(void)
576 #ifdef HAVE_RECORDING
577 bool was_clipping
= pm_clip_left
|| pm_clip_right
;
579 /* read current values */
580 #if CONFIG_CODEC == SWCODEC
582 pcm_calculate_peaks(&pm_cur_left
, &pm_cur_right
);
583 #ifdef HAVE_RECORDING
585 pcm_calculate_rec_peaks(&pm_cur_left
, &pm_cur_right
);
588 right
= pm_cur_right
;
591 pm_cur_left
= left
= mas_codec_readreg(pm_src_left
);
592 pm_cur_right
= right
= mas_codec_readreg(pm_src_right
);
594 pm_cur_left
= left
= 8000;
595 pm_cur_right
= right
= 9000;
600 An clip is assumed when two consecutive readouts
601 of the volume are at full scale. This is proven
602 to be inaccurate in both ways: it may detect clips
603 when no clip occurred and it may fail to detect
604 a real clip. For software codecs, the peak is already
605 the max of a bunch of samples, so use one max value
606 or you fail to detect clipping! */
607 #if CONFIG_CODEC == SWCODEC
608 if (left
== MAX_PEAK
- 1) {
610 if ((left
== pm_max_left
) &&
611 (left
== MAX_PEAK
- 1)) {
615 current_tick
+ clip_time_out
[pm_clip_hold
];
618 #if CONFIG_CODEC == SWCODEC
619 if (right
== MAX_PEAK
- 1) {
621 if ((right
== pm_max_right
) &&
622 (right
== MAX_PEAK
- 1)) {
624 pm_clip_right
= true;
626 current_tick
+ clip_time_out
[pm_clip_hold
];
629 #ifdef HAVE_RECORDING
630 if(!was_clipping
&& (pm_clip_left
|| pm_clip_right
))
632 if(pm_clipcount_active
)
637 /* peaks are searched -> we have to find the maximum. When
638 many calls of peak_meter_peek the maximum value will be
639 stored in pm_max_xxx. This maximum is reset by the
640 functions peak_meter_read_x. */
641 pm_max_left
= MAX(pm_max_left
, left
);
642 pm_max_right
= MAX(pm_max_right
, right
);
644 #ifdef HAVE_RECORDING
645 #if CONFIG_CODEC == SWCODEC
646 /* Ignore any unread peakmeter data */
647 #define MAX_DROP_TIME HZ/7 /* this value may need tweaking. Increase if you are
648 getting trig events when you shouldn't with
651 trig_stp_hold
= MAX_DROP_TIME
;
654 switch (trig_status
) {
656 /* no more changes, if trigger was activated as release trigger */
657 /* threshold exceeded? */
658 if ((left
> trig_strt_threshold
)
659 || (right
> trig_strt_threshold
)) {
660 /* reset trigger duration */
661 trig_hightime
= current_tick
;
663 /* reset dropout duration */
664 trig_lowtime
= current_tick
;
666 if (trig_strt_duration
)
667 set_trig_status(TRIG_STEADY
);
669 /* if trig_duration is set to 0 the user wants to start
670 recording immediately */
671 set_trig_status(TRIG_GO
);
677 /* trigger duration exceeded */
678 if (current_tick
- trig_hightime
> trig_strt_duration
) {
679 set_trig_status(TRIG_GO
);
681 /* threshold exceeded? */
682 if ((left
> trig_strt_threshold
)
683 || (right
> trig_strt_threshold
)) {
685 trig_lowtime
= current_tick
;
687 /* volume is below threshold */
689 /* dropout occurred? */
690 if (current_tick
- trig_lowtime
> trig_strt_dropout
){
691 if (trig_status
== TRIG_STEADY
){
692 set_trig_status(TRIG_READY
);
694 /* trig_status == TRIG_RETRIG */
696 /* the gap has already expired */
697 trig_lowtime
= current_tick
- trig_rstrt_gap
- 1;
698 set_trig_status(TRIG_POSTREC
);
707 /* threshold exceeded? */
708 if ((left
> trig_stp_threshold
)
709 || (right
> trig_stp_threshold
)) {
710 /* restart hold time countdown */
711 trig_lowtime
= current_tick
;
712 #if CONFIG_CODEC == SWCODEC
713 } else if (current_tick
- trig_lowtime
> MAX_DROP_TIME
){
717 set_trig_status(TRIG_POSTREC
);
718 trig_hightime
= current_tick
;
723 /* gap time expired? */
724 if (current_tick
- trig_lowtime
> trig_rstrt_gap
){
725 /* start threshold exceeded? */
726 if ((left
> trig_strt_threshold
)
727 || (right
> trig_strt_threshold
)) {
729 set_trig_status(TRIG_RETRIG
);
730 trig_hightime
= current_tick
;
731 trig_lowtime
= current_tick
;
735 /* stop threshold exceeded */
736 if ((left
> trig_stp_threshold
)
737 || (right
> trig_stp_threshold
)) {
738 if (current_tick
- trig_hightime
> trig_stp_hold
){
739 trig_lowtime
= current_tick
;
740 set_trig_status(TRIG_CONTINUE
);
742 trig_lowtime
= current_tick
- trig_rstrt_gap
- 1;
746 /* below any threshold */
748 if (current_tick
- trig_lowtime
> trig_stp_hold
){
749 set_trig_status(TRIG_READY
);
751 trig_hightime
= current_tick
;
756 /* still within the gap time */
758 /* stop threshold exceeded */
759 if ((left
> trig_stp_threshold
)
760 || (right
> trig_stp_threshold
)) {
761 set_trig_status(TRIG_CONTINUE
);
762 trig_lowtime
= current_tick
;
765 /* hold time expired */
766 else if (current_tick
- trig_lowtime
> trig_stp_hold
){
767 trig_hightime
= current_tick
;
768 trig_lowtime
= current_tick
;
769 set_trig_status(TRIG_READY
);
774 #if CONFIG_CODEC == SWCODEC
775 /* restore stop hold value */
776 if (trig_stp_hold
== MAX_DROP_TIME
)
780 /* check levels next time peakmeter drawn */
788 * Reads out the peak volume of the left channel.
789 * @return int - The maximum value that has been detected
790 * since the last call of peak_meter_read_l. The value
791 * is in the range 0 <= value < MAX_PEAK.
793 static int peak_meter_read_l(void)
795 /* pm_max_left contains the maximum of all peak values that were read
796 by peak_meter_peek since the last call of peak_meter_read_l */
797 int retval
= pm_max_left
;
800 /* store max peak value for peak_meter_get_peakhold_x readout */
801 pm_peakhold_left
= MAX(pm_max_left
, pm_peakhold_left
);
806 /* reset pm_max_left so that subsequent calls of peak_meter_peek don't
807 get fooled by an old maximum value */
808 pm_max_left
= pm_cur_left
;
810 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
812 retval
= rand()%MAX_PEAK
;
819 * Reads out the peak volume of the right channel.
820 * @return int - The maximum value that has been detected
821 * since the last call of peak_meter_read_l. The value
822 * is in the range 0 <= value < MAX_PEAK.
824 static int peak_meter_read_r(void)
826 /* peak_meter_r contains the maximum of all peak values that were read
827 by peak_meter_peek since the last call of peak_meter_read_r */
828 int retval
= pm_max_right
;
831 /* store max peak value for peak_meter_get_peakhold_x readout */
832 pm_peakhold_right
= MAX(pm_max_right
, pm_peakhold_right
);
837 /* reset pm_max_right so that subsequent calls of peak_meter_peek don't
838 get fooled by an old maximum value */
839 pm_max_right
= pm_cur_right
;
841 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
843 retval
= rand()%MAX_PEAK
;
851 * Reads out the current peak-hold values since the last call.
852 * This is used by the histogram feature in the recording screen.
853 * Values are in the range 0 <= peak_x < MAX_PEAK. MAX_PEAK is typ 32767.
855 void peak_meter_get_peakhold(int *peak_left
, int *peak_right
)
858 *peak_left
= pm_peakhold_left
;
860 *peak_right
= pm_peakhold_right
;
861 pm_peakhold_left
= 0;
862 pm_peakhold_right
= 0;
867 * Reset the detected clips. This method is for
868 * use by the user interface.
869 * @param int unused - This parameter was added to
870 * make the function compatible with set_int
872 void peak_meter_set_clip_hold(int time
)
874 pm_clip_left
= false;
875 pm_clip_right
= false;
876 pm_clip_eternal
= (time
> 0) ? false : true;
880 * Scales a peak value as read from the MAS to the range of meterwidth.
881 * The scaling is performed according to the scaling method (dBfs / linear)
882 * and the range (peak_meter_range_min .. peak_meter_range_max).
883 * @param unsigned short val - The volume value. Range: 0 <= val < MAX_PEAK
884 * @param int meterwidht - The widht of the meter in pixel
885 * @return unsigned short - A value 0 <= return value <= meterwidth
887 unsigned short peak_meter_scale_value(unsigned short val
, int meterwidth
)
891 if (val
<= peak_meter_range_min
) {
895 if (val
>= peak_meter_range_max
) {
901 /* different scaling is used for dBfs and linear percent */
904 /* scale the samples dBfs */
905 retval
= (calc_db(retval
) - pm_db_min
) * meterwidth
/ pm_db_range
;
908 /* Scale for linear percent display */
911 /* scale the samples */
912 retval
= ((retval
- peak_meter_range_min
) * meterwidth
)
917 void peak_meter_screen(struct screen
*display
, int x
, int y
, int height
)
919 peak_meter_draw(display
, &scales
[display
->screen_type
], x
, y
,
920 display
->getwidth() - x
, height
);
923 * Draws a peak meter in the specified size at the specified position.
924 * @param int x - The x coordinate.
925 * Make sure that 0 <= x and x + width < display->getwidth()
926 * @param int y - The y coordinate.
927 * Make sure that 0 <= y and y + height < display->getheight()
928 * @param int width - The width of the peak meter. Note that for display
929 * of clips a 3 pixel wide area is used ->
931 * @param int height - The height of the peak meter. height > 3
933 static void peak_meter_draw(struct screen
*display
, struct meter_scales
*scales
,
934 int x
, int y
, int width
, int height
)
936 static int left_level
= 0, right_level
= 0;
937 int left
= 0, right
= 0;
938 int meterwidth
= width
- 3;
940 #if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
941 static long peak_release_tick
[2] = {0,0};
942 int screen_nr
= display
->screen_type
== SCREEN_MAIN
? 0 : 1;
944 static long peak_release_tick
= 0;
948 static long pm_tick
= 0;
949 int tmp
= peek_calls
;
952 /* if disabled only draw the peak meter */
953 if (peak_meter_enabled
) {
957 /* only read the volume info from MAS if peek since last read*/
958 left_level
= peak_meter_read_l();
959 right_level
= peak_meter_read_r();
963 /* scale the samples dBfs */
964 left
= peak_meter_scale_value(left_level
, meterwidth
);
965 right
= peak_meter_scale_value(right_level
, meterwidth
);
967 /*if the scale has changed -> recalculate the scale
968 (The scale becomes invalid when the range changed.) */
969 if (!scales
->db_scale_valid
){
972 db_scale_count
= DB_SCALE_SRC_VALUES_SIZE
;
973 for (i
= 0; i
< db_scale_count
; i
++){
974 /* find the real x-coords for predefined interesting
975 dBfs values. These only are recalculated when the
976 scaling of the meter changed. */
977 scales
->db_scale_lcd_coord
[i
] =
978 peak_meter_scale_value(
979 db_scale_src_values
[i
],
984 /* when scaling linear we simly make 10% steps */
987 for (i
= 0; i
< db_scale_count
; i
++) {
988 scales
->db_scale_lcd_coord
[i
] =
989 (i
* (MAX_PEAK
/ 10) - peak_meter_range_min
) *
990 meterwidth
/ pm_range
;
994 /* mark scale valid to avoid recalculating dBfs values
996 scales
->db_scale_valid
= true;
1000 #if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
1001 delta
= current_tick
- peak_release_tick
[screen_nr
];
1002 peak_release_tick
[screen_nr
] = current_tick
;
1004 delta
= current_tick
- peak_release_tick
;
1005 peak_release_tick
= current_tick
;
1007 left
= MAX(left
, scales
->last_left
- delta
* pm_peak_release
);
1008 right
= MAX(right
, scales
->last_right
- delta
* pm_peak_release
);
1010 /* reset max values after timeout */
1011 if (TIME_AFTER(current_tick
, scales
->pm_peak_timeout_l
)){
1012 scales
->pm_peak_left
= 0;
1015 if (TIME_AFTER(current_tick
, scales
->pm_peak_timeout_r
)){
1016 scales
->pm_peak_right
= 0;
1019 if (!pm_clip_eternal
) {
1021 TIME_AFTER(current_tick
, pm_clip_timeout_l
)){
1022 pm_clip_left
= false;
1025 if (pm_clip_right
&&
1026 TIME_AFTER(current_tick
, pm_clip_timeout_r
)){
1027 pm_clip_right
= false;
1031 /* check for new max values */
1032 if (left
> scales
->pm_peak_left
) {
1033 scales
->pm_peak_left
= left
- 1;
1034 scales
->pm_peak_timeout_l
=
1035 current_tick
+ peak_time_out
[pm_peak_hold
];
1038 if (right
> scales
->pm_peak_right
) {
1039 scales
->pm_peak_right
= right
- 1;
1040 scales
->pm_peak_timeout_r
=
1041 current_tick
+ peak_time_out
[pm_peak_hold
];
1045 /* draw the peak meter */
1046 display
->set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
1047 display
->fillrect(x
, y
, width
, height
);
1048 display
->set_drawmode(DRMODE_SOLID
);
1051 display
->fillrect (x
, y
, left
, height
/ 2 - 2 );
1052 if (scales
->pm_peak_left
> 0) {
1053 display
->vline(x
+ scales
->pm_peak_left
, y
, y
+ height
/ 2 - 2 );
1056 display
->fillrect(x
+ meterwidth
, y
, 3, height
/ 2 - 1);
1060 display
->fillrect(x
, y
+ height
/ 2 + 1, right
, height
/ 2 - 2);
1061 if (scales
->pm_peak_right
> 0) {
1062 display
->vline( x
+ scales
->pm_peak_right
, y
+ height
/ 2, y
+ height
- 2);
1064 if (pm_clip_right
) {
1065 display
->fillrect(x
+ meterwidth
, y
+ height
/ 2, 3, height
/ 2 - 1);
1068 /* draw scale end */
1069 display
->vline(x
+ meterwidth
, y
, y
+ height
- 2);
1071 /* draw dots for scale marks */
1072 for (i
= 0; i
< db_scale_count
; i
++) {
1073 /* The x-coordinates of interesting scale mark points
1074 have been calculated before */
1075 display
->drawpixel(x
+ scales
->db_scale_lcd_coord
[i
],
1076 y
+ height
/ 2 - 1);
1079 #ifdef HAVE_RECORDING
1081 #ifdef HAVE_BACKLIGHT
1083 if ((pm_clip_left
|| pm_clip_right
) &&
1084 global_settings
.cliplight
&&
1085 #if CONFIG_CODEC == SWCODEC
1088 !(audio_status() & (AUDIO_STATUS_PLAY
| AUDIO_STATUS_ERROR
)))
1091 /* if clipping, cliplight setting on and in recording screen */
1092 if (global_settings
.cliplight
<= 2)
1094 /* turn on main unit light if setting set to main or both*/
1097 #ifdef HAVE_REMOTE_LCD
1098 if (global_settings
.cliplight
>= 2)
1100 /* turn remote light unit on if setting set to remote or both */
1101 remote_backlight_on();
1103 #endif /* HAVE_REMOTE_LCD */
1105 #endif /* HAVE_BACKLIGHT */
1107 if (trig_status
!= TRIG_OFF
) {
1108 int start_trigx
, stop_trigx
, ycenter
;
1110 display
->set_drawmode(DRMODE_SOLID
);
1111 ycenter
= y
+ height
/ 2;
1112 /* display threshold value */
1113 start_trigx
= x
+peak_meter_scale_value(trig_strt_threshold
,meterwidth
);
1114 display
->vline(start_trigx
, ycenter
- 2, ycenter
);
1116 if (start_trigx
< display
->getwidth() ) display
->drawpixel(start_trigx
,
1119 stop_trigx
= x
+ peak_meter_scale_value(trig_stp_threshold
,meterwidth
);
1120 display
->vline(stop_trigx
, ycenter
- 2, ycenter
);
1121 if (stop_trigx
> 0) display
->drawpixel(stop_trigx
- 1, ycenter
- 1);
1123 #endif /*HAVE_RECORDING*/
1126 /* display a bar to show how many calls to peak_meter_peek
1127 have ocurred since the last display */
1128 display
->set_drawmode(DRMODE_COMPLEMENT
);
1129 display
->fillrect(x
, y
, tmp
, 3);
1131 if (tmp
< PEEKS_PER_DRAW_SIZE
) {
1132 peeks_per_redraw
[tmp
]++;
1135 tmp
= current_tick
- pm_tick
;
1136 if (tmp
< TICKS_PER_DRAW_SIZE
){
1137 ticks_per_redraw
[tmp
] ++;
1140 /* display a bar to show how many ticks have passed since
1142 display
->fillrect(x
, y
+ height
/ 2, current_tick
- pm_tick
, 2);
1143 pm_tick
= current_tick
;
1146 scales
->last_left
= left
;
1147 scales
->last_right
= right
;
1149 display
->set_drawmode(DRMODE_SOLID
);
1152 #ifdef HAVE_RECORDING
1154 * Defines the parameters of the trigger. After these parameters are defined
1155 * the trigger can be started either by peak_meter_attack_trigger or by
1156 * peak_meter_release_trigger. Note that you can pass either linear (%) or
1157 * logarithmic (db) values to the thresholds. Positive values are intepreted as
1158 * percent (0 is 0% .. 100 is 100%). Negative values are interpreted as db.
1159 * To avoid ambiguosity of the value 0 the negative values are shifted by -1.
1160 * Thus -75 is -74db .. -1 is 0db.
1161 * @param start_threshold - The threshold used for attack trigger. Negative
1162 * values are interpreted as db -1, positive as %.
1163 * @param start_duration - The minimum time span within which start_threshold
1164 * must be exceeded to fire the attack trigger.
1165 * @param start_dropout - The maximum time span the level may fall below
1166 * start_threshold without releasing the attack trigger.
1167 * @param stop_threshold - The threshold the volume must fall below to release
1168 * the release trigger.Negative values are
1169 * interpreted as db -1, positive as %.
1170 * @param stop_hold - The minimum time the volume must fall below the
1171 * stop_threshold to release the trigger.
1174 void peak_meter_define_trigger(
1175 int start_threshold
,
1176 long start_duration
,
1179 long stop_hold_time
,
1183 if (start_threshold
< 0) {
1185 if (start_threshold
< -89) {
1186 trig_strt_threshold
= 0;
1188 trig_strt_threshold
=peak_meter_db2sample((start_threshold
+1)*100);
1191 /* linear percent */
1192 trig_strt_threshold
= start_threshold
* MAX_PEAK
/ 100;
1194 trig_strt_duration
= start_duration
;
1195 trig_strt_dropout
= start_dropout
;
1196 if (stop_threshold
< 0) {
1198 trig_stp_threshold
= peak_meter_db2sample((stop_threshold
+ 1) * 100);
1200 /* linear percent */
1201 trig_stp_threshold
= stop_threshold
* MAX_PEAK
/ 100;
1203 trig_stp_hold
= stop_hold_time
;
1204 trig_rstrt_gap
= restart_gap
;
1208 * Enables or disables the trigger.
1209 * @param on - If true the trigger is turned on.
1211 void peak_meter_trigger(bool on
)
1213 /* don't use set_trigger here as that would fire an undesired event */
1214 trig_status
= on
? TRIG_READY
: TRIG_OFF
;
1218 * Registers the listener function that listenes on trig_status changes.
1219 * @param listener - The function that is called with each change of
1220 * trig_status. May be set to NULL if no callback is desired.
1222 void peak_meter_set_trigger_listener(void (*listener
)(int status
))
1224 trigger_listener
= listener
;
1228 * Fetches the status of the trigger.
1229 * TRIG_OFF: the trigger is inactive
1230 * TRIG_RELEASED: The volume level is below the threshold
1231 * TRIG_ACTIVATED: The volume level has exceeded the threshold, but the trigger
1232 * hasn't been fired yet.
1233 * TRIG_FIRED: The volume exceeds the threshold
1235 * To activate the trigger call either peak_meter_attack_trigger or
1236 * peak_meter_release_trigger. To turn the trigger off call
1237 * peak_meter_trigger_off.
1239 int peak_meter_trigger_status(void)
1241 return trig_status
; /* & TRIG_PIT_MASK;*/
1244 void peak_meter_draw_trig(int xpos
[], int ypos
[],
1245 int trig_width
[], int nb_screens
)
1247 int barstart
[NB_SCREENS
];
1248 int barend
[NB_SCREENS
];
1250 int ixpos
[NB_SCREENS
];
1252 int trigbar_width
[NB_SCREENS
];
1255 trigbar_width
[i
] = (trig_width
[i
] - (2 * (ICON_PLAY_STATE_WIDTH
+ 1)));
1257 switch (trig_status
) {
1274 barend
[i
] = (trig_strt_duration
== 0) ? trigbar_width
[i
] :
1276 (current_tick
- trig_hightime
) / trig_strt_duration
;
1287 barstart
[i
] = trigbar_width
[i
];
1288 barend
[i
] = trigbar_width
[i
];
1292 ixpos
[i
] = xpos
[i
]+ trig_width
[i
] - ICON_PLAY_STATE_WIDTH
;
1298 barstart
[i
] = (trig_stp_hold
== 0) ? 0 :
1299 trigbar_width
[i
] - trigbar_width
[i
] *
1300 (current_tick
- trig_lowtime
) / trig_stp_hold
;
1301 barend
[i
] = trigbar_width
[i
];
1305 ixpos
[i
] = xpos
[i
] + trig_width
[i
] - ICON_PLAY_STATE_WIDTH
;
1312 for(i
= 0; i
< nb_screens
; i
++)
1314 gui_scrollbar_draw(&screens
[i
], xpos
[i
] + ICON_PLAY_STATE_WIDTH
+ 1,
1315 ypos
[i
] + 1, trigbar_width
[i
], TRIG_HEIGHT
- 2,
1316 trigbar_width
[i
], barstart
[i
], barend
[i
],
1319 screens
[i
].mono_bitmap(bitmap_icons_7x8
[icon
], ixpos
[i
], ypos
[i
],
1320 ICON_PLAY_STATE_WIDTH
, STATUSBAR_HEIGHT
);
1325 int peak_meter_draw_get_btn(int action_context
, int x
, int y
[],
1326 int height
, int nb_screens
)
1328 int button
= BUTTON_NONE
;
1329 long next_refresh
= current_tick
;
1330 long next_big_refresh
= current_tick
+ HZ
/ 10;
1332 #if (CONFIG_CODEC == SWCODEC) || defined(SIMULATOR)
1333 bool highperf
= false;
1335 /* On MAS targets, we need to poll as often as possible in order to not
1336 * miss a peak, as the MAS does only provide a quasi-peak. When the disk
1337 * is active, it must not draw too much CPU power or a buffer overrun can
1338 * happen when saving a recording. As a compromise, poll only once per tick
1339 * when the disk is active, otherwise spin around as fast as possible. */
1340 bool highperf
= !ata_disk_is_active();
1344 while (TIME_BEFORE(current_tick
, next_big_refresh
)) {
1345 button
= get_action(action_context
, TIMEOUT_NOBLOCK
);
1346 if (button
!= BUTTON_NONE
) {
1349 if (dopeek
) { /* Peek only once per refresh when disk is */
1350 peak_meter_peek(); /* spinning, but as often as possible */
1351 dopeek
= highperf
; /* otherwise. */
1354 sleep(0); /* Sleep until end of current tick. */
1356 if (TIME_AFTER(current_tick
, next_refresh
)) {
1357 for(i
= 0; i
< nb_screens
; i
++)
1359 peak_meter_screen(&screens
[i
], x
, y
[i
], height
);
1360 screens
[i
].update_viewport_rect(x
, y
[i
],
1361 screens
[i
].getwidth() - x
,
1364 next_refresh
+= HZ
/ PEAK_METER_FPS
;
1373 static void peak_meter_clear_histogram(void)
1376 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1377 ticks_per_redraw
[i
] = (unsigned int)0;
1380 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1381 peeks_per_redraw
[i
] = (unsigned int)0;
1385 bool peak_meter_histogram(void)
1388 int btn
= BUTTON_NONE
;
1389 while ((btn
& BUTTON_OFF
) != BUTTON_OFF
)
1391 unsigned int max
= 0;
1394 screens
[0].clear_display();
1396 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1397 max
= MAX(max
, peeks_per_redraw
[i
]);
1400 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1401 x
= peeks_per_redraw
[i
] * (LCD_WIDTH
- 1)/ max
;
1402 screens
[0].hline(0, x
, y
+ i
);
1405 y
= PEEKS_PER_DRAW_SIZE
+ 1;
1408 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1409 max
= MAX(max
, ticks_per_redraw
[i
]);
1412 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1413 x
= ticks_per_redraw
[i
] * (LCD_WIDTH
- 1)/ max
;
1414 screens
[0].hline(0, x
, y
+ i
);
1416 screens
[0].update();
1418 btn
= button_get(true);
1419 if (btn
== BUTTON_PLAY
) {
1420 peak_meter_clear_histogram();