Qt–使用RtAudio进行录音以及播放

其实,本篇博客主要是介绍1下RtAudio,只是在qt中使用罢了!

甚么是RtAudio?
RtAudio provides a common API for realtime audio input/output across Linux (native ALSA, Jack, and OSS), Macintosh OS X (CoreAudio and Jack), and Windows(DirectSound, ASIO and WASAPI) operating systems.

官方地址:
http://www.music.mcgill.ca/~gary/rtaudio/

github地址:
https://github.com/thestk/rtaudio

下载rtaudio:
最新版本 (22 February 2016): Version 4.1.2

官方文档利用例子:

#include "RtAudio.h"
#include <iostream>
#include <cstdlib>
#include <cstring>
int record( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
double streamTime, RtAudioStreamStatus status, void *userData )
{
if ( status )
std::cout << "Stream overflow detected!" << std::endl;
// Do something with the data in the "inputBuffer" buffer.
return 0;
}
int main()
{
RtAudio adc;
if ( adc.getDeviceCount() < 1 ) {
std::cout << "\nNo audio devices found!\n";
exit( 0 );
}
RtAudio::StreamParameters parameters;
parameters.deviceId = adc.getDefaultInputDevice();
parameters.nChannels = 2;
parameters.firstChannel = 0;
unsigned int sampleRate = 44100;
unsigned int bufferFrames = 256; // 256 sample frames
try {
adc.openStream( NULL, &parameters, RTAUDIO_SINT16,
sampleRate, &bufferFrames, &record );
adc.startStream();
}
catch ( RtAudioError& e ) {
e.printMessage();
exit( 0 );
}

char input;
std::cout << "\nRecording ... press <enter> to quit.\n";
std::cin.get( input );
try {
// Stop the stream
adc.stopStream();
}
catch (RtAudioError& e) {
e.printMessage();
}
if ( adc.isStreamOpen() ) adc.closeStream();
return 0;
}

Qt中的利用

功能:
1左声道
2右声道
3双声道

封装1个类,叫ThreadRecord,在多线程中使用RrAudio,固然是继承QThread:

class ThreadRecord :public QThread
{
public:
ThreadRecord();
static int record(void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames, double streamTime, RtAudioStreamStatus status, void *userData);
bool flag;
int type;//通过type进行判断是使用左声道、右声道、双声道

protected:
void run();
private:
RtAudio adc;
RtAudio::StreamParameters input;
RtAudio::StreamParameters output;
};

实现threadrecord.cc:

int ThreadRecord::record(void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames, double streamTime, RtAudioStreamStatus status, void *userData)
{
if (status)
cout << "Stream overflow detected!" << std::endl;

short* output = static_cast<short*>(outputBuffer);
short* input = static_cast<short*>(inputBuffer);
short n = nBufferFrames;

int i = *(static_cast<int*>(userData));

if(i == 2) {
for (i = 0; i < nBufferFrames; i += 1) {
output[i] = input[i];
}
} else {
for (; i < nBufferFrames; i += 2) {
output[i] = input[i];
}
}

return 0;
}

void ThreadRecord::run()
{
//adc为成员变量:RtAudio adc_;
// adc_.getDeviceCount()取得音频装备数量
for(int i = 0 ; i < adc_.getDeviceCount() ; i++)
{
qDebug()<< i << adc_.getDeviceInfo(i).name.c_str();
}

if (adc_.getDeviceCount() < 1) {
cout << "\nNo audio devices found!\n";
exit( 0 );
}

//adc_.getDefaultInputDevice();取得默许输入装备
input.deviceId = adc_.getDefaultInputDevice();
input.nChannels = 2;
input.firstChannel = 0;

output.deviceId = adc_.getDefaultOutputDevice();
output.nChannels = 2;
output.firstChannel = 0;

unsigned int sampleRate = 44100;
unsigned int bufferFrames = 256;
try {
adc_.openStream(&output, &input, RTAUDIO_SINT16, sampleRate,
&bufferFrames, &record, static_cast<void*>(&type),
NULL, &ErrorCallback);
adc_.startStream();
}
catch (RtAudioError& e) {
e.printMessage();
exit(0);
}

while (flag);

try {
adc_.stopStream();
}
catch (RtAudioError& e) {
e.printMessage();
}
if (adc_.isStreamOpen())
adc_.closeStream();

}

使用,为了节省篇幅,只写1个函数:
type=0 左声道
type=1 右声道
type=2 双声道

void Recording::onLeftSoundButtonClicked()
{
if (thread_record.isRunning())
thread_record.exit(0);
thread_record.flag = true;
thread_record.type = 0;
thread_record.start();
}
波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » Qt–使用RtAudio进行录音以及播放

发表评论

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡