Final changes to the recording naming code, this should make jhMikeS happy ;)
[Rockbox.git] / apps / recorder / recording.c
blob8bcc3ee9af1244ceffda8b778fd6d917eb3222d4
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 /* steals mp3 buffer, creates unique filename and starts recording */
698 void rec_record(void)
700 #if CONFIG_CODEC != SWCODEC
701 talk_buffer_steal(); /* we use the mp3 buffer */
702 #endif
703 audio_record(rec_create_filename(path_buffer));
706 /* creates unique filename and starts recording */
707 void rec_new_file(void)
709 audio_new_file(rec_create_filename(path_buffer));
712 /* used in trigger_listerner and recording_screen */
713 static unsigned int last_seconds = 0;
716 * Callback function so that the peak meter code can send an event
717 * to this application. This function can be passed to
718 * peak_meter_set_trigger_listener in order to activate the trigger.
720 static void trigger_listener(int trigger_status)
722 switch (trigger_status)
724 case TRIG_GO:
725 if(!(audio_status() & AUDIO_STATUS_RECORD))
727 rec_status |= RCSTAT_HAVE_RECORDED;
728 rec_record();
729 #if CONFIG_CODEC != SWCODEC
730 /* give control to mpeg thread so that it can start
731 recording */
732 yield(); yield(); yield();
733 #endif
736 /* if we're already recording this is a retrigger */
737 else
739 if((audio_status() & AUDIO_STATUS_PAUSE) &&
740 (global_settings.rec_trigger_type == 1))
741 audio_resume_recording();
742 /* New file on trig start*/
743 else if (global_settings.rec_trigger_type != 2)
745 rec_new_file();
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 0: /* Stop */
759 audio_stop_recording();
760 break;
762 case 1: /* Pause */
763 audio_pause_recording();
764 break;
766 case 2: /* New file on trig stop*/
767 rec_new_file();
768 /* tell recording_screen to reset the time */
769 last_seconds = 0;
770 break;
773 if (global_settings.rec_trigger_mode != TRIG_MODE_REARM)
775 peak_meter_set_trigger_listener(NULL);
776 peak_meter_trigger(false);
779 break;
783 bool recording_start_automatic = false;
785 bool recording_screen(bool no_source)
787 long button;
788 bool done = false;
789 char buf[32];
790 char buf2[32];
791 int w, h;
792 int update_countdown = 1;
793 unsigned int seconds;
794 int hours, minutes;
795 char filename[13];
796 int last_audio_stat = -1;
797 int audio_stat;
798 #if CONFIG_CODEC == SWCODEC
799 int warning_counter = 0;
800 #define WARNING_PERIOD 7
801 #endif
802 #ifdef HAVE_FMRADIO_REC
803 /* Radio is left on if:
804 * 1) Is was on at the start and the initial source is FM Radio
805 * 2) 1) and the source was never changed to something else
807 int radio_status = (global_settings.rec_source != AUDIO_SRC_FMRADIO) ?
808 FMRADIO_OFF : get_radio_status();
809 #endif
810 #if (CONFIG_LED == LED_REAL)
811 bool led_state = false;
812 int led_countdown = 2;
813 #endif
814 #ifdef HAVE_AGC
815 bool peak_read = false;
816 bool peak_valid = false;
817 int peak_l, peak_r;
818 int balance = 0;
819 bool display_agc[NB_SCREENS];
820 #endif
821 int line[NB_SCREENS];
822 int i;
823 int filename_offset[NB_SCREENS];
824 int pm_y[NB_SCREENS];
825 int trig_xpos[NB_SCREENS];
826 int trig_ypos[NB_SCREENS];
827 int trig_width[NB_SCREENS];
829 static const unsigned char *byte_units[] = {
830 ID2P(LANG_BYTE),
831 ID2P(LANG_KILOBYTE),
832 ID2P(LANG_MEGABYTE),
833 ID2P(LANG_GIGABYTE)
836 struct audio_recording_options rec_options;
837 if (check_dir(global_settings.rec_directory) == false)
839 do {
840 gui_syncsplash(0, "%s %s",
841 str(LANG_REC_DIR_NOT_WRITABLE),
842 str(LANG_OFF_ABORT));
843 } while (action_userabort(HZ) == false);
844 return false;
847 rec_status = RCSTAT_IN_RECSCREEN;
848 cursor = 0;
849 #if (CONFIG_LED == LED_REAL) && !defined(SIMULATOR)
850 ata_set_led_enabled(false);
851 #endif
853 #if CONFIG_CODEC == SWCODEC
854 /* recording_menu gets messed up: so prevent manus talking */
855 talk_disable_menus();
856 /* audio_init_recording stops anything playing when it takes the audio
857 buffer */
858 #else
859 /* Yes, we use the D/A for monitoring */
860 peak_meter_enabled = true;
861 peak_meter_playback(true);
862 #endif
864 audio_init_recording(0);
865 sound_set_volume(global_settings.volume);
867 #ifdef HAVE_AGC
868 peak_meter_get_peakhold(&peak_l, &peak_r);
869 #endif
871 rec_init_recording_options(&rec_options);
872 rec_set_recording_options(&rec_options);
874 set_gain();
876 if(rec_create_directory() > 0)
877 rec_status |= RCSTAT_CREATED_DIRECTORY;
879 #if CONFIG_RTC == 0
880 /* Create new filename for recording start */
881 rec_init_filename();
882 #endif
884 settings_apply_trigger();
886 #ifdef HAVE_AGC
887 agc_preset_str[0] = str(LANG_SYSFONT_OFF);
888 agc_preset_str[1] = str(LANG_SYSFONT_AGC_SAFETY);
889 agc_preset_str[2] = str(LANG_SYSFONT_AGC_LIVE);
890 agc_preset_str[3] = str(LANG_SYSFONT_AGC_DJSET);
891 agc_preset_str[4] = str(LANG_SYSFONT_AGC_MEDIUM);
892 agc_preset_str[5] = str(LANG_SYSFONT_AGC_VOICE);
893 if (global_settings.rec_source == AUDIO_SRC_MIC) {
894 agc_preset = global_settings.rec_agc_preset_mic;
895 agc_maxgain = global_settings.rec_agc_maxgain_mic;
897 else {
898 agc_preset = global_settings.rec_agc_preset_line;
899 agc_maxgain = global_settings.rec_agc_maxgain_line;
901 #endif /* HAVE_AGC */
903 FOR_NB_SCREENS(i)
905 screens[i].setfont(FONT_SYSFIXED);
906 screens[i].getstringsize("M", &w, &h);
907 screens[i].setmargins(global_settings.invert_cursor ? 0 : w, 8);
908 filename_offset[i] = ((screens[i].height >= 80) ? 1 : 0);
909 pm_y[i] = 8 + h * (2 + filename_offset[i]);
912 #ifdef HAVE_REMOTE_LCD
913 if (!remote_display_on)
915 screens[1].clear_display();
916 snprintf(buf, sizeof(buf), str(LANG_REMOTE_LCD_ON));
917 screens[1].puts((screens[1].width/w - strlen(buf))/2 + 1,
918 screens[1].height/(h*2) + 1, buf);
919 screens[1].update();
920 gui_syncsplash(0, str(LANG_REMOTE_LCD_OFF));
922 #endif
924 while(!done)
926 audio_stat = audio_status();
928 #if (CONFIG_LED == LED_REAL)
931 * Flash the LED while waiting to record. Turn it on while
932 * recording.
934 if(audio_stat & AUDIO_STATUS_RECORD)
936 if (audio_stat & AUDIO_STATUS_PAUSE)
938 if (--led_countdown <= 0)
940 led_state = !led_state;
941 led(led_state);
942 led_countdown = 2;
945 else
947 /* trigger is on in status TRIG_READY (no check needed) */
948 led(true);
951 else
953 int trigStat = peak_meter_trigger_status();
955 * other trigger stati than trig_off and trig_steady
956 * already imply that we are recording.
958 if (trigStat == TRIG_STEADY)
960 if (--led_countdown <= 0)
962 led_state = !led_state;
963 led(led_state);
964 led_countdown = 2;
967 else
969 /* trigger is on in status TRIG_READY (no check needed) */
970 led(false);
973 #endif /* CONFIG_LED */
975 /* Wait for a button a while (HZ/10) drawing the peak meter */
976 button = peak_meter_draw_get_btn(0, pm_y, h * PM_HEIGHT, screen_update);
978 if (last_audio_stat != audio_stat)
980 if (audio_stat & AUDIO_STATUS_RECORD)
982 rec_status |= RCSTAT_HAVE_RECORDED;
984 last_audio_stat = audio_stat;
988 if (recording_start_automatic)
990 /* simulate a button press */
991 button = ACTION_REC_PAUSE;
992 recording_start_automatic = false;
995 switch(button)
997 #ifdef HAVE_REMOTE_LCD
998 case ACTION_REC_LCD:
999 if (remote_display_on)
1001 remote_display_on = false;
1002 screen_update = 1;
1003 screens[1].clear_display();
1004 snprintf(buf, sizeof(buf), str(LANG_REMOTE_LCD_ON));
1005 screens[1].puts((screens[1].width/w - strlen(buf))/2 + 1,
1006 screens[1].height/(h*2) + 1, buf);
1007 screens[1].update();
1008 gui_syncsplash(0, str(LANG_REMOTE_LCD_OFF));
1010 else
1012 remote_display_on = true;
1013 screen_update = NB_SCREENS;
1015 break;
1016 #endif
1017 case ACTION_STD_CANCEL:
1018 /* turn off the trigger */
1019 peak_meter_trigger(false);
1020 peak_meter_set_trigger_listener(NULL);
1022 if(audio_stat & AUDIO_STATUS_RECORD)
1024 audio_stop_recording();
1026 else
1028 #if CONFIG_CODEC != SWCODEC
1029 peak_meter_playback(true);
1030 peak_meter_enabled = false;
1031 #endif
1032 done = true;
1034 update_countdown = 1; /* Update immediately */
1035 break;
1037 case ACTION_REC_PAUSE:
1038 case ACTION_REC_NEWFILE:
1039 /* Only act if the mpeg is stopped */
1040 if(!(audio_stat & AUDIO_STATUS_RECORD))
1042 /* is this manual or triggered recording? */
1043 if ((global_settings.rec_trigger_mode == TRIG_MODE_OFF) ||
1044 (peak_meter_trigger_status() != TRIG_OFF))
1046 /* manual recording */
1047 rec_status |= RCSTAT_HAVE_RECORDED;
1048 rec_record();
1049 last_seconds = 0;
1050 if (global_settings.talk_menu)
1052 /* no voice possible here, but a beep */
1053 audio_beep(HZ/2); /* longer beep on start */
1056 /* this is triggered recording */
1057 else
1059 /* we don't start recording now, but enable the
1060 trigger and let the callback function
1061 trigger_listener control when the recording starts */
1062 peak_meter_trigger(true);
1063 peak_meter_set_trigger_listener(&trigger_listener);
1066 else
1068 /*if new file button pressed, start new file */
1069 if (button == ACTION_REC_NEWFILE)
1071 rec_new_file();
1072 last_seconds = 0;
1074 else
1075 /* if pause button pressed, pause or resume */
1077 if(audio_stat & AUDIO_STATUS_PAUSE)
1079 audio_resume_recording();
1080 if (global_settings.talk_menu)
1082 /* no voice possible here, but a beep */
1083 audio_beep(HZ/4); /* short beep on resume */
1086 else
1088 audio_pause_recording();
1092 update_countdown = 1; /* Update immediately */
1093 break;
1095 case ACTION_STD_PREV:
1096 cursor--;
1097 adjust_cursor();
1098 update_countdown = 1; /* Update immediately */
1099 break;
1101 case ACTION_STD_NEXT:
1102 cursor++;
1103 adjust_cursor();
1104 update_countdown = 1; /* Update immediately */
1105 break;
1107 case ACTION_SETTINGS_INC:
1108 switch(cursor)
1110 case 0:
1111 global_settings.volume++;
1112 setvol();
1113 break;
1114 case 1:
1115 if(global_settings.rec_source == AUDIO_SRC_MIC)
1117 if(global_settings.rec_mic_gain <
1118 sound_max(SOUND_MIC_GAIN))
1119 global_settings.rec_mic_gain++;
1121 else
1123 if(global_settings.rec_left_gain <
1124 sound_max(SOUND_LEFT_GAIN))
1125 global_settings.rec_left_gain++;
1126 if(global_settings.rec_right_gain <
1127 sound_max(SOUND_RIGHT_GAIN))
1128 global_settings.rec_right_gain++;
1130 break;
1131 case 2:
1132 if(global_settings.rec_left_gain <
1133 sound_max(SOUND_LEFT_GAIN))
1134 global_settings.rec_left_gain++;
1135 break;
1136 case 3:
1137 if(global_settings.rec_right_gain <
1138 sound_max(SOUND_RIGHT_GAIN))
1139 global_settings.rec_right_gain++;
1140 break;
1141 #ifdef HAVE_AGC
1142 case 4:
1143 agc_preset = MIN(agc_preset + 1, AGC_MODE_SIZE);
1144 agc_enable = (agc_preset != 0);
1145 if (global_settings.rec_source == AUDIO_SRC_MIC) {
1146 global_settings.rec_agc_preset_mic = agc_preset;
1147 agc_maxgain = global_settings.rec_agc_maxgain_mic;
1148 } else {
1149 global_settings.rec_agc_preset_line = agc_preset;
1150 agc_maxgain = global_settings.rec_agc_maxgain_line;
1152 break;
1153 case 5:
1154 if (global_settings.rec_source == AUDIO_SRC_MIC)
1156 agc_maxgain = MIN(agc_maxgain + 1,
1157 sound_max(SOUND_MIC_GAIN));
1158 global_settings.rec_agc_maxgain_mic = agc_maxgain;
1160 else
1162 agc_maxgain = MIN(agc_maxgain + 1,
1163 sound_max(SOUND_LEFT_GAIN));
1164 global_settings.rec_agc_maxgain_line = agc_maxgain;
1166 break;
1167 #endif /* HAVE_AGC */
1169 set_gain();
1170 update_countdown = 1; /* Update immediately */
1171 break;
1173 case ACTION_SETTINGS_DEC:
1174 switch(cursor)
1176 case 0:
1177 global_settings.volume--;
1178 setvol();
1179 break;
1180 case 1:
1181 if(global_settings.rec_source == AUDIO_SRC_MIC)
1183 if(global_settings.rec_mic_gain >
1184 sound_min(SOUND_MIC_GAIN))
1185 global_settings.rec_mic_gain--;
1187 else
1189 if(global_settings.rec_left_gain >
1190 sound_min(SOUND_LEFT_GAIN))
1191 global_settings.rec_left_gain--;
1192 if(global_settings.rec_right_gain >
1193 sound_min(SOUND_RIGHT_GAIN))
1194 global_settings.rec_right_gain--;
1196 break;
1197 case 2:
1198 if(global_settings.rec_left_gain >
1199 sound_min(SOUND_LEFT_GAIN))
1200 global_settings.rec_left_gain--;
1201 break;
1202 case 3:
1203 if(global_settings.rec_right_gain >
1204 sound_min(SOUND_RIGHT_GAIN))
1205 global_settings.rec_right_gain--;
1206 break;
1207 #ifdef HAVE_AGC
1208 case 4:
1209 agc_preset = MAX(agc_preset - 1, 0);
1210 agc_enable = (agc_preset != 0);
1211 if (global_settings.rec_source == AUDIO_SRC_MIC) {
1212 global_settings.rec_agc_preset_mic = agc_preset;
1213 agc_maxgain = global_settings.rec_agc_maxgain_mic;
1214 } else {
1215 global_settings.rec_agc_preset_line = agc_preset;
1216 agc_maxgain = global_settings.rec_agc_maxgain_line;
1218 break;
1219 case 5:
1220 if (global_settings.rec_source == AUDIO_SRC_MIC)
1222 agc_maxgain = MAX(agc_maxgain - 1,
1223 sound_min(SOUND_MIC_GAIN));
1224 global_settings.rec_agc_maxgain_mic = agc_maxgain;
1226 else
1228 agc_maxgain = MAX(agc_maxgain - 1,
1229 sound_min(SOUND_LEFT_GAIN));
1230 global_settings.rec_agc_maxgain_line = agc_maxgain;
1232 break;
1233 #endif /* HAVE_AGC */
1235 set_gain();
1236 update_countdown = 1; /* Update immediately */
1237 break;
1239 case ACTION_STD_MENU:
1240 #if CONFIG_CODEC == SWCODEC
1241 if(!(audio_stat & AUDIO_STATUS_RECORD))
1242 #else
1243 if(audio_stat != AUDIO_STATUS_RECORD)
1244 #endif
1246 #ifdef HAVE_FMRADIO_REC
1247 const int prev_rec_source = global_settings.rec_source;
1248 #endif
1250 #if (CONFIG_LED == LED_REAL)
1251 /* led is restored at begin of loop / end of function */
1252 led(false);
1253 #endif
1254 if (recording_menu(no_source))
1256 done = true;
1257 rec_status |= RCSTAT_BEEN_IN_USB_MODE;
1258 #ifdef HAVE_FMRADIO_REC
1259 radio_status = FMRADIO_OFF;
1260 #endif
1262 else
1264 #ifdef HAVE_FMRADIO_REC
1265 /* If input changes away from FM Radio, radio will
1266 remain off when recording screen closes. */
1267 if (global_settings.rec_source != prev_rec_source
1268 && prev_rec_source == AUDIO_SRC_FMRADIO)
1269 radio_status = FMRADIO_OFF;
1270 #endif
1272 #if CONFIG_CODEC == SWCODEC
1273 /* reinit after submenu exit */
1274 audio_close_recording();
1275 audio_init_recording(0);
1276 #endif
1278 rec_init_recording_options(&rec_options);
1279 rec_set_recording_options(&rec_options);
1281 if(rec_create_directory() > 0)
1282 rec_status |= RCSTAT_CREATED_DIRECTORY;
1284 #if CONFIG_CODEC == SWCODEC && CONFIG_RTC == 0
1285 /* If format changed, a new number is required */
1286 rec_init_filename();
1287 #endif
1289 #ifdef HAVE_AGC
1290 if (global_settings.rec_source == AUDIO_SRC_MIC) {
1291 agc_preset = global_settings.rec_agc_preset_mic;
1292 agc_maxgain = global_settings.rec_agc_maxgain_mic;
1294 else {
1295 agc_preset = global_settings.rec_agc_preset_line;
1296 agc_maxgain = global_settings.rec_agc_maxgain_line;
1298 #endif
1300 adjust_cursor();
1301 set_gain();
1302 update_countdown = 1; /* Update immediately */
1304 FOR_NB_SCREENS(i)
1306 screens[i].setfont(FONT_SYSFIXED);
1307 screens[i].setmargins(
1308 global_settings.invert_cursor ? 0 : w, 8);
1312 break;
1314 #if CONFIG_KEYPAD == RECORDER_PAD
1315 case ACTION_REC_F2:
1316 if(audio_stat != AUDIO_STATUS_RECORD)
1318 #if (CONFIG_LED == LED_REAL)
1319 /* led is restored at begin of loop / end of function */
1320 led(false);
1321 #endif
1322 if (f2_rec_screen())
1324 rec_status |= RCSTAT_HAVE_RECORDED;
1325 done = true;
1327 else
1328 update_countdown = 1; /* Update immediately */
1330 break;
1332 case ACTION_REC_F3:
1333 if(audio_stat & AUDIO_STATUS_RECORD)
1335 rec_new_file();
1336 last_seconds = 0;
1338 else
1340 #if (CONFIG_LED == LED_REAL)
1341 /* led is restored at begin of loop / end of function */
1342 led(false);
1343 #endif
1344 if (f3_rec_screen())
1346 rec_status |= RCSTAT_HAVE_RECORDED;
1347 done = true;
1349 else
1350 update_countdown = 1; /* Update immediately */
1352 break;
1353 #endif /* CONFIG_KEYPAD == RECORDER_PAD */
1355 case SYS_USB_CONNECTED:
1356 /* Only accept USB connection when not recording */
1357 if(!(audio_stat & AUDIO_STATUS_RECORD))
1359 default_event_handler(SYS_USB_CONNECTED);
1360 done = true;
1361 rec_status |= RCSTAT_BEEN_IN_USB_MODE;
1362 #ifdef HAVE_FMRADIO_REC
1363 radio_status = FMRADIO_OFF;
1364 #endif
1366 break;
1368 default:
1369 default_event_handler(button);
1370 break;
1371 } /* end switch */
1373 #ifdef HAVE_AGC
1374 peak_read = !peak_read;
1375 if (peak_read) { /* every 2nd run of loop */
1376 peak_time++;
1377 peak_valid = read_peak_levels(&peak_l, &peak_r, &balance);
1380 /* Handle AGC every 200ms when enabled and peak data is valid */
1381 if (peak_read && agc_enable && peak_valid)
1382 auto_gain_control(&peak_l, &peak_r, &balance);
1383 #endif
1385 FOR_NB_SCREENS(i)
1386 screens[i].setfont(FONT_SYSFIXED);
1388 seconds = audio_recorded_time() / HZ;
1390 update_countdown--;
1391 if(update_countdown == 0 || seconds > last_seconds)
1393 unsigned int dseconds, dhours, dminutes;
1394 unsigned long num_recorded_bytes, dsize, dmb;
1395 int pos = 0;
1397 update_countdown = 5;
1398 last_seconds = seconds;
1400 dseconds = rec_timesplit_seconds();
1401 dsize = rec_sizesplit_bytes();
1402 num_recorded_bytes = audio_num_recorded_bytes();
1404 for(i = 0; i < screen_update; i++)
1405 screens[i].clear_display();
1407 #if CONFIG_CODEC == SWCODEC
1408 if ((audio_stat & AUDIO_STATUS_WARNING)
1409 && (warning_counter++ % WARNING_PERIOD) < WARNING_PERIOD/2)
1411 /* Switch back and forth displaying warning on first available
1412 line to ensure visibility - the motion should also help
1413 draw attention */
1414 /* Don't use language string unless agreed upon to make this
1415 method permanent - could do something in the statusbar */
1416 snprintf(buf, sizeof(buf), "Warning: %08X",
1417 pcm_rec_get_warnings());
1419 else
1420 #endif /* CONFIG_CODEC == SWCODEC */
1421 if ((global_settings.rec_sizesplit) && (global_settings.rec_split_method))
1423 dmb = dsize/1024/1024;
1424 snprintf(buf, sizeof(buf), "%s %dMB",
1425 str(LANG_SYSFONT_SPLIT_SIZE), dmb);
1427 else
1429 hours = seconds / 3600;
1430 minutes = (seconds - (hours * 3600)) / 60;
1431 snprintf(buf, sizeof(buf), "%s %02d:%02d:%02d",
1432 str(LANG_SYSFONT_RECORDING_TIME),
1433 hours, minutes, seconds%60);
1436 for(i = 0; i < screen_update; i++)
1437 screens[i].puts(0, 0, buf);
1439 if(audio_stat & AUDIO_STATUS_PRERECORD)
1441 snprintf(buf, sizeof(buf), "%s...", str(LANG_SYSFONT_RECORD_PRERECORD));
1443 else
1445 /* Display the split interval if the record timesplit
1446 is active */
1447 if ((global_settings.rec_timesplit) && !(global_settings.rec_split_method))
1449 /* Display the record timesplit interval rather
1450 than the file size if the record timer is
1451 active */
1452 dhours = dseconds / 3600;
1453 dminutes = (dseconds - (dhours * 3600)) / 60;
1454 snprintf(buf, sizeof(buf), "%s %02d:%02d",
1455 str(LANG_SYSFONT_RECORD_TIMESPLIT_REC),
1456 dhours, dminutes);
1458 else
1460 output_dyn_value(buf2, sizeof buf2,
1461 num_recorded_bytes,
1462 byte_units, true);
1463 snprintf(buf, sizeof(buf), "%s %s",
1464 str(LANG_SYSFONT_RECORDING_SIZE), buf2);
1467 for(i = 0; i < screen_update; i++)
1468 screens[i].puts(0, 1, buf);
1470 for(i = 0; i < screen_update; i++)
1472 if (filename_offset[i] > 0)
1474 *filename = '\0';
1475 if (audio_stat & AUDIO_STATUS_RECORD)
1477 strncpy(filename, path_buffer +
1478 strlen(path_buffer) - 12, 13);
1479 filename[12]='\0';
1482 snprintf(buf, sizeof(buf), "%s %s",
1483 str(LANG_SYSFONT_RECORDING_FILENAME), filename);
1484 screens[i].puts(0, 2, buf);
1488 /* We will do file splitting regardless, either at the end of
1489 a split interval, or when the filesize approaches the 2GB
1490 FAT file size (compatibility) limit. */
1491 if ((audio_stat && !(global_settings.rec_split_method)
1492 && global_settings.rec_timesplit && (seconds >= dseconds))
1493 || (audio_stat && global_settings.rec_split_method
1494 && global_settings.rec_sizesplit && (num_recorded_bytes >= dsize))
1495 || (num_recorded_bytes >= MAX_FILE_SIZE))
1497 if (!(global_settings.rec_split_type)
1498 || (num_recorded_bytes >= MAX_FILE_SIZE))
1500 rec_new_file();
1501 last_seconds = 0;
1503 else
1505 peak_meter_trigger(false);
1506 peak_meter_set_trigger_listener(NULL);
1507 audio_stop_recording();
1509 update_countdown = 1;
1512 snprintf(buf, sizeof(buf), "%s: %s", str(LANG_SYSFONT_VOLUME),
1513 fmt_gain(SOUND_VOLUME,
1514 global_settings.volume,
1515 buf2, sizeof(buf2)));
1517 if (global_settings.invert_cursor && (pos++ == cursor))
1519 for(i = 0; i < screen_update; i++)
1520 screens[i].puts_style_offset(0, filename_offset[i] +
1521 PM_HEIGHT + 2, buf, STYLE_INVERT,0);
1523 else
1525 for(i = 0; i < screen_update; i++)
1526 screens[i].puts(0, filename_offset[i] + PM_HEIGHT + 2, buf);
1529 if(global_settings.rec_source == AUDIO_SRC_MIC)
1531 /* Draw MIC recording gain */
1532 snprintf(buf, sizeof(buf), "%s:%s", str(LANG_SYSFONT_RECORDING_GAIN),
1533 fmt_gain(SOUND_MIC_GAIN,
1534 global_settings.rec_mic_gain,
1535 buf2, sizeof(buf2)));
1536 if(global_settings.invert_cursor && ((1==cursor)||(2==cursor)))
1538 for(i = 0; i < screen_update; i++)
1539 screens[i].puts_style_offset(0, filename_offset[i] +
1540 PM_HEIGHT + 3, buf, STYLE_INVERT,0);
1542 else
1544 for(i = 0; i < screen_update; i++)
1545 screens[i].puts(0, filename_offset[i] +
1546 PM_HEIGHT + 3, buf);
1549 else if(0
1550 HAVE_LINE_REC_( || global_settings.rec_source == AUDIO_SRC_LINEIN)
1551 HAVE_FMRADIO_REC_( || global_settings.rec_source == AUDIO_SRC_FMRADIO)
1554 /* Draw LINE or FMRADIO recording gain */
1555 snprintf(buf, sizeof(buf), "%s:%s",
1556 str(LANG_SYSFONT_RECORDING_LEFT),
1557 fmt_gain(SOUND_LEFT_GAIN,
1558 global_settings.rec_left_gain,
1559 buf2, sizeof(buf2)));
1560 if(global_settings.invert_cursor && ((1==cursor)||(2==cursor)))
1562 for(i = 0; i < screen_update; i++)
1563 screens[i].puts_style_offset(0, filename_offset[i] +
1564 PM_HEIGHT + 3, buf, STYLE_INVERT,0);
1566 else
1568 for(i = 0; i < screen_update; i++)
1569 screens[i].puts(0, filename_offset[i] +
1570 PM_HEIGHT + 3, buf);
1573 snprintf(buf, sizeof(buf), "%s:%s",
1574 str(LANG_SYSFONT_RECORDING_RIGHT),
1575 fmt_gain(SOUND_RIGHT_GAIN,
1576 global_settings.rec_right_gain,
1577 buf2, sizeof(buf2)));
1578 if(global_settings.invert_cursor && ((1==cursor)||(3==cursor)))
1580 for(i = 0; i < screen_update; i++)
1581 screens[i].puts_style_offset(0, filename_offset[i] +
1582 PM_HEIGHT + 4, buf, STYLE_INVERT,0);
1584 else
1586 for(i = 0; i < screen_update; i++)
1587 screens[i].puts(0, filename_offset[i] +
1588 PM_HEIGHT + 4, buf);
1592 FOR_NB_SCREENS(i)
1594 switch (global_settings.rec_source)
1596 HAVE_LINE_REC_(case AUDIO_SRC_LINEIN:)
1597 HAVE_FMRADIO_REC_(case AUDIO_SRC_FMRADIO:)
1598 line[i] = 5;
1599 break;
1601 case AUDIO_SRC_MIC:
1602 line[i] = 4;
1603 break;
1604 #ifdef HAVE_SPDIF_REC
1605 case AUDIO_SRC_SPDIF:
1606 line[i] = 3;
1607 break;
1608 #endif
1609 default:
1610 line[i] = 5; /* to prevent uninitialisation
1611 warnings for line[0] */
1612 break;
1613 } /* end switch */
1614 #ifdef HAVE_AGC
1615 if (screens[i].height < h * (2 + filename_offset[i] + PM_HEIGHT + line[i]))
1617 line[i] -= 1;
1618 display_agc[i] = false;
1620 else
1621 display_agc[i] = true;
1623 if ((cursor==4) || (cursor==5))
1624 display_agc[i] = true;
1627 /************** AGC test info ******************
1628 snprintf(buf, sizeof(buf), "D:%d U:%d",
1629 (agc_droptime+2)/5, (agc_risetime+2)/5);
1630 lcd_putsxy(1, LCD_HEIGHT - 8, buf);
1631 snprintf(buf, sizeof(buf), "B:%d",
1632 (agc_baltime+2)/5);
1633 lcd_putsxy(LCD_WIDTH/2 + 3, LCD_HEIGHT - 8, buf);
1634 ***********************************************/
1636 if (cursor == 5)
1637 snprintf(buf, sizeof(buf), "%s: %s",
1638 str(LANG_SYSFONT_RECORDING_AGC_MAXGAIN),
1639 fmt_gain(SOUND_LEFT_GAIN,
1640 agc_maxgain, buf2, sizeof(buf2)));
1641 else if (agc_preset == 0)
1642 snprintf(buf, sizeof(buf), "%s: %s",
1643 str(LANG_SYSFONT_RECORDING_AGC_PRESET),
1644 agc_preset_str[agc_preset]);
1645 else if (global_settings.rec_source == AUDIO_SRC_MIC)
1646 snprintf(buf, sizeof(buf), "%s: %s%s",
1647 str(LANG_SYSFONT_RECORDING_AGC_PRESET),
1648 agc_preset_str[agc_preset],
1649 fmt_gain(SOUND_LEFT_GAIN,
1650 agc_maxgain -
1651 global_settings.rec_mic_gain,
1652 buf2, sizeof(buf2)));
1653 else
1654 snprintf(buf, sizeof(buf), "%s: %s%s",
1655 str(LANG_SYSFONT_RECORDING_AGC_PRESET),
1656 agc_preset_str[agc_preset],
1657 fmt_gain(SOUND_LEFT_GAIN,
1658 agc_maxgain -
1659 (global_settings.rec_left_gain +
1660 global_settings.rec_right_gain)/2,
1661 buf2, sizeof(buf2)));
1663 if(global_settings.invert_cursor && ((cursor==4) || (cursor==5)))
1665 for(i = 0; i < screen_update; i++)
1666 screens[i].puts_style_offset(0, filename_offset[i] +
1667 PM_HEIGHT + line[i], buf, STYLE_INVERT,0);
1669 else if (
1670 global_settings.rec_source == AUDIO_SRC_MIC
1671 HAVE_LINE_REC_(|| global_settings.rec_source == AUDIO_SRC_LINEIN)
1672 HAVE_FMRADIO_REC_(|| global_settings.rec_source == AUDIO_SRC_FMRADIO)
1675 for(i = 0; i < screen_update; i++) {
1676 if (display_agc[i]) {
1677 screens[i].puts(0, filename_offset[i] +
1678 PM_HEIGHT + line[i], buf);
1683 if (global_settings.rec_source == AUDIO_SRC_MIC)
1685 if(agc_maxgain < (global_settings.rec_mic_gain))
1686 change_recording_gain(false, true, true);
1688 else
1690 if(agc_maxgain < (global_settings.rec_left_gain))
1691 change_recording_gain(false, true, false);
1692 if(agc_maxgain < (global_settings.rec_right_gain))
1693 change_recording_gain(false, false, true);
1695 #else /* !HAVE_AGC */
1697 #endif /* HAVE_AGC */
1699 if(!global_settings.invert_cursor) {
1700 switch(cursor)
1702 case 1:
1703 for(i = 0; i < screen_update; i++)
1704 screen_put_cursorxy(&screens[i], 0,
1705 filename_offset[i] +
1706 PM_HEIGHT + 3, true);
1708 if(global_settings.rec_source != AUDIO_SRC_MIC)
1710 for(i = 0; i < screen_update; i++)
1711 screen_put_cursorxy(&screens[i], 0,
1712 filename_offset[i] +
1713 PM_HEIGHT + 4, true);
1715 break;
1716 case 2:
1717 for(i = 0; i < screen_update; i++)
1718 screen_put_cursorxy(&screens[i], 0,
1719 filename_offset[i] +
1720 PM_HEIGHT + 3, true);
1721 break;
1722 case 3:
1723 for(i = 0; i < screen_update; i++)
1724 screen_put_cursorxy(&screens[i], 0,
1725 filename_offset[i] +
1726 PM_HEIGHT + 4, true);
1727 break;
1728 #ifdef HAVE_AGC
1729 case 4:
1730 case 5:
1731 for(i = 0; i < screen_update; i++)
1732 screen_put_cursorxy(&screens[i], 0,
1733 filename_offset[i] +
1734 PM_HEIGHT + line[i], true);
1735 break;
1736 #endif /* HAVE_AGC */
1737 default:
1738 for(i = 0; i < screen_update; i++)
1739 screen_put_cursorxy(&screens[i], 0,
1740 filename_offset[i] +
1741 PM_HEIGHT + 2, true);
1745 #ifdef HAVE_AGC
1746 hist_time++;
1747 #endif
1749 for(i = 0; i < screen_update; i++)
1751 gui_statusbar_draw(&(statusbars.statusbars[i]), true);
1752 peak_meter_screen(&screens[i], 0, pm_y[i], h*PM_HEIGHT);
1753 screens[i].update();
1756 /* draw the trigger status */
1757 FOR_NB_SCREENS(i)
1759 trig_width[i] = ((screens[i].height < 64) ||
1760 ((screens[i].height < 72) && (PM_HEIGHT > 1))) ?
1761 screens[i].width - 14 * w : screens[i].width;
1762 trig_xpos[i] = screens[i].width - trig_width[i];
1763 trig_ypos[i] = ((screens[i].height < 72) && (PM_HEIGHT > 1)) ?
1764 h*2 :
1765 h*(1 + filename_offset[i] + PM_HEIGHT + line[i]
1766 #ifdef HAVE_AGC
1768 #endif
1772 if (peak_meter_trigger_status() != TRIG_OFF)
1774 peak_meter_draw_trig(trig_xpos, trig_ypos, trig_width,
1775 screen_update);
1776 for(i = 0; i < screen_update; i++){
1777 screens[i].update_rect(trig_xpos[i], trig_ypos[i],
1778 trig_width[i] + 2, TRIG_HEIGHT);
1783 if(audio_stat & AUDIO_STATUS_ERROR)
1785 done = true;
1787 } /* end while(!done) */
1789 audio_stat = audio_status();
1790 if (audio_stat & AUDIO_STATUS_ERROR)
1792 gui_syncsplash(0, str(LANG_SYSFONT_DISK_FULL));
1793 gui_syncstatusbar_draw(&statusbars, true);
1795 FOR_NB_SCREENS(i)
1796 screens[i].update();
1798 audio_error_clear();
1800 while(1)
1802 if (action_userabort(TIMEOUT_NOBLOCK))
1803 break;
1807 #if CONFIG_CODEC == SWCODEC
1808 audio_stop_recording();
1809 audio_close_recording();
1811 #ifdef HAVE_FMRADIO_REC
1812 if (radio_status != FMRADIO_OFF)
1813 /* Restore radio playback - radio_status should be unchanged if started
1814 through fm radio screen (barring usb connect) */
1815 rec_set_source(AUDIO_SRC_FMRADIO, (radio_status & FMRADIO_PAUSED) ?
1816 SRCF_FMRADIO_PAUSED : SRCF_FMRADIO_PLAYING);
1817 else
1818 #endif
1819 /* Go back to playback mode */
1820 rec_set_source(AUDIO_SRC_PLAYBACK, SRCF_PLAYBACK);
1822 /* restore talk_menu setting */
1823 talk_enable_menus();
1824 #else /* !SWCODEC */
1825 audio_init_playback();
1826 #endif /* CONFIG_CODEC == SWCODEC */
1828 /* make sure the trigger is really turned off */
1829 peak_meter_trigger(false);
1830 peak_meter_set_trigger_listener(NULL);
1832 rec_status &= ~RCSTAT_IN_RECSCREEN;
1833 sound_settings_apply();
1835 FOR_NB_SCREENS(i)
1836 screens[i].setfont(FONT_UI);
1838 /* if the directory was created or recording happened, make sure the
1839 browser is updated */
1840 if (rec_status & (RCSTAT_CREATED_DIRECTORY | RCSTAT_HAVE_RECORDED))
1841 reload_directory();
1843 #if (CONFIG_LED == LED_REAL) && !defined(SIMULATOR)
1844 ata_set_led_enabled(true);
1845 #endif
1847 settings_save();
1849 return (rec_status & RCSTAT_BEEN_IN_USB_MODE) != 0;
1850 } /* recording_screen */
1852 #if CONFIG_KEYPAD == RECORDER_PAD
1853 static bool f2_rec_screen(void)
1855 static const char* const freq_str[6] =
1857 "44.1kHz",
1858 "48kHz",
1859 "32kHz",
1860 "22.05kHz",
1861 "24kHz",
1862 "16kHz"
1865 bool exit = false;
1866 bool used = false;
1867 int w, h, i;
1868 char buf[32];
1869 int button;
1870 struct audio_recording_options rec_options;
1872 FOR_NB_SCREENS(i)
1874 screens[i].setfont(FONT_SYSFIXED);
1875 screens[i].getstringsize("A",&w,&h);
1878 while (!exit) {
1879 const char* ptr=NULL;
1881 FOR_NB_SCREENS(i)
1883 screens[i].clear_display();
1885 /* Recording quality */
1886 screens[i].putsxy(0, LCD_HEIGHT/2 - h*2,
1887 str(LANG_SYSFONT_RECORDING_QUALITY));
1890 snprintf(buf, sizeof(buf), "%d", global_settings.rec_quality);
1891 FOR_NB_SCREENS(i)
1893 screens[i].putsxy(0, LCD_HEIGHT/2-h, buf);
1894 screens[i].mono_bitmap(bitmap_icons_7x8[Icon_FastBackward],
1895 LCD_WIDTH/2 - 16, LCD_HEIGHT/2 - 4, 7, 8);
1898 /* Frequency */
1899 snprintf(buf, sizeof buf, "%s:", str(LANG_SYSFONT_RECORDING_FREQUENCY));
1900 ptr = freq_str[global_settings.rec_frequency];
1901 FOR_NB_SCREENS(i)
1903 screens[i].getstringsize(buf,&w,&h);
1904 screens[i].putsxy((LCD_WIDTH-w)/2, LCD_HEIGHT - h*2, buf);
1905 screens[i].getstringsize(ptr, &w, &h);
1906 screens[i].putsxy((LCD_WIDTH-w)/2, LCD_HEIGHT - h, ptr);
1907 screens[i].mono_bitmap(bitmap_icons_7x8[Icon_DownArrow],
1908 LCD_WIDTH/2 - 3, LCD_HEIGHT - h*3, 7, 8);
1911 /* Channel mode */
1912 switch ( global_settings.rec_channels ) {
1913 case 0:
1914 ptr = str(LANG_SYSFONT_CHANNEL_STEREO);
1915 break;
1917 case 1:
1918 ptr = str(LANG_SYSFONT_CHANNEL_MONO);
1919 break;
1922 FOR_NB_SCREENS(i)
1924 screens[i].getstringsize(str(LANG_SYSFONT_RECORDING_CHANNELS), &w, &h);
1925 screens[i].putsxy(LCD_WIDTH - w, LCD_HEIGHT/2 - h*2,
1926 str(LANG_SYSFONT_RECORDING_CHANNELS));
1927 screens[i].getstringsize(str(LANG_SYSFONT_F2_MODE), &w, &h);
1928 screens[i].putsxy(LCD_WIDTH - w, LCD_HEIGHT/2 - h,
1929 str(LANG_SYSFONT_F2_MODE));
1930 screens[i].getstringsize(ptr, &w, &h);
1931 screens[i].putsxy(LCD_WIDTH - w, LCD_HEIGHT/2, ptr);
1932 screens[i].mono_bitmap(bitmap_icons_7x8[Icon_FastForward],
1933 LCD_WIDTH/2 + 8, LCD_HEIGHT/2 - 4, 7, 8);
1935 screens[i].update();
1938 button = button_get(true);
1939 switch (button) {
1940 case BUTTON_LEFT:
1941 case BUTTON_F2 | BUTTON_LEFT:
1942 global_settings.rec_quality++;
1943 if(global_settings.rec_quality > 7)
1944 global_settings.rec_quality = 0;
1945 used = true;
1946 break;
1948 case BUTTON_DOWN:
1949 case BUTTON_F2 | BUTTON_DOWN:
1950 global_settings.rec_frequency++;
1951 if(global_settings.rec_frequency > 5)
1952 global_settings.rec_frequency = 0;
1953 used = true;
1954 break;
1956 case BUTTON_RIGHT:
1957 case BUTTON_F2 | BUTTON_RIGHT:
1958 global_settings.rec_channels++;
1959 if(global_settings.rec_channels > 1)
1960 global_settings.rec_channels = 0;
1961 used = true;
1962 break;
1964 case BUTTON_F2 | BUTTON_REL:
1965 if ( used )
1966 exit = true;
1967 used = true;
1968 break;
1970 case BUTTON_F2 | BUTTON_REPEAT:
1971 used = true;
1972 break;
1974 default:
1975 if(default_event_handler(button) == SYS_USB_CONNECTED)
1976 return true;
1977 break;
1981 rec_init_recording_options(&rec_options);
1982 rec_set_recording_options(&rec_options);
1984 set_gain();
1986 settings_save();
1987 FOR_NB_SCREENS(i)
1988 screens[i].setfont(FONT_UI);
1990 return false;
1993 static bool f3_rec_screen(void)
1995 bool exit = false;
1996 bool used = false;
1997 int w, h, i;
1998 int button;
1999 char *src_str[] =
2001 str(LANG_SYSFONT_RECORDING_SRC_MIC),
2002 str(LANG_SYSFONT_RECORDING_SRC_LINE),
2003 str(LANG_SYSFONT_RECORDING_SRC_DIGITAL)
2005 struct audio_recording_options rec_options;
2007 FOR_NB_SCREENS(i)
2009 screens[i].setfont(FONT_SYSFIXED);
2010 screens[i].getstringsize("A",&w,&h);
2013 while (!exit) {
2014 char* ptr=NULL;
2015 ptr = src_str[global_settings.rec_source];
2016 FOR_NB_SCREENS(i)
2018 screens[i].clear_display();
2020 /* Recording source */
2021 screens[i].putsxy(0, LCD_HEIGHT/2 - h*2,
2022 str(LANG_SYSFONT_RECORDING_SOURCE));
2024 screens[i].getstringsize(ptr, &w, &h);
2025 screens[i].putsxy(0, LCD_HEIGHT/2-h, ptr);
2026 screens[i].mono_bitmap(bitmap_icons_7x8[Icon_FastBackward],
2027 LCD_WIDTH/2 - 16, LCD_HEIGHT/2 - 4, 7, 8);
2030 /* trigger setup */
2031 ptr = str(LANG_SYSFONT_RECORD_TRIGGER);
2032 FOR_NB_SCREENS(i)
2034 screens[i].getstringsize(ptr,&w,&h);
2035 screens[i].putsxy((LCD_WIDTH-w)/2, LCD_HEIGHT - h*2, ptr);
2036 screens[i].mono_bitmap(bitmap_icons_7x8[Icon_DownArrow],
2037 LCD_WIDTH/2 - 3, LCD_HEIGHT - h*3, 7, 8);
2039 screens[i].update();
2042 button = button_get(true);
2043 switch (button) {
2044 case BUTTON_DOWN:
2045 case BUTTON_F3 | BUTTON_DOWN:
2046 #ifndef SIMULATOR
2047 rectrigger();
2048 settings_apply_trigger();
2049 #endif
2050 exit = true;
2051 break;
2053 case BUTTON_LEFT:
2054 case BUTTON_F3 | BUTTON_LEFT:
2055 global_settings.rec_source++;
2056 if(global_settings.rec_source > AUDIO_SRC_MAX)
2057 global_settings.rec_source = 0;
2058 used = true;
2059 break;
2061 case BUTTON_F3 | BUTTON_REL:
2062 if ( used )
2063 exit = true;
2064 used = true;
2065 break;
2067 case BUTTON_F3 | BUTTON_REPEAT:
2068 used = true;
2069 break;
2071 default:
2072 if(default_event_handler(button) == SYS_USB_CONNECTED)
2073 return true;
2074 break;
2078 rec_init_recording_options(&rec_options);
2079 rec_set_recording_options(&rec_options);
2081 set_gain();
2083 settings_save();
2084 FOR_NB_SCREENS(i)
2085 screens[i].setfont(FONT_UI);
2087 return false;
2089 #endif /* CONFIG_KEYPAD == RECORDER_PAD */
2091 #if CONFIG_CODEC == SWCODEC
2092 void audio_beep(int duration)
2094 /* dummy */
2095 (void)duration;
2098 #ifdef SIMULATOR
2099 /* stubs for recording sim */
2100 void audio_init_recording(unsigned int buffer_offset)
2102 buffer_offset = buffer_offset;
2105 void audio_close_recording(void)
2109 unsigned long pcm_rec_get_warnings(void)
2111 return 0;
2114 unsigned long audio_recorded_time(void)
2116 return 123;
2119 unsigned long audio_num_recorded_bytes(void)
2121 return 5 * 1024 * 1024;
2124 void rec_set_source(int source, unsigned flags)
2126 source = source;
2127 flags = flags;
2130 void audio_set_recording_options(struct audio_recording_options *options)
2132 options = options;
2135 void audio_set_recording_gain(int left, int right, int type)
2137 left = left;
2138 right = right;
2139 type = type;
2142 void audio_record(const char *filename)
2144 filename = filename;
2147 void audio_new_file(const char *filename)
2149 filename = filename;
2152 void audio_stop_recording(void)
2156 void audio_pause_recording(void)
2160 void audio_resume_recording(void)
2164 #endif /* #ifdef SIMULATOR */
2165 #endif /* #ifdef CONFIG_CODEC == SWCODEC */
2167 #endif /* HAVE_RECORDING */