Home | History | Annotate | Download | only in tinyalsa
      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 #include <time.h>
     36 
     37 #define ID_RIFF 0x46464952
     38 #define ID_WAVE 0x45564157
     39 #define ID_FMT  0x20746d66
     40 #define ID_DATA 0x61746164
     41 
     42 #define FORMAT_PCM 1
     43 
     44 struct wav_header {
     45     uint32_t riff_id;
     46     uint32_t riff_sz;
     47     uint32_t riff_fmt;
     48     uint32_t fmt_id;
     49     uint32_t fmt_sz;
     50     uint16_t audio_format;
     51     uint16_t num_channels;
     52     uint32_t sample_rate;
     53     uint32_t byte_rate;
     54     uint16_t block_align;
     55     uint16_t bits_per_sample;
     56     uint32_t data_id;
     57     uint32_t data_sz;
     58 };
     59 
     60 int capturing = 1;
     61 
     62 unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device,
     63                             unsigned int channels, unsigned int rate,
     64                             enum pcm_format format, unsigned int period_size,
     65                             unsigned int period_count, unsigned int cap_time);
     66 
     67 void sigint_handler(int sig __unused)
     68 {
     69     capturing = 0;
     70 }
     71 
     72 int main(int argc, char **argv)
     73 {
     74     FILE *file;
     75     struct wav_header header;
     76     unsigned int card = 0;
     77     unsigned int device = 0;
     78     unsigned int channels = 2;
     79     unsigned int rate = 44100;
     80     unsigned int bits = 16;
     81     unsigned int frames;
     82     unsigned int period_size = 1024;
     83     unsigned int period_count = 4;
     84     unsigned int cap_time = 0;
     85     enum pcm_format format;
     86 
     87     if (argc < 2) {
     88         fprintf(stderr, "Usage: %s file.wav [-D card] [-d device]"
     89                 " [-c channels] [-r rate] [-b bits] [-p period_size]"
     90                 " [-n n_periods] [-T capture time]\n", argv[0]);
     91         return 1;
     92     }
     93 
     94     file = fopen(argv[1], "wb");
     95     if (!file) {
     96         fprintf(stderr, "Unable to create file '%s'\n", argv[1]);
     97         return 1;
     98     }
     99 
    100     /* parse command line arguments */
    101     argv += 2;
    102     while (*argv) {
    103         if (strcmp(*argv, "-d") == 0) {
    104             argv++;
    105             if (*argv)
    106                 device = atoi(*argv);
    107         } else if (strcmp(*argv, "-c") == 0) {
    108             argv++;
    109             if (*argv)
    110                 channels = atoi(*argv);
    111         } else if (strcmp(*argv, "-r") == 0) {
    112             argv++;
    113             if (*argv)
    114                 rate = atoi(*argv);
    115         } else if (strcmp(*argv, "-b") == 0) {
    116             argv++;
    117             if (*argv)
    118                 bits = atoi(*argv);
    119         } else if (strcmp(*argv, "-D") == 0) {
    120             argv++;
    121             if (*argv)
    122                 card = atoi(*argv);
    123         } else if (strcmp(*argv, "-p") == 0) {
    124             argv++;
    125             if (*argv)
    126                 period_size = atoi(*argv);
    127         } else if (strcmp(*argv, "-n") == 0) {
    128             argv++;
    129             if (*argv)
    130                 period_count = atoi(*argv);
    131         } else if (strcmp(*argv, "-T") == 0) {
    132             argv++;
    133             if (*argv)
    134                 cap_time = atoi(*argv);
    135         }
    136         if (*argv)
    137             argv++;
    138     }
    139 
    140     header.riff_id = ID_RIFF;
    141     header.riff_sz = 0;
    142     header.riff_fmt = ID_WAVE;
    143     header.fmt_id = ID_FMT;
    144     header.fmt_sz = 16;
    145     header.audio_format = FORMAT_PCM;
    146     header.num_channels = channels;
    147     header.sample_rate = rate;
    148 
    149     switch (bits) {
    150     case 32:
    151         format = PCM_FORMAT_S32_LE;
    152         break;
    153     case 24:
    154         format = PCM_FORMAT_S24_LE;
    155         break;
    156     case 16:
    157         format = PCM_FORMAT_S16_LE;
    158         break;
    159     default:
    160         fprintf(stderr, "%u bits is not supported.\n", bits);
    161         fclose(file);
    162         return 1;
    163     }
    164 
    165     header.bits_per_sample = pcm_format_to_bits(format);
    166     header.byte_rate = (header.bits_per_sample / 8) * channels * rate;
    167     header.block_align = channels * (header.bits_per_sample / 8);
    168     header.data_id = ID_DATA;
    169 
    170     /* leave enough room for header */
    171     fseek(file, sizeof(struct wav_header), SEEK_SET);
    172 
    173     /* install signal handler and begin capturing */
    174     signal(SIGINT, sigint_handler);
    175     signal(SIGHUP, sigint_handler);
    176     signal(SIGTERM, sigint_handler);
    177     frames = capture_sample(file, card, device, header.num_channels,
    178                             header.sample_rate, format,
    179                             period_size, period_count, cap_time);
    180     printf("Captured %u frames\n", frames);
    181 
    182     /* write header now all information is known */
    183     header.data_sz = frames * header.block_align;
    184     header.riff_sz = header.data_sz + sizeof(header) - 8;
    185     fseek(file, 0, SEEK_SET);
    186     fwrite(&header, sizeof(struct wav_header), 1, file);
    187 
    188     fclose(file);
    189 
    190     return 0;
    191 }
    192 
    193 unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device,
    194                             unsigned int channels, unsigned int rate,
    195                             enum pcm_format format, unsigned int period_size,
    196                             unsigned int period_count, unsigned int cap_time)
    197 {
    198     struct pcm_config config;
    199     struct pcm *pcm;
    200     char *buffer;
    201     unsigned int size;
    202     unsigned int bytes_read = 0;
    203     unsigned int frames = 0;
    204     struct timespec end;
    205     struct timespec now;
    206 
    207     memset(&config, 0, sizeof(config));
    208     config.channels = channels;
    209     config.rate = rate;
    210     config.period_size = period_size;
    211     config.period_count = period_count;
    212     config.format = format;
    213     config.start_threshold = 0;
    214     config.stop_threshold = 0;
    215     config.silence_threshold = 0;
    216 
    217     pcm = pcm_open(card, device, PCM_IN, &config);
    218     if (!pcm || !pcm_is_ready(pcm)) {
    219         fprintf(stderr, "Unable to open PCM device (%s)\n",
    220                 pcm_get_error(pcm));
    221         return 0;
    222     }
    223 
    224     size = pcm_frames_to_bytes(pcm, pcm_get_buffer_size(pcm));
    225     buffer = malloc(size);
    226     if (!buffer) {
    227         fprintf(stderr, "Unable to allocate %u bytes\n", size);
    228         free(buffer);
    229         pcm_close(pcm);
    230         return 0;
    231     }
    232 
    233     printf("Capturing sample: %u ch, %u hz, %u bit\n", channels, rate,
    234            pcm_format_to_bits(format));
    235 
    236     clock_gettime(CLOCK_MONOTONIC, &now);
    237     end.tv_sec = now.tv_sec + cap_time;
    238     end.tv_nsec = now.tv_nsec;
    239 
    240     while (capturing && !pcm_read(pcm, buffer, size)) {
    241         if (fwrite(buffer, 1, size, file) != size) {
    242             fprintf(stderr,"Error capturing sample\n");
    243             break;
    244         }
    245         bytes_read += size;
    246         if (cap_time) {
    247             clock_gettime(CLOCK_MONOTONIC, &now);
    248             if (now.tv_sec > end.tv_sec ||
    249                 (now.tv_sec == end.tv_sec && now.tv_nsec >= end.tv_nsec))
    250                 break;
    251         }
    252     }
    253 
    254     frames = pcm_bytes_to_frames(pcm, bytes_read);
    255     free(buffer);
    256     pcm_close(pcm);
    257     return frames;
    258 }
    259