User configurable recording path (my patch in FS#7201). path defaults to / and can...
[Rockbox.git] / apps / recorder / recording.c
bloba085bbbc03d9124600702a3598f017c4467d49f1
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
73 /* recording screen status flags */
74 enum rec_status_flags
76 RCSTAT_IN_RECSCREEN = 0x00000001,
77 RCSTAT_BEEN_IN_USB_MODE = 0x00000002,
78 RCSTAT_CREATED_DIRECTORY = 0x00000004,
79 RCSTAT_HAVE_RECORDED = 0x00000008,
82 static int rec_status = 0;
84 bool in_recording_screen(void)
86 return (rec_status & RCSTAT_IN_RECSCREEN) != 0;
89 #define PM_HEIGHT ((LCD_HEIGHT >= 72) ? 2 : 1)
91 #if CONFIG_KEYPAD == RECORDER_PAD
92 static bool f2_rec_screen(void);
93 static bool f3_rec_screen(void);
94 #endif
96 #define MAX_FILE_SIZE 0x7F800000 /* 2 GB - 4 MB */
98 static int screen_update = NB_SCREENS;
99 #ifdef HAVE_REMOTE_LCD
100 static bool remote_display_on = true;
101 #endif
103 /** File name creation **/
104 #if CONFIG_RTC == 0
105 /* current file number to assist in creating unique numbered filenames
106 without actually having to create the file on disk */
107 static int file_number = -1;
108 #endif /* CONFIG_RTC */
110 #if CONFIG_CODEC == SWCODEC
112 #define REC_FILE_ENDING(rec_format) \
113 (audio_formats[rec_format_afmt[rec_format]].ext_list)
115 #else /* CONFIG_CODEC != SWCODEC */
117 /* default record file extension for HWCODEC */
118 #define REC_FILE_ENDING(rec_format) \
119 (audio_formats[AFMT_MPA_L3].ext_list)
121 #endif /* CONFIG_CODEC == SWCODEC */
123 /* path for current file */
124 static char path_buffer[MAX_PATH];
126 /** Automatic Gain Control (AGC) **/
127 #ifdef HAVE_AGC
128 /* Timing counters:
129 * peak_time is incremented every 0.2s, every 2nd run of record screen loop.
130 * hist_time is incremented every 0.5s, display update.
131 * peak_time is the counter of the peak hold read and agc process,
132 * overflow every 13 years 8-)
134 static long peak_time = 0;
135 static long hist_time = 0;
137 static short peak_valid_mem[4];
138 #define BAL_MEM_SIZE 24
139 static short balance_mem[BAL_MEM_SIZE];
141 /* Automatic Gain Control */
142 #define AGC_MODE_SIZE 5
143 #define AGC_SAFETY_MODE 0
145 static const char* agc_preset_str[] =
146 { "Off", "S", "L", "D", "M", "V" };
147 /* "Off",
148 "Safety (clip)",
149 "Live (slow)",
150 "DJ-Set (slow)",
151 "Medium",
152 "Voice (fast)" */
153 #define AGC_CLIP 32766
154 #define AGC_PEAK 29883 /* fast gain reduction threshold -0.8dB */
155 #define AGC_HIGH 27254 /* accelerated gain reduction threshold -1.6dB */
156 #define AGC_IMG 823 /* threshold for balance control -32dB */
157 /* autogain high level thresholds (-3dB, -7dB, -4dB, -5dB, -5dB) */
158 static const short agc_th_hi[AGC_MODE_SIZE] =
159 { 23197, 14637, 21156, 18428, 18426 };
160 /* autogain low level thresholds (-14dB, -11dB, -6dB, -7dB, -8dB) */
161 static const short agc_th_lo[AGC_MODE_SIZE] =
162 { 6538, 9235, 16422, 14636, 13045 };
163 /* autogain threshold times [1/5s] or [200ms] */
164 static const short agc_tdrop[AGC_MODE_SIZE] =
165 { 900, 225, 150, 60, 8 };
166 static const short agc_trise[AGC_MODE_SIZE] =
167 { 9000, 750, 400, 150, 20 };
168 static const short agc_tbal[AGC_MODE_SIZE] =
169 { 4500, 500, 300, 100, 15 };
170 /* AGC operation */
171 static bool agc_enable = true;
172 static short agc_preset;
173 /* AGC levels */
174 static int agc_left = 0;
175 static int agc_right = 0;
176 /* AGC time since high target volume was exceeded */
177 static short agc_droptime = 0;
178 /* AGC time since volume fallen below low target */
179 static short agc_risetime = 0;
180 /* AGC balance time exceeding +/- 0.7dB */
181 static short agc_baltime = 0;
182 /* AGC maximum gain */
183 static short agc_maxgain;
184 #endif /* HAVE_AGC */
186 static void set_gain(void)
188 if(global_settings.rec_source == AUDIO_SRC_MIC)
190 audio_set_recording_gain(global_settings.rec_mic_gain,
191 0, AUDIO_GAIN_MIC);
193 else
195 /* AUDIO_SRC_LINEIN, AUDIO_SRC_FMRADIO, AUDIO_SRC_SPDIF */
196 audio_set_recording_gain(global_settings.rec_left_gain,
197 global_settings.rec_right_gain,
198 AUDIO_GAIN_LINEIN);
202 #ifdef HAVE_AGC
203 /* Read peak meter values & calculate balance.
204 * Returns validity of peak values.
205 * Used for automatic gain control and history diagram.
207 static bool read_peak_levels(int *peak_l, int *peak_r, int *balance)
209 peak_meter_get_peakhold(peak_l, peak_r);
210 peak_valid_mem[peak_time % 3] = *peak_l;
211 if (((peak_valid_mem[0] == peak_valid_mem[1]) &&
212 (peak_valid_mem[1] == peak_valid_mem[2])) &&
213 ((*peak_l < 32767)
214 #ifndef SIMULATOR
215 || ata_disk_is_active()
216 #endif
218 return false;
220 if (*peak_r > *peak_l)
221 balance_mem[peak_time % BAL_MEM_SIZE] = (*peak_l ?
222 MIN((10000 * *peak_r) / *peak_l - 10000, 15118) : 15118);
223 else
224 balance_mem[peak_time % BAL_MEM_SIZE] = (*peak_r ?
225 MAX(10000 - (10000 * *peak_l) / *peak_r, -15118) : -15118);
226 *balance = 0;
227 int i;
228 for (i = 0; i < BAL_MEM_SIZE; i++)
229 *balance += balance_mem[i];
230 *balance = *balance / BAL_MEM_SIZE;
232 return true;
235 /* AGC helper function to check if maximum gain is reached */
236 static bool agc_gain_is_max(bool left, bool right)
238 /* range -128...+108 [0.5dB] */
239 short gain_current_l;
240 short gain_current_r;
242 if (agc_preset == 0)
243 return false;
245 switch (global_settings.rec_source)
247 HAVE_LINE_REC_(case AUDIO_SRC_LINEIN:)
248 HAVE_FMRADIO_REC_(case AUDIO_SRC_FMRADIO:)
249 gain_current_l = global_settings.rec_left_gain;
250 gain_current_r = global_settings.rec_right_gain;
251 break;
252 case AUDIO_SRC_MIC:
253 default:
254 gain_current_l = global_settings.rec_mic_gain;
255 gain_current_r = global_settings.rec_mic_gain;
258 return ((left && (gain_current_l >= agc_maxgain)) ||
259 (right && (gain_current_r >= agc_maxgain)));
262 static void change_recording_gain(bool increment, bool left, bool right)
264 int factor = (increment ? 1 : -1);
266 switch (global_settings.rec_source)
268 HAVE_LINE_REC_(case AUDIO_SRC_LINEIN:)
269 HAVE_FMRADIO_REC_(case AUDIO_SRC_FMRADIO:)
270 if (left) global_settings.rec_left_gain += factor;
271 if (right) global_settings.rec_right_gain += factor;
272 break;
273 case AUDIO_SRC_MIC:
274 global_settings.rec_mic_gain += factor;
279 * Handle automatic gain control (AGC).
280 * Change recording gain if peak_x levels are above or below
281 * target volume for specified timeouts.
283 static void auto_gain_control(int *peak_l, int *peak_r, int *balance)
285 int agc_mono;
286 short agc_mode;
287 bool increment;
289 if (*peak_l > agc_left)
290 agc_left = *peak_l;
291 else
292 agc_left -= (agc_left - *peak_l + 3) >> 2;
293 if (*peak_r > agc_right)
294 agc_right = *peak_r;
295 else
296 agc_right -= (agc_right - *peak_r + 3) >> 2;
297 agc_mono = (agc_left + agc_right) / 2;
299 agc_mode = abs(agc_preset) - 1;
300 if (agc_mode < 0) {
301 agc_enable = false;
302 return;
305 if (agc_mode != AGC_SAFETY_MODE) {
306 /* Automatic balance control - only if not in safety mode */
307 if ((agc_left > AGC_IMG) && (agc_right > AGC_IMG))
309 if (*balance < -556)
311 if (*balance > -900)
312 agc_baltime -= !(peak_time % 4); /* 0.47 - 0.75dB */
313 else if (*balance > -4125)
314 agc_baltime--; /* 0.75 - 3.00dB */
315 else if (*balance > -7579)
316 agc_baltime -= 2; /* 3.00 - 4.90dB */
317 else
318 agc_baltime -= !(peak_time % 8); /* 4.90 - inf dB */
319 if (agc_baltime > 0)
320 agc_baltime -= (peak_time % 2);
322 else if (*balance > 556)
324 if (*balance < 900)
325 agc_baltime += !(peak_time % 4);
326 else if (*balance < 4125)
327 agc_baltime++;
328 else if (*balance < 7579)
329 agc_baltime += 2;
330 else
331 agc_baltime += !(peak_time % 8);
332 if (agc_baltime < 0)
333 agc_baltime += (peak_time % 2);
336 if ((*balance * agc_baltime) < 0)
338 if (*balance < 0)
339 agc_baltime -= peak_time % 2;
340 else
341 agc_baltime += peak_time % 2;
344 increment = ((agc_risetime / 2) > agc_droptime);
346 if (agc_baltime < -agc_tbal[agc_mode])
348 if (!increment || !agc_gain_is_max(!increment, increment)) {
349 change_recording_gain(increment, !increment, increment);
350 set_gain();
352 agc_baltime = 0;
354 else if (agc_baltime > +agc_tbal[agc_mode])
356 if (!increment || !agc_gain_is_max(increment, !increment)) {
357 change_recording_gain(increment, increment, !increment);
358 set_gain();
360 agc_baltime = 0;
363 else if (!(hist_time % 4))
365 if (agc_baltime < 0)
366 agc_baltime++;
367 else
368 agc_baltime--;
372 /* Automatic gain control */
373 if ((agc_left > agc_th_hi[agc_mode]) || (agc_right > agc_th_hi[agc_mode]))
375 if ((agc_left > AGC_CLIP) || (agc_right > AGC_CLIP))
376 agc_droptime += agc_tdrop[agc_mode] /
377 (global_settings.rec_agc_cliptime + 1);
378 if (agc_left > AGC_HIGH) {
379 agc_droptime++;
380 agc_risetime=0;
381 if (agc_left > AGC_PEAK)
382 agc_droptime += 2;
384 if (agc_right > AGC_HIGH) {
385 agc_droptime++;
386 agc_risetime=0;
387 if (agc_right > AGC_PEAK)
388 agc_droptime += 2;
390 if (agc_mono > agc_th_hi[agc_mode])
391 agc_droptime++;
392 else
393 agc_droptime += !(peak_time % 2);
395 if (agc_droptime >= agc_tdrop[agc_mode])
397 change_recording_gain(false, true, true);
398 agc_droptime = 0;
399 agc_risetime = 0;
400 set_gain();
402 agc_risetime = MAX(agc_risetime - 1, 0);
404 else if (agc_mono < agc_th_lo[agc_mode])
406 if (agc_mono < (agc_th_lo[agc_mode] / 8))
407 agc_risetime += !(peak_time % 5);
408 else if (agc_mono < (agc_th_lo[agc_mode] / 2))
409 agc_risetime += 2;
410 else
411 agc_risetime++;
413 if (agc_risetime >= agc_trise[agc_mode]) {
414 if ((agc_mode != AGC_SAFETY_MODE) &&
415 (!agc_gain_is_max(true, true))) {
416 change_recording_gain(true, true, true);
417 set_gain();
419 agc_risetime = 0;
420 agc_droptime = 0;
422 agc_droptime = MAX(agc_droptime - 1, 0);
424 else if (!(peak_time % 6)) /* on target level every 1.2 sec */
426 agc_risetime = MAX(agc_risetime - 1, 0);
427 agc_droptime = MAX(agc_droptime - 1, 0);
430 #endif /* HAVE_AGC */
432 static const char* const fmtstr[] =
434 "%c%d %s", /* no decimals */
435 "%c%d.%d %s ", /* 1 decimal */
436 "%c%d.%02d %s " /* 2 decimals */
439 static char *fmt_gain(int snd, int val, char *str, int len)
441 int i, d, numdec;
442 const char *unit;
443 char sign = ' ';
445 val = sound_val2phys(snd, val);
446 if(val < 0)
448 sign = '-';
449 val = -val;
451 numdec = sound_numdecimals(snd);
452 unit = sound_unit(snd);
454 if(numdec)
456 i = val / (10*numdec);
457 d = val % (10*numdec);
458 snprintf(str, len, fmtstr[numdec], sign, i, d, unit);
460 else
461 snprintf(str, len, fmtstr[numdec], sign, val, unit);
463 return str;
466 static int cursor;
468 static void adjust_cursor(void)
470 int max_cursor;
472 if(cursor < 0)
473 cursor = 0;
475 #ifdef HAVE_AGC
476 switch(global_settings.rec_source)
478 case REC_SRC_MIC:
479 if(cursor == 2)
480 cursor = 4;
481 else if(cursor == 3)
482 cursor = 1;
483 HAVE_LINE_REC_(case AUDIO_SRC_LINEIN:)
484 HAVE_FMRADIO_REC_(case AUDIO_SRC_FMRADIO:)
485 max_cursor = 5;
486 break;
487 default:
488 max_cursor = 0;
489 break;
491 #else /* !HAVE_AGC */
492 switch(global_settings.rec_source)
494 case AUDIO_SRC_MIC:
495 max_cursor = 1;
496 break;
497 HAVE_LINE_REC_(case AUDIO_SRC_LINEIN:)
498 HAVE_FMRADIO_REC_(case AUDIO_SRC_FMRADIO:)
499 max_cursor = 3;
500 break;
501 default:
502 max_cursor = 0;
503 break;
505 #endif /* HAVE_AGC */
507 if(cursor > max_cursor)
508 cursor = max_cursor;
511 static bool check_dir(char *folder)
513 DIR *dir = opendir(folder);
514 if (!dir && strcmp(folder, "/"))
516 int rc = mkdir(folder);
517 if(rc < 0)
518 return false;
519 return true;
521 closedir(dir);
522 return true;
525 char *rec_create_filename(char *buffer)
527 char ext[16];
528 strcpy(buffer,global_settings.rec_directory);
529 if (!check_dir(buffer))
530 return NULL;
532 snprintf(ext, sizeof(ext), ".%s",
533 REC_FILE_ENDING(global_settings.rec_format));
535 #if CONFIG_RTC == 0
536 return create_numbered_filename(buffer, buffer, "rec_", ext, 4,
537 &file_number);
538 #else
539 /* We'll wait at least up to the start of the next second so no duplicate
540 names are created */
541 return create_datetime_filename(buffer, buffer, "R", ext, true);
542 #endif
545 #if CONFIG_RTC == 0
546 /* Hit disk to get a starting filename for the type */
547 void rec_init_filename(void)
549 file_number = -1;
550 rec_create_filename(path_buffer);
551 file_number--;
553 #endif
555 int rec_create_directory(void)
557 return check_dir(global_settings.rec_directory)?1:0;
560 void rec_init_recording_options(struct audio_recording_options *options)
562 options->rec_source = global_settings.rec_source;
563 options->rec_frequency = global_settings.rec_frequency;
564 options->rec_channels = global_settings.rec_channels;
565 options->rec_prerecord_time = global_settings.rec_prerecord_time;
566 #if CONFIG_CODEC == SWCODEC
567 options->rec_source_flags = 0;
568 options->enc_config.rec_format = global_settings.rec_format;
569 global_to_encoder_config(&options->enc_config);
570 #else
571 options->rec_quality = global_settings.rec_quality;
572 options->rec_editable = global_settings.rec_editable;
573 #endif
576 #if CONFIG_CODEC == SWCODEC && !defined (SIMULATOR)
577 void rec_set_source(int source, unsigned flags)
579 /* Set audio input source, power up/down devices */
580 audio_set_input_source(source, flags);
582 /* Set peakmeters for recording or reset to playback */
583 peak_meter_playback((flags & SRCF_RECORDING) == 0);
584 peak_meter_enabled = true;
586 #endif /* CONFIG_CODEC == SWCODEC && !defined (SIMULATOR) */
588 void rec_set_recording_options(struct audio_recording_options *options)
590 #if CONFIG_CODEC != SWCODEC
591 if (global_settings.rec_prerecord_time)
592 talk_buffer_steal(); /* will use the mp3 buffer */
593 #else /* == SWOCODEC */
594 rec_set_source(options->rec_source,
595 options->rec_source_flags | SRCF_RECORDING);
596 #endif /* CONFIG_CODEC != SWCODEC */
598 audio_set_recording_options(options);
601 /* steals mp3 buffer, creates unique filename and starts recording */
602 void rec_record(void)
604 #if CONFIG_CODEC != SWCODEC
605 talk_buffer_steal(); /* we use the mp3 buffer */
606 #endif
607 audio_record(rec_create_filename(path_buffer));
610 /* creates unique filename and starts recording */
611 void rec_new_file(void)
613 audio_new_file(rec_create_filename(path_buffer));
616 /* used in trigger_listerner and recording_screen */
617 static unsigned int last_seconds = 0;
620 * Callback function so that the peak meter code can send an event
621 * to this application. This function can be passed to
622 * peak_meter_set_trigger_listener in order to activate the trigger.
624 static void trigger_listener(int trigger_status)
626 switch (trigger_status)
628 case TRIG_GO:
629 if(!(audio_status() & AUDIO_STATUS_RECORD))
631 rec_status |= RCSTAT_HAVE_RECORDED;
632 rec_record();
633 #if CONFIG_CODEC != SWCODEC
634 /* give control to mpeg thread so that it can start
635 recording */
636 yield(); yield(); yield();
637 #endif
640 /* if we're already recording this is a retrigger */
641 else
643 if((audio_status() & AUDIO_STATUS_PAUSE) &&
644 (global_settings.rec_trigger_type == 1))
645 audio_resume_recording();
646 /* New file on trig start*/
647 else if (global_settings.rec_trigger_type != 2)
649 rec_new_file();
650 /* tell recording_screen to reset the time */
651 last_seconds = 0;
654 break;
656 /* A _change_ to TRIG_READY means the current recording has stopped */
657 case TRIG_READY:
658 if(audio_status() & AUDIO_STATUS_RECORD)
660 switch(global_settings.rec_trigger_type)
662 case 0: /* Stop */
663 audio_stop_recording();
664 break;
666 case 1: /* Pause */
667 audio_pause_recording();
668 break;
670 case 2: /* New file on trig stop*/
671 rec_new_file();
672 /* tell recording_screen to reset the time */
673 last_seconds = 0;
674 break;
677 if (global_settings.rec_trigger_mode != TRIG_MODE_REARM)
679 peak_meter_set_trigger_listener(NULL);
680 peak_meter_trigger(false);
683 break;
687 bool recording_start_automatic = false;
689 bool recording_screen(bool no_source)
691 long button;
692 bool done = false;
693 char buf[32];
694 char buf2[32];
695 int w, h;
696 int update_countdown = 1;
697 unsigned int seconds;
698 int hours, minutes;
699 char filename[13];
700 int last_audio_stat = -1;
701 int audio_stat;
702 #if CONFIG_CODEC == SWCODEC
703 int warning_counter = 0;
704 #define WARNING_PERIOD 7
705 #endif
706 #ifdef HAVE_FMRADIO_REC
707 /* Radio is left on if:
708 * 1) Is was on at the start and the initial source is FM Radio
709 * 2) 1) and the source was never changed to something else
711 int radio_status = (global_settings.rec_source != AUDIO_SRC_FMRADIO) ?
712 FMRADIO_OFF : get_radio_status();
713 #endif
714 #if (CONFIG_LED == LED_REAL)
715 bool led_state = false;
716 int led_countdown = 2;
717 #endif
718 #ifdef HAVE_AGC
719 bool peak_read = false;
720 bool peak_valid = false;
721 int peak_l, peak_r;
722 int balance = 0;
723 bool display_agc[NB_SCREENS];
724 #endif
725 int line[NB_SCREENS];
726 int i;
727 int filename_offset[NB_SCREENS];
728 int pm_y[NB_SCREENS];
729 int trig_xpos[NB_SCREENS];
730 int trig_ypos[NB_SCREENS];
731 int trig_width[NB_SCREENS];
733 static const unsigned char *byte_units[] = {
734 ID2P(LANG_BYTE),
735 ID2P(LANG_KILOBYTE),
736 ID2P(LANG_MEGABYTE),
737 ID2P(LANG_GIGABYTE)
740 struct audio_recording_options rec_options;
741 if (check_dir(global_settings.rec_directory) == false)
743 do {
744 gui_syncsplash(0, "%s %s",
745 str(LANG_REC_DIR_NOT_WRITABLE),
746 str(LANG_OFF_ABORT));
747 } while (action_userabort(HZ) == false);
748 return false;
751 rec_status = RCSTAT_IN_RECSCREEN;
752 cursor = 0;
753 #if (CONFIG_LED == LED_REAL) && !defined(SIMULATOR)
754 ata_set_led_enabled(false);
755 #endif
757 #if CONFIG_CODEC == SWCODEC
758 /* recording_menu gets messed up: so prevent manus talking */
759 talk_disable_menus();
760 /* audio_init_recording stops anything playing when it takes the audio
761 buffer */
762 #else
763 /* Yes, we use the D/A for monitoring */
764 peak_meter_enabled = true;
765 peak_meter_playback(true);
766 #endif
768 audio_init_recording(0);
769 sound_set_volume(global_settings.volume);
771 #ifdef HAVE_AGC
772 peak_meter_get_peakhold(&peak_l, &peak_r);
773 #endif
775 rec_init_recording_options(&rec_options);
776 rec_set_recording_options(&rec_options);
778 set_gain();
780 if(rec_create_directory() > 0)
781 rec_status |= RCSTAT_CREATED_DIRECTORY;
783 #if CONFIG_RTC == 0
784 /* Create new filename for recording start */
785 rec_init_filename();
786 #endif
788 settings_apply_trigger();
790 #ifdef HAVE_AGC
791 agc_preset_str[0] = str(LANG_SYSFONT_OFF);
792 agc_preset_str[1] = str(LANG_SYSFONT_AGC_SAFETY);
793 agc_preset_str[2] = str(LANG_SYSFONT_AGC_LIVE);
794 agc_preset_str[3] = str(LANG_SYSFONT_AGC_DJSET);
795 agc_preset_str[4] = str(LANG_SYSFONT_AGC_MEDIUM);
796 agc_preset_str[5] = str(LANG_SYSFONT_AGC_VOICE);
797 if (global_settings.rec_source == AUDIO_SRC_MIC) {
798 agc_preset = global_settings.rec_agc_preset_mic;
799 agc_maxgain = global_settings.rec_agc_maxgain_mic;
801 else {
802 agc_preset = global_settings.rec_agc_preset_line;
803 agc_maxgain = global_settings.rec_agc_maxgain_line;
805 #endif /* HAVE_AGC */
807 FOR_NB_SCREENS(i)
809 screens[i].setfont(FONT_SYSFIXED);
810 screens[i].getstringsize("M", &w, &h);
811 screens[i].setmargins(global_settings.invert_cursor ? 0 : w, 8);
812 filename_offset[i] = ((screens[i].height >= 80) ? 1 : 0);
813 pm_y[i] = 8 + h * (2 + filename_offset[i]);
816 #ifdef HAVE_REMOTE_LCD
817 if (!remote_display_on)
819 screens[1].clear_display();
820 snprintf(buf, sizeof(buf), str(LANG_REMOTE_LCD_ON));
821 screens[1].puts((screens[1].width/w - strlen(buf))/2 + 1,
822 screens[1].height/(h*2) + 1, buf);
823 screens[1].update();
824 gui_syncsplash(0, str(LANG_REMOTE_LCD_OFF));
826 #endif
828 while(!done)
830 audio_stat = audio_status();
832 #if (CONFIG_LED == LED_REAL)
835 * Flash the LED while waiting to record. Turn it on while
836 * recording.
838 if(audio_stat & AUDIO_STATUS_RECORD)
840 if (audio_stat & AUDIO_STATUS_PAUSE)
842 if (--led_countdown <= 0)
844 led_state = !led_state;
845 led(led_state);
846 led_countdown = 2;
849 else
851 /* trigger is on in status TRIG_READY (no check needed) */
852 led(true);
855 else
857 int trigStat = peak_meter_trigger_status();
859 * other trigger stati than trig_off and trig_steady
860 * already imply that we are recording.
862 if (trigStat == TRIG_STEADY)
864 if (--led_countdown <= 0)
866 led_state = !led_state;
867 led(led_state);
868 led_countdown = 2;
871 else
873 /* trigger is on in status TRIG_READY (no check needed) */
874 led(false);
877 #endif /* CONFIG_LED */
879 /* Wait for a button a while (HZ/10) drawing the peak meter */
880 button = peak_meter_draw_get_btn(0, pm_y, h * PM_HEIGHT, screen_update);
882 if (last_audio_stat != audio_stat)
884 if (audio_stat & AUDIO_STATUS_RECORD)
886 rec_status |= RCSTAT_HAVE_RECORDED;
888 last_audio_stat = audio_stat;
892 if (recording_start_automatic)
894 /* simulate a button press */
895 button = ACTION_REC_PAUSE;
896 recording_start_automatic = false;
899 switch(button)
901 #ifdef HAVE_REMOTE_LCD
902 case ACTION_REC_LCD:
903 if (remote_display_on)
905 remote_display_on = false;
906 screen_update = 1;
907 screens[1].clear_display();
908 snprintf(buf, sizeof(buf), str(LANG_REMOTE_LCD_ON));
909 screens[1].puts((screens[1].width/w - strlen(buf))/2 + 1,
910 screens[1].height/(h*2) + 1, buf);
911 screens[1].update();
912 gui_syncsplash(0, str(LANG_REMOTE_LCD_OFF));
914 else
916 remote_display_on = true;
917 screen_update = NB_SCREENS;
919 break;
920 #endif
921 case ACTION_STD_CANCEL:
922 /* turn off the trigger */
923 peak_meter_trigger(false);
924 peak_meter_set_trigger_listener(NULL);
926 if(audio_stat & AUDIO_STATUS_RECORD)
928 audio_stop_recording();
930 else
932 #if CONFIG_CODEC != SWCODEC
933 peak_meter_playback(true);
934 peak_meter_enabled = false;
935 #endif
936 done = true;
938 update_countdown = 1; /* Update immediately */
939 break;
941 case ACTION_REC_PAUSE:
942 case ACTION_REC_NEWFILE:
943 /* Only act if the mpeg is stopped */
944 if(!(audio_stat & AUDIO_STATUS_RECORD))
946 /* is this manual or triggered recording? */
947 if ((global_settings.rec_trigger_mode == TRIG_MODE_OFF) ||
948 (peak_meter_trigger_status() != TRIG_OFF))
950 /* manual recording */
951 rec_status |= RCSTAT_HAVE_RECORDED;
952 rec_record();
953 last_seconds = 0;
954 if (global_settings.talk_menu)
956 /* no voice possible here, but a beep */
957 audio_beep(HZ/2); /* longer beep on start */
960 /* this is triggered recording */
961 else
963 /* we don't start recording now, but enable the
964 trigger and let the callback function
965 trigger_listener control when the recording starts */
966 peak_meter_trigger(true);
967 peak_meter_set_trigger_listener(&trigger_listener);
970 else
972 /*if new file button pressed, start new file */
973 if (button == ACTION_REC_NEWFILE)
975 rec_new_file();
976 last_seconds = 0;
978 else
979 /* if pause button pressed, pause or resume */
981 if(audio_stat & AUDIO_STATUS_PAUSE)
983 audio_resume_recording();
984 if (global_settings.talk_menu)
986 /* no voice possible here, but a beep */
987 audio_beep(HZ/4); /* short beep on resume */
990 else
992 audio_pause_recording();
996 update_countdown = 1; /* Update immediately */
997 break;
999 case ACTION_STD_PREV:
1000 cursor--;
1001 adjust_cursor();
1002 update_countdown = 1; /* Update immediately */
1003 break;
1005 case ACTION_STD_NEXT:
1006 cursor++;
1007 adjust_cursor();
1008 update_countdown = 1; /* Update immediately */
1009 break;
1011 case ACTION_SETTINGS_INC:
1012 switch(cursor)
1014 case 0:
1015 global_settings.volume++;
1016 setvol();
1017 break;
1018 case 1:
1019 if(global_settings.rec_source == AUDIO_SRC_MIC)
1021 if(global_settings.rec_mic_gain <
1022 sound_max(SOUND_MIC_GAIN))
1023 global_settings.rec_mic_gain++;
1025 else
1027 if(global_settings.rec_left_gain <
1028 sound_max(SOUND_LEFT_GAIN))
1029 global_settings.rec_left_gain++;
1030 if(global_settings.rec_right_gain <
1031 sound_max(SOUND_RIGHT_GAIN))
1032 global_settings.rec_right_gain++;
1034 break;
1035 case 2:
1036 if(global_settings.rec_left_gain <
1037 sound_max(SOUND_LEFT_GAIN))
1038 global_settings.rec_left_gain++;
1039 break;
1040 case 3:
1041 if(global_settings.rec_right_gain <
1042 sound_max(SOUND_RIGHT_GAIN))
1043 global_settings.rec_right_gain++;
1044 break;
1045 #ifdef HAVE_AGC
1046 case 4:
1047 agc_preset = MIN(agc_preset + 1, AGC_MODE_SIZE);
1048 agc_enable = (agc_preset != 0);
1049 if (global_settings.rec_source == AUDIO_SRC_MIC) {
1050 global_settings.rec_agc_preset_mic = agc_preset;
1051 agc_maxgain = global_settings.rec_agc_maxgain_mic;
1052 } else {
1053 global_settings.rec_agc_preset_line = agc_preset;
1054 agc_maxgain = global_settings.rec_agc_maxgain_line;
1056 break;
1057 case 5:
1058 if (global_settings.rec_source == AUDIO_SRC_MIC)
1060 agc_maxgain = MIN(agc_maxgain + 1,
1061 sound_max(SOUND_MIC_GAIN));
1062 global_settings.rec_agc_maxgain_mic = agc_maxgain;
1064 else
1066 agc_maxgain = MIN(agc_maxgain + 1,
1067 sound_max(SOUND_LEFT_GAIN));
1068 global_settings.rec_agc_maxgain_line = agc_maxgain;
1070 break;
1071 #endif /* HAVE_AGC */
1073 set_gain();
1074 update_countdown = 1; /* Update immediately */
1075 break;
1077 case ACTION_SETTINGS_DEC:
1078 switch(cursor)
1080 case 0:
1081 global_settings.volume--;
1082 setvol();
1083 break;
1084 case 1:
1085 if(global_settings.rec_source == AUDIO_SRC_MIC)
1087 if(global_settings.rec_mic_gain >
1088 sound_min(SOUND_MIC_GAIN))
1089 global_settings.rec_mic_gain--;
1091 else
1093 if(global_settings.rec_left_gain >
1094 sound_min(SOUND_LEFT_GAIN))
1095 global_settings.rec_left_gain--;
1096 if(global_settings.rec_right_gain >
1097 sound_min(SOUND_RIGHT_GAIN))
1098 global_settings.rec_right_gain--;
1100 break;
1101 case 2:
1102 if(global_settings.rec_left_gain >
1103 sound_min(SOUND_LEFT_GAIN))
1104 global_settings.rec_left_gain--;
1105 break;
1106 case 3:
1107 if(global_settings.rec_right_gain >
1108 sound_min(SOUND_RIGHT_GAIN))
1109 global_settings.rec_right_gain--;
1110 break;
1111 #ifdef HAVE_AGC
1112 case 4:
1113 agc_preset = MAX(agc_preset - 1, 0);
1114 agc_enable = (agc_preset != 0);
1115 if (global_settings.rec_source == AUDIO_SRC_MIC) {
1116 global_settings.rec_agc_preset_mic = agc_preset;
1117 agc_maxgain = global_settings.rec_agc_maxgain_mic;
1118 } else {
1119 global_settings.rec_agc_preset_line = agc_preset;
1120 agc_maxgain = global_settings.rec_agc_maxgain_line;
1122 break;
1123 case 5:
1124 if (global_settings.rec_source == AUDIO_SRC_MIC)
1126 agc_maxgain = MAX(agc_maxgain - 1,
1127 sound_min(SOUND_MIC_GAIN));
1128 global_settings.rec_agc_maxgain_mic = agc_maxgain;
1130 else
1132 agc_maxgain = MAX(agc_maxgain - 1,
1133 sound_min(SOUND_LEFT_GAIN));
1134 global_settings.rec_agc_maxgain_line = agc_maxgain;
1136 break;
1137 #endif /* HAVE_AGC */
1139 set_gain();
1140 update_countdown = 1; /* Update immediately */
1141 break;
1143 case ACTION_STD_MENU:
1144 #if CONFIG_CODEC == SWCODEC
1145 if(!(audio_stat & AUDIO_STATUS_RECORD))
1146 #else
1147 if(audio_stat != AUDIO_STATUS_RECORD)
1148 #endif
1150 #ifdef HAVE_FMRADIO_REC
1151 const int prev_rec_source = global_settings.rec_source;
1152 #endif
1154 #if (CONFIG_LED == LED_REAL)
1155 /* led is restored at begin of loop / end of function */
1156 led(false);
1157 #endif
1158 if (recording_menu(no_source))
1160 done = true;
1161 rec_status |= RCSTAT_BEEN_IN_USB_MODE;
1162 #ifdef HAVE_FMRADIO_REC
1163 radio_status = FMRADIO_OFF;
1164 #endif
1166 else
1168 #ifdef HAVE_FMRADIO_REC
1169 /* If input changes away from FM Radio, radio will
1170 remain off when recording screen closes. */
1171 if (global_settings.rec_source != prev_rec_source
1172 && prev_rec_source == AUDIO_SRC_FMRADIO)
1173 radio_status = FMRADIO_OFF;
1174 #endif
1176 #if CONFIG_CODEC == SWCODEC
1177 /* reinit after submenu exit */
1178 audio_close_recording();
1179 audio_init_recording(0);
1180 #endif
1182 rec_init_recording_options(&rec_options);
1183 rec_set_recording_options(&rec_options);
1185 if(rec_create_directory() > 0)
1186 rec_status |= RCSTAT_CREATED_DIRECTORY;
1188 #if CONFIG_CODEC == SWCODEC && CONFIG_RTC == 0
1189 /* If format changed, a new number is required */
1190 rec_init_filename();
1191 #endif
1193 #ifdef HAVE_AGC
1194 if (global_settings.rec_source == AUDIO_SRC_MIC) {
1195 agc_preset = global_settings.rec_agc_preset_mic;
1196 agc_maxgain = global_settings.rec_agc_maxgain_mic;
1198 else {
1199 agc_preset = global_settings.rec_agc_preset_line;
1200 agc_maxgain = global_settings.rec_agc_maxgain_line;
1202 #endif
1204 adjust_cursor();
1205 set_gain();
1206 update_countdown = 1; /* Update immediately */
1208 FOR_NB_SCREENS(i)
1210 screens[i].setfont(FONT_SYSFIXED);
1211 screens[i].setmargins(
1212 global_settings.invert_cursor ? 0 : w, 8);
1216 break;
1218 #if CONFIG_KEYPAD == RECORDER_PAD
1219 case ACTION_REC_F2:
1220 if(audio_stat != AUDIO_STATUS_RECORD)
1222 #if (CONFIG_LED == LED_REAL)
1223 /* led is restored at begin of loop / end of function */
1224 led(false);
1225 #endif
1226 if (f2_rec_screen())
1228 rec_status |= RCSTAT_HAVE_RECORDED;
1229 done = true;
1231 else
1232 update_countdown = 1; /* Update immediately */
1234 break;
1236 case ACTION_REC_F3:
1237 if(audio_stat & AUDIO_STATUS_RECORD)
1239 rec_new_file();
1240 last_seconds = 0;
1242 else
1244 #if (CONFIG_LED == LED_REAL)
1245 /* led is restored at begin of loop / end of function */
1246 led(false);
1247 #endif
1248 if (f3_rec_screen())
1250 rec_status |= RCSTAT_HAVE_RECORDED;
1251 done = true;
1253 else
1254 update_countdown = 1; /* Update immediately */
1256 break;
1257 #endif /* CONFIG_KEYPAD == RECORDER_PAD */
1259 case SYS_USB_CONNECTED:
1260 /* Only accept USB connection when not recording */
1261 if(!(audio_stat & AUDIO_STATUS_RECORD))
1263 default_event_handler(SYS_USB_CONNECTED);
1264 done = true;
1265 rec_status |= RCSTAT_BEEN_IN_USB_MODE;
1266 #ifdef HAVE_FMRADIO_REC
1267 radio_status = FMRADIO_OFF;
1268 #endif
1270 break;
1272 default:
1273 default_event_handler(button);
1274 break;
1275 } /* end switch */
1277 #ifdef HAVE_AGC
1278 peak_read = !peak_read;
1279 if (peak_read) { /* every 2nd run of loop */
1280 peak_time++;
1281 peak_valid = read_peak_levels(&peak_l, &peak_r, &balance);
1284 /* Handle AGC every 200ms when enabled and peak data is valid */
1285 if (peak_read && agc_enable && peak_valid)
1286 auto_gain_control(&peak_l, &peak_r, &balance);
1287 #endif
1289 FOR_NB_SCREENS(i)
1290 screens[i].setfont(FONT_SYSFIXED);
1292 seconds = audio_recorded_time() / HZ;
1294 update_countdown--;
1295 if(update_countdown == 0 || seconds > last_seconds)
1297 unsigned int dseconds, dhours, dminutes;
1298 unsigned long num_recorded_bytes, dsize, dmb;
1299 int pos = 0;
1301 update_countdown = 5;
1302 last_seconds = seconds;
1304 dseconds = rec_timesplit_seconds();
1305 dsize = rec_sizesplit_bytes();
1306 num_recorded_bytes = audio_num_recorded_bytes();
1308 for(i = 0; i < screen_update; i++)
1309 screens[i].clear_display();
1311 #if CONFIG_CODEC == SWCODEC
1312 if ((audio_stat & AUDIO_STATUS_WARNING)
1313 && (warning_counter++ % WARNING_PERIOD) < WARNING_PERIOD/2)
1315 /* Switch back and forth displaying warning on first available
1316 line to ensure visibility - the motion should also help
1317 draw attention */
1318 /* Don't use language string unless agreed upon to make this
1319 method permanent - could do something in the statusbar */
1320 snprintf(buf, sizeof(buf), "Warning: %08X",
1321 pcm_rec_get_warnings());
1323 else
1324 #endif /* CONFIG_CODEC == SWCODEC */
1325 if ((global_settings.rec_sizesplit) && (global_settings.rec_split_method))
1327 dmb = dsize/1024/1024;
1328 snprintf(buf, sizeof(buf), "%s %dMB",
1329 str(LANG_SYSFONT_SPLIT_SIZE), dmb);
1331 else
1333 hours = seconds / 3600;
1334 minutes = (seconds - (hours * 3600)) / 60;
1335 snprintf(buf, sizeof(buf), "%s %02d:%02d:%02d",
1336 str(LANG_SYSFONT_RECORDING_TIME),
1337 hours, minutes, seconds%60);
1340 for(i = 0; i < screen_update; i++)
1341 screens[i].puts(0, 0, buf);
1343 if(audio_stat & AUDIO_STATUS_PRERECORD)
1345 snprintf(buf, sizeof(buf), "%s...", str(LANG_SYSFONT_RECORD_PRERECORD));
1347 else
1349 /* Display the split interval if the record timesplit
1350 is active */
1351 if ((global_settings.rec_timesplit) && !(global_settings.rec_split_method))
1353 /* Display the record timesplit interval rather
1354 than the file size if the record timer is
1355 active */
1356 dhours = dseconds / 3600;
1357 dminutes = (dseconds - (dhours * 3600)) / 60;
1358 snprintf(buf, sizeof(buf), "%s %02d:%02d",
1359 str(LANG_SYSFONT_RECORD_TIMESPLIT_REC),
1360 dhours, dminutes);
1362 else
1364 output_dyn_value(buf2, sizeof buf2,
1365 num_recorded_bytes,
1366 byte_units, true);
1367 snprintf(buf, sizeof(buf), "%s %s",
1368 str(LANG_SYSFONT_RECORDING_SIZE), buf2);
1371 for(i = 0; i < screen_update; i++)
1372 screens[i].puts(0, 1, buf);
1374 for(i = 0; i < screen_update; i++)
1376 if (filename_offset[i] > 0)
1378 *filename = '\0';
1379 if (audio_stat & AUDIO_STATUS_RECORD)
1381 strncpy(filename, path_buffer +
1382 strlen(path_buffer) - 12, 13);
1383 filename[12]='\0';
1386 snprintf(buf, sizeof(buf), "%s %s",
1387 str(LANG_SYSFONT_RECORDING_FILENAME), filename);
1388 screens[i].puts(0, 2, buf);
1392 /* We will do file splitting regardless, either at the end of
1393 a split interval, or when the filesize approaches the 2GB
1394 FAT file size (compatibility) limit. */
1395 if ((audio_stat && !(global_settings.rec_split_method)
1396 && global_settings.rec_timesplit && (seconds >= dseconds))
1397 || (audio_stat && global_settings.rec_split_method
1398 && global_settings.rec_sizesplit && (num_recorded_bytes >= dsize))
1399 || (num_recorded_bytes >= MAX_FILE_SIZE))
1401 if (!(global_settings.rec_split_type)
1402 || (num_recorded_bytes >= MAX_FILE_SIZE))
1404 rec_new_file();
1405 last_seconds = 0;
1407 else
1409 peak_meter_trigger(false);
1410 peak_meter_set_trigger_listener(NULL);
1411 audio_stop_recording();
1413 update_countdown = 1;
1416 snprintf(buf, sizeof(buf), "%s: %s", str(LANG_SYSFONT_VOLUME),
1417 fmt_gain(SOUND_VOLUME,
1418 global_settings.volume,
1419 buf2, sizeof(buf2)));
1421 if (global_settings.invert_cursor && (pos++ == cursor))
1423 for(i = 0; i < screen_update; i++)
1424 screens[i].puts_style_offset(0, filename_offset[i] +
1425 PM_HEIGHT + 2, buf, STYLE_INVERT,0);
1427 else
1429 for(i = 0; i < screen_update; i++)
1430 screens[i].puts(0, filename_offset[i] + PM_HEIGHT + 2, buf);
1433 if(global_settings.rec_source == AUDIO_SRC_MIC)
1435 /* Draw MIC recording gain */
1436 snprintf(buf, sizeof(buf), "%s:%s", str(LANG_SYSFONT_RECORDING_GAIN),
1437 fmt_gain(SOUND_MIC_GAIN,
1438 global_settings.rec_mic_gain,
1439 buf2, sizeof(buf2)));
1440 if(global_settings.invert_cursor && ((1==cursor)||(2==cursor)))
1442 for(i = 0; i < screen_update; i++)
1443 screens[i].puts_style_offset(0, filename_offset[i] +
1444 PM_HEIGHT + 3, buf, STYLE_INVERT,0);
1446 else
1448 for(i = 0; i < screen_update; i++)
1449 screens[i].puts(0, filename_offset[i] +
1450 PM_HEIGHT + 3, buf);
1453 else if(0
1454 HAVE_LINE_REC_( || global_settings.rec_source == AUDIO_SRC_LINEIN)
1455 HAVE_FMRADIO_REC_( || global_settings.rec_source == AUDIO_SRC_FMRADIO)
1458 /* Draw LINE or FMRADIO recording gain */
1459 snprintf(buf, sizeof(buf), "%s:%s",
1460 str(LANG_SYSFONT_RECORDING_LEFT),
1461 fmt_gain(SOUND_LEFT_GAIN,
1462 global_settings.rec_left_gain,
1463 buf2, sizeof(buf2)));
1464 if(global_settings.invert_cursor && ((1==cursor)||(2==cursor)))
1466 for(i = 0; i < screen_update; i++)
1467 screens[i].puts_style_offset(0, filename_offset[i] +
1468 PM_HEIGHT + 3, buf, STYLE_INVERT,0);
1470 else
1472 for(i = 0; i < screen_update; i++)
1473 screens[i].puts(0, filename_offset[i] +
1474 PM_HEIGHT + 3, buf);
1477 snprintf(buf, sizeof(buf), "%s:%s",
1478 str(LANG_SYSFONT_RECORDING_RIGHT),
1479 fmt_gain(SOUND_RIGHT_GAIN,
1480 global_settings.rec_right_gain,
1481 buf2, sizeof(buf2)));
1482 if(global_settings.invert_cursor && ((1==cursor)||(3==cursor)))
1484 for(i = 0; i < screen_update; i++)
1485 screens[i].puts_style_offset(0, filename_offset[i] +
1486 PM_HEIGHT + 4, buf, STYLE_INVERT,0);
1488 else
1490 for(i = 0; i < screen_update; i++)
1491 screens[i].puts(0, filename_offset[i] +
1492 PM_HEIGHT + 4, buf);
1496 FOR_NB_SCREENS(i)
1498 switch (global_settings.rec_source)
1500 HAVE_LINE_REC_(case AUDIO_SRC_LINEIN:)
1501 HAVE_FMRADIO_REC_(case AUDIO_SRC_FMRADIO:)
1502 line[i] = 5;
1503 break;
1505 case AUDIO_SRC_MIC:
1506 line[i] = 4;
1507 break;
1508 #ifdef HAVE_SPDIF_REC
1509 case AUDIO_SRC_SPDIF:
1510 line[i] = 3;
1511 break;
1512 #endif
1513 default:
1514 line[i] = 5; /* to prevent uninitialisation
1515 warnings for line[0] */
1516 break;
1517 } /* end switch */
1518 #ifdef HAVE_AGC
1519 if (screens[i].height < h * (2 + filename_offset[i] + PM_HEIGHT + line[i]))
1521 line[i] -= 1;
1522 display_agc[i] = false;
1524 else
1525 display_agc[i] = true;
1527 if ((cursor==4) || (cursor==5))
1528 display_agc[i] = true;
1531 /************** AGC test info ******************
1532 snprintf(buf, sizeof(buf), "D:%d U:%d",
1533 (agc_droptime+2)/5, (agc_risetime+2)/5);
1534 lcd_putsxy(1, LCD_HEIGHT - 8, buf);
1535 snprintf(buf, sizeof(buf), "B:%d",
1536 (agc_baltime+2)/5);
1537 lcd_putsxy(LCD_WIDTH/2 + 3, LCD_HEIGHT - 8, buf);
1538 ***********************************************/
1540 if (cursor == 5)
1541 snprintf(buf, sizeof(buf), "%s: %s",
1542 str(LANG_SYSFONT_RECORDING_AGC_MAXGAIN),
1543 fmt_gain(SOUND_LEFT_GAIN,
1544 agc_maxgain, buf2, sizeof(buf2)));
1545 else if (agc_preset == 0)
1546 snprintf(buf, sizeof(buf), "%s: %s",
1547 str(LANG_SYSFONT_RECORDING_AGC_PRESET),
1548 agc_preset_str[agc_preset]);
1549 else if (global_settings.rec_source == AUDIO_SRC_MIC)
1550 snprintf(buf, sizeof(buf), "%s: %s%s",
1551 str(LANG_SYSFONT_RECORDING_AGC_PRESET),
1552 agc_preset_str[agc_preset],
1553 fmt_gain(SOUND_LEFT_GAIN,
1554 agc_maxgain -
1555 global_settings.rec_mic_gain,
1556 buf2, sizeof(buf2)));
1557 else
1558 snprintf(buf, sizeof(buf), "%s: %s%s",
1559 str(LANG_SYSFONT_RECORDING_AGC_PRESET),
1560 agc_preset_str[agc_preset],
1561 fmt_gain(SOUND_LEFT_GAIN,
1562 agc_maxgain -
1563 (global_settings.rec_left_gain +
1564 global_settings.rec_right_gain)/2,
1565 buf2, sizeof(buf2)));
1567 if(global_settings.invert_cursor && ((cursor==4) || (cursor==5)))
1569 for(i = 0; i < screen_update; i++)
1570 screens[i].puts_style_offset(0, filename_offset[i] +
1571 PM_HEIGHT + line[i], buf, STYLE_INVERT,0);
1573 else if (
1574 global_settings.rec_source == AUDIO_SRC_MIC
1575 HAVE_LINE_REC_(|| global_settings.rec_source == AUDIO_SRC_LINEIN)
1576 HAVE_FMRADIO_REC_(|| global_settings.rec_source == AUDIO_SRC_FMRADIO)
1579 for(i = 0; i < screen_update; i++) {
1580 if (display_agc[i]) {
1581 screens[i].puts(0, filename_offset[i] +
1582 PM_HEIGHT + line[i], buf);
1587 if (global_settings.rec_source == AUDIO_SRC_MIC)
1589 if(agc_maxgain < (global_settings.rec_mic_gain))
1590 change_recording_gain(false, true, true);
1592 else
1594 if(agc_maxgain < (global_settings.rec_left_gain))
1595 change_recording_gain(false, true, false);
1596 if(agc_maxgain < (global_settings.rec_right_gain))
1597 change_recording_gain(false, false, true);
1599 #else /* !HAVE_AGC */
1601 #endif /* HAVE_AGC */
1603 if(!global_settings.invert_cursor) {
1604 switch(cursor)
1606 case 1:
1607 for(i = 0; i < screen_update; i++)
1608 screen_put_cursorxy(&screens[i], 0,
1609 filename_offset[i] +
1610 PM_HEIGHT + 3, true);
1612 if(global_settings.rec_source != AUDIO_SRC_MIC)
1614 for(i = 0; i < screen_update; i++)
1615 screen_put_cursorxy(&screens[i], 0,
1616 filename_offset[i] +
1617 PM_HEIGHT + 4, true);
1619 break;
1620 case 2:
1621 for(i = 0; i < screen_update; i++)
1622 screen_put_cursorxy(&screens[i], 0,
1623 filename_offset[i] +
1624 PM_HEIGHT + 3, true);
1625 break;
1626 case 3:
1627 for(i = 0; i < screen_update; i++)
1628 screen_put_cursorxy(&screens[i], 0,
1629 filename_offset[i] +
1630 PM_HEIGHT + 4, true);
1631 break;
1632 #ifdef HAVE_AGC
1633 case 4:
1634 case 5:
1635 for(i = 0; i < screen_update; i++)
1636 screen_put_cursorxy(&screens[i], 0,
1637 filename_offset[i] +
1638 PM_HEIGHT + line[i], true);
1639 break;
1640 #endif /* HAVE_AGC */
1641 default:
1642 for(i = 0; i < screen_update; i++)
1643 screen_put_cursorxy(&screens[i], 0,
1644 filename_offset[i] +
1645 PM_HEIGHT + 2, true);
1649 #ifdef HAVE_AGC
1650 hist_time++;
1651 #endif
1653 for(i = 0; i < screen_update; i++)
1655 gui_statusbar_draw(&(statusbars.statusbars[i]), true);
1656 peak_meter_screen(&screens[i], 0, pm_y[i], h*PM_HEIGHT);
1657 screens[i].update();
1660 /* draw the trigger status */
1661 FOR_NB_SCREENS(i)
1663 trig_width[i] = ((screens[i].height < 64) ||
1664 ((screens[i].height < 72) && (PM_HEIGHT > 1))) ?
1665 screens[i].width - 14 * w : screens[i].width;
1666 trig_xpos[i] = screens[i].width - trig_width[i];
1667 trig_ypos[i] = ((screens[i].height < 72) && (PM_HEIGHT > 1)) ?
1668 h*2 :
1669 h*(1 + filename_offset[i] + PM_HEIGHT + line[i]
1670 #ifdef HAVE_AGC
1672 #endif
1676 if (peak_meter_trigger_status() != TRIG_OFF)
1678 peak_meter_draw_trig(trig_xpos, trig_ypos, trig_width,
1679 screen_update);
1680 for(i = 0; i < screen_update; i++){
1681 screens[i].update_rect(trig_xpos[i], trig_ypos[i],
1682 trig_width[i] + 2, TRIG_HEIGHT);
1687 if(audio_stat & AUDIO_STATUS_ERROR)
1689 done = true;
1691 } /* end while(!done) */
1693 audio_stat = audio_status();
1694 if (audio_stat & AUDIO_STATUS_ERROR)
1696 gui_syncsplash(0, str(LANG_SYSFONT_DISK_FULL));
1697 gui_syncstatusbar_draw(&statusbars, true);
1699 FOR_NB_SCREENS(i)
1700 screens[i].update();
1702 audio_error_clear();
1704 while(1)
1706 if (action_userabort(TIMEOUT_NOBLOCK))
1707 break;
1711 #if CONFIG_CODEC == SWCODEC
1712 audio_stop_recording();
1713 audio_close_recording();
1715 #ifdef HAVE_FMRADIO_REC
1716 if (radio_status != FMRADIO_OFF)
1717 /* Restore radio playback - radio_status should be unchanged if started
1718 through fm radio screen (barring usb connect) */
1719 rec_set_source(AUDIO_SRC_FMRADIO, (radio_status & FMRADIO_PAUSED) ?
1720 SRCF_FMRADIO_PAUSED : SRCF_FMRADIO_PLAYING);
1721 else
1722 #endif
1723 /* Go back to playback mode */
1724 rec_set_source(AUDIO_SRC_PLAYBACK, SRCF_PLAYBACK);
1726 /* restore talk_menu setting */
1727 talk_enable_menus();
1728 #else /* !SWCODEC */
1729 audio_init_playback();
1730 #endif /* CONFIG_CODEC == SWCODEC */
1732 /* make sure the trigger is really turned off */
1733 peak_meter_trigger(false);
1734 peak_meter_set_trigger_listener(NULL);
1736 rec_status &= ~RCSTAT_IN_RECSCREEN;
1737 sound_settings_apply();
1739 FOR_NB_SCREENS(i)
1740 screens[i].setfont(FONT_UI);
1742 /* if the directory was created or recording happened, make sure the
1743 browser is updated */
1744 if (rec_status & (RCSTAT_CREATED_DIRECTORY | RCSTAT_HAVE_RECORDED))
1745 reload_directory();
1747 #if (CONFIG_LED == LED_REAL) && !defined(SIMULATOR)
1748 ata_set_led_enabled(true);
1749 #endif
1751 settings_save();
1753 return (rec_status & RCSTAT_BEEN_IN_USB_MODE) != 0;
1754 } /* recording_screen */
1756 #if CONFIG_KEYPAD == RECORDER_PAD
1757 static bool f2_rec_screen(void)
1759 static const char* const freq_str[6] =
1761 "44.1kHz",
1762 "48kHz",
1763 "32kHz",
1764 "22.05kHz",
1765 "24kHz",
1766 "16kHz"
1769 bool exit = false;
1770 bool used = false;
1771 int w, h, i;
1772 char buf[32];
1773 int button;
1774 struct audio_recording_options rec_options;
1776 FOR_NB_SCREENS(i)
1778 screens[i].setfont(FONT_SYSFIXED);
1779 screens[i].getstringsize("A",&w,&h);
1782 while (!exit) {
1783 const char* ptr=NULL;
1785 FOR_NB_SCREENS(i)
1787 screens[i].clear_display();
1789 /* Recording quality */
1790 screens[i].putsxy(0, LCD_HEIGHT/2 - h*2,
1791 str(LANG_SYSFONT_RECORDING_QUALITY));
1794 snprintf(buf, sizeof(buf), "%d", global_settings.rec_quality);
1795 FOR_NB_SCREENS(i)
1797 screens[i].putsxy(0, LCD_HEIGHT/2-h, buf);
1798 screens[i].mono_bitmap(bitmap_icons_7x8[Icon_FastBackward],
1799 LCD_WIDTH/2 - 16, LCD_HEIGHT/2 - 4, 7, 8);
1802 /* Frequency */
1803 snprintf(buf, sizeof buf, "%s:", str(LANG_SYSFONT_RECORDING_FREQUENCY));
1804 ptr = freq_str[global_settings.rec_frequency];
1805 FOR_NB_SCREENS(i)
1807 screens[i].getstringsize(buf,&w,&h);
1808 screens[i].putsxy((LCD_WIDTH-w)/2, LCD_HEIGHT - h*2, buf);
1809 screens[i].getstringsize(ptr, &w, &h);
1810 screens[i].putsxy((LCD_WIDTH-w)/2, LCD_HEIGHT - h, ptr);
1811 screens[i].mono_bitmap(bitmap_icons_7x8[Icon_DownArrow],
1812 LCD_WIDTH/2 - 3, LCD_HEIGHT - h*3, 7, 8);
1815 /* Channel mode */
1816 switch ( global_settings.rec_channels ) {
1817 case 0:
1818 ptr = str(LANG_SYSFONT_CHANNEL_STEREO);
1819 break;
1821 case 1:
1822 ptr = str(LANG_SYSFONT_CHANNEL_MONO);
1823 break;
1826 FOR_NB_SCREENS(i)
1828 screens[i].getstringsize(str(LANG_SYSFONT_RECORDING_CHANNELS), &w, &h);
1829 screens[i].putsxy(LCD_WIDTH - w, LCD_HEIGHT/2 - h*2,
1830 str(LANG_SYSFONT_RECORDING_CHANNELS));
1831 screens[i].getstringsize(str(LANG_SYSFONT_F2_MODE), &w, &h);
1832 screens[i].putsxy(LCD_WIDTH - w, LCD_HEIGHT/2 - h,
1833 str(LANG_SYSFONT_F2_MODE));
1834 screens[i].getstringsize(ptr, &w, &h);
1835 screens[i].putsxy(LCD_WIDTH - w, LCD_HEIGHT/2, ptr);
1836 screens[i].mono_bitmap(bitmap_icons_7x8[Icon_FastForward],
1837 LCD_WIDTH/2 + 8, LCD_HEIGHT/2 - 4, 7, 8);
1839 screens[i].update();
1842 button = button_get(true);
1843 switch (button) {
1844 case BUTTON_LEFT:
1845 case BUTTON_F2 | BUTTON_LEFT:
1846 global_settings.rec_quality++;
1847 if(global_settings.rec_quality > 7)
1848 global_settings.rec_quality = 0;
1849 used = true;
1850 break;
1852 case BUTTON_DOWN:
1853 case BUTTON_F2 | BUTTON_DOWN:
1854 global_settings.rec_frequency++;
1855 if(global_settings.rec_frequency > 5)
1856 global_settings.rec_frequency = 0;
1857 used = true;
1858 break;
1860 case BUTTON_RIGHT:
1861 case BUTTON_F2 | BUTTON_RIGHT:
1862 global_settings.rec_channels++;
1863 if(global_settings.rec_channels > 1)
1864 global_settings.rec_channels = 0;
1865 used = true;
1866 break;
1868 case BUTTON_F2 | BUTTON_REL:
1869 if ( used )
1870 exit = true;
1871 used = true;
1872 break;
1874 case BUTTON_F2 | BUTTON_REPEAT:
1875 used = true;
1876 break;
1878 default:
1879 if(default_event_handler(button) == SYS_USB_CONNECTED)
1880 return true;
1881 break;
1885 rec_init_recording_options(&rec_options);
1886 rec_set_recording_options(&rec_options);
1888 set_gain();
1890 settings_save();
1891 FOR_NB_SCREENS(i)
1892 screens[i].setfont(FONT_UI);
1894 return false;
1897 static bool f3_rec_screen(void)
1899 bool exit = false;
1900 bool used = false;
1901 int w, h, i;
1902 int button;
1903 char *src_str[] =
1905 str(LANG_SYSFONT_RECORDING_SRC_MIC),
1906 str(LANG_SYSFONT_RECORDING_SRC_LINE),
1907 str(LANG_SYSFONT_RECORDING_SRC_DIGITAL)
1909 struct audio_recording_options rec_options;
1911 FOR_NB_SCREENS(i)
1913 screens[i].setfont(FONT_SYSFIXED);
1914 screens[i].getstringsize("A",&w,&h);
1917 while (!exit) {
1918 char* ptr=NULL;
1919 ptr = src_str[global_settings.rec_source];
1920 FOR_NB_SCREENS(i)
1922 screens[i].clear_display();
1924 /* Recording source */
1925 screens[i].putsxy(0, LCD_HEIGHT/2 - h*2,
1926 str(LANG_SYSFONT_RECORDING_SOURCE));
1928 screens[i].getstringsize(ptr, &w, &h);
1929 screens[i].putsxy(0, LCD_HEIGHT/2-h, ptr);
1930 screens[i].mono_bitmap(bitmap_icons_7x8[Icon_FastBackward],
1931 LCD_WIDTH/2 - 16, LCD_HEIGHT/2 - 4, 7, 8);
1934 /* trigger setup */
1935 ptr = str(LANG_SYSFONT_RECORD_TRIGGER);
1936 FOR_NB_SCREENS(i)
1938 screens[i].getstringsize(ptr,&w,&h);
1939 screens[i].putsxy((LCD_WIDTH-w)/2, LCD_HEIGHT - h*2, ptr);
1940 screens[i].mono_bitmap(bitmap_icons_7x8[Icon_DownArrow],
1941 LCD_WIDTH/2 - 3, LCD_HEIGHT - h*3, 7, 8);
1943 screens[i].update();
1946 button = button_get(true);
1947 switch (button) {
1948 case BUTTON_DOWN:
1949 case BUTTON_F3 | BUTTON_DOWN:
1950 #ifndef SIMULATOR
1951 rectrigger();
1952 settings_apply_trigger();
1953 #endif
1954 exit = true;
1955 break;
1957 case BUTTON_LEFT:
1958 case BUTTON_F3 | BUTTON_LEFT:
1959 global_settings.rec_source++;
1960 if(global_settings.rec_source > AUDIO_SRC_MAX)
1961 global_settings.rec_source = 0;
1962 used = true;
1963 break;
1965 case BUTTON_F3 | BUTTON_REL:
1966 if ( used )
1967 exit = true;
1968 used = true;
1969 break;
1971 case BUTTON_F3 | BUTTON_REPEAT:
1972 used = true;
1973 break;
1975 default:
1976 if(default_event_handler(button) == SYS_USB_CONNECTED)
1977 return true;
1978 break;
1982 rec_init_recording_options(&rec_options);
1983 rec_set_recording_options(&rec_options);
1985 set_gain();
1987 settings_save();
1988 FOR_NB_SCREENS(i)
1989 screens[i].setfont(FONT_UI);
1991 return false;
1993 #endif /* CONFIG_KEYPAD == RECORDER_PAD */
1995 #if CONFIG_CODEC == SWCODEC
1996 void audio_beep(int duration)
1998 /* dummy */
1999 (void)duration;
2002 #ifdef SIMULATOR
2003 /* stubs for recording sim */
2004 void audio_init_recording(unsigned int buffer_offset)
2006 buffer_offset = buffer_offset;
2009 void audio_close_recording(void)
2013 unsigned long pcm_rec_get_warnings(void)
2015 return 0;
2018 unsigned long audio_recorded_time(void)
2020 return 123;
2023 unsigned long audio_num_recorded_bytes(void)
2025 return 5 * 1024 * 1024;
2028 void rec_set_source(int source, unsigned flags)
2030 source = source;
2031 flags = flags;
2034 void audio_set_recording_options(struct audio_recording_options *options)
2036 options = options;
2039 void audio_set_recording_gain(int left, int right, int type)
2041 left = left;
2042 right = right;
2043 type = type;
2046 void audio_record(const char *filename)
2048 filename = filename;
2051 void audio_new_file(const char *filename)
2053 filename = filename;
2056 void audio_stop_recording(void)
2060 void audio_pause_recording(void)
2064 void audio_resume_recording(void)
2068 #endif /* #ifdef SIMULATOR */
2069 #endif /* #ifdef CONFIG_CODEC == SWCODEC */
2071 #endif /* HAVE_RECORDING */