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

相关推荐
bing_feilong15 小时前
Mid360(2):运行livox_ros_driver2的demo失败
ubuntu·机器人
Code_LT20 小时前
【AIGC】Claude Code Rules配置
linux·ubuntu·aigc
TroubleMakerQi1 天前
[虚拟机环境配置]07_Ubuntu中安装vscode教程
linux·人工智能·vscode·ubuntu
ken22321 天前
在ubuntu终端里, 怎样让历史不要记录本条命令:禁止记录包含密码之类的命令
linux·运维·ubuntu
i建模1 天前
Ubuntu系统中安装NVIDIA驱动
linux·运维·ubuntu
张3蜂1 天前
Ubuntu Linux 与 Ubuntu with Rosetta:深入解析两者的区别与适用场景
linux·运维·ubuntu
廿一夏1 天前
搭建Ubuntu 虚拟机与部署docker
linux·ubuntu·docker
千里马-horse1 天前
ubuntu 电脑安装protoc-gen-grpc-kotlin
linux·运维·ubuntu
柯儿的天空1 天前
【OpenClaw 全面解析:从零到精通】第 004 篇:OpenClaw 在 Linux/Ubuntu 上的安装与部署实战
linux·人工智能·ubuntu·elasticsearch·知识图谱
开开心心就好1 天前
免费无广告的礼金记账本,安卓应用
java·前端·ubuntu·edge·pdf·负载均衡·语音识别