xref: /OK3568_Linux_fs/external/rk_pcba_test/tinyalsa/tinycap.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /* tinycap.c
2 **
3 ** Copyright 2011, The Android Open Source Project
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions are met:
7 **     * Redistributions of source code must retain the above copyright
8 **       notice, this list of conditions and the following disclaimer.
9 **     * Redistributions in binary form must reproduce the above copyright
10 **       notice, this list of conditions and the following disclaimer in the
11 **       documentation and/or other materials provided with the distribution.
12 **     * Neither the name of The Android Open Source Project nor the names of
13 **       its contributors may be used to endorse or promote products derived
14 **       from this software without specific prior written permission.
15 **
16 ** THIS SOFTWARE IS PROVIDED BY The Android Open Source Project ``AS IS'' AND
17 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE
20 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 ** DAMAGE.
27 */
28 
29 #include <tinyalsa/asoundlib.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <stdint.h>
33 #include <signal.h>
34 #include <string.h>
35 
36 #define ID_RIFF 0x46464952
37 #define ID_WAVE 0x45564157
38 #define ID_FMT  0x20746d66
39 #define ID_DATA 0x61746164
40 
41 #define FORMAT_PCM 1
42 
43 struct wav_header {
44     uint32_t riff_id;
45     uint32_t riff_sz;
46     uint32_t riff_fmt;
47     uint32_t fmt_id;
48     uint32_t fmt_sz;
49     uint16_t audio_format;
50     uint16_t num_channels;
51     uint32_t sample_rate;
52     uint32_t byte_rate;
53     uint16_t block_align;
54     uint16_t bits_per_sample;
55     uint32_t data_id;
56     uint32_t data_sz;
57 };
58 
59 int capturing = 1;
60 
61 unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device,
62                             unsigned int channels, unsigned int rate,
63                             enum pcm_format format, unsigned int period_size,
64                             unsigned int period_count);
65 
stop_record(void)66 void stop_record(void)
67 {
68     capturing = 0;
69 }
70 
tiny_cap(int argc,char ** argv)71 int tiny_cap(int argc, char **argv)
72 {
73     FILE *file;
74     struct wav_header header;
75     unsigned int card = 0;
76     unsigned int device = 0;
77     unsigned int channels = 2;
78     unsigned int rate = 44100;
79     unsigned int bits = 16;
80     unsigned int frames;
81     unsigned int period_size = 1024;
82     unsigned int period_count = 4;
83     enum pcm_format format;
84 
85 	capturing = 1;
86 
87     if (argc < 2) {
88         fprintf(stderr, "Usage: %s file.wav [-D card] [-d device] [-c channels] "
89                 "[-r rate] [-b bits] [-p period_size] [-n n_periods]\n", argv[0]);
90         return 1;
91     }
92 
93     file = fopen(argv[1], "wb");
94     if (!file) {
95         fprintf(stderr, "Unable to create file '%s'\n", argv[1]);
96         return 1;
97     }
98 
99     /* parse command line arguments */
100     argv += 2;
101     while (*argv) {
102         if (strcmp(*argv, "-d") == 0) {
103             argv++;
104             if (*argv)
105                 device = atoi(*argv);
106         } else if (strcmp(*argv, "-c") == 0) {
107             argv++;
108             if (*argv)
109                 channels = atoi(*argv);
110         } else if (strcmp(*argv, "-r") == 0) {
111             argv++;
112             if (*argv)
113                 rate = atoi(*argv);
114         } else if (strcmp(*argv, "-b") == 0) {
115             argv++;
116             if (*argv)
117                 bits = atoi(*argv);
118         } else if (strcmp(*argv, "-D") == 0) {
119             argv++;
120             if (*argv)
121                 card = atoi(*argv);
122         } else if (strcmp(*argv, "-p") == 0) {
123             argv++;
124             if (*argv)
125                 period_size = atoi(*argv);
126         } else if (strcmp(*argv, "-n") == 0) {
127             argv++;
128             if (*argv)
129                 period_count = atoi(*argv);
130         }
131         if (*argv)
132             argv++;
133     }
134 
135     header.riff_id = ID_RIFF;
136     header.riff_sz = 0;
137     header.riff_fmt = ID_WAVE;
138     header.fmt_id = ID_FMT;
139     header.fmt_sz = 16;
140     header.audio_format = FORMAT_PCM;
141     header.num_channels = channels;
142     header.sample_rate = rate;
143 
144     switch (bits) {
145     case 32:
146         format = PCM_FORMAT_S32_LE;
147         break;
148     case 24:
149         format = PCM_FORMAT_S24_LE;
150         break;
151     case 16:
152         format = PCM_FORMAT_S16_LE;
153         break;
154     default:
155         fprintf(stderr, "%d bits is not supported.\n", bits);
156         return 1;
157     }
158 
159     header.bits_per_sample = pcm_format_to_bits(format);
160     header.byte_rate = (header.bits_per_sample / 8) * channels * rate;
161     header.block_align = channels * (header.bits_per_sample / 8);
162     header.data_id = ID_DATA;
163 
164     /* leave enough room for header */
165     fseek(file, sizeof(struct wav_header), SEEK_SET);
166 
167     frames = capture_sample(file, card, device, header.num_channels,
168                             header.sample_rate, format,
169                             period_size, period_count);
170     printf("Captured %d frames\n", frames);
171 
172     /* write header now all information is known */
173     header.data_sz = frames * header.block_align;
174     header.riff_sz = header.data_sz + sizeof(header) - 8;
175     fseek(file, 0, SEEK_SET);
176     fwrite(&header, sizeof(struct wav_header), 1, file);
177 
178     fclose(file);
179 
180     return 0;
181 }
182 
capture_sample(FILE * file,unsigned int card,unsigned int device,unsigned int channels,unsigned int rate,enum pcm_format format,unsigned int period_size,unsigned int period_count)183 unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device,
184                             unsigned int channels, unsigned int rate,
185                             enum pcm_format format, unsigned int period_size,
186                             unsigned int period_count)
187 {
188     struct pcm_config config;
189     struct pcm *pcm;
190     char *buffer;
191     unsigned int size;
192     unsigned int bytes_read = 0;
193 
194     config.channels = channels;
195     config.rate = rate;
196     config.period_size = period_size;
197     config.period_count = period_count;
198     config.format = format;
199     config.start_threshold = 0;
200     config.stop_threshold = 0;
201     config.silence_threshold = 0;
202 
203     pcm = pcm_open(card, device, PCM_IN, &config);
204     if (!pcm || !pcm_is_ready(pcm)) {
205         fprintf(stderr, "Unable to open PCM device (%s)\n",
206                 pcm_get_error(pcm));
207         return 0;
208     }
209 
210     size = pcm_frames_to_bytes(pcm, pcm_get_buffer_size(pcm));
211     buffer = malloc(size);
212     if (!buffer) {
213         fprintf(stderr, "Unable to allocate %d bytes\n", size);
214         free(buffer);
215         pcm_close(pcm);
216         return 0;
217     }
218 
219     printf("Capturing sample: %u ch, %u hz, %u bit\n", channels, rate,
220            pcm_format_to_bits(format));
221 
222     while (capturing && !pcm_read(pcm, buffer, size)) {
223         if (fwrite(buffer, 1, size, file) != size) {
224             fprintf(stderr,"Error capturing sample\n");
225             break;
226         }
227         bytes_read += size;
228     }
229 
230     free(buffer);
231     pcm_close(pcm);
232     return pcm_bytes_to_frames(pcm, bytes_read);
233 }
234 
235