The install window doesn't need to be wider than the other ones.
[Rockbox.git] / apps / recorder / recording.c
blob053eecd844a67c8d006724133cf7227c803d96a9
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "config.h"
22 #include <stdio.h>
23 #include <stdbool.h>
24 #include <stdlib.h>
26 #include "system.h"
27 #include "power.h"
28 #include "lcd.h"
29 #include "led.h"
30 #include "mpeg.h"
31 #include "audio.h"
32 #if CONFIG_CODEC == SWCODEC
33 #include "thread.h"
34 #include "pcm_playback.h"
35 #include "playback.h"
36 #include "enc_config.h"
37 #if defined(HAVE_SPDIF_IN) || defined(HAVE_SPDIF_OUT)
38 #include "spdif.h"
39 #endif
40 #endif /* CONFIG_CODEC == SWCODEC */
41 #include "recording.h"
42 #include "mp3_playback.h"
43 #include "mas.h"
44 #include "button.h"
45 #include "kernel.h"
46 #include "settings.h"
47 #include "lang.h"
48 #include "font.h"
49 #include "icons.h"
50 #include "icon.h"
51 #include "screens.h"
52 #include "peakmeter.h"
53 #include "statusbar.h"
54 #include "menu.h"
55 #include "sound_menu.h"
56 #include "timefuncs.h"
57 #include "debug.h"
58 #include "misc.h"
59 #include "tree.h"
60 #include "string.h"
61 #include "dir.h"
62 #include "errno.h"
63 #include "talk.h"
64 #include "atoi.h"
65 #include "sound.h"
66 #include "ata.h"
67 #include "splash.h"
68 #include "screen_access.h"
69 #include "action.h"
70 #include "radio.h"
71 #ifdef HAVE_RECORDING
72 /* This array holds the record timer interval lengths, in seconds */
73 static const unsigned long rec_timer_seconds[] =
75 0, /* 0 means OFF */
76 5*60, /* 00:05 */
77 10*60, /* 00:10 */
78 15*60, /* 00:15 */
79 30*60, /* 00:30 */
80 60*60, /* 01:00 */
81 74*60, /* 74:00 */
82 80*60, /* 80:00 */
83 2*60*60, /* 02:00 */
84 4*60*60, /* 04:00 */
85 6*60*60, /* 06:00 */
86 8*60*60, /* 08:00 */
87 10L*60*60, /* 10:00 */
88 12L*60*60, /* 12:00 */
89 18L*60*60, /* 18:00 */
90 24L*60*60 /* 24:00 */
93 static unsigned int rec_timesplit_seconds(void)
95 return rec_timer_seconds[global_settings.rec_timesplit];
98 /* This array holds the record size interval lengths, in bytes */
99 static const unsigned long rec_size_bytes[] =
101 0, /* 0 means OFF */
102 5*1024*1024, /* 5MB */
103 10*1024*1024, /* 10MB */
104 15*1024*1024, /* 15MB */
105 32*1024*1024, /* 32MB */
106 64*1024*1024, /* 64MB */
107 75*1024*1024, /* 75MB */
108 100*1024*1024, /* 100MB */
109 128*1024*1024, /* 128MB */
110 256*1024*1024, /* 256MB */
111 512*1024*1024, /* 512MB */
112 650*1024*1024, /* 650MB */
113 700*1024*1024, /* 700MB */
114 1024*1024*1024, /* 1GB */
115 1536*1024*1024, /* 1.5GB */
116 1792*1024*1024, /* 1.75GB */
119 static unsigned long rec_sizesplit_bytes(void)
121 return rec_size_bytes[global_settings.rec_sizesplit];
124 * Time strings used for the trigger durations.
125 * Keep synchronous to trigger_times in settings_apply_trigger
127 const char * const trig_durations[TRIG_DURATION_COUNT] =
129 "0s", "1s", "2s", "5s",
130 "10s", "15s", "20s", "25s", "30s",
131 "1min", "2min", "5min", "10min"
134 void settings_apply_trigger(void)
136 /* Keep synchronous to trig_durations and trig_durations_conf*/
137 static const long trigger_times[TRIG_DURATION_COUNT] = {
138 0, HZ, 2*HZ, 5*HZ,
139 10*HZ, 15*HZ, 20*HZ, 25*HZ, 30*HZ,
140 60*HZ, 2*60*HZ, 5*60*HZ, 10*60*HZ
143 peak_meter_define_trigger(
144 global_settings.rec_start_thres,
145 trigger_times[global_settings.rec_start_duration],
146 MIN(trigger_times[global_settings.rec_start_duration] / 2, 2*HZ),
147 global_settings.rec_stop_thres,
148 trigger_times[global_settings.rec_stop_postrec],
149 trigger_times[global_settings.rec_stop_gap]
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 #define PM_HEIGHT ((LCD_HEIGHT >= 72) ? 2 : 1)
170 #if CONFIG_KEYPAD == RECORDER_PAD
171 static bool f2_rec_screen(void);
172 static bool f3_rec_screen(void);
173 #endif
175 #define MAX_FILE_SIZE 0x7F800000 /* 2 GB - 4 MB */
177 static int screen_update = NB_SCREENS;
178 #ifdef HAVE_REMOTE_LCD
179 static bool remote_display_on = true;
180 #endif
182 /** File name creation **/
183 #if CONFIG_RTC == 0
184 /* current file number to assist in creating unique numbered filenames
185 without actually having to create the file on disk */
186 static int file_number = -1;
187 #endif /* CONFIG_RTC */
189 #if CONFIG_CODEC == SWCODEC
191 #define REC_FILE_ENDING(rec_format) \
192 (audio_formats[rec_format_afmt[rec_format]].ext_list)
194 #else /* CONFIG_CODEC != SWCODEC */
196 /* default record file extension for HWCODEC */
197 #define REC_FILE_ENDING(rec_format) \
198 (audio_formats[AFMT_MPA_L3].ext_list)
200 #endif /* CONFIG_CODEC == SWCODEC */
202 /* path for current file */
203 static char path_buffer[MAX_PATH];
205 /** Automatic Gain Control (AGC) **/
206 #ifdef HAVE_AGC
207 /* Timing counters:
208 * peak_time is incremented every 0.2s, every 2nd run of record screen loop.
209 * hist_time is incremented every 0.5s, display update.
210 * peak_time is the counter of the peak hold read and agc process,
211 * overflow every 13 years 8-)
213 static long peak_time = 0;
214 static long hist_time = 0;
216 static short peak_valid_mem[4];
217 #define BAL_MEM_SIZE 24
218 static short balance_mem[BAL_MEM_SIZE];
220 /* Automatic Gain Control */
221 #define AGC_MODE_SIZE 5
222 #define AGC_SAFETY_MODE 0
224 static const char* agc_preset_str[] =
225 { "Off", "S", "L", "D", "M", "V" };
226 /* "Off",
227 "Safety (clip)",
228 "Live (slow)",
229 "DJ-Set (slow)",
230 "Medium",
231 "Voice (fast)" */
232 #define AGC_CLIP 32766
233 #define AGC_PEAK 29883 /* fast gain reduction threshold -0.8dB */
234 #define AGC_HIGH 27254 /* accelerated gain reduction threshold -1.6dB */
235 #define AGC_IMG 823 /* threshold for balance control -32dB */
236 /* autogain high level thresholds (-3dB, -7dB, -4dB, -5dB, -5dB) */
237 static const short agc_th_hi[AGC_MODE_SIZE] =
238 { 23197, 14637, 21156, 18428, 18426 };
239 /* autogain low level thresholds (-14dB, -11dB, -6dB, -7dB, -8dB) */
240 static const short agc_th_lo[AGC_MODE_SIZE] =
241 { 6538, 9235, 16422, 14636, 13045 };
242 /* autogain threshold times [1/5s] or [200ms] */
243 static const short agc_tdrop[AGC_MODE_SIZE] =
244 { 900, 225, 150, 60, 8 };
245 static const short agc_trise[AGC_MODE_SIZE] =
246 { 9000, 750, 400, 150, 20 };
247 static const short agc_tbal[AGC_MODE_SIZE] =
248 { 4500, 500, 300, 100, 15 };
249 /* AGC operation */
250 static bool agc_enable = true;
251 static short agc_preset;
252 /* AGC levels */
253 static int agc_left = 0;
254 static int agc_right = 0;
255 /* AGC time since high target volume was exceeded */
256 static short agc_droptime = 0;
257 /* AGC time since volume fallen below low target */
258 static short agc_risetime = 0;
259 /* AGC balance time exceeding +/- 0.7dB */
260 static short agc_baltime = 0;
261 /* AGC maximum gain */
262 static short agc_maxgain;
263 #endif /* HAVE_AGC */
265 static void set_gain(void)
267 if(global_settings.rec_source == AUDIO_SRC_MIC)
269 audio_set_recording_gain(global_settings.rec_mic_gain,
270 0, AUDIO_GAIN_MIC);
272 else
274 /* AUDIO_SRC_LINEIN, AUDIO_SRC_FMRADIO, AUDIO_SRC_SPDIF */
275 audio_set_recording_gain(global_settings.rec_left_gain,
276 global_settings.rec_right_gain,
277 AUDIO_GAIN_LINEIN);
281 #ifdef HAVE_AGC
282 /* Read peak meter values & calculate balance.
283 * Returns validity of peak values.
284 * Used for automatic gain control and history diagram.
286 static bool read_peak_levels(int *peak_l, int *peak_r, int *balance)
288 peak_meter_get_peakhold(peak_l, peak_r);
289 peak_valid_mem[peak_time % 3] = *peak_l;
290 if (((peak_valid_mem[0] == peak_valid_mem[1]) &&
291 (peak_valid_mem[1] == peak_valid_mem[2])) &&
292 ((*peak_l < 32767)
293 #ifndef SIMULATOR
294 || ata_disk_is_active()
295 #endif
297 return false;
299 if (*peak_r > *peak_l)
300 balance_mem[peak_time % BAL_MEM_SIZE] = (*peak_l ?
301 MIN((10000 * *peak_r) / *peak_l - 10000, 15118) : 15118);
302 else
303 balance_mem[peak_time % BAL_MEM_SIZE] = (*peak_r ?
304 MAX(10000 - (10000 * *peak_l) / *peak_r, -15118) : -15118);
305 *balance = 0;
306 int i;
307 for (i = 0; i < BAL_MEM_SIZE; i++)
308 *balance += balance_mem[i];
309 *balance = *balance / BAL_MEM_SIZE;
311 return true;
314 /* AGC helper function to check if maximum gain is reached */
315 static bool agc_gain_is_max(bool left, bool right)
317 /* range -128...+108 [0.5dB] */
318 short gain_current_l;
319 short gain_current_r;
321 if (agc_preset == 0)
322 return false;
324 switch (global_settings.rec_source)
326 HAVE_LINE_REC_(case AUDIO_SRC_LINEIN:)
327 HAVE_FMRADIO_REC_(case AUDIO_SRC_FMRADIO:)
328 gain_current_l = global_settings.rec_left_gain;
329 gain_current_r = global_settings.rec_right_gain;
330 break;
331 case AUDIO_SRC_MIC:
332 default:
333 gain_current_l = global_settings.rec_mic_gain;
334 gain_current_r = global_settings.rec_mic_gain;
337 return ((left && (gain_current_l >= agc_maxgain)) ||
338 (right && (gain_current_r >= agc_maxgain)));
341 static void change_recording_gain(bool increment, bool left, bool right)
343 int factor = (increment ? 1 : -1);
345 switch (global_settings.rec_source)
347 HAVE_LINE_REC_(case AUDIO_SRC_LINEIN:)
348 HAVE_FMRADIO_REC_(case AUDIO_SRC_FMRADIO:)
349 if (left) global_settings.rec_left_gain += factor;
350 if (right) global_settings.rec_right_gain += factor;
351 break;
352 case AUDIO_SRC_MIC:
353 global_settings.rec_mic_gain += factor;
358 * Handle automatic gain control (AGC).
359 * Change recording gain if peak_x levels are above or below
360 * target volume for specified timeouts.
362 static void auto_gain_control(int *peak_l, int *peak_r, int *balance)
364 int agc_mono;
365 short agc_mode;
366 bool increment;
368 if (*peak_l > agc_left)
369 agc_left = *peak_l;
370 else
371 agc_left -= (agc_left - *peak_l + 3) >> 2;
372 if (*peak_r > agc_right)
373 agc_right = *peak_r;
374 else
375 agc_right -= (agc_right - *peak_r + 3) >> 2;
376 agc_mono = (agc_left + agc_right) / 2;
378 agc_mode = abs(agc_preset) - 1;
379 if (agc_mode < 0) {
380 agc_enable = false;
381 return;
384 if (agc_mode != AGC_SAFETY_MODE) {
385 /* Automatic balance control - only if not in safety mode */
386 if ((agc_left > AGC_IMG) && (agc_right > AGC_IMG))
388 if (*balance < -556)
390 if (*balance > -900)
391 agc_baltime -= !(peak_time % 4); /* 0.47 - 0.75dB */
392 else if (*balance > -4125)
393 agc_baltime--; /* 0.75 - 3.00dB */
394 else if (*balance > -7579)
395 agc_baltime -= 2; /* 3.00 - 4.90dB */
396 else
397 agc_baltime -= !(peak_time % 8); /* 4.90 - inf dB */
398 if (agc_baltime > 0)
399 agc_baltime -= (peak_time % 2);
401 else if (*balance > 556)
403 if (*balance < 900)
404 agc_baltime += !(peak_time % 4);
405 else if (*balance < 4125)
406 agc_baltime++;
407 else if (*balance < 7579)
408 agc_baltime += 2;
409 else
410 agc_baltime += !(peak_time % 8);
411 if (agc_baltime < 0)
412 agc_baltime += (peak_time % 2);
415 if ((*balance * agc_baltime) < 0)
417 if (*balance < 0)
418 agc_baltime -= peak_time % 2;
419 else
420 agc_baltime += peak_time % 2;
423 increment = ((agc_risetime / 2) > agc_droptime);
425 if (agc_baltime < -agc_tbal[agc_mode])
427 if (!increment || !agc_gain_is_max(!increment, increment)) {
428 change_recording_gain(increment, !increment, increment);
429 set_gain();
431 agc_baltime = 0;
433 else if (agc_baltime > +agc_tbal[agc_mode])
435 if (!increment || !agc_gain_is_max(increment, !increment)) {
436 change_recording_gain(increment, increment, !increment);
437 set_gain();
439 agc_baltime = 0;
442 else if (!(hist_time % 4))
444 if (agc_baltime < 0)
445 agc_baltime++;
446 else
447 agc_baltime--;
451 /* Automatic gain control */
452 if ((agc_left > agc_th_hi[agc_mode]) || (agc_right > agc_th_hi[agc_mode]))
454 if ((agc_left > AGC_CLIP) || (agc_right > AGC_CLIP))
455 agc_droptime += agc_tdrop[agc_mode] /
456 (global_settings.rec_agc_cliptime + 1);
457 if (agc_left > AGC_HIGH) {
458 agc_droptime++;
459 agc_risetime=0;
460 if (agc_left > AGC_PEAK)
461 agc_droptime += 2;
463 if (agc_right > AGC_HIGH) {
464 agc_droptime++;
465 agc_risetime=0;
466 if (agc_right > AGC_PEAK)
467 agc_droptime += 2;
469 if (agc_mono > agc_th_hi[agc_mode])
470 agc_droptime++;
471 else
472 agc_droptime += !(peak_time % 2);
474 if (agc_droptime >= agc_tdrop[agc_mode])
476 change_recording_gain(false, true, true);
477 agc_droptime = 0;
478 agc_risetime = 0;
479 set_gain();
481 agc_risetime = MAX(agc_risetime - 1, 0);
483 else if (agc_mono < agc_th_lo[agc_mode])
485 if (agc_mono < (agc_th_lo[agc_mode] / 8))
486 agc_risetime += !(peak_time % 5);
487 else if (agc_mono < (agc_th_lo[agc_mode] / 2))
488 agc_risetime += 2;
489 else
490 agc_risetime++;
492 if (agc_risetime >= agc_trise[agc_mode]) {
493 if ((agc_mode != AGC_SAFETY_MODE) &&
494 (!agc_gain_is_max(true, true))) {
495 change_recording_gain(true, true, true);
496 set_gain();
498 agc_risetime = 0;
499 agc_droptime = 0;
501 agc_droptime = MAX(agc_droptime - 1, 0);
503 else if (!(peak_time % 6)) /* on target level every 1.2 sec */
505 agc_risetime = MAX(agc_risetime - 1, 0);
506 agc_droptime = MAX(agc_droptime - 1, 0);
509 #endif /* HAVE_AGC */
511 static const char* const fmtstr[] =
513 "%c%d %s", /* no decimals */
514 "%c%d.%d %s ", /* 1 decimal */
515 "%c%d.%02d %s " /* 2 decimals */
518 static char *fmt_gain(int snd, int val, char *str, int len)
520 int i, d, numdec;
521 const char *unit;
522 char sign = ' ';
524 val = sound_val2phys(snd, val);
525 if(val < 0)
527 sign = '-';
528 val = -val;
530 numdec = sound_numdecimals(snd);
531 unit = sound_unit(snd);
533 if(numdec)
535 i = val / (10*numdec);
536 d = val % (10*numdec);
537 snprintf(str, len, fmtstr[numdec], sign, i, d, unit);
539 else
540 snprintf(str, len, fmtstr[numdec], sign, val, unit);
542 return str;
545 static int cursor;
547 static void adjust_cursor(void)
549 int max_cursor;
551 if(cursor < 0)
552 cursor = 0;
554 #ifdef HAVE_AGC
555 switch(global_settings.rec_source)
557 case REC_SRC_MIC:
558 if(cursor == 2)
559 cursor = 4;
560 else if(cursor == 3)
561 cursor = 1;
562 HAVE_LINE_REC_(case AUDIO_SRC_LINEIN:)
563 HAVE_FMRADIO_REC_(case AUDIO_SRC_FMRADIO:)
564 max_cursor = 5;
565 break;
566 default:
567 max_cursor = 0;
568 break;
570 #else /* !HAVE_AGC */
571 switch(global_settings.rec_source)
573 case AUDIO_SRC_MIC:
574 max_cursor = 1;
575 break;
576 HAVE_LINE_REC_(case AUDIO_SRC_LINEIN:)
577 HAVE_FMRADIO_REC_(case AUDIO_SRC_FMRADIO:)
578 max_cursor = 3;
579 break;
580 default:
581 max_cursor = 0;
582 break;
584 #endif /* HAVE_AGC */
586 if(cursor > max_cursor)
587 cursor = max_cursor;
590 static bool check_dir(char *folder)
592 DIR *dir = opendir(folder);
593 if (!dir && strcmp(folder, "/"))
595 int rc = mkdir(folder);
596 if(rc < 0)
597 return false;
598 return true;
600 closedir(dir);
601 return true;
604 /* the list below must match enum audio_sources in audio.h */
605 static const char* const prestr[] =
607 HAVE_MIC_IN_([AUDIO_SRC_MIC] = "R_MIC_",)
608 HAVE_LINE_REC_([AUDIO_SRC_LINEIN] = "R_LINE_",)
609 HAVE_SPDIF_IN_([AUDIO_SRC_SPDIF] = "R_SPDIF_",)
610 HAVE_FMRADIO_REC_([AUDIO_SRC_FMRADIO] = "R_FM_",)
613 char *rec_create_filename(char *buffer)
615 char ext[16];
616 const char *pref = "R_";
618 strcpy(buffer,global_settings.rec_directory);
619 if (!check_dir(buffer))
620 return NULL;
622 if((global_settings.rec_source > AUDIO_SRC_PLAYBACK) &&
623 (global_settings.rec_source < AUDIO_NUM_SOURCES))
625 pref = prestr[global_settings.rec_source];
628 snprintf(ext, sizeof(ext), ".%s",
629 REC_FILE_ENDING(global_settings.rec_format));
631 #if CONFIG_RTC == 0
632 return create_numbered_filename(buffer, buffer, pref, ext, 4,
633 &file_number);
634 #else
635 /* We'll wait at least up to the start of the next second so no duplicate
636 names are created */
637 return create_datetime_filename(buffer, buffer, pref, ext, true);
638 #endif
641 #if CONFIG_RTC == 0
642 /* Hit disk to get a starting filename for the type */
643 void rec_init_filename(void)
645 file_number = -1;
646 rec_create_filename(path_buffer);
647 file_number--;
649 #endif
651 int rec_create_directory(void)
653 return check_dir(global_settings.rec_directory)?1:0;
656 void rec_init_recording_options(struct audio_recording_options *options)
658 options->rec_source = global_settings.rec_source;
659 options->rec_frequency = global_settings.rec_frequency;
660 options->rec_channels = global_settings.rec_channels;
661 options->rec_prerecord_time = global_settings.rec_prerecord_time;
662 #if CONFIG_CODEC == SWCODEC
663 options->rec_source_flags = 0;
664 options->enc_config.rec_format = global_settings.rec_format;
665 global_to_encoder_config(&options->enc_config);
666 #else
667 options->rec_quality = global_settings.rec_quality;
668 options->rec_editable = global_settings.rec_editable;
669 #endif
672 #if CONFIG_CODEC == SWCODEC && !defined (SIMULATOR)
673 void rec_set_source(int source, unsigned flags)
675 /* Set audio input source, power up/down devices */
676 audio_set_input_source(source, flags);
678 /* Set peakmeters for recording or reset to playback */
679 peak_meter_playback((flags & SRCF_RECORDING) == 0);
680 peak_meter_enabled = true;
682 #endif /* CONFIG_CODEC == SWCODEC && !defined (SIMULATOR) */
684 void rec_set_recording_options(struct audio_recording_options *options)
686 #if CONFIG_CODEC != SWCODEC
687 if (global_settings.rec_prerecord_time)
688 talk_buffer_steal(); /* will use the mp3 buffer */
689 #else /* == SWOCODEC */
690 rec_set_source(options->rec_source,
691 options->rec_source_flags | SRCF_RECORDING);
692 #endif /* CONFIG_CODEC != SWCODEC */
694 audio_set_recording_options(options);
697 void rec_command(enum recording_command cmd)
699 switch(cmd)
701 case RECORDING_CMD_STOP:
702 pm_activate_clipcount(false);
703 audio_stop_recording();
704 break;
705 case RECORDING_CMD_START:
706 /* steal mp3 buffer, create unique filename and start recording */
707 pm_reset_clipcount();
708 pm_activate_clipcount(true);
709 #if CONFIG_CODEC != SWCODEC
710 talk_buffer_steal(); /* we use the mp3 buffer */
711 #endif
712 audio_record(rec_create_filename(path_buffer));
713 break;
714 case RECORDING_CMD_START_NEWFILE:
715 /* create unique filename and start recording*/
716 pm_reset_clipcount();
717 pm_activate_clipcount(true); /* just to be sure */
718 audio_new_file(rec_create_filename(path_buffer));
719 break;
720 case RECORDING_CMD_PAUSE:
721 pm_activate_clipcount(false);
722 audio_pause_recording();
723 break;
724 case RECORDING_CMD_RESUME:
725 pm_activate_clipcount(true);
726 audio_resume_recording();
727 break;
731 /* used in trigger_listerner and recording_screen */
732 static unsigned int last_seconds = 0;
735 * Callback function so that the peak meter code can send an event
736 * to this application. This function can be passed to
737 * peak_meter_set_trigger_listener in order to activate the trigger.
739 static void trigger_listener(int trigger_status)
741 switch (trigger_status)
743 case TRIG_GO:
744 if(!(audio_status() & AUDIO_STATUS_RECORD))
746 rec_status |= RCSTAT_HAVE_RECORDED;
747 rec_command(RECORDING_CMD_START);
748 #if CONFIG_CODEC != SWCODEC
749 /* give control to mpeg thread so that it can start
750 recording */
751 yield(); yield(); yield();
752 #endif
755 /* if we're already recording this is a retrigger */
756 else
758 if((audio_status() & AUDIO_STATUS_PAUSE) &&
759 (global_settings.rec_trigger_type == 1))
761 rec_command(RECORDING_CMD_RESUME);
763 /* New file on trig start*/
764 else if (global_settings.rec_trigger_type != 2)
766 rec_command(RECORDING_CMD_START_NEWFILE);
767 /* tell recording_screen to reset the time */
768 last_seconds = 0;
771 break;
773 /* A _change_ to TRIG_READY means the current recording has stopped */
774 case TRIG_READY:
775 if(audio_status() & AUDIO_STATUS_RECORD)
777 switch(global_settings.rec_trigger_type)
779 case 0: /* Stop */
780 rec_command(RECORDING_CMD_STOP);
781 break;
783 case 1: /* Pause */
784 rec_command(RECORDING_CMD_PAUSE);
785 break;
787 case 2: /* New file on trig stop*/
788 rec_command(RECORDING_CMD_START_NEWFILE);
789 /* tell recording_screen to reset the time */
790 last_seconds = 0;
791 break;
794 if (global_settings.rec_trigger_mode != TRIG_MODE_REARM)
796 peak_meter_set_trigger_listener(NULL);
797 peak_meter_trigger(false);
800 break;
804 bool recording_start_automatic = false;
806 bool recording_screen(bool no_source)
808 long button;
809 bool done = false;
810 char buf[32];
811 char buf2[32];
812 int w, h;
813 int update_countdown = 1;
814 unsigned int seconds;
815 int hours, minutes;
816 char filename[13];
817 int last_audio_stat = -1;
818 int audio_stat;
819 #if CONFIG_CODEC == SWCODEC
820 int warning_counter = 0;
821 #define WARNING_PERIOD 7
822 #endif
823 #ifdef HAVE_FMRADIO_REC
824 /* Radio is left on if:
825 * 1) Is was on at the start and the initial source is FM Radio
826 * 2) 1) and the source was never changed to something else
828 int radio_status = (global_settings.rec_source != AUDIO_SRC_FMRADIO) ?
829 FMRADIO_OFF : get_radio_status();
830 #endif
831 #if (CONFIG_LED == LED_REAL)
832 bool led_state = false;
833 int led_countdown = 2;
834 #endif
835 #ifdef HAVE_AGC
836 bool peak_read = false;
837 bool peak_valid = false;
838 int peak_l, peak_r;
839 int balance = 0;
840 bool display_agc[NB_SCREENS];
841 #endif
842 int line[NB_SCREENS];
843 int i;
844 int filename_offset[NB_SCREENS];
845 int pm_y[NB_SCREENS];
846 int trig_xpos[NB_SCREENS];
847 int trig_ypos[NB_SCREENS];
848 int trig_width[NB_SCREENS];
849 /* pm_x = offset pm to put clipcount in front.
850 Use lcd_getstringsize() when not using SYSFONT */
851 int pm_x = global_settings.peak_meter_clipcounter ? 30 : 0;
853 static const unsigned char *byte_units[] = {
854 ID2P(LANG_BYTE),
855 ID2P(LANG_KILOBYTE),
856 ID2P(LANG_MEGABYTE),
857 ID2P(LANG_GIGABYTE)
860 struct audio_recording_options rec_options;
861 if (check_dir(global_settings.rec_directory) == false)
863 do {
864 gui_syncsplash(0, "%s %s",
865 str(LANG_REC_DIR_NOT_WRITABLE),
866 str(LANG_OFF_ABORT));
867 } while (action_userabort(HZ) == false);
868 return false;
871 rec_status = RCSTAT_IN_RECSCREEN;
872 cursor = 0;
873 #if (CONFIG_LED == LED_REAL) && !defined(SIMULATOR)
874 ata_set_led_enabled(false);
875 #endif
877 #if CONFIG_CODEC == SWCODEC
878 /* recording_menu gets messed up: so prevent manus talking */
879 talk_disable_menus();
880 /* audio_init_recording stops anything playing when it takes the audio
881 buffer */
882 #else
883 /* Yes, we use the D/A for monitoring */
884 peak_meter_enabled = true;
885 peak_meter_playback(true);
886 #endif
888 audio_init_recording(0);
889 sound_set_volume(global_settings.volume);
891 #ifdef HAVE_AGC
892 peak_meter_get_peakhold(&peak_l, &peak_r);
893 #endif
895 rec_init_recording_options(&rec_options);
896 rec_set_recording_options(&rec_options);
898 set_gain();
900 if(rec_create_directory() > 0)
901 rec_status |= RCSTAT_CREATED_DIRECTORY;
903 #if CONFIG_RTC == 0
904 /* Create new filename for recording start */
905 rec_init_filename();
906 #endif
908 pm_reset_clipcount();
909 pm_activate_clipcount(false);
910 settings_apply_trigger();
912 #ifdef HAVE_AGC
913 agc_preset_str[0] = str(LANG_SYSFONT_OFF);
914 agc_preset_str[1] = str(LANG_SYSFONT_AGC_SAFETY);
915 agc_preset_str[2] = str(LANG_SYSFONT_AGC_LIVE);
916 agc_preset_str[3] = str(LANG_SYSFONT_AGC_DJSET);
917 agc_preset_str[4] = str(LANG_SYSFONT_AGC_MEDIUM);
918 agc_preset_str[5] = str(LANG_SYSFONT_AGC_VOICE);
919 if (global_settings.rec_source == AUDIO_SRC_MIC) {
920 agc_preset = global_settings.rec_agc_preset_mic;
921 agc_maxgain = global_settings.rec_agc_maxgain_mic;
923 else {
924 agc_preset = global_settings.rec_agc_preset_line;
925 agc_maxgain = global_settings.rec_agc_maxgain_line;
927 #endif /* HAVE_AGC */
929 FOR_NB_SCREENS(i)
931 screens[i].setfont(FONT_SYSFIXED);
932 screens[i].getstringsize("M", &w, &h);
933 screens[i].setmargins(global_settings.invert_cursor ? 0 : w, 8);
934 filename_offset[i] = ((screens[i].height >= 80) ? 1 : 0);
935 pm_y[i] = 8 + h * (2 + filename_offset[i]);
938 #ifdef HAVE_REMOTE_LCD
939 if (!remote_display_on)
941 screens[1].clear_display();
942 snprintf(buf, sizeof(buf), str(LANG_REMOTE_LCD_ON));
943 screens[1].puts((screens[1].width/w - strlen(buf))/2 + 1,
944 screens[1].height/(h*2) + 1, buf);
945 screens[1].update();
946 gui_syncsplash(0, str(LANG_REMOTE_LCD_OFF));
948 #endif
950 while(!done)
952 audio_stat = audio_status();
954 #if (CONFIG_LED == LED_REAL)
957 * Flash the LED while waiting to record. Turn it on while
958 * recording.
960 if(audio_stat & AUDIO_STATUS_RECORD)
962 if (audio_stat & AUDIO_STATUS_PAUSE)
964 if (--led_countdown <= 0)
966 led_state = !led_state;
967 led(led_state);
968 led_countdown = 2;
971 else
973 /* trigger is on in status TRIG_READY (no check needed) */
974 led(true);
977 else
979 int trigStat = peak_meter_trigger_status();
981 * other trigger stati than trig_off and trig_steady
982 * already imply that we are recording.
984 if (trigStat == TRIG_STEADY)
986 if (--led_countdown <= 0)
988 led_state = !led_state;
989 led(led_state);
990 led_countdown = 2;
993 else
995 /* trigger is on in status TRIG_READY (no check needed) */
996 led(false);
999 #endif /* CONFIG_LED */
1001 /* Wait for a button a while (HZ/10) drawing the peak meter */
1002 button = peak_meter_draw_get_btn(pm_x, pm_y, h * PM_HEIGHT, screen_update);
1004 if (last_audio_stat != audio_stat)
1006 if (audio_stat & AUDIO_STATUS_RECORD)
1008 rec_status |= RCSTAT_HAVE_RECORDED;
1010 last_audio_stat = audio_stat;
1014 if (recording_start_automatic)
1016 /* simulate a button press */
1017 button = ACTION_REC_PAUSE;
1018 recording_start_automatic = false;
1021 switch(button)
1023 #ifdef HAVE_REMOTE_LCD
1024 case ACTION_REC_LCD:
1025 if (remote_display_on)
1027 remote_display_on = false;
1028 screen_update = 1;
1029 screens[1].clear_display();
1030 snprintf(buf, sizeof(buf), str(LANG_REMOTE_LCD_ON));
1031 screens[1].puts((screens[1].width/w - strlen(buf))/2 + 1,
1032 screens[1].height/(h*2) + 1, buf);
1033 screens[1].update();
1034 gui_syncsplash(0, str(LANG_REMOTE_LCD_OFF));
1036 else
1038 remote_display_on = true;
1039 screen_update = NB_SCREENS;
1041 break;
1042 #endif
1043 case ACTION_STD_CANCEL:
1044 /* turn off the trigger */
1045 peak_meter_trigger(false);
1046 peak_meter_set_trigger_listener(NULL);
1048 if(audio_stat & AUDIO_STATUS_RECORD)
1050 rec_command(RECORDING_CMD_STOP);
1052 else
1054 #if CONFIG_CODEC != SWCODEC
1055 peak_meter_playback(true);
1056 peak_meter_enabled = false;
1057 #endif
1058 done = true;
1060 update_countdown = 1; /* Update immediately */
1061 break;
1063 case ACTION_REC_PAUSE:
1064 case ACTION_REC_NEWFILE:
1065 /* Only act if the mpeg is stopped */
1066 if(!(audio_stat & AUDIO_STATUS_RECORD))
1068 /* is this manual or triggered recording? */
1069 if ((global_settings.rec_trigger_mode == TRIG_MODE_OFF) ||
1070 (peak_meter_trigger_status() != TRIG_OFF))
1072 /* manual recording */
1073 rec_status |= RCSTAT_HAVE_RECORDED;
1074 rec_command(RECORDING_CMD_START);
1075 last_seconds = 0;
1076 if (global_settings.talk_menu)
1078 /* no voice possible here, but a beep */
1079 audio_beep(HZ/2); /* longer beep on start */
1082 /* this is triggered recording */
1083 else
1085 /* we don't start recording now, but enable the
1086 trigger and let the callback function
1087 trigger_listener control when the recording starts */
1088 peak_meter_trigger(true);
1089 peak_meter_set_trigger_listener(&trigger_listener);
1092 else
1094 /*if new file button pressed, start new file */
1095 if (button == ACTION_REC_NEWFILE)
1097 rec_command(RECORDING_CMD_START_NEWFILE);
1098 last_seconds = 0;
1100 else
1101 /* if pause button pressed, pause or resume */
1103 if(audio_stat & AUDIO_STATUS_PAUSE)
1105 rec_command(RECORDING_CMD_RESUME);
1106 if (global_settings.talk_menu)
1108 /* no voice possible here, but a beep */
1109 audio_beep(HZ/4); /* short beep on resume */
1112 else
1114 rec_command(RECORDING_CMD_PAUSE);
1118 update_countdown = 1; /* Update immediately */
1119 break;
1121 case ACTION_STD_PREV:
1122 cursor--;
1123 adjust_cursor();
1124 update_countdown = 1; /* Update immediately */
1125 break;
1127 case ACTION_STD_NEXT:
1128 cursor++;
1129 adjust_cursor();
1130 update_countdown = 1; /* Update immediately */
1131 break;
1133 case ACTION_SETTINGS_INC:
1134 switch(cursor)
1136 case 0:
1137 global_settings.volume++;
1138 setvol();
1139 break;
1140 case 1:
1141 if(global_settings.rec_source == AUDIO_SRC_MIC)
1143 if(global_settings.rec_mic_gain <
1144 sound_max(SOUND_MIC_GAIN))
1145 global_settings.rec_mic_gain++;
1147 else
1149 if(global_settings.rec_left_gain <
1150 sound_max(SOUND_LEFT_GAIN))
1151 global_settings.rec_left_gain++;
1152 if(global_settings.rec_right_gain <
1153 sound_max(SOUND_RIGHT_GAIN))
1154 global_settings.rec_right_gain++;
1156 break;
1157 case 2:
1158 if(global_settings.rec_left_gain <
1159 sound_max(SOUND_LEFT_GAIN))
1160 global_settings.rec_left_gain++;
1161 break;
1162 case 3:
1163 if(global_settings.rec_right_gain <
1164 sound_max(SOUND_RIGHT_GAIN))
1165 global_settings.rec_right_gain++;
1166 break;
1167 #ifdef HAVE_AGC
1168 case 4:
1169 agc_preset = MIN(agc_preset + 1, AGC_MODE_SIZE);
1170 agc_enable = (agc_preset != 0);
1171 if (global_settings.rec_source == AUDIO_SRC_MIC) {
1172 global_settings.rec_agc_preset_mic = agc_preset;
1173 agc_maxgain = global_settings.rec_agc_maxgain_mic;
1174 } else {
1175 global_settings.rec_agc_preset_line = agc_preset;
1176 agc_maxgain = global_settings.rec_agc_maxgain_line;
1178 break;
1179 case 5:
1180 if (global_settings.rec_source == AUDIO_SRC_MIC)
1182 agc_maxgain = MIN(agc_maxgain + 1,
1183 sound_max(SOUND_MIC_GAIN));
1184 global_settings.rec_agc_maxgain_mic = agc_maxgain;
1186 else
1188 agc_maxgain = MIN(agc_maxgain + 1,
1189 sound_max(SOUND_LEFT_GAIN));
1190 global_settings.rec_agc_maxgain_line = agc_maxgain;
1192 break;
1193 #endif /* HAVE_AGC */
1195 set_gain();
1196 update_countdown = 1; /* Update immediately */
1197 break;
1199 case ACTION_SETTINGS_DEC:
1200 switch(cursor)
1202 case 0:
1203 global_settings.volume--;
1204 setvol();
1205 break;
1206 case 1:
1207 if(global_settings.rec_source == AUDIO_SRC_MIC)
1209 if(global_settings.rec_mic_gain >
1210 sound_min(SOUND_MIC_GAIN))
1211 global_settings.rec_mic_gain--;
1213 else
1215 if(global_settings.rec_left_gain >
1216 sound_min(SOUND_LEFT_GAIN))
1217 global_settings.rec_left_gain--;
1218 if(global_settings.rec_right_gain >
1219 sound_min(SOUND_RIGHT_GAIN))
1220 global_settings.rec_right_gain--;
1222 break;
1223 case 2:
1224 if(global_settings.rec_left_gain >
1225 sound_min(SOUND_LEFT_GAIN))
1226 global_settings.rec_left_gain--;
1227 break;
1228 case 3:
1229 if(global_settings.rec_right_gain >
1230 sound_min(SOUND_RIGHT_GAIN))
1231 global_settings.rec_right_gain--;
1232 break;
1233 #ifdef HAVE_AGC
1234 case 4:
1235 agc_preset = MAX(agc_preset - 1, 0);
1236 agc_enable = (agc_preset != 0);
1237 if (global_settings.rec_source == AUDIO_SRC_MIC) {
1238 global_settings.rec_agc_preset_mic = agc_preset;
1239 agc_maxgain = global_settings.rec_agc_maxgain_mic;
1240 } else {
1241 global_settings.rec_agc_preset_line = agc_preset;
1242 agc_maxgain = global_settings.rec_agc_maxgain_line;
1244 break;
1245 case 5:
1246 if (global_settings.rec_source == AUDIO_SRC_MIC)
1248 agc_maxgain = MAX(agc_maxgain - 1,
1249 sound_min(SOUND_MIC_GAIN));
1250 global_settings.rec_agc_maxgain_mic = agc_maxgain;
1252 else
1254 agc_maxgain = MAX(agc_maxgain - 1,
1255 sound_min(SOUND_LEFT_GAIN));
1256 global_settings.rec_agc_maxgain_line = agc_maxgain;
1258 break;
1259 #endif /* HAVE_AGC */
1261 set_gain();
1262 update_countdown = 1; /* Update immediately */
1263 break;
1265 case ACTION_STD_MENU:
1266 #if CONFIG_CODEC == SWCODEC
1267 if(!(audio_stat & AUDIO_STATUS_RECORD))
1268 #else
1269 if(audio_stat != AUDIO_STATUS_RECORD)
1270 #endif
1272 #ifdef HAVE_FMRADIO_REC
1273 const int prev_rec_source = global_settings.rec_source;
1274 #endif
1276 #if (CONFIG_LED == LED_REAL)
1277 /* led is restored at begin of loop / end of function */
1278 led(false);
1279 #endif
1280 if (recording_menu(no_source))
1282 done = true;
1283 rec_status |= RCSTAT_BEEN_IN_USB_MODE;
1284 #ifdef HAVE_FMRADIO_REC
1285 radio_status = FMRADIO_OFF;
1286 #endif
1288 else
1290 #ifdef HAVE_FMRADIO_REC
1291 /* If input changes away from FM Radio, radio will
1292 remain off when recording screen closes. */
1293 if (global_settings.rec_source != prev_rec_source
1294 && prev_rec_source == AUDIO_SRC_FMRADIO)
1295 radio_status = FMRADIO_OFF;
1296 #endif
1298 #if CONFIG_CODEC == SWCODEC
1299 /* reinit after submenu exit */
1300 audio_close_recording();
1301 audio_init_recording(0);
1302 #endif
1304 rec_init_recording_options(&rec_options);
1305 rec_set_recording_options(&rec_options);
1307 if(rec_create_directory() > 0)
1308 rec_status |= RCSTAT_CREATED_DIRECTORY;
1310 #if CONFIG_CODEC == SWCODEC && CONFIG_RTC == 0
1311 /* If format changed, a new number is required */
1312 rec_init_filename();
1313 #endif
1315 #ifdef HAVE_AGC
1316 if (global_settings.rec_source == AUDIO_SRC_MIC) {
1317 agc_preset = global_settings.rec_agc_preset_mic;
1318 agc_maxgain = global_settings.rec_agc_maxgain_mic;
1320 else {
1321 agc_preset = global_settings.rec_agc_preset_line;
1322 agc_maxgain = global_settings.rec_agc_maxgain_line;
1324 #endif
1326 adjust_cursor();
1327 set_gain();
1328 update_countdown = 1; /* Update immediately */
1330 FOR_NB_SCREENS(i)
1332 screens[i].setfont(FONT_SYSFIXED);
1333 screens[i].setmargins(
1334 global_settings.invert_cursor ? 0 : w, 8);
1338 break;
1340 #if CONFIG_KEYPAD == RECORDER_PAD
1341 case ACTION_REC_F2:
1342 if(audio_stat != AUDIO_STATUS_RECORD)
1344 #if (CONFIG_LED == LED_REAL)
1345 /* led is restored at begin of loop / end of function */
1346 led(false);
1347 #endif
1348 if (f2_rec_screen())
1350 rec_status |= RCSTAT_HAVE_RECORDED;
1351 done = true;
1353 else
1354 update_countdown = 1; /* Update immediately */
1356 break;
1358 case ACTION_REC_F3:
1359 if(audio_stat & AUDIO_STATUS_RECORD)
1361 rec_command(RECORDING_CMD_START_NEWFILE);
1362 last_seconds = 0;
1364 else
1366 #if (CONFIG_LED == LED_REAL)
1367 /* led is restored at begin of loop / end of function */
1368 led(false);
1369 #endif
1370 if (f3_rec_screen())
1372 rec_status |= RCSTAT_HAVE_RECORDED;
1373 done = true;
1375 else
1376 update_countdown = 1; /* Update immediately */
1378 break;
1379 #endif /* CONFIG_KEYPAD == RECORDER_PAD */
1381 case SYS_USB_CONNECTED:
1382 /* Only accept USB connection when not recording */
1383 if(!(audio_stat & AUDIO_STATUS_RECORD))
1385 default_event_handler(SYS_USB_CONNECTED);
1386 done = true;
1387 rec_status |= RCSTAT_BEEN_IN_USB_MODE;
1388 #ifdef HAVE_FMRADIO_REC
1389 radio_status = FMRADIO_OFF;
1390 #endif
1392 break;
1394 default:
1395 default_event_handler(button);
1396 break;
1397 } /* end switch */
1399 #ifdef HAVE_AGC
1400 peak_read = !peak_read;
1401 if (peak_read) { /* every 2nd run of loop */
1402 peak_time++;
1403 peak_valid = read_peak_levels(&peak_l, &peak_r, &balance);
1406 /* Handle AGC every 200ms when enabled and peak data is valid */
1407 if (peak_read && agc_enable && peak_valid)
1408 auto_gain_control(&peak_l, &peak_r, &balance);
1409 #endif
1411 FOR_NB_SCREENS(i)
1412 screens[i].setfont(FONT_SYSFIXED);
1414 seconds = audio_recorded_time() / HZ;
1416 update_countdown--;
1417 if(update_countdown == 0 || seconds > last_seconds)
1419 unsigned int dseconds, dhours, dminutes;
1420 unsigned long num_recorded_bytes, dsize, dmb;
1421 int pos = 0;
1423 update_countdown = 5;
1424 last_seconds = seconds;
1426 dseconds = rec_timesplit_seconds();
1427 dsize = rec_sizesplit_bytes();
1428 num_recorded_bytes = audio_num_recorded_bytes();
1430 for(i = 0; i < screen_update; i++)
1431 screens[i].clear_display();
1433 #if CONFIG_CODEC == SWCODEC
1434 if ((audio_stat & AUDIO_STATUS_WARNING)
1435 && (warning_counter++ % WARNING_PERIOD) < WARNING_PERIOD/2)
1437 /* Switch back and forth displaying warning on first available
1438 line to ensure visibility - the motion should also help
1439 draw attention */
1440 /* Don't use language string unless agreed upon to make this
1441 method permanent - could do something in the statusbar */
1442 snprintf(buf, sizeof(buf), "Warning: %08X",
1443 pcm_rec_get_warnings());
1445 else
1446 #endif /* CONFIG_CODEC == SWCODEC */
1447 if ((global_settings.rec_sizesplit) && (global_settings.rec_split_method))
1449 dmb = dsize/1024/1024;
1450 snprintf(buf, sizeof(buf), "%s %dMB",
1451 str(LANG_SYSFONT_SPLIT_SIZE), dmb);
1453 else
1455 hours = seconds / 3600;
1456 minutes = (seconds - (hours * 3600)) / 60;
1457 snprintf(buf, sizeof(buf), "%s %02d:%02d:%02d",
1458 str(LANG_SYSFONT_RECORDING_TIME),
1459 hours, minutes, seconds%60);
1462 for(i = 0; i < screen_update; i++)
1463 screens[i].puts(0, 0, buf);
1465 if(audio_stat & AUDIO_STATUS_PRERECORD)
1467 snprintf(buf, sizeof(buf), "%s...", str(LANG_SYSFONT_RECORD_PRERECORD));
1469 else
1471 /* Display the split interval if the record timesplit
1472 is active */
1473 if ((global_settings.rec_timesplit) && !(global_settings.rec_split_method))
1475 /* Display the record timesplit interval rather
1476 than the file size if the record timer is
1477 active */
1478 dhours = dseconds / 3600;
1479 dminutes = (dseconds - (dhours * 3600)) / 60;
1480 snprintf(buf, sizeof(buf), "%s %02d:%02d",
1481 str(LANG_SYSFONT_RECORD_TIMESPLIT_REC),
1482 dhours, dminutes);
1484 else
1486 output_dyn_value(buf2, sizeof buf2,
1487 num_recorded_bytes,
1488 byte_units, true);
1489 snprintf(buf, sizeof(buf), "%s %s",
1490 str(LANG_SYSFONT_RECORDING_SIZE), buf2);
1493 for(i = 0; i < screen_update; i++)
1494 screens[i].puts(0, 1, buf);
1496 for(i = 0; i < screen_update; i++)
1498 if (filename_offset[i] > 0)
1500 *filename = '\0';
1501 if (audio_stat & AUDIO_STATUS_RECORD)
1503 strncpy(filename, path_buffer +
1504 strlen(path_buffer) - 12, 13);
1505 filename[12]='\0';
1508 snprintf(buf, sizeof(buf), "%s %s",
1509 str(LANG_SYSFONT_RECORDING_FILENAME), filename);
1510 screens[i].puts(0, 2, buf);
1514 /* We will do file splitting regardless, either at the end of
1515 a split interval, or when the filesize approaches the 2GB
1516 FAT file size (compatibility) limit. */
1517 if ((audio_stat && !(global_settings.rec_split_method)
1518 && global_settings.rec_timesplit && (seconds >= dseconds))
1519 || (audio_stat && global_settings.rec_split_method
1520 && global_settings.rec_sizesplit && (num_recorded_bytes >= dsize))
1521 || (num_recorded_bytes >= MAX_FILE_SIZE))
1523 if (!(global_settings.rec_split_type)
1524 || (num_recorded_bytes >= MAX_FILE_SIZE))
1526 rec_command(RECORDING_CMD_START_NEWFILE);
1527 last_seconds = 0;
1529 else
1531 peak_meter_trigger(false);
1532 peak_meter_set_trigger_listener(NULL);
1533 rec_command(RECORDING_CMD_STOP);
1535 update_countdown = 1;
1538 /* draw the clipcounter just in front of the peakmeter */
1539 if(global_settings.peak_meter_clipcounter)
1541 char clpstr[32];
1542 snprintf(clpstr, 32, "%4d", pm_get_clipcount());
1543 for(i = 0; i < screen_update; i++)
1545 if(PM_HEIGHT > 1)
1546 screens[i].puts(0, 2 + filename_offset[i],
1547 str(LANG_PM_CLIPCOUNT));
1548 screens[i].puts(0, 1 + PM_HEIGHT + filename_offset[i],
1549 clpstr);
1553 snprintf(buf, sizeof(buf), "%s: %s", str(LANG_SYSFONT_VOLUME),
1554 fmt_gain(SOUND_VOLUME,
1555 global_settings.volume,
1556 buf2, sizeof(buf2)));
1558 if (global_settings.invert_cursor && (pos++ == cursor))
1560 for(i = 0; i < screen_update; i++)
1561 screens[i].puts_style_offset(0, filename_offset[i] +
1562 PM_HEIGHT + 2, buf, STYLE_INVERT,0);
1564 else
1566 for(i = 0; i < screen_update; i++)
1567 screens[i].puts(0, filename_offset[i] + PM_HEIGHT + 2, buf);
1570 if(global_settings.rec_source == AUDIO_SRC_MIC)
1572 /* Draw MIC recording gain */
1573 snprintf(buf, sizeof(buf), "%s:%s", str(LANG_SYSFONT_GAIN),
1574 fmt_gain(SOUND_MIC_GAIN,
1575 global_settings.rec_mic_gain,
1576 buf2, sizeof(buf2)));
1577 if(global_settings.invert_cursor && ((1==cursor)||(2==cursor)))
1579 for(i = 0; i < screen_update; i++)
1580 screens[i].puts_style_offset(0, filename_offset[i] +
1581 PM_HEIGHT + 3, buf, STYLE_INVERT,0);
1583 else
1585 for(i = 0; i < screen_update; i++)
1586 screens[i].puts(0, filename_offset[i] +
1587 PM_HEIGHT + 3, buf);
1590 else if(0
1591 HAVE_LINE_REC_( || global_settings.rec_source == AUDIO_SRC_LINEIN)
1592 HAVE_FMRADIO_REC_( || global_settings.rec_source == AUDIO_SRC_FMRADIO)
1595 /* Draw LINE or FMRADIO recording gain */
1596 snprintf(buf, sizeof(buf), "%s:%s",
1597 str(LANG_SYSFONT_RECORDING_LEFT),
1598 fmt_gain(SOUND_LEFT_GAIN,
1599 global_settings.rec_left_gain,
1600 buf2, sizeof(buf2)));
1601 if(global_settings.invert_cursor && ((1==cursor)||(2==cursor)))
1603 for(i = 0; i < screen_update; i++)
1604 screens[i].puts_style_offset(0, filename_offset[i] +
1605 PM_HEIGHT + 3, buf, STYLE_INVERT,0);
1607 else
1609 for(i = 0; i < screen_update; i++)
1610 screens[i].puts(0, filename_offset[i] +
1611 PM_HEIGHT + 3, buf);
1614 snprintf(buf, sizeof(buf), "%s:%s",
1615 str(LANG_SYSFONT_RECORDING_RIGHT),
1616 fmt_gain(SOUND_RIGHT_GAIN,
1617 global_settings.rec_right_gain,
1618 buf2, sizeof(buf2)));
1619 if(global_settings.invert_cursor && ((1==cursor)||(3==cursor)))
1621 for(i = 0; i < screen_update; i++)
1622 screens[i].puts_style_offset(0, filename_offset[i] +
1623 PM_HEIGHT + 4, buf, STYLE_INVERT,0);
1625 else
1627 for(i = 0; i < screen_update; i++)
1628 screens[i].puts(0, filename_offset[i] +
1629 PM_HEIGHT + 4, buf);
1633 FOR_NB_SCREENS(i)
1635 switch (global_settings.rec_source)
1637 HAVE_LINE_REC_(case AUDIO_SRC_LINEIN:)
1638 HAVE_FMRADIO_REC_(case AUDIO_SRC_FMRADIO:)
1639 line[i] = 5;
1640 break;
1642 case AUDIO_SRC_MIC:
1643 line[i] = 4;
1644 break;
1645 #ifdef HAVE_SPDIF_REC
1646 case AUDIO_SRC_SPDIF:
1647 line[i] = 3;
1648 break;
1649 #endif
1650 default:
1651 line[i] = 5; /* to prevent uninitialisation
1652 warnings for line[0] */
1653 break;
1654 } /* end switch */
1655 #ifdef HAVE_AGC
1656 if (screens[i].height < h * (2 + filename_offset[i] + PM_HEIGHT + line[i]))
1658 line[i] -= 1;
1659 display_agc[i] = false;
1661 else
1662 display_agc[i] = true;
1664 if ((cursor==4) || (cursor==5))
1665 display_agc[i] = true;
1668 /************** AGC test info ******************
1669 snprintf(buf, sizeof(buf), "D:%d U:%d",
1670 (agc_droptime+2)/5, (agc_risetime+2)/5);
1671 lcd_putsxy(1, LCD_HEIGHT - 8, buf);
1672 snprintf(buf, sizeof(buf), "B:%d",
1673 (agc_baltime+2)/5);
1674 lcd_putsxy(LCD_WIDTH/2 + 3, LCD_HEIGHT - 8, buf);
1675 ***********************************************/
1677 if (cursor == 5)
1678 snprintf(buf, sizeof(buf), "%s: %s",
1679 str(LANG_SYSFONT_RECORDING_AGC_MAXGAIN),
1680 fmt_gain(SOUND_LEFT_GAIN,
1681 agc_maxgain, buf2, sizeof(buf2)));
1682 else if (agc_preset == 0)
1683 snprintf(buf, sizeof(buf), "%s: %s",
1684 str(LANG_SYSFONT_RECORDING_AGC_PRESET),
1685 agc_preset_str[agc_preset]);
1686 else if (global_settings.rec_source == AUDIO_SRC_MIC)
1687 snprintf(buf, sizeof(buf), "%s: %s%s",
1688 str(LANG_SYSFONT_RECORDING_AGC_PRESET),
1689 agc_preset_str[agc_preset],
1690 fmt_gain(SOUND_LEFT_GAIN,
1691 agc_maxgain -
1692 global_settings.rec_mic_gain,
1693 buf2, sizeof(buf2)));
1694 else
1695 snprintf(buf, sizeof(buf), "%s: %s%s",
1696 str(LANG_SYSFONT_RECORDING_AGC_PRESET),
1697 agc_preset_str[agc_preset],
1698 fmt_gain(SOUND_LEFT_GAIN,
1699 agc_maxgain -
1700 (global_settings.rec_left_gain +
1701 global_settings.rec_right_gain)/2,
1702 buf2, sizeof(buf2)));
1704 if(global_settings.invert_cursor && ((cursor==4) || (cursor==5)))
1706 for(i = 0; i < screen_update; i++)
1707 screens[i].puts_style_offset(0, filename_offset[i] +
1708 PM_HEIGHT + line[i], buf, STYLE_INVERT,0);
1710 else if (
1711 global_settings.rec_source == AUDIO_SRC_MIC
1712 HAVE_LINE_REC_(|| global_settings.rec_source == AUDIO_SRC_LINEIN)
1713 HAVE_FMRADIO_REC_(|| global_settings.rec_source == AUDIO_SRC_FMRADIO)
1716 for(i = 0; i < screen_update; i++) {
1717 if (display_agc[i]) {
1718 screens[i].puts(0, filename_offset[i] +
1719 PM_HEIGHT + line[i], buf);
1724 if (global_settings.rec_source == AUDIO_SRC_MIC)
1726 if(agc_maxgain < (global_settings.rec_mic_gain))
1727 change_recording_gain(false, true, true);
1729 else
1731 if(agc_maxgain < (global_settings.rec_left_gain))
1732 change_recording_gain(false, true, false);
1733 if(agc_maxgain < (global_settings.rec_right_gain))
1734 change_recording_gain(false, false, true);
1736 #else /* !HAVE_AGC */
1738 #endif /* HAVE_AGC */
1740 if(!global_settings.invert_cursor) {
1741 switch(cursor)
1743 case 1:
1744 for(i = 0; i < screen_update; i++)
1745 screen_put_cursorxy(&screens[i], 0,
1746 filename_offset[i] +
1747 PM_HEIGHT + 3, true);
1749 if(global_settings.rec_source != AUDIO_SRC_MIC)
1751 for(i = 0; i < screen_update; i++)
1752 screen_put_cursorxy(&screens[i], 0,
1753 filename_offset[i] +
1754 PM_HEIGHT + 4, true);
1756 break;
1757 case 2:
1758 for(i = 0; i < screen_update; i++)
1759 screen_put_cursorxy(&screens[i], 0,
1760 filename_offset[i] +
1761 PM_HEIGHT + 3, true);
1762 break;
1763 case 3:
1764 for(i = 0; i < screen_update; i++)
1765 screen_put_cursorxy(&screens[i], 0,
1766 filename_offset[i] +
1767 PM_HEIGHT + 4, true);
1768 break;
1769 #ifdef HAVE_AGC
1770 case 4:
1771 case 5:
1772 for(i = 0; i < screen_update; i++)
1773 screen_put_cursorxy(&screens[i], 0,
1774 filename_offset[i] +
1775 PM_HEIGHT + line[i], true);
1776 break;
1777 #endif /* HAVE_AGC */
1778 default:
1779 for(i = 0; i < screen_update; i++)
1780 screen_put_cursorxy(&screens[i], 0,
1781 filename_offset[i] +
1782 PM_HEIGHT + 2, true);
1786 #ifdef HAVE_AGC
1787 hist_time++;
1788 #endif
1790 for(i = 0; i < screen_update; i++)
1792 gui_statusbar_draw(&(statusbars.statusbars[i]), true);
1793 peak_meter_screen(&screens[i], pm_x, pm_y[i], h*PM_HEIGHT);
1794 screens[i].update();
1797 /* draw the trigger status */
1798 FOR_NB_SCREENS(i)
1800 trig_width[i] = ((screens[i].height < 64) ||
1801 ((screens[i].height < 72) && (PM_HEIGHT > 1))) ?
1802 screens[i].width - 14 * w : screens[i].width;
1803 trig_xpos[i] = screens[i].width - trig_width[i];
1804 trig_ypos[i] = ((screens[i].height < 72) && (PM_HEIGHT > 1)) ?
1805 h*2 :
1806 h*(1 + filename_offset[i] + PM_HEIGHT + line[i]
1807 #ifdef HAVE_AGC
1809 #endif
1813 if (peak_meter_trigger_status() != TRIG_OFF)
1815 peak_meter_draw_trig(trig_xpos, trig_ypos, trig_width,
1816 screen_update);
1817 for(i = 0; i < screen_update; i++){
1818 screens[i].update_rect(trig_xpos[i], trig_ypos[i],
1819 trig_width[i] + 2, TRIG_HEIGHT);
1824 if(audio_stat & AUDIO_STATUS_ERROR)
1826 done = true;
1828 } /* end while(!done) */
1830 audio_stat = audio_status();
1831 if (audio_stat & AUDIO_STATUS_ERROR)
1833 gui_syncsplash(0, str(LANG_SYSFONT_DISK_FULL));
1834 gui_syncstatusbar_draw(&statusbars, true);
1836 FOR_NB_SCREENS(i)
1837 screens[i].update();
1839 audio_error_clear();
1841 while(1)
1843 if (action_userabort(TIMEOUT_NOBLOCK))
1844 break;
1848 #if CONFIG_CODEC == SWCODEC
1849 rec_command(RECORDING_CMD_STOP);
1850 audio_close_recording();
1852 #ifdef HAVE_FMRADIO_REC
1853 if (radio_status != FMRADIO_OFF)
1854 /* Restore radio playback - radio_status should be unchanged if started
1855 through fm radio screen (barring usb connect) */
1856 rec_set_source(AUDIO_SRC_FMRADIO, (radio_status & FMRADIO_PAUSED) ?
1857 SRCF_FMRADIO_PAUSED : SRCF_FMRADIO_PLAYING);
1858 else
1859 #endif
1860 /* Go back to playback mode */
1861 rec_set_source(AUDIO_SRC_PLAYBACK, SRCF_PLAYBACK);
1863 /* restore talk_menu setting */
1864 talk_enable_menus();
1865 #else /* !SWCODEC */
1866 audio_init_playback();
1867 #endif /* CONFIG_CODEC == SWCODEC */
1869 /* make sure the trigger is really turned off */
1870 peak_meter_trigger(false);
1871 peak_meter_set_trigger_listener(NULL);
1873 rec_status &= ~RCSTAT_IN_RECSCREEN;
1874 sound_settings_apply();
1876 FOR_NB_SCREENS(i)
1877 screens[i].setfont(FONT_UI);
1879 /* if the directory was created or recording happened, make sure the
1880 browser is updated */
1881 if (rec_status & (RCSTAT_CREATED_DIRECTORY | RCSTAT_HAVE_RECORDED))
1882 reload_directory();
1884 #if (CONFIG_LED == LED_REAL) && !defined(SIMULATOR)
1885 ata_set_led_enabled(true);
1886 #endif
1888 settings_save();
1890 return (rec_status & RCSTAT_BEEN_IN_USB_MODE) != 0;
1891 } /* recording_screen */
1893 #if CONFIG_KEYPAD == RECORDER_PAD
1894 static bool f2_rec_screen(void)
1896 static const char* const freq_str[6] =
1898 "44.1kHz",
1899 "48kHz",
1900 "32kHz",
1901 "22.05kHz",
1902 "24kHz",
1903 "16kHz"
1906 bool exit = false;
1907 bool used = false;
1908 int w, h, i;
1909 char buf[32];
1910 int button;
1911 struct audio_recording_options rec_options;
1913 FOR_NB_SCREENS(i)
1915 screens[i].setfont(FONT_SYSFIXED);
1916 screens[i].getstringsize("A",&w,&h);
1919 while (!exit) {
1920 const char* ptr=NULL;
1922 FOR_NB_SCREENS(i)
1924 screens[i].clear_display();
1926 /* Recording quality */
1927 screens[i].putsxy(0, LCD_HEIGHT/2 - h*2,
1928 str(LANG_SYSFONT_RECORDING_QUALITY));
1931 snprintf(buf, sizeof(buf), "%d", global_settings.rec_quality);
1932 FOR_NB_SCREENS(i)
1934 screens[i].putsxy(0, LCD_HEIGHT/2-h, buf);
1935 screens[i].mono_bitmap(bitmap_icons_7x8[Icon_FastBackward],
1936 LCD_WIDTH/2 - 16, LCD_HEIGHT/2 - 4, 7, 8);
1939 /* Frequency */
1940 snprintf(buf, sizeof buf, "%s:", str(LANG_SYSFONT_RECORDING_FREQUENCY));
1941 ptr = freq_str[global_settings.rec_frequency];
1942 FOR_NB_SCREENS(i)
1944 screens[i].getstringsize(buf,&w,&h);
1945 screens[i].putsxy((LCD_WIDTH-w)/2, LCD_HEIGHT - h*2, buf);
1946 screens[i].getstringsize(ptr, &w, &h);
1947 screens[i].putsxy((LCD_WIDTH-w)/2, LCD_HEIGHT - h, ptr);
1948 screens[i].mono_bitmap(bitmap_icons_7x8[Icon_DownArrow],
1949 LCD_WIDTH/2 - 3, LCD_HEIGHT - h*3, 7, 8);
1952 /* Channel mode */
1953 switch ( global_settings.rec_channels ) {
1954 case 0:
1955 ptr = str(LANG_SYSFONT_CHANNEL_STEREO);
1956 break;
1958 case 1:
1959 ptr = str(LANG_SYSFONT_CHANNEL_MONO);
1960 break;
1963 FOR_NB_SCREENS(i)
1965 screens[i].getstringsize(str(LANG_SYSFONT_CHANNELS), &w, &h);
1966 screens[i].putsxy(LCD_WIDTH - w, LCD_HEIGHT/2 - h*2,
1967 str(LANG_SYSFONT_CHANNELS));
1968 screens[i].getstringsize(str(LANG_SYSFONT_MODE), &w, &h);
1969 screens[i].putsxy(LCD_WIDTH - w, LCD_HEIGHT/2 - h,
1970 str(LANG_SYSFONT_MODE));
1971 screens[i].getstringsize(ptr, &w, &h);
1972 screens[i].putsxy(LCD_WIDTH - w, LCD_HEIGHT/2, ptr);
1973 screens[i].mono_bitmap(bitmap_icons_7x8[Icon_FastForward],
1974 LCD_WIDTH/2 + 8, LCD_HEIGHT/2 - 4, 7, 8);
1976 screens[i].update();
1979 button = button_get(true);
1980 switch (button) {
1981 case BUTTON_LEFT:
1982 case BUTTON_F2 | BUTTON_LEFT:
1983 global_settings.rec_quality++;
1984 if(global_settings.rec_quality > 7)
1985 global_settings.rec_quality = 0;
1986 used = true;
1987 break;
1989 case BUTTON_DOWN:
1990 case BUTTON_F2 | BUTTON_DOWN:
1991 global_settings.rec_frequency++;
1992 if(global_settings.rec_frequency > 5)
1993 global_settings.rec_frequency = 0;
1994 used = true;
1995 break;
1997 case BUTTON_RIGHT:
1998 case BUTTON_F2 | BUTTON_RIGHT:
1999 global_settings.rec_channels++;
2000 if(global_settings.rec_channels > 1)
2001 global_settings.rec_channels = 0;
2002 used = true;
2003 break;
2005 case BUTTON_F2 | BUTTON_REL:
2006 if ( used )
2007 exit = true;
2008 used = true;
2009 break;
2011 case BUTTON_F2 | BUTTON_REPEAT:
2012 used = true;
2013 break;
2015 default:
2016 if(default_event_handler(button) == SYS_USB_CONNECTED)
2017 return true;
2018 break;
2022 rec_init_recording_options(&rec_options);
2023 rec_set_recording_options(&rec_options);
2025 set_gain();
2027 settings_save();
2028 FOR_NB_SCREENS(i)
2029 screens[i].setfont(FONT_UI);
2031 return false;
2034 static bool f3_rec_screen(void)
2036 bool exit = false;
2037 bool used = false;
2038 int w, h, i;
2039 int button;
2040 char *src_str[] =
2042 str(LANG_SYSFONT_RECORDING_SRC_MIC),
2043 str(LANG_SYSFONT_LINE_IN),
2044 str(LANG_SYSFONT_RECORDING_SRC_DIGITAL)
2046 struct audio_recording_options rec_options;
2048 FOR_NB_SCREENS(i)
2050 screens[i].setfont(FONT_SYSFIXED);
2051 screens[i].getstringsize("A",&w,&h);
2054 while (!exit) {
2055 char* ptr=NULL;
2056 ptr = src_str[global_settings.rec_source];
2057 FOR_NB_SCREENS(i)
2059 screens[i].clear_display();
2061 /* Recording source */
2062 screens[i].putsxy(0, LCD_HEIGHT/2 - h*2,
2063 str(LANG_SYSFONT_RECORDING_SOURCE));
2065 screens[i].getstringsize(ptr, &w, &h);
2066 screens[i].putsxy(0, LCD_HEIGHT/2-h, ptr);
2067 screens[i].mono_bitmap(bitmap_icons_7x8[Icon_FastBackward],
2068 LCD_WIDTH/2 - 16, LCD_HEIGHT/2 - 4, 7, 8);
2071 /* trigger setup */
2072 ptr = str(LANG_SYSFONT_RECORD_TRIGGER);
2073 FOR_NB_SCREENS(i)
2075 screens[i].getstringsize(ptr,&w,&h);
2076 screens[i].putsxy((LCD_WIDTH-w)/2, LCD_HEIGHT - h*2, ptr);
2077 screens[i].mono_bitmap(bitmap_icons_7x8[Icon_DownArrow],
2078 LCD_WIDTH/2 - 3, LCD_HEIGHT - h*3, 7, 8);
2080 screens[i].update();
2083 button = button_get(true);
2084 switch (button) {
2085 case BUTTON_DOWN:
2086 case BUTTON_F3 | BUTTON_DOWN:
2087 #ifndef SIMULATOR
2088 rectrigger();
2089 settings_apply_trigger();
2090 #endif
2091 exit = true;
2092 break;
2094 case BUTTON_LEFT:
2095 case BUTTON_F3 | BUTTON_LEFT:
2096 global_settings.rec_source++;
2097 if(global_settings.rec_source > AUDIO_SRC_MAX)
2098 global_settings.rec_source = 0;
2099 used = true;
2100 break;
2102 case BUTTON_F3 | BUTTON_REL:
2103 if ( used )
2104 exit = true;
2105 used = true;
2106 break;
2108 case BUTTON_F3 | BUTTON_REPEAT:
2109 used = true;
2110 break;
2112 default:
2113 if(default_event_handler(button) == SYS_USB_CONNECTED)
2114 return true;
2115 break;
2119 rec_init_recording_options(&rec_options);
2120 rec_set_recording_options(&rec_options);
2122 set_gain();
2124 settings_save();
2125 FOR_NB_SCREENS(i)
2126 screens[i].setfont(FONT_UI);
2128 return false;
2130 #endif /* CONFIG_KEYPAD == RECORDER_PAD */
2132 #if CONFIG_CODEC == SWCODEC
2133 void audio_beep(int duration)
2135 /* dummy */
2136 (void)duration;
2139 #ifdef SIMULATOR
2140 /* stubs for recording sim */
2141 void audio_init_recording(unsigned int buffer_offset)
2143 buffer_offset = buffer_offset;
2146 void audio_close_recording(void)
2150 unsigned long pcm_rec_get_warnings(void)
2152 return 0;
2155 unsigned long audio_recorded_time(void)
2157 return 123;
2160 unsigned long audio_num_recorded_bytes(void)
2162 return 5 * 1024 * 1024;
2165 void rec_set_source(int source, unsigned flags)
2167 source = source;
2168 flags = flags;
2171 void audio_set_recording_options(struct audio_recording_options *options)
2173 options = options;
2176 void audio_set_recording_gain(int left, int right, int type)
2178 left = left;
2179 right = right;
2180 type = type;
2183 void audio_record(const char *filename)
2185 filename = filename;
2188 void audio_new_file(const char *filename)
2190 filename = filename;
2193 void audio_stop_recording(void)
2197 void audio_pause_recording(void)
2201 void audio_resume_recording(void)
2205 #endif /* #ifdef SIMULATOR */
2206 #endif /* #ifdef CONFIG_CODEC == SWCODEC */
2208 #endif /* HAVE_RECORDING */