Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/UI/BackgroundAudio.h
3185 views
1
#pragma once
2
3
#include <atomic>
4
#include <mutex>
5
#include <string>
6
#include <vector>
7
8
#include "Common/File/Path.h"
9
#include "Common/UI/Root.h"
10
11
class AT3PlusReader;
12
13
struct Sample {
14
// data must be new[]-ed.
15
Sample(int16_t *data, int channels, int length, int rateInHz) : channels_(channels), data_(data), length_(length), rateInHz_(rateInHz) {}
16
~Sample() {
17
delete[] data_;
18
}
19
int16_t *data_;
20
int length_; // stereo or mono samples.
21
int rateInHz_; // sampleRate
22
int channels_;
23
24
static Sample *Load(const std::string &path);
25
};
26
27
// Mixer for things played on top of everything.
28
class SoundEffectMixer {
29
public:
30
void Init();
31
void Mix(int16_t *buffer, int sz, int sampleRateHz);
32
void Play(UI::UISound sfx, float volume);
33
34
void UpdateSample(UI::UISound sound, Sample *sample);
35
void LoadDefaultSample(UI::UISound sound);
36
37
std::vector<std::unique_ptr<Sample>> samples_;
38
39
struct PlayInstance {
40
UI::UISound sound;
41
int64_t offset; // 32.32 fixed point
42
int volume; // 0..255
43
bool done;
44
};
45
46
// This can be called on a thread.
47
void LoadSamplesOnThread();
48
private:
49
std::mutex mutex_;
50
std::vector<PlayInstance> queue_;
51
std::vector<PlayInstance> plays_;
52
};
53
54
class BackgroundAudio {
55
public:
56
BackgroundAudio();
57
~BackgroundAudio();
58
59
void SetGame(const Path &path);
60
void Update();
61
bool Play();
62
63
SoundEffectMixer &SFX() {
64
return sfxMixer_;
65
}
66
67
private:
68
void Clear(bool hard);
69
70
enum {
71
// 0.5 ms buffer at 44.1 khz should be enough.
72
BUFSIZE = 22050,
73
};
74
75
std::mutex mutex_;
76
Path bgGamePath_;
77
std::atomic<bool> sndLoadPending_;
78
int playbackOffset_ = 0;
79
AT3PlusReader *at3Reader_ = nullptr;
80
double gameLastChanged_ = 0.0;
81
double lastPlaybackTime_ = 0.0;
82
int *buffer_ = nullptr;
83
bool fadingOut_ = true;
84
float volumeFader_ = 0.0f;
85
float delta_ = -0.0001f;
86
SoundEffectMixer sfxMixer_;
87
};
88
89
extern BackgroundAudio g_BackgroundAudio;
90
91