linux usb麦克风采集, 注意设备名称(plughw:2,0)

/*

alsa_record.c
 */

#include <alsa/asoundlib.h>
#include <unistd.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    short buffer[4096];
    int err;
    unsigned int samplerate = 44100;
    snd_pcm_t *handle;
    snd_pcm_sframes_t frames;
    unsigned long ptr = 0, i;
    
    FILE *pOutFile = fopen("rec.pcm","wb+");

    err = snd_pcm_open(&handle, "plughw:2,0", SND_PCM_STREAM_CAPTURE, 0);
    if (err < 0) {
        perror("Device open error.\n");
        return -1;
    }

    err = snd_pcm_set_params(handle,
                             SND_PCM_FORMAT_S16_LE,
                             SND_PCM_ACCESS_RW_INTERLEAVED,
                             1,         /* 通道数 */
                             samplerate ,        /* 采样率 */
                             1,         /* 重采样 */
                             100000     /* 延迟(us) */
                            );
    if (err < 0) {
        perror("Playback open error.\n");
        return -1;
    }

    int number = 0;
    while (1) {
        
        number++;
        if(number > 100)
            break;
        
        frames = snd_pcm_readi(handle, buffer, 4096);
        if (frames == -EPIPE) {
            /*  EPIPE means overrun */
            fprintf(stderr, "overrun occurred\n");
            snd_pcm_prepare(handle);
        } else if (frames < 0) {
            fprintf(stderr, "error from read.\n");
        } else if (4096 != (int)frames) {
            fprintf(stderr, "read %ld, expected 4096.\n", frames);
        }


        int ret = fwrite(buffer,1,sizeof(buffer),pOutFile);
        printf("the ret is %d, the number is %d \n",ret,number);
    }
    snd_pcm_close(handle);  
    
    fclose(pOutFile);

    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值