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 小时前
如何在 Ubuntu 24.04 (Noble) 上使用阿里源
linux·运维·ubuntu
好奇的菜鸟8 小时前
如何在Ubuntu上检查MySQL是否启动并放开3306端口
mysql·ubuntu·adb
ZPC821011 小时前
ubuntu 6.8.0 安装xenomai3.3
linux·运维·ubuntu
电脑能手12 小时前
遇到该问题:kex_exchange_identification: read: Connection reset`的解决办法
linux·ubuntu·ssh
snoopyfly~12 小时前
Ubuntu 24.04 安装配置 Redis 7.0 开机自启
linux·redis·ubuntu
精英的英12 小时前
在Ubuntu 24.04主机上创建Ubuntu 14.04编译环境的完整指南
linux·运维·ubuntu
奇妙之二进制13 小时前
计算机科学导论(10)什么是BIOS
ubuntu·计算机基础
岁月玲珑13 小时前
【如何判断Linux系统是Ubuntu还是CentOS】
linux·ubuntu·centos
Kevin不想说话9261914 小时前
Ubuntu 24.04 安装搜狗输入法完整教程
ubuntu
矩阵老炮18 小时前
Ubuntu20.4编译AOSP源码实践
ubuntu·aosp