1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Philipp Pertermann
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
23 #include <stdlib.h> /* sim uses rand for peakmeter simulation */
30 #include "scrollbar.h"
37 #include "peakmeter.h"
39 #include "screen_access.h"
41 #include "backlight.h"
45 #if CONFIG_CODEC == SWCODEC
47 #include "pcm_mixer.h"
50 #include "pcm_record.h"
53 static bool pm_playback
= true; /* selects between playback and recording peaks */
56 static struct meter_scales scales
[NB_SCREENS
];
58 #if !defined(SIMULATOR) && CONFIG_CODEC != SWCODEC
60 static int pm_src_left
= MAS_REG_DQPEAK_L
;
61 static int pm_src_right
= MAS_REG_DQPEAK_R
;
64 /* Current values and cumulation */
65 static int pm_cur_left
; /* current values (last peak_meter_peek) */
66 static int pm_cur_right
;
67 static int pm_max_left
; /* maximum values between peak meter draws */
68 static int pm_max_right
;
69 #if defined(HAVE_AGC) || defined(HAVE_HISTOGRAM)
70 static int pm_peakhold_left
; /* max. peak values between peakhold calls */
71 static int pm_peakhold_right
; /* used for AGC and histogram display */
72 static long next_histogram_update
;
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 static bool peak_meter_enabled
= true;
84 void peak_meter_enable(bool enable
)
86 peak_meter_enabled
= enable
;
91 static unsigned short peak_meter_range_min
; /* minimum of range in samples */
92 static unsigned short peak_meter_range_max
; /* maximum of range in samples */
93 static unsigned short pm_range
; /* range width in samples */
94 static bool pm_use_dbfs
= true; /* true if peakmeter displays dBfs */
95 static bool level_check
; /* true if peeked at peakmeter before drawing */
96 static unsigned short pm_db_min
= 0; /* minimum of range in 1/100 dB */
97 static unsigned short pm_db_max
= 9000; /* maximum of range in 1/100 dB */
98 static unsigned short pm_db_range
= 9000; /* range width in 1/100 dB */
99 /* Timing behaviour */
100 static int pm_peak_hold
= 1; /* peak hold timeout index */
101 static int pm_peak_release
= 8; /* peak release in units per read */
102 static int pm_clip_hold
= 16; /* clip hold timeout index */
103 static bool pm_clip_eternal
= false; /* true if clip timeout is disabled */
105 #ifdef HAVE_RECORDING
106 static unsigned short trig_strt_threshold
;
107 static long trig_strt_duration
;
108 static long trig_strt_dropout
;
109 static unsigned short trig_stp_threshold
;
110 static long trig_stp_hold
;
111 static long trig_rstrt_gap
;
113 /* point in time when the threshold was exceeded */
114 static long trig_hightime
;
116 /* point in time when the volume fell below the threshold*/
117 static long trig_lowtime
;
119 /* The output value of the trigger. See TRIG_XXX constants for valid values */
120 static int trig_status
= TRIG_OFF
;
122 static void (*trigger_listener
)(int) = NULL
;
124 /* clipping counter (only used for recording) */
125 static unsigned int pm_clipcount
= 0; /* clipping count */
126 static bool pm_clipcount_active
= false; /* counting or not */
131 static int peek_calls
= 0;
133 #define PEEKS_PER_DRAW_SIZE 40
134 static unsigned int peeks_per_redraw
[PEEKS_PER_DRAW_SIZE
];
136 #define TICKS_PER_DRAW_SIZE 20
137 static unsigned int ticks_per_redraw
[TICKS_PER_DRAW_SIZE
];
140 #if defined(HAVE_HISTOGRAM)
141 #define HIST_BUF_SIZE (LCD_WIDTH / 2)
142 static int hist_l
= 0;
143 static int hist_r
= 0;
144 static unsigned char history_l
[HIST_BUF_SIZE
];
145 static unsigned char history_r
[HIST_BUF_SIZE
];
146 static const char hist_level_marks
[6] = { 29, 26, 23, 17, 9, 2};
147 static int history_pos
= 0;
148 #define HIST_W (LCD_WIDTH / 2)
150 #ifdef HAVE_LCD_COLOR
151 #define LCD_BAL_L LCD_RGBPACK(0, 0, 255)
152 #define LCD_BAL_R LCD_RGBPACK(204, 0, 0)
153 #define LCD_HIST_OVER LCD_RGBPACK(204, 0, 0)
154 #define LCD_HIST_HI LCD_RGBPACK(255, 204, 0)
155 #define LCD_HIST_OK LCD_RGBPACK(51, 153, 0)
156 #else /* HAVE_LCD_COLOR */
157 #define LCD_BATT_OK LCD_BLACK
158 #define LCD_BATT_LO LCD_DARKGRAY
159 #define LCD_DISK_OK LCD_BLACK
160 #define LCD_DISK_LO LCD_DARKGRAY
161 #define LCD_HIST_OVER LCD_BLACK
162 #define LCD_HIST_OK LCD_DARKGRAY
163 #define LCD_BAL LCD_DARKGRAY
164 #endif /* HAVE_LCD_COLOR */
165 #else /* LCD_DEPTH > 1 */
166 #define LCD_HIST_OVER LCD_DEFAULT_FG
167 #define LCD_HIST_OK LCD_DEFAULT_FG
168 #define LCD_BAL LCD_DEFAULT_FG
169 #endif /* LCD_DEPTH > 1 */
170 #endif /* HAVE_HISTOGRAM */
172 static void peak_meter_draw(struct screen
*display
, struct meter_scales
*meter_scales
,
173 int x
, int y
, int width
, int height
);
175 /* time out values for max */
176 static const short peak_time_out
[] = {
177 0 * HZ
, HZ
/ 5, 30, HZ
/ 2, HZ
, 2 * HZ
,
178 3 * HZ
, 4 * HZ
, 5 * HZ
, 6 * HZ
, 7 * HZ
, 8 * HZ
,
179 9 * HZ
, 10 * HZ
, 15 * HZ
, 20 * HZ
, 30 * HZ
, 60 * HZ
182 /* time out values for clip */
183 static const long clip_time_out
[] = {
184 0 * HZ
, 1 * HZ
, 2 * HZ
, 3 * HZ
, 4 * HZ
, 5 * HZ
,
185 6 * HZ
, 7 * HZ
, 8 * HZ
, 9 * HZ
, 10 * HZ
, 15 * HZ
,
186 20 * HZ
, 25 * HZ
, 30 * HZ
, 45 * HZ
, 60 * HZ
, 90 * HZ
,
187 120 * HZ
, 180 * HZ
, 300 * HZ
, 600L * HZ
, 1200L * HZ
,
188 2700L * HZ
, 5400L * HZ
191 /* precalculated peak values that represent magical
192 dBfs values. Used to draw the scale */
193 static const short db_scale_src_values
[DB_SCALE_SRC_VALUES_SIZE
] = {
208 static int db_scale_count
= DB_SCALE_SRC_VALUES_SIZE
;
211 * Calculates dB Value for the peak meter, uses peak value as input
212 * @param int sample - The input value
213 * Make sure that 0 <= value < SAMPLE_RANGE
215 * @return int - The 2 digit fixed point result of the euation
216 * 20 * log (sample / SAMPLE_RANGE) + 90
217 * Output range is 0-9000 (that is 0.0 - 90.0 dB).
218 * Normally 0dB is full scale, here it is shifted +90dB.
219 * The calculation is based on the results of a linear
220 * approximation tool written specifically for this problem
221 * by Andreas Zwirtes (radhard@gmx.de). The result has an
222 * accurracy of better than 2%. It is highly runtime optimized,
223 * the cascading if-clauses do an successive approximation on
224 * the input value. This avoids big lookup-tables and
226 * Improved by Jvo Studer for errors < 0.2dB for critical
227 * range of -12dB to 0dB (78.0 to 90.0dB).
230 static int calc_db (int isample
)
232 /* return n+m*(isample-istart)/100 */
237 if (isample
< 2308) { /* Range 1-5 */
239 if (isample
< 115) { /* Range 1-3 */
244 istart
= 1; /* Range 1 */
249 istart
= 5; /* Range 2 */
255 istart
= 24; /* Range 3 */
260 else { /* Range 4-5 */
263 istart
= 114; /* Range 4 */
268 istart
= 588; /* Range 5 */
275 else { /* Range 6-9 */
277 if (isample
< 12932) {
279 if (isample
< 6394) {
280 istart
= 2608; /* Range 6 */
285 istart
= 7000; /* Range 7 */
292 if (isample
< 22450) {
293 istart
= 13000; /* Range 8 */
298 istart
= 22636; /* Range 9 */
305 return n
+ (m
* (long)(isample
- istart
)) / 100L;
310 * A helper function for peak_meter_db2sample. Don't call it separately but
311 * use peak_meter_db2sample. If one or both of min and max are outside the
312 * range 0 <= min (or max) < 8961 the behaviour of this function is
313 * undefined. It may not return.
314 * @param int min - The minimum of the value range that is searched.
315 * @param int max - The maximum of the value range that is searched.
316 * @param int db - The value in dBfs * (-100) for which the according
317 * minimal peak sample is searched.
318 * @return int - A linear volume value with 0 <= value < MAX_PEAK
320 static int db_to_sample_bin_search(int min
, int max
, int db
)
322 int test
= min
+ (max
- min
) / 2;
325 if (calc_db(test
) < db
) {
326 test
= db_to_sample_bin_search(test
, max
, db
);
328 if (calc_db(test
-1) > db
) {
329 test
= db_to_sample_bin_search(min
, test
, db
);
337 * Converts a value representing dBfs to a linear
338 * scaled volume info as it is used by the MAS.
339 * An incredibly inefficiant function which is
340 * the vague inverse of calc_db. This really
341 * should be replaced by something better soon.
343 * @param int db - A dBfs * 100 value with
345 * @return int - The return value is in the range of
346 * 0 <= return value < MAX_PEAK
348 int peak_meter_db2sample(int db
)
352 /* what is the maximum pseudo db value */
353 int max_peak_db
= calc_db(MAX_PEAK
- 1);
355 /* range check: db value to big */
356 if (max_peak_db
+ db
< 0) {
360 /* range check: db value too small */
361 else if (max_peak_db
+ db
>= max_peak_db
) {
362 retval
= MAX_PEAK
-1;
365 /* value in range: find the matching linear value */
367 retval
= db_to_sample_bin_search(0, MAX_PEAK
, max_peak_db
+ db
);
369 /* as this is a dirty function anyway, we want to adjust the
370 full scale hit manually to avoid users complaining that when
371 they adjust maximum for 0 dBfs and display it in percent it
372 shows 99%. That is due to precision loss and this is the
380 * Set the min value for restriction of the value range.
381 * @param int newmin - depending whether dBfs is used
382 * newmin is a value in dBfs * 100 or in linear percent values.
383 * for dBfs: -9000 < newmin <= 0
384 * for linear: 0 <= newmin <= 100
386 static void peak_meter_set_min(int newmin
)
389 peak_meter_range_min
= peak_meter_db2sample(newmin
);
392 if (newmin
< peak_meter_range_max
) {
393 peak_meter_range_min
= newmin
* MAX_PEAK
/ 100;
397 pm_range
= peak_meter_range_max
- peak_meter_range_min
;
399 /* Avoid division by zero. */
404 pm_db_min
= calc_db(peak_meter_range_min
);
405 pm_db_range
= pm_db_max
- pm_db_min
;
408 scales
[i
].db_scale_valid
= false;
412 * Returns the minimum value of the range the meter
413 * displays. If the scale is set to dBfs it returns
414 * dBfs values * 100 or linear percent values.
415 * @return: using dBfs : -9000 < value <= 0
416 * using linear scale: 0 <= value <= 100
418 int peak_meter_get_min(void)
422 retval
= calc_db(peak_meter_range_min
) - calc_db(MAX_PEAK
- 1);
424 retval
= peak_meter_range_min
* 100 / MAX_PEAK
;
430 * Set the max value for restriction of the value range.
431 * @param int newmax - depending wether dBfs is used
432 * newmax is a value in dBfs * 100 or in linear percent values.
433 * for dBfs: -9000 < newmax <= 0
434 * for linear: 0 <= newmax <= 100
436 static void peak_meter_set_max(int newmax
)
439 peak_meter_range_max
= peak_meter_db2sample(newmax
);
441 if (newmax
> peak_meter_range_min
) {
442 peak_meter_range_max
= newmax
* MAX_PEAK
/ 100;
446 pm_range
= peak_meter_range_max
- peak_meter_range_min
;
448 /* Avoid division by zero. */
453 pm_db_max
= calc_db(peak_meter_range_max
);
454 pm_db_range
= pm_db_max
- pm_db_min
;
457 scales
[i
].db_scale_valid
= false;
461 * Returns the minimum value of the range the meter
462 * displays. If the scale is set to dBfs it returns
463 * dBfs values * 100 or linear percent values
464 * @return: using dBfs : -9000 < value <= 0
465 * using linear scale: 0 <= value <= 100
467 int peak_meter_get_max(void)
471 retval
= calc_db(peak_meter_range_max
) - calc_db(MAX_PEAK
- 1);
473 retval
= peak_meter_range_max
* 100 / MAX_PEAK
;
479 * Returns whether the meter is currently displaying dBfs or percent values.
480 * @return bool - true if the meter is displaying dBfs
481 false if the meter is displaying percent values.
483 bool peak_meter_get_use_dbfs(void)
489 * Specifies whether the values displayed are scaled
490 * as dBfs or as linear percent values.
491 * @param use - set to true for dBfs,
492 * set to false for linear scaling in percent
494 void peak_meter_set_use_dbfs(bool use
)
498 scales
[i
].db_scale_valid
= false;
502 * Initialize the range of the meter. Only values
503 * that are in the range of [range_min ... range_max]
505 * @param bool dbfs - set to true for dBfs,
506 * set to false for linear scaling in percent
507 * @param int range_min - Specifies the lower value of the range.
508 * Pass a value dBfs * 100 when dbfs is set to true.
509 * Pass a percent value when dbfs is set to false.
510 * @param int range_max - Specifies the upper value of the range.
511 * Pass a value dBfs * 100 when dbfs is set to true.
512 * Pass a percent value when dbfs is set to false.
514 void peak_meter_init_range( bool dbfs
, int range_min
, int range_max
)
517 peak_meter_set_min(range_min
);
518 peak_meter_set_max(range_max
);
522 * Initialize the peak meter with all relevant values concerning times.
523 * @param int release - Set the maximum amount of pixels the meter is allowed
524 * to decrease with each redraw
525 * @param int hold - Select the time preset for the time the peak indicator
526 * is reset after a peak occurred. The preset values are
527 * stored in peak_time_out.
528 * @param int clip_hold - Select the time preset for the time the peak
529 * indicator is reset after a peak occurred. The preset
530 * values are stored in clip_time_out.
532 void peak_meter_init_times(int release
, int hold
, int clip_hold
)
535 pm_peak_release
= release
;
536 pm_clip_hold
= clip_hold
;
539 #ifdef HAVE_RECORDING
541 * Enable/disable clip counting
543 void pm_activate_clipcount(bool active
)
545 pm_clipcount_active
= active
;
549 * Get clipping counter value
551 int pm_get_clipcount(void)
557 * Set clipping counter to zero (typically at start of recording or playback)
559 void pm_reset_clipcount(void)
566 * Set the source of the peak meter to playback or to
568 * @param: bool playback - If true playback peak meter is used.
569 * If false recording peak meter is used.
571 void peak_meter_playback(bool playback
)
573 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
575 #elif CONFIG_CODEC == SWCODEC
576 pm_playback
= playback
;
579 pm_src_left
= MAS_REG_DQPEAK_L
;
580 pm_src_right
= MAS_REG_DQPEAK_R
;
582 pm_src_left
= MAS_REG_QPEAK_L
;
583 pm_src_right
= MAS_REG_QPEAK_R
;
586 /* reset the scales just in case recording and playback
587 use different viewport sizes. Normally we should be checking viewport
588 sizes every time but this will do for now */
590 scales
[i
].db_scale_valid
= false;
593 #ifdef HAVE_RECORDING
594 static void set_trig_status(int new_state
)
596 if (trig_status
!= new_state
) {
597 trig_status
= new_state
;
598 if (trigger_listener
!= NULL
) {
599 trigger_listener(trig_status
);
607 * Reads peak values from the MAS, and detects clips. The
608 * values are stored in pm_max_left pm_max_right for later
609 * evauluation. Consecutive calls to peak_meter_peek detect
610 * that ocurred. This function could be used by a thread for
611 * busy reading the MAS.
613 void peak_meter_peek(void)
616 #ifdef HAVE_RECORDING
617 bool was_clipping
= pm_clip_left
|| pm_clip_right
;
619 /* read current values */
620 #if CONFIG_CODEC == SWCODEC
623 static struct pcm_peaks chan_peaks
; /* *MUST* be static */
624 mixer_channel_calculate_peaks(PCM_MIXER_CHAN_PLAYBACK
,
626 pm_cur_left
= chan_peaks
.left
;
627 pm_cur_right
= chan_peaks
.right
;
629 #ifdef HAVE_RECORDING
631 pcm_calculate_rec_peaks(&pm_cur_left
, &pm_cur_right
);
634 right
= pm_cur_right
;
636 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
637 pm_cur_left
= left
= mas_codec_readreg(pm_src_left
);
638 pm_cur_right
= right
= mas_codec_readreg(pm_src_right
);
640 pm_cur_left
= left
= 8000;
641 pm_cur_right
= right
= 9000;
646 An clip is assumed when two consecutive readouts
647 of the volume are at full scale. This is proven
648 to be inaccurate in both ways: it may detect clips
649 when no clip occurred and it may fail to detect
650 a real clip. For software codecs, the peak is already
651 the max of a bunch of samples, so use one max value
652 or you fail to detect clipping! */
653 #if CONFIG_CODEC == SWCODEC
654 if (left
== MAX_PEAK
- 1) {
656 if ((left
== pm_max_left
) &&
657 (left
== MAX_PEAK
- 1)) {
661 current_tick
+ clip_time_out
[pm_clip_hold
];
664 #if CONFIG_CODEC == SWCODEC
665 if (right
== MAX_PEAK
- 1) {
667 if ((right
== pm_max_right
) &&
668 (right
== MAX_PEAK
- 1)) {
670 pm_clip_right
= true;
672 current_tick
+ clip_time_out
[pm_clip_hold
];
675 #ifdef HAVE_RECORDING
676 if(!was_clipping
&& (pm_clip_left
|| pm_clip_right
))
678 if(pm_clipcount_active
)
683 /* peaks are searched -> we have to find the maximum. When
684 many calls of peak_meter_peek the maximum value will be
685 stored in pm_max_xxx. This maximum is reset by the
686 functions peak_meter_read_x. */
687 pm_max_left
= MAX(pm_max_left
, left
);
688 pm_max_right
= MAX(pm_max_right
, right
);
690 #ifdef HAVE_RECORDING
691 #if CONFIG_CODEC == SWCODEC
692 /* Ignore any unread peakmeter data */
693 #define MAX_DROP_TIME HZ/7 /* this value may need tweaking. Increase if you are
694 getting trig events when you shouldn't with
697 trig_stp_hold
= MAX_DROP_TIME
;
700 switch (trig_status
) {
702 /* no more changes, if trigger was activated as release trigger */
703 /* threshold exceeded? */
704 if ((left
> trig_strt_threshold
)
705 || (right
> trig_strt_threshold
)) {
706 /* reset trigger duration */
707 trig_hightime
= current_tick
;
709 /* reset dropout duration */
710 trig_lowtime
= current_tick
;
712 if (trig_strt_duration
)
713 set_trig_status(TRIG_STEADY
);
715 /* if trig_duration is set to 0 the user wants to start
716 recording immediately */
717 set_trig_status(TRIG_GO
);
723 /* trigger duration exceeded */
724 if (current_tick
- trig_hightime
> trig_strt_duration
) {
725 set_trig_status(TRIG_GO
);
727 /* threshold exceeded? */
728 if ((left
> trig_strt_threshold
)
729 || (right
> trig_strt_threshold
)) {
731 trig_lowtime
= current_tick
;
733 /* volume is below threshold */
735 /* dropout occurred? */
736 if (current_tick
- trig_lowtime
> trig_strt_dropout
){
737 if (trig_status
== TRIG_STEADY
){
738 set_trig_status(TRIG_READY
);
740 /* trig_status == TRIG_RETRIG */
742 /* the gap has already expired */
743 trig_lowtime
= current_tick
- trig_rstrt_gap
- 1;
744 set_trig_status(TRIG_POSTREC
);
753 /* threshold exceeded? */
754 if ((left
> trig_stp_threshold
)
755 || (right
> trig_stp_threshold
)) {
756 /* restart hold time countdown */
757 trig_lowtime
= current_tick
;
758 #if CONFIG_CODEC == SWCODEC
759 } else if (current_tick
- trig_lowtime
> MAX_DROP_TIME
){
763 set_trig_status(TRIG_POSTREC
);
764 trig_hightime
= current_tick
;
769 /* gap time expired? */
770 if (current_tick
- trig_lowtime
> trig_rstrt_gap
){
771 /* start threshold exceeded? */
772 if ((left
> trig_strt_threshold
)
773 || (right
> trig_strt_threshold
)) {
775 set_trig_status(TRIG_RETRIG
);
776 trig_hightime
= current_tick
;
777 trig_lowtime
= current_tick
;
781 /* stop threshold exceeded */
782 if ((left
> trig_stp_threshold
)
783 || (right
> trig_stp_threshold
)) {
784 if (current_tick
- trig_hightime
> trig_stp_hold
){
785 trig_lowtime
= current_tick
;
786 set_trig_status(TRIG_CONTINUE
);
788 trig_lowtime
= current_tick
- trig_rstrt_gap
- 1;
792 /* below any threshold */
794 if (current_tick
- trig_lowtime
> trig_stp_hold
){
795 set_trig_status(TRIG_READY
);
797 trig_hightime
= current_tick
;
802 /* still within the gap time */
804 /* stop threshold exceeded */
805 if ((left
> trig_stp_threshold
)
806 || (right
> trig_stp_threshold
)) {
807 set_trig_status(TRIG_CONTINUE
);
808 trig_lowtime
= current_tick
;
811 /* hold time expired */
812 else if (current_tick
- trig_lowtime
> trig_stp_hold
){
813 trig_hightime
= current_tick
;
814 trig_lowtime
= current_tick
;
815 set_trig_status(TRIG_READY
);
820 #if CONFIG_CODEC == SWCODEC
821 /* restore stop hold value */
822 if (trig_stp_hold
== MAX_DROP_TIME
)
826 /* check levels next time peakmeter drawn */
834 * Reads out the peak volume of the left channel.
835 * @return int - The maximum value that has been detected
836 * since the last call of peak_meter_read_l. The value
837 * is in the range 0 <= value < MAX_PEAK.
839 static int peak_meter_read_l(void)
841 /* pm_max_left contains the maximum of all peak values that were read
842 by peak_meter_peek since the last call of peak_meter_read_l */
845 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
847 pm_max_left
= rand()%MAX_PEAK
;
850 retval
= pm_max_left
;
852 #if defined(HAVE_HISTOGRAM) || defined(HAVE_AGC)
853 /* store max peak value for peak_meter_get_peakhold_x readout */
854 pm_peakhold_left
= MAX(pm_max_left
, pm_peakhold_left
);
859 /* reset pm_max_left so that subsequent calls of peak_meter_peek don't
860 get fooled by an old maximum value */
861 pm_max_left
= pm_cur_left
;
867 * Reads out the peak volume of the right channel.
868 * @return int - The maximum value that has been detected
869 * since the last call of peak_meter_read_l. The value
870 * is in the range 0 <= value < MAX_PEAK.
872 static int peak_meter_read_r(void)
874 /* peak_meter_r contains the maximum of all peak values that were read
875 by peak_meter_peek since the last call of peak_meter_read_r */
878 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
880 pm_max_right
= rand()%MAX_PEAK
;
883 retval
= pm_max_right
;
885 #if defined(HAVE_HISTOGRAM) || defined(HAVE_AGC)
886 /* store max peak value for peak_meter_get_peakhold_x readout */
887 pm_peakhold_right
= MAX(pm_max_right
, pm_peakhold_right
);
892 /* reset pm_max_right so that subsequent calls of peak_meter_peek don't
893 get fooled by an old maximum value */
894 pm_max_right
= pm_cur_right
;
899 #if defined(HAVE_AGC) || defined(HAVE_HISTOGRAM)
901 * Reads out the current peak-hold values since the last call.
902 * This is used by the histogram feature in the recording screen.
903 * Values are in the range 0 <= peak_x < MAX_PEAK. MAX_PEAK is typ 32767.
905 void peak_meter_get_peakhold(int *peak_left
, int *peak_right
)
908 *peak_left
= pm_peakhold_left
;
910 *peak_right
= pm_peakhold_right
;
912 #ifdef HAVE_HISTOGRAM
913 if (*peak_left
> hist_l
)
915 if (*peak_right
> hist_r
)
916 hist_r
= *peak_right
;
919 pm_peakhold_left
= 0;
920 pm_peakhold_right
= 0;
925 * Reset the detected clips. This method is for
926 * use by the user interface.
927 * @param int unused - This parameter was added to
928 * make the function compatible with set_int
930 void peak_meter_set_clip_hold(int time
)
932 pm_clip_left
= false;
933 pm_clip_right
= false;
934 pm_clip_eternal
= (time
> 0) ? false : true;
938 * Scales a peak value as read from the MAS to the range of meterwidth.
939 * The scaling is performed according to the scaling method (dBfs / linear)
940 * and the range (peak_meter_range_min .. peak_meter_range_max).
941 * @param unsigned short val - The volume value. Range: 0 <= val < MAX_PEAK
942 * @param int meterwidht - The widht of the meter in pixel
943 * @return unsigned short - A value 0 <= return value <= meterwidth
945 unsigned short peak_meter_scale_value(unsigned short val
, int meterwidth
)
949 if (val
<= peak_meter_range_min
) {
953 if (val
>= peak_meter_range_max
) {
959 /* different scaling is used for dBfs and linear percent */
962 /* scale the samples dBfs */
963 retval
= (calc_db(retval
) - pm_db_min
) * meterwidth
/ pm_db_range
;
966 /* Scale for linear percent display */
969 /* scale the samples */
970 retval
= ((retval
- peak_meter_range_min
) * meterwidth
)
975 void peak_meter_screen(struct screen
*display
, int x
, int y
, int height
)
977 peak_meter_draw(display
, &scales
[display
->screen_type
], x
, y
,
978 display
->getwidth() - x
, height
);
981 /* sets *left and *right to the current *unscaled* values */
982 void peak_meter_current_vals(int *left
, int *right
)
984 static int left_level
= 0, right_level
= 0;
986 /* only read the volume info from MAS if peek since last read*/
987 left_level
= peak_meter_read_l();
988 right_level
= peak_meter_read_r();
992 *right
= right_level
;
996 * Draws a peak meter in the specified size at the specified position.
997 * @param int x - The x coordinate.
998 * Make sure that 0 <= x and x + width < display->getwidth()
999 * @param int y - The y coordinate.
1000 * Make sure that 0 <= y and y + height < display->getheight()
1001 * @param int width - The width of the peak meter. Note that for display
1002 * of clips a 3 pixel wide area is used ->
1004 * @param int height - The height of the peak meter. height > 3
1006 static void peak_meter_draw(struct screen
*display
, struct meter_scales
*scales
,
1007 int x
, int y
, int width
, int height
)
1009 int left_level
= 0, right_level
= 0;
1010 int left
= 0, right
= 0;
1011 int meterwidth
= width
- 3;
1013 #if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
1014 static long peak_release_tick
[2] = {0,0};
1015 int screen_nr
= display
->screen_type
== SCREEN_MAIN
? 0 : 1;
1017 static long peak_release_tick
= 0;
1021 static long pm_tick
= 0;
1022 int tmp
= peek_calls
;
1025 /* if disabled only draw the peak meter */
1026 if (peak_meter_enabled
) {
1029 peak_meter_current_vals(&left_level
, &right_level
);
1031 /* scale the samples dBfs */
1032 left
= peak_meter_scale_value(left_level
, meterwidth
);
1033 right
= peak_meter_scale_value(right_level
, meterwidth
);
1035 /*if the scale has changed -> recalculate the scale
1036 (The scale becomes invalid when the range changed.) */
1037 if (!scales
->db_scale_valid
){
1040 db_scale_count
= DB_SCALE_SRC_VALUES_SIZE
;
1041 for (i
= 0; i
< db_scale_count
; i
++){
1042 /* find the real x-coords for predefined interesting
1043 dBfs values. These only are recalculated when the
1044 scaling of the meter changed. */
1045 scales
->db_scale_lcd_coord
[i
] =
1046 peak_meter_scale_value(
1047 db_scale_src_values
[i
],
1052 /* when scaling linear we simly make 10% steps */
1054 db_scale_count
= 10;
1055 for (i
= 0; i
< db_scale_count
; i
++) {
1056 scales
->db_scale_lcd_coord
[i
] =
1057 (i
* (MAX_PEAK
/ 10) - peak_meter_range_min
) *
1058 meterwidth
/ pm_range
;
1062 /* mark scale valid to avoid recalculating dBfs values
1064 scales
->db_scale_valid
= true;
1068 #if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
1069 delta
= current_tick
- peak_release_tick
[screen_nr
];
1070 peak_release_tick
[screen_nr
] = current_tick
;
1072 delta
= current_tick
- peak_release_tick
;
1073 peak_release_tick
= current_tick
;
1075 left
= MAX(left
, scales
->last_left
- delta
* pm_peak_release
);
1076 right
= MAX(right
, scales
->last_right
- delta
* pm_peak_release
);
1078 /* reset max values after timeout */
1079 if (TIME_AFTER(current_tick
, scales
->pm_peak_timeout_l
)){
1080 scales
->pm_peak_left
= 0;
1083 if (TIME_AFTER(current_tick
, scales
->pm_peak_timeout_r
)){
1084 scales
->pm_peak_right
= 0;
1087 if (!pm_clip_eternal
) {
1089 TIME_AFTER(current_tick
, pm_clip_timeout_l
)){
1090 pm_clip_left
= false;
1093 if (pm_clip_right
&&
1094 TIME_AFTER(current_tick
, pm_clip_timeout_r
)){
1095 pm_clip_right
= false;
1099 /* check for new max values */
1100 if (left
> scales
->pm_peak_left
) {
1101 scales
->pm_peak_left
= left
- 1;
1102 scales
->pm_peak_timeout_l
=
1103 current_tick
+ peak_time_out
[pm_peak_hold
];
1106 if (right
> scales
->pm_peak_right
) {
1107 scales
->pm_peak_right
= right
- 1;
1108 scales
->pm_peak_timeout_r
=
1109 current_tick
+ peak_time_out
[pm_peak_hold
];
1113 /* draw the peak meter */
1114 display
->set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
1115 display
->fillrect(x
, y
, width
, height
);
1116 display
->set_drawmode(DRMODE_SOLID
);
1119 display
->fillrect (x
, y
, left
, height
/ 2 - 2 );
1120 if (scales
->pm_peak_left
> 0) {
1121 display
->vline(x
+ scales
->pm_peak_left
, y
, y
+ height
/ 2 - 2 );
1124 display
->fillrect(x
+ meterwidth
, y
, 3, height
/ 2 - 1);
1128 display
->fillrect(x
, y
+ height
/ 2 + 1, right
, height
/ 2 - 2);
1129 if (scales
->pm_peak_right
> 0) {
1130 display
->vline( x
+ scales
->pm_peak_right
, y
+ height
/ 2, y
+ height
- 2);
1132 if (pm_clip_right
) {
1133 display
->fillrect(x
+ meterwidth
, y
+ height
/ 2, 3, height
/ 2 - 1);
1136 /* draw scale end */
1137 display
->vline(x
+ meterwidth
, y
, y
+ height
- 2);
1139 /* draw dots for scale marks */
1140 for (i
= 0; i
< db_scale_count
; i
++) {
1141 /* The x-coordinates of interesting scale mark points
1142 have been calculated before */
1143 display
->drawpixel(x
+ scales
->db_scale_lcd_coord
[i
],
1144 y
+ height
/ 2 - 1);
1147 #ifdef HAVE_RECORDING
1149 #ifdef HAVE_BACKLIGHT
1151 if ((pm_clip_left
|| pm_clip_right
) &&
1152 global_settings
.cliplight
&&
1153 #if CONFIG_CODEC == SWCODEC
1156 !(audio_status() & (AUDIO_STATUS_PLAY
| AUDIO_STATUS_ERROR
)))
1159 /* if clipping, cliplight setting on and in recording screen */
1160 if (global_settings
.cliplight
<= 2)
1162 /* turn on main unit light if setting set to main or both*/
1165 #ifdef HAVE_REMOTE_LCD
1166 if (global_settings
.cliplight
>= 2)
1168 /* turn remote light unit on if setting set to remote or both */
1169 remote_backlight_on();
1171 #endif /* HAVE_REMOTE_LCD */
1173 #endif /* HAVE_BACKLIGHT */
1175 if (trig_status
!= TRIG_OFF
) {
1176 int start_trigx
, stop_trigx
, ycenter
;
1178 display
->set_drawmode(DRMODE_SOLID
);
1179 ycenter
= y
+ height
/ 2;
1180 /* display threshold value */
1181 start_trigx
= x
+peak_meter_scale_value(trig_strt_threshold
,meterwidth
);
1182 display
->vline(start_trigx
, ycenter
- 2, ycenter
);
1184 if (start_trigx
< display
->getwidth() ) display
->drawpixel(start_trigx
,
1187 stop_trigx
= x
+ peak_meter_scale_value(trig_stp_threshold
,meterwidth
);
1188 display
->vline(stop_trigx
, ycenter
- 2, ycenter
);
1189 if (stop_trigx
> 0) display
->drawpixel(stop_trigx
- 1, ycenter
- 1);
1191 #endif /*HAVE_RECORDING*/
1194 /* display a bar to show how many calls to peak_meter_peek
1195 have ocurred since the last display */
1196 display
->set_drawmode(DRMODE_COMPLEMENT
);
1197 display
->fillrect(x
, y
, tmp
, 3);
1199 if (tmp
< PEEKS_PER_DRAW_SIZE
) {
1200 peeks_per_redraw
[tmp
]++;
1203 tmp
= current_tick
- pm_tick
;
1204 if (tmp
< TICKS_PER_DRAW_SIZE
){
1205 ticks_per_redraw
[tmp
] ++;
1208 /* display a bar to show how many ticks have passed since
1210 display
->fillrect(x
, y
+ height
/ 2, current_tick
- pm_tick
, 2);
1211 pm_tick
= current_tick
;
1214 scales
->last_left
= left
;
1215 scales
->last_right
= right
;
1217 display
->set_drawmode(DRMODE_SOLID
);
1220 #ifdef HAVE_RECORDING
1222 * Defines the parameters of the trigger. After these parameters are defined
1223 * the trigger can be started either by peak_meter_attack_trigger or by
1224 * peak_meter_release_trigger. Note that you can pass either linear (%) or
1225 * logarithmic (db) values to the thresholds. Positive values are intepreted as
1226 * percent (0 is 0% .. 100 is 100%). Negative values are interpreted as db.
1227 * To avoid ambiguosity of the value 0 the negative values are shifted by -1.
1228 * Thus -75 is -74db .. -1 is 0db.
1229 * @param start_threshold - The threshold used for attack trigger. Negative
1230 * values are interpreted as db -1, positive as %.
1231 * @param start_duration - The minimum time span within which start_threshold
1232 * must be exceeded to fire the attack trigger.
1233 * @param start_dropout - The maximum time span the level may fall below
1234 * start_threshold without releasing the attack trigger.
1235 * @param stop_threshold - The threshold the volume must fall below to release
1236 * the release trigger.Negative values are
1237 * interpreted as db -1, positive as %.
1238 * @param stop_hold - The minimum time the volume must fall below the
1239 * stop_threshold to release the trigger.
1242 void peak_meter_define_trigger(
1243 int start_threshold
,
1244 long start_duration
,
1247 long stop_hold_time
,
1251 if (start_threshold
< 0) {
1253 if (start_threshold
< -89) {
1254 trig_strt_threshold
= 0;
1256 trig_strt_threshold
=peak_meter_db2sample((start_threshold
+1)*100);
1259 /* linear percent */
1260 trig_strt_threshold
= start_threshold
* MAX_PEAK
/ 100;
1262 trig_strt_duration
= start_duration
;
1263 trig_strt_dropout
= start_dropout
;
1264 if (stop_threshold
< 0) {
1266 trig_stp_threshold
= peak_meter_db2sample((stop_threshold
+ 1) * 100);
1268 /* linear percent */
1269 trig_stp_threshold
= stop_threshold
* MAX_PEAK
/ 100;
1271 trig_stp_hold
= stop_hold_time
;
1272 trig_rstrt_gap
= restart_gap
;
1276 * Enables or disables the trigger.
1277 * @param on - If true the trigger is turned on.
1279 void peak_meter_trigger(bool on
)
1281 /* don't use set_trigger here as that would fire an undesired event */
1282 trig_status
= on
? TRIG_READY
: TRIG_OFF
;
1286 * Registers the listener function that listenes on trig_status changes.
1287 * @param listener - The function that is called with each change of
1288 * trig_status. May be set to NULL if no callback is desired.
1290 void peak_meter_set_trigger_listener(void (*listener
)(int status
))
1292 trigger_listener
= listener
;
1296 * Fetches the status of the trigger.
1297 * TRIG_OFF: the trigger is inactive
1298 * TRIG_RELEASED: The volume level is below the threshold
1299 * TRIG_ACTIVATED: The volume level has exceeded the threshold, but the trigger
1300 * hasn't been fired yet.
1301 * TRIG_FIRED: The volume exceeds the threshold
1303 * To activate the trigger call either peak_meter_attack_trigger or
1304 * peak_meter_release_trigger. To turn the trigger off call
1305 * peak_meter_trigger_off.
1307 int peak_meter_trigger_status(void)
1309 return trig_status
; /* & TRIG_PIT_MASK;*/
1312 void peak_meter_draw_trig(int xpos
[], int ypos
[],
1313 int trig_width
[], int nb_screens
)
1315 int barstart
[NB_SCREENS
];
1316 int barend
[NB_SCREENS
];
1318 int ixpos
[NB_SCREENS
];
1319 int trigbar_width
[NB_SCREENS
];
1322 trigbar_width
[i
] = (trig_width
[i
] - (2 * (ICON_PLAY_STATE_WIDTH
+ 1)));
1324 switch (trig_status
) {
1341 barend
[i
] = (trig_strt_duration
== 0) ? trigbar_width
[i
] :
1343 (current_tick
- trig_hightime
) / trig_strt_duration
;
1354 barstart
[i
] = trigbar_width
[i
];
1355 barend
[i
] = trigbar_width
[i
];
1359 ixpos
[i
] = xpos
[i
]+ trig_width
[i
] - ICON_PLAY_STATE_WIDTH
;
1365 barstart
[i
] = (trig_stp_hold
== 0) ? 0 :
1366 trigbar_width
[i
] - trigbar_width
[i
] *
1367 (current_tick
- trig_lowtime
) / trig_stp_hold
;
1368 barend
[i
] = trigbar_width
[i
];
1372 ixpos
[i
] = xpos
[i
] + trig_width
[i
] - ICON_PLAY_STATE_WIDTH
;
1379 for(int i
= 0; i
< nb_screens
; i
++)
1381 gui_scrollbar_draw(&screens
[i
], xpos
[i
] + ICON_PLAY_STATE_WIDTH
+ 1,
1382 ypos
[i
] + 1, trigbar_width
[i
], TRIG_HEIGHT
- 2,
1383 trigbar_width
[i
], barstart
[i
], barend
[i
],
1386 screens
[i
].mono_bitmap(bitmap_icons_7x8
[icon
], ixpos
[i
], ypos
[i
],
1387 ICON_PLAY_STATE_WIDTH
, STATUSBAR_HEIGHT
);
1392 int peak_meter_draw_get_btn(int action_context
, int x
[], int y
[],
1393 int height
[], int nb_screens
,
1394 struct viewport vps
[])
1396 int button
= BUTTON_NONE
;
1397 long next_refresh
= current_tick
;
1398 long next_big_refresh
= current_tick
+ HZ
/ 10;
1400 #if (CONFIG_CODEC == SWCODEC)
1401 bool highperf
= false;
1403 /* On MAS targets, we need to poll as often as possible in order to not
1404 * miss a peak, as the MAS does only provide a quasi-peak. When the disk
1405 * is active, it must not draw too much CPU power or a buffer overrun can
1406 * happen when saving a recording. As a compromise, poll only once per tick
1407 * when the disk is active, otherwise spin around as fast as possible. */
1408 bool highperf
= !storage_disk_is_active();
1412 while (TIME_BEFORE(current_tick
, next_big_refresh
)) {
1413 button
= get_action(action_context
, TIMEOUT_NOBLOCK
);
1414 if (button
!= BUTTON_NONE
) {
1417 if (dopeek
) { /* Peek only once per refresh when disk is */
1418 peak_meter_peek(); /* spinning, but as often as possible */
1419 dopeek
= highperf
; /* otherwise. */
1422 sleep(0); /* Sleep until end of current tick. */
1424 if (TIME_AFTER(current_tick
, next_refresh
)) {
1425 for(i
= 0; i
< nb_screens
; i
++)
1427 screens
[i
].set_viewport(&vps
[i
]);
1428 peak_meter_screen(&screens
[i
], x
[i
], y
[i
], height
[i
]);
1429 screens
[i
].update_viewport_rect(x
[i
], y
[i
],
1430 screens
[i
].getwidth() - x
[i
],
1433 next_refresh
+= HZ
/ PEAK_METER_FPS
;
1442 static void peak_meter_clear_histogram(void)
1445 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1446 ticks_per_redraw
[i
] = (unsigned int)0;
1449 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1450 peeks_per_redraw
[i
] = (unsigned int)0;
1454 bool peak_meter_histogram(void)
1457 int btn
= BUTTON_NONE
;
1458 while ((btn
& BUTTON_OFF
) != BUTTON_OFF
)
1460 unsigned int max
= 0;
1463 screens
[0].clear_display();
1465 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1466 max
= MAX(max
, peeks_per_redraw
[i
]);
1469 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1470 x
= peeks_per_redraw
[i
] * (LCD_WIDTH
- 1)/ max
;
1471 screens
[0].hline(0, x
, y
+ i
);
1474 y
= PEEKS_PER_DRAW_SIZE
+ 1;
1477 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1478 max
= MAX(max
, ticks_per_redraw
[i
]);
1481 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1482 x
= ticks_per_redraw
[i
] * (LCD_WIDTH
- 1)/ max
;
1483 screens
[0].hline(0, x
, y
+ i
);
1485 screens
[0].update();
1487 btn
= button_get(true);
1488 if (btn
== BUTTON_PLAY
) {
1489 peak_meter_clear_histogram();
1496 #ifdef HAVE_HISTOGRAM
1497 void histogram_init()
1499 /* get update interval, clear buffer, reset drawing position */
1500 memset(history_l
, 0, sizeof(unsigned char)*HIST_BUF_SIZE
);
1501 memset(history_r
, 0, sizeof(unsigned char)*HIST_BUF_SIZE
);
1502 next_histogram_update
= current_tick
+
1503 (global_settings
.histogram_interval
* HZ
);
1506 void histogram_draw(int x1
, int x2
, int y1
, int y2
, int width
, int height
)
1509 if (current_tick
>= next_histogram_update
)
1511 /* fill history buffer */
1512 history_l
[history_pos
] = hist_l
* height
/ 32767;
1513 history_r
[history_pos
] = hist_r
* height
/ 32767;
1514 history_pos
= (history_pos
+ 1) % HIST_BUF_SIZE
;
1515 history_l
[history_pos
] = history_r
[history_pos
] = 0;
1516 history_l
[(history_pos
+ 1) % HIST_BUF_SIZE
] = 0;
1517 history_r
[(history_pos
+ 1) % HIST_BUF_SIZE
] = 0;
1520 next_histogram_update
= current_tick
+
1521 (global_settings
.histogram_interval
* HZ
);
1523 lcd_set_drawmode(DRMODE_SOLID
);
1524 lcd_drawrect(x1
, y1
, width
, height
);
1525 lcd_drawrect(x2
, y2
, width
, height
);
1526 lcd_set_drawmode(DRMODE_FG
);
1529 for (i
= width
-2; i
>= 0; i
--)
1533 j
= HIST_BUF_SIZE
-1;
1536 if (history_l
[j
] == height
)
1537 lcd_set_foreground(LCD_HIST_OVER
);
1538 #ifdef HAVE_LCD_COLOR
1539 else if (history_l
[j
] > hist_level_marks
[1])
1540 lcd_set_foreground(LCD_HIST_HI
);
1543 lcd_set_foreground(LCD_HIST_OK
);
1544 lcd_vline(x1
+ i
, y1
+ height
- 2, y1
+ height
- history_l
[j
]);
1548 if (history_r
[j
] == height
)
1549 lcd_set_foreground(LCD_HIST_OVER
);
1550 #ifdef HAVE_LCD_COLOR
1551 else if (history_r
[j
] > hist_level_marks
[1])
1552 lcd_set_foreground(LCD_HIST_HI
);
1555 lcd_set_foreground(LCD_HIST_OK
);
1556 lcd_vline(x2
+ i
, y2
+ height
- 2, y2
+ height
- history_r
[j
]);
1560 #ifdef HAVE_LCD_COLOR
1561 global_settings
.fg_color
);
1566 #endif /* HAVE_HISTOGRAM */