rtaudio hello world
This commit is contained in:
@@ -1,2 +1,85 @@
|
||||
|
||||
#include "AudioEngine.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <numbers>
|
||||
|
||||
AudioEngine::AudioEngine() {
|
||||
|
||||
if(audioDevice_.getDeviceCount() < 1) {
|
||||
std::cout << "No audio devices found" << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
AudioEngine::~AudioEngine() {
|
||||
(void)stop();
|
||||
}
|
||||
|
||||
bool AudioEngine::start() {
|
||||
|
||||
// initialize the audio engine
|
||||
RtAudio::StreamParameters params;
|
||||
params.deviceId = audioDevice_.getDefaultOutputDevice();
|
||||
params.nChannels = channels_; // we're doing two duplicate channels for pseudo-mono
|
||||
params.firstChannel = 0;
|
||||
|
||||
RtAudio::StreamOptions options;
|
||||
options.flags = RTAUDIO_MINIMIZE_LATENCY;
|
||||
|
||||
RtAudioErrorType status = audioDevice_.openStream(¶ms, nullptr, RTAUDIO_FLOAT32, sampleRate_, &bufferFrames_, &AudioEngine::audioCallback, this, &options);
|
||||
if(status != RTAUDIO_NO_ERROR) {
|
||||
std::cout << "Error opening RtAudio stream" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
status = audioDevice_.startStream();
|
||||
if(status != RTAUDIO_NO_ERROR) {
|
||||
std::cout << "Error starting RtAudio stream" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// sanity check
|
||||
std::cout << "sample rate: " << sampleRate_ << " buffer frames: " << bufferFrames_ << std::endl;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
bool AudioEngine::stop() {
|
||||
|
||||
if(audioDevice_.isStreamRunning()) audioDevice_.stopStream();
|
||||
if(audioDevice_.isStreamOpen()) audioDevice_.closeStream();
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
int32_t AudioEngine::audioCallback(void* outputBuffer, void* inputBuffer, uint32_t nFrames, double, RtAudioStreamStatus status, void* userData) {
|
||||
|
||||
// error if the callback is late
|
||||
if(status) std::cout << "stream underflow" << std::endl;
|
||||
|
||||
return static_cast<AudioEngine*>(userData)->process(static_cast<float*>(outputBuffer), static_cast<size_t>(nFrames));
|
||||
|
||||
}
|
||||
|
||||
int32_t AudioEngine::process(float* out, size_t nFrames) {
|
||||
|
||||
for(size_t i = 0; i < nFrames; i++) {
|
||||
|
||||
// simulate a sine wave
|
||||
phase_ += 0.04f;
|
||||
const float twoPi = 2.0f*std::numbers::pi_v<float>;
|
||||
if(phase_ > twoPi) {
|
||||
phase_ -= twoPi;
|
||||
}
|
||||
float outSample = std::sin(phase_) / 4.0f;
|
||||
|
||||
out[2*i] = outSample;
|
||||
out[2*i+1] = outSample;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user