[RK 3288][Android 6.0]Tinycap録音プログラムソースコードの概要

3113 ワード

Platform: Rockchip OS: Android 6.0
Kernel: 3.10.92
ソースコードはexternal/tinyalsa/tinycapにあります.c
int main(int argc, char **argv)
{
    FILE *file;
    struct wav_header header;
    //           ,   ,44.1kHz   ,16bit
    //period 1024 ,count  4 。
    unsigned int card = 0;
    unsigned int device = 0;
    unsigned int channels = 2;
    unsigned int rate = 44100;
    unsigned int bits = 16;
    unsigned int frames;
    unsigned int period_size = 1024;
    unsigned int period_count = 4;
    enum pcm_format format;


	//          。
    file = fopen(argv[1], "wb");
    if (!file) {
        fprintf(stderr, "Unable to create file '%s'
", argv[1]); return 1; } // argv += 2; while (*argv) { if (strcmp(*argv, "-d") == 0) { argv++; if (*argv) device = atoi(*argv); ...... } //wav header.riff_id = ID_RIFF; header.riff_sz = 0; header.riff_fmt = ID_WAVE; header.fmt_id = ID_FMT; header.fmt_sz = 16; header.audio_format = FORMAT_PCM; header.num_channels = channels; header.sample_rate = rate; ...... /* leave enough room for header */ fseek(file, sizeof(struct wav_header), SEEK_SET); ...... frames = capture_sample(file, card, device, header.num_channels, header.sample_rate, format, period_size, period_count); ...... // wav header 。 fwrite(&header, sizeof(struct wav_header), 1, file); fclose(file); return 0; }
capture_sample():
unsigned int 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)
{
    struct pcm_config config;
......
	//   config
    config.channels = channels;
    config.rate = rate;
    config.period_size = period_size;
    config.period_count = period_count;
    config.format = format;
    config.start_threshold = 0;
    config.stop_threshold = 0;
    config.silence_threshold = 0;
	//  tinyalsa    
    pcm = pcm_open(card, device, PCM_IN, &config);
    if (!pcm || !pcm_is_ready(pcm)) {
        fprintf(stderr, "Unable to open PCM device (%s)
", pcm_get_error(pcm)); return 0; } // frame, size. size = pcm_frames_to_bytes(pcm, pcm_get_buffer_size(pcm)); buffer = malloc(size); ...... // size 。 while (capturing && !pcm_read(pcm, buffer, size)) { // if (fwrite(buffer, 1, size, file) != size) { fprintf(stderr,"Error capturing sample
"); break; } // bytes_read += size; } ...... // return pcm_bytes_to_frames(pcm, bytes_read); }

全体の流れは比較的簡単で、コア部分は主にpcm_Open()およびpcm_read()興味があれば検討してください.
でも注意したいのはperoid_sizeとperoid_countには範囲があり、どんな数値でもいいわけではないが、frame rateは固定で44.1 kHzである.