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

相关推荐
序属秋秋秋6 小时前
《Linux系统编程之开发工具》【实战:倒计时 + 进度条】
linux·运维·服务器·c语言·c++·ubuntu·系统编程
曾小蛙18 小时前
【ROS2+深度相机】在Ubuntu安装realsense-ros
ubuntu·realsense-ros·ros2 humble·d435
Elendill20 小时前
【Ubuntu】Ubuntu 服务器升级系统操作记录
运维·服务器·ubuntu
HIT_Weston21 小时前
16、【Ubuntu】【VSCode】VSCode 断联问题分析:问题解决
linux·vscode·ubuntu
NON-JUDGMENTAL1 天前
在 Ubuntu 上安装 Ollama 并通过 Open WebUI 运行本地大语言模型
linux·ubuntu·语言模型
小白也想学C1 天前
ubuntu22.04下载QQ音乐闪退问题
ubuntu
海蓝可知天湛1 天前
Ubuntu24.10禁用该源...+vmware无法复制黏贴“天坑闭环”——从 DNS 诡异解析到 Ubuntu EOL 引发的 apt 404排除折
linux·ubuntu
HIT_Weston1 天前
23、【Ubuntu】【远程开发】内网穿透:SSH 反向隧道
linux·ubuntu·ssh
成为你的宁宁1 天前
Ubuntu安装mysql5.7及常见错误问题
linux·mysql·ubuntu
HIT_Weston1 天前
22、【Ubuntu】【远程开发】技术方案选择
linux·tcp/ip·ubuntu