Combining libsndfile and RtAudio?
Combining libsndfile and RtAudio?
所以,我一直在尝试RtAudio和libsndfile。
任务似乎很简单:
1.)使用libsndfile(使用SndfileHandle类和readf())将一些样本读取到缓冲区中
2.)使用RtAudio播放样本
但是,出于某种原因,我得到的只是黑色和巨大的失真。。。我尝试将样本读取为16位PCM(使用短缓冲区),并使用RTAUDIO_SINT16格式进行回放。没有成功。。。事实上,我试过很多不同的组合,只有噪音。。。
我最接近实际播放的是使用int16_t作为缓冲区和RTAUDIO_SINT16。看起来合乎逻辑,对吧?不管怎么说,这仍然像是在用一台破碎机。尝试了JACK和ALSA,以及缓冲区大小的许多不同组合,但都无济于事。。。
有没有关于如何将这两者结合起来的提示?
我使用静态缓冲区(只是为了实验,当然不会在最终产品中这样做…)会有问题吗?
好的,在这里回答我自己的问题,这只是一些参数之间的混淆,主要是帧和样本。。。虽然一个样本只是一个样本,但一帧是所有通道的样本。。。
#include <iostream>
#include <sndfile.hh>
#include "RtAudio.h"
/*
* Audio-Wiedergabe mit RtAudio und libsndfile !
*/
// Call
int fplay( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
double streamTime, RtAudioStreamStatus status, void *userData )
{
int16_t *buffer = (int16_t *) outputBuffer;
// ok, i know this is not the best way to do file i/o in the audio thread, but
// this is just for demonstration purposes ...
SndfileHandle *sndfile = reinterpret_cast<SndfileHandle*>(userData);
// Error handling !
if ( status ){
std::cout << "Stream underflow detected!" << std::endl;
}
// 'readf()' liest frames
// 'read()' liest einzelne Samples !
// ACHTUNG! Frames != Samples
// ein Frame = Samples für alle Kanäle
// d.h. |Samples| = Kanäle * Frames !
sndfile->readf(buffer, nBufferFrames);
return 0;
}
int main () {
// Damit das Programm funktioniert, muss eine 16Bit PCM Wave-Datei im
// gleichen Ordner liegen !
const char * fname = "test.wav" ;
// Soundfile-Handle aus der libsndfile-Bibliothek
SndfileHandle file = SndfileHandle (fname) ;
// Alle möglichen Infos über die Audio-Datei ausgeben !
std::cout << "Reading file: " << fname << std::endl;
std::cout << "File format: " << file.format() << std::endl;
std::cout << "PCM 16 BIT: " << (SF_FORMAT_WAV | SF_FORMAT_PCM_16) << std::endl;
std::cout << "Samples in file: " << file.frames() << std::endl;
std::cout << "Samplerate " << file.samplerate() << std::endl;
std::cout << "Channels: " << file.channels() << std::endl;
// Die RtAudio-Klasse ist gleichermassen dac und adc, wird hier aber nur als dac verwendet !
RtAudio dac;
if ( dac.getDeviceCount() < 1 ) {
std::cout << "nNo audio devices found!n";
return 0;
}
// Output params ...
RtAudio::StreamParameters parameters;
parameters.deviceId = dac.getDefaultOutputDevice();
parameters.nChannels = 2;
parameters.firstChannel = 0;
unsigned int sampleRate = 44100;
// ACHTUNG! Frames != Samples
// ein Frame = Samples für alle Kanäle
// d.h. |Samples| = Kanäle*Frames !
unsigned int bufferFrames = 1024;
try {
dac.openStream( ¶meters, NULL, RTAUDIO_SINT16,
sampleRate, &bufferFrames, &fplay, (void *)&file);
dac.startStream();
}
catch ( RtAudioError& e ) {
e.printMessage();
return 0;
}
char input;
std::cout << "nPlaying ... press <enter> to quit.n";
std::cin.get( input );
try {
// Stop the stream
dac.stopStream();
}
catch (RtAudioError& e) {
e.printMessage();
}
if ( dac.isStreamOpen() ) dac.closeStream();
return 0 ;
}
相关文章:
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- 为什么C++逐位AND运算符在不同大小的操作数中表现为这样
- 为什么 Clang 不允许"and"作为函数名称?
- 位阵列上的快速AND运算
- 是否可以在 C++03 中定义'move-and-swap idiom'等效项
- BoostPython and CMake
- OpenSSL BIO and SSL_read
- Gurobi GRBModel and GRBmodel in C++
- std::visit and std::variant usage
- SHBrowseForFolder with BIF_BROWSEFORCOMPUTER and SHGetPathFr
- Directx12 and keystrokes
- different between int **arr =new int [ n]; and int a[i][j]?
- C++ getenv and setenv
- Inference pytorch C++ with alexnet and cv::imread image
- Visual Studio 2019 C++ and std::filesystem
- 保证逻辑 AND 表达式中的函数调用
- python ctypes and C++ pointers
- C++ const char with .begin() and .end()
- Threads with Classes and std::packaged_task
- Combining libsndfile and RtAudio?