Linux下使用ALSA进行音频播放

在这里插入图片描述

编译方法

gcc -o main main.c -lasound

  • 注: 这个程序必须链接到alsalib库,通过在编译时需要加上-lasound选项。有些alsa库函数使用dlopen函数以及浮点操作,所以您可能还需要加上-ldl,-lm选项。

alsa显示详细参数信息

#include <alsa/asoundlib.h>

int main()
{
    int val;
    printf("ALSA library version: %s\n",SND_LIB_VERSION_STR);
    
    printf("\nPCM stream types:\n");
	for (val = 0; val <= SND_PCM_STREAM_LAST; val++){
		printf(" %s\n",snd_pcm_stream_name((snd_pcm_stream_t)val));
	}


    printf("\nPCM access types:\n");
    for (val = 0; val <= SND_PCM_ACCESS_LAST; val++)
    {
            printf(" %s\n",snd_pcm_access_name((snd_pcm_access_t)val));
    }

    printf("\nPCM formats:\n");
    for(val = 0; val <= SND_PCM_FORMAT_LAST; val++)
    {
        if(snd_pcm_format_name((snd_pcm_format_t)val)!= NULL)
        {
                  printf(" %s (%s)\n",snd_pcm_format_name((snd_pcm_format_t)val),snd_pcm_format_description((snd_pcm_format_t)val));
        }
    }

    printf("\nPCM subformats:\n");
    for (val = 0; val <= SND_PCM_SUBFORMAT_LAST;val++)
    {
        printf(" %s (%s)\n",snd_pcm_subformat_name((snd_pcm_subformat_t)val),snd_pcm_subformat_description((snd_pcm_subformat_t)val));
    }

    printf("\nPCM states:\n");
    for (val = 0; val <= SND_PCM_STATE_LAST; val++){
		printf(" %s\n",snd_pcm_state_name((snd_pcm_state_t)val));
    }

    return 0;
}

alsa 播放音频代码

#include <alsa/asoundlib.h>

snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
snd_pcm_uframes_t frames = 1024;//一个周期多少帧
snd_pcm_access_t access_mode = SND_PCM_ACCESS_RW_INTERLEAVED;//访问模式:交错访问
snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;//采样位数:16位,小端存储
int channel = 2;
unsigned int simple_rate = 44100;//采样率
//int dir;//设备采样率与输入采样的偏差

char *buffer = NULL;//缓冲区数据
int size;//缓冲区大小

int main()
{
    /* 1. 打开pcm设备 */
    int rc = snd_pcm_open(&handle, "default",SND_PCM_STREAM_PLAYBACK, 0);
    if (rc < 0) {
        printf("open device failed\n");
        return 0;
    }

    /* 分配一个硬件参数对象 */
    snd_pcm_hw_params_alloca(&params);
    snd_pcm_hw_params_any(handle, params);/* 使用默认值填充参数对象. */
    /* 设置硬件参数 */
    snd_pcm_hw_params_set_access(handle, params,access_mode);/* 交错模式 Interleaved mode */
    snd_pcm_hw_params_set_format(handle, params,format);/* 采样位数 Signed 16-bit little-endian format */
    snd_pcm_hw_params_set_channels(handle, params, channel);/* 通道数 Two channels (stereo) */
    snd_pcm_hw_params_set_rate_near(handle, params,&simple_rate, NULL);/* 采样率 44100 bits/second sampling rate (CD quality) */
    snd_pcm_hw_params_set_period_size_near(handle,params, &frames, NULL);//设置一个周期的多少帧
    rc = snd_pcm_hw_params(handle, params);/* 将设置好的参数写入驱动 */
    if (rc < 0) {
        printf("unable to set hw parameters: %s\n",snd_strerror(rc));
        return 0;
    }
    
    do{
        rc = snd_pcm_writei(handle, buffer, frames);
        if( rc>0 && rc<frames ){
            printf("short write\n");
        }else if(rc == -EPIPE){
            printf("underrun occurred\n");
            snd_pcm_prepare(handle);
        }else if (rc < 0){
            printf("error from writei: %s\n",snd_strerror(rc));
            break;
        }
    }while(1);
    
    snd_pcm_drain(handle);
    snd_pcm_close(handle);
}
  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux系统中使用ALSA(Advanced Linux Sound Architecture)库进行录音是一个相对简单的过程。ALSALinux内核中的一种音频驱动框架,提供了对音频硬件的底层控制和访问接口。 要使用ALSA库录音,需要进行以下步骤: 1. 打开音频设备:首先需要打开音频设备以开始录音。可以使用`snd_pcm_open()`函数来打开默认音频设备。例如,可以使用如下代码打开默认的音频捕获设备: ```C++ snd_pcm_t* handle; int err = snd_pcm_open(&handle, "default", SND_PCM_STREAM_CAPTURE,0); if(err < 0) { // 错误处理 } ``` 2. 配置硬件参数:在打开音频设备后,需要通过设置硬件参数来配置录音质量。可以使用`snd_pcm_hw_params_t`类型的变量来设置参数。例如,可以使用如下代码配置采样率为44.1kHz,通道数为2的参数: ```C++ snd_pcm_hw_params_t *params; int err = snd_pcm_hw_params_malloc(&params); if (err < 0) { // 错误处理 } err = snd_pcm_hw_params_any(handle, params); if (err < 0) { // 错误处理 } err = snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED); if (err < 0) { // 错误处理 } err = snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE); if (err < 0) { // 错误处理 } unsigned int rate = 44100; err = snd_pcm_hw_params_set_rate_near(handle, params, &rate, 0); if (err < 0) { // 错误处理 } unsigned int channels = 2; err = snd_pcm_hw_params_set_channels(handle, params, channels); if (err < 0) { // 错误处理 } err = snd_pcm_hw_params(handle, params); if (err < 0) { // 错误处理 } ``` 3. 录音处理:在配置完硬件参数后,可以使用`snd_pcm_readi()`函数来读取音频数据进行录音。例如,可以使用如下代码读取音频数据并输出到文件中: ```C++ FILE *file; file = fopen("recording.wav", "w"); if (file == NULL) { // 错误处理 } char buffer[1024]; int frames = 1024; int err; while (1) { err = snd_pcm_readi(handle, buffer, frames); if (err == -EPIPE) { // 捕获到溢出错误,需要进行错误处理 } else if (err < 0) { // 其他错误处理 } else { fwrite(buffer, sizeof(char), frames, file); } } fclose(file); ``` 4. 关闭音频设备:录音完成后,需要关闭音频设备以释放资源。可以使用`snd_pcm_close()`函数来关闭音频设备。例如,可以使用如下代码关闭音频设备: ```C++ snd_pcm_close(handle); ``` 以上就是在Linux系统下使用ALSA库录音的简要步骤。通过控制音频设备和配置参数,我们可以实现自定义的录音功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值