feat: play music through librubberband

This commit is contained in:
2026-03-20 23:17:34 +01:00
parent 5b26203533
commit b14ea02694
8 changed files with 314 additions and 7 deletions

View File

@@ -6,7 +6,10 @@
#define LOCKSTEP_MIXINGPLAYER_H
#include <oboe/Oboe.h>
#include <math.h>
#include <cmath>
#include <atomic>
#include "AudioCallback.h"
using namespace oboe;
/**
@@ -20,7 +23,7 @@ protected:
std::atomic<int> startBeat;
int numBeatsPlaying;
public:
explicit MixingPlayer(std::vector<float> beatSound) : beatSound(beatSound), startBeat(0), numBeatsPlaying(0) {}
explicit MixingPlayer(std::vector<float> beatSound) : beatSound(beatSound), startBeat(0), numBeatsPlaying(0), mHaveMusic(false) {}
virtual ~MixingPlayer() = default;
@@ -63,6 +66,12 @@ public:
}
}
void setMusic(std::shared_ptr<AudioCallbackProvider> cb) {
std::lock_guard<std::mutex> lock(mLock);
mMusic = std::move(cb);
mHaveMusic.store((bool) mMusic);
}
oboe::DataCallbackResult onAudioReady(oboe::AudioStream *oboeStream, void *audioData, int32_t numFrames) override {
// fetch startBeat register
int isStartBeat = startBeat.load();
@@ -78,7 +87,7 @@ public:
int N = (int) beatSound.size();
for (int i = 0; i < numFrames; i++) {
float sample = 0.0;
float norm = (float) numBeatsPlaying;
float norm = (float) (std::max(numBeatsPlaying, 1));
for (int k = 0; k < numBeatsPlaying; k++) {
sample += beatSound[beatIdx[k]];
beatIdx[k] += 1;
@@ -102,12 +111,18 @@ public:
}
}
if(mHaveMusic.load()) {
mMusic->onAudioReady(floatData, numFrames);
}
return oboe::DataCallbackResult::Continue;
}
private:
std::mutex mLock;
std::shared_ptr<oboe::AudioStream> mStream;
std::shared_ptr<AudioCallbackProvider> mMusic;
std::atomic<bool> mHaveMusic;
// Stream params
static int constexpr kChannelCount = 2;