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

相关推荐
davenian5 小时前
< 自用文 OS 有关 > (续)发现正在被攻击 后的自救 Fail2ban + IPset + UFW 工作流程详解
ubuntu·bash·fail2ban·ipset
阿贤Linux6 小时前
设置网卡名称为传统命名方式
linux·ubuntu
GoodG_study7 小时前
windows通过xrdp远程连接Ubuntu黑屏问题解决
linux·ubuntu
一张假钞8 小时前
Windows 11主机Ubuntu 24.04虚机共享目录权限问题
linux·运维·ubuntu
GoodG_study8 小时前
一文教您解决win11运行Ubuntu,wsl相关命令出现系统找不到指定文件的错误提示
linux·ubuntu·wsl
daizhe10 小时前
Mac M4环境下基于VMware Fusion虚拟机安装Ubuntu24.04 LTS ARM版
ubuntu·macos
光电的一只菜鸡19 小时前
ubuntu之坑(十九)——VMware虚拟机扩容磁盘
linux·数据库·ubuntu
格林威20 小时前
Linux使用-Linux系统管理
linux·运维·服务器·深度学习·ubuntu·计算机视觉
一匹电信狗1 天前
【Linux我做主】细说进程等待
linux·运维·服务器·c++·ubuntu·小程序·开源