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;
885 static long pm_tick
= 0;
886 int tmp
= peek_calls
;
889 /* if disabled only draw the peak meter */
890 if (peak_meter_enabled
) {
894 /* only read the volume info from MAS if peek since last read*/
895 left_level
= peak_meter_read_l();
896 right_level
= peak_meter_read_r();
900 /* scale the samples dBfs */
901 left
= peak_meter_scale_value(left_level
, meterwidth
);
902 right
= peak_meter_scale_value(right_level
, meterwidth
);
904 /*if the scale has changed -> recalculate the scale
905 (The scale becomes invalid when the range changed.) */
906 if (!scales
->db_scale_valid
){
909 db_scale_count
= DB_SCALE_SRC_VALUES_SIZE
;
910 for (i
= 0; i
< db_scale_count
; i
++){
911 /* find the real x-coords for predefined interesting
912 dBfs values. These only are recalculated when the
913 scaling of the meter changed. */
914 scales
->db_scale_lcd_coord
[i
] =
915 peak_meter_scale_value(
916 db_scale_src_values
[i
],
921 /* when scaling linear we simly make 10% steps */
924 for (i
= 0; i
< db_scale_count
; i
++) {
925 scales
->db_scale_lcd_coord
[i
] =
926 (i
* (MAX_PEAK
/ 10) - peak_meter_range_min
) *
927 meterwidth
/ pm_range
;
931 /* mark scale valid to avoid recalculating dBfs values
933 scales
->db_scale_valid
= true;
937 left
= MAX(left
, scales
->last_left
- pm_peak_release
);
938 right
= MAX(right
, scales
->last_right
- pm_peak_release
);
940 /* reset max values after timeout */
941 if (TIME_AFTER(current_tick
, scales
->pm_peak_timeout_l
)){
942 scales
->pm_peak_left
= 0;
945 if (TIME_AFTER(current_tick
, scales
->pm_peak_timeout_r
)){
946 scales
->pm_peak_right
= 0;
949 if (!pm_clip_eternal
) {
951 TIME_AFTER(current_tick
, pm_clip_timeout_l
)){
952 pm_clip_left
= false;
956 TIME_AFTER(current_tick
, pm_clip_timeout_r
)){
957 pm_clip_right
= false;
961 /* check for new max values */
962 if (left
> scales
->pm_peak_left
) {
963 scales
->pm_peak_left
= left
- 1;
964 scales
->pm_peak_timeout_l
=
965 current_tick
+ peak_time_out
[pm_peak_hold
];
968 if (right
> scales
->pm_peak_right
) {
969 scales
->pm_peak_right
= right
- 1;
970 scales
->pm_peak_timeout_r
=
971 current_tick
+ peak_time_out
[pm_peak_hold
];
975 /* draw the peak meter */
976 display
->set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
977 display
->fillrect(x
, y
, width
, height
);
978 display
->set_drawmode(DRMODE_SOLID
);
981 display
->fillrect (x
, y
, left
, height
/ 2 - 2 );
982 if (scales
->pm_peak_left
> 0) {
983 display
->vline(x
+ scales
->pm_peak_left
, y
, y
+ height
/ 2 - 2 );
986 display
->fillrect(x
+ meterwidth
, y
, 3, height
/ 2 - 1);
990 display
->fillrect(x
, y
+ height
/ 2 + 1, right
, height
/ 2 - 2);
991 if (scales
->pm_peak_right
> 0) {
992 display
->vline( x
+ scales
->pm_peak_right
, y
+ height
/ 2, y
+ height
- 2);
995 display
->fillrect(x
+ meterwidth
, y
+ height
/ 2, 3, height
/ 2 - 1);
999 display
->vline(x
+ meterwidth
, y
, y
+ height
- 2);
1001 /* draw dots for scale marks */
1002 for (i
= 0; i
< db_scale_count
; i
++) {
1003 /* The x-coordinates of interesting scale mark points
1004 have been calculated before */
1005 display
->drawpixel(x
+ scales
->db_scale_lcd_coord
[i
],
1006 y
+ height
/ 2 - 1);
1009 #ifdef HAVE_RECORDING
1011 #ifdef HAVE_BACKLIGHT
1013 if ((pm_clip_left
|| pm_clip_right
) &&
1014 global_settings
.cliplight
&&
1015 #if CONFIG_CODEC == SWCODEC
1018 !(audio_status() & (AUDIO_STATUS_PLAY
| AUDIO_STATUS_ERROR
)))
1021 /* if clipping, cliplight setting on and in recording screen */
1022 if (global_settings
.cliplight
<= 2)
1024 /* turn on main unit light if setting set to main or both*/
1027 #ifdef HAVE_REMOTE_LCD
1028 if (global_settings
.cliplight
>= 2)
1030 /* turn remote light unit on if setting set to remote or both */
1031 remote_backlight_on();
1033 #endif /* HAVE_REMOTE_LCD */
1035 #endif /* HAVE_BACKLIGHT */
1037 if (trig_status
!= TRIG_OFF
) {
1038 int start_trigx
, stop_trigx
, ycenter
;
1040 display
->set_drawmode(DRMODE_SOLID
);
1041 ycenter
= y
+ height
/ 2;
1042 /* display threshold value */
1043 start_trigx
= x
+peak_meter_scale_value(trig_strt_threshold
,meterwidth
);
1044 display
->vline(start_trigx
, ycenter
- 2, ycenter
);
1046 if (start_trigx
< display
->width
) display
->drawpixel(start_trigx
, ycenter
- 1);
1048 stop_trigx
= x
+ peak_meter_scale_value(trig_stp_threshold
,meterwidth
);
1049 display
->vline(stop_trigx
, ycenter
- 2, ycenter
);
1050 if (stop_trigx
> 0) display
->drawpixel(stop_trigx
- 1, ycenter
- 1);
1052 #endif /*HAVE_RECORDING*/
1055 /* display a bar to show how many calls to peak_meter_peek
1056 have ocurred since the last display */
1057 display
->set_drawmode(DRMODE_COMPLEMENT
);
1058 display
->fillrect(x
, y
, tmp
, 3);
1060 if (tmp
< PEEKS_PER_DRAW_SIZE
) {
1061 peeks_per_redraw
[tmp
]++;
1064 tmp
= current_tick
- pm_tick
;
1065 if (tmp
< TICKS_PER_DRAW_SIZE
){
1066 ticks_per_redraw
[tmp
] ++;
1069 /* display a bar to show how many ticks have passed since
1071 display
->fillrect(x
, y
+ height
/ 2, current_tick
- pm_tick
, 2);
1072 pm_tick
= current_tick
;
1075 scales
->last_left
= left
;
1076 scales
->last_right
= right
;
1078 display
->set_drawmode(DRMODE_SOLID
);
1081 #ifdef HAVE_RECORDING
1083 * Defines the parameters of the trigger. After these parameters are defined
1084 * the trigger can be started either by peak_meter_attack_trigger or by
1085 * peak_meter_release_trigger. Note that you can pass either linear (%) or
1086 * logarithmic (db) values to the thresholds. Positive values are intepreted as
1087 * percent (0 is 0% .. 100 is 100%). Negative values are interpreted as db.
1088 * To avoid ambiguosity of the value 0 the negative values are shifted by -1.
1089 * Thus -75 is -74db .. -1 is 0db.
1090 * @param start_threshold - The threshold used for attack trigger. Negative
1091 * values are interpreted as db -1, positive as %.
1092 * @param start_duration - The minimum time span within which start_threshold
1093 * must be exceeded to fire the attack trigger.
1094 * @param start_dropout - The maximum time span the level may fall below
1095 * start_threshold without releasing the attack trigger.
1096 * @param stop_threshold - The threshold the volume must fall below to release
1097 * the release trigger.Negative values are
1098 * interpreted as db -1, positive as %.
1099 * @param stop_hold - The minimum time the volume must fall below the
1100 * stop_threshold to release the trigger.
1103 void peak_meter_define_trigger(
1104 int start_threshold
,
1105 long start_duration
,
1108 long stop_hold_time
,
1112 if (start_threshold
< 0) {
1114 if (start_threshold
< -89) {
1115 trig_strt_threshold
= 0;
1117 trig_strt_threshold
=peak_meter_db2sample((start_threshold
+1)*100);
1120 /* linear percent */
1121 trig_strt_threshold
= start_threshold
* MAX_PEAK
/ 100;
1123 trig_strt_duration
= start_duration
;
1124 trig_strt_dropout
= start_dropout
;
1125 if (stop_threshold
< 0) {
1127 trig_stp_threshold
= peak_meter_db2sample((stop_threshold
+ 1) * 100);
1129 /* linear percent */
1130 trig_stp_threshold
= stop_threshold
* MAX_PEAK
/ 100;
1132 trig_stp_hold
= stop_hold_time
;
1133 trig_rstrt_gap
= restart_gap
;
1137 * Enables or disables the trigger.
1138 * @param on - If true the trigger is turned on.
1140 void peak_meter_trigger(bool on
)
1142 /* don't use set_trigger here as that would fire an undesired event */
1143 trig_status
= on
? TRIG_READY
: TRIG_OFF
;
1147 * Registers the listener function that listenes on trig_status changes.
1148 * @param listener - The function that is called with each change of
1149 * trig_status. May be set to NULL if no callback is desired.
1151 void peak_meter_set_trigger_listener(void (*listener
)(int status
))
1153 trigger_listener
= listener
;
1157 * Fetches the status of the trigger.
1158 * TRIG_OFF: the trigger is inactive
1159 * TRIG_RELEASED: The volume level is below the threshold
1160 * TRIG_ACTIVATED: The volume level has exceeded the threshold, but the trigger
1161 * hasn't been fired yet.
1162 * TRIG_FIRED: The volume exceeds the threshold
1164 * To activate the trigger call either peak_meter_attack_trigger or
1165 * peak_meter_release_trigger. To turn the trigger off call
1166 * peak_meter_trigger_off.
1168 int peak_meter_trigger_status(void)
1170 return trig_status
; /* & TRIG_PIT_MASK;*/
1173 void peak_meter_draw_trig(int xpos
[], int ypos
[], int trig_width
[], int nb_screens
)
1175 int barstart
[NB_SCREENS
];
1176 int barend
[NB_SCREENS
];
1178 int ixpos
[NB_SCREENS
];
1180 int trigbar_width
[NB_SCREENS
];
1183 trigbar_width
[i
] = (trig_width
[i
] - (2 * (ICON_PLAY_STATE_WIDTH
+ 1)));
1185 switch (trig_status
) {
1202 barend
[i
] = (trig_strt_duration
== 0) ? trigbar_width
[i
] :
1204 (current_tick
- trig_hightime
) / trig_strt_duration
;
1215 barstart
[i
] = trigbar_width
[i
];
1216 barend
[i
] = trigbar_width
[i
];
1220 ixpos
[i
] = xpos
[i
]+ trig_width
[i
] - ICON_PLAY_STATE_WIDTH
;
1226 barstart
[i
] = (trig_stp_hold
== 0) ? 0 :
1227 trigbar_width
[i
] - trigbar_width
[i
] *
1228 (current_tick
- trig_lowtime
) / trig_stp_hold
;
1229 barend
[i
] = trigbar_width
[i
];
1233 ixpos
[i
] = xpos
[i
] + trig_width
[i
] - ICON_PLAY_STATE_WIDTH
;
1240 for(i
= 0; i
< nb_screens
; i
++)
1242 gui_scrollbar_draw(&screens
[i
], xpos
[i
] + ICON_PLAY_STATE_WIDTH
+ 1,
1243 ypos
[i
] + 1, trigbar_width
[i
], TRIG_HEIGHT
- 2,
1244 trigbar_width
[i
], barstart
[i
], barend
[i
],
1247 screens
[i
].mono_bitmap(bitmap_icons_7x8
[icon
], ixpos
[i
], ypos
[i
],
1248 ICON_PLAY_STATE_WIDTH
, STATUSBAR_HEIGHT
);
1253 int peak_meter_draw_get_btn(int x
, int y
[], int height
, int nb_screens
)
1255 int button
= BUTTON_NONE
;
1256 long next_refresh
= current_tick
;
1257 long next_big_refresh
= current_tick
+ HZ
/ 10;
1260 bool highperf
= !ata_disk_is_active();
1262 bool highperf
= false;
1266 while (TIME_BEFORE(current_tick
, next_big_refresh
)) {
1267 button
= get_action(CONTEXT_RECSCREEN
, TIMEOUT_NOBLOCK
);
1268 if (button
!= BUTTON_NONE
) {
1271 if (dopeek
) { /* Peek only once per refresh when disk is */
1272 peak_meter_peek(); /* spinning, but as often as possible */
1273 dopeek
= highperf
; /* otherwise. */
1276 sleep(0); /* Sleep until end of current tick. */
1278 if (TIME_AFTER(current_tick
, next_refresh
)) {
1279 for(i
= 0; i
< nb_screens
; i
++)
1281 peak_meter_screen(&screens
[i
], x
, y
[i
], height
);
1282 screens
[i
].update_rect(x
, y
[i
], screens
[i
].width
, height
);
1284 next_refresh
+= HZ
/ PEAK_METER_FPS
;
1293 static void peak_meter_clear_histogram(void)
1296 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1297 ticks_per_redraw
[i
] = (unsigned int)0;
1300 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1301 peeks_per_redraw
[i
] = (unsigned int)0;
1305 bool peak_meter_histogram(void)
1308 int btn
= BUTTON_NONE
;
1309 while ((btn
& BUTTON_OFF
) != BUTTON_OFF
)
1311 unsigned int max
= 0;
1314 screens
[0].clear_display();
1316 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1317 max
= MAX(max
, peeks_per_redraw
[i
]);
1320 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1321 x
= peeks_per_redraw
[i
] * (LCD_WIDTH
- 1)/ max
;
1322 screens
[0].hline(0, x
, y
+ i
);
1325 y
= PEEKS_PER_DRAW_SIZE
+ 1;
1328 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1329 max
= MAX(max
, ticks_per_redraw
[i
]);
1332 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1333 x
= ticks_per_redraw
[i
] * (LCD_WIDTH
- 1)/ max
;
1334 screens
[0].hline(0, x
, y
+ i
);
1336 screens
[0].update();
1338 btn
= button_get(true);
1339 if (btn
== BUTTON_PLAY
) {
1340 peak_meter_clear_histogram();