1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Philipp Pertermann
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <stdlib.h> /* sim uses rand for peakmeter simulation */
29 #include "scrollbar.h"
37 #include "peakmeter.h"
39 #include "screen_access.h"
41 #include "backlight.h"
45 #if CONFIG_CODEC == SWCODEC
46 #include "pcm_playback.h"
49 #include "pcm_record.h"
52 static bool pm_playback
= true; /* selects between playback and recording peaks */
55 static struct meter_scales scales
[NB_SCREENS
];
57 #if !defined(SIMULATOR) && CONFIG_CODEC != SWCODEC
59 static int pm_src_left
= MAS_REG_DQPEAK_L
;
60 static int pm_src_right
= MAS_REG_DQPEAK_R
;
63 /* Current values and cumulation */
64 static int pm_cur_left
; /* current values (last peak_meter_peek) */
65 static int pm_cur_right
;
66 static int pm_max_left
; /* maximum values between peak meter draws */
67 static int pm_max_right
;
69 static int pm_peakhold_left
; /* max. peak values between peakhold calls */
70 static int pm_peakhold_right
; /* used for AGC and histogram display */
74 static bool pm_clip_left
= false; /* when true a clip has occurred */
75 static bool pm_clip_right
= false;
76 static long pm_clip_timeout_l
; /* clip hold timeouts */
77 static long pm_clip_timeout_r
;
79 /* Temporarily en- / disables peak meter. This is especially for external
80 applications to detect if the peak_meter is in use and needs drawing at all */
81 bool peak_meter_enabled
= true;
85 unsigned short peak_meter_range_min
; /* minimum of range in samples */
86 unsigned short peak_meter_range_max
; /* maximum of range in samples */
87 static unsigned short pm_range
; /* range width in samples */
88 static bool pm_use_dbfs
= true; /* true if peakmeter displays dBfs */
89 static bool level_check
; /* true if peeked at peakmeter before drawing */
90 static unsigned short pm_db_min
= 0; /* minimum of range in 1/100 dB */
91 static unsigned short pm_db_max
= 9000; /* maximum of range in 1/100 dB */
92 static unsigned short pm_db_range
= 9000; /* range width in 1/100 dB */
93 /* Timing behaviour */
94 static int pm_peak_hold
= 1; /* peak hold timeout index */
95 static int pm_peak_release
= 8; /* peak release in units per read */
96 static int pm_clip_hold
= 16; /* clip hold timeout index */
97 static bool pm_clip_eternal
= false; /* true if clip timeout is disabled */
100 static unsigned short trig_strt_threshold
;
101 static long trig_strt_duration
;
102 static long trig_strt_dropout
;
103 static unsigned short trig_stp_threshold
;
104 static long trig_stp_hold
;
105 static long trig_rstrt_gap
;
107 /* point in time when the threshold was exceeded */
108 static long trig_hightime
;
110 /* point in time when the volume fell below the threshold*/
111 static long trig_lowtime
;
113 /* The output value of the trigger. See TRIG_XXX constants for valid values */
114 static int trig_status
= TRIG_OFF
;
116 static void (*trigger_listener
)(int) = NULL
;
118 /* clipping counter (only used for recording) */
119 static unsigned int pm_clipcount
= 0; /* clipping count */
120 static bool pm_clipcount_active
= false; /* counting or not */
125 static int peek_calls
= 0;
127 #define PEEKS_PER_DRAW_SIZE 40
128 static unsigned int peeks_per_redraw
[PEEKS_PER_DRAW_SIZE
];
130 #define TICKS_PER_DRAW_SIZE 20
131 static unsigned int ticks_per_redraw
[TICKS_PER_DRAW_SIZE
];
134 /* time out values for max */
135 static const short peak_time_out
[] = {
136 0 * HZ
, HZ
/ 5, 30, HZ
/ 2, HZ
, 2 * HZ
,
137 3 * HZ
, 4 * HZ
, 5 * HZ
, 6 * HZ
, 7 * HZ
, 8 * HZ
,
138 9 * HZ
, 10 * HZ
, 15 * HZ
, 20 * HZ
, 30 * HZ
, 60 * HZ
141 /* time out values for clip */
142 static const long clip_time_out
[] = {
143 0 * HZ
, 1 * HZ
, 2 * HZ
, 3 * HZ
, 4 * HZ
, 5 * HZ
,
144 6 * HZ
, 7 * HZ
, 8 * HZ
, 9 * HZ
, 10 * HZ
, 15 * HZ
,
145 20 * HZ
, 25 * HZ
, 30 * HZ
, 45 * HZ
, 60 * HZ
, 90 * HZ
,
146 120 * HZ
, 180 * HZ
, 300 * HZ
, 600L * HZ
, 1200L * HZ
,
147 2700L * HZ
, 5400L * HZ
150 /* precalculated peak values that represent magical
151 dBfs values. Used to draw the scale */
152 static const int db_scale_src_values
[DB_SCALE_SRC_VALUES_SIZE
] = {
167 static int db_scale_count
= DB_SCALE_SRC_VALUES_SIZE
;
170 * Calculates dB Value for the peak meter, uses peak value as input
171 * @param int sample - The input value
172 * Make sure that 0 <= value < SAMPLE_RANGE
174 * @return int - The 2 digit fixed point result of the euation
175 * 20 * log (sample / SAMPLE_RANGE) + 90
176 * Output range is 0-9000 (that is 0.0 - 90.0 dB).
177 * Normally 0dB is full scale, here it is shifted +90dB.
178 * The calculation is based on the results of a linear
179 * approximation tool written specifically for this problem
180 * by Andreas Zwirtes (radhard@gmx.de). The result has an
181 * accurracy of better than 2%. It is highly runtime optimized,
182 * the cascading if-clauses do an successive approximation on
183 * the input value. This avoids big lookup-tables and
185 * Improved by Jvo Studer for errors < 0.2dB for critical
186 * range of -12dB to 0dB (78.0 to 90.0dB).
189 int calc_db (int isample
)
191 /* return n+m*(isample-istart)/100 */
196 if (isample
< 2308) { /* Range 1-5 */
198 if (isample
< 115) { /* Range 1-3 */
203 istart
= 1; /* Range 1 */
208 istart
= 5; /* Range 2 */
214 istart
= 24; /* Range 3 */
219 else { /* Range 4-5 */
222 istart
= 114; /* Range 4 */
227 istart
= 588; /* Range 5 */
234 else { /* Range 6-9 */
236 if (isample
< 12932) {
238 if (isample
< 6394) {
239 istart
= 2608; /* Range 6 */
244 istart
= 7000; /* Range 7 */
251 if (isample
< 22450) {
252 istart
= 13000; /* Range 8 */
257 istart
= 22636; /* Range 9 */
264 return n
+ (m
* (long)(isample
- istart
)) / 100L;
269 * A helper function for peak_meter_db2sample. Don't call it separately but
270 * use peak_meter_db2sample. If one or both of min and max are outside the
271 * range 0 <= min (or max) < 8961 the behaviour of this function is
272 * undefined. It may not return.
273 * @param int min - The minimum of the value range that is searched.
274 * @param int max - The maximum of the value range that is searched.
275 * @param int db - The value in dBfs * (-100) for which the according
276 * minimal peak sample is searched.
277 * @return int - A linear volume value with 0 <= value < MAX_PEAK
279 static int db_to_sample_bin_search(int min
, int max
, int db
)
281 int test
= min
+ (max
- min
) / 2;
284 if (calc_db(test
) < db
) {
285 test
= db_to_sample_bin_search(test
, max
, db
);
287 if (calc_db(test
-1) > db
) {
288 test
= db_to_sample_bin_search(min
, test
, db
);
296 * Converts a value representing dBfs to a linear
297 * scaled volume info as it is used by the MAS.
298 * An incredibly inefficiant function which is
299 * the vague inverse of calc_db. This really
300 * should be replaced by something better soon.
302 * @param int db - A dBfs * 100 value with
304 * @return int - The return value is in the range of
305 * 0 <= return value < MAX_PEAK
307 int peak_meter_db2sample(int db
)
311 /* what is the maximum pseudo db value */
312 int max_peak_db
= calc_db(MAX_PEAK
- 1);
314 /* range check: db value to big */
315 if (max_peak_db
+ db
< 0) {
319 /* range check: db value too small */
320 else if (max_peak_db
+ db
>= max_peak_db
) {
321 retval
= MAX_PEAK
-1;
324 /* value in range: find the matching linear value */
326 retval
= db_to_sample_bin_search(0, MAX_PEAK
, max_peak_db
+ db
);
328 /* as this is a dirty function anyway, we want to adjust the
329 full scale hit manually to avoid users complaining that when
330 they adjust maximum for 0 dBfs and display it in percent it
331 shows 99%. That is due to precision loss and this is the
339 * Set the min value for restriction of the value range.
340 * @param int newmin - depending whether dBfs is used
341 * newmin is a value in dBfs * 100 or in linear percent values.
342 * for dBfs: -9000 < newmin <= 0
343 * for linear: 0 <= newmin <= 100
345 void peak_meter_set_min(int newmin
)
348 peak_meter_range_min
= peak_meter_db2sample(newmin
);
351 if (newmin
< peak_meter_range_max
) {
352 peak_meter_range_min
= newmin
* MAX_PEAK
/ 100;
356 pm_range
= peak_meter_range_max
- peak_meter_range_min
;
358 /* Avoid division by zero. */
363 pm_db_min
= calc_db(peak_meter_range_min
);
364 pm_db_range
= pm_db_max
- pm_db_min
;
367 scales
[i
].db_scale_valid
= false;
371 * Returns the minimum value of the range the meter
372 * displays. If the scale is set to dBfs it returns
373 * dBfs values * 100 or linear percent values.
374 * @return: using dBfs : -9000 < value <= 0
375 * using linear scale: 0 <= value <= 100
377 int peak_meter_get_min(void)
381 retval
= calc_db(peak_meter_range_min
) - calc_db(MAX_PEAK
- 1);
383 retval
= peak_meter_range_min
* 100 / MAX_PEAK
;
389 * Set the max value for restriction of the value range.
390 * @param int newmax - depending wether dBfs is used
391 * newmax is a value in dBfs * 100 or in linear percent values.
392 * for dBfs: -9000 < newmax <= 0
393 * for linear: 0 <= newmax <= 100
395 void peak_meter_set_max(int newmax
)
398 peak_meter_range_max
= peak_meter_db2sample(newmax
);
400 if (newmax
> peak_meter_range_min
) {
401 peak_meter_range_max
= newmax
* MAX_PEAK
/ 100;
405 pm_range
= peak_meter_range_max
- peak_meter_range_min
;
407 /* Avoid division by zero. */
412 pm_db_max
= calc_db(peak_meter_range_max
);
413 pm_db_range
= pm_db_max
- pm_db_min
;
416 scales
[i
].db_scale_valid
= false;
420 * Returns the minimum value of the range the meter
421 * displays. If the scale is set to dBfs it returns
422 * dBfs values * 100 or linear percent values
423 * @return: using dBfs : -9000 < value <= 0
424 * using linear scale: 0 <= value <= 100
426 int peak_meter_get_max(void)
430 retval
= calc_db(peak_meter_range_max
) - calc_db(MAX_PEAK
- 1);
432 retval
= peak_meter_range_max
* 100 / MAX_PEAK
;
438 * Returns whether the meter is currently displaying dBfs or percent values.
439 * @return bool - true if the meter is displaying dBfs
440 false if the meter is displaying percent values.
442 bool peak_meter_get_use_dbfs(void)
448 * Specifies whether the values displayed are scaled
449 * as dBfs or as linear percent values.
450 * @param use - set to true for dBfs,
451 * set to false for linear scaling in percent
453 void peak_meter_set_use_dbfs(bool use
)
458 scales
[i
].db_scale_valid
= false;
462 * Initialize the range of the meter. Only values
463 * that are in the range of [range_min ... range_max]
465 * @param bool dbfs - set to true for dBfs,
466 * set to false for linear scaling in percent
467 * @param int range_min - Specifies the lower value of the range.
468 * Pass a value dBfs * 100 when dbfs is set to true.
469 * Pass a percent value when dbfs is set to false.
470 * @param int range_max - Specifies the upper value of the range.
471 * Pass a value dBfs * 100 when dbfs is set to true.
472 * Pass a percent value when dbfs is set to false.
474 void peak_meter_init_range( bool dbfs
, int range_min
, int range_max
)
477 peak_meter_set_min(range_min
);
478 peak_meter_set_max(range_max
);
482 * Initialize the peak meter with all relevant values concerning times.
483 * @param int release - Set the maximum amount of pixels the meter is allowed
484 * to decrease with each redraw
485 * @param int hold - Select the time preset for the time the peak indicator
486 * is reset after a peak occurred. The preset values are
487 * stored in peak_time_out.
488 * @param int clip_hold - Select the time preset for the time the peak
489 * indicator is reset after a peak occurred. The preset
490 * values are stored in clip_time_out.
492 void peak_meter_init_times(int release
, int hold
, int clip_hold
)
495 pm_peak_release
= release
;
496 pm_clip_hold
= clip_hold
;
499 #ifdef HAVE_RECORDING
501 * Enable/disable clip counting
503 void pm_activate_clipcount(bool active
)
505 pm_clipcount_active
= active
;
509 * Get clipping counter value
511 int pm_get_clipcount(void)
517 * Set clipping counter to zero (typically at start of recording or playback)
519 void pm_reset_clipcount(void)
526 * Set the source of the peak meter to playback or to
528 * @param: bool playback - If true playback peak meter is used.
529 * If false recording peak meter is used.
531 void peak_meter_playback(bool playback
)
535 #elif CONFIG_CODEC == SWCODEC
536 pm_playback
= playback
;
539 pm_src_left
= MAS_REG_DQPEAK_L
;
540 pm_src_right
= MAS_REG_DQPEAK_R
;
542 pm_src_left
= MAS_REG_QPEAK_L
;
543 pm_src_right
= MAS_REG_QPEAK_R
;
548 #ifdef HAVE_RECORDING
549 static void set_trig_status(int new_state
)
551 if (trig_status
!= new_state
) {
552 trig_status
= new_state
;
553 if (trigger_listener
!= NULL
) {
554 trigger_listener(trig_status
);
562 * Reads peak values from the MAS, and detects clips. The
563 * values are stored in pm_max_left pm_max_right for later
564 * evauluation. Consecutive calls to peak_meter_peek detect
565 * that ocurred. This function could be used by a thread for
566 * busy reading the MAS.
568 void peak_meter_peek(void)
571 #ifdef HAVE_RECORDING
572 bool was_clipping
= pm_clip_left
|| pm_clip_right
;
574 /* read current values */
575 #if CONFIG_CODEC == SWCODEC
577 pcm_calculate_peaks(&pm_cur_left
, &pm_cur_right
);
578 #ifdef HAVE_RECORDING
580 pcm_calculate_rec_peaks(&pm_cur_left
, &pm_cur_right
);
583 right
= pm_cur_right
;
586 pm_cur_left
= left
= mas_codec_readreg(pm_src_left
);
587 pm_cur_right
= right
= mas_codec_readreg(pm_src_right
);
589 pm_cur_left
= left
= 8000;
590 pm_cur_right
= right
= 9000;
595 An clip is assumed when two consecutive readouts
596 of the volume are at full scale. This is proven
597 to be inaccurate in both ways: it may detect clips
598 when no clip occurred and it may fail to detect
599 a real clip. For software codecs, the peak is already
600 the max of a bunch of samples, so use one max value
601 or you fail to detect clipping! */
602 #if CONFIG_CODEC == SWCODEC
603 if (left
== MAX_PEAK
- 1) {
605 if ((left
== pm_max_left
) &&
606 (left
== MAX_PEAK
- 1)) {
610 current_tick
+ clip_time_out
[pm_clip_hold
];
613 #if CONFIG_CODEC == SWCODEC
614 if (right
== MAX_PEAK
- 1) {
616 if ((right
== pm_max_right
) &&
617 (right
== MAX_PEAK
- 1)) {
619 pm_clip_right
= true;
621 current_tick
+ clip_time_out
[pm_clip_hold
];
624 #ifdef HAVE_RECORDING
625 if(!was_clipping
&& (pm_clip_left
|| pm_clip_right
))
627 if(pm_clipcount_active
)
632 /* peaks are searched -> we have to find the maximum. When
633 many calls of peak_meter_peek the maximum value will be
634 stored in pm_max_xxx. This maximum is reset by the
635 functions peak_meter_read_x. */
636 pm_max_left
= MAX(pm_max_left
, left
);
637 pm_max_right
= MAX(pm_max_right
, right
);
639 #ifdef HAVE_RECORDING
640 #if CONFIG_CODEC == SWCODEC
641 /* Ignore any unread peakmeter data */
642 #define MAX_DROP_TIME HZ/7 /* this value may need tweaking. Increase if you are
643 getting trig events when you shouldn't with
646 trig_stp_hold
= MAX_DROP_TIME
;
649 switch (trig_status
) {
651 /* no more changes, if trigger was activated as release trigger */
652 /* threshold exceeded? */
653 if ((left
> trig_strt_threshold
)
654 || (right
> trig_strt_threshold
)) {
655 /* reset trigger duration */
656 trig_hightime
= current_tick
;
658 /* reset dropout duration */
659 trig_lowtime
= current_tick
;
661 if (trig_strt_duration
)
662 set_trig_status(TRIG_STEADY
);
664 /* if trig_duration is set to 0 the user wants to start
665 recording immediately */
666 set_trig_status(TRIG_GO
);
672 /* trigger duration exceeded */
673 if (current_tick
- trig_hightime
> trig_strt_duration
) {
674 set_trig_status(TRIG_GO
);
676 /* threshold exceeded? */
677 if ((left
> trig_strt_threshold
)
678 || (right
> trig_strt_threshold
)) {
680 trig_lowtime
= current_tick
;
682 /* volume is below threshold */
684 /* dropout occurred? */
685 if (current_tick
- trig_lowtime
> trig_strt_dropout
){
686 if (trig_status
== TRIG_STEADY
){
687 set_trig_status(TRIG_READY
);
689 /* trig_status == TRIG_RETRIG */
691 /* the gap has already expired */
692 trig_lowtime
= current_tick
- trig_rstrt_gap
- 1;
693 set_trig_status(TRIG_POSTREC
);
702 /* threshold exceeded? */
703 if ((left
> trig_stp_threshold
)
704 || (right
> trig_stp_threshold
)) {
705 /* restart hold time countdown */
706 trig_lowtime
= current_tick
;
707 #if CONFIG_CODEC == SWCODEC
708 } else if (current_tick
- trig_lowtime
> MAX_DROP_TIME
){
712 set_trig_status(TRIG_POSTREC
);
713 trig_hightime
= current_tick
;
718 /* gap time expired? */
719 if (current_tick
- trig_lowtime
> trig_rstrt_gap
){
720 /* start threshold exceeded? */
721 if ((left
> trig_strt_threshold
)
722 || (right
> trig_strt_threshold
)) {
724 set_trig_status(TRIG_RETRIG
);
725 trig_hightime
= current_tick
;
726 trig_lowtime
= current_tick
;
730 /* stop threshold exceeded */
731 if ((left
> trig_stp_threshold
)
732 || (right
> trig_stp_threshold
)) {
733 if (current_tick
- trig_hightime
> trig_stp_hold
){
734 trig_lowtime
= current_tick
;
735 set_trig_status(TRIG_CONTINUE
);
737 trig_lowtime
= current_tick
- trig_rstrt_gap
- 1;
741 /* below any threshold */
743 if (current_tick
- trig_lowtime
> trig_stp_hold
){
744 set_trig_status(TRIG_READY
);
746 trig_hightime
= current_tick
;
751 /* still within the gap time */
753 /* stop threshold exceeded */
754 if ((left
> trig_stp_threshold
)
755 || (right
> trig_stp_threshold
)) {
756 set_trig_status(TRIG_CONTINUE
);
757 trig_lowtime
= current_tick
;
760 /* hold time expired */
761 else if (current_tick
- trig_lowtime
> trig_stp_hold
){
762 trig_hightime
= current_tick
;
763 trig_lowtime
= current_tick
;
764 set_trig_status(TRIG_READY
);
769 #if CONFIG_CODEC == SWCODEC
770 /* restore stop hold value */
771 if (trig_stp_hold
== MAX_DROP_TIME
)
775 /* check levels next time peakmeter drawn */
783 * Reads out the peak volume of the left channel.
784 * @return int - The maximum value that has been detected
785 * since the last call of peak_meter_read_l. The value
786 * is in the range 0 <= value < MAX_PEAK.
788 static int peak_meter_read_l(void)
790 /* pm_max_left contains the maximum of all peak values that were read
791 by peak_meter_peek since the last call of peak_meter_read_l */
792 int retval
= pm_max_left
;
795 /* store max peak value for peak_meter_get_peakhold_x readout */
796 pm_peakhold_left
= MAX(pm_max_left
, pm_peakhold_left
);
801 /* reset pm_max_left so that subsequent calls of peak_meter_peek don't
802 get fooled by an old maximum value */
803 pm_max_left
= pm_cur_left
;
807 retval
= rand()%MAX_PEAK
;
814 * Reads out the peak volume of the right channel.
815 * @return int - The maximum value that has been detected
816 * since the last call of peak_meter_read_l. The value
817 * is in the range 0 <= value < MAX_PEAK.
819 static int peak_meter_read_r(void)
821 /* peak_meter_r contains the maximum of all peak values that were read
822 by peak_meter_peek since the last call of peak_meter_read_r */
823 int retval
= pm_max_right
;
826 /* store max peak value for peak_meter_get_peakhold_x readout */
827 pm_peakhold_right
= MAX(pm_max_right
, pm_peakhold_right
);
832 /* reset pm_max_right so that subsequent calls of peak_meter_peek don't
833 get fooled by an old maximum value */
834 pm_max_right
= pm_cur_right
;
838 retval
= rand()%MAX_PEAK
;
846 * Reads out the current peak-hold values since the last call.
847 * This is used by the histogram feature in the recording screen.
848 * Values are in the range 0 <= peak_x < MAX_PEAK. MAX_PEAK is typ 32767.
850 void peak_meter_get_peakhold(int *peak_left
, int *peak_right
)
853 *peak_left
= pm_peakhold_left
;
855 *peak_right
= pm_peakhold_right
;
856 pm_peakhold_left
= 0;
857 pm_peakhold_right
= 0;
862 * Reset the detected clips. This method is for
863 * use by the user interface.
864 * @param int unused - This parameter was added to
865 * make the function compatible with set_int
867 void peak_meter_set_clip_hold(int time
)
869 pm_clip_eternal
= false;
872 pm_clip_left
= false;
873 pm_clip_right
= false;
874 pm_clip_eternal
= true;
879 * Scales a peak value as read from the MAS to the range of meterwidth.
880 * The scaling is performed according to the scaling method (dBfs / linear)
881 * and the range (peak_meter_range_min .. peak_meter_range_max).
882 * @param unsigned short val - The volume value. Range: 0 <= val < MAX_PEAK
883 * @param int meterwidht - The widht of the meter in pixel
884 * @return unsigned short - A value 0 <= return value <= meterwidth
886 unsigned short peak_meter_scale_value(unsigned short val
, int meterwidth
)
890 if (val
<= peak_meter_range_min
) {
894 if (val
>= peak_meter_range_max
) {
900 /* different scaling is used for dBfs and linear percent */
903 /* scale the samples dBfs */
904 retval
= (calc_db(retval
) - pm_db_min
) * meterwidth
/ pm_db_range
;
907 /* Scale for linear percent display */
910 /* scale the samples */
911 retval
= ((retval
- peak_meter_range_min
) * meterwidth
)
916 void peak_meter_screen(struct screen
*display
, int x
, int y
, int height
)
918 peak_meter_draw(display
, &scales
[display
->screen_type
], x
, y
,
919 display
->width
- x
, height
);
922 * Draws a peak meter in the specified size at the specified position.
923 * @param int x - The x coordinate.
924 * Make sure that 0 <= x and x + width < display->width
925 * @param int y - The y coordinate.
926 * Make sure that 0 <= y and y + height < display->height
927 * @param int width - The width of the peak meter. Note that for display
928 * of clips a 3 pixel wide area is used ->
930 * @param int height - The height of the peak meter. height > 3
932 void peak_meter_draw(struct screen
*display
, struct meter_scales
*scales
,
933 int x
, int y
, int width
, int height
)
935 static int left_level
= 0, right_level
= 0;
936 int left
= 0, right
= 0;
937 int meterwidth
= width
- 3;
939 static long peak_release_tick
= 0;
942 static long pm_tick
= 0;
943 int tmp
= peek_calls
;
946 /* if disabled only draw the peak meter */
947 if (peak_meter_enabled
) {
951 /* only read the volume info from MAS if peek since last read*/
952 left_level
= peak_meter_read_l();
953 right_level
= peak_meter_read_r();
957 /* scale the samples dBfs */
958 left
= peak_meter_scale_value(left_level
, meterwidth
);
959 right
= peak_meter_scale_value(right_level
, meterwidth
);
961 /*if the scale has changed -> recalculate the scale
962 (The scale becomes invalid when the range changed.) */
963 if (!scales
->db_scale_valid
){
966 db_scale_count
= DB_SCALE_SRC_VALUES_SIZE
;
967 for (i
= 0; i
< db_scale_count
; i
++){
968 /* find the real x-coords for predefined interesting
969 dBfs values. These only are recalculated when the
970 scaling of the meter changed. */
971 scales
->db_scale_lcd_coord
[i
] =
972 peak_meter_scale_value(
973 db_scale_src_values
[i
],
978 /* when scaling linear we simly make 10% steps */
981 for (i
= 0; i
< db_scale_count
; i
++) {
982 scales
->db_scale_lcd_coord
[i
] =
983 (i
* (MAX_PEAK
/ 10) - peak_meter_range_min
) *
984 meterwidth
/ pm_range
;
988 /* mark scale valid to avoid recalculating dBfs values
990 scales
->db_scale_valid
= true;
994 delta
= current_tick
- peak_release_tick
;
995 peak_release_tick
= current_tick
;
996 left
= MAX(left
, scales
->last_left
- delta
* pm_peak_release
);
997 right
= MAX(right
, scales
->last_right
- delta
* pm_peak_release
);
999 /* reset max values after timeout */
1000 if (TIME_AFTER(current_tick
, scales
->pm_peak_timeout_l
)){
1001 scales
->pm_peak_left
= 0;
1004 if (TIME_AFTER(current_tick
, scales
->pm_peak_timeout_r
)){
1005 scales
->pm_peak_right
= 0;
1008 if (!pm_clip_eternal
) {
1010 TIME_AFTER(current_tick
, pm_clip_timeout_l
)){
1011 pm_clip_left
= false;
1014 if (pm_clip_right
&&
1015 TIME_AFTER(current_tick
, pm_clip_timeout_r
)){
1016 pm_clip_right
= false;
1020 /* check for new max values */
1021 if (left
> scales
->pm_peak_left
) {
1022 scales
->pm_peak_left
= left
- 1;
1023 scales
->pm_peak_timeout_l
=
1024 current_tick
+ peak_time_out
[pm_peak_hold
];
1027 if (right
> scales
->pm_peak_right
) {
1028 scales
->pm_peak_right
= right
- 1;
1029 scales
->pm_peak_timeout_r
=
1030 current_tick
+ peak_time_out
[pm_peak_hold
];
1034 /* draw the peak meter */
1035 display
->set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
1036 display
->fillrect(x
, y
, width
, height
);
1037 display
->set_drawmode(DRMODE_SOLID
);
1040 display
->fillrect (x
, y
, left
, height
/ 2 - 2 );
1041 if (scales
->pm_peak_left
> 0) {
1042 display
->vline(x
+ scales
->pm_peak_left
, y
, y
+ height
/ 2 - 2 );
1045 display
->fillrect(x
+ meterwidth
, y
, 3, height
/ 2 - 1);
1049 display
->fillrect(x
, y
+ height
/ 2 + 1, right
, height
/ 2 - 2);
1050 if (scales
->pm_peak_right
> 0) {
1051 display
->vline( x
+ scales
->pm_peak_right
, y
+ height
/ 2, y
+ height
- 2);
1053 if (pm_clip_right
) {
1054 display
->fillrect(x
+ meterwidth
, y
+ height
/ 2, 3, height
/ 2 - 1);
1057 /* draw scale end */
1058 display
->vline(x
+ meterwidth
, y
, y
+ height
- 2);
1060 /* draw dots for scale marks */
1061 for (i
= 0; i
< db_scale_count
; i
++) {
1062 /* The x-coordinates of interesting scale mark points
1063 have been calculated before */
1064 display
->drawpixel(x
+ scales
->db_scale_lcd_coord
[i
],
1065 y
+ height
/ 2 - 1);
1068 #ifdef HAVE_RECORDING
1070 #ifdef HAVE_BACKLIGHT
1072 if ((pm_clip_left
|| pm_clip_right
) &&
1073 global_settings
.cliplight
&&
1074 #if CONFIG_CODEC == SWCODEC
1077 !(audio_status() & (AUDIO_STATUS_PLAY
| AUDIO_STATUS_ERROR
)))
1080 /* if clipping, cliplight setting on and in recording screen */
1081 if (global_settings
.cliplight
<= 2)
1083 /* turn on main unit light if setting set to main or both*/
1086 #ifdef HAVE_REMOTE_LCD
1087 if (global_settings
.cliplight
>= 2)
1089 /* turn remote light unit on if setting set to remote or both */
1090 remote_backlight_on();
1092 #endif /* HAVE_REMOTE_LCD */
1094 #endif /* HAVE_BACKLIGHT */
1096 if (trig_status
!= TRIG_OFF
) {
1097 int start_trigx
, stop_trigx
, ycenter
;
1099 display
->set_drawmode(DRMODE_SOLID
);
1100 ycenter
= y
+ height
/ 2;
1101 /* display threshold value */
1102 start_trigx
= x
+peak_meter_scale_value(trig_strt_threshold
,meterwidth
);
1103 display
->vline(start_trigx
, ycenter
- 2, ycenter
);
1105 if (start_trigx
< display
->width
) display
->drawpixel(start_trigx
, ycenter
- 1);
1107 stop_trigx
= x
+ peak_meter_scale_value(trig_stp_threshold
,meterwidth
);
1108 display
->vline(stop_trigx
, ycenter
- 2, ycenter
);
1109 if (stop_trigx
> 0) display
->drawpixel(stop_trigx
- 1, ycenter
- 1);
1111 #endif /*HAVE_RECORDING*/
1114 /* display a bar to show how many calls to peak_meter_peek
1115 have ocurred since the last display */
1116 display
->set_drawmode(DRMODE_COMPLEMENT
);
1117 display
->fillrect(x
, y
, tmp
, 3);
1119 if (tmp
< PEEKS_PER_DRAW_SIZE
) {
1120 peeks_per_redraw
[tmp
]++;
1123 tmp
= current_tick
- pm_tick
;
1124 if (tmp
< TICKS_PER_DRAW_SIZE
){
1125 ticks_per_redraw
[tmp
] ++;
1128 /* display a bar to show how many ticks have passed since
1130 display
->fillrect(x
, y
+ height
/ 2, current_tick
- pm_tick
, 2);
1131 pm_tick
= current_tick
;
1134 scales
->last_left
= left
;
1135 scales
->last_right
= right
;
1137 display
->set_drawmode(DRMODE_SOLID
);
1140 #ifdef HAVE_RECORDING
1142 * Defines the parameters of the trigger. After these parameters are defined
1143 * the trigger can be started either by peak_meter_attack_trigger or by
1144 * peak_meter_release_trigger. Note that you can pass either linear (%) or
1145 * logarithmic (db) values to the thresholds. Positive values are intepreted as
1146 * percent (0 is 0% .. 100 is 100%). Negative values are interpreted as db.
1147 * To avoid ambiguosity of the value 0 the negative values are shifted by -1.
1148 * Thus -75 is -74db .. -1 is 0db.
1149 * @param start_threshold - The threshold used for attack trigger. Negative
1150 * values are interpreted as db -1, positive as %.
1151 * @param start_duration - The minimum time span within which start_threshold
1152 * must be exceeded to fire the attack trigger.
1153 * @param start_dropout - The maximum time span the level may fall below
1154 * start_threshold without releasing the attack trigger.
1155 * @param stop_threshold - The threshold the volume must fall below to release
1156 * the release trigger.Negative values are
1157 * interpreted as db -1, positive as %.
1158 * @param stop_hold - The minimum time the volume must fall below the
1159 * stop_threshold to release the trigger.
1162 void peak_meter_define_trigger(
1163 int start_threshold
,
1164 long start_duration
,
1167 long stop_hold_time
,
1171 if (start_threshold
< 0) {
1173 if (start_threshold
< -89) {
1174 trig_strt_threshold
= 0;
1176 trig_strt_threshold
=peak_meter_db2sample((start_threshold
+1)*100);
1179 /* linear percent */
1180 trig_strt_threshold
= start_threshold
* MAX_PEAK
/ 100;
1182 trig_strt_duration
= start_duration
;
1183 trig_strt_dropout
= start_dropout
;
1184 if (stop_threshold
< 0) {
1186 trig_stp_threshold
= peak_meter_db2sample((stop_threshold
+ 1) * 100);
1188 /* linear percent */
1189 trig_stp_threshold
= stop_threshold
* MAX_PEAK
/ 100;
1191 trig_stp_hold
= stop_hold_time
;
1192 trig_rstrt_gap
= restart_gap
;
1196 * Enables or disables the trigger.
1197 * @param on - If true the trigger is turned on.
1199 void peak_meter_trigger(bool on
)
1201 /* don't use set_trigger here as that would fire an undesired event */
1202 trig_status
= on
? TRIG_READY
: TRIG_OFF
;
1206 * Registers the listener function that listenes on trig_status changes.
1207 * @param listener - The function that is called with each change of
1208 * trig_status. May be set to NULL if no callback is desired.
1210 void peak_meter_set_trigger_listener(void (*listener
)(int status
))
1212 trigger_listener
= listener
;
1216 * Fetches the status of the trigger.
1217 * TRIG_OFF: the trigger is inactive
1218 * TRIG_RELEASED: The volume level is below the threshold
1219 * TRIG_ACTIVATED: The volume level has exceeded the threshold, but the trigger
1220 * hasn't been fired yet.
1221 * TRIG_FIRED: The volume exceeds the threshold
1223 * To activate the trigger call either peak_meter_attack_trigger or
1224 * peak_meter_release_trigger. To turn the trigger off call
1225 * peak_meter_trigger_off.
1227 int peak_meter_trigger_status(void)
1229 return trig_status
; /* & TRIG_PIT_MASK;*/
1232 void peak_meter_draw_trig(int xpos
[], int ypos
[], int trig_width
[], int nb_screens
)
1234 int barstart
[NB_SCREENS
];
1235 int barend
[NB_SCREENS
];
1237 int ixpos
[NB_SCREENS
];
1239 int trigbar_width
[NB_SCREENS
];
1242 trigbar_width
[i
] = (trig_width
[i
] - (2 * (ICON_PLAY_STATE_WIDTH
+ 1)));
1244 switch (trig_status
) {
1261 barend
[i
] = (trig_strt_duration
== 0) ? trigbar_width
[i
] :
1263 (current_tick
- trig_hightime
) / trig_strt_duration
;
1274 barstart
[i
] = trigbar_width
[i
];
1275 barend
[i
] = trigbar_width
[i
];
1279 ixpos
[i
] = xpos
[i
]+ trig_width
[i
] - ICON_PLAY_STATE_WIDTH
;
1285 barstart
[i
] = (trig_stp_hold
== 0) ? 0 :
1286 trigbar_width
[i
] - trigbar_width
[i
] *
1287 (current_tick
- trig_lowtime
) / trig_stp_hold
;
1288 barend
[i
] = trigbar_width
[i
];
1292 ixpos
[i
] = xpos
[i
] + trig_width
[i
] - ICON_PLAY_STATE_WIDTH
;
1299 for(i
= 0; i
< nb_screens
; i
++)
1301 gui_scrollbar_draw(&screens
[i
], xpos
[i
] + ICON_PLAY_STATE_WIDTH
+ 1,
1302 ypos
[i
] + 1, trigbar_width
[i
], TRIG_HEIGHT
- 2,
1303 trigbar_width
[i
], barstart
[i
], barend
[i
],
1306 screens
[i
].mono_bitmap(bitmap_icons_7x8
[icon
], ixpos
[i
], ypos
[i
],
1307 ICON_PLAY_STATE_WIDTH
, STATUSBAR_HEIGHT
);
1312 int peak_meter_draw_get_btn(int x
, int y
[], int height
, int nb_screens
)
1314 int button
= BUTTON_NONE
;
1315 long next_refresh
= current_tick
;
1316 long next_big_refresh
= current_tick
+ HZ
/ 10;
1319 bool highperf
= !ata_disk_is_active();
1321 bool highperf
= false;
1325 while (TIME_BEFORE(current_tick
, next_big_refresh
)) {
1326 button
= get_action(CONTEXT_RECSCREEN
, TIMEOUT_NOBLOCK
);
1327 if (button
!= BUTTON_NONE
) {
1330 if (dopeek
) { /* Peek only once per refresh when disk is */
1331 peak_meter_peek(); /* spinning, but as often as possible */
1332 dopeek
= highperf
; /* otherwise. */
1335 sleep(0); /* Sleep until end of current tick. */
1337 if (TIME_AFTER(current_tick
, next_refresh
)) {
1338 for(i
= 0; i
< nb_screens
; i
++)
1340 peak_meter_screen(&screens
[i
], x
, y
[i
], height
);
1341 screens
[i
].update_rect(x
, y
[i
], screens
[i
].width
- x
, height
);
1343 next_refresh
+= HZ
/ PEAK_METER_FPS
;
1352 static void peak_meter_clear_histogram(void)
1355 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1356 ticks_per_redraw
[i
] = (unsigned int)0;
1359 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1360 peeks_per_redraw
[i
] = (unsigned int)0;
1364 bool peak_meter_histogram(void)
1367 int btn
= BUTTON_NONE
;
1368 while ((btn
& BUTTON_OFF
) != BUTTON_OFF
)
1370 unsigned int max
= 0;
1373 screens
[0].clear_display();
1375 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1376 max
= MAX(max
, peeks_per_redraw
[i
]);
1379 for (i
= 0; i
< PEEKS_PER_DRAW_SIZE
; i
++) {
1380 x
= peeks_per_redraw
[i
] * (LCD_WIDTH
- 1)/ max
;
1381 screens
[0].hline(0, x
, y
+ i
);
1384 y
= PEEKS_PER_DRAW_SIZE
+ 1;
1387 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1388 max
= MAX(max
, ticks_per_redraw
[i
]);
1391 for (i
= 0; i
< TICKS_PER_DRAW_SIZE
; i
++) {
1392 x
= ticks_per_redraw
[i
] * (LCD_WIDTH
- 1)/ max
;
1393 screens
[0].hline(0, x
, y
+ i
);
1395 screens
[0].update();
1397 btn
= button_get(true);
1398 if (btn
== BUTTON_PLAY
) {
1399 peak_meter_clear_histogram();