CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/UI/BackgroundAudio.h
Views: 1401
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 LoadSamples();
31
32
void Mix(int16_t *buffer, int sz, int sampleRateHz);
33
void Play(UI::UISound sfx, float volume);
34
35
void UpdateSample(UI::UISound sound, Sample *sample);
36
void LoadDefaultSample(UI::UISound sound);
37
38
std::vector<std::unique_ptr<Sample>> samples_;
39
40
struct PlayInstance {
41
UI::UISound sound;
42
int64_t offset; // 32.32 fixed point
43
int volume; // 0..255
44
bool done;
45
};
46
47
private:
48
std::mutex mutex_;
49
std::vector<PlayInstance> queue_;
50
std::vector<PlayInstance> plays_;
51
};
52
53
class BackgroundAudio {
54
public:
55
BackgroundAudio();
56
~BackgroundAudio();
57
58
void SetGame(const Path &path);
59
void Update();
60
bool Play();
61
62
SoundEffectMixer &SFX() {
63
return sfxMixer_;
64
}
65
66
private:
67
void Clear(bool hard);
68
69
enum {
70
// 0.5 ms buffer at 44.1 khz should be enough.
71
BUFSIZE = 22050,
72
};
73
74
std::mutex mutex_;
75
Path bgGamePath_;
76
std::atomic<bool> sndLoadPending_;
77
int playbackOffset_ = 0;
78
AT3PlusReader *at3Reader_ = nullptr;
79
double gameLastChanged_ = 0.0;
80
double lastPlaybackTime_ = 0.0;
81
int *buffer = nullptr;
82
bool fadingOut_ = true;
83
float volume_ = 0.0f;
84
float delta_ = -0.0001f;
85
SoundEffectMixer sfxMixer_;
86
};
87
88
extern BackgroundAudio g_BackgroundAudio;
89
90