// // 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 #include #include #include /** 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 *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 *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 musicFile; std::atomic haveMusicFile; std::unique_ptr musicFeed; std::atomic exitMusicFeedThread; /** where musicFeedThread() keeps track of the fact that we have music set -- will start the audio cb */ std::atomic isSetMusic; int android_fd; std::atomic haveTimeRatio; std::atomic timeRatio; std::atomic playbackRate; std::atomic numOutChannels; std::atomic numInChannels; /** contains the current available() frames from 'stretcher' in the audio callback thread 2 (oboe) */ std::atomic 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