Combining libsndfile and RtAudio?

Combining libsndfile and RtAudio?

本文关键字:RtAudio and libsndfile Combining      更新时间:2023-10-16

所以,我一直在尝试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( &parameters, 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 ;
}