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

72 lines
2.3 KiB
C
Raw Normal View History

2026-02-01 02:54:05 +01:00
//
// Created by david on 01.02.2026.
//
#ifndef LOCKSTEP_PLAYBACKENGINE_H
#define LOCKSTEP_PLAYBACKENGINE_H
2026-03-04 01:31:35 +01:00
#include "StepListener.h"
#include "MixingPlayer.h"
2026-03-20 23:17:34 +01:00
#include "RubberBandStretcher.h"
#include "mp3file.h"
#include "AudioCallback.h"
2026-03-03 12:04:17 +01:00
#include <string>
2026-03-20 23:17:34 +01:00
#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);
2026-03-20 23:17:34 +01:00
~MusicProvider() override;
/** Called from separate oboe thread. */
void onAudioReady(float *data, int32_t frames) override;
private:
RubberBand::RubberBandStretcher *stretcher;
float *buf;
2026-03-22 08:44:41 +01:00
float **buf_ptr;
2026-03-20 23:17:34 +01:00
int idebug;
2026-03-22 07:49:05 +01:00
size_t buf_size_samples;
2026-03-22 08:44:41 +01:00
int num_ch_out;
/** contains the current available() frames from 'stretcher' in the audio callback thread 2 (oboe) */
std::atomic<int> *back_pressure;
2026-03-20 23:17:34 +01:00
};
2026-02-01 02:54:05 +01:00
2026-03-04 01:31:35 +01:00
class PlaybackEngine : public StepListener {
2026-02-01 02:54:05 +01:00
public:
PlaybackEngine(std::string filesDir, int resid);
2026-02-01 02:54:05 +01:00
virtual ~PlaybackEngine();
2026-03-04 01:31:35 +01:00
/** Play a beat sound. */
virtual void playBeat();
2026-03-19 19:17:57 +01:00
void playMusic(int fd);
2026-02-01 02:54:05 +01:00
private:
2026-03-20 23:17:34 +01:00
RubberBand::RubberBandStretcher stretcher;
MixingPlayer *mPlayer;
2026-03-03 12:04:17 +01:00
std::string mFilesDir;
2026-03-20 23:17:34 +01:00
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;
2026-03-20 23:17:34 +01:00
int android_fd;
2026-03-22 07:37:27 +01:00
std::atomic<bool> haveTimeRatio;
2026-03-22 08:44:41 +01:00
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;
2026-03-22 08:44:41 +01:00
/** this is actually in frames, not samples */
2026-03-22 07:49:05 +01:00
static size_t constexpr buf_size_samples = 1024;
2026-03-20 23:17:34 +01:00
void initRubberBand();
void closeRubberBand();
void closeMusicFile();
void musicFeedThread();
2026-03-22 08:44:41 +01:00
void mapChannels(int *channel_map, int num_ch_in, int num_ch_out);
2026-02-01 02:54:05 +01:00
};
#endif //LOCKSTEP_PLAYBACKENGINE_H