Linux alsa 音频 录制与播放

一. 概述

ALSA是Advanced Linux Sound Architecture 的缩写,目前已经成为了linux的主流音频体系结构。

在内核设备驱动层,ALSA提供了alsa-driver,同时在应用层,ALSA为咱们提供了alsa-lib,应用程序只要调用alsa-lib提供的API,便可以完成对底层音频硬件的控制。

Linux alsa 音频 录制与播放_第1张图片

二.常用命令

aplay -l 显示实际声卡序号

查看声卡:

cat /proc/asound/cards

录音:

arecord -D hw:0,0 -c 2 -r 44100 -f S16_LE test.wav

播放:

aplay -Dplughw:0,0 test.wav

打开耳机功能

amixer cset numid=1,iface=MIXER,name='Playback Path' 3
Linux alsa 音频 录制与播放_第2张图片

打开MIC(麦克风)功能:

amixer cset numid=2,iface=MIXER,name='Capture MIC Path' 1
Linux alsa 音频 录制与播放_第3张图片

三.音频参数理解

声道

单声道:mono

双声道:stereo。最常见的类型,包含左声道以及右声道

采样率

音频采样,是把声音从模拟信号转换为数字信号。采样率,就是每秒对声音进行采集的次数,同样也是所得的数字信号的每秒样本数

采样越高,声音的还原就越真实越自然,人对频率的识别范围是 20HZ - 22000HZ, 如果每秒钟能对声音做 22000 个采样, 回放时就足可以满足人耳的需求. 所以 22050 的采样频率是常用的, 根据奈奎斯特采样定理44100Hz是不失真的情况下的采样率, 超过48000的采样对人耳已经没有意义。

四.用户空间打开PCM设备录制声音

#define ALSA_PCM_NEW_HW_PARAMS_API

#include 

int main() {
    long loops;
    int rc;
    int size;
    unsigned int val;
    int dir;
    char *buffer;
    snd_pcm_t *handle;
    snd_pcm_hw_params_t *params;
    snd_pcm_uframes_t frames;

    /*以录制模式打开*/
    /* Open PCM device for recording (capture). */
    rc = snd_pcm_open( &handle, "default", SND_PCM_STREAM_CAPTURE, 0);
    if (rc < 0) {
        fprintf(stderr, "unable to open pcm device");
        exit(EXIT_FAILURE);
    }

    /*分配一个参数对象*/
    /* Allocate a hardware parameters object. */
    snd_pcm_hw_params_alloca(¶ms);
    /*初始化参数对象*/
    /* Fill it in with default values. */
    rc = snd_pcm_hw_params_any(handle, params);
    if (rc < 0) {
        printf("Err\n");
    }
    /* Set the desired hardware parameters. */

    /*交错模式*/
    /* Interleaved mode */
    rc = snd_pcm_hw_params_set_access(handle, params,
                          SND_PCM_ACCESS_RW_INTERLEAVED);
    if (rc < 0) {
        printf("Err\n");
    }
    /*PCM格式*/
    /* Signed 16-bit little-endian format */
    rc = snd_pcm_hw_params_set_format(handle, params,
                                  SND_PCM_FORMAT_S16_LE);
    if (rc < 0) {
        printf("Err\n");
    }
    /*设置通道数*/
    /* Two channels (stereo) */
    rc = snd_pcm_hw_params_set_channels(handle, params, 2);
    if (rc < 0) {
        printf("Err\n");
    }
    /*设置采样率*/
    /* 44100 bits/second sampling rate (CD quality) */
    val = 44100;
    rc = snd_pcm_hw_params_set_rate_near(handle, params,
                                &val, &dir);
    if (rc < 0) {
        printf("Err\n");
    }
    /*没周期的帧数*/
    /* Set period size to 32 frames. */
    frames = 32;
    rc = snd_pcm_hw_params_set_period_size_near(handle,
                            params, &frames, &dir);
    if (rc < 0) {
        printf("Err\n");
    }
    /* Write the parameters to the driver */
    rc = snd_pcm_hw_params(handle, params);
    if (rc < 0) {
        fprintf(stderr,
                "unable to set hw parameters: %s/n",
                snd_strerror(rc));
        exit(1);
    }

    /* Use a buffer large enough to hold one period */
    rc = snd_pcm_hw_params_get_period_size(params,
                                          &frames, &dir);
    if (rc < 0) {
        printf("Err\n");
    }
    size = frames * 4; /* 2 bytes/sample, 2 channels */
    buffer = (char *) malloc(size);

    /* We want to loop for 5 seconds */
    rc = snd_pcm_hw_params_get_period_time(params, &val, &dir);
    loops = 5000000 / val;

    while (loops > 0) {
        loops--;
        rc = snd_pcm_readi(handle, buffer, frames);
        if (rc == -EPIPE) {
          /* EPIPE means overrun */
          fprintf(stderr, "overrun occurred/n");
          //把PCM流置于PREPARED状态,这样下次我们向该PCM流中数据时,它就能重新开始处理数据。
          snd_pcm_prepare(handle);
        } else if (rc < 0) {
          fprintf(stderr,
                  "error from read: %s/n",
                  snd_strerror(rc));
        } else if (rc != (int)frames) {
          fprintf(stderr, "short read, read %d frames/n", rc);
        }
        rc = write(1, buffer, size);
        if (rc != size)
          fprintf(stderr,
                  "short write: wrote %d bytes/n", rc);
    }

    //调用snd_pcm_drain把所有挂起没有传输完的声音样本传输完全
    rc = snd_pcm_drain(handle);
    //关闭该音频流,释放之前动态分配的缓冲区,退出
    rc = snd_pcm_close(handle);
    free(buffer);

    return 0;
}

五.用户空间打开PCM设备播放声音

#define ALSA_PCM_NEW_HW_PARAMS_API

#include 

int main() {
    long loops;
    int rc;
    int size;
    snd_pcm_t *handle;
    snd_pcm_hw_params_t *params;
    unsigned int val;
    int dir;
    snd_pcm_uframes_t frames;
    char *buffer;

    /* Open PCM device for playback. */
    rc = snd_pcm_open(&handle, "default",
                    SND_PCM_STREAM_PLAYBACK, 0);
    if (rc < 0) {
        fprintf(stderr,
                "unable to open pcm device: %s/n",
                snd_strerror(rc));
        exit(1);
    }

    /*分配一个参数对象*/
    /* Allocate a hardware parameters object. */
    snd_pcm_hw_params_alloca(¶ms);

    /*初始化参数对象*/
    /* Fill it in with default values. */
    snd_pcm_hw_params_any(handle, params);

    /* Set the desired hardware parameters. */

    /*交错模式*/
    /* Interleaved mode */
    snd_pcm_hw_params_set_access(handle, params,
                      SND_PCM_ACCESS_RW_INTERLEAVED);

    /*设置PCM格式*/
    /* Signed 16-bit little-endian format */
    snd_pcm_hw_params_set_format(handle, params,
                              SND_PCM_FORMAT_S16_LE);

    /*设置通道数*/
    /* Two channels (stereo) */
    snd_pcm_hw_params_set_channels(handle, params, 2);

    /*设置采样率*/
    /* 44100 bits/second sampling rate (CD quality) */
    val = 44100;
    snd_pcm_hw_params_set_rate_near(handle, params,
                                  &val, &dir);

    /* Set period size to 32 frames. */
    frames = 32;
    snd_pcm_hw_params_set_period_size_near(handle,
                              params, &frames, &dir);

    /* Write the parameters to the driver */
    rc = snd_pcm_hw_params(handle, params);
    if (rc < 0) {
        fprintf(stderr,
                "unable to set hw parameters: %s/n",
                snd_strerror(rc));
        exit(1);
    }

    /* Use a buffer large enough to hold one period */
    snd_pcm_hw_params_get_period_size(params, &frames, &dir);
    size = frames * 4; /* 2 bytes/sample, 2 channels */
    buffer = (char *) malloc(size);

    /* We want to loop for 5 seconds */
    snd_pcm_hw_params_get_period_time(params,
                                    &val, &dir);
    /* 5 seconds in microseconds divided by
    * period time */
    loops = 5000000 / val;

    while (loops > 0) {
        loops--;
        rc = read(0, buffer, size);
        if (rc == 0) {
            fprintf(stderr, "end of file on input/n");
            break;
        } else if (rc != size) {
            fprintf(stderr,
                  "short read: read %d bytes/n", rc);
        }
        rc = snd_pcm_writei(handle, buffer, frames);
        if (rc == -EPIPE) {
            /* EPIPE means underrun */
            fprintf(stderr, "underrun occurred/n");
            //把PCM流置于PREPARED状态,这样下次我们向该PCM流中数据时,它就能重新开始处理数据。
            snd_pcm_prepare(handle); 
        } else if (rc < 0) {
            fprintf(stderr,
                "error from writei: %s/n",
            snd_strerror(rc));
        }  else if (rc != (int)frames) {
            fprintf(stderr,
                  "short write, write %d frames/n", rc);
        }
    }

    //调用snd_pcm_drain把所有挂起没有传输完的声音样本传输完全
    snd_pcm_drain(handle);
    //关闭该音频流,释放之前动态分配的缓冲区,退出
    snd_pcm_close(handle);
    free(buffer);

  return 0;
}

你可能感兴趣的:(应用编程,linux音频子系统,linux)