OpenWrt:使用ALSA实现边录边播

ALSA是Linux系统中的高级音频架构(Advanced Linux Sound Architecture)。目前已经成为了linux的主流音频体系结构,想了解更多的关于ALSA的知识,详见:http://www.alsa-project.org

在内核设备驱动层,ALSA提供了alsa-driver。同时在应用层,ALSA为我们提供了alsa-lib,应用程序只要调用alsa-lib提供的API,即可以完成对底层音频硬件的控制。ALSA也包括一系列实用工具如aplay(播放音频文件)、arecord(录制音频文件)、amixer(调节混音器设置)。

一.Ubuntu

在Ubuntu中,首先要确保已经安装了ALSA。可以用以下指令安装:

sudo apt-get install libasound2-dev

PS:centos 需换一个包:yum install alsa-lib-devel

安装后就可以引用libasound库了。实际上alsa-lib源码可编译出libasound.a

下面是实现代码
TestChat.cpp

复制代码
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>

snd_pcm_t *open_sound_dev(snd_pcm_stream_t type)
{
    int err;
    snd_pcm_t *handle;

    if ((err = snd_pcm_open (&handle, "default", type, 0)) < 0) {
        printf("open error: %s\n", snd_strerror(err));
        return NULL;
    }

    if ((err = snd_pcm_set_params(handle,
                                        SND_PCM_FORMAT_S16_LE,
                                        SND_PCM_ACCESS_RW_INTERLEAVED,
                                        1,
                                        16000,
                                        1,
                                        500000)) < 0) {    /* 0.5sec */
                printf("set params error: %s\n", snd_strerror(err));
                return NULL;
            }

    return handle;
}

void close_sound_dev(snd_pcm_t *handle)
{
    snd_pcm_close (handle);
}

snd_pcm_t *open_playback(void)
{
    return open_sound_dev(SND_PCM_STREAM_PLAYBACK);
}

snd_pcm_t *open_capture(void)
{
    return open_sound_dev(SND_PCM_STREAM_CAPTURE);
}

int main (int argc, char *argv[])
{
    int err;
    char buf[128];
    snd_pcm_t *playback_handle;
    snd_pcm_t *capture_handle;

    playback_handle = open_playback();
    if (!playback_handle)
    {
        fprintf (stderr, "cannot open for playback\n");
        return -1;
    }


    capture_handle = open_capture();
    if (!capture_handle)
    {
        fprintf (stderr, "cannot open for capture\n");
        return -1;
    }

    if ((err = snd_pcm_prepare (playback_handle)) < 0) {
        fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
             snd_strerror (err));
        return -1;
    }

    if ((err = snd_pcm_prepare (capture_handle)) < 0) {
        fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
             snd_strerror (err));
        return -1;
    }

    while (1) {
        if ((err = snd_pcm_readi (capture_handle, buf, 128)) != 128) {
            fprintf (stderr, "read from audio interface failed (%s)\n",
                 snd_strerror (err));
            break;
        }

        if ((err = snd_pcm_writei (playback_handle, buf, 128)) != 128) {
            fprintf (stderr, "write to audio interface failed (%s)\n",
                 snd_strerror (err));
            break;
        }
    }

    snd_pcm_close (playback_handle);
    snd_pcm_close (capture_handle);
    return 0;
}

编译

g++ TestCat.cpp -o TestChat -lasound

执行

./TestChat

就可以正常测试了

如果是VM虚拟机中Ubuntu,需要虚拟机列表中右键这个虚拟机,在弹出的这个快捷菜单中选择:可移动设备-》声卡-》连接,否则测试无声音。

二.OpenWrt

相关推荐
欧云服务器4 天前
怎么让脚本命令可以同时在centos、debian、ubuntu执行?
ubuntu·centos·debian
智渊AI4 天前
Ubuntu 20.04/22.04 下通过 NVM 安装 Node.js 22(LTS 稳定版)
ubuntu·node.js·vim
The️5 天前
Linux驱动开发之Read_Write函数
linux·运维·服务器·驱动开发·ubuntu·交互
再战300年5 天前
Samba在ubuntu上安装部署
linux·运维·ubuntu
qwfys2005 天前
How to install golang 1.26.0 to Ubuntu 24.04
ubuntu·golang·install
木尧大兄弟5 天前
Ubuntu 系统安装 OpenClaw 并接入飞书记录
linux·ubuntu·飞书·openclaw
小虾爬滑丫爬5 天前
ubuntu上设置Tomcat 开机启动
ubuntu·tomcat·开机启动
老师用之于民5 天前
【DAY25】线程与进程通信:共享内存、同步机制及实现方案
linux·c语言·ubuntu·visual studio code
小虾爬滑丫爬5 天前
Ubuntu 上设置防火墙
ubuntu·防火墙
林开落L5 天前
解决云服务器内存不足:2 分钟搞定 Ubuntu swap 交换区配置(新手友好版)
运维·服务器·ubuntu·swap交换区