RtAudio介绍(A Cross-Platform C++ Class for Realtime Audio Input/Output)

前言

昨天,写了在qt中使用rtaudio。但是总觉得不踏实,不负责任,还是应该再介绍介绍RtAudio,尤其是为那些不喜欢读英文文档的人。

根据这篇博客的题目,就可以看到rtaudio最主要的特性:
Cross-Platform 跨平台

RtAudio的设计

面向对象的C++结构

简单独立的头文件和源文件,方便加入工程

模块化、有函数回调

易用

control over multiple audio streams and devices with a single class instance

audio device capability probing

RtAudio 包含了audio streams和recording两个概念:
audio streams表示output,也就是playback
recording表示input。

RtAudio API

现在才是重头戏,介绍几个API。

设备功能相关

下面两个函数获取设备数量以及信息

int getDeviceCount (void);
void getDeviceInfo (int device, RTAUDIO_DEVICE *info);

其中,RTAUDIO_DEVICE这个结构体包含了一些信息,包括:
设备名称
最大最小可用的输入、输出设备
多通道
rates
数据格式

Stream的创建以及参数

构造函数(也有默认构造函数):

RtAudio (int *streamId,
         int outputDevice,
         int outputChannels,
         int inputDevice,
         int inputChannels,
         RTAUDIO_FORMAT format,
         int sampleRate,
         int *bufferSize,
         int numberOfBuffers);

A stream is opened with specified output and input devices, output and input channels, data format, sample rate, and buffer parameters. When successful, a stream identifier is returned which must be used for subsequent function calls on the stream.

int openStream (int outputDevice,
                int outputChannels,
                int inputDevice,
                int inputChannels,
                RTAUDIO_FORMAT format,
                int sampleRate,
                int *bufferSize,
                int numberOfBuffers);
Stream控制

openStream 后,我们还不能开始input和output,api为我们提供了几个控制函数:

void startStream (int streamId);
void stopStream (int streamId);
void abortStream (int streamId);
void closeStream (int streamId);
input/output

首先是得到指向stream buffer的指针。Memory management

char *const getStreamBuffer (int streamId);

The tickStream() function blocks until the data within the stream buffer can be completely processed by the audio device.
void tickStream (int streamId);

streamWillBlock() function is provided as a means for determining, a priori, whether the
tickStream() function will block, returning the number of sample frames that cannot be processed
without blocking.

int streamWillBlock (int streamId);
回调函数
void setStreamCallback (int streamId, RTAUDIO_CALLBACK callback, void *userData);
void cancelStreamCallback (int streamId);
代码片段

1

#include "RtAudio.h"
int main()
{
    int buffer_size = 256; // sample frames
    int id; // the stream id
    RtAudio *out;
    // Open a 2 channel output stream during
    // class instantiation using the default
    // device, 32-bit floating point data,
    // and 44100 Hz sample rate. Suggest the
    // use of 4 internal device buffers of
    // 256 sample frames each.
    out = new RtAudio(&id, 0, 2, 0, 0, RtAudio::RTAUDIO_FLOAT32, 44100, &buffer_size, 4);
    // Get a pointer to the stream buffer
    float *buf;
    buf = (float *)out->getStreamBuffer(id);
    // An example loop which runs for about
    // 40000 sample frames
    int count = 0;
    out->startStream(id);
    while (count < 40000) {
        // Generate samples and fill the buffer
        // with buffer_size sample frames.
        // Trigger the output of the data buffer
        out->tickStream(id);
        count += buffer_size;
    }
    out->stopStream(id);
    out->closeStream(id);
    delete out; // Cleanup.
    return 0;
}

2

#include <iostream.h>
#include "RtAudio.h"
using namespace std;
// Pass-through callback function.
int pass(char *buffer, int size, void *)
{
    // Surprise!! Nothing to do here.
    return 0;
}
int main()
{
    int buffer_size = 256; // sample frames
    int stream; // the stream id
    RtAudio *audio;
    // Open a 2 channel input/output stream
    // during class instantiation using the
    // default devices, 64-bit floating point
    // data, and 44100 Hz sample rate.
    // Suggest the use of 2 internal device
    // buffers of 256 sample frames each.
    audio = new RtAudio(&stream, 0, 2, 0, 2, RtAudio::RTAUDIO_FLOAT64, 44100, &buffer_size, 2);
    // Set the stream callback function
    audio->setStreamCallback(stream, &pass, NULL);
    audio->startStream(stream);
    cout << "Hit <enter> to quit." << endl;
    char input;
    cin.get(input);
    audio->stopStream(stream);
    audio->closeStream(stream);
    delete audio; // Cleanup
    return 0;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一苇渡江694

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值