FS#9281 Rename of splash functions.
[kugel-rb.git] / apps / recorder / recording.c
blob5b51cc161d1453a052e1aca0eb4f03557192d33a
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "config.h"
24 #include <stdio.h>
25 #include <stdbool.h>
26 #include <stdlib.h>
28 #include "system.h"
29 #include "power.h"
30 #include "powermgmt.h"
31 #include "lcd.h"
32 #include "led.h"
33 #include "mpeg.h"
34 #include "audio.h"
35 #if CONFIG_CODEC == SWCODEC
36 #include "thread.h"
37 #include "playback.h"
38 #include "enc_config.h"
39 #if defined(HAVE_SPDIF_IN) || defined(HAVE_SPDIF_OUT)
40 #include "spdif.h"
41 #endif
42 #endif /* CONFIG_CODEC == SWCODEC */
43 #include "recording.h"
44 #include "mp3_playback.h"
45 #include "mas.h"
46 #include "button.h"
47 #include "kernel.h"
48 #include "settings.h"
49 #include "lang.h"
50 #include "font.h"
51 #include "icons.h"
52 #include "icon.h"
53 #include "screens.h"
54 #include "peakmeter.h"
55 #include "statusbar.h"
56 #include "menu.h"
57 #include "sound_menu.h"
58 #include "timefuncs.h"
59 #include "debug.h"
60 #include "misc.h"
61 #include "tree.h"
62 #include "string.h"
63 #include "dir.h"
64 #include "errno.h"
65 #include "talk.h"
66 #include "sound.h"
67 #include "ata.h"
68 #include "splash.h"
69 #include "screen_access.h"
70 #include "action.h"
71 #include "radio.h"
72 #include "sound_menu.h"
73 #include "viewport.h"
74 #include "list.h"
76 #ifdef HAVE_RECORDING
77 /* This array holds the record timer interval lengths, in seconds */
78 static const unsigned long rec_timer_seconds[] =
80 0, /* 0 means OFF */
81 5*60, /* 00:05 */
82 10*60, /* 00:10 */
83 15*60, /* 00:15 */
84 30*60, /* 00:30 */
85 60*60, /* 01:00 */
86 74*60, /* 74:00 */
87 80*60, /* 80:00 */
88 2*60*60, /* 02:00 */
89 4*60*60, /* 04:00 */
90 6*60*60, /* 06:00 */
91 8*60*60, /* 08:00 */
92 10L*60*60, /* 10:00 */
93 12L*60*60, /* 12:00 */
94 18L*60*60, /* 18:00 */
95 24L*60*60 /* 24:00 */
98 static unsigned int rec_timesplit_seconds(void)
100 return rec_timer_seconds[global_settings.rec_timesplit];
103 /* This array holds the record size interval lengths, in bytes */
104 static const unsigned long rec_size_bytes[] =
106 0, /* 0 means OFF */
107 5*1024*1024, /* 5MB */
108 10*1024*1024, /* 10MB */
109 15*1024*1024, /* 15MB */
110 32*1024*1024, /* 32MB */
111 64*1024*1024, /* 64MB */
112 75*1024*1024, /* 75MB */
113 100*1024*1024, /* 100MB */
114 128*1024*1024, /* 128MB */
115 256*1024*1024, /* 256MB */
116 512*1024*1024, /* 512MB */
117 650*1024*1024, /* 650MB */
118 700*1024*1024, /* 700MB */
119 1024*1024*1024, /* 1GB */
120 1536*1024*1024, /* 1.5GB */
121 1792*1024*1024, /* 1.75GB */
124 static unsigned long rec_sizesplit_bytes(void)
126 return rec_size_bytes[global_settings.rec_sizesplit];
129 void settings_apply_trigger(void)
131 int start_thres, stop_thres;
132 if (global_settings.peak_meter_dbfs)
134 start_thres = global_settings.rec_start_thres_db - 1;
135 stop_thres = global_settings.rec_stop_thres_db - 1;
137 else
139 start_thres = global_settings.rec_start_thres_linear;
140 stop_thres = global_settings.rec_stop_thres_linear;
143 peak_meter_define_trigger(
144 start_thres,
145 global_settings.rec_start_duration*HZ,
146 MIN(global_settings.rec_start_duration*HZ / 2, 2*HZ),
147 stop_thres,
148 global_settings.rec_stop_postrec*HZ,
149 global_settings.rec_stop_gap*HZ
152 /* recording screen status flags */
153 enum rec_status_flags
155 RCSTAT_IN_RECSCREEN = 0x00000001,
156 RCSTAT_BEEN_IN_USB_MODE = 0x00000002,
157 RCSTAT_CREATED_DIRECTORY = 0x00000004,
158 RCSTAT_HAVE_RECORDED = 0x00000008,
161 static int rec_status = 0;
163 bool in_recording_screen(void)
165 return (rec_status & RCSTAT_IN_RECSCREEN) != 0;
168 #if CONFIG_KEYPAD == RECORDER_PAD
169 static bool f2_rec_screen(void);
170 static bool f3_rec_screen(void);
171 #endif
173 #define MAX_FILE_SIZE 0x7F800000 /* 2 GB - 4 MB */
175 #ifndef HAVE_REMOTE_LCD
176 static const int screen_update = NB_SCREENS;
177 #else
178 static int screen_update = NB_SCREENS;
179 static bool remote_display_on = true;
180 #endif
182 /* as we have the ability to disable the remote, we need an alternative loop */
183 #define FOR_NB_ACTIVE_SCREENS(i) for(i = 0; i < screen_update; i++)
185 static bool update_list = false; /* (GIU) list needs updating */
187 /** File name creation **/
188 #if CONFIG_RTC == 0
189 /* current file number to assist in creating unique numbered filenames
190 without actually having to create the file on disk */
191 static int file_number = -1;
192 #endif /* CONFIG_RTC */
194 #if CONFIG_CODEC == SWCODEC
196 #define REC_FILE_ENDING(rec_format) \
197 (audio_formats[rec_format_afmt[rec_format]].ext_list)
199 #else /* CONFIG_CODEC != SWCODEC */
201 /* default record file extension for HWCODEC */
202 #define REC_FILE_ENDING(rec_format) \
203 (audio_formats[AFMT_MPA_L3].ext_list)
205 #endif /* CONFIG_CODEC == SWCODEC */
207 /* path for current file */
208 static char path_buffer[MAX_PATH];
210 /** Automatic Gain Control (AGC) **/
211 #ifdef HAVE_AGC
212 /* Timing counters:
213 * peak_time is incremented every 0.2s, every 2nd run of record screen loop.
214 * hist_time is incremented every 0.5s, display update.
215 * peak_time is the counter of the peak hold read and agc process,
216 * overflow every 13 years 8-)
218 static long peak_time = 0;
219 static long hist_time = 0;
221 static short peak_valid_mem[4];
222 #define BAL_MEM_SIZE 24
223 static short balance_mem[BAL_MEM_SIZE];
225 /* Automatic Gain Control */
226 #define AGC_MODE_SIZE 5
227 #define AGC_SAFETY_MODE 0
229 static const char* agc_preset_str[] =
230 { "Off", "S", "L", "D", "M", "V" };
231 /* "Off",
232 "Safety (clip)",
233 "Live (slow)",
234 "DJ-Set (slow)",
235 "Medium",
236 "Voice (fast)" */
237 #define AGC_CLIP 32766
238 #define AGC_PEAK 29883 /* fast gain reduction threshold -0.8dB */
239 #define AGC_HIGH 27254 /* accelerated gain reduction threshold -1.6dB */
240 #define AGC_IMG 823 /* threshold for balance control -32dB */
241 /* autogain high level thresholds (-3dB, -7dB, -4dB, -5dB, -5dB) */
242 static const short agc_th_hi[AGC_MODE_SIZE] =
243 { 23197, 14637, 21156, 18428, 18426 };
244 /* autogain low level thresholds (-14dB, -11dB, -6dB, -7dB, -8dB) */
245 static const short agc_th_lo[AGC_MODE_SIZE] =
246 { 6538, 9235, 16422, 14636, 13045 };
247 /* autogain threshold times [1/5s] or [200ms] */
248 static const short agc_tdrop[AGC_MODE_SIZE] =
249 { 900, 225, 150, 60, 8 };
250 static const short agc_trise[AGC_MODE_SIZE] =
251 { 9000, 750, 400, 150, 20 };
252 static const short agc_tbal[AGC_MODE_SIZE] =
253 { 4500, 500, 300, 100, 15 };
254 /* AGC operation */
255 static bool agc_enable = true;
256 static short agc_preset;
257 /* AGC levels */
258 static int agc_left = 0;
259 static int agc_right = 0;
260 /* AGC time since high target volume was exceeded */
261 static short agc_droptime = 0;
262 /* AGC time since volume fallen below low target */
263 static short agc_risetime = 0;
264 /* AGC balance time exceeding +/- 0.7dB */
265 static short agc_baltime = 0;
266 /* AGC maximum gain */
267 static short agc_maxgain;
268 #endif /* HAVE_AGC */
270 static void set_gain(void)
272 if(global_settings.rec_source == AUDIO_SRC_MIC)
274 audio_set_recording_gain(global_settings.rec_mic_gain,
275 0, AUDIO_GAIN_MIC);
277 else
279 /* AUDIO_SRC_LINEIN, AUDIO_SRC_FMRADIO, AUDIO_SRC_SPDIF */
280 audio_set_recording_gain(global_settings.rec_left_gain,
281 global_settings.rec_right_gain,
282 AUDIO_GAIN_LINEIN);
284 /* reset the clipping indicators */
285 peak_meter_set_clip_hold(global_settings.peak_meter_clip_hold);
286 update_list = true;
289 #ifdef HAVE_AGC
290 /* Read peak meter values & calculate balance.
291 * Returns validity of peak values.
292 * Used for automatic gain control and history diagram.
294 static bool read_peak_levels(int *peak_l, int *peak_r, int *balance)
296 peak_meter_get_peakhold(peak_l, peak_r);
297 peak_valid_mem[peak_time % 3] = *peak_l;
298 if (((peak_valid_mem[0] == peak_valid_mem[1]) &&
299 (peak_valid_mem[1] == peak_valid_mem[2])) &&
300 ((*peak_l < 32767) || ata_disk_is_active()))
301 return false;
303 if (*peak_r > *peak_l)
304 balance_mem[peak_time % BAL_MEM_SIZE] = (*peak_l ?
305 MIN((10000 * *peak_r) / *peak_l - 10000, 15118) : 15118);
306 else
307 balance_mem[peak_time % BAL_MEM_SIZE] = (*peak_r ?
308 MAX(10000 - (10000 * *peak_l) / *peak_r, -15118) : -15118);
309 *balance = 0;
310 int i;
311 for (i = 0; i < BAL_MEM_SIZE; i++)
312 *balance += balance_mem[i];
313 *balance = *balance / BAL_MEM_SIZE;
315 return true;
318 /* AGC helper function to check if maximum gain is reached */
319 static bool agc_gain_is_max(bool left, bool right)
321 /* range -128...+108 [0.5dB] */
322 short gain_current_l;
323 short gain_current_r;
325 if (agc_preset == 0)
326 return false;
328 switch (global_settings.rec_source)
330 HAVE_LINE_REC_(case AUDIO_SRC_LINEIN:)
331 HAVE_FMRADIO_REC_(case AUDIO_SRC_FMRADIO:)
332 gain_current_l = global_settings.rec_left_gain;
333 gain_current_r = global_settings.rec_right_gain;
334 break;
335 case AUDIO_SRC_MIC:
336 default:
337 gain_current_l = global_settings.rec_mic_gain;
338 gain_current_r = global_settings.rec_mic_gain;
341 return ((left && (gain_current_l >= agc_maxgain)) ||
342 (right && (gain_current_r >= agc_maxgain)));
345 static void change_recording_gain(bool increment, bool left, bool right)
347 int factor = (increment ? 1 : -1);
349 switch (global_settings.rec_source)
351 HAVE_LINE_REC_(case AUDIO_SRC_LINEIN:)
352 HAVE_FMRADIO_REC_(case AUDIO_SRC_FMRADIO:)
353 if (left) global_settings.rec_left_gain += factor;
354 if (right) global_settings.rec_right_gain += factor;
355 break;
356 case AUDIO_SRC_MIC:
357 global_settings.rec_mic_gain += factor;
362 * Handle automatic gain control (AGC).
363 * Change recording gain if peak_x levels are above or below
364 * target volume for specified timeouts.
366 static void auto_gain_control(int *peak_l, int *peak_r, int *balance)
368 int agc_mono;
369 short agc_mode;
370 bool increment;
372 if (*peak_l > agc_left)
373 agc_left = *peak_l;
374 else
375 agc_left -= (agc_left - *peak_l + 3) >> 2;
376 if (*peak_r > agc_right)
377 agc_right = *peak_r;
378 else
379 agc_right -= (agc_right - *peak_r + 3) >> 2;
380 agc_mono = (agc_left + agc_right) / 2;
382 agc_mode = abs(agc_preset) - 1;
383 if (agc_mode < 0) {
384 agc_enable = false;
385 return;
388 if (agc_mode != AGC_SAFETY_MODE) {
389 /* Automatic balance control - only if not in safety mode */
390 if ((agc_left > AGC_IMG) && (agc_right > AGC_IMG))
392 if (*balance < -556)
394 if (*balance > -900)
395 agc_baltime -= !(peak_time % 4); /* 0.47 - 0.75dB */
396 else if (*balance > -4125)
397 agc_baltime--; /* 0.75 - 3.00dB */
398 else if (*balance > -7579)
399 agc_baltime -= 2; /* 3.00 - 4.90dB */
400 else
401 agc_baltime -= !(peak_time % 8); /* 4.90 - inf dB */
402 if (agc_baltime > 0)
403 agc_baltime -= (peak_time % 2);
405 else if (*balance > 556)
407 if (*balance < 900)
408 agc_baltime += !(peak_time % 4);
409 else if (*balance < 4125)
410 agc_baltime++;
411 else if (*balance < 7579)
412 agc_baltime += 2;
413 else
414 agc_baltime += !(peak_time % 8);
415 if (agc_baltime < 0)
416 agc_baltime += (peak_time % 2);
419 if ((*balance * agc_baltime) < 0)
421 if (*balance < 0)
422 agc_baltime -= peak_time % 2;
423 else
424 agc_baltime += peak_time % 2;
427 increment = ((agc_risetime / 2) > agc_droptime);
429 if (agc_baltime < -agc_tbal[agc_mode])
431 if (!increment || !agc_gain_is_max(!increment, increment)) {
432 change_recording_gain(increment, !increment, increment);
433 set_gain();
435 agc_baltime = 0;
437 else if (agc_baltime > +agc_tbal[agc_mode])
439 if (!increment || !agc_gain_is_max(increment, !increment)) {
440 change_recording_gain(increment, increment, !increment);
441 set_gain();
443 agc_baltime = 0;
446 else if (!(hist_time % 4))
448 if (agc_baltime < 0)
449 agc_baltime++;
450 else
451 agc_baltime--;
455 /* Automatic gain control */
456 if ((agc_left > agc_th_hi[agc_mode]) || (agc_right > agc_th_hi[agc_mode]))
458 if ((agc_left > AGC_CLIP) || (agc_right > AGC_CLIP))
459 agc_droptime += agc_tdrop[agc_mode] /
460 (global_settings.rec_agc_cliptime + 1);
461 if (agc_left > AGC_HIGH) {
462 agc_droptime++;
463 agc_risetime=0;
464 if (agc_left > AGC_PEAK)
465 agc_droptime += 2;
467 if (agc_right > AGC_HIGH) {
468 agc_droptime++;
469 agc_risetime=0;
470 if (agc_right > AGC_PEAK)
471 agc_droptime += 2;
473 if (agc_mono > agc_th_hi[agc_mode])
474 agc_droptime++;
475 else
476 agc_droptime += !(peak_time % 2);
478 if (agc_droptime >= agc_tdrop[agc_mode])
480 change_recording_gain(false, true, true);
481 agc_droptime = 0;
482 agc_risetime = 0;
483 set_gain();
485 agc_risetime = MAX(agc_risetime - 1, 0);
487 else if (agc_mono < agc_th_lo[agc_mode])
489 if (agc_mono < (agc_th_lo[agc_mode] / 8))
490 agc_risetime += !(peak_time % 5);
491 else if (agc_mono < (agc_th_lo[agc_mode] / 2))
492 agc_risetime += 2;
493 else
494 agc_risetime++;
496 if (agc_risetime >= agc_trise[agc_mode]) {
497 if ((agc_mode != AGC_SAFETY_MODE) &&
498 (!agc_gain_is_max(true, true))) {
499 change_recording_gain(true, true, true);
500 set_gain();
502 agc_risetime = 0;
503 agc_droptime = 0;
505 agc_droptime = MAX(agc_droptime - 1, 0);
507 else if (!(peak_time % 6)) /* on target level every 1.2 sec */
509 agc_risetime = MAX(agc_risetime - 1, 0);
510 agc_droptime = MAX(agc_droptime - 1, 0);
513 #endif /* HAVE_AGC */
515 static const char* const fmtstr[] =
517 "%c%d %s", /* no decimals */
518 "%c%d.%d %s ", /* 1 decimal */
519 "%c%d.%02d %s " /* 2 decimals */
522 static char *fmt_gain(int snd, int val, char *str, int len)
524 int i, d, numdec;
525 const char *unit;
526 char sign = ' ';
528 val = sound_val2phys(snd, val);
529 if(val < 0)
531 sign = '-';
532 val = -val;
534 numdec = sound_numdecimals(snd);
535 unit = sound_unit(snd);
537 if(numdec)
539 i = val / (10*numdec);
540 d = val % (10*numdec);
541 snprintf(str, len, fmtstr[numdec], sign, i, d, unit);
543 else
544 snprintf(str, len, fmtstr[numdec], sign, val, unit);
546 return str;
549 /* the list below must match enum audio_sources in audio.h */
550 static const char* const prestr[] =
552 HAVE_MIC_IN_([AUDIO_SRC_MIC] = "R_MIC_",)
553 HAVE_LINE_REC_([AUDIO_SRC_LINEIN] = "R_LINE_",)
554 HAVE_SPDIF_IN_([AUDIO_SRC_SPDIF] = "R_SPDIF_",)
555 HAVE_FMRADIO_REC_([AUDIO_SRC_FMRADIO] = "R_FM_",)
558 char *rec_create_filename(char *buffer)
560 char ext[16];
561 const char *pref = "R_";
563 /* Directory existence and writeablility should have already been
564 * verified - do not pass NULL pointers to pcmrec */
566 if((unsigned)global_settings.rec_source < AUDIO_NUM_SOURCES)
568 pref = prestr[global_settings.rec_source];
571 strcpy(buffer, global_settings.rec_directory);
573 snprintf(ext, sizeof(ext), ".%s",
574 REC_FILE_ENDING(global_settings.rec_format));
576 #if CONFIG_RTC == 0
577 return create_numbered_filename(buffer, buffer, pref, ext, 4,
578 &file_number);
579 #else
580 /* We'll wait at least up to the start of the next second so no duplicate
581 names are created */
582 return create_datetime_filename(buffer, buffer, pref, ext, true);
583 #endif
586 #if CONFIG_RTC == 0
587 /* Hit disk to get a starting filename for the type */
588 static void rec_init_filename(void)
590 file_number = -1;
591 rec_create_filename(path_buffer);
592 file_number--;
594 #endif
596 int rec_create_directory(void)
598 int rc = 0;
599 const char * const folder = global_settings.rec_directory;
601 if (strcmp(folder, "/") && !dir_exists(folder))
603 rc = mkdir(folder);
605 if(rc < 0)
607 while (action_userabort(HZ) == false)
609 splashf(0, "%s %s",
610 str(LANG_REC_DIR_NOT_WRITABLE),
611 str(LANG_OFF_ABORT));
614 else
616 rec_status |= RCSTAT_CREATED_DIRECTORY;
617 rc = 1;
621 return rc;
624 void rec_init_recording_options(struct audio_recording_options *options)
626 options->rec_source = global_settings.rec_source;
627 options->rec_frequency = global_settings.rec_frequency;
628 options->rec_channels = global_settings.rec_channels;
629 options->rec_prerecord_time = global_settings.rec_prerecord_time;
630 #if CONFIG_CODEC == SWCODEC
631 options->rec_source_flags = 0;
632 options->enc_config.rec_format = global_settings.rec_format;
633 global_to_encoder_config(&options->enc_config);
634 #else
635 options->rec_quality = global_settings.rec_quality;
636 options->rec_editable = global_settings.rec_editable;
637 #endif
640 #if CONFIG_CODEC == SWCODEC && !defined (SIMULATOR)
641 void rec_set_source(int source, unsigned flags)
643 /* Set audio input source, power up/down devices */
644 audio_set_input_source(source, flags);
646 /* Set peakmeters for recording or reset to playback */
647 peak_meter_playback((flags & SRCF_RECORDING) == 0);
648 peak_meter_enabled = true;
650 #endif /* CONFIG_CODEC == SWCODEC && !defined (SIMULATOR) */
652 void rec_set_recording_options(struct audio_recording_options *options)
654 #if CONFIG_CODEC != SWCODEC
655 if (global_settings.rec_prerecord_time)
657 talk_buffer_steal(); /* will use the mp3 buffer */
659 #else /* == SWCODEC */
660 rec_set_source(options->rec_source,
661 options->rec_source_flags | SRCF_RECORDING);
662 #endif /* CONFIG_CODEC != SWCODEC */
664 audio_set_recording_options(options);
667 void rec_command(enum recording_command cmd)
669 switch(cmd)
671 case RECORDING_CMD_STOP_SHUTDOWN:
672 pm_activate_clipcount(false);
673 audio_stop_recording();
674 #if CONFIG_CODEC == SWCODEC
675 audio_close_recording();
676 #endif
677 sys_poweroff();
678 break;
679 case RECORDING_CMD_STOP:
680 pm_activate_clipcount(false);
681 audio_stop_recording();
682 break;
683 case RECORDING_CMD_START:
684 /* steal mp3 buffer, create unique filename and start recording */
685 pm_reset_clipcount();
686 pm_activate_clipcount(true);
687 #if CONFIG_CODEC != SWCODEC
688 talk_buffer_steal(); /* we use the mp3 buffer */
689 #endif
690 audio_record(rec_create_filename(path_buffer));
691 break;
692 case RECORDING_CMD_START_NEWFILE:
693 /* create unique filename and start recording*/
694 pm_reset_clipcount();
695 pm_activate_clipcount(true); /* just to be sure */
696 audio_new_file(rec_create_filename(path_buffer));
697 break;
698 case RECORDING_CMD_PAUSE:
699 pm_activate_clipcount(false);
700 audio_pause_recording();
701 break;
702 case RECORDING_CMD_RESUME:
703 pm_activate_clipcount(true);
704 audio_resume_recording();
705 break;
707 update_list = true;
710 /* used in trigger_listerner and recording_screen */
711 static unsigned int last_seconds = 0;
714 * Callback function so that the peak meter code can send an event
715 * to this application. This function can be passed to
716 * peak_meter_set_trigger_listener in order to activate the trigger.
718 static void trigger_listener(int trigger_status)
720 switch (trigger_status)
722 case TRIG_GO:
723 if(!(audio_status() & AUDIO_STATUS_RECORD))
725 rec_status |= RCSTAT_HAVE_RECORDED;
726 rec_command(RECORDING_CMD_START);
727 #if CONFIG_CODEC != SWCODEC
728 /* give control to mpeg thread so that it can start
729 recording */
730 yield(); yield(); yield();
731 #endif
734 /* if we're already recording this is a retrigger */
735 else
737 if((audio_status() & AUDIO_STATUS_PAUSE) &&
738 (global_settings.rec_trigger_type == TRIG_TYPE_PAUSE))
740 rec_command(RECORDING_CMD_RESUME);
742 /* New file on trig start*/
743 else if (global_settings.rec_trigger_type != TRIG_TYPE_NEW_FILE)
745 rec_command(RECORDING_CMD_START_NEWFILE);
746 /* tell recording_screen to reset the time */
747 last_seconds = 0;
750 break;
752 /* A _change_ to TRIG_READY means the current recording has stopped */
753 case TRIG_READY:
754 if(audio_status() & AUDIO_STATUS_RECORD)
756 switch(global_settings.rec_trigger_type)
758 case TRIG_TYPE_STOP: /* Stop */
759 rec_command(RECORDING_CMD_STOP);
760 break;
762 case TRIG_TYPE_PAUSE: /* Pause */
763 rec_command(RECORDING_CMD_PAUSE);
764 break;
766 case TRIG_TYPE_NEW_FILE: /* New file on trig stop*/
767 rec_command(RECORDING_CMD_START_NEWFILE);
768 /* tell recording_screen to reset the time */
769 last_seconds = 0;
770 break;
772 case 3: /* Stop and shutdown */
773 rec_command(RECORDING_CMD_STOP_SHUTDOWN);
774 break;
777 if (global_settings.rec_trigger_mode != TRIG_MODE_REARM)
779 peak_meter_set_trigger_listener(NULL);
780 peak_meter_trigger(false);
783 break;
787 /* Stuff for drawing the screen */
789 enum rec_list_items_stereo {
790 ITEM_VOLUME = 0,
791 ITEM_GAIN = 1,
792 ITEM_GAIN_L = 2,
793 ITEM_GAIN_R = 3,
794 #ifdef HAVE_AGC
795 ITEM_AGC_MODE = 4,
796 ITEM_AGC_MAXDB = 5,
797 ITEM_FILENAME = 6,
798 ITEM_COUNT = 7,
799 #else
800 ITEM_FILENAME = 4,
801 ITEM_COUNT = 5,
802 #endif
805 enum rec_list_items_mono {
806 ITEM_VOLUME_M = 0,
807 ITEM_GAIN_M = 1,
808 #ifdef HAVE_AGC
809 ITEM_AGC_MODE_M = 4,
810 ITEM_AGC_MAXDB_M = 5,
811 ITEM_FILENAME_M = 6,
812 ITEM_COUNT_M = 5,
813 #else
814 ITEM_FILENAME_M = 4,
815 ITEM_COUNT_M = 3,
816 #endif
819 static int listid_to_enum[ITEM_COUNT];
821 static char * reclist_get_name(int selected_item, void * data,
822 char * buffer, size_t buffer_len)
824 char buf2[32];
825 #ifdef HAVE_AGC
826 char buf3[32];
827 #endif
828 data = data; /* not used */
829 if(selected_item >= ITEM_COUNT)
830 return "";
832 switch (listid_to_enum[selected_item])
834 case ITEM_VOLUME:
835 snprintf(buffer, buffer_len, "%s: %s", str(LANG_VOLUME),
836 fmt_gain(SOUND_VOLUME,
837 global_settings.volume,
838 buf2, sizeof(buf2)));
839 break;
840 case ITEM_GAIN:
841 if(global_settings.rec_source == AUDIO_SRC_MIC)
843 /* Draw MIC recording gain */
844 snprintf(buffer, buffer_len, "%s: %s", str(LANG_GAIN),
845 fmt_gain(SOUND_MIC_GAIN,
846 global_settings.rec_mic_gain,
847 buf2, sizeof(buf2)));
849 else
851 int avg_gain = (global_settings.rec_left_gain +
852 global_settings.rec_right_gain) / 2;
853 snprintf(buffer, buffer_len, "%s: %s", str(LANG_GAIN),
854 fmt_gain(SOUND_LEFT_GAIN,
855 avg_gain,
856 buf2, sizeof(buf2)));
858 break;
859 case ITEM_GAIN_L:
860 snprintf(buffer, buffer_len, "%s: %s",
861 str(LANG_GAIN_LEFT),
862 fmt_gain(SOUND_LEFT_GAIN,
863 global_settings.rec_left_gain,
864 buf2, sizeof(buf2)));
865 break;
866 case ITEM_GAIN_R:
867 snprintf(buffer, buffer_len, "%s: %s",
868 str(LANG_GAIN_RIGHT),
869 fmt_gain(SOUND_RIGHT_GAIN,
870 global_settings.rec_right_gain,
871 buf2, sizeof(buf2)));
872 break;
873 #ifdef HAVE_AGC
874 case ITEM_AGC_MODE:
875 snprintf(buffer, buffer_len, "%s: %s",
876 str(LANG_RECORDING_AGC_PRESET),
877 agc_preset_str[agc_preset]);
878 break;
879 case ITEM_AGC_MAXDB:
880 if (agc_preset == 0)
881 snprintf(buffer, buffer_len, "%s: %s",
882 str(LANG_RECORDING_AGC_MAXGAIN),
883 fmt_gain(SOUND_LEFT_GAIN,
884 agc_maxgain, buf2, sizeof(buf2)));
885 else if (global_settings.rec_source == AUDIO_SRC_MIC)
886 snprintf(buffer, buffer_len, "%s: %s (%s)",
887 str(LANG_RECORDING_AGC_MAXGAIN),
888 fmt_gain(SOUND_MIC_GAIN,
889 agc_maxgain, buf2, sizeof(buf2)),
890 fmt_gain(SOUND_MIC_GAIN,
891 agc_maxgain - global_settings.rec_mic_gain,
892 buf3, sizeof(buf3)));
893 else
894 snprintf(buffer, buffer_len, "%s: %s (%s)",
895 str(LANG_RECORDING_AGC_MAXGAIN),
896 fmt_gain(SOUND_LEFT_GAIN,
897 agc_maxgain, buf2, sizeof(buf2)),
898 fmt_gain(SOUND_LEFT_GAIN,
899 agc_maxgain -
900 (global_settings.rec_left_gain +
901 global_settings.rec_right_gain)/2,
902 buf3, sizeof(buf3)));
903 break;
904 #endif
905 case ITEM_FILENAME:
907 if(audio_status() & AUDIO_STATUS_RECORD)
909 size_t tot_len = strlen(path_buffer) +
910 strlen(str(LANG_RECORDING_FILENAME)) + 1;
911 if(tot_len > buffer_len)
913 snprintf(buffer, buffer_len, "%s %s",
914 str(LANG_RECORDING_FILENAME),
915 path_buffer + tot_len - buffer_len);
917 else
919 snprintf(buffer, buffer_len, "%s %s",
920 str(LANG_RECORDING_FILENAME), path_buffer);
923 else
925 strncpy(buffer, str(LANG_RECORDING_FILENAME), buffer_len);
927 break;
929 default:
930 return "";
932 return buffer;
936 bool recording_start_automatic = false;
938 bool recording_screen(bool no_source)
940 int button;
941 int done = -1; /* negative to re-init, positive to quit, zero to run */
942 char buf[32]; /* for preparing strings */
943 char buf2[32]; /* for preparing strings */
944 int w, h; /* character width/height */
945 int update_countdown = 0; /* refresh counter */
946 unsigned int seconds;
947 int hours, minutes;
948 int audio_stat = 0; /* status of the audio system */
949 int last_audio_stat = -1; /* previous status so we can act on changes */
950 struct viewport vp_list[NB_SCREENS], vp_top[NB_SCREENS]; /* the viewports */
952 #if CONFIG_CODEC == SWCODEC
953 int warning_counter = 0;
954 #define WARNING_PERIOD 7
955 #endif
957 #ifdef HAVE_FMRADIO_REC
958 /* Radio is left on if:
959 * 1) Is was on at the start and the initial source is FM Radio
960 * 2) 1) and the source was never changed to something else
962 int radio_status = (global_settings.rec_source != AUDIO_SRC_FMRADIO) ?
963 FMRADIO_OFF : get_radio_status();
964 #endif
965 #if (CONFIG_LED == LED_REAL)
966 bool led_state = false;
967 int led_countdown = 2;
968 #endif
969 #ifdef HAVE_AGC
970 bool peak_read = false;
971 bool peak_valid = false;
972 int peak_l, peak_r;
973 int balance = 0;
974 #endif
975 int i;
976 int pm_x[NB_SCREENS]; /* peakmeter (and trigger bar) x pos */
977 int pm_y[NB_SCREENS]; /* peakmeter y pos */
978 int pm_h[NB_SCREENS]; /* peakmeter height */
979 int trig_ypos[NB_SCREENS]; /* trigger bar y pos */
980 int trig_width[NB_SCREENS]; /* trigger bar width */
981 bool compact_view[NB_SCREENS]; /* tweak layout tiny screens / big fonts */
983 struct gui_synclist lists; /* the list in the bottom vp */
984 #ifdef HAVE_FMRADIO_REC
985 int prev_rec_source = global_settings.rec_source; /* detect source change */
986 #endif
988 #if CONFIG_TUNER
989 bool statusbar = global_settings.statusbar;
990 global_status.statusbar_forced = statusbar?0:1;
991 global_settings.statusbar = true;
992 gui_syncstatusbar_draw(&statusbars,true);
993 #endif
995 static const unsigned char *byte_units[] = {
996 ID2P(LANG_BYTE),
997 ID2P(LANG_KILOBYTE),
998 ID2P(LANG_MEGABYTE),
999 ID2P(LANG_GIGABYTE)
1002 struct audio_recording_options rec_options;
1003 rec_status = RCSTAT_IN_RECSCREEN;
1005 #if (CONFIG_LED == LED_REAL) && !defined(SIMULATOR)
1006 ata_set_led_enabled(false);
1007 #endif
1009 #if CONFIG_CODEC == SWCODEC
1010 /* recording_menu gets messed up: so prevent manus talking */
1011 talk_disable(true);
1012 /* audio_init_recording stops anything playing when it takes the audio
1013 buffer */
1014 #else
1015 /* Yes, we use the D/A for monitoring */
1016 peak_meter_enabled = true;
1017 peak_meter_playback(true);
1018 #endif
1020 #ifdef HAVE_AGC
1021 peak_meter_get_peakhold(&peak_l, &peak_r);
1022 #endif
1024 pm_reset_clipcount();
1025 pm_activate_clipcount(false);
1026 settings_apply_trigger();
1028 #ifdef HAVE_AGC
1029 agc_preset_str[0] = str(LANG_OFF);
1030 agc_preset_str[1] = str(LANG_AGC_SAFETY);
1031 agc_preset_str[2] = str(LANG_AGC_LIVE);
1032 agc_preset_str[3] = str(LANG_AGC_DJSET);
1033 agc_preset_str[4] = str(LANG_AGC_MEDIUM);
1034 agc_preset_str[5] = str(LANG_AGC_VOICE);
1035 #endif /* HAVE_AGC */
1037 #if CONFIG_CODEC == SWCODEC
1038 audio_close_recording();
1039 #endif
1040 audio_init_recording(0);
1041 sound_set_volume(global_settings.volume);
1043 #if CONFIG_RTC == 0
1044 /* Create new filename for recording start */
1045 rec_init_filename();
1046 #endif
1048 /* viewport init and calculations that only needs to be done once */
1049 FOR_NB_SCREENS(i)
1051 struct viewport *v;
1052 /* top vp, 4 lines, force sys font if total screen < 6 lines
1053 NOTE: one could limit the list to 1 line and get away with 5 lines */
1054 v = &vp_top[i];
1055 viewport_set_defaults(v, i); /*already takes care of statusbar*/
1056 if (viewport_get_nb_lines(v) < 4)
1058 /* compact needs 4 lines total */
1059 v->font = FONT_SYSFIXED;
1060 compact_view[i] = false;
1062 else
1064 if (viewport_get_nb_lines(v) < (4+2)) /*top=4,list=2*/
1065 compact_view[i] = true;
1066 else
1067 compact_view[i] = false;
1069 v->height = (font_get(v->font)->height)*(compact_view[i] ? 3 : 4);
1071 /* list section, rest of the screen */
1072 v = &vp_list[i];
1073 viewport_set_defaults(v, i);
1074 v->font = vp_top[i].font;
1075 v->y = vp_top[i].y + vp_top[i].height;
1076 v->height = screens[i].lcdheight - v->y; /* the rest */
1077 screens[i].set_viewport(&vp_top[i]); /* req for next calls */
1079 screens[i].getstringsize("W", &w, &h);
1080 pm_y[i] = font_get(vp_top[i].font)->height * 2;
1081 trig_ypos[i] = font_get(vp_top[i].font)->height * 3;
1082 if(compact_view[i])
1083 trig_ypos[i] -= (font_get(vp_top[i].font)->height)/2;
1086 /* init the bottom list */
1087 gui_synclist_init(&lists, reclist_get_name, NULL, false, 1, vp_list);
1088 gui_synclist_set_title(&lists, NULL, Icon_NOICON);
1090 /* start of the loop: we stay in this loop until user quits recscreen */
1091 while(done <= 0)
1093 if(done < 0)
1095 /* request to re-init stuff, done after settings screen */
1096 done = 0;
1097 #ifdef HAVE_FMRADIO_REC
1098 /* If input changes away from FM Radio,
1099 radio will remain off when recording screen closes. */
1100 if (global_settings.rec_source != prev_rec_source
1101 && prev_rec_source == AUDIO_SRC_FMRADIO)
1102 radio_status = FMRADIO_OFF;
1103 prev_rec_source = global_settings.rec_source;
1104 #endif
1106 FOR_NB_SCREENS(i)
1108 pm_x[i] = 0;
1109 if(global_settings.peak_meter_clipcounter)
1111 int clipwidth = 0;
1112 screens[i].getstringsize(str(LANG_PM_CLIPCOUNT),
1113 &clipwidth, &h); /* h is same */
1114 pm_x[i] = clipwidth+1;
1116 if(global_settings.rec_trigger_mode == TRIG_MODE_OFF)
1117 pm_h[i] = font_get(vp_top[i].font)->height * 2;
1118 else
1119 pm_h[i] = font_get(vp_top[i].font)->height;
1120 if(compact_view[i])
1121 pm_h[i] /= 2;
1122 trig_width[i] = vp_top[i].width - pm_x[i];
1123 screens[i].clear_display();
1124 screens[i].update();
1127 #if CONFIG_CODEC == SWCODEC
1128 audio_close_recording();
1129 audio_init_recording(0);
1130 #endif
1132 rec_init_recording_options(&rec_options);
1133 rec_set_recording_options(&rec_options);
1135 if(rec_create_directory() < 0)
1137 rec_status = 0;
1138 goto rec_abort;
1141 #if CONFIG_CODEC == SWCODEC && CONFIG_RTC == 0
1142 /* If format changed, a new number is required */
1143 rec_init_filename();
1144 #endif
1146 #ifdef HAVE_AGC
1147 if (global_settings.rec_source == AUDIO_SRC_MIC) {
1148 agc_preset = global_settings.rec_agc_preset_mic;
1149 agc_maxgain = global_settings.rec_agc_maxgain_mic;
1151 else {
1152 agc_preset = global_settings.rec_agc_preset_line;
1153 agc_maxgain = global_settings.rec_agc_maxgain_line;
1155 #endif
1157 set_gain();
1158 update_countdown = 0; /* Update immediately */
1160 /* populate translation table for list id -> enum */
1161 if(global_settings.rec_source == AUDIO_SRC_MIC)
1163 listid_to_enum[0] = ITEM_VOLUME_M;
1164 listid_to_enum[1] = ITEM_GAIN_M;
1165 #ifdef HAVE_AGC
1166 listid_to_enum[2] = ITEM_AGC_MODE_M;
1167 listid_to_enum[3] = ITEM_AGC_MAXDB_M;
1168 listid_to_enum[4] = ITEM_FILENAME_M;
1169 #else
1170 listid_to_enum[2] = ITEM_FILENAME_M;
1171 #endif
1173 else
1175 listid_to_enum[0] = ITEM_VOLUME;
1176 listid_to_enum[1] = ITEM_GAIN;
1177 listid_to_enum[2] = ITEM_GAIN_L;
1178 listid_to_enum[3] = ITEM_GAIN_R;
1179 #ifdef HAVE_AGC
1180 listid_to_enum[4] = ITEM_AGC_MODE;
1181 listid_to_enum[5] = ITEM_AGC_MAXDB;
1182 listid_to_enum[6] = ITEM_FILENAME;
1183 #else
1184 listid_to_enum[4] = ITEM_FILENAME;
1185 #endif
1188 if(global_settings.rec_source == AUDIO_SRC_MIC)
1189 gui_synclist_set_nb_items(&lists, ITEM_COUNT_M); /* mono */
1190 else
1191 gui_synclist_set_nb_items(&lists, ITEM_COUNT); /* stereo */
1192 gui_synclist_draw(&lists);
1193 } /* if(done < 0) */
1195 audio_stat = audio_status();
1197 #if (CONFIG_LED == LED_REAL)
1200 * Flash the LED while waiting to record. Turn it on while
1201 * recording.
1203 if(audio_stat & AUDIO_STATUS_RECORD)
1205 if (audio_stat & AUDIO_STATUS_PAUSE)
1207 if (--led_countdown <= 0)
1209 led_state = !led_state;
1210 led(led_state);
1211 led_countdown = 2;
1214 else
1216 /* trigger is on in status TRIG_READY (no check needed) */
1217 led(true);
1220 else
1222 int trig_stat = peak_meter_trigger_status();
1224 * other trigger stati than trig_off and trig_steady
1225 * already imply that we are recording.
1227 if (trig_stat == TRIG_STEADY)
1229 if (--led_countdown <= 0)
1231 led_state = !led_state;
1232 led(led_state);
1233 led_countdown = 2;
1236 else
1238 /* trigger is on in status TRIG_READY (no check needed) */
1239 led(false);
1242 #endif /* CONFIG_LED */
1244 /* first set current vp - stays like this for drawing that follows */
1245 FOR_NB_SCREENS(i)
1246 screens[i].set_viewport(&vp_top[i]);
1248 /* Wait for a button a while (HZ/10) drawing the peak meter */
1249 button = peak_meter_draw_get_btn(CONTEXT_RECSCREEN,
1250 pm_x, pm_y, pm_h,
1251 screen_update);
1253 if (last_audio_stat != audio_stat)
1255 if (audio_stat & AUDIO_STATUS_RECORD)
1257 rec_status |= RCSTAT_HAVE_RECORDED;
1259 last_audio_stat = audio_stat;
1260 update_list = true;
1263 if (recording_start_automatic)
1265 /* simulate a button press */
1266 button = ACTION_REC_PAUSE;
1267 recording_start_automatic = false;
1270 /* let list handle the button */
1271 gui_synclist_do_button(&lists, &button, LIST_WRAP_UNLESS_HELD);
1273 /* list code changes active viewport - change it back */
1274 FOR_NB_SCREENS(i)
1275 screens[i].set_viewport(&vp_top[i]);
1277 switch(button)
1279 case ACTION_SETTINGS_INC:
1280 case ACTION_SETTINGS_INCREPEAT:
1281 switch (listid_to_enum[gui_synclist_get_sel_pos(&lists)])
1283 case ITEM_VOLUME:
1284 global_settings.volume++;
1285 setvol();
1286 break;
1287 case ITEM_GAIN:
1288 if(global_settings.rec_source == AUDIO_SRC_MIC)
1290 if(global_settings.rec_mic_gain <
1291 sound_max(SOUND_MIC_GAIN))
1292 global_settings.rec_mic_gain++;
1294 else
1296 if(global_settings.rec_left_gain <
1297 sound_max(SOUND_LEFT_GAIN))
1298 global_settings.rec_left_gain++;
1299 if(global_settings.rec_right_gain <
1300 sound_max(SOUND_RIGHT_GAIN))
1301 global_settings.rec_right_gain++;
1303 break;
1304 case ITEM_GAIN_L:
1305 if(global_settings.rec_left_gain <
1306 sound_max(SOUND_LEFT_GAIN))
1307 global_settings.rec_left_gain++;
1308 break;
1309 case ITEM_GAIN_R:
1310 if(global_settings.rec_right_gain <
1311 sound_max(SOUND_RIGHT_GAIN))
1312 global_settings.rec_right_gain++;
1313 break;
1314 #ifdef HAVE_AGC
1315 case ITEM_AGC_MODE:
1316 agc_preset = MIN(agc_preset + 1, AGC_MODE_SIZE);
1317 agc_enable = (agc_preset != 0);
1318 if (global_settings.rec_source == AUDIO_SRC_MIC) {
1319 global_settings.rec_agc_preset_mic = agc_preset;
1320 agc_maxgain = global_settings.rec_agc_maxgain_mic;
1321 } else {
1322 global_settings.rec_agc_preset_line = agc_preset;
1323 agc_maxgain = global_settings.rec_agc_maxgain_line;
1325 break;
1326 case ITEM_AGC_MAXDB:
1327 if (global_settings.rec_source == AUDIO_SRC_MIC)
1329 agc_maxgain = MIN(agc_maxgain + 1,
1330 sound_max(SOUND_MIC_GAIN));
1331 global_settings.rec_agc_maxgain_mic = agc_maxgain;
1333 else
1335 agc_maxgain = MIN(agc_maxgain + 1,
1336 sound_max(SOUND_LEFT_GAIN));
1337 global_settings.rec_agc_maxgain_line = agc_maxgain;
1339 break;
1340 #endif /* HAVE_AGC */
1342 set_gain();
1343 update_countdown = 0; /* Update immediately */
1344 break;
1345 case ACTION_SETTINGS_DEC:
1346 case ACTION_SETTINGS_DECREPEAT:
1347 switch (listid_to_enum[gui_synclist_get_sel_pos(&lists)])
1349 case ITEM_VOLUME:
1350 global_settings.volume--;
1351 setvol();
1352 break;
1353 case ITEM_GAIN:
1354 if(global_settings.rec_source == AUDIO_SRC_MIC)
1356 if(global_settings.rec_mic_gain >
1357 sound_min(SOUND_MIC_GAIN))
1358 global_settings.rec_mic_gain--;
1360 else
1362 if(global_settings.rec_left_gain >
1363 sound_min(SOUND_LEFT_GAIN))
1364 global_settings.rec_left_gain--;
1365 if(global_settings.rec_right_gain >
1366 sound_min(SOUND_RIGHT_GAIN))
1367 global_settings.rec_right_gain--;
1369 break;
1370 case ITEM_GAIN_L:
1371 if(global_settings.rec_left_gain >
1372 sound_min(SOUND_LEFT_GAIN))
1373 global_settings.rec_left_gain--;
1374 break;
1375 case ITEM_GAIN_R:
1376 if(global_settings.rec_right_gain >
1377 sound_min(SOUND_RIGHT_GAIN))
1378 global_settings.rec_right_gain--;
1379 break;
1380 #ifdef HAVE_AGC
1381 case ITEM_AGC_MODE:
1382 agc_preset = MAX(agc_preset - 1, 0);
1383 agc_enable = (agc_preset != 0);
1384 if (global_settings.rec_source == AUDIO_SRC_MIC) {
1385 global_settings.rec_agc_preset_mic = agc_preset;
1386 agc_maxgain = global_settings.rec_agc_maxgain_mic;
1387 } else {
1388 global_settings.rec_agc_preset_line = agc_preset;
1389 agc_maxgain = global_settings.rec_agc_maxgain_line;
1391 break;
1392 case ITEM_AGC_MAXDB:
1393 if (global_settings.rec_source == AUDIO_SRC_MIC)
1395 agc_maxgain = MAX(agc_maxgain - 1,
1396 sound_min(SOUND_MIC_GAIN));
1397 global_settings.rec_agc_maxgain_mic = agc_maxgain;
1399 else
1401 agc_maxgain = MAX(agc_maxgain - 1,
1402 sound_min(SOUND_LEFT_GAIN));
1403 global_settings.rec_agc_maxgain_line = agc_maxgain;
1405 break;
1406 #endif /* HAVE_AGC */
1408 set_gain();
1409 update_countdown = 0; /* Update immediately */
1410 break;
1411 case ACTION_STD_CANCEL:
1412 /* turn off the trigger */
1413 peak_meter_trigger(false);
1414 peak_meter_set_trigger_listener(NULL);
1416 if(audio_stat & AUDIO_STATUS_RECORD)
1418 rec_command(RECORDING_CMD_STOP);
1420 else
1422 #if CONFIG_CODEC != SWCODEC
1423 peak_meter_playback(true);
1424 peak_meter_enabled = false;
1425 #endif
1426 done = 1;
1428 update_countdown = 0; /* Update immediately */
1429 break;
1430 #ifdef HAVE_REMOTE_LCD
1431 case ACTION_REC_LCD:
1432 /* this feature exists for some h1x0/h3x0 targets that suffer
1433 from noise caused by remote LCD updates
1434 NOTE 1: this will leave the list on the remote
1435 NOTE 2: to be replaced by a global LCD_off() routine */
1436 if(remote_display_on)
1438 /* switch to single screen and put up a splash on the main.
1439 On the remote we put a two line message */
1440 screen_update = 1;
1441 screens[1].clear_viewport();
1442 screens[1].puts(0, 0, str(LANG_REMOTE_LCD_OFF));
1443 screens[1].puts(0, 1, str(LANG_REMOTE_LCD_ON));
1444 screens[1].update_viewport();
1446 else
1448 /* remote switched on again */
1449 update_list = true;
1450 screen_update = NB_SCREENS;
1452 remote_display_on = !remote_display_on; /* toggle */
1453 update_countdown = 0; /* Update immediately */
1454 break;
1455 #endif
1456 case ACTION_REC_PAUSE:
1457 case ACTION_REC_NEWFILE:
1458 /* Only act if the mpeg is stopped */
1459 if(!(audio_stat & AUDIO_STATUS_RECORD))
1461 /* is this manual or triggered recording? */
1462 if ((global_settings.rec_trigger_mode == TRIG_MODE_OFF) ||
1463 (peak_meter_trigger_status() != TRIG_OFF))
1465 /* manual recording */
1466 rec_status |= RCSTAT_HAVE_RECORDED;
1467 rec_command(RECORDING_CMD_START);
1468 last_seconds = 0;
1469 if (global_settings.talk_menu)
1471 /* no voice possible here, but a beep */
1472 audio_beep(HZ/2); /* longer beep on start */
1475 /* this is triggered recording */
1476 else
1478 /* we don't start recording now, but enable the
1479 trigger and let the callback function
1480 trigger_listener control when the recording starts */
1481 peak_meter_trigger(true);
1482 peak_meter_set_trigger_listener(&trigger_listener);
1485 else
1487 /*if new file button pressed, start new file */
1488 if (button == ACTION_REC_NEWFILE)
1490 rec_command(RECORDING_CMD_START_NEWFILE);
1491 last_seconds = 0;
1493 else
1494 /* if pause button pressed, pause or resume */
1496 if(audio_stat & AUDIO_STATUS_PAUSE)
1498 rec_command(RECORDING_CMD_RESUME);
1499 if (global_settings.talk_menu)
1501 /* no voice possible here, but a beep */
1502 audio_beep(HZ/4); /* short beep on resume */
1505 else
1507 rec_command(RECORDING_CMD_PAUSE);
1511 update_countdown = 0; /* Update immediately */
1512 break;
1513 case ACTION_STD_MENU:
1514 #if CONFIG_CODEC == SWCODEC
1515 if(!(audio_stat & AUDIO_STATUS_RECORD))
1516 #else
1517 if(audio_stat != AUDIO_STATUS_RECORD)
1518 #endif
1520 #if (CONFIG_LED == LED_REAL)
1521 /* led is restored at begin of loop / end of function */
1522 led(false);
1523 #endif
1524 if (recording_menu(no_source))
1526 done = 1;
1527 rec_status |= RCSTAT_BEEN_IN_USB_MODE;
1528 #ifdef HAVE_FMRADIO_REC
1529 radio_status = FMRADIO_OFF;
1530 #endif
1532 else
1534 done = -1;
1535 /* the init is now done at the beginning of the loop */
1538 break;
1540 #if CONFIG_KEYPAD == RECORDER_PAD
1541 case ACTION_REC_F2:
1542 if(audio_stat != AUDIO_STATUS_RECORD)
1544 #if (CONFIG_LED == LED_REAL)
1545 /* led is restored at begin of loop / end of function */
1546 led(false);
1547 #endif
1548 if (f2_rec_screen())
1550 rec_status |= RCSTAT_HAVE_RECORDED;
1551 done = true;
1553 else
1554 update_countdown = 0; /* Update immediately */
1556 break;
1558 case ACTION_REC_F3:
1559 if(audio_stat & AUDIO_STATUS_RECORD)
1561 rec_command(RECORDING_CMD_START_NEWFILE);
1562 last_seconds = 0;
1564 else
1566 #if (CONFIG_LED == LED_REAL)
1567 /* led is restored at begin of loop / end of function */
1568 led(false);
1569 #endif
1570 if (f3_rec_screen())
1572 rec_status |= RCSTAT_HAVE_RECORDED;
1573 done = true;
1575 else
1576 update_countdown = 0; /* Update immediately */
1578 break;
1579 #endif /* CONFIG_KEYPAD == RECORDER_PAD */
1581 case SYS_USB_CONNECTED:
1582 /* Only accept USB connection when not recording */
1583 if(!(audio_stat & AUDIO_STATUS_RECORD))
1585 FOR_NB_SCREENS(i)
1586 screens[i].set_viewport(NULL);
1587 default_event_handler(SYS_USB_CONNECTED);
1588 done = true;
1589 rec_status |= RCSTAT_BEEN_IN_USB_MODE;
1590 #ifdef HAVE_FMRADIO_REC
1591 radio_status = FMRADIO_OFF;
1592 #endif
1594 break;
1595 } /*switch(button)*/
1597 #ifdef HAVE_AGC
1598 peak_read = !peak_read;
1599 if (peak_read) { /* every 2nd run of loop */
1600 peak_time++;
1601 peak_valid = read_peak_levels(&peak_l, &peak_r, &balance);
1604 /* Handle AGC every 200ms when enabled and peak data is valid */
1605 if (peak_read && agc_enable && peak_valid)
1606 auto_gain_control(&peak_l, &peak_r, &balance);
1607 #endif
1609 seconds = audio_recorded_time() / HZ;
1611 /* start of vp_top drawing */
1612 if(update_countdown-- == 0 || seconds > last_seconds)
1614 unsigned int dseconds, dhours, dminutes;
1615 unsigned long num_recorded_bytes, dsize, dmb;
1617 /* we assume vp_top is the current viewport! */
1618 FOR_NB_ACTIVE_SCREENS(i)
1619 screens[i].clear_viewport();
1621 update_countdown = 5;
1622 last_seconds = seconds;
1624 dseconds = rec_timesplit_seconds();
1625 dsize = rec_sizesplit_bytes();
1626 num_recorded_bytes = audio_num_recorded_bytes();
1628 #if CONFIG_CODEC == SWCODEC
1629 if ((audio_stat & AUDIO_STATUS_WARNING)
1630 && (warning_counter++ % WARNING_PERIOD) < WARNING_PERIOD/2)
1632 /* Switch back and forth displaying warning on first available
1633 line to ensure visibility - the motion should also help
1634 draw attention */
1635 /* Don't use language string unless agreed upon to make this
1636 method permanent - could do something in the statusbar */
1637 snprintf(buf, sizeof(buf), "Warning: %08X",
1638 pcm_rec_get_warnings());
1640 else
1641 #endif /* CONFIG_CODEC == SWCODEC */
1642 if ((global_settings.rec_sizesplit) &&
1643 (global_settings.rec_split_method))
1645 dmb = dsize/1024/1024;
1646 snprintf(buf, sizeof(buf), "%s %dMB",
1647 str(LANG_SPLIT_SIZE), dmb);
1649 else
1651 hours = seconds / 3600;
1652 minutes = (seconds - (hours * 3600)) / 60;
1653 snprintf(buf, sizeof(buf), "%s %02d:%02d:%02d",
1654 str(LANG_RECORDING_TIME),
1655 hours, minutes, seconds%60);
1658 FOR_NB_ACTIVE_SCREENS(i)
1659 screens[i].puts(0, 0, buf);
1661 if(audio_stat & AUDIO_STATUS_PRERECORD)
1663 snprintf(buf, sizeof(buf), "%s...",
1664 str(LANG_RECORD_PRERECORD));
1666 else
1668 /* Display the split interval if the record timesplit
1669 is active */
1670 if ((global_settings.rec_timesplit) &&
1671 !(global_settings.rec_split_method))
1673 /* Display the record timesplit interval rather
1674 than the file size if the record timer is active */
1675 dhours = dseconds / 3600;
1676 dminutes = (dseconds - (dhours * 3600)) / 60;
1677 snprintf(buf, sizeof(buf), "%s %02d:%02d",
1678 str(LANG_RECORDING_TIMESPLIT_REC),
1679 dhours, dminutes);
1681 else
1683 output_dyn_value(buf2, sizeof buf2,
1684 num_recorded_bytes,
1685 byte_units, true);
1686 snprintf(buf, sizeof(buf), "%s %s",
1687 str(LANG_RECORDING_SIZE), buf2);
1691 FOR_NB_ACTIVE_SCREENS(i)
1692 screens[i].puts(0, 1, buf);
1694 /* We will do file splitting regardless, either at the end of
1695 a split interval, or when the filesize approaches the 2GB
1696 FAT file size (compatibility) limit. */
1697 if ((audio_stat && !(global_settings.rec_split_method)
1698 && global_settings.rec_timesplit && (seconds >= dseconds))
1699 || (audio_stat && global_settings.rec_split_method
1700 && global_settings.rec_sizesplit
1701 && (num_recorded_bytes >= dsize))
1702 || (num_recorded_bytes >= MAX_FILE_SIZE))
1704 if (!(global_settings.rec_split_type)
1705 || (num_recorded_bytes >= MAX_FILE_SIZE))
1707 rec_command(RECORDING_CMD_START_NEWFILE);
1708 last_seconds = 0;
1710 else
1712 peak_meter_trigger(false);
1713 peak_meter_set_trigger_listener(NULL);
1714 if( global_settings.rec_split_type == 1)
1715 rec_command(RECORDING_CMD_STOP);
1716 else
1717 rec_command(RECORDING_CMD_STOP_SHUTDOWN);
1719 update_countdown = 0;
1722 /* draw the clipcounter just in front of the peakmeter */
1723 if(global_settings.peak_meter_clipcounter)
1725 char clpstr[32];
1726 snprintf(clpstr, 32, "%4d", pm_get_clipcount());
1727 FOR_NB_ACTIVE_SCREENS(i)
1729 if(!compact_view[i])
1730 screens[i].puts(0, 2,str(LANG_PM_CLIPCOUNT));
1731 screens[i].puts(0, compact_view[i] ? 2 : 3, clpstr);
1735 #ifdef HAVE_AGC
1736 hist_time++;
1737 #endif
1739 /* draw the trigger status */
1740 if (peak_meter_trigger_status() != TRIG_OFF)
1742 peak_meter_draw_trig(pm_x, trig_ypos, trig_width,
1743 screen_update);
1744 FOR_NB_ACTIVE_SCREENS(i)
1745 screens[i].update_viewport_rect(pm_x[i], trig_ypos[i],
1746 trig_width[i] + 2, TRIG_HEIGHT);
1749 #ifdef HAVE_AGC
1750 if (global_settings.rec_source == AUDIO_SRC_MIC)
1752 if(agc_maxgain < (global_settings.rec_mic_gain))
1753 change_recording_gain(false, true, true);
1755 else
1757 if(agc_maxgain < (global_settings.rec_left_gain))
1758 change_recording_gain(false, true, false);
1759 if(agc_maxgain < (global_settings.rec_right_gain))
1760 change_recording_gain(false, false, true);
1762 #endif /* HAVE_AGC */
1764 if(update_list)
1766 /* update_list is set whenever content changes */
1767 update_list = false;
1768 gui_synclist_draw(&lists);
1771 /* draw peakmeter again (check if this can be removed) */
1772 FOR_NB_ACTIVE_SCREENS(i)
1774 screens[i].set_viewport(NULL);
1775 gui_statusbar_draw(&(statusbars.statusbars[i]), true);
1776 screens[i].set_viewport(&vp_top[i]);
1777 peak_meter_screen(&screens[i], pm_x[i], pm_y[i], pm_h[i]);
1778 screens[i].update();
1780 } /* display update every second */
1782 if(audio_stat & AUDIO_STATUS_ERROR)
1784 done = true;
1786 } /* end while(!done) */
1788 audio_stat = audio_status();
1789 if (audio_stat & AUDIO_STATUS_ERROR)
1791 splash(0, str(LANG_DISK_FULL));
1792 gui_syncstatusbar_draw(&statusbars, true);
1794 FOR_NB_SCREENS(i)
1795 screens[i].update();
1797 #if CONFIG_CODEC == SWCODEC
1798 /* stop recording - some players like H10 freeze otherwise
1799 TO DO: find out why it freezes and fix properly */
1800 rec_command(RECORDING_CMD_STOP);
1801 audio_close_recording();
1802 #endif
1804 audio_error_clear();
1806 while(1)
1808 if (action_userabort(TIMEOUT_NOBLOCK))
1809 break;
1813 rec_abort:
1815 #if CONFIG_CODEC == SWCODEC
1816 rec_command(RECORDING_CMD_STOP);
1817 audio_close_recording();
1819 #ifdef HAVE_FMRADIO_REC
1820 if (radio_status != FMRADIO_OFF)
1821 /* Restore radio playback - radio_status should be unchanged if started
1822 through fm radio screen (barring usb connect) */
1823 rec_set_source(AUDIO_SRC_FMRADIO, (radio_status & FMRADIO_PAUSED) ?
1824 SRCF_FMRADIO_PAUSED : SRCF_FMRADIO_PLAYING);
1825 else
1826 #endif
1827 /* Go back to playback mode */
1828 rec_set_source(AUDIO_SRC_PLAYBACK, SRCF_PLAYBACK);
1830 /* restore talking */
1831 talk_disable(false);
1832 #else /* !SWCODEC */
1833 audio_init_playback();
1834 #endif /* CONFIG_CODEC == SWCODEC */
1836 /* make sure the trigger is really turned off */
1837 peak_meter_trigger(false);
1838 peak_meter_set_trigger_listener(NULL);
1840 rec_status &= ~RCSTAT_IN_RECSCREEN;
1841 sound_settings_apply();
1843 FOR_NB_SCREENS(i)
1844 screens[i].setfont(FONT_UI);
1846 /* if the directory was created or recording happened, make sure the
1847 browser is updated */
1848 if (rec_status & (RCSTAT_CREATED_DIRECTORY | RCSTAT_HAVE_RECORDED))
1849 reload_directory();
1851 #if (CONFIG_LED == LED_REAL) && !defined(SIMULATOR)
1852 ata_set_led_enabled(true);
1853 #endif
1855 #if CONFIG_TUNER
1856 global_settings.statusbar = statusbar;
1857 global_status.statusbar_forced = 0;
1858 #endif
1860 settings_save();
1862 return (rec_status & RCSTAT_BEEN_IN_USB_MODE) != 0;
1863 } /* recording_screen */
1865 #if CONFIG_KEYPAD == RECORDER_PAD
1866 static bool f2_rec_screen(void)
1868 static const char* const freq_str[6] =
1870 "44.1kHz",
1871 "48kHz",
1872 "32kHz",
1873 "22.05kHz",
1874 "24kHz",
1875 "16kHz"
1878 bool exit = false;
1879 bool used = false;
1880 int w, h, i;
1881 char buf[32];
1882 int button;
1883 struct audio_recording_options rec_options;
1885 FOR_NB_SCREENS(i)
1887 screens[i].setfont(FONT_SYSFIXED);
1888 screens[i].getstringsize("A",&w,&h);
1891 while (!exit) {
1892 const char* ptr=NULL;
1894 FOR_NB_SCREENS(i)
1896 screens[i].clear_display();
1898 /* Recording quality */
1899 screens[i].putsxy(0, LCD_HEIGHT/2 - h*2,
1900 str(LANG_SYSFONT_RECORDING_QUALITY));
1903 snprintf(buf, sizeof(buf), "%d", global_settings.rec_quality);
1904 FOR_NB_SCREENS(i)
1906 screens[i].putsxy(0, LCD_HEIGHT/2-h, buf);
1907 screens[i].mono_bitmap(bitmap_icons_7x8[Icon_FastBackward],
1908 LCD_WIDTH/2 - 16, LCD_HEIGHT/2 - 4, 7, 8);
1911 /* Frequency */
1912 snprintf(buf, sizeof buf, "%s:", str(LANG_SYSFONT_RECORDING_FREQUENCY));
1913 ptr = freq_str[global_settings.rec_frequency];
1914 FOR_NB_SCREENS(i)
1916 screens[i].getstringsize(buf,&w,&h);
1917 screens[i].putsxy((LCD_WIDTH-w)/2, LCD_HEIGHT - h*2, buf);
1918 screens[i].getstringsize(ptr, &w, &h);
1919 screens[i].putsxy((LCD_WIDTH-w)/2, LCD_HEIGHT - h, ptr);
1920 screens[i].mono_bitmap(bitmap_icons_7x8[Icon_DownArrow],
1921 LCD_WIDTH/2 - 3, LCD_HEIGHT - h*3, 7, 8);
1924 /* Channel mode */
1925 switch ( global_settings.rec_channels ) {
1926 case 0:
1927 ptr = str(LANG_SYSFONT_CHANNEL_STEREO);
1928 break;
1930 case 1:
1931 ptr = str(LANG_SYSFONT_CHANNEL_MONO);
1932 break;
1935 FOR_NB_SCREENS(i)
1937 screens[i].getstringsize(str(LANG_SYSFONT_CHANNELS), &w, &h);
1938 screens[i].putsxy(LCD_WIDTH - w, LCD_HEIGHT/2 - h*2,
1939 str(LANG_SYSFONT_CHANNELS));
1940 screens[i].getstringsize(str(LANG_SYSFONT_MODE), &w, &h);
1941 screens[i].putsxy(LCD_WIDTH - w, LCD_HEIGHT/2 - h,
1942 str(LANG_SYSFONT_MODE));
1943 screens[i].getstringsize(ptr, &w, &h);
1944 screens[i].putsxy(LCD_WIDTH - w, LCD_HEIGHT/2, ptr);
1945 screens[i].mono_bitmap(bitmap_icons_7x8[Icon_FastForward],
1946 LCD_WIDTH/2 + 8, LCD_HEIGHT/2 - 4, 7, 8);
1948 screens[i].update();
1951 button = button_get(true);
1952 switch (button) {
1953 case BUTTON_LEFT:
1954 case BUTTON_F2 | BUTTON_LEFT:
1955 global_settings.rec_quality++;
1956 if(global_settings.rec_quality > 7)
1957 global_settings.rec_quality = 0;
1958 used = true;
1959 break;
1961 case BUTTON_DOWN:
1962 case BUTTON_F2 | BUTTON_DOWN:
1963 global_settings.rec_frequency++;
1964 if(global_settings.rec_frequency > 5)
1965 global_settings.rec_frequency = 0;
1966 used = true;
1967 break;
1969 case BUTTON_RIGHT:
1970 case BUTTON_F2 | BUTTON_RIGHT:
1971 global_settings.rec_channels++;
1972 if(global_settings.rec_channels > 1)
1973 global_settings.rec_channels = 0;
1974 used = true;
1975 break;
1977 case BUTTON_F2 | BUTTON_REL:
1978 if ( used )
1979 exit = true;
1980 used = true;
1981 break;
1983 case BUTTON_F2 | BUTTON_REPEAT:
1984 used = true;
1985 break;
1987 default:
1988 if(default_event_handler(button) == SYS_USB_CONNECTED)
1989 return true;
1990 break;
1994 rec_init_recording_options(&rec_options);
1995 rec_set_recording_options(&rec_options);
1997 set_gain();
1999 settings_save();
2000 FOR_NB_SCREENS(i)
2001 screens[i].setfont(FONT_UI);
2003 return false;
2006 static bool f3_rec_screen(void)
2008 bool exit = false;
2009 bool used = false;
2010 int w, h, i;
2011 int button;
2012 char *src_str[] =
2014 str(LANG_SYSFONT_RECORDING_SRC_MIC),
2015 str(LANG_SYSFONT_LINE_IN),
2016 str(LANG_SYSFONT_RECORDING_SRC_DIGITAL)
2018 struct audio_recording_options rec_options;
2020 FOR_NB_SCREENS(i)
2022 screens[i].setfont(FONT_SYSFIXED);
2023 screens[i].getstringsize("A",&w,&h);
2026 while (!exit) {
2027 char* ptr=NULL;
2028 ptr = src_str[global_settings.rec_source];
2029 FOR_NB_SCREENS(i)
2031 screens[i].clear_display();
2033 /* Recording source */
2034 screens[i].putsxy(0, LCD_HEIGHT/2 - h*2,
2035 str(LANG_SYSFONT_RECORDING_SOURCE));
2037 screens[i].getstringsize(ptr, &w, &h);
2038 screens[i].putsxy(0, LCD_HEIGHT/2-h, ptr);
2039 screens[i].mono_bitmap(bitmap_icons_7x8[Icon_FastBackward],
2040 LCD_WIDTH/2 - 16, LCD_HEIGHT/2 - 4, 7, 8);
2043 /* trigger setup */
2044 ptr = str(LANG_SYSFONT_RECORD_TRIGGER);
2045 FOR_NB_SCREENS(i)
2047 screens[i].getstringsize(ptr,&w,&h);
2048 screens[i].putsxy((LCD_WIDTH-w)/2, LCD_HEIGHT - h*2, ptr);
2049 screens[i].mono_bitmap(bitmap_icons_7x8[Icon_DownArrow],
2050 LCD_WIDTH/2 - 3, LCD_HEIGHT - h*3, 7, 8);
2052 screens[i].update();
2055 button = button_get(true);
2056 switch (button) {
2057 case BUTTON_DOWN:
2058 case BUTTON_F3 | BUTTON_DOWN:
2059 #ifndef SIMULATOR
2060 rectrigger();
2061 settings_apply_trigger();
2062 #endif
2063 exit = true;
2064 break;
2066 case BUTTON_LEFT:
2067 case BUTTON_F3 | BUTTON_LEFT:
2068 global_settings.rec_source++;
2069 if(global_settings.rec_source > AUDIO_SRC_MAX)
2070 global_settings.rec_source = 0;
2071 used = true;
2072 break;
2074 case BUTTON_F3 | BUTTON_REL:
2075 if ( used )
2076 exit = true;
2077 used = true;
2078 break;
2080 case BUTTON_F3 | BUTTON_REPEAT:
2081 used = true;
2082 break;
2084 default:
2085 if(default_event_handler(button) == SYS_USB_CONNECTED)
2086 return true;
2087 break;
2091 rec_init_recording_options(&rec_options);
2092 rec_set_recording_options(&rec_options);
2094 set_gain();
2096 settings_save();
2097 FOR_NB_SCREENS(i)
2098 screens[i].setfont(FONT_UI);
2100 return false;
2102 #endif /* CONFIG_KEYPAD == RECORDER_PAD */
2104 #if CONFIG_CODEC == SWCODEC
2105 void audio_beep(int duration)
2107 /* dummy */
2108 (void)duration;
2111 #ifdef SIMULATOR
2112 /* stubs for recording sim */
2113 void audio_init_recording(unsigned int buffer_offset)
2115 buffer_offset = buffer_offset;
2118 void audio_close_recording(void)
2122 unsigned long pcm_rec_get_warnings(void)
2124 return 0;
2127 unsigned long audio_recorded_time(void)
2129 return 123;
2132 unsigned long audio_num_recorded_bytes(void)
2134 return 5 * 1024 * 1024;
2137 void rec_set_source(int source, unsigned flags)
2139 source = source;
2140 flags = flags;
2143 void audio_set_recording_options(struct audio_recording_options *options)
2145 options = options;
2148 void audio_set_recording_gain(int left, int right, int type)
2150 left = left;
2151 right = right;
2152 type = type;
2155 void audio_record(const char *filename)
2157 filename = filename;
2160 void audio_new_file(const char *filename)
2162 filename = filename;
2165 void audio_stop_recording(void)
2169 void audio_pause_recording(void)
2173 void audio_resume_recording(void)
2177 #endif /* #ifdef SIMULATOR */
2178 #endif /* #ifdef CONFIG_CODEC == SWCODEC */
2180 #endif /* HAVE_RECORDING */