So I’m just getting started with JUCE. Based on the Audioplayer-Tutorial I tried to write a simple CommandLine Program just playing a wav-File.
Everything works fine on my Mac, but when I try to play the same file on an ARM-based Debian (Cortex A53) Device, the Playback sounds scrambled, hardly recognizable. And it plays a little too fast, as if the sample rate was 48000 (which it is not, it is 44100) and JUCE tries to resample in transportSource.setSource-method. The sound-chip on the ARM-Device is set to 44100 as well.
I can’t see the issue. Does anyone else does? Or maybe there are some general advices in the JUCE-Part of the code for a newbie like me.
main
#include <iostream>
#include "WavPlayer.h"
int main (int argc, char* argv[]) {
if(argc != 2) {
std::cout << "Usage: " << argv[0] << " /absolute/path/to/wavefile.wav" << std::endl;
return -1;
}
std::unique_ptr<WavPlayer> player (new WavPlayer());
if(!player->play(argv[1])) {
std::cout << "WAV-File not found." << std::endl;
return -1;
}
while(!player->finished()) {
Time::waitForMillisecondCounter(Time::getMillisecondCounter() + 1000);
std::cout << "Still playing" << std::endl;
}
return 0;
}
WavPlayer.h
#include <juce_core/juce_core.h>
#include <JuceHeader.h>
#ifndef HELLO_PHIL_WAVPLAYER_H
#define HELLO_PHIL_WAVPLAYER_H
class WavPlayer : public juce::AudioAppComponent {
public:
WavPlayer();
~WavPlayer() override;
bool play(const String &absolutePath);
bool finished();
void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override;
void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override;
void releaseResources() override;
private:
AudioFormatManager formatManager;
AudioTransportSource transportSource;
std::unique_ptr<AudioFormatReaderSource> readerSource;
};
#endif //HELLO_PHIL_WAVPLAYER_H
WavPlayer.cpp
#include "WavPlayer.h"
WavPlayer::WavPlayer() {
formatManager.registerBasicFormats() ;
setAudioChannels (0, 2);
}
WavPlayer::~WavPlayer() {
std::cout << "Fertig!" << std::endl;
releaseResources();
shutdownAudio();
}
bool WavPlayer::play(const String &absolutePath) {
std::cout << "Trying to play " << absolutePath << std::endl;
File file (absolutePath);
auto* reader = formatManager.createReaderFor (file);
if (reader != nullptr)
{
std::cout << "Sample Rate: " << reader->sampleRate << std::endl;
std::unique_ptr<juce::AudioFormatReaderSource> newSource (new juce::AudioFormatReaderSource (reader, true));
transportSource.setSource (newSource.get(),0, nullptr, reader->sampleRate);
readerSource.reset (newSource.release());
transportSource.start();
return true;
}
return false;
}
bool WavPlayer::finished() {
return transportSource.hasStreamFinished();
}
void WavPlayer::prepareToPlay (int samplesPerBlockExpected, double sampleRate) {
transportSource.prepareToPlay (samplesPerBlockExpected, sampleRate);
}
void WavPlayer::releaseResources() {
transportSource.releaseResources();
}
void WavPlayer::getNextAudioBlock (const juce::AudioSourceChannelInfo& bufferToFill) {
if (readerSource.get() == nullptr) {
bufferToFill.clearActiveBufferRegion();
return;
}
transportSource.getNextAudioBlock (bufferToFill);
}