Files
lockstep/app/src/main/cpp/PlaybackEngine.h

72 lines
2.3 KiB
C++

//
// Created by david on 01.02.2026.
//
#ifndef LOCKSTEP_PLAYBACKENGINE_H
#define LOCKSTEP_PLAYBACKENGINE_H
#include "StepListener.h"
#include "MixingPlayer.h"
#include "RubberBandStretcher.h"
#include "mp3file.h"
#include "AudioCallback.h"
#include <string>
#include <thread>
#include <memory>
#include <atomic>
/** Provides music through a regular callback to oboe. Called from separate oboe thread. */
class MusicProvider : public AudioCallbackProvider {
public:
explicit MusicProvider(RubberBand::RubberBandStretcher *stretcher, size_t buf_size_samples, int num_ch_out, std::atomic<int> *back_pressure);
~MusicProvider() override;
/** Called from separate oboe thread. */
void onAudioReady(float *data, int32_t frames) override;
private:
RubberBand::RubberBandStretcher *stretcher;
float *buf;
float **buf_ptr;
int idebug;
size_t buf_size_samples;
int num_ch_out;
/** contains the current available() frames from 'stretcher' in the audio callback thread 2 (oboe) */
std::atomic<int> *back_pressure;
};
class PlaybackEngine : public StepListener {
public:
PlaybackEngine(std::string filesDir, int resid);
virtual ~PlaybackEngine();
/** Play a beat sound. */
virtual void playBeat();
void playMusic(int fd);
private:
RubberBand::RubberBandStretcher stretcher;
MixingPlayer *mPlayer;
std::string mFilesDir;
std::unique_ptr<MP3File> musicFile;
std::atomic<bool> haveMusicFile;
std::unique_ptr<std::thread> musicFeed;
std::atomic<bool> exitMusicFeedThread;
/** where musicFeedThread() keeps track of the fact that we have music set -- will start the audio cb */
std::atomic<bool> isSetMusic;
int android_fd;
std::atomic<bool> haveTimeRatio;
std::atomic<double> timeRatio;
std::atomic<int> playbackRate;
std::atomic<int> numOutChannels;
std::atomic<int> numInChannels;
/** contains the current available() frames from 'stretcher' in the audio callback thread 2 (oboe) */
std::atomic<int> back_pressure;
/** this is actually in frames, not samples */
static size_t constexpr buf_size_samples = 1024;
void initRubberBand();
void closeRubberBand();
void closeMusicFile();
void musicFeedThread();
void mapChannels(int *channel_map, int num_ch_in, int num_ch_out);
};
#endif //LOCKSTEP_PLAYBACKENGINE_H