1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Philipp Pertermann
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <stdlib.h> /* sim uses rand for peakmeter simulation */
29 #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
;
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
)
538 #elif CONFIG_CODEC == SWCODEC
539 pm_playback
= playback
;
542 pm_src_left
= MAS_REG_DQPEAK_L
;
543 pm_src_right
= MAS_REG_DQPEAK_R
;
545 pm_src_left
= MAS_REG_QPEAK_L
;
546 pm_src_right
= MAS_REG_QPEAK_R
;
551 #ifdef HAVE_RECORDING
552 static void set_trig_status(int new_state
)
554 if (trig_status
!= new_state
) {
555 trig_status
= new_state
;
556 if (trigger_listener
!= NULL
) {
557 trigger_listener(trig_status
);
565 * Reads peak values from the MAS, and detects clips. The
566 * values are stored in pm_max_left pm_max_right for later
567 * evauluation. Consecutive calls to peak_meter_peek detect
568 * that ocurred. This function could be used by a thread for
569 * busy reading the MAS.
571 void peak_meter_peek(void)
574 #ifdef HAVE_RECORDING
575 bool was_clipping
= pm_clip_left
|| pm_clip_right
;
577 /* read current values */
578 #if CONFIG_CODEC == SWCODEC
580 pcm_calculate_peaks(&pm_cur_left
, &pm_cur_right
);
581 #ifdef HAVE_RECORDING
583 pcm_calculate_rec_peaks(&pm_cur_left
, &pm_cur_right
);
586 right
= pm_cur_right
;
589 pm_cur_left
= left
= mas_codec_readreg(pm_src_left
);
590 pm_cur_right
= right
= mas_codec_readreg(pm_src_right
);
592 pm_cur_left
= left
= 8000;
593 pm_cur_right
= right
= 9000;
598 An clip is assumed when two consecutive readouts
599 of the volume are at full scale. This is proven
600 to be inaccurate in both ways: it may detect clips
601 when no clip occurred and it may fail to detect
602 a real clip. For software codecs, the peak is already
603 the max of a bunch of samples, so use one max value
604 or you fail to detect clipping! */
605 #if CONFIG_CODEC == SWCODEC
606 if (left
== MAX_PEAK
- 1) {
608 if ((left
== pm_max_left
) &&
609 (left
== MAX_PEAK
- 1)) {
613 current_tick
+ clip_time_out
[pm_clip_hold
];
616 #if CONFIG_CODEC == SWCODEC
617 if (right
== MAX_PEAK
- 1) {
619 if ((right
== pm_max_right
) &&
620 (right
== MAX_PEAK
- 1)) {
622 pm_clip_right
= true;
624 current_tick
+ clip_time_out
[pm_clip_hold
];
627 #ifdef HAVE_RECORDING
628 if(!was_clipping
&& (pm_clip_left
|| pm_clip_right
))
630 if(pm_clipcount_active
)
635 /* peaks are searched -> we have to find the maximum. When
636 many calls of peak_meter_peek the maximum value will be
637 stored in pm_max_xxx. This maximum is reset by the
638 functions peak_meter_read_x. */
639 pm_max_left
= MAX(pm_max_left
, left
);
640 pm_max_right
= MAX(pm_max_right
, right
);
642 #ifdef HAVE_RECORDING
643 #if CONFIG_CODEC == SWCODEC
644 /* Ignore any unread peakmeter data */
645 #define MAX_DROP_TIME HZ/7 /* this value may need tweaking. Increase if you are
646 getting trig events when you shouldn't with
649 trig_stp_hold
= MAX_DROP_TIME
;
652 switch (trig_status
) {
654 /* no more changes, if trigger was activated as release trigger */
655 /* threshold exceeded? */
656 if ((left
> trig_strt_threshold
)
657 || (right
> trig_strt_threshold
)) {
658 /* reset trigger duration */
659 trig_hightime
= current_tick
;
661 /* reset dropout duration */
662 trig_lowtime
= current_tick
;
664 if (trig_strt_duration
)
665 set_trig_status(TRIG_STEADY
);
667 /* if trig_duration is set to 0 the user wants to start
668 recording immediately */
669 set_trig_status(TRIG_GO
);
675 /* trigger duration exceeded */
676 if (current_tick
- trig_hightime
> trig_strt_duration
) {
677 set_trig_status(TRIG_GO
);
679 /* threshold exceeded? */
680 if ((left
> trig_strt_threshold
)
681 || (right
> trig_strt_threshold
)) {
683 trig_lowtime
= current_tick
;
685 /* volume is below threshold */
687 /* dropout occurred? */
688 if (current_tick
- trig_lowtime
> trig_strt_dropout
){
689 if (trig_status
== TRIG_STEADY
){
690 set_trig_status(TRIG_READY
);
692 /* trig_status == TRIG_RETRIG */
694 /* the gap has already expired */
695 trig_lowtime
= current_tick
- trig_rstrt_gap
- 1;
696 set_trig_status(TRIG_POSTREC
);
705 /* threshold exceeded? */
706 if ((left
> trig_stp_threshold
)
707 || (right
> trig_stp_threshold
)) {
708 /* restart hold time countdown */
709 trig_lowtime
= current_tick
;
710 #if CONFIG_CODEC == SWCODEC
711 } else if (current_tick
- trig_lowtime
> MAX_DROP_TIME
){
715 set_trig_status(TRIG_POSTREC
);
716 trig_hightime
= current_tick
;
721 /* gap time expired? */
722 if (current_tick
- trig_lowtime
> trig_rstrt_gap
){
723 /* start threshold exceeded? */
724 if ((left
> trig_strt_threshold
)
725 || (right
> trig_strt_threshold
)) {
727 set_trig_status(TRIG_RETRIG
);
728 trig_hightime
= current_tick
;
729 trig_lowtime
= current_tick
;
733 /* stop threshold exceeded */
734 if ((left
> trig_stp_threshold
)
735 || (right
> trig_stp_threshold
)) {
736 if (current_tick
- trig_hightime
> trig_stp_hold
){
737 trig_lowtime
= current_tick
;
738 set_trig_status(TRIG_CONTINUE
);
740 trig_lowtime
= current_tick
- trig_rstrt_gap
- 1;
744 /* below any threshold */
746 if (current_tick
- trig_lowtime
> trig_stp_hold
){
747 set_trig_status(TRIG_READY
);
749 trig_hightime
= current_tick
;
754 /* still within the gap time */
756 /* stop threshold exceeded */
757 if ((left
> trig_stp_threshold
)
758 || (right
> trig_stp_threshold
)) {
759 set_trig_status(TRIG_CONTINUE
);
760 trig_lowtime
= current_tick
;
763 /* hold time expired */
764 else if (current_tick
- trig_lowtime
> trig_stp_hold
){
765 trig_hightime
= current_tick
;
766 trig_lowtime
= current_tick
;
767 set_trig_status(TRIG_READY
);
772 #if CONFIG_CODEC == SWCODEC
773 /* restore stop hold value */
774 if (trig_stp_hold
== MAX_DROP_TIME
)
778 /* check levels next time peakmeter drawn */
786 * Reads out the peak volume of the left channel.
787 * @return int - The maximum value that has been detected
788 * since the last call of peak_meter_read_l. The value
789 * is in the range 0 <= value < MAX_PEAK.
791 static int peak_meter_read_l(void)
793 /* pm_max_left contains the maximum of all peak values that were read
794 by peak_meter_peek since the last call of peak_meter_read_l */
795 int retval
= pm_max_left
;
798 /* store max peak value for peak_meter_get_peakhold_x readout */
799 pm_peakhold_left
= MAX(pm_max_left
, pm_peakhold_left
);
804 /* reset pm_max_left so that subsequent calls of peak_meter_peek don't
805 get fooled by an old maximum value */
806 pm_max_left
= pm_cur_left
;
808 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
810 retval
= rand()%MAX_PEAK
;
817 * Reads out the peak volume of the right channel.
818 * @return int - The maximum value that has been detected
819 * since the last call of peak_meter_read_l. The value
820 * is in the range 0 <= value < MAX_PEAK.
822 static int peak_meter_read_r(void)
824 /* peak_meter_r contains the maximum of all peak values that were read
825 by peak_meter_peek since the last call of peak_meter_read_r */
826 int retval
= pm_max_right
;
829 /* store max peak value for peak_meter_get_peakhold_x readout */
830 pm_peakhold_right
= MAX(pm_max_right
, pm_peakhold_right
);
835 /* reset pm_max_right so that subsequent calls of peak_meter_peek don't
836 get fooled by an old maximum value */
837 pm_max_right
= pm_cur_right
;
839 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
841 retval
= rand()%MAX_PEAK
;
849 * Reads out the current peak-hold values since the last call.
850 * This is used by the histogram feature in the recording screen.
851 * Values are in the range 0 <= peak_x < MAX_PEAK. MAX_PEAK is typ 32767.
853 void peak_meter_get_peakhold(int *peak_left
, int *peak_right
)
856 *peak_left
= pm_peakhold_left
;
858 *peak_right
= pm_peakhold_right
;
859 pm_peakhold_left
= 0;
860 pm_peakhold_right
= 0;
865 * Reset the detected clips. This method is for
866 * use by the user interface.
867 * @param int unused - This parameter was added to
868 * make the function compatible with set_int
870 void peak_meter_set_clip_hold(int time
)
872 pm_clip_left
= false;
873 pm_clip_right
= false;
874 pm_clip_eternal
= (time
> 0) ? false : true;
878 * Scales a peak value as read from the MAS to the range of meterwidth.
879 * The scaling is performed according to the scaling method (dBfs / linear)
880 * and the range (peak_meter_range_min .. peak_meter_range_max).
881 * @param unsigned short val - The volume value. Range: 0 <= val < MAX_PEAK
882 * @param int meterwidht - The widht of the meter in pixel
883 * @return unsigned short - A value 0 <= return value <= meterwidth
885 unsigned short peak_meter_scale_value(unsigned short val
, int meterwidth
)
889 if (val
<= peak_meter_range_min
) {
893 if (val
>= peak_meter_range_max
) {
899 /* different scaling is used for dBfs and linear percent */
902 /* scale the samples dBfs */
903 retval
= (calc_db(retval
) - pm_db_min
) * meterwidth
/ pm_db_range
;
906 /* Scale for linear percent display */
909 /* scale the samples */
910 retval
= ((retval
- peak_meter_range_min
) * meterwidth
)
915 void peak_meter_screen(struct screen
*display
, int x
, int y
, int height
)
917 peak_meter_draw(display
, &scales
[display
->screen_type
], x
, y
,
918 display
->getwidth() - x
, height
);
921 * Draws a peak meter in the specified size at the specified position.
922 * @param int x - The x coordinate.
923 * Make sure that 0 <= x and x + width < display->getwidth()
924 * @param int y - The y coordinate.
925 * Make sure that 0 <= y and y + height < display->getheight()
926 * @param int width - The width of the peak meter. Note that for display
927 * of clips a 3 pixel wide area is used ->
929 * @param int height - The height of the peak meter. height > 3
931 static void peak_meter_draw(struct screen
*display
, struct meter_scales
*scales
,
932 int x
, int y
, int width
, int height
)
934 static int left_level
= 0, right_level
= 0;
935 int left
= 0, right
= 0;
936 int meterwidth
= width
- 3;
938 #if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
939 static long peak_release_tick
[2] = {0,0};
940 int screen_nr
= display
->screen_type
== SCREEN_MAIN
? 0 : 1;
942 static long peak_release_tick
= 0;
946 static long pm_tick
= 0;
947 int tmp
= peek_calls
;
950 /* if disabled only draw the peak meter */
951 if (peak_meter_enabled
) {
955 /* only read the volume info from MAS if peek since last read*/
956 left_level
= peak_meter_read_l();
957 right_level
= peak_meter_read_r();
961 /* scale the samples dBfs */
962 left
= peak_meter_scale_value(left_level
, meterwidth
);
963 right
= peak_meter_scale_value(right_level
, meterwidth
);
965 /*if the scale has changed -> recalculate the scale
966 (The scale becomes invalid when the range changed.) */
967 if (!scales
->db_scale_valid
){
970 db_scale_count
= DB_SCALE_SRC_VALUES_SIZE
;
971 for (i
= 0; i
< db_scale_count
; i
++){
972 /* find the real x-coords for predefined interesting
973 dBfs values. These only are recalculated when the
974 scaling of the meter changed. */
975 scales
->db_scale_lcd_coord
[i
] =
976 peak_meter_scale_value(
977 db_scale_src_values
[i
],
982 /* when scaling linear we simly make 10% steps */
985 for (i
= 0; i
< db_scale_count
; i
++) {
986 scales
->db_scale_lcd_coord
[i
] =
987 (i
* (MAX_PEAK
/ 10) - peak_meter_range_min
) *
988 meterwidth
/ pm_range
;
992 /* mark scale valid to avoid recalculating dBfs values
994 scales
->db_scale_valid
= true;
998 #if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
999 delta
= current_tick
- peak_release_tick
[screen_nr
];
1000 peak_release_tick
[screen_nr
] = current_tick
;
1002 delta
= current_tick
- peak_release_tick
;
1003 peak_release_tick
= current_tick
;
1005 left
= MAX(left
, scales
->last_left
- delta
* pm_peak_release
);
1006 right
= MAX(right
, scales
->last_right
- delta
* pm_peak_release
);
1008 /* reset max values after timeout */
1009 if (TIME_AFTER(current_tick
, scales
->pm_peak_timeout_l
)){
1010 scales
->pm_peak_left
= 0;
1013 if (TIME_AFTER(current_tick
, scales
->pm_peak_timeout_r
)){
1014 scales
->pm_peak_right
= 0;
1017 if (!pm_clip_eternal
) {
1019 TIME_AFTER(current_tick
, pm_clip_timeout_l
)){
1020 pm_clip_left
= false;
1023 if (pm_clip_right
&&
1024 TIME_AFTER(current_tick
, pm_clip_timeout_r
)){
1025 pm_clip_right
= false;
1029 /* check for new max values */
1030 if (left
> scales
->pm_peak_left
) {
1031 scales
->pm_peak_left
= left
- 1;
1032 scales
->pm_peak_timeout_l
=
1033 current_tick
+ peak_time_out
[pm_peak_hold
];
1036 if (right
> scales
->pm_peak_right
) {
1037 scales
->pm_peak_right
= right
- 1;
1038 scales
->pm_peak_timeout_r
=
1039 current_tick
+ peak_time_out
[pm_peak_hold
];
1043 /* draw the peak meter */
1044 display
->set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
1045 display
->fillrect(x
, y
, width
, height
);
1046 display
->set_drawmode(DRMODE_SOLID
);
1049 display
->fillrect (x
, y
, left
, height
/ 2 - 2 );
1050 if (scales
->pm_peak_left
> 0) {
1051 display
->vline(x
+ scales
->pm_peak_left
, y
, y
+ height
/ 2 - 2 );
1054 display
->fillrect(x
+ meterwidth
, y
, 3, height
/ 2 - 1);
1058 display
->fillrect(x
, y
+ height
/ 2 + 1, right
, height
/ 2 - 2);
1059 if (scales
->pm_peak_right
> 0) {
1060 display
->vline( x
+ scales
->pm_peak_right
, y
+ height
/ 2, y
+ height
- 2);
1062 if (pm_clip_right
) {
1063 display
->fillrect(x
+ meterwidth
, y
+ height
/ 2, 3, height
/ 2 - 1);
1066 /* draw scale end */
1067 display
->vline(x
+ meterwidth
, y
, y
+ height
- 2);
1069 /* draw dots for scale marks */
1070 for (i
= 0; i
< db_scale_count
; i
++) {
1071 /* The x-coordinates of interesting scale mark points
1072 have been calculated before */
1073 display
->drawpixel(x
+ scales
->db_scale_lcd_coord
[i
],
1074 y
+ height
/ 2 - 1);
1077 #ifdef HAVE_RECORDING
1079 #ifdef HAVE_BACKLIGHT
1081 if ((pm_clip_left
|| pm_clip_right
) &&
1082 global_settings
.cliplight
&&
1083 #if CONFIG_CODEC == SWCODEC
1086 !(audio_status() & (AUDIO_STATUS_PLAY
| AUDIO_STATUS_ERROR
)))
1089 /* if clipping, cliplight setting on and in recording screen */
1090 if (global_settings
.cliplight
<= 2)
1092 /* turn on main unit light if setting set to main or both*/
1095 #ifdef HAVE_REMOTE_LCD
1096 if (global_settings
.cliplight
>= 2)
1098 /* turn remote light unit on if setting set to remote or both */
1099 remote_backlight_on();
1101 #endif /* HAVE_REMOTE_LCD */
1103 #endif /* HAVE_BACKLIGHT */
1105 if (trig_status
!= TRIG_OFF
) {
1106 int start_trigx
, stop_trigx
, ycenter
;
1108 display
->set_drawmode(DRMODE_SOLID
);
1109 ycenter
= y
+ height
/ 2;
1110 /* display threshold value */
1111 start_trigx
= x
+peak_meter_scale_value(trig_strt_threshold
,meterwidth
);
1112 display
->vline(start_trigx
, ycenter
- 2, ycenter
);
1114 if (start_trigx
< display
->getwidth() ) display
->drawpixel(start_trigx
, ycenter
- 1);
1116 stop_trigx
= x
+ peak_meter_scale_value(trig_stp_threshold
,meterwidth
);
1117 display
->vline(stop_trigx
, ycenter
- 2, ycenter
);
1118 if (stop_trigx
> 0) display
->drawpixel(stop_trigx
- 1, ycenter
- 1);
1120 #endif /*HAVE_RECORDING*/
1123 /* display a bar to show how many calls to peak_meter_peek
1124 have ocurred since the last display */
1125 display
->set_drawmode(DRMODE_COMPLEMENT
);
1126 display
->fillrect(x
, y
, tmp
, 3);
1128 if (tmp
< PEEKS_PER_DRAW_SIZE
) {
1129 peeks_per_redraw
[tmp
]++;
1132 tmp
= current_tick
- pm_tick
;
1133 if (tmp
< TICKS_PER_DRAW_SIZE
){
1134 ticks_per_redraw
[tmp
] ++;
1137 /* display a bar to show how many ticks have passed since
1139 display
->fillrect(x
, y
+ height
/ 2, current_tick
- pm_tick
, 2);
1140 pm_tick
= current_tick
;
1143 scales
->last_left
= left
;
1144 scales
->last_right
= right
;
1146 display
->set_drawmode(DRMODE_SOLID
);
1149 #ifdef HAVE_RECORDING
1151 * Defines the parameters of the trigger. After these parameters are defined
1152 * the trigger can be started either by peak_meter_attack_trigger or by
1153 * peak_meter_release_trigger. Note that you can pass either linear (%) or
1154 * logarithmic (db) values to the thresholds. Positive values are intepreted as
1155 * percent (0 is 0% .. 100 is 100%). Negative values are interpreted as db.
1156 * To avoid ambiguosity of the value 0 the negative values are shifted by -1.
1157 * Thus -75 is -74db .. -1 is 0db.
1158 * @param start_threshold - The threshold used for attack trigger. Negative
1159 * values are interpreted as db -1, positive as %.
1160 * @param start_duration - The minimum time span within which start_threshold
1161 * must be exceeded to fire the attack trigger.
1162 * @param start_dropout - The maximum time span the level may fall below
1163 * start_threshold without releasing the attack trigger.
1164 * @param stop_threshold - The threshold the volume must fall below to release
1165 * the release trigger.Negative values are
1166 * interpreted as db -1, positive as %.
1167 * @param stop_hold - The minimum time the volume must fall below the
1168 * stop_threshold to release the trigger.
1171 void peak_meter_define_trigger(
1172 int start_threshold
,
1173 long start_duration
,
1176 long stop_hold_time
,
1180 if (start_threshold
< 0) {
1182 if (start_threshold
< -89) {
1183 trig_strt_threshold
= 0;
1185 trig_strt_threshold
=peak_meter_db2sample((start_threshold
+1)*100);
1188 /* linear percent */
1189 trig_strt_threshold
= start_threshold
* MAX_PEAK
/ 100;
1191 trig_strt_duration
= start_duration
;
1192 trig_strt_dropout
= start_dropout
;
1193 if (stop_threshold
< 0) {
1195 trig_stp_threshold
= peak_meter_db2sample((stop_threshold
+ 1) * 100);
1197 /* linear percent */
1198 trig_stp_threshold
= stop_threshold
* MAX_PEAK
/ 100;
1200 trig_stp_hold
= stop_hold_time
;
1201 trig_rstrt_gap
= restart_gap
;
1205 * Enables or disables the trigger.
1206 * @param on - If true the trigger is turned on.
1208 void peak_meter_trigger(bool on
)
1210 /* don't use set_trigger here as that would fire an undesired event */
1211 trig_status
= on
? TRIG_READY
: TRIG_OFF
;
1215 * Registers the listener function that listenes on trig_status changes.
1216 * @param listener - The function that is called with each change of
1217 * trig_status. May be set to NULL if no callback is desired.
1219 void peak_meter_set_trigger_listener(void (*listener
)(int status
))
1221 trigger_listener
= listener
;
1225 * Fetches the status of the trigger.
1226 * TRIG_OFF: the trigger is inactive
1227 * TRIG_RELEASED: The volume level is below the threshold
1228 * TRIG_ACTIVATED: The volume level has exceeded the threshold, but the trigger
1229 * hasn't been fired yet.
1230 * TRIG_FIRED: The volume exceeds the threshold
1232 * To activate the trigger call either peak_meter_attack_trigger or
1233 * peak_meter_release_trigger. To turn the trigger off call
1234 * peak_meter_trigger_off.
1236 int peak_meter_trigger_status(void)
1238 return trig_status
; /* & TRIG_PIT_MASK;*/
1241 void peak_meter_draw_trig(int xpos
[], int ypos
[], int trig_width
[], int nb_screens
)
1243 int barstart
[NB_SCREENS
];
1244 int barend
[NB_SCREENS
];
1246 int ixpos
[NB_SCREENS
];
1248 int trigbar_width
[NB_SCREENS
];
1251 trigbar_width
[i
] = (trig_width
[i
] - (2 * (ICON_PLAY_STATE_WIDTH
+ 1)));
1253 switch (trig_status
) {
1270 barend
[i
] = (trig_strt_duration
== 0) ? trigbar_width
[i
] :
1272 (current_tick
- trig_hightime
) / trig_strt_duration
;
1283 barstart
[i
] = trigbar_width
[i
];
1284 barend
[i
] = trigbar_width
[i
];
1288 ixpos
[i
] = xpos
[i
]+ trig_width
[i
] - ICON_PLAY_STATE_WIDTH
;
1294 barstart
[i
] = (trig_stp_hold
== 0) ? 0 :
1295 trigbar_width
[i
] - trigbar_width
[i
] *
1296 (current_tick
- trig_lowtime
) / trig_stp_hold
;
1297 barend
[i
] = trigbar_width
[i
];
1301 ixpos
[i
] = xpos
[i
] + trig_width
[i
] - ICON_PLAY_STATE_WIDTH
;
1308 for(i
= 0; i
< nb_screens
; i
++)
1310 gui_scrollbar_draw(&screens
[i
], xpos
[i
] + ICON_PLAY_STATE_WIDTH
+ 1,
1311 ypos
[i
] + 1, trigbar_width
[i
], TRIG_HEIGHT
- 2,
1312 trigbar_width
[i
], barstart
[i
], barend
[i
],
1315 screens
[i
].mono_bitmap(bitmap_icons_7x8
[icon
], ixpos
[i
], ypos
[i
],
1316 ICON_PLAY_STATE_WIDTH
, STATUSBAR_HEIGHT
);
1321 int peak_meter_draw_get_btn(int action_context
, int x
, int y
[],
1322 int height
, int nb_screens
)
1324 int button
= BUTTON_NONE
;
1325 long next_refresh
= current_tick
;
1326 long next_big_refresh
= current_tick
+ HZ
/ 10;
1328 #if (CONFIG_CODEC == SWCODEC) || defined(SIMULATOR)
1329 bool highperf
= false;
1331 /* On MAS targets, we need to poll as often as possible in order to not
1332 * miss a peak, as the MAS does only provide a quasi-peak. When the disk
1333 * is active, it must not draw too much CPU power or a buffer overrun can
1334 * happen when saving a recording. As a compromise, poll only once per tick
1335 * when the disk is active, otherwise spin around as fast as possible. */
1336 bool highperf
= !ata_disk_is_active();
1340 while (TIME_BEFORE(current_tick
, next_big_refresh
)) {
1341 button
= get_action(action_context
, TIMEOUT_NOBLOCK
);
1342 if (button
!= BUTTON_NONE
) {
1345 if (dopeek
) { /* Peek only once per refresh when disk is */
1346 peak_meter_peek(); /* spinning, but as often as possible */
1347 dopeek
= highperf
; /* otherwise. */
1350 sleep(0); /* Sleep until end of current tick. */
1352 if (TIME_AFTER(current_tick
, next_refresh
)) {
1353 for(i
= 0; i
< nb_screens
; i
++)
1355 peak_meter_screen(&screens
[i
], x
, y
[i
], height
);
1356 screens
[i
].update_rect(x
, y
[i
], screens
[i
].width
- x
, height
);
1358 next_refresh
+= HZ
/ PEAK_METER_FPS
;
1367 static void peak_meter_clear_histogram(void)
1370 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1371 ticks_per_redraw
[i
] = (unsigned int)0;
1374 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1375 peeks_per_redraw
[i
] = (unsigned int)0;
1379 bool peak_meter_histogram(void)
1382 int btn
= BUTTON_NONE
;
1383 while ((btn
& BUTTON_OFF
) != BUTTON_OFF
)
1385 unsigned int max
= 0;
1388 screens
[0].clear_display();
1390 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1391 max
= MAX(max
, peeks_per_redraw
[i
]);
1394 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1395 x
= peeks_per_redraw
[i
] * (LCD_WIDTH
- 1)/ max
;
1396 screens
[0].hline(0, x
, y
+ i
);
1399 y
= PEEKS_PER_DRAW_SIZE
+ 1;
1402 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1403 max
= MAX(max
, ticks_per_redraw
[i
]);
1406 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1407 x
= ticks_per_redraw
[i
] * (LCD_WIDTH
- 1)/ max
;
1408 screens
[0].hline(0, x
, y
+ i
);
1410 screens
[0].update();
1412 btn
= button_get(true);
1413 if (btn
== BUTTON_PLAY
) {
1414 peak_meter_clear_histogram();