blob: fc68ce4267f62d8601971791d03412aba4dca1c7 [file] [log] [blame]
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 by Philipp Pertermann
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
Peter D'Hoye583f1862007-08-25 15:53:54 +000021#ifdef SIMULATOR
22#include <stdlib.h> /* sim uses rand for peakmeter simulation */
23#endif
Hardeep Sidhuc74a28f2003-01-16 20:25:49 +000024#include "config.h"
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +000025#include "mas.h"
26#include "thread.h"
27#include "kernel.h"
28#include "settings.h"
Frank Gevaerts2f8a0082008-11-01 16:14:28 +000029#include "storage.h"
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +000030#include "lcd.h"
Martin Scarrattd8103f32006-11-09 12:27:56 +000031#include "scrollbar.h"
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +000032#include "sprintf.h"
33#include "button.h"
34#include "system.h"
35#include "font.h"
36#include "icons.h"
Robert Hak2eda5382002-10-03 09:19:11 +000037#include "lang.h"
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +000038#include "peakmeter.h"
Peter D'Hoye3467ba62006-02-17 22:47:56 +000039#include "audio.h"
Dan Evertonb66477a2006-03-25 13:35:31 +000040#include "screen_access.h"
Jens Arnold47bf6c52007-04-12 22:12:13 +000041#ifdef HAVE_BACKLIGHT
Peter D'Hoye3467ba62006-02-17 22:47:56 +000042#include "backlight.h"
43#endif
Martin Scarrattc8bd9122006-08-20 21:33:40 +000044#include "action.h"
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +000045
Jens Arnoldd6c05452005-08-29 21:15:27 +000046#if CONFIG_CODEC == SWCODEC
Michael Sevakis6077e5b2007-10-06 22:27:27 +000047#include "pcm.h"
Andy1e4d1f82005-12-02 01:04:03 +000048
Peter D'Hoye496e1f72008-08-06 20:12:44 +000049#ifdef HAVE_RECORDING
Andy1e4d1f82005-12-02 01:04:03 +000050#include "pcm_record.h"
51#endif
Daniel Stenberg727eab12005-12-02 08:44:09 +000052
Andy1e4d1f82005-12-02 01:04:03 +000053static bool pm_playback = true; /* selects between playback and recording peaks */
Daniel Stenberg727eab12005-12-02 08:44:09 +000054#endif
55
Jens Arnold2597a132006-12-25 14:01:47 +000056static struct meter_scales scales[NB_SCREENS];
Dan Evertonb66477a2006-03-25 13:35:31 +000057
Jens Arnoldd6c05452005-08-29 21:15:27 +000058#if !defined(SIMULATOR) && CONFIG_CODEC != SWCODEC
Jens Arnold99a05982005-08-29 20:07:17 +000059/* Data source */
60static int pm_src_left = MAS_REG_DQPEAK_L;
61static int pm_src_right = MAS_REG_DQPEAK_R;
Felix Arendsaa8adb22003-01-30 15:52:47 +000062#endif
63
Jens Arnold99a05982005-08-29 20:07:17 +000064/* Current values and cumulation */
65static int pm_cur_left; /* current values (last peak_meter_peek) */
66static int pm_cur_right;
67static int pm_max_left; /* maximum values between peak meter draws */
68static int pm_max_right;
Peter D'Hoye5fc66e52006-08-16 23:26:55 +000069#ifdef HAVE_AGC
70static int pm_peakhold_left; /* max. peak values between peakhold calls */
71static int pm_peakhold_right; /* used for AGC and histogram display */
72#endif
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +000073
Jens Arnold99a05982005-08-29 20:07:17 +000074/* Clip hold */
75static bool pm_clip_left = false; /* when true a clip has occurred */
76static bool pm_clip_right = false;
77static long pm_clip_timeout_l; /* clip hold timeouts */
78static long pm_clip_timeout_r;
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +000079
Jens Arnold99a05982005-08-29 20:07:17 +000080/* Temporarily en- / disables peak meter. This is especially for external
81 applications to detect if the peak_meter is in use and needs drawing at all */
82bool peak_meter_enabled = true;
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +000083
Jens Arnold99a05982005-08-29 20:07:17 +000084/** Parameters **/
85/* Range */
Bertrik Sikkendb2d61f2008-04-20 10:24:15 +000086static unsigned short peak_meter_range_min; /* minimum of range in samples */
87static unsigned short peak_meter_range_max; /* maximum of range in samples */
Jens Arnold99a05982005-08-29 20:07:17 +000088static unsigned short pm_range; /* range width in samples */
89static bool pm_use_dbfs = true; /* true if peakmeter displays dBfs */
Jens Arnold2597a132006-12-25 14:01:47 +000090static bool level_check; /* true if peeked at peakmeter before drawing */
Jens Arnold99a05982005-08-29 20:07:17 +000091static unsigned short pm_db_min = 0; /* minimum of range in 1/100 dB */
92static unsigned short pm_db_max = 9000; /* maximum of range in 1/100 dB */
93static unsigned short pm_db_range = 9000; /* range width in 1/100 dB */
94/* Timing behaviour */
95static int pm_peak_hold = 1; /* peak hold timeout index */
96static int pm_peak_release = 8; /* peak release in units per read */
97static int pm_clip_hold = 16; /* clip hold timeout index */
98static bool pm_clip_eternal = false; /* true if clip timeout is disabled */
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +000099
Jens Arnoldcf986e82005-04-04 20:16:01 +0000100#ifdef HAVE_RECORDING
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +0000101static unsigned short trig_strt_threshold;
102static long trig_strt_duration;
103static long trig_strt_dropout;
104static unsigned short trig_stp_threshold;
105static long trig_stp_hold;
106static long trig_rstrt_gap;
107
108/* point in time when the threshold was exceeded */
109static long trig_hightime;
110
111/* point in time when the volume fell below the threshold*/
112static long trig_lowtime;
113
Jens Arnoldcf986e82005-04-04 20:16:01 +0000114/* The output value of the trigger. See TRIG_XXX constants for valid values */
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +0000115static int trig_status = TRIG_OFF;
116
117static void (*trigger_listener)(int) = NULL;
Peter D'Hoye6109d392007-08-26 21:34:36 +0000118
119/* clipping counter (only used for recording) */
120static unsigned int pm_clipcount = 0; /* clipping count */
121static bool pm_clipcount_active = false; /* counting or not */
Jens Arnoldcf986e82005-04-04 20:16:01 +0000122#endif
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +0000123
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000124/* debug only */
125#ifdef PM_DEBUG
126static int peek_calls = 0;
127
128#define PEEKS_PER_DRAW_SIZE 40
129static unsigned int peeks_per_redraw[PEEKS_PER_DRAW_SIZE];
130
131#define TICKS_PER_DRAW_SIZE 20
132static unsigned int ticks_per_redraw[TICKS_PER_DRAW_SIZE];
133#endif
134
Nils Wallméniusc70d9462007-09-20 16:44:28 +0000135static void peak_meter_draw(struct screen *display, struct meter_scales *meter_scales,
136 int x, int y, int width, int height);
137
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000138/* time out values for max */
Jens Arnold99a05982005-08-29 20:07:17 +0000139static const short peak_time_out[] = {
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000140 0 * HZ, HZ / 5, 30, HZ / 2, HZ, 2 * HZ,
141 3 * HZ, 4 * HZ, 5 * HZ, 6 * HZ, 7 * HZ, 8 * HZ,
142 9 * HZ, 10 * HZ, 15 * HZ, 20 * HZ, 30 * HZ, 60 * HZ
143};
144
145/* time out values for clip */
Jens Arnold839067b2004-08-01 23:34:44 +0000146static const long clip_time_out[] = {
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000147 0 * HZ, 1 * HZ, 2 * HZ, 3 * HZ, 4 * HZ, 5 * HZ,
148 6 * HZ, 7 * HZ, 8 * HZ, 9 * HZ, 10 * HZ, 15 * HZ,
149 20 * HZ, 25 * HZ, 30 * HZ, 45 * HZ, 60 * HZ, 90 * HZ,
Jean-Philippe Bernardy4ff0c8e2005-02-16 11:04:35 +0000150 120 * HZ, 180 * HZ, 300 * HZ, 600L * HZ, 1200L * HZ,
151 2700L * HZ, 5400L * HZ
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000152};
153
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000154/* precalculated peak values that represent magical
155 dBfs values. Used to draw the scale */
Nils Wallméniusc70d9462007-09-20 16:44:28 +0000156static const short db_scale_src_values[DB_SCALE_SRC_VALUES_SIZE] = {
Peter D'Hoye96d269e2006-07-02 22:36:46 +0000157 32736, /* 0 db */
158 22752, /* - 3 db */
159 16640, /* - 6 db */
160 11648, /* - 9 db */
161 8320, /* -12 db */
162 4364, /* -18 db */
163 2064, /* -24 db */
164 1194, /* -30 db */
165 363, /* -40 db */
166 101, /* -50 db */
167 34, /* -60 db */
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +0000168 0, /* -inf */
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000169};
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000170
Linus Nielsen Feltzinga02ffd52002-10-30 23:01:27 +0000171static int db_scale_count = DB_SCALE_SRC_VALUES_SIZE;
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000172
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000173/**
174 * Calculates dB Value for the peak meter, uses peak value as input
175 * @param int sample - The input value
176 * Make sure that 0 <= value < SAMPLE_RANGE
177 *
Peter D'Hoye96d269e2006-07-02 22:36:46 +0000178 * @return int - The 2 digit fixed point result of the euation
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000179 * 20 * log (sample / SAMPLE_RANGE) + 90
Peter D'Hoye96d269e2006-07-02 22:36:46 +0000180 * Output range is 0-9000 (that is 0.0 - 90.0 dB).
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000181 * Normally 0dB is full scale, here it is shifted +90dB.
182 * The calculation is based on the results of a linear
183 * approximation tool written specifically for this problem
Peter D'Hoye96d269e2006-07-02 22:36:46 +0000184 * by Andreas Zwirtes (radhard@gmx.de). The result has an
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000185 * accurracy of better than 2%. It is highly runtime optimized,
186 * the cascading if-clauses do an successive approximation on
187 * the input value. This avoids big lookup-tables and
188 * for-loops.
Peter D'Hoye96d269e2006-07-02 22:36:46 +0000189 * Improved by Jvo Studer for errors < 0.2dB for critical
190 * range of -12dB to 0dB (78.0 to 90.0dB).
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000191 */
192
Jens Arnold99a05982005-08-29 20:07:17 +0000193int calc_db (int isample)
194{
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000195 /* return n+m*(isample-istart)/100 */
196 int n;
197 long m;
198 int istart;
199
Peter D'Hoye96d269e2006-07-02 22:36:46 +0000200 if (isample < 2308) { /* Range 1-5 */
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000201
Peter D'Hoye96d269e2006-07-02 22:36:46 +0000202 if (isample < 115) { /* Range 1-3 */
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000203
Peter D'Hoye96d269e2006-07-02 22:36:46 +0000204 if (isample < 24) {
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000205
Peter D'Hoye96d269e2006-07-02 22:36:46 +0000206 if (isample < 5) {
207 istart = 1; /* Range 1 */
208 n = 98;
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000209 m = 34950;
210 }
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000211 else {
Peter D'Hoye96d269e2006-07-02 22:36:46 +0000212 istart = 5; /* Range 2 */
213 n = 1496;
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000214 m = 7168;
215 }
Peter D'Hoye96d269e2006-07-02 22:36:46 +0000216 }
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000217 else {
Peter D'Hoye96d269e2006-07-02 22:36:46 +0000218 istart = 24; /* Range 3 */
219 n = 2858;
220 m = 1498;
221 }
222 }
223 else { /* Range 4-5 */
224
225 if (isample < 534) {
226 istart = 114; /* Range 4 */
227 n = 4207;
228 m = 319;
229 }
230 else {
231 istart = 588; /* Range 5 */
232 n = 5583;
233 m = 69;
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000234 }
235 }
236 }
237
Peter D'Hoye96d269e2006-07-02 22:36:46 +0000238 else { /* Range 6-9 */
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000239
Peter D'Hoye96d269e2006-07-02 22:36:46 +0000240 if (isample < 12932) {
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000241
Peter D'Hoye96d269e2006-07-02 22:36:46 +0000242 if (isample < 6394) {
243 istart = 2608; /* Range 6 */
244 n = 6832;
245 m = 21;
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000246 }
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000247 else {
Peter D'Hoye96d269e2006-07-02 22:36:46 +0000248 istart = 7000; /* Range 7 */
249 n = 7682;
250 m = 9;
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000251 }
252 }
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000253 else {
254
Peter D'Hoye96d269e2006-07-02 22:36:46 +0000255 if (isample < 22450) {
256 istart = 13000; /* Range 8 */
257 n = 8219;
258 m = 5;
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000259 }
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000260 else {
Peter D'Hoye96d269e2006-07-02 22:36:46 +0000261 istart = 22636; /* Range 9 */
262 n = 8697;
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000263 m = 3;
264 }
265 }
266 }
Peter D'Hoye496e1f72008-08-06 20:12:44 +0000267
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000268 return n + (m * (long)(isample - istart)) / 100L;
269}
270
271
272/**
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +0000273 * A helper function for peak_meter_db2sample. Don't call it separately but
274 * use peak_meter_db2sample. If one or both of min and max are outside the
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000275 * range 0 <= min (or max) < 8961 the behaviour of this function is
276 * undefined. It may not return.
277 * @param int min - The minimum of the value range that is searched.
278 * @param int max - The maximum of the value range that is searched.
279 * @param int db - The value in dBfs * (-100) for which the according
280 * minimal peak sample is searched.
281 * @return int - A linear volume value with 0 <= value < MAX_PEAK
282 */
Jens Arnold99a05982005-08-29 20:07:17 +0000283static int db_to_sample_bin_search(int min, int max, int db)
284{
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000285 int test = min + (max - min) / 2;
286
287 if (min < max) {
288 if (calc_db(test) < db) {
289 test = db_to_sample_bin_search(test, max, db);
290 } else {
291 if (calc_db(test-1) > db) {
292 test = db_to_sample_bin_search(min, test, db);
293 }
294 }
295 }
296 return test;
297}
298
299/**
300 * Converts a value representing dBfs to a linear
301 * scaled volume info as it is used by the MAS.
302 * An incredibly inefficiant function which is
303 * the vague inverse of calc_db. This really
304 * should be replaced by something better soon.
305 *
306 * @param int db - A dBfs * 100 value with
307 * -9000 < value <= 0
308 * @return int - The return value is in the range of
309 * 0 <= return value < MAX_PEAK
310 */
Jens Arnold2597a132006-12-25 14:01:47 +0000311int peak_meter_db2sample(int db)
Jens Arnold99a05982005-08-29 20:07:17 +0000312{
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000313 int retval = 0;
314
315 /* what is the maximum pseudo db value */
316 int max_peak_db = calc_db(MAX_PEAK - 1);
317
318 /* range check: db value to big */
319 if (max_peak_db + db < 0) {
320 retval = 0;
Peter D'Hoye496e1f72008-08-06 20:12:44 +0000321 }
322
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000323 /* range check: db value too small */
324 else if (max_peak_db + db >= max_peak_db) {
325 retval = MAX_PEAK -1;
Peter D'Hoye496e1f72008-08-06 20:12:44 +0000326 }
327
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000328 /* value in range: find the matching linear value */
329 else {
330 retval = db_to_sample_bin_search(0, MAX_PEAK, max_peak_db + db);
331
332 /* as this is a dirty function anyway, we want to adjust the
333 full scale hit manually to avoid users complaining that when
334 they adjust maximum for 0 dBfs and display it in percent it
335 shows 99%. That is due to precision loss and this is the
336 optical fix */
337 }
338
339 return retval;
340}
341
342/**
343 * Set the min value for restriction of the value range.
Jens Arnold99a05982005-08-29 20:07:17 +0000344 * @param int newmin - depending whether dBfs is used
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000345 * newmin is a value in dBfs * 100 or in linear percent values.
346 * for dBfs: -9000 < newmin <= 0
347 * for linear: 0 <= newmin <= 100
348 */
Nils Wallméniusc70d9462007-09-20 16:44:28 +0000349static void peak_meter_set_min(int newmin)
Jens Arnold99a05982005-08-29 20:07:17 +0000350{
351 if (pm_use_dbfs) {
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +0000352 peak_meter_range_min = peak_meter_db2sample(newmin);
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000353
354 } else {
355 if (newmin < peak_meter_range_max) {
356 peak_meter_range_min = newmin * MAX_PEAK / 100;
357 }
358 }
Linus Nielsen Feltzinga02ffd52002-10-30 23:01:27 +0000359
Jens Arnold99a05982005-08-29 20:07:17 +0000360 pm_range = peak_meter_range_max - peak_meter_range_min;
Peter D'Hoye496e1f72008-08-06 20:12:44 +0000361
Magnus Holmgren0b35bcf2006-08-17 09:35:27 +0000362 /* Avoid division by zero. */
363 if (pm_range == 0) {
364 pm_range = 1;
365 }
Linus Nielsen Feltzinga02ffd52002-10-30 23:01:27 +0000366
Jens Arnold99a05982005-08-29 20:07:17 +0000367 pm_db_min = calc_db(peak_meter_range_min);
368 pm_db_range = pm_db_max - pm_db_min;
Dan Evertonb66477a2006-03-25 13:35:31 +0000369 int i;
370 FOR_NB_SCREENS(i)
371 scales[i].db_scale_valid = false;
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000372}
373
374/**
375 * Returns the minimum value of the range the meter
376 * displays. If the scale is set to dBfs it returns
377 * dBfs values * 100 or linear percent values.
378 * @return: using dBfs : -9000 < value <= 0
379 * using linear scale: 0 <= value <= 100
380 */
Jens Arnold99a05982005-08-29 20:07:17 +0000381int peak_meter_get_min(void)
382{
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000383 int retval = 0;
Jens Arnold99a05982005-08-29 20:07:17 +0000384 if (pm_use_dbfs) {
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000385 retval = calc_db(peak_meter_range_min) - calc_db(MAX_PEAK - 1);
386 } else {
387 retval = peak_meter_range_min * 100 / MAX_PEAK;
388 }
389 return retval;
390}
391
392/**
393 * Set the max value for restriction of the value range.
394 * @param int newmax - depending wether dBfs is used
395 * newmax is a value in dBfs * 100 or in linear percent values.
396 * for dBfs: -9000 < newmax <= 0
397 * for linear: 0 <= newmax <= 100
398 */
Nils Wallméniusc70d9462007-09-20 16:44:28 +0000399static void peak_meter_set_max(int newmax)
Jens Arnold99a05982005-08-29 20:07:17 +0000400{
401 if (pm_use_dbfs) {
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +0000402 peak_meter_range_max = peak_meter_db2sample(newmax);
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000403 } else {
404 if (newmax > peak_meter_range_min) {
405 peak_meter_range_max = newmax * MAX_PEAK / 100;
406 }
407 }
Linus Nielsen Feltzinga02ffd52002-10-30 23:01:27 +0000408
Jens Arnold99a05982005-08-29 20:07:17 +0000409 pm_range = peak_meter_range_max - peak_meter_range_min;
Linus Nielsen Feltzinga02ffd52002-10-30 23:01:27 +0000410
Magnus Holmgren0b35bcf2006-08-17 09:35:27 +0000411 /* Avoid division by zero. */
412 if (pm_range == 0) {
413 pm_range = 1;
414 }
415
Jens Arnold99a05982005-08-29 20:07:17 +0000416 pm_db_max = calc_db(peak_meter_range_max);
417 pm_db_range = pm_db_max - pm_db_min;
Dan Evertonb66477a2006-03-25 13:35:31 +0000418 int i;
419 FOR_NB_SCREENS(i)
420 scales[i].db_scale_valid = false;
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000421}
422
423/**
424 * Returns the minimum value of the range the meter
425 * displays. If the scale is set to dBfs it returns
426 * dBfs values * 100 or linear percent values
427 * @return: using dBfs : -9000 < value <= 0
428 * using linear scale: 0 <= value <= 100
429 */
Jens Arnold99a05982005-08-29 20:07:17 +0000430int peak_meter_get_max(void)
431{
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000432 int retval = 0;
Jens Arnold99a05982005-08-29 20:07:17 +0000433 if (pm_use_dbfs) {
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000434 retval = calc_db(peak_meter_range_max) - calc_db(MAX_PEAK - 1);
435 } else {
436 retval = peak_meter_range_max * 100 / MAX_PEAK;
437 }
438 return retval;
439}
440
441/**
Jens Arnold99a05982005-08-29 20:07:17 +0000442 * Returns whether the meter is currently displaying dBfs or percent values.
443 * @return bool - true if the meter is displaying dBfs
444 false if the meter is displaying percent values.
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000445 */
Jens Arnold99a05982005-08-29 20:07:17 +0000446bool peak_meter_get_use_dbfs(void)
447{
448 return pm_use_dbfs;
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000449}
450
451/**
Jens Arnold99a05982005-08-29 20:07:17 +0000452 * Specifies whether the values displayed are scaled
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000453 * as dBfs or as linear percent values.
Jens Arnold99a05982005-08-29 20:07:17 +0000454 * @param use - set to true for dBfs,
455 * set to false for linear scaling in percent
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000456 */
Jens Arnold99a05982005-08-29 20:07:17 +0000457void peak_meter_set_use_dbfs(bool use)
458{
Dan Evertonb66477a2006-03-25 13:35:31 +0000459 int i;
Jens Arnold99a05982005-08-29 20:07:17 +0000460 pm_use_dbfs = use;
Dan Evertonb66477a2006-03-25 13:35:31 +0000461 FOR_NB_SCREENS(i)
462 scales[i].db_scale_valid = false;
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000463}
464
465/**
466 * Initialize the range of the meter. Only values
467 * that are in the range of [range_min ... range_max]
468 * are displayed.
469 * @param bool dbfs - set to true for dBfs,
470 * set to false for linear scaling in percent
471 * @param int range_min - Specifies the lower value of the range.
472 * Pass a value dBfs * 100 when dbfs is set to true.
473 * Pass a percent value when dbfs is set to false.
474 * @param int range_max - Specifies the upper value of the range.
475 * Pass a value dBfs * 100 when dbfs is set to true.
476 * Pass a percent value when dbfs is set to false.
477 */
Daniel Stenbergdb32fb12005-02-02 21:49:19 +0000478void peak_meter_init_range( bool dbfs, int range_min, int range_max)
479{
Jens Arnold99a05982005-08-29 20:07:17 +0000480 pm_use_dbfs = dbfs;
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000481 peak_meter_set_min(range_min);
482 peak_meter_set_max(range_max);
483}
484
485/**
486 * Initialize the peak meter with all relevant values concerning times.
487 * @param int release - Set the maximum amount of pixels the meter is allowed
488 * to decrease with each redraw
489 * @param int hold - Select the time preset for the time the peak indicator
490 * is reset after a peak occurred. The preset values are
Jens Arnold99a05982005-08-29 20:07:17 +0000491 * stored in peak_time_out.
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000492 * @param int clip_hold - Select the time preset for the time the peak
493 * indicator is reset after a peak occurred. The preset
494 * values are stored in clip_time_out.
495 */
Jens Arnold99a05982005-08-29 20:07:17 +0000496void peak_meter_init_times(int release, int hold, int clip_hold)
497{
498 pm_peak_hold = hold;
499 pm_peak_release = release;
500 pm_clip_hold = clip_hold;
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000501}
502
Peter D'Hoye6109d392007-08-26 21:34:36 +0000503#ifdef HAVE_RECORDING
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000504/**
Peter D'Hoye583f1862007-08-25 15:53:54 +0000505 * Enable/disable clip counting
506 */
507void pm_activate_clipcount(bool active)
508{
509 pm_clipcount_active = active;
510}
511
512/**
513 * Get clipping counter value
514 */
515int pm_get_clipcount(void)
516{
517 return pm_clipcount;
518}
519
520/**
521 * Set clipping counter to zero (typically at start of recording or playback)
522 */
523void pm_reset_clipcount(void)
524{
525 pm_clipcount = 0;
526}
Peter D'Hoye6109d392007-08-26 21:34:36 +0000527#endif
Peter D'Hoye583f1862007-08-25 15:53:54 +0000528
529/**
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000530 * Set the source of the peak meter to playback or to
531 * record.
532 * @param: bool playback - If true playback peak meter is used.
533 * If false recording peak meter is used.
534 */
Daniel Stenbergdb32fb12005-02-02 21:49:19 +0000535void peak_meter_playback(bool playback)
536{
Peter D'Hoye05cb4212008-06-29 08:05:00 +0000537 int i;
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000538#ifdef SIMULATOR
539 (void)playback;
Jens Arnoldd6c05452005-08-29 21:15:27 +0000540#elif CONFIG_CODEC == SWCODEC
Andy1e4d1f82005-12-02 01:04:03 +0000541 pm_playback = playback;
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000542#else
543 if (playback) {
Jens Arnold99a05982005-08-29 20:07:17 +0000544 pm_src_left = MAS_REG_DQPEAK_L;
545 pm_src_right = MAS_REG_DQPEAK_R;
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000546 } else {
Jens Arnold99a05982005-08-29 20:07:17 +0000547 pm_src_left = MAS_REG_QPEAK_L;
548 pm_src_right = MAS_REG_QPEAK_R;
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000549 }
550#endif
Peter D'Hoye05cb4212008-06-29 08:05:00 +0000551 /* reset the scales just in case recording and playback
552 use different viewport sizes. Normally we should be checking viewport
553 sizes every time but this will do for now */
554 FOR_NB_SCREENS(i)
555 scales[i].db_scale_valid = false;
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000556}
557
Jens Arnoldcf986e82005-04-04 20:16:01 +0000558#ifdef HAVE_RECORDING
Jens Arnold99a05982005-08-29 20:07:17 +0000559static void set_trig_status(int new_state)
560{
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +0000561 if (trig_status != new_state) {
562 trig_status = new_state;
563 if (trigger_listener != NULL) {
564 trigger_listener(trig_status);
565 }
566 }
567}
Peter D'Hoye583f1862007-08-25 15:53:54 +0000568
Jens Arnoldcf986e82005-04-04 20:16:01 +0000569#endif
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +0000570
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000571/**
572 * Reads peak values from the MAS, and detects clips. The
Jens Arnold99a05982005-08-29 20:07:17 +0000573 * values are stored in pm_max_left pm_max_right for later
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000574 * evauluation. Consecutive calls to peak_meter_peek detect
575 * that ocurred. This function could be used by a thread for
576 * busy reading the MAS.
577 */
Jens Arnold99a05982005-08-29 20:07:17 +0000578void peak_meter_peek(void)
Daniel Stenbergdb32fb12005-02-02 21:49:19 +0000579{
Jens Arnold99a05982005-08-29 20:07:17 +0000580 int left, right;
Peter D'Hoye6109d392007-08-26 21:34:36 +0000581#ifdef HAVE_RECORDING
Peter D'Hoye583f1862007-08-25 15:53:54 +0000582 bool was_clipping = pm_clip_left || pm_clip_right;
Peter D'Hoye6109d392007-08-26 21:34:36 +0000583#endif
Jens Arnold99a05982005-08-29 20:07:17 +0000584 /* read current values */
Dan Evertone3765452006-02-23 21:13:03 +0000585#if CONFIG_CODEC == SWCODEC
Andy1e4d1f82005-12-02 01:04:03 +0000586 if (pm_playback)
587 pcm_calculate_peaks(&pm_cur_left, &pm_cur_right);
Peter D'Hoye496e1f72008-08-06 20:12:44 +0000588#ifdef HAVE_RECORDING
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000589 else
590 pcm_calculate_rec_peaks(&pm_cur_left, &pm_cur_right);
Peter D'Hoye496e1f72008-08-06 20:12:44 +0000591#endif
Jens Arnold99a05982005-08-29 20:07:17 +0000592 left = pm_cur_left;
593 right = pm_cur_right;
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000594#else
Dan Evertone3765452006-02-23 21:13:03 +0000595#ifndef SIMULATOR
Jens Arnold99a05982005-08-29 20:07:17 +0000596 pm_cur_left = left = mas_codec_readreg(pm_src_left);
597 pm_cur_right = right = mas_codec_readreg(pm_src_right);
Dan Evertone3765452006-02-23 21:13:03 +0000598#else
599 pm_cur_left = left = 8000;
600 pm_cur_right = right = 9000;
601#endif
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000602#endif
603
604 /* check for clips
605 An clip is assumed when two consecutive readouts
606 of the volume are at full scale. This is proven
607 to be inaccurate in both ways: it may detect clips
608 when no clip occurred and it may fail to detect
Peter D'Hoye311f7322006-03-08 20:38:16 +0000609 a real clip. For software codecs, the peak is already
610 the max of a bunch of samples, so use one max value
611 or you fail to detect clipping! */
612#if CONFIG_CODEC == SWCODEC
613 if (left == MAX_PEAK - 1) {
614#else
Jens Arnold99a05982005-08-29 20:07:17 +0000615 if ((left == pm_max_left) &&
616 (left == MAX_PEAK - 1)) {
Peter D'Hoye311f7322006-03-08 20:38:16 +0000617#endif
Jens Arnold99a05982005-08-29 20:07:17 +0000618 pm_clip_left = true;
619 pm_clip_timeout_l =
620 current_tick + clip_time_out[pm_clip_hold];
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000621 }
622
Peter D'Hoye311f7322006-03-08 20:38:16 +0000623#if CONFIG_CODEC == SWCODEC
624 if (right == MAX_PEAK - 1) {
625#else
Jens Arnold99a05982005-08-29 20:07:17 +0000626 if ((right == pm_max_right) &&
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000627 (right == MAX_PEAK - 1)) {
Peter D'Hoye311f7322006-03-08 20:38:16 +0000628#endif
Jens Arnold99a05982005-08-29 20:07:17 +0000629 pm_clip_right = true;
630 pm_clip_timeout_r =
631 current_tick + clip_time_out[pm_clip_hold];
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000632 }
633
Peter D'Hoye6109d392007-08-26 21:34:36 +0000634#ifdef HAVE_RECORDING
Peter D'Hoye583f1862007-08-25 15:53:54 +0000635 if(!was_clipping && (pm_clip_left || pm_clip_right))
636 {
637 if(pm_clipcount_active)
638 pm_clipcount++;
639 }
Peter D'Hoye6109d392007-08-26 21:34:36 +0000640#endif
641
Jens Arnold99a05982005-08-29 20:07:17 +0000642 /* peaks are searched -> we have to find the maximum. When
643 many calls of peak_meter_peek the maximum value will be
644 stored in pm_max_xxx. This maximum is reset by the
645 functions peak_meter_read_x. */
646 pm_max_left = MAX(pm_max_left, left);
647 pm_max_right = MAX(pm_max_right, right);
648
Jens Arnoldcf986e82005-04-04 20:16:01 +0000649#ifdef HAVE_RECORDING
Martin Scarrattd8103f32006-11-09 12:27:56 +0000650#if CONFIG_CODEC == SWCODEC
651 /* Ignore any unread peakmeter data */
652#define MAX_DROP_TIME HZ/7 /* this value may need tweaking. Increase if you are
653 getting trig events when you shouldn't with
654 trig_stp_hold = 0 */
655 if (!trig_stp_hold)
656 trig_stp_hold = MAX_DROP_TIME;
657#endif
658
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +0000659 switch (trig_status) {
660 case TRIG_READY:
661 /* no more changes, if trigger was activated as release trigger */
662 /* threshold exceeded? */
Jens Arnold99a05982005-08-29 20:07:17 +0000663 if ((left > trig_strt_threshold)
664 || (right > trig_strt_threshold)) {
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +0000665 /* reset trigger duration */
666 trig_hightime = current_tick;
667
668 /* reset dropout duration */
669 trig_lowtime = current_tick;
670
Martin Scarrattd8103f32006-11-09 12:27:56 +0000671 if (trig_strt_duration)
672 set_trig_status(TRIG_STEADY);
673 else
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +0000674 /* if trig_duration is set to 0 the user wants to start
675 recording immediately */
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +0000676 set_trig_status(TRIG_GO);
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +0000677 }
678 break;
679
680 case TRIG_STEADY:
681 case TRIG_RETRIG:
682 /* trigger duration exceeded */
683 if (current_tick - trig_hightime > trig_strt_duration) {
684 set_trig_status(TRIG_GO);
685 } else {
686 /* threshold exceeded? */
Jens Arnold99a05982005-08-29 20:07:17 +0000687 if ((left > trig_strt_threshold)
688 || (right > trig_strt_threshold)) {
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +0000689 /* reset lowtime */
690 trig_lowtime = current_tick;
691 }
692 /* volume is below threshold */
693 else {
694 /* dropout occurred? */
695 if (current_tick - trig_lowtime > trig_strt_dropout){
696 if (trig_status == TRIG_STEADY){
697 set_trig_status(TRIG_READY);
698 }
699 /* trig_status == TRIG_RETRIG */
700 else {
701 /* the gap has already expired */
702 trig_lowtime = current_tick - trig_rstrt_gap - 1;
703 set_trig_status(TRIG_POSTREC);
704 }
705 }
706 }
707 }
708 break;
709
710 case TRIG_GO:
711 case TRIG_CONTINUE:
712 /* threshold exceeded? */
Jens Arnold99a05982005-08-29 20:07:17 +0000713 if ((left > trig_stp_threshold)
714 || (right > trig_stp_threshold)) {
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +0000715 /* restart hold time countdown */
716 trig_lowtime = current_tick;
Martin Scarrattd8103f32006-11-09 12:27:56 +0000717#if CONFIG_CODEC == SWCODEC
718 } else if (current_tick - trig_lowtime > MAX_DROP_TIME){
719#else
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +0000720 } else {
Martin Scarrattd8103f32006-11-09 12:27:56 +0000721#endif
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +0000722 set_trig_status(TRIG_POSTREC);
723 trig_hightime = current_tick;
724 }
725 break;
726
727 case TRIG_POSTREC:
728 /* gap time expired? */
729 if (current_tick - trig_lowtime > trig_rstrt_gap){
730 /* start threshold exceeded? */
Jens Arnold99a05982005-08-29 20:07:17 +0000731 if ((left > trig_strt_threshold)
732 || (right > trig_strt_threshold)) {
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +0000733
734 set_trig_status(TRIG_RETRIG);
735 trig_hightime = current_tick;
Martin Scarrattd8103f32006-11-09 12:27:56 +0000736 trig_lowtime = current_tick;
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +0000737 }
738 else
739
740 /* stop threshold exceeded */
Jens Arnold99a05982005-08-29 20:07:17 +0000741 if ((left > trig_stp_threshold)
742 || (right > trig_stp_threshold)) {
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +0000743 if (current_tick - trig_hightime > trig_stp_hold){
744 trig_lowtime = current_tick;
745 set_trig_status(TRIG_CONTINUE);
746 } else {
747 trig_lowtime = current_tick - trig_rstrt_gap - 1;
748 }
749 }
750
751 /* below any threshold */
752 else {
753 if (current_tick - trig_lowtime > trig_stp_hold){
754 set_trig_status(TRIG_READY);
755 } else {
756 trig_hightime = current_tick;
757 }
758 }
759 }
760
761 /* still within the gap time */
762 else {
763 /* stop threshold exceeded */
Jens Arnold99a05982005-08-29 20:07:17 +0000764 if ((left > trig_stp_threshold)
765 || (right > trig_stp_threshold)) {
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +0000766 set_trig_status(TRIG_CONTINUE);
767 trig_lowtime = current_tick;
768 }
769
770 /* hold time expired */
771 else if (current_tick - trig_lowtime > trig_stp_hold){
772 trig_hightime = current_tick;
773 trig_lowtime = current_tick;
774 set_trig_status(TRIG_READY);
775 }
776 }
777 break;
778 }
Martin Scarrattd8103f32006-11-09 12:27:56 +0000779#if CONFIG_CODEC == SWCODEC
780 /* restore stop hold value */
781 if (trig_stp_hold == MAX_DROP_TIME)
782 trig_stp_hold = 0;
783#endif
Jens Arnoldcf986e82005-04-04 20:16:01 +0000784#endif
Dan Evertonb66477a2006-03-25 13:35:31 +0000785 /* check levels next time peakmeter drawn */
786 level_check = true;
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000787#ifdef PM_DEBUG
788 peek_calls++;
789#endif
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000790}
791
792/**
793 * Reads out the peak volume of the left channel.
794 * @return int - The maximum value that has been detected
795 * since the last call of peak_meter_read_l. The value
796 * is in the range 0 <= value < MAX_PEAK.
797 */
Jens Arnold99a05982005-08-29 20:07:17 +0000798static int peak_meter_read_l(void)
Daniel Stenbergdb32fb12005-02-02 21:49:19 +0000799{
Jens Arnold99a05982005-08-29 20:07:17 +0000800 /* pm_max_left contains the maximum of all peak values that were read
801 by peak_meter_peek since the last call of peak_meter_read_l */
802 int retval = pm_max_left;
803
Peter D'Hoye5fc66e52006-08-16 23:26:55 +0000804#ifdef HAVE_AGC
805 /* store max peak value for peak_meter_get_peakhold_x readout */
806 pm_peakhold_left = MAX(pm_max_left, pm_peakhold_left);
807#endif
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000808#ifdef PM_DEBUG
809 peek_calls = 0;
810#endif
Jens Arnold99a05982005-08-29 20:07:17 +0000811 /* reset pm_max_left so that subsequent calls of peak_meter_peek don't
812 get fooled by an old maximum value */
813 pm_max_left = pm_cur_left;
Peter D'Hoye496e1f72008-08-06 20:12:44 +0000814
Jens Arnold945462e2007-09-09 09:07:22 +0000815#if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
Peter D'Hoye583f1862007-08-25 15:53:54 +0000816 srand(current_tick);
817 retval = rand()%MAX_PEAK;
818#endif
819
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000820 return retval;
821}
822
823/**
824 * Reads out the peak volume of the right channel.
825 * @return int - The maximum value that has been detected
826 * since the last call of peak_meter_read_l. The value
827 * is in the range 0 <= value < MAX_PEAK.
828 */
Jens Arnold99a05982005-08-29 20:07:17 +0000829static int peak_meter_read_r(void)
830{
831 /* peak_meter_r contains the maximum of all peak values that were read
832 by peak_meter_peek since the last call of peak_meter_read_r */
833 int retval = pm_max_right;
834
Peter D'Hoye5fc66e52006-08-16 23:26:55 +0000835#ifdef HAVE_AGC
836 /* store max peak value for peak_meter_get_peakhold_x readout */
837 pm_peakhold_right = MAX(pm_max_right, pm_peakhold_right);
838#endif
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000839#ifdef PM_DEBUG
840 peek_calls = 0;
841#endif
Jens Arnold99a05982005-08-29 20:07:17 +0000842 /* reset pm_max_right so that subsequent calls of peak_meter_peek don't
843 get fooled by an old maximum value */
844 pm_max_right = pm_cur_right;
Peter D'Hoye583f1862007-08-25 15:53:54 +0000845
Jens Arnold945462e2007-09-09 09:07:22 +0000846#if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
Peter D'Hoye583f1862007-08-25 15:53:54 +0000847 srand(current_tick);
848 retval = rand()%MAX_PEAK;
849#endif
850
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000851 return retval;
852}
853
Peter D'Hoye5fc66e52006-08-16 23:26:55 +0000854#ifdef HAVE_AGC
855/**
856 * Reads out the current peak-hold values since the last call.
857 * This is used by the histogram feature in the recording screen.
858 * Values are in the range 0 <= peak_x < MAX_PEAK. MAX_PEAK is typ 32767.
859 */
Jens Arnold2597a132006-12-25 14:01:47 +0000860void peak_meter_get_peakhold(int *peak_left, int *peak_right)
Peter D'Hoye5fc66e52006-08-16 23:26:55 +0000861{
862 if (peak_left)
863 *peak_left = pm_peakhold_left;
864 if (peak_right)
865 *peak_right = pm_peakhold_right;
866 pm_peakhold_left = 0;
867 pm_peakhold_right = 0;
868}
869#endif
870
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000871/**
872 * Reset the detected clips. This method is for
873 * use by the user interface.
874 * @param int unused - This parameter was added to
875 * make the function compatible with set_int
876 */
Jens Arnold99a05982005-08-29 20:07:17 +0000877void peak_meter_set_clip_hold(int time)
878{
Peter D'Hoye9c3f6c42008-03-08 20:32:37 +0000879 pm_clip_left = false;
880 pm_clip_right = false;
881 pm_clip_eternal = (time > 0) ? false : true;
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000882}
883
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000884/**
885 * Scales a peak value as read from the MAS to the range of meterwidth.
886 * The scaling is performed according to the scaling method (dBfs / linear)
887 * and the range (peak_meter_range_min .. peak_meter_range_max).
888 * @param unsigned short val - The volume value. Range: 0 <= val < MAX_PEAK
889 * @param int meterwidht - The widht of the meter in pixel
890 * @return unsigned short - A value 0 <= return value <= meterwidth
891 */
Jens Arnold99a05982005-08-29 20:07:17 +0000892unsigned short peak_meter_scale_value(unsigned short val, int meterwidth)
893{
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000894 int retval;
895
896 if (val <= peak_meter_range_min) {
897 return 0;
898 }
899
900 if (val >= peak_meter_range_max) {
901 return meterwidth;
902 }
903
904 retval = val;
905
906 /* different scaling is used for dBfs and linear percent */
Jens Arnold99a05982005-08-29 20:07:17 +0000907 if (pm_use_dbfs) {
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000908
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000909 /* scale the samples dBfs */
Jens Arnold99a05982005-08-29 20:07:17 +0000910 retval = (calc_db(retval) - pm_db_min) * meterwidth / pm_db_range;
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000911 }
912
913 /* Scale for linear percent display */
914 else
915 {
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000916 /* scale the samples */
Linus Nielsen Feltzinga02ffd52002-10-30 23:01:27 +0000917 retval = ((retval - peak_meter_range_min) * meterwidth)
Jens Arnold99a05982005-08-29 20:07:17 +0000918 / pm_range;
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000919 }
920 return retval;
921}
Dan Evertonb66477a2006-03-25 13:35:31 +0000922void peak_meter_screen(struct screen *display, int x, int y, int height)
923{
924 peak_meter_draw(display, &scales[display->screen_type], x, y,
Dave Chapmand02c79c2008-03-21 19:38:00 +0000925 display->getwidth() - x, height);
Peter D'Hoye90b61692008-06-28 16:59:56 +0000926}
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000927/**
928 * Draws a peak meter in the specified size at the specified position.
929 * @param int x - The x coordinate.
Dave Chapmand02c79c2008-03-21 19:38:00 +0000930 * Make sure that 0 <= x and x + width < display->getwidth()
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000931 * @param int y - The y coordinate.
Dave Chapmand02c79c2008-03-21 19:38:00 +0000932 * Make sure that 0 <= y and y + height < display->getheight()
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000933 * @param int width - The width of the peak meter. Note that for display
934 * of clips a 3 pixel wide area is used ->
935 * width > 3
936 * @param int height - The height of the peak meter. height > 3
937 */
Nils Wallméniusc70d9462007-09-20 16:44:28 +0000938static void peak_meter_draw(struct screen *display, struct meter_scales *scales,
Dan Evertonb66477a2006-03-25 13:35:31 +0000939 int x, int y, int width, int height)
Jens Arnold04daef12005-06-24 22:33:21 +0000940{
Dan Evertonb66477a2006-03-25 13:35:31 +0000941 static int left_level = 0, right_level = 0;
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000942 int left = 0, right = 0;
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000943 int meterwidth = width - 3;
Jens Arnold99ff02d2007-08-20 06:36:24 +0000944 int i, delta;
Peter D'Hoye0ecdb372008-02-06 21:53:07 +0000945#if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
946 static long peak_release_tick[2] = {0,0};
947 int screen_nr = display->screen_type == SCREEN_MAIN ? 0 : 1;
948#else
Peter D'Hoyeebcf06d2007-08-18 23:03:03 +0000949 static long peak_release_tick = 0;
Peter D'Hoye0ecdb372008-02-06 21:53:07 +0000950#endif
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000951
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000952#ifdef PM_DEBUG
953 static long pm_tick = 0;
954 int tmp = peek_calls;
955#endif
956
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +0000957 /* if disabled only draw the peak meter */
958 if (peak_meter_enabled) {
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000959
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000960
Dan Evertonb66477a2006-03-25 13:35:31 +0000961 if (level_check){
Peter D'Hoye90b61692008-06-28 16:59:56 +0000962 /* only read the volume info from MAS if peek since last read*/
Dan Evertonb66477a2006-03-25 13:35:31 +0000963 left_level = peak_meter_read_l();
964 right_level = peak_meter_read_r();
965 level_check = false;
966 }
967
968 /* scale the samples dBfs */
969 left = peak_meter_scale_value(left_level, meterwidth);
Peter D'Hoye90b61692008-06-28 16:59:56 +0000970 right = peak_meter_scale_value(right_level, meterwidth);
Dan Evertonb66477a2006-03-25 13:35:31 +0000971
972 /*if the scale has changed -> recalculate the scale
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000973 (The scale becomes invalid when the range changed.) */
Dan Evertonb66477a2006-03-25 13:35:31 +0000974 if (!scales->db_scale_valid){
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000975
Jens Arnold99a05982005-08-29 20:07:17 +0000976 if (pm_use_dbfs) {
Linus Nielsen Feltzinga02ffd52002-10-30 23:01:27 +0000977 db_scale_count = DB_SCALE_SRC_VALUES_SIZE;
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000978 for (i = 0; i < db_scale_count; i++){
979 /* find the real x-coords for predefined interesting
980 dBfs values. These only are recalculated when the
981 scaling of the meter changed. */
Peter D'Hoye90b61692008-06-28 16:59:56 +0000982 scales->db_scale_lcd_coord[i] =
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000983 peak_meter_scale_value(
Peter D'Hoye90b61692008-06-28 16:59:56 +0000984 db_scale_src_values[i],
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000985 meterwidth - 1);
986 }
987 }
988
989 /* when scaling linear we simly make 10% steps */
990 else {
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000991 db_scale_count = 10;
992 for (i = 0; i < db_scale_count; i++) {
Peter D'Hoye90b61692008-06-28 16:59:56 +0000993 scales->db_scale_lcd_coord[i] =
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000994 (i * (MAX_PEAK / 10) - peak_meter_range_min) *
Jens Arnold99a05982005-08-29 20:07:17 +0000995 meterwidth / pm_range;
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +0000996 }
997 }
998
999 /* mark scale valid to avoid recalculating dBfs values
1000 of the scale. */
Dan Evertonb66477a2006-03-25 13:35:31 +00001001 scales->db_scale_valid = true;
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +00001002 }
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +00001003
1004 /* apply release */
Peter D'Hoye0ecdb372008-02-06 21:53:07 +00001005#if defined(HAVE_REMOTE_LCD) && !defined (ROCKBOX_HAS_LOGF)
1006 delta = current_tick - peak_release_tick[screen_nr];
1007 peak_release_tick[screen_nr] = current_tick;
1008#else
Jens Arnold99ff02d2007-08-20 06:36:24 +00001009 delta = current_tick - peak_release_tick;
1010 peak_release_tick = current_tick;
Peter D'Hoye0ecdb372008-02-06 21:53:07 +00001011#endif
Jens Arnold99ff02d2007-08-20 06:36:24 +00001012 left = MAX(left , scales->last_left - delta * pm_peak_release);
1013 right = MAX(right, scales->last_right - delta * pm_peak_release);
1014
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +00001015 /* reset max values after timeout */
Dan Evertonb66477a2006-03-25 13:35:31 +00001016 if (TIME_AFTER(current_tick, scales->pm_peak_timeout_l)){
1017 scales->pm_peak_left = 0;
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +00001018 }
1019
Dan Evertonb66477a2006-03-25 13:35:31 +00001020 if (TIME_AFTER(current_tick, scales->pm_peak_timeout_r)){
1021 scales->pm_peak_right = 0;
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +00001022 }
1023
Jens Arnold99a05982005-08-29 20:07:17 +00001024 if (!pm_clip_eternal) {
1025 if (pm_clip_left &&
1026 TIME_AFTER(current_tick, pm_clip_timeout_l)){
1027 pm_clip_left = false;
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +00001028 }
1029
Jens Arnold99a05982005-08-29 20:07:17 +00001030 if (pm_clip_right &&
1031 TIME_AFTER(current_tick, pm_clip_timeout_r)){
1032 pm_clip_right = false;
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +00001033 }
1034 }
1035
1036 /* check for new max values */
Dan Evertonb66477a2006-03-25 13:35:31 +00001037 if (left > scales->pm_peak_left) {
1038 scales->pm_peak_left = left - 1;
1039 scales->pm_peak_timeout_l =
Jens Arnold99a05982005-08-29 20:07:17 +00001040 current_tick + peak_time_out[pm_peak_hold];
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +00001041 }
1042
Dan Evertonb66477a2006-03-25 13:35:31 +00001043 if (right > scales->pm_peak_right) {
1044 scales->pm_peak_right = right - 1;
1045 scales->pm_peak_timeout_r =
Jens Arnold99a05982005-08-29 20:07:17 +00001046 current_tick + peak_time_out[pm_peak_hold];
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +00001047 }
1048 }
1049
1050 /* draw the peak meter */
Dan Evertonb66477a2006-03-25 13:35:31 +00001051 display->set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
1052 display->fillrect(x, y, width, height);
1053 display->set_drawmode(DRMODE_SOLID);
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +00001054
1055 /* draw left */
Dan Evertonb66477a2006-03-25 13:35:31 +00001056 display->fillrect (x, y, left, height / 2 - 2 );
1057 if (scales->pm_peak_left > 0) {
1058 display->vline(x + scales->pm_peak_left, y, y + height / 2 - 2 );
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +00001059 }
Jens Arnold99a05982005-08-29 20:07:17 +00001060 if (pm_clip_left) {
Dan Evertonb66477a2006-03-25 13:35:31 +00001061 display->fillrect(x + meterwidth, y, 3, height / 2 - 1);
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +00001062 }
1063
1064 /* draw right */
Dan Evertonb66477a2006-03-25 13:35:31 +00001065 display->fillrect(x, y + height / 2 + 1, right, height / 2 - 2);
1066 if (scales->pm_peak_right > 0) {
1067 display->vline( x + scales->pm_peak_right, y + height / 2, y + height - 2);
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +00001068 }
Jens Arnold99a05982005-08-29 20:07:17 +00001069 if (pm_clip_right) {
Dan Evertonb66477a2006-03-25 13:35:31 +00001070 display->fillrect(x + meterwidth, y + height / 2, 3, height / 2 - 1);
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +00001071 }
1072
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +00001073 /* draw scale end */
Dan Evertonb66477a2006-03-25 13:35:31 +00001074 display->vline(x + meterwidth, y, y + height - 2);
Jens Arnold93494122005-06-25 00:28:09 +00001075
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +00001076 /* draw dots for scale marks */
1077 for (i = 0; i < db_scale_count; i++) {
1078 /* The x-coordinates of interesting scale mark points
1079 have been calculated before */
Linus Nielsen Feltzingdefbc692006-04-03 08:20:20 +00001080 display->drawpixel(x + scales->db_scale_lcd_coord[i],
1081 y + height / 2 - 1);
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +00001082 }
Peter D'Hoye05cb4212008-06-29 08:05:00 +00001083
Jens Arnoldcf986e82005-04-04 20:16:01 +00001084#ifdef HAVE_RECORDING
Peter D'Hoye3467ba62006-02-17 22:47:56 +00001085
Jens Arnold47bf6c52007-04-12 22:12:13 +00001086#ifdef HAVE_BACKLIGHT
Peter D'Hoye3467ba62006-02-17 22:47:56 +00001087 /* cliplight */
1088 if ((pm_clip_left || pm_clip_right) &&
1089 global_settings.cliplight &&
1090#if CONFIG_CODEC == SWCODEC
Peter D'Hoye64f06822006-02-24 19:17:47 +00001091 !pm_playback)
Peter D'Hoye3467ba62006-02-17 22:47:56 +00001092#else
Peter D'Hoye64f06822006-02-24 19:17:47 +00001093 !(audio_status() & (AUDIO_STATUS_PLAY | AUDIO_STATUS_ERROR)))
Peter D'Hoye3467ba62006-02-17 22:47:56 +00001094#endif
1095 {
1096 /* if clipping, cliplight setting on and in recording screen */
1097 if (global_settings.cliplight <= 2)
1098 {
1099 /* turn on main unit light if setting set to main or both*/
1100 backlight_on();
1101 }
1102#ifdef HAVE_REMOTE_LCD
1103 if (global_settings.cliplight >= 2)
1104 {
1105 /* turn remote light unit on if setting set to remote or both */
1106 remote_backlight_on();
1107 }
1108#endif /* HAVE_REMOTE_LCD */
1109 }
Jens Arnold47bf6c52007-04-12 22:12:13 +00001110#endif /* HAVE_BACKLIGHT */
Peter D'Hoye3467ba62006-02-17 22:47:56 +00001111
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +00001112 if (trig_status != TRIG_OFF) {
1113 int start_trigx, stop_trigx, ycenter;
1114
Dan Evertonb66477a2006-03-25 13:35:31 +00001115 display->set_drawmode(DRMODE_SOLID);
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +00001116 ycenter = y + height / 2;
1117 /* display threshold value */
1118 start_trigx = x+peak_meter_scale_value(trig_strt_threshold,meterwidth);
Dan Evertonb66477a2006-03-25 13:35:31 +00001119 display->vline(start_trigx, ycenter - 2, ycenter);
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +00001120 start_trigx ++;
Peter D'Hoye90b61692008-06-28 16:59:56 +00001121 if (start_trigx < display->getwidth() ) display->drawpixel(start_trigx,
1122 ycenter - 1);
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +00001123
1124 stop_trigx = x + peak_meter_scale_value(trig_stp_threshold,meterwidth);
Dan Evertonb66477a2006-03-25 13:35:31 +00001125 display->vline(stop_trigx, ycenter - 2, ycenter);
1126 if (stop_trigx > 0) display->drawpixel(stop_trigx - 1, ycenter - 1);
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +00001127 }
Peter D'Hoye3467ba62006-02-17 22:47:56 +00001128#endif /*HAVE_RECORDING*/
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +00001129
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +00001130#ifdef PM_DEBUG
Nils Wallméniusb41e93a2007-03-19 16:49:55 +00001131 /* display a bar to show how many calls to peak_meter_peek
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +00001132 have ocurred since the last display */
Dan Evertonb66477a2006-03-25 13:35:31 +00001133 display->set_drawmode(DRMODE_COMPLEMENT);
1134 display->fillrect(x, y, tmp, 3);
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +00001135
1136 if (tmp < PEEKS_PER_DRAW_SIZE) {
1137 peeks_per_redraw[tmp]++;
1138 }
1139
1140 tmp = current_tick - pm_tick;
1141 if (tmp < TICKS_PER_DRAW_SIZE ){
1142 ticks_per_redraw[tmp] ++;
1143 }
1144
1145 /* display a bar to show how many ticks have passed since
1146 the last redraw */
Dan Evertonb66477a2006-03-25 13:35:31 +00001147 display->fillrect(x, y + height / 2, current_tick - pm_tick, 2);
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +00001148 pm_tick = current_tick;
1149#endif
1150
Dan Evertonb66477a2006-03-25 13:35:31 +00001151 scales->last_left = left;
1152 scales->last_right = right;
Jens Arnold04daef12005-06-24 22:33:21 +00001153
Dan Evertonb66477a2006-03-25 13:35:31 +00001154 display->set_drawmode(DRMODE_SOLID);
Linus Nielsen Feltzingf1170292002-09-27 09:44:51 +00001155}
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +00001156
Jens Arnoldcf986e82005-04-04 20:16:01 +00001157#ifdef HAVE_RECORDING
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +00001158/**
1159 * Defines the parameters of the trigger. After these parameters are defined
1160 * the trigger can be started either by peak_meter_attack_trigger or by
1161 * peak_meter_release_trigger. Note that you can pass either linear (%) or
1162 * logarithmic (db) values to the thresholds. Positive values are intepreted as
1163 * percent (0 is 0% .. 100 is 100%). Negative values are interpreted as db.
1164 * To avoid ambiguosity of the value 0 the negative values are shifted by -1.
1165 * Thus -75 is -74db .. -1 is 0db.
1166 * @param start_threshold - The threshold used for attack trigger. Negative
1167 * values are interpreted as db -1, positive as %.
1168 * @param start_duration - The minimum time span within which start_threshold
1169 * must be exceeded to fire the attack trigger.
1170 * @param start_dropout - The maximum time span the level may fall below
1171 * start_threshold without releasing the attack trigger.
1172 * @param stop_threshold - The threshold the volume must fall below to release
1173 * the release trigger.Negative values are
1174 * interpreted as db -1, positive as %.
1175 * @param stop_hold - The minimum time the volume must fall below the
1176 * stop_threshold to release the trigger.
1177 * @param
1178 */
1179void peak_meter_define_trigger(
1180 int start_threshold,
1181 long start_duration,
1182 long start_dropout,
1183 int stop_threshold,
1184 long stop_hold_time,
1185 long restart_gap
1186 )
1187{
1188 if (start_threshold < 0) {
1189 /* db */
1190 if (start_threshold < -89) {
1191 trig_strt_threshold = 0;
1192 } else {
1193 trig_strt_threshold =peak_meter_db2sample((start_threshold+1)*100);
1194 }
1195 } else {
1196 /* linear percent */
1197 trig_strt_threshold = start_threshold * MAX_PEAK / 100;
1198 }
1199 trig_strt_duration = start_duration;
1200 trig_strt_dropout = start_dropout;
1201 if (stop_threshold < 0) {
1202 /* db */
1203 trig_stp_threshold = peak_meter_db2sample((stop_threshold + 1) * 100);
1204 } else {
1205 /* linear percent */
1206 trig_stp_threshold = stop_threshold * MAX_PEAK / 100;
1207 }
1208 trig_stp_hold = stop_hold_time;
1209 trig_rstrt_gap = restart_gap;
1210}
1211
1212/**
1213 * Enables or disables the trigger.
1214 * @param on - If true the trigger is turned on.
1215 */
Jens Arnold99a05982005-08-29 20:07:17 +00001216void peak_meter_trigger(bool on)
1217{
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +00001218 /* don't use set_trigger here as that would fire an undesired event */
1219 trig_status = on ? TRIG_READY : TRIG_OFF;
1220}
1221
1222/**
1223 * Registers the listener function that listenes on trig_status changes.
1224 * @param listener - The function that is called with each change of
1225 * trig_status. May be set to NULL if no callback is desired.
1226 */
Jens Arnold99a05982005-08-29 20:07:17 +00001227void peak_meter_set_trigger_listener(void (*listener)(int status))
1228{
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +00001229 trigger_listener = listener;
1230}
1231
1232/**
1233 * Fetches the status of the trigger.
1234 * TRIG_OFF: the trigger is inactive
1235 * TRIG_RELEASED: The volume level is below the threshold
1236 * TRIG_ACTIVATED: The volume level has exceeded the threshold, but the trigger
1237 * hasn't been fired yet.
1238 * TRIG_FIRED: The volume exceeds the threshold
1239 *
1240 * To activate the trigger call either peak_meter_attack_trigger or
1241 * peak_meter_release_trigger. To turn the trigger off call
1242 * peak_meter_trigger_off.
1243 */
Jens Arnold99a05982005-08-29 20:07:17 +00001244int peak_meter_trigger_status(void)
1245{
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +00001246 return trig_status; /* & TRIG_PIT_MASK;*/
1247}
1248
Peter D'Hoye90b61692008-06-28 16:59:56 +00001249void peak_meter_draw_trig(int xpos[], int ypos[],
1250 int trig_width[], int nb_screens)
Jens Arnold99a05982005-08-29 20:07:17 +00001251{
Martin Scarrattd8103f32006-11-09 12:27:56 +00001252 int barstart[NB_SCREENS];
1253 int barend[NB_SCREENS];
1254 int icon;
1255 int ixpos[NB_SCREENS];
Martin Scarratt13e49f52006-07-19 15:30:40 +00001256 int i;
Martin Scarrattd8103f32006-11-09 12:27:56 +00001257 int trigbar_width[NB_SCREENS];
1258
1259 FOR_NB_SCREENS(i)
1260 trigbar_width[i] = (trig_width[i] - (2 * (ICON_PLAY_STATE_WIDTH + 1)));
1261
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +00001262 switch (trig_status) {
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +00001263
1264 case TRIG_READY:
Martin Scarrattd8103f32006-11-09 12:27:56 +00001265 FOR_NB_SCREENS(i){
1266 barstart[i] = 0;
1267 barend[i] = 0;
1268 }
Jens Arnold99a05982005-08-29 20:07:17 +00001269 icon = Icon_Stop;
Martin Scarrattd8103f32006-11-09 12:27:56 +00001270 FOR_NB_SCREENS(i)
1271 ixpos[i] = xpos[i];
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +00001272 break;
1273
1274 case TRIG_STEADY:
Martin Scarrattd8103f32006-11-09 12:27:56 +00001275 case TRIG_RETRIG:
1276 FOR_NB_SCREENS(i)
1277 {
1278 barstart[i] = 0;
1279 barend[i] = (trig_strt_duration == 0) ? trigbar_width[i] :
1280 trigbar_width[i] *
1281 (current_tick - trig_hightime) / trig_strt_duration;
1282 }
Jens Arnold99a05982005-08-29 20:07:17 +00001283 icon = Icon_Stop;
Martin Scarrattd8103f32006-11-09 12:27:56 +00001284 FOR_NB_SCREENS(i)
1285 ixpos[i] = xpos[i];
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +00001286 break;
1287
1288 case TRIG_GO:
1289 case TRIG_CONTINUE:
Martin Scarrattd8103f32006-11-09 12:27:56 +00001290 FOR_NB_SCREENS(i)
1291 {
1292 barstart[i] = trigbar_width[i];
1293 barend[i] = trigbar_width[i];
1294 }
Jens Arnold99a05982005-08-29 20:07:17 +00001295 icon = Icon_Record;
Martin Scarrattd8103f32006-11-09 12:27:56 +00001296 FOR_NB_SCREENS(i)
1297 ixpos[i] = xpos[i]+ trig_width[i] - ICON_PLAY_STATE_WIDTH;
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +00001298 break;
1299
Martin Scarrattd8103f32006-11-09 12:27:56 +00001300 case TRIG_POSTREC:
1301 FOR_NB_SCREENS(i)
1302 {
1303 barstart[i] = (trig_stp_hold == 0) ? 0 :
1304 trigbar_width[i] - trigbar_width[i] *
1305 (current_tick - trig_lowtime) / trig_stp_hold;
1306 barend[i] = trigbar_width[i];
1307 }
Jens Arnold99a05982005-08-29 20:07:17 +00001308 icon = Icon_Record;
Martin Scarrattd8103f32006-11-09 12:27:56 +00001309 FOR_NB_SCREENS(i)
1310 ixpos[i] = xpos[i] + trig_width[i] - ICON_PLAY_STATE_WIDTH;
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +00001311 break;
Jens Arnold99a05982005-08-29 20:07:17 +00001312
1313 default:
1314 return;
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +00001315 }
Martin Scarrattd8103f32006-11-09 12:27:56 +00001316
1317 for(i = 0; i < nb_screens; i++)
Martin Scarratt13e49f52006-07-19 15:30:40 +00001318 {
Martin Scarrattd8103f32006-11-09 12:27:56 +00001319 gui_scrollbar_draw(&screens[i], xpos[i] + ICON_PLAY_STATE_WIDTH + 1,
1320 ypos[i] + 1, trigbar_width[i], TRIG_HEIGHT - 2,
1321 trigbar_width[i], barstart[i], barend[i],
1322 HORIZONTAL);
1323
1324 screens[i].mono_bitmap(bitmap_icons_7x8[icon], ixpos[i], ypos[i],
Martin Scarratt13e49f52006-07-19 15:30:40 +00001325 ICON_PLAY_STATE_WIDTH, STATUSBAR_HEIGHT);
1326 }
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +00001327}
Jens Arnoldcf986e82005-04-04 20:16:01 +00001328#endif
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +00001329
Peter D'Hoye496e1f72008-08-06 20:12:44 +00001330int peak_meter_draw_get_btn(int action_context, int x[], int y[],
Jonathan Gordon701a5932009-02-02 03:14:51 +00001331 int height[], int nb_screens,
1332 struct viewport vps[])
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +00001333{
Jens Arnold99a05982005-08-29 20:07:17 +00001334 int button = BUTTON_NONE;
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +00001335 long next_refresh = current_tick;
1336 long next_big_refresh = current_tick + HZ / 10;
Dan Evertonb66477a2006-03-25 13:35:31 +00001337 int i;
Bertrik Sikkenf4d4a6a2008-07-03 13:37:57 +00001338#if (CONFIG_CODEC == SWCODEC)
Jens Arnold99a05982005-08-29 20:07:17 +00001339 bool highperf = false;
Jens Arnold8b446012007-10-16 08:34:51 +00001340#else
1341 /* On MAS targets, we need to poll as often as possible in order to not
1342 * miss a peak, as the MAS does only provide a quasi-peak. When the disk
1343 * is active, it must not draw too much CPU power or a buffer overrun can
1344 * happen when saving a recording. As a compromise, poll only once per tick
1345 * when the disk is active, otherwise spin around as fast as possible. */
Frank Gevaerts2f8a0082008-11-01 16:14:28 +00001346 bool highperf = !storage_disk_is_active();
Jens Arnold99a05982005-08-29 20:07:17 +00001347#endif
1348 bool dopeek = true;
Jens Arnold9d14b732005-06-04 09:48:03 +00001349
Jens Arnold99a05982005-08-29 20:07:17 +00001350 while (TIME_BEFORE(current_tick, next_big_refresh)) {
Jonathan Gordonff469ab2008-05-28 10:55:39 +00001351 button = get_action(action_context, TIMEOUT_NOBLOCK);
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +00001352 if (button != BUTTON_NONE) {
1353 break;
1354 }
Jens Arnold99a05982005-08-29 20:07:17 +00001355 if (dopeek) { /* Peek only once per refresh when disk is */
1356 peak_meter_peek(); /* spinning, but as often as possible */
1357 dopeek = highperf; /* otherwise. */
1358 yield();
1359 } else {
1360 sleep(0); /* Sleep until end of current tick. */
1361 }
1362 if (TIME_AFTER(current_tick, next_refresh)) {
Martin Scarratt1ae0cdb2006-07-31 12:55:27 +00001363 for(i = 0; i < nb_screens; i++)
Dan Evertonb66477a2006-03-25 13:35:31 +00001364 {
Jonathan Gordon701a5932009-02-02 03:14:51 +00001365 screens[i].set_viewport(&vps[i]);
Peter D'Hoye496e1f72008-08-06 20:12:44 +00001366 peak_meter_screen(&screens[i], x[i], y[i], height[i]);
1367 screens[i].update_viewport_rect(x[i], y[i],
1368 screens[i].getwidth() - x[i],
1369 height[i]);
Dan Evertonb66477a2006-03-25 13:35:31 +00001370 }
Jens Arnold99a05982005-08-29 20:07:17 +00001371 next_refresh += HZ / PEAK_METER_FPS;
1372 dopeek = true;
1373 }
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +00001374 }
Jens Arnold99a05982005-08-29 20:07:17 +00001375
Linus Nielsen Feltzing68482bb2005-04-04 09:12:12 +00001376 return button;
1377}
1378
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +00001379#ifdef PM_DEBUG
Jens Arnold99a05982005-08-29 20:07:17 +00001380static void peak_meter_clear_histogram(void)
1381{
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +00001382 int i = 0;
1383 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1384 ticks_per_redraw[i] = (unsigned int)0;
1385 }
1386
1387 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1388 peeks_per_redraw[i] = (unsigned int)0;
1389 }
1390}
1391
Jens Arnold99a05982005-08-29 20:07:17 +00001392bool peak_meter_histogram(void)
1393{
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +00001394 int i;
1395 int btn = BUTTON_NONE;
1396 while ((btn & BUTTON_OFF) != BUTTON_OFF )
1397 {
1398 unsigned int max = 0;
1399 int y = 0;
1400 int x = 0;
Martin Scarratt13e49f52006-07-19 15:30:40 +00001401 screens[0].clear_display();
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +00001402
1403 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1404 max = MAX(max, peeks_per_redraw[i]);
1405 }
1406
1407 for (i = 0; i < PEEKS_PER_DRAW_SIZE; i++) {
1408 x = peeks_per_redraw[i] * (LCD_WIDTH - 1)/ max;
Martin Scarratt13e49f52006-07-19 15:30:40 +00001409 screens[0].hline(0, x, y + i);
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +00001410 }
1411
1412 y = PEEKS_PER_DRAW_SIZE + 1;
1413 max = 0;
1414
1415 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1416 max = MAX(max, ticks_per_redraw[i]);
1417 }
1418
1419 for (i = 0; i < TICKS_PER_DRAW_SIZE; i++) {
1420 x = ticks_per_redraw[i] * (LCD_WIDTH - 1)/ max;
Martin Scarratt13e49f52006-07-19 15:30:40 +00001421 screens[0].hline(0, x, y + i);
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +00001422 }
Martin Scarratt13e49f52006-07-19 15:30:40 +00001423 screens[0].update();
Jens Arnold99a05982005-08-29 20:07:17 +00001424
Linus Nielsen Feltzingfd0cc3b2002-10-29 12:09:15 +00001425 btn = button_get(true);
1426 if (btn == BUTTON_PLAY) {
1427 peak_meter_clear_histogram();
1428 }
1429 }
1430 return false;
1431}
Daniel Stenbergdb32fb12005-02-02 21:49:19 +00001432#endif
1433
Peter D'Hoye496e1f72008-08-06 20:12:44 +00001434