summaryrefslogtreecommitdiffstats
path: root/apps/pcm_recording.c
blob: 67f7d4da3c84af00f7f569940e587e4e5de0a423 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 by Linus Nielsen Feltzing
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "config.h"

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

#include "system.h"
#include "lcd.h"
#include "led.h"
#include "audio.h"
#include "button.h"
#include "kernel.h"
#include "settings.h"
#include "lang.h"
#include "font.h"
#include "icons.h"
#include "screens.h"
#include "status.h"
#include "menu.h"
#include "sound_menu.h"
#include "timefuncs.h"
#include "debug.h"
#include "misc.h"
#include "tree.h"
#include "string.h"
#include "dir.h"
#include "errno.h"
#include "atoi.h"
#include "sound.h"
#include "ata.h"
#include "logf.h"
#include "uda1380.h"
#include "pcm_record.h"

#ifdef HAVE_UDA1380

bool pcm_rec_screen(void)
{
    char buf[80];
    char filename[MAX_PATH];
    char *rec_sources[2] = {"Line-in","Mic"};
    int line=0;
    int play_vol;
    int rec_monitor, rec_gain, rec_vol, rec_source, rec_count, rec_waveform;   
    int rec_time;
    int done, button;
    int w, h;
    
    lcd_setfont(FONT_SYSFIXED);
    lcd_getstringsize("M", &w, &h);
    lcd_setmargins(global_settings.invert_cursor ? 0 : w, 8);
 
    play_vol = 120;    

    //cpu_boost(true);

    uda1380_enable_output(true);
    uda1380_set_master_vol(play_vol, play_vol);
      
    rec_monitor = 0;     // No record feedback
    rec_source  = 1;     // Mic
    rec_gain    = 0;     // 0-15
    rec_vol     = 0;     // 0-255
    rec_count   = 0;
    rec_waveform = 0;
    
    pcm_open_recording();
    pcm_set_recording_options(rec_source, rec_waveform);
    pcm_set_recording_gain(rec_gain, rec_vol);
    
    //rec_create_directory();
    
    done = 0;
    while(!done)
    {
        line = 0;
        
        snprintf(buf, sizeof(buf), "PlayVolume: %3d", play_vol);
        lcd_puts(0,line++, buf);    
        snprintf(buf, sizeof(buf), "Gain   : %2d  Volume  : %2d", rec_gain, rec_vol);
        lcd_puts(0,line++, buf);    
        snprintf(buf, sizeof(buf), "Monitor: %2d  Waveform: %2d", rec_monitor, rec_waveform);
        lcd_puts(0,line++, buf);    

        line++;

        rec_time = pcm_recorded_time();

        snprintf(buf, sizeof(buf), "Status: %s", pcm_status() & AUDIO_STATUS_RECORD ? "RUNNING" : "STOPPED");
        lcd_puts(0,line++, buf);
        snprintf(buf, sizeof(buf), "Source: %s", rec_sources[rec_source]);
        lcd_puts(0,line++, buf);
        snprintf(buf, sizeof(buf), "File  : %s", filename);
        lcd_puts(0,line++, buf);
        snprintf(buf, sizeof(buf), "Time  : %02d:%02d.%02d", rec_time/HZ/60, (rec_time/HZ)%60, rec_time%HZ);
        lcd_puts(0,line++, buf);        
        
        line++;
        
        snprintf(buf, sizeof(buf), "MODE    : Select source");
        lcd_puts(0,line++, buf);
        snprintf(buf, sizeof(buf), "UP/DOWN : Record volume");
        lcd_puts(0,line++, buf);
        snprintf(buf, sizeof(buf), "LFT/RGHT: Record gain");
        lcd_puts(0,line++, buf);
        snprintf(buf, sizeof(buf), "RMT MENU: Toggle monitor");
        lcd_puts(0,line++, buf);
        snprintf(buf, sizeof(buf), "RMT PLAY: Toggle waveform");
        lcd_puts(0,line++, buf);

        
        lcd_update();
        
        
        button = button_get_w_tmo(HZ/8);
        switch (button)
        {
            case BUTTON_OFF:
                done = true;
                break;

            case BUTTON_REC:
                if (pcm_status() & AUDIO_STATUS_RECORD)
                {
                    pcm_stop_recording();
                   
                } else
                {
                                snprintf(filename, MAX_PATH, "/record-%0d.wav", rec_count++);
                                pcm_record(filename);
                            }
                break;
        
            case BUTTON_ON:
                break;

            case BUTTON_MODE:
                rec_source = 1 - rec_source;
                pcm_set_recording_options(rec_source, rec_waveform);
                break;

            case BUTTON_RIGHT:
            case BUTTON_RIGHT | BUTTON_REPEAT:
                if (rec_gain < 15)
                    rec_gain++;
                
                pcm_set_recording_gain(rec_gain, rec_vol);
                break;

            case BUTTON_LEFT:
            case BUTTON_LEFT | BUTTON_REPEAT:
                if (rec_gain > 0)
                    rec_gain--;
                    
                pcm_set_recording_gain(rec_gain, rec_vol);
                break;
            
            case BUTTON_RC_MENU:
                rec_monitor = 1 - rec_monitor;
                uda1380_set_monitor(rec_monitor);
                break;
            
            case BUTTON_RC_ON:
                rec_waveform = 1 - rec_waveform;
                pcm_set_recording_options(rec_source, rec_waveform);
                break;
            
            case BUTTON_UP:
            case BUTTON_UP | BUTTON_REPEAT:
                if (rec_vol<255) 
                    rec_vol++;
                pcm_set_recording_gain(rec_gain, rec_vol);
                break;

            case BUTTON_DOWN:
            case BUTTON_DOWN | BUTTON_REPEAT:
                if (rec_vol)
                    rec_vol--;
                pcm_set_recording_gain(rec_gain, rec_vol);
                break;

            case SYS_USB_CONNECTED:
                if (pcm_status() & AUDIO_STATUS_RECORD)
                {
                    // ignore usb while recording
                } else
                {
                    pcm_stop_recording();
                    
                    uda1380_enable_output(false);
                    
                    default_event_handler(SYS_USB_CONNECTED);
                    return false;
                }
                break;
            
        }
        
    }
 
    pcm_stop_recording(); 
    pcm_close_recording();
    
    uda1380_enable_output(false);
 
    return true;   
}

#endif