2014-06-22 15:02:04 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-02-16 02:04:11 +00:00
|
|
|
#include <atomic>
|
2020-08-02 17:56:57 +00:00
|
|
|
#include <mutex>
|
2021-02-16 02:04:11 +00:00
|
|
|
#include <string>
|
2020-08-02 19:55:46 +00:00
|
|
|
#include <vector>
|
2014-06-22 15:02:04 +00:00
|
|
|
|
2021-05-05 23:31:38 +00:00
|
|
|
#include "Common/File/Path.h"
|
2020-10-04 18:48:47 +00:00
|
|
|
#include "Common/UI/Root.h"
|
2020-08-02 17:44:48 +00:00
|
|
|
|
2020-08-03 09:29:54 +00:00
|
|
|
class AT3PlusReader;
|
2020-08-02 19:55:46 +00:00
|
|
|
|
2020-08-02 17:44:48 +00:00
|
|
|
class BackgroundAudio {
|
|
|
|
public:
|
2020-08-02 17:56:57 +00:00
|
|
|
BackgroundAudio();
|
|
|
|
~BackgroundAudio();
|
|
|
|
|
2021-05-05 23:31:38 +00:00
|
|
|
void SetGame(const Path &path);
|
2020-08-02 17:44:48 +00:00
|
|
|
void Update();
|
|
|
|
int Play();
|
2020-08-02 19:55:46 +00:00
|
|
|
|
|
|
|
void LoadSamples();
|
2020-08-03 09:29:54 +00:00
|
|
|
void PlaySFX(UI::UISound sfx);
|
2020-08-02 19:55:46 +00:00
|
|
|
|
2020-08-02 17:44:48 +00:00
|
|
|
private:
|
2021-02-16 02:04:11 +00:00
|
|
|
void Clear(bool hard);
|
|
|
|
|
2020-08-02 17:56:57 +00:00
|
|
|
enum {
|
|
|
|
BUFSIZE = 44100,
|
|
|
|
};
|
|
|
|
|
2020-08-03 09:29:54 +00:00
|
|
|
std::mutex mutex_;
|
2021-05-05 23:31:38 +00:00
|
|
|
Path bgGamePath_;
|
2021-02-16 02:04:11 +00:00
|
|
|
std::atomic<bool> sndLoadPending_;
|
2020-08-03 09:29:54 +00:00
|
|
|
int playbackOffset_ = 0;
|
|
|
|
AT3PlusReader *at3Reader_;
|
|
|
|
double gameLastChanged_ = 0.0;
|
|
|
|
double lastPlaybackTime_ = 0.0;
|
2020-08-02 17:56:57 +00:00
|
|
|
int *buffer = nullptr;
|
2020-08-03 09:29:54 +00:00
|
|
|
bool fadingOut_ = true;
|
|
|
|
float volume_ = 0.0f;
|
|
|
|
float delta_ = -0.0001f;
|
2020-08-02 19:55:46 +00:00
|
|
|
|
|
|
|
struct PlayInstance {
|
2020-08-03 09:29:54 +00:00
|
|
|
UI::UISound sound;
|
2020-08-02 19:55:46 +00:00
|
|
|
int offset;
|
2020-08-02 21:02:30 +00:00
|
|
|
int volume; // 0..255
|
|
|
|
bool done;
|
2020-08-02 19:55:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Sample {
|
|
|
|
// data must be new-ed.
|
|
|
|
Sample(int16_t *data, int length) : data_(data), length_(length) {}
|
|
|
|
~Sample() {
|
|
|
|
delete[] data_;
|
|
|
|
}
|
|
|
|
int16_t *data_;
|
|
|
|
int length_; // stereo samples.
|
|
|
|
};
|
|
|
|
|
|
|
|
static Sample *LoadSample(const std::string &path);
|
|
|
|
|
|
|
|
std::vector<PlayInstance> plays_;
|
|
|
|
std::vector<std::unique_ptr<Sample>> samples_;
|
2020-08-02 17:44:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern BackgroundAudio g_BackgroundAudio;
|