您的位置:首页 > 编程语言 > C语言/C++

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

2016-09-22 11:28 633 查看

前言

昨天,写了在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。

[b]设备功能相关[/b]

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

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


其中,RTAUDIO_DEVICE这个结构体包含了一些信息,包括:

设备名称

最大最小可用的输入、输出设备

多通道

rates

数据格式

[b]Stream的创建以及参数[/b]

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

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);


[b]Stream控制[/b]

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

void startStream (int streamId);
void stopStream (int streamId);
void abortStream (int streamId);
void closeStream (int streamId);


[b]input/output[/b]

首先是得到指向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);


[b]回调函数[/b]

void setStreamCallback (int streamId, RTAUDIO_CALLBACK callback, void *userData);
void cancelStreamCallback (int streamId);


[b]代码片段[/b]

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: