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 ****************************************************************************/
26 #include "scrollbar.h"
34 #include "peakmeter.h"
36 #include "screen_access.h"
38 #include "backlight.h"
42 #if CONFIG_CODEC == SWCODEC
43 #include "pcm_playback.h"
46 #include "pcm_record.h"
49 static bool pm_playback
= true; /* selects between playback and recording peaks */
53 static struct meter_scales scales
[NB_SCREENS
];
55 #if !defined(SIMULATOR) && CONFIG_CODEC != SWCODEC
57 static int pm_src_left
= MAS_REG_DQPEAK_L
;
58 static int pm_src_right
= MAS_REG_DQPEAK_R
;
61 /* Current values and cumulation */
62 static int pm_cur_left
; /* current values (last peak_meter_peek) */
63 static int pm_cur_right
;
64 static int pm_max_left
; /* maximum values between peak meter draws */
65 static int pm_max_right
;
67 static int pm_peakhold_left
; /* max. peak values between peakhold calls */
68 static int pm_peakhold_right
; /* used for AGC and histogram display */
72 static bool pm_clip_left
= false; /* when true a clip has occurred */
73 static bool pm_clip_right
= false;
74 static long pm_clip_timeout_l
; /* clip hold timeouts */
75 static long pm_clip_timeout_r
;
77 /* Temporarily en- / disables peak meter. This is especially for external
78 applications to detect if the peak_meter is in use and needs drawing at all */
79 bool peak_meter_enabled
= true;
83 unsigned short peak_meter_range_min
; /* minimum of range in samples */
84 unsigned short peak_meter_range_max
; /* maximum of range in samples */
85 static unsigned short pm_range
; /* range width in samples */
86 static bool pm_use_dbfs
= true; /* true if peakmeter displays dBfs */
87 static bool level_check
; /* true if peeked at peakmeter before drawing */
88 static unsigned short pm_db_min
= 0; /* minimum of range in 1/100 dB */
89 static unsigned short pm_db_max
= 9000; /* maximum of range in 1/100 dB */
90 static unsigned short pm_db_range
= 9000; /* range width in 1/100 dB */
91 /* Timing behaviour */
92 static int pm_peak_hold
= 1; /* peak hold timeout index */
93 static int pm_peak_release
= 8; /* peak release in units per read */
94 static int pm_clip_hold
= 16; /* clip hold timeout index */
95 static bool pm_clip_eternal
= false; /* true if clip timeout is disabled */
98 static unsigned short trig_strt_threshold
;
99 static long trig_strt_duration
;
100 static long trig_strt_dropout
;
101 static unsigned short trig_stp_threshold
;
102 static long trig_stp_hold
;
103 static long trig_rstrt_gap
;
105 /* point in time when the threshold was exceeded */
106 static long trig_hightime
;
108 /* point in time when the volume fell below the threshold*/
109 static long trig_lowtime
;
111 /* The output value of the trigger. See TRIG_XXX constants for valid values */
112 static int trig_status
= TRIG_OFF
;
114 static void (*trigger_listener
)(int) = NULL
;
119 static int peek_calls
= 0;
121 #define PEEKS_PER_DRAW_SIZE 40
122 static unsigned int peeks_per_redraw
[PEEKS_PER_DRAW_SIZE
];
124 #define TICKS_PER_DRAW_SIZE 20
125 static unsigned int ticks_per_redraw
[TICKS_PER_DRAW_SIZE
];
128 /* time out values for max */
129 static const short peak_time_out
[] = {
130 0 * HZ
, HZ
/ 5, 30, HZ
/ 2, HZ
, 2 * HZ
,
131 3 * HZ
, 4 * HZ
, 5 * HZ
, 6 * HZ
, 7 * HZ
, 8 * HZ
,
132 9 * HZ
, 10 * HZ
, 15 * HZ
, 20 * HZ
, 30 * HZ
, 60 * HZ
135 /* time out values for clip */
136 static const long clip_time_out
[] = {
137 0 * HZ
, 1 * HZ
, 2 * HZ
, 3 * HZ
, 4 * HZ
, 5 * HZ
,
138 6 * HZ
, 7 * HZ
, 8 * HZ
, 9 * HZ
, 10 * HZ
, 15 * HZ
,
139 20 * HZ
, 25 * HZ
, 30 * HZ
, 45 * HZ
, 60 * HZ
, 90 * HZ
,
140 120 * HZ
, 180 * HZ
, 300 * HZ
, 600L * HZ
, 1200L * HZ
,
141 2700L * HZ
, 5400L * HZ
144 /* precalculated peak values that represent magical
145 dBfs values. Used to draw the scale */
146 static const int db_scale_src_values
[DB_SCALE_SRC_VALUES_SIZE
] = {
161 static int db_scale_count
= DB_SCALE_SRC_VALUES_SIZE
;
164 * Calculates dB Value for the peak meter, uses peak value as input
165 * @param int sample - The input value
166 * Make sure that 0 <= value < SAMPLE_RANGE
168 * @return int - The 2 digit fixed point result of the euation
169 * 20 * log (sample / SAMPLE_RANGE) + 90
170 * Output range is 0-9000 (that is 0.0 - 90.0 dB).
171 * Normally 0dB is full scale, here it is shifted +90dB.
172 * The calculation is based on the results of a linear
173 * approximation tool written specifically for this problem
174 * by Andreas Zwirtes (radhard@gmx.de). The result has an
175 * accurracy of better than 2%. It is highly runtime optimized,
176 * the cascading if-clauses do an successive approximation on
177 * the input value. This avoids big lookup-tables and
179 * Improved by Jvo Studer for errors < 0.2dB for critical
180 * range of -12dB to 0dB (78.0 to 90.0dB).
183 int calc_db (int isample
)
185 /* return n+m*(isample-istart)/100 */
190 if (isample
< 2308) { /* Range 1-5 */
192 if (isample
< 115) { /* Range 1-3 */
197 istart
= 1; /* Range 1 */
202 istart
= 5; /* Range 2 */
208 istart
= 24; /* Range 3 */
213 else { /* Range 4-5 */
216 istart
= 114; /* Range 4 */
221 istart
= 588; /* Range 5 */
228 else { /* Range 6-9 */
230 if (isample
< 12932) {
232 if (isample
< 6394) {
233 istart
= 2608; /* Range 6 */
238 istart
= 7000; /* Range 7 */
245 if (isample
< 22450) {
246 istart
= 13000; /* Range 8 */
251 istart
= 22636; /* Range 9 */
258 return n
+ (m
* (long)(isample
- istart
)) / 100L;
263 * A helper function for peak_meter_db2sample. Don't call it separately but
264 * use peak_meter_db2sample. If one or both of min and max are outside the
265 * range 0 <= min (or max) < 8961 the behaviour of this function is
266 * undefined. It may not return.
267 * @param int min - The minimum of the value range that is searched.
268 * @param int max - The maximum of the value range that is searched.
269 * @param int db - The value in dBfs * (-100) for which the according
270 * minimal peak sample is searched.
271 * @return int - A linear volume value with 0 <= value < MAX_PEAK
273 static int db_to_sample_bin_search(int min
, int max
, int db
)
275 int test
= min
+ (max
- min
) / 2;
278 if (calc_db(test
) < db
) {
279 test
= db_to_sample_bin_search(test
, max
, db
);
281 if (calc_db(test
-1) > db
) {
282 test
= db_to_sample_bin_search(min
, test
, db
);
290 * Converts a value representing dBfs to a linear
291 * scaled volume info as it is used by the MAS.
292 * An incredibly inefficiant function which is
293 * the vague inverse of calc_db. This really
294 * should be replaced by something better soon.
296 * @param int db - A dBfs * 100 value with
298 * @return int - The return value is in the range of
299 * 0 <= return value < MAX_PEAK
301 int peak_meter_db2sample(int db
)
305 /* what is the maximum pseudo db value */
306 int max_peak_db
= calc_db(MAX_PEAK
- 1);
308 /* range check: db value to big */
309 if (max_peak_db
+ db
< 0) {
313 /* range check: db value too small */
314 else if (max_peak_db
+ db
>= max_peak_db
) {
315 retval
= MAX_PEAK
-1;
318 /* value in range: find the matching linear value */
320 retval
= db_to_sample_bin_search(0, MAX_PEAK
, max_peak_db
+ db
);
322 /* as this is a dirty function anyway, we want to adjust the
323 full scale hit manually to avoid users complaining that when
324 they adjust maximum for 0 dBfs and display it in percent it
325 shows 99%. That is due to precision loss and this is the
333 * Set the min value for restriction of the value range.
334 * @param int newmin - depending whether dBfs is used
335 * newmin is a value in dBfs * 100 or in linear percent values.
336 * for dBfs: -9000 < newmin <= 0
337 * for linear: 0 <= newmin <= 100
339 void peak_meter_set_min(int newmin
)
342 peak_meter_range_min
= peak_meter_db2sample(newmin
);
345 if (newmin
< peak_meter_range_max
) {
346 peak_meter_range_min
= newmin
* MAX_PEAK
/ 100;
350 pm_range
= peak_meter_range_max
- peak_meter_range_min
;
352 /* Avoid division by zero. */
357 pm_db_min
= calc_db(peak_meter_range_min
);
358 pm_db_range
= pm_db_max
- pm_db_min
;
361 scales
[i
].db_scale_valid
= false;
365 * Returns the minimum value of the range the meter
366 * displays. If the scale is set to dBfs it returns
367 * dBfs values * 100 or linear percent values.
368 * @return: using dBfs : -9000 < value <= 0
369 * using linear scale: 0 <= value <= 100
371 int peak_meter_get_min(void)
375 retval
= calc_db(peak_meter_range_min
) - calc_db(MAX_PEAK
- 1);
377 retval
= peak_meter_range_min
* 100 / MAX_PEAK
;
383 * Set the max value for restriction of the value range.
384 * @param int newmax - depending wether dBfs is used
385 * newmax is a value in dBfs * 100 or in linear percent values.
386 * for dBfs: -9000 < newmax <= 0
387 * for linear: 0 <= newmax <= 100
389 void peak_meter_set_max(int newmax
)
392 peak_meter_range_max
= peak_meter_db2sample(newmax
);
394 if (newmax
> peak_meter_range_min
) {
395 peak_meter_range_max
= newmax
* MAX_PEAK
/ 100;
399 pm_range
= peak_meter_range_max
- peak_meter_range_min
;
401 /* Avoid division by zero. */
406 pm_db_max
= calc_db(peak_meter_range_max
);
407 pm_db_range
= pm_db_max
- pm_db_min
;
410 scales
[i
].db_scale_valid
= false;
414 * Returns the minimum value of the range the meter
415 * displays. If the scale is set to dBfs it returns
416 * dBfs values * 100 or linear percent values
417 * @return: using dBfs : -9000 < value <= 0
418 * using linear scale: 0 <= value <= 100
420 int peak_meter_get_max(void)
424 retval
= calc_db(peak_meter_range_max
) - calc_db(MAX_PEAK
- 1);
426 retval
= peak_meter_range_max
* 100 / MAX_PEAK
;
432 * Returns whether the meter is currently displaying dBfs or percent values.
433 * @return bool - true if the meter is displaying dBfs
434 false if the meter is displaying percent values.
436 bool peak_meter_get_use_dbfs(void)
442 * Specifies whether the values displayed are scaled
443 * as dBfs or as linear percent values.
444 * @param use - set to true for dBfs,
445 * set to false for linear scaling in percent
447 void peak_meter_set_use_dbfs(bool use
)
452 scales
[i
].db_scale_valid
= false;
456 * Initialize the range of the meter. Only values
457 * that are in the range of [range_min ... range_max]
459 * @param bool dbfs - set to true for dBfs,
460 * set to false for linear scaling in percent
461 * @param int range_min - Specifies the lower value of the range.
462 * Pass a value dBfs * 100 when dbfs is set to true.
463 * Pass a percent value when dbfs is set to false.
464 * @param int range_max - Specifies the upper value of the range.
465 * Pass a value dBfs * 100 when dbfs is set to true.
466 * Pass a percent value when dbfs is set to false.
468 void peak_meter_init_range( bool dbfs
, int range_min
, int range_max
)
471 peak_meter_set_min(range_min
);
472 peak_meter_set_max(range_max
);
476 * Initialize the peak meter with all relevant values concerning times.
477 * @param int release - Set the maximum amount of pixels the meter is allowed
478 * to decrease with each redraw
479 * @param int hold - Select the time preset for the time the peak indicator
480 * is reset after a peak occurred. The preset values are
481 * stored in peak_time_out.
482 * @param int clip_hold - Select the time preset for the time the peak
483 * indicator is reset after a peak occurred. The preset
484 * values are stored in clip_time_out.
486 void peak_meter_init_times(int release
, int hold
, int clip_hold
)
489 pm_peak_release
= release
;
490 pm_clip_hold
= clip_hold
;
494 * Set the source of the peak meter to playback or to
496 * @param: bool playback - If true playback peak meter is used.
497 * If false recording peak meter is used.
499 void peak_meter_playback(bool playback
)
503 #elif CONFIG_CODEC == SWCODEC
504 pm_playback
= playback
;
507 pm_src_left
= MAS_REG_DQPEAK_L
;
508 pm_src_right
= MAS_REG_DQPEAK_R
;
510 pm_src_left
= MAS_REG_QPEAK_L
;
511 pm_src_right
= MAS_REG_QPEAK_R
;
516 #ifdef HAVE_RECORDING
517 static void set_trig_status(int new_state
)
519 if (trig_status
!= new_state
) {
520 trig_status
= new_state
;
521 if (trigger_listener
!= NULL
) {
522 trigger_listener(trig_status
);
529 * Reads peak values from the MAS, and detects clips. The
530 * values are stored in pm_max_left pm_max_right for later
531 * evauluation. Consecutive calls to peak_meter_peek detect
532 * that ocurred. This function could be used by a thread for
533 * busy reading the MAS.
535 void peak_meter_peek(void)
538 /* read current values */
539 #if CONFIG_CODEC == SWCODEC
541 pcm_calculate_peaks(&pm_cur_left
, &pm_cur_right
);
542 #ifdef HAVE_RECORDING
544 pcm_calculate_rec_peaks(&pm_cur_left
, &pm_cur_right
);
547 right
= pm_cur_right
;
550 pm_cur_left
= left
= mas_codec_readreg(pm_src_left
);
551 pm_cur_right
= right
= mas_codec_readreg(pm_src_right
);
553 pm_cur_left
= left
= 8000;
554 pm_cur_right
= right
= 9000;
559 An clip is assumed when two consecutive readouts
560 of the volume are at full scale. This is proven
561 to be inaccurate in both ways: it may detect clips
562 when no clip occurred and it may fail to detect
563 a real clip. For software codecs, the peak is already
564 the max of a bunch of samples, so use one max value
565 or you fail to detect clipping! */
566 #if CONFIG_CODEC == SWCODEC
567 if (left
== MAX_PEAK
- 1) {
569 if ((left
== pm_max_left
) &&
570 (left
== MAX_PEAK
- 1)) {
574 current_tick
+ clip_time_out
[pm_clip_hold
];
577 #if CONFIG_CODEC == SWCODEC
578 if (right
== MAX_PEAK
- 1) {
580 if ((right
== pm_max_right
) &&
581 (right
== MAX_PEAK
- 1)) {
583 pm_clip_right
= true;
585 current_tick
+ clip_time_out
[pm_clip_hold
];
588 /* peaks are searched -> we have to find the maximum. When
589 many calls of peak_meter_peek the maximum value will be
590 stored in pm_max_xxx. This maximum is reset by the
591 functions peak_meter_read_x. */
592 pm_max_left
= MAX(pm_max_left
, left
);
593 pm_max_right
= MAX(pm_max_right
, right
);
595 #ifdef HAVE_RECORDING
596 #if CONFIG_CODEC == SWCODEC
597 /* Ignore any unread peakmeter data */
598 #define MAX_DROP_TIME HZ/7 /* this value may need tweaking. Increase if you are
599 getting trig events when you shouldn't with
602 trig_stp_hold
= MAX_DROP_TIME
;
605 switch (trig_status
) {
607 /* no more changes, if trigger was activated as release trigger */
608 /* threshold exceeded? */
609 if ((left
> trig_strt_threshold
)
610 || (right
> trig_strt_threshold
)) {
611 /* reset trigger duration */
612 trig_hightime
= current_tick
;
614 /* reset dropout duration */
615 trig_lowtime
= current_tick
;
617 if (trig_strt_duration
)
618 set_trig_status(TRIG_STEADY
);
620 /* if trig_duration is set to 0 the user wants to start
621 recording immediately */
622 set_trig_status(TRIG_GO
);
628 /* trigger duration exceeded */
629 if (current_tick
- trig_hightime
> trig_strt_duration
) {
630 set_trig_status(TRIG_GO
);
632 /* threshold exceeded? */
633 if ((left
> trig_strt_threshold
)
634 || (right
> trig_strt_threshold
)) {
636 trig_lowtime
= current_tick
;
638 /* volume is below threshold */
640 /* dropout occurred? */
641 if (current_tick
- trig_lowtime
> trig_strt_dropout
){
642 if (trig_status
== TRIG_STEADY
){
643 set_trig_status(TRIG_READY
);
645 /* trig_status == TRIG_RETRIG */
647 /* the gap has already expired */
648 trig_lowtime
= current_tick
- trig_rstrt_gap
- 1;
649 set_trig_status(TRIG_POSTREC
);
658 /* threshold exceeded? */
659 if ((left
> trig_stp_threshold
)
660 || (right
> trig_stp_threshold
)) {
661 /* restart hold time countdown */
662 trig_lowtime
= current_tick
;
663 #if CONFIG_CODEC == SWCODEC
664 } else if (current_tick
- trig_lowtime
> MAX_DROP_TIME
){
668 set_trig_status(TRIG_POSTREC
);
669 trig_hightime
= current_tick
;
674 /* gap time expired? */
675 if (current_tick
- trig_lowtime
> trig_rstrt_gap
){
676 /* start threshold exceeded? */
677 if ((left
> trig_strt_threshold
)
678 || (right
> trig_strt_threshold
)) {
680 set_trig_status(TRIG_RETRIG
);
681 trig_hightime
= current_tick
;
682 trig_lowtime
= current_tick
;
686 /* stop threshold exceeded */
687 if ((left
> trig_stp_threshold
)
688 || (right
> trig_stp_threshold
)) {
689 if (current_tick
- trig_hightime
> trig_stp_hold
){
690 trig_lowtime
= current_tick
;
691 set_trig_status(TRIG_CONTINUE
);
693 trig_lowtime
= current_tick
- trig_rstrt_gap
- 1;
697 /* below any threshold */
699 if (current_tick
- trig_lowtime
> trig_stp_hold
){
700 set_trig_status(TRIG_READY
);
702 trig_hightime
= current_tick
;
707 /* still within the gap time */
709 /* stop threshold exceeded */
710 if ((left
> trig_stp_threshold
)
711 || (right
> trig_stp_threshold
)) {
712 set_trig_status(TRIG_CONTINUE
);
713 trig_lowtime
= current_tick
;
716 /* hold time expired */
717 else if (current_tick
- trig_lowtime
> trig_stp_hold
){
718 trig_hightime
= current_tick
;
719 trig_lowtime
= current_tick
;
720 set_trig_status(TRIG_READY
);
725 #if CONFIG_CODEC == SWCODEC
726 /* restore stop hold value */
727 if (trig_stp_hold
== MAX_DROP_TIME
)
731 /* check levels next time peakmeter drawn */
739 * Reads out the peak volume of the left channel.
740 * @return int - The maximum value that has been detected
741 * since the last call of peak_meter_read_l. The value
742 * is in the range 0 <= value < MAX_PEAK.
744 static int peak_meter_read_l(void)
746 /* pm_max_left contains the maximum of all peak values that were read
747 by peak_meter_peek since the last call of peak_meter_read_l */
748 int retval
= pm_max_left
;
751 /* store max peak value for peak_meter_get_peakhold_x readout */
752 pm_peakhold_left
= MAX(pm_max_left
, pm_peakhold_left
);
757 /* reset pm_max_left so that subsequent calls of peak_meter_peek don't
758 get fooled by an old maximum value */
759 pm_max_left
= pm_cur_left
;
764 * Reads out the peak volume of the right channel.
765 * @return int - The maximum value that has been detected
766 * since the last call of peak_meter_read_l. The value
767 * is in the range 0 <= value < MAX_PEAK.
769 static int peak_meter_read_r(void)
771 /* peak_meter_r contains the maximum of all peak values that were read
772 by peak_meter_peek since the last call of peak_meter_read_r */
773 int retval
= pm_max_right
;
776 /* store max peak value for peak_meter_get_peakhold_x readout */
777 pm_peakhold_right
= MAX(pm_max_right
, pm_peakhold_right
);
782 /* reset pm_max_right so that subsequent calls of peak_meter_peek don't
783 get fooled by an old maximum value */
784 pm_max_right
= pm_cur_right
;
790 * Reads out the current peak-hold values since the last call.
791 * This is used by the histogram feature in the recording screen.
792 * Values are in the range 0 <= peak_x < MAX_PEAK. MAX_PEAK is typ 32767.
794 void peak_meter_get_peakhold(int *peak_left
, int *peak_right
)
797 *peak_left
= pm_peakhold_left
;
799 *peak_right
= pm_peakhold_right
;
800 pm_peakhold_left
= 0;
801 pm_peakhold_right
= 0;
806 * Reset the detected clips. This method is for
807 * use by the user interface.
808 * @param int unused - This parameter was added to
809 * make the function compatible with set_int
811 void peak_meter_set_clip_hold(int time
)
813 pm_clip_eternal
= false;
816 pm_clip_left
= false;
817 pm_clip_right
= false;
818 pm_clip_eternal
= true;
823 * Scales a peak value as read from the MAS to the range of meterwidth.
824 * The scaling is performed according to the scaling method (dBfs / linear)
825 * and the range (peak_meter_range_min .. peak_meter_range_max).
826 * @param unsigned short val - The volume value. Range: 0 <= val < MAX_PEAK
827 * @param int meterwidht - The widht of the meter in pixel
828 * @return unsigned short - A value 0 <= return value <= meterwidth
830 unsigned short peak_meter_scale_value(unsigned short val
, int meterwidth
)
834 if (val
<= peak_meter_range_min
) {
838 if (val
>= peak_meter_range_max
) {
844 /* different scaling is used for dBfs and linear percent */
847 /* scale the samples dBfs */
848 retval
= (calc_db(retval
) - pm_db_min
) * meterwidth
/ pm_db_range
;
851 /* Scale for linear percent display */
854 /* scale the samples */
855 retval
= ((retval
- peak_meter_range_min
) * meterwidth
)
860 void peak_meter_screen(struct screen
*display
, int x
, int y
, int height
)
862 peak_meter_draw(display
, &scales
[display
->screen_type
], x
, y
,
863 display
->width
, height
);
866 * Draws a peak meter in the specified size at the specified position.
867 * @param int x - The x coordinate.
868 * Make sure that 0 <= x and x + width < display->width
869 * @param int y - The y coordinate.
870 * Make sure that 0 <= y and y + height < display->height
871 * @param int width - The width of the peak meter. Note that for display
872 * of clips a 3 pixel wide area is used ->
874 * @param int height - The height of the peak meter. height > 3
876 void peak_meter_draw(struct screen
*display
, struct meter_scales
*scales
,
877 int x
, int y
, int width
, int height
)
879 static int left_level
= 0, right_level
= 0;
880 int left
= 0, right
= 0;
881 int meterwidth
= width
- 3;
883 static long peak_release_tick
= 0;
886 static long pm_tick
= 0;
887 int tmp
= peek_calls
;
890 /* if disabled only draw the peak meter */
891 if (peak_meter_enabled
) {
895 /* only read the volume info from MAS if peek since last read*/
896 left_level
= peak_meter_read_l();
897 right_level
= peak_meter_read_r();
901 /* scale the samples dBfs */
902 left
= peak_meter_scale_value(left_level
, meterwidth
);
903 right
= peak_meter_scale_value(right_level
, meterwidth
);
905 /*if the scale has changed -> recalculate the scale
906 (The scale becomes invalid when the range changed.) */
907 if (!scales
->db_scale_valid
){
910 db_scale_count
= DB_SCALE_SRC_VALUES_SIZE
;
911 for (i
= 0; i
< db_scale_count
; i
++){
912 /* find the real x-coords for predefined interesting
913 dBfs values. These only are recalculated when the
914 scaling of the meter changed. */
915 scales
->db_scale_lcd_coord
[i
] =
916 peak_meter_scale_value(
917 db_scale_src_values
[i
],
922 /* when scaling linear we simly make 10% steps */
925 for (i
= 0; i
< db_scale_count
; i
++) {
926 scales
->db_scale_lcd_coord
[i
] =
927 (i
* (MAX_PEAK
/ 10) - peak_meter_range_min
) *
928 meterwidth
/ pm_range
;
932 /* mark scale valid to avoid recalculating dBfs values
934 scales
->db_scale_valid
= true;
938 if(current_tick
!= peak_release_tick
)
940 peak_release_tick
= current_tick
;
941 left
= MAX(left
, scales
->last_left
- pm_peak_release
);
942 right
= MAX(right
, scales
->last_right
- pm_peak_release
);
946 left
= MAX(left
, scales
->last_left
);
947 right
= MAX(right
, scales
->last_right
);
949 /* reset max values after timeout */
950 if (TIME_AFTER(current_tick
, scales
->pm_peak_timeout_l
)){
951 scales
->pm_peak_left
= 0;
954 if (TIME_AFTER(current_tick
, scales
->pm_peak_timeout_r
)){
955 scales
->pm_peak_right
= 0;
958 if (!pm_clip_eternal
) {
960 TIME_AFTER(current_tick
, pm_clip_timeout_l
)){
961 pm_clip_left
= false;
965 TIME_AFTER(current_tick
, pm_clip_timeout_r
)){
966 pm_clip_right
= false;
970 /* check for new max values */
971 if (left
> scales
->pm_peak_left
) {
972 scales
->pm_peak_left
= left
- 1;
973 scales
->pm_peak_timeout_l
=
974 current_tick
+ peak_time_out
[pm_peak_hold
];
977 if (right
> scales
->pm_peak_right
) {
978 scales
->pm_peak_right
= right
- 1;
979 scales
->pm_peak_timeout_r
=
980 current_tick
+ peak_time_out
[pm_peak_hold
];
984 /* draw the peak meter */
985 display
->set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
986 display
->fillrect(x
, y
, width
, height
);
987 display
->set_drawmode(DRMODE_SOLID
);
990 display
->fillrect (x
, y
, left
, height
/ 2 - 2 );
991 if (scales
->pm_peak_left
> 0) {
992 display
->vline(x
+ scales
->pm_peak_left
, y
, y
+ height
/ 2 - 2 );
995 display
->fillrect(x
+ meterwidth
, y
, 3, height
/ 2 - 1);
999 display
->fillrect(x
, y
+ height
/ 2 + 1, right
, height
/ 2 - 2);
1000 if (scales
->pm_peak_right
> 0) {
1001 display
->vline( x
+ scales
->pm_peak_right
, y
+ height
/ 2, y
+ height
- 2);
1003 if (pm_clip_right
) {
1004 display
->fillrect(x
+ meterwidth
, y
+ height
/ 2, 3, height
/ 2 - 1);
1007 /* draw scale end */
1008 display
->vline(x
+ meterwidth
, y
, y
+ height
- 2);
1010 /* draw dots for scale marks */
1011 for (i
= 0; i
< db_scale_count
; i
++) {
1012 /* The x-coordinates of interesting scale mark points
1013 have been calculated before */
1014 display
->drawpixel(x
+ scales
->db_scale_lcd_coord
[i
],
1015 y
+ height
/ 2 - 1);
1018 #ifdef HAVE_RECORDING
1020 #ifdef HAVE_BACKLIGHT
1022 if ((pm_clip_left
|| pm_clip_right
) &&
1023 global_settings
.cliplight
&&
1024 #if CONFIG_CODEC == SWCODEC
1027 !(audio_status() & (AUDIO_STATUS_PLAY
| AUDIO_STATUS_ERROR
)))
1030 /* if clipping, cliplight setting on and in recording screen */
1031 if (global_settings
.cliplight
<= 2)
1033 /* turn on main unit light if setting set to main or both*/
1036 #ifdef HAVE_REMOTE_LCD
1037 if (global_settings
.cliplight
>= 2)
1039 /* turn remote light unit on if setting set to remote or both */
1040 remote_backlight_on();
1042 #endif /* HAVE_REMOTE_LCD */
1044 #endif /* HAVE_BACKLIGHT */
1046 if (trig_status
!= TRIG_OFF
) {
1047 int start_trigx
, stop_trigx
, ycenter
;
1049 display
->set_drawmode(DRMODE_SOLID
);
1050 ycenter
= y
+ height
/ 2;
1051 /* display threshold value */
1052 start_trigx
= x
+peak_meter_scale_value(trig_strt_threshold
,meterwidth
);
1053 display
->vline(start_trigx
, ycenter
- 2, ycenter
);
1055 if (start_trigx
< display
->width
) display
->drawpixel(start_trigx
, ycenter
- 1);
1057 stop_trigx
= x
+ peak_meter_scale_value(trig_stp_threshold
,meterwidth
);
1058 display
->vline(stop_trigx
, ycenter
- 2, ycenter
);
1059 if (stop_trigx
> 0) display
->drawpixel(stop_trigx
- 1, ycenter
- 1);
1061 #endif /*HAVE_RECORDING*/
1064 /* display a bar to show how many calls to peak_meter_peek
1065 have ocurred since the last display */
1066 display
->set_drawmode(DRMODE_COMPLEMENT
);
1067 display
->fillrect(x
, y
, tmp
, 3);
1069 if (tmp
< PEEKS_PER_DRAW_SIZE
) {
1070 peeks_per_redraw
[tmp
]++;
1073 tmp
= current_tick
- pm_tick
;
1074 if (tmp
< TICKS_PER_DRAW_SIZE
){
1075 ticks_per_redraw
[tmp
] ++;
1078 /* display a bar to show how many ticks have passed since
1080 display
->fillrect(x
, y
+ height
/ 2, current_tick
- pm_tick
, 2);
1081 pm_tick
= current_tick
;
1084 scales
->last_left
= left
;
1085 scales
->last_right
= right
;
1087 display
->set_drawmode(DRMODE_SOLID
);
1090 #ifdef HAVE_RECORDING
1092 * Defines the parameters of the trigger. After these parameters are defined
1093 * the trigger can be started either by peak_meter_attack_trigger or by
1094 * peak_meter_release_trigger. Note that you can pass either linear (%) or
1095 * logarithmic (db) values to the thresholds. Positive values are intepreted as
1096 * percent (0 is 0% .. 100 is 100%). Negative values are interpreted as db.
1097 * To avoid ambiguosity of the value 0 the negative values are shifted by -1.
1098 * Thus -75 is -74db .. -1 is 0db.
1099 * @param start_threshold - The threshold used for attack trigger. Negative
1100 * values are interpreted as db -1, positive as %.
1101 * @param start_duration - The minimum time span within which start_threshold
1102 * must be exceeded to fire the attack trigger.
1103 * @param start_dropout - The maximum time span the level may fall below
1104 * start_threshold without releasing the attack trigger.
1105 * @param stop_threshold - The threshold the volume must fall below to release
1106 * the release trigger.Negative values are
1107 * interpreted as db -1, positive as %.
1108 * @param stop_hold - The minimum time the volume must fall below the
1109 * stop_threshold to release the trigger.
1112 void peak_meter_define_trigger(
1113 int start_threshold
,
1114 long start_duration
,
1117 long stop_hold_time
,
1121 if (start_threshold
< 0) {
1123 if (start_threshold
< -89) {
1124 trig_strt_threshold
= 0;
1126 trig_strt_threshold
=peak_meter_db2sample((start_threshold
+1)*100);
1129 /* linear percent */
1130 trig_strt_threshold
= start_threshold
* MAX_PEAK
/ 100;
1132 trig_strt_duration
= start_duration
;
1133 trig_strt_dropout
= start_dropout
;
1134 if (stop_threshold
< 0) {
1136 trig_stp_threshold
= peak_meter_db2sample((stop_threshold
+ 1) * 100);
1138 /* linear percent */
1139 trig_stp_threshold
= stop_threshold
* MAX_PEAK
/ 100;
1141 trig_stp_hold
= stop_hold_time
;
1142 trig_rstrt_gap
= restart_gap
;
1146 * Enables or disables the trigger.
1147 * @param on - If true the trigger is turned on.
1149 void peak_meter_trigger(bool on
)
1151 /* don't use set_trigger here as that would fire an undesired event */
1152 trig_status
= on
? TRIG_READY
: TRIG_OFF
;
1156 * Registers the listener function that listenes on trig_status changes.
1157 * @param listener - The function that is called with each change of
1158 * trig_status. May be set to NULL if no callback is desired.
1160 void peak_meter_set_trigger_listener(void (*listener
)(int status
))
1162 trigger_listener
= listener
;
1166 * Fetches the status of the trigger.
1167 * TRIG_OFF: the trigger is inactive
1168 * TRIG_RELEASED: The volume level is below the threshold
1169 * TRIG_ACTIVATED: The volume level has exceeded the threshold, but the trigger
1170 * hasn't been fired yet.
1171 * TRIG_FIRED: The volume exceeds the threshold
1173 * To activate the trigger call either peak_meter_attack_trigger or
1174 * peak_meter_release_trigger. To turn the trigger off call
1175 * peak_meter_trigger_off.
1177 int peak_meter_trigger_status(void)
1179 return trig_status
; /* & TRIG_PIT_MASK;*/
1182 void peak_meter_draw_trig(int xpos
[], int ypos
[], int trig_width
[], int nb_screens
)
1184 int barstart
[NB_SCREENS
];
1185 int barend
[NB_SCREENS
];
1187 int ixpos
[NB_SCREENS
];
1189 int trigbar_width
[NB_SCREENS
];
1192 trigbar_width
[i
] = (trig_width
[i
] - (2 * (ICON_PLAY_STATE_WIDTH
+ 1)));
1194 switch (trig_status
) {
1211 barend
[i
] = (trig_strt_duration
== 0) ? trigbar_width
[i
] :
1213 (current_tick
- trig_hightime
) / trig_strt_duration
;
1224 barstart
[i
] = trigbar_width
[i
];
1225 barend
[i
] = trigbar_width
[i
];
1229 ixpos
[i
] = xpos
[i
]+ trig_width
[i
] - ICON_PLAY_STATE_WIDTH
;
1235 barstart
[i
] = (trig_stp_hold
== 0) ? 0 :
1236 trigbar_width
[i
] - trigbar_width
[i
] *
1237 (current_tick
- trig_lowtime
) / trig_stp_hold
;
1238 barend
[i
] = trigbar_width
[i
];
1242 ixpos
[i
] = xpos
[i
] + trig_width
[i
] - ICON_PLAY_STATE_WIDTH
;
1249 for(i
= 0; i
< nb_screens
; i
++)
1251 gui_scrollbar_draw(&screens
[i
], xpos
[i
] + ICON_PLAY_STATE_WIDTH
+ 1,
1252 ypos
[i
] + 1, trigbar_width
[i
], TRIG_HEIGHT
- 2,
1253 trigbar_width
[i
], barstart
[i
], barend
[i
],
1256 screens
[i
].mono_bitmap(bitmap_icons_7x8
[icon
], ixpos
[i
], ypos
[i
],
1257 ICON_PLAY_STATE_WIDTH
, STATUSBAR_HEIGHT
);
1262 int peak_meter_draw_get_btn(int x
, int y
[], int height
, int nb_screens
)
1264 int button
= BUTTON_NONE
;
1265 long next_refresh
= current_tick
;
1266 long next_big_refresh
= current_tick
+ HZ
/ 10;
1269 bool highperf
= !ata_disk_is_active();
1271 bool highperf
= false;
1275 while (TIME_BEFORE(current_tick
, next_big_refresh
)) {
1276 button
= get_action(CONTEXT_RECSCREEN
, TIMEOUT_NOBLOCK
);
1277 if (button
!= BUTTON_NONE
) {
1280 if (dopeek
) { /* Peek only once per refresh when disk is */
1281 peak_meter_peek(); /* spinning, but as often as possible */
1282 dopeek
= highperf
; /* otherwise. */
1285 sleep(0); /* Sleep until end of current tick. */
1287 if (TIME_AFTER(current_tick
, next_refresh
)) {
1288 for(i
= 0; i
< nb_screens
; i
++)
1290 peak_meter_screen(&screens
[i
], x
, y
[i
], height
);
1291 screens
[i
].update_rect(x
, y
[i
], screens
[i
].width
, height
);
1293 next_refresh
+= HZ
/ PEAK_METER_FPS
;
1302 static void peak_meter_clear_histogram(void)
1305 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1306 ticks_per_redraw
[i
] = (unsigned int)0;
1309 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1310 peeks_per_redraw
[i
] = (unsigned int)0;
1314 bool peak_meter_histogram(void)
1317 int btn
= BUTTON_NONE
;
1318 while ((btn
& BUTTON_OFF
) != BUTTON_OFF
)
1320 unsigned int max
= 0;
1323 screens
[0].clear_display();
1325 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1326 max
= MAX(max
, peeks_per_redraw
[i
]);
1329 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1330 x
= peeks_per_redraw
[i
] * (LCD_WIDTH
- 1)/ max
;
1331 screens
[0].hline(0, x
, y
+ i
);
1334 y
= PEEKS_PER_DRAW_SIZE
+ 1;
1337 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1338 max
= MAX(max
, ticks_per_redraw
[i
]);
1341 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1342 x
= ticks_per_redraw
[i
] * (LCD_WIDTH
- 1)/ max
;
1343 screens
[0].hline(0, x
, y
+ i
);
1345 screens
[0].update();
1347 btn
= button_get(true);
1348 if (btn
== BUTTON_PLAY
) {
1349 peak_meter_clear_histogram();