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
622 mixer_channel_calculate_peaks(PCM_MIXER_CHAN_PLAYBACK
,
623 &pm_cur_left
, &pm_cur_right
);
624 #ifdef HAVE_RECORDING
626 pcm_calculate_rec_peaks(&pm_cur_left
, &pm_cur_right
);
629 right
= pm_cur_right
;
631 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
632 pm_cur_left
= left
= mas_codec_readreg(pm_src_left
);
633 pm_cur_right
= right
= mas_codec_readreg(pm_src_right
);
635 pm_cur_left
= left
= 8000;
636 pm_cur_right
= right
= 9000;
641 An clip is assumed when two consecutive readouts
642 of the volume are at full scale. This is proven
643 to be inaccurate in both ways: it may detect clips
644 when no clip occurred and it may fail to detect
645 a real clip. For software codecs, the peak is already
646 the max of a bunch of samples, so use one max value
647 or you fail to detect clipping! */
648 #if CONFIG_CODEC == SWCODEC
649 if (left
== MAX_PEAK
- 1) {
651 if ((left
== pm_max_left
) &&
652 (left
== MAX_PEAK
- 1)) {
656 current_tick
+ clip_time_out
[pm_clip_hold
];
659 #if CONFIG_CODEC == SWCODEC
660 if (right
== MAX_PEAK
- 1) {
662 if ((right
== pm_max_right
) &&
663 (right
== MAX_PEAK
- 1)) {
665 pm_clip_right
= true;
667 current_tick
+ clip_time_out
[pm_clip_hold
];
670 #ifdef HAVE_RECORDING
671 if(!was_clipping
&& (pm_clip_left
|| pm_clip_right
))
673 if(pm_clipcount_active
)
678 /* peaks are searched -> we have to find the maximum. When
679 many calls of peak_meter_peek the maximum value will be
680 stored in pm_max_xxx. This maximum is reset by the
681 functions peak_meter_read_x. */
682 pm_max_left
= MAX(pm_max_left
, left
);
683 pm_max_right
= MAX(pm_max_right
, right
);
685 #ifdef HAVE_RECORDING
686 #if CONFIG_CODEC == SWCODEC
687 /* Ignore any unread peakmeter data */
688 #define MAX_DROP_TIME HZ/7 /* this value may need tweaking. Increase if you are
689 getting trig events when you shouldn't with
692 trig_stp_hold
= MAX_DROP_TIME
;
695 switch (trig_status
) {
697 /* no more changes, if trigger was activated as release trigger */
698 /* threshold exceeded? */
699 if ((left
> trig_strt_threshold
)
700 || (right
> trig_strt_threshold
)) {
701 /* reset trigger duration */
702 trig_hightime
= current_tick
;
704 /* reset dropout duration */
705 trig_lowtime
= current_tick
;
707 if (trig_strt_duration
)
708 set_trig_status(TRIG_STEADY
);
710 /* if trig_duration is set to 0 the user wants to start
711 recording immediately */
712 set_trig_status(TRIG_GO
);
718 /* trigger duration exceeded */
719 if (current_tick
- trig_hightime
> trig_strt_duration
) {
720 set_trig_status(TRIG_GO
);
722 /* threshold exceeded? */
723 if ((left
> trig_strt_threshold
)
724 || (right
> trig_strt_threshold
)) {
726 trig_lowtime
= current_tick
;
728 /* volume is below threshold */
730 /* dropout occurred? */
731 if (current_tick
- trig_lowtime
> trig_strt_dropout
){
732 if (trig_status
== TRIG_STEADY
){
733 set_trig_status(TRIG_READY
);
735 /* trig_status == TRIG_RETRIG */
737 /* the gap has already expired */
738 trig_lowtime
= current_tick
- trig_rstrt_gap
- 1;
739 set_trig_status(TRIG_POSTREC
);
748 /* threshold exceeded? */
749 if ((left
> trig_stp_threshold
)
750 || (right
> trig_stp_threshold
)) {
751 /* restart hold time countdown */
752 trig_lowtime
= current_tick
;
753 #if CONFIG_CODEC == SWCODEC
754 } else if (current_tick
- trig_lowtime
> MAX_DROP_TIME
){
758 set_trig_status(TRIG_POSTREC
);
759 trig_hightime
= current_tick
;
764 /* gap time expired? */
765 if (current_tick
- trig_lowtime
> trig_rstrt_gap
){
766 /* start threshold exceeded? */
767 if ((left
> trig_strt_threshold
)
768 || (right
> trig_strt_threshold
)) {
770 set_trig_status(TRIG_RETRIG
);
771 trig_hightime
= current_tick
;
772 trig_lowtime
= current_tick
;
776 /* stop threshold exceeded */
777 if ((left
> trig_stp_threshold
)
778 || (right
> trig_stp_threshold
)) {
779 if (current_tick
- trig_hightime
> trig_stp_hold
){
780 trig_lowtime
= current_tick
;
781 set_trig_status(TRIG_CONTINUE
);
783 trig_lowtime
= current_tick
- trig_rstrt_gap
- 1;
787 /* below any threshold */
789 if (current_tick
- trig_lowtime
> trig_stp_hold
){
790 set_trig_status(TRIG_READY
);
792 trig_hightime
= current_tick
;
797 /* still within the gap time */
799 /* stop threshold exceeded */
800 if ((left
> trig_stp_threshold
)
801 || (right
> trig_stp_threshold
)) {
802 set_trig_status(TRIG_CONTINUE
);
803 trig_lowtime
= current_tick
;
806 /* hold time expired */
807 else if (current_tick
- trig_lowtime
> trig_stp_hold
){
808 trig_hightime
= current_tick
;
809 trig_lowtime
= current_tick
;
810 set_trig_status(TRIG_READY
);
815 #if CONFIG_CODEC == SWCODEC
816 /* restore stop hold value */
817 if (trig_stp_hold
== MAX_DROP_TIME
)
821 /* check levels next time peakmeter drawn */
829 * Reads out the peak volume of the left channel.
830 * @return int - The maximum value that has been detected
831 * since the last call of peak_meter_read_l. The value
832 * is in the range 0 <= value < MAX_PEAK.
834 static int peak_meter_read_l(void)
836 /* pm_max_left contains the maximum of all peak values that were read
837 by peak_meter_peek since the last call of peak_meter_read_l */
840 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
842 pm_max_left
= rand()%MAX_PEAK
;
845 retval
= pm_max_left
;
847 #if defined(HAVE_HISTOGRAM) || defined(HAVE_AGC)
848 /* store max peak value for peak_meter_get_peakhold_x readout */
849 pm_peakhold_left
= MAX(pm_max_left
, pm_peakhold_left
);
854 /* reset pm_max_left so that subsequent calls of peak_meter_peek don't
855 get fooled by an old maximum value */
856 pm_max_left
= pm_cur_left
;
862 * Reads out the peak volume of the right channel.
863 * @return int - The maximum value that has been detected
864 * since the last call of peak_meter_read_l. The value
865 * is in the range 0 <= value < MAX_PEAK.
867 static int peak_meter_read_r(void)
869 /* peak_meter_r contains the maximum of all peak values that were read
870 by peak_meter_peek since the last call of peak_meter_read_r */
873 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
875 pm_max_right
= rand()%MAX_PEAK
;
878 retval
= pm_max_right
;
880 #if defined(HAVE_HISTOGRAM) || defined(HAVE_AGC)
881 /* store max peak value for peak_meter_get_peakhold_x readout */
882 pm_peakhold_right
= MAX(pm_max_right
, pm_peakhold_right
);
887 /* reset pm_max_right so that subsequent calls of peak_meter_peek don't
888 get fooled by an old maximum value */
889 pm_max_right
= pm_cur_right
;
894 #if defined(HAVE_AGC) || defined(HAVE_HISTOGRAM)
896 * Reads out the current peak-hold values since the last call.
897 * This is used by the histogram feature in the recording screen.
898 * Values are in the range 0 <= peak_x < MAX_PEAK. MAX_PEAK is typ 32767.
900 void peak_meter_get_peakhold(int *peak_left
, int *peak_right
)
903 *peak_left
= pm_peakhold_left
;
905 *peak_right
= pm_peakhold_right
;
907 #ifdef HAVE_HISTOGRAM
908 if (*peak_left
> hist_l
)
910 if (*peak_right
> hist_r
)
911 hist_r
= *peak_right
;
914 pm_peakhold_left
= 0;
915 pm_peakhold_right
= 0;
920 * Reset the detected clips. This method is for
921 * use by the user interface.
922 * @param int unused - This parameter was added to
923 * make the function compatible with set_int
925 void peak_meter_set_clip_hold(int time
)
927 pm_clip_left
= false;
928 pm_clip_right
= false;
929 pm_clip_eternal
= (time
> 0) ? false : true;
933 * Scales a peak value as read from the MAS to the range of meterwidth.
934 * The scaling is performed according to the scaling method (dBfs / linear)
935 * and the range (peak_meter_range_min .. peak_meter_range_max).
936 * @param unsigned short val - The volume value. Range: 0 <= val < MAX_PEAK
937 * @param int meterwidht - The widht of the meter in pixel
938 * @return unsigned short - A value 0 <= return value <= meterwidth
940 unsigned short peak_meter_scale_value(unsigned short val
, int meterwidth
)
944 if (val
<= peak_meter_range_min
) {
948 if (val
>= peak_meter_range_max
) {
954 /* different scaling is used for dBfs and linear percent */
957 /* scale the samples dBfs */
958 retval
= (calc_db(retval
) - pm_db_min
) * meterwidth
/ pm_db_range
;
961 /* Scale for linear percent display */
964 /* scale the samples */
965 retval
= ((retval
- peak_meter_range_min
) * meterwidth
)
970 void peak_meter_screen(struct screen
*display
, int x
, int y
, int height
)
972 peak_meter_draw(display
, &scales
[display
->screen_type
], x
, y
,
973 display
->getwidth() - x
, height
);
976 /* sets *left and *right to the current *unscaled* values */
977 void peak_meter_current_vals(int *left
, int *right
)
979 static int left_level
= 0, right_level
= 0;
981 /* only read the volume info from MAS if peek since last read*/
982 left_level
= peak_meter_read_l();
983 right_level
= peak_meter_read_r();
987 *right
= right_level
;
991 * Draws a peak meter in the specified size at the specified position.
992 * @param int x - The x coordinate.
993 * Make sure that 0 <= x and x + width < display->getwidth()
994 * @param int y - The y coordinate.
995 * Make sure that 0 <= y and y + height < display->getheight()
996 * @param int width - The width of the peak meter. Note that for display
997 * of clips a 3 pixel wide area is used ->
999 * @param int height - The height of the peak meter. height > 3
1001 static void peak_meter_draw(struct screen
*display
, struct meter_scales
*scales
,
1002 int x
, int y
, int width
, int height
)
1004 int left_level
= 0, right_level
= 0;
1005 int left
= 0, right
= 0;
1006 int meterwidth
= width
- 3;
1008 #if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
1009 static long peak_release_tick
[2] = {0,0};
1010 int screen_nr
= display
->screen_type
== SCREEN_MAIN
? 0 : 1;
1012 static long peak_release_tick
= 0;
1016 static long pm_tick
= 0;
1017 int tmp
= peek_calls
;
1020 /* if disabled only draw the peak meter */
1021 if (peak_meter_enabled
) {
1024 peak_meter_current_vals(&left_level
, &right_level
);
1026 /* scale the samples dBfs */
1027 left
= peak_meter_scale_value(left_level
, meterwidth
);
1028 right
= peak_meter_scale_value(right_level
, meterwidth
);
1030 /*if the scale has changed -> recalculate the scale
1031 (The scale becomes invalid when the range changed.) */
1032 if (!scales
->db_scale_valid
){
1035 db_scale_count
= DB_SCALE_SRC_VALUES_SIZE
;
1036 for (i
= 0; i
< db_scale_count
; i
++){
1037 /* find the real x-coords for predefined interesting
1038 dBfs values. These only are recalculated when the
1039 scaling of the meter changed. */
1040 scales
->db_scale_lcd_coord
[i
] =
1041 peak_meter_scale_value(
1042 db_scale_src_values
[i
],
1047 /* when scaling linear we simly make 10% steps */
1049 db_scale_count
= 10;
1050 for (i
= 0; i
< db_scale_count
; i
++) {
1051 scales
->db_scale_lcd_coord
[i
] =
1052 (i
* (MAX_PEAK
/ 10) - peak_meter_range_min
) *
1053 meterwidth
/ pm_range
;
1057 /* mark scale valid to avoid recalculating dBfs values
1059 scales
->db_scale_valid
= true;
1063 #if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
1064 delta
= current_tick
- peak_release_tick
[screen_nr
];
1065 peak_release_tick
[screen_nr
] = current_tick
;
1067 delta
= current_tick
- peak_release_tick
;
1068 peak_release_tick
= current_tick
;
1070 left
= MAX(left
, scales
->last_left
- delta
* pm_peak_release
);
1071 right
= MAX(right
, scales
->last_right
- delta
* pm_peak_release
);
1073 /* reset max values after timeout */
1074 if (TIME_AFTER(current_tick
, scales
->pm_peak_timeout_l
)){
1075 scales
->pm_peak_left
= 0;
1078 if (TIME_AFTER(current_tick
, scales
->pm_peak_timeout_r
)){
1079 scales
->pm_peak_right
= 0;
1082 if (!pm_clip_eternal
) {
1084 TIME_AFTER(current_tick
, pm_clip_timeout_l
)){
1085 pm_clip_left
= false;
1088 if (pm_clip_right
&&
1089 TIME_AFTER(current_tick
, pm_clip_timeout_r
)){
1090 pm_clip_right
= false;
1094 /* check for new max values */
1095 if (left
> scales
->pm_peak_left
) {
1096 scales
->pm_peak_left
= left
- 1;
1097 scales
->pm_peak_timeout_l
=
1098 current_tick
+ peak_time_out
[pm_peak_hold
];
1101 if (right
> scales
->pm_peak_right
) {
1102 scales
->pm_peak_right
= right
- 1;
1103 scales
->pm_peak_timeout_r
=
1104 current_tick
+ peak_time_out
[pm_peak_hold
];
1108 /* draw the peak meter */
1109 display
->set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
1110 display
->fillrect(x
, y
, width
, height
);
1111 display
->set_drawmode(DRMODE_SOLID
);
1114 display
->fillrect (x
, y
, left
, height
/ 2 - 2 );
1115 if (scales
->pm_peak_left
> 0) {
1116 display
->vline(x
+ scales
->pm_peak_left
, y
, y
+ height
/ 2 - 2 );
1119 display
->fillrect(x
+ meterwidth
, y
, 3, height
/ 2 - 1);
1123 display
->fillrect(x
, y
+ height
/ 2 + 1, right
, height
/ 2 - 2);
1124 if (scales
->pm_peak_right
> 0) {
1125 display
->vline( x
+ scales
->pm_peak_right
, y
+ height
/ 2, y
+ height
- 2);
1127 if (pm_clip_right
) {
1128 display
->fillrect(x
+ meterwidth
, y
+ height
/ 2, 3, height
/ 2 - 1);
1131 /* draw scale end */
1132 display
->vline(x
+ meterwidth
, y
, y
+ height
- 2);
1134 /* draw dots for scale marks */
1135 for (i
= 0; i
< db_scale_count
; i
++) {
1136 /* The x-coordinates of interesting scale mark points
1137 have been calculated before */
1138 display
->drawpixel(x
+ scales
->db_scale_lcd_coord
[i
],
1139 y
+ height
/ 2 - 1);
1142 #ifdef HAVE_RECORDING
1144 #ifdef HAVE_BACKLIGHT
1146 if ((pm_clip_left
|| pm_clip_right
) &&
1147 global_settings
.cliplight
&&
1148 #if CONFIG_CODEC == SWCODEC
1151 !(audio_status() & (AUDIO_STATUS_PLAY
| AUDIO_STATUS_ERROR
)))
1154 /* if clipping, cliplight setting on and in recording screen */
1155 if (global_settings
.cliplight
<= 2)
1157 /* turn on main unit light if setting set to main or both*/
1160 #ifdef HAVE_REMOTE_LCD
1161 if (global_settings
.cliplight
>= 2)
1163 /* turn remote light unit on if setting set to remote or both */
1164 remote_backlight_on();
1166 #endif /* HAVE_REMOTE_LCD */
1168 #endif /* HAVE_BACKLIGHT */
1170 if (trig_status
!= TRIG_OFF
) {
1171 int start_trigx
, stop_trigx
, ycenter
;
1173 display
->set_drawmode(DRMODE_SOLID
);
1174 ycenter
= y
+ height
/ 2;
1175 /* display threshold value */
1176 start_trigx
= x
+peak_meter_scale_value(trig_strt_threshold
,meterwidth
);
1177 display
->vline(start_trigx
, ycenter
- 2, ycenter
);
1179 if (start_trigx
< display
->getwidth() ) display
->drawpixel(start_trigx
,
1182 stop_trigx
= x
+ peak_meter_scale_value(trig_stp_threshold
,meterwidth
);
1183 display
->vline(stop_trigx
, ycenter
- 2, ycenter
);
1184 if (stop_trigx
> 0) display
->drawpixel(stop_trigx
- 1, ycenter
- 1);
1186 #endif /*HAVE_RECORDING*/
1189 /* display a bar to show how many calls to peak_meter_peek
1190 have ocurred since the last display */
1191 display
->set_drawmode(DRMODE_COMPLEMENT
);
1192 display
->fillrect(x
, y
, tmp
, 3);
1194 if (tmp
< PEEKS_PER_DRAW_SIZE
) {
1195 peeks_per_redraw
[tmp
]++;
1198 tmp
= current_tick
- pm_tick
;
1199 if (tmp
< TICKS_PER_DRAW_SIZE
){
1200 ticks_per_redraw
[tmp
] ++;
1203 /* display a bar to show how many ticks have passed since
1205 display
->fillrect(x
, y
+ height
/ 2, current_tick
- pm_tick
, 2);
1206 pm_tick
= current_tick
;
1209 scales
->last_left
= left
;
1210 scales
->last_right
= right
;
1212 display
->set_drawmode(DRMODE_SOLID
);
1215 #ifdef HAVE_RECORDING
1217 * Defines the parameters of the trigger. After these parameters are defined
1218 * the trigger can be started either by peak_meter_attack_trigger or by
1219 * peak_meter_release_trigger. Note that you can pass either linear (%) or
1220 * logarithmic (db) values to the thresholds. Positive values are intepreted as
1221 * percent (0 is 0% .. 100 is 100%). Negative values are interpreted as db.
1222 * To avoid ambiguosity of the value 0 the negative values are shifted by -1.
1223 * Thus -75 is -74db .. -1 is 0db.
1224 * @param start_threshold - The threshold used for attack trigger. Negative
1225 * values are interpreted as db -1, positive as %.
1226 * @param start_duration - The minimum time span within which start_threshold
1227 * must be exceeded to fire the attack trigger.
1228 * @param start_dropout - The maximum time span the level may fall below
1229 * start_threshold without releasing the attack trigger.
1230 * @param stop_threshold - The threshold the volume must fall below to release
1231 * the release trigger.Negative values are
1232 * interpreted as db -1, positive as %.
1233 * @param stop_hold - The minimum time the volume must fall below the
1234 * stop_threshold to release the trigger.
1237 void peak_meter_define_trigger(
1238 int start_threshold
,
1239 long start_duration
,
1242 long stop_hold_time
,
1246 if (start_threshold
< 0) {
1248 if (start_threshold
< -89) {
1249 trig_strt_threshold
= 0;
1251 trig_strt_threshold
=peak_meter_db2sample((start_threshold
+1)*100);
1254 /* linear percent */
1255 trig_strt_threshold
= start_threshold
* MAX_PEAK
/ 100;
1257 trig_strt_duration
= start_duration
;
1258 trig_strt_dropout
= start_dropout
;
1259 if (stop_threshold
< 0) {
1261 trig_stp_threshold
= peak_meter_db2sample((stop_threshold
+ 1) * 100);
1263 /* linear percent */
1264 trig_stp_threshold
= stop_threshold
* MAX_PEAK
/ 100;
1266 trig_stp_hold
= stop_hold_time
;
1267 trig_rstrt_gap
= restart_gap
;
1271 * Enables or disables the trigger.
1272 * @param on - If true the trigger is turned on.
1274 void peak_meter_trigger(bool on
)
1276 /* don't use set_trigger here as that would fire an undesired event */
1277 trig_status
= on
? TRIG_READY
: TRIG_OFF
;
1281 * Registers the listener function that listenes on trig_status changes.
1282 * @param listener - The function that is called with each change of
1283 * trig_status. May be set to NULL if no callback is desired.
1285 void peak_meter_set_trigger_listener(void (*listener
)(int status
))
1287 trigger_listener
= listener
;
1291 * Fetches the status of the trigger.
1292 * TRIG_OFF: the trigger is inactive
1293 * TRIG_RELEASED: The volume level is below the threshold
1294 * TRIG_ACTIVATED: The volume level has exceeded the threshold, but the trigger
1295 * hasn't been fired yet.
1296 * TRIG_FIRED: The volume exceeds the threshold
1298 * To activate the trigger call either peak_meter_attack_trigger or
1299 * peak_meter_release_trigger. To turn the trigger off call
1300 * peak_meter_trigger_off.
1302 int peak_meter_trigger_status(void)
1304 return trig_status
; /* & TRIG_PIT_MASK;*/
1307 void peak_meter_draw_trig(int xpos
[], int ypos
[],
1308 int trig_width
[], int nb_screens
)
1310 int barstart
[NB_SCREENS
];
1311 int barend
[NB_SCREENS
];
1313 int ixpos
[NB_SCREENS
];
1314 int trigbar_width
[NB_SCREENS
];
1317 trigbar_width
[i
] = (trig_width
[i
] - (2 * (ICON_PLAY_STATE_WIDTH
+ 1)));
1319 switch (trig_status
) {
1336 barend
[i
] = (trig_strt_duration
== 0) ? trigbar_width
[i
] :
1338 (current_tick
- trig_hightime
) / trig_strt_duration
;
1349 barstart
[i
] = trigbar_width
[i
];
1350 barend
[i
] = trigbar_width
[i
];
1354 ixpos
[i
] = xpos
[i
]+ trig_width
[i
] - ICON_PLAY_STATE_WIDTH
;
1360 barstart
[i
] = (trig_stp_hold
== 0) ? 0 :
1361 trigbar_width
[i
] - trigbar_width
[i
] *
1362 (current_tick
- trig_lowtime
) / trig_stp_hold
;
1363 barend
[i
] = trigbar_width
[i
];
1367 ixpos
[i
] = xpos
[i
] + trig_width
[i
] - ICON_PLAY_STATE_WIDTH
;
1374 for(int i
= 0; i
< nb_screens
; i
++)
1376 gui_scrollbar_draw(&screens
[i
], xpos
[i
] + ICON_PLAY_STATE_WIDTH
+ 1,
1377 ypos
[i
] + 1, trigbar_width
[i
], TRIG_HEIGHT
- 2,
1378 trigbar_width
[i
], barstart
[i
], barend
[i
],
1381 screens
[i
].mono_bitmap(bitmap_icons_7x8
[icon
], ixpos
[i
], ypos
[i
],
1382 ICON_PLAY_STATE_WIDTH
, STATUSBAR_HEIGHT
);
1387 int peak_meter_draw_get_btn(int action_context
, int x
[], int y
[],
1388 int height
[], int nb_screens
,
1389 struct viewport vps
[])
1391 int button
= BUTTON_NONE
;
1392 long next_refresh
= current_tick
;
1393 long next_big_refresh
= current_tick
+ HZ
/ 10;
1395 #if (CONFIG_CODEC == SWCODEC)
1396 bool highperf
= false;
1398 /* On MAS targets, we need to poll as often as possible in order to not
1399 * miss a peak, as the MAS does only provide a quasi-peak. When the disk
1400 * is active, it must not draw too much CPU power or a buffer overrun can
1401 * happen when saving a recording. As a compromise, poll only once per tick
1402 * when the disk is active, otherwise spin around as fast as possible. */
1403 bool highperf
= !storage_disk_is_active();
1407 while (TIME_BEFORE(current_tick
, next_big_refresh
)) {
1408 button
= get_action(action_context
, TIMEOUT_NOBLOCK
);
1409 if (button
!= BUTTON_NONE
) {
1412 if (dopeek
) { /* Peek only once per refresh when disk is */
1413 peak_meter_peek(); /* spinning, but as often as possible */
1414 dopeek
= highperf
; /* otherwise. */
1417 sleep(0); /* Sleep until end of current tick. */
1419 if (TIME_AFTER(current_tick
, next_refresh
)) {
1420 for(i
= 0; i
< nb_screens
; i
++)
1422 screens
[i
].set_viewport(&vps
[i
]);
1423 peak_meter_screen(&screens
[i
], x
[i
], y
[i
], height
[i
]);
1424 screens
[i
].update_viewport_rect(x
[i
], y
[i
],
1425 screens
[i
].getwidth() - x
[i
],
1428 next_refresh
+= HZ
/ PEAK_METER_FPS
;
1437 static void peak_meter_clear_histogram(void)
1440 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1441 ticks_per_redraw
[i
] = (unsigned int)0;
1444 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1445 peeks_per_redraw
[i
] = (unsigned int)0;
1449 bool peak_meter_histogram(void)
1452 int btn
= BUTTON_NONE
;
1453 while ((btn
& BUTTON_OFF
) != BUTTON_OFF
)
1455 unsigned int max
= 0;
1458 screens
[0].clear_display();
1460 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1461 max
= MAX(max
, peeks_per_redraw
[i
]);
1464 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1465 x
= peeks_per_redraw
[i
] * (LCD_WIDTH
- 1)/ max
;
1466 screens
[0].hline(0, x
, y
+ i
);
1469 y
= PEEKS_PER_DRAW_SIZE
+ 1;
1472 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1473 max
= MAX(max
, ticks_per_redraw
[i
]);
1476 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1477 x
= ticks_per_redraw
[i
] * (LCD_WIDTH
- 1)/ max
;
1478 screens
[0].hline(0, x
, y
+ i
);
1480 screens
[0].update();
1482 btn
= button_get(true);
1483 if (btn
== BUTTON_PLAY
) {
1484 peak_meter_clear_histogram();
1491 #ifdef HAVE_HISTOGRAM
1492 void histogram_init()
1494 /* get update interval, clear buffer, reset drawing position */
1495 memset(history_l
, 0, sizeof(unsigned char)*HIST_BUF_SIZE
);
1496 memset(history_r
, 0, sizeof(unsigned char)*HIST_BUF_SIZE
);
1497 next_histogram_update
= current_tick
+
1498 (global_settings
.histogram_interval
* HZ
);
1501 void histogram_draw(int x1
, int x2
, int y1
, int y2
, int width
, int height
)
1504 if (current_tick
>= next_histogram_update
)
1506 /* fill history buffer */
1507 history_l
[history_pos
] = hist_l
* height
/ 32767;
1508 history_r
[history_pos
] = hist_r
* height
/ 32767;
1509 history_pos
= (history_pos
+ 1) % HIST_BUF_SIZE
;
1510 history_l
[history_pos
] = history_r
[history_pos
] = 0;
1511 history_l
[(history_pos
+ 1) % HIST_BUF_SIZE
] = 0;
1512 history_r
[(history_pos
+ 1) % HIST_BUF_SIZE
] = 0;
1515 next_histogram_update
= current_tick
+
1516 (global_settings
.histogram_interval
* HZ
);
1518 lcd_set_drawmode(DRMODE_SOLID
);
1519 lcd_drawrect(x1
, y1
, width
, height
);
1520 lcd_drawrect(x2
, y2
, width
, height
);
1521 lcd_set_drawmode(DRMODE_FG
);
1524 for (i
= width
-2; i
>= 0; i
--)
1528 j
= HIST_BUF_SIZE
-1;
1531 if (history_l
[j
] == height
)
1532 lcd_set_foreground(LCD_HIST_OVER
);
1533 #ifdef HAVE_LCD_COLOR
1534 else if (history_l
[j
] > hist_level_marks
[1])
1535 lcd_set_foreground(LCD_HIST_HI
);
1538 lcd_set_foreground(LCD_HIST_OK
);
1539 lcd_vline(x1
+ i
, y1
+ height
- 2, y1
+ height
- history_l
[j
]);
1543 if (history_r
[j
] == height
)
1544 lcd_set_foreground(LCD_HIST_OVER
);
1545 #ifdef HAVE_LCD_COLOR
1546 else if (history_r
[j
] > hist_level_marks
[1])
1547 lcd_set_foreground(LCD_HIST_HI
);
1550 lcd_set_foreground(LCD_HIST_OK
);
1551 lcd_vline(x2
+ i
, y2
+ height
- 2, y2
+ height
- history_r
[j
]);
1555 #ifdef HAVE_LCD_COLOR
1556 global_settings
.fg_color
);
1561 #endif /* HAVE_HISTOGRAM */