Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Rubberduckycooly
GitHub Repository: Rubberduckycooly/RSDKv5-Decompilation
Path: blob/master/RSDKv5/RSDK/Audio/Audio.hpp
1168 views
1
#ifndef AUDIO_H
2
#define AUDIO_H
3
4
namespace RSDK
5
{
6
7
#define SFX_COUNT (0x100)
8
#define CHANNEL_COUNT (0x10)
9
10
#define MIX_BUFFER_SIZE (0x800)
11
#define SAMPLE_FORMAT float
12
13
#define AUDIO_FREQUENCY (44100)
14
#define AUDIO_CHANNELS (2)
15
16
struct SFXInfo {
17
RETRO_HASH_MD5(hash);
18
float *buffer;
19
size_t length;
20
int32 playCount;
21
uint8 maxConcurrentPlays;
22
uint8 scope;
23
};
24
25
struct ChannelInfo {
26
float *samplePtr;
27
float pan;
28
float volume;
29
int32 speed;
30
size_t sampleLength;
31
int32 bufferPos;
32
int32 playIndex;
33
uint32 loop;
34
int16 soundID;
35
uint8 priority;
36
uint8 state;
37
};
38
39
enum ChannelStates { CHANNEL_IDLE, CHANNEL_SFX, CHANNEL_STREAM, CHANNEL_LOADING_STREAM, CHANNEL_PAUSED = 0x40 };
40
41
extern SFXInfo sfxList[SFX_COUNT];
42
extern ChannelInfo channels[CHANNEL_COUNT];
43
44
class AudioDeviceBase
45
{
46
public:
47
static bool32 Init();
48
static void Release();
49
50
static void ProcessAudioMixing(void *stream, int32 length);
51
52
static void FrameInit();
53
54
static void HandleStreamLoad(ChannelInfo *channel, bool32 async);
55
56
static uint8 initializedAudioChannels;
57
static uint8 audioState;
58
static uint8 audioFocus;
59
60
protected:
61
static void InitAudioChannels();
62
};
63
64
void UpdateStreamBuffer(ChannelInfo *channel);
65
void LoadStream(ChannelInfo *channel);
66
int32 PlayStream(const char *filename, uint32 slot, uint32 startPos, uint32 loopPoint, bool32 loadASync);
67
68
void LoadSfxToSlot(char *filename, uint8 slot, uint8 plays, uint8 scope);
69
void LoadSfx(char *filePath, uint8 plays, uint8 scope);
70
71
} // namespace RSDK
72
73
#if RETRO_AUDIODEVICE_XAUDIO
74
#include "XAudio/XAudioDevice.hpp"
75
#elif RETRO_AUDIODEVICE_PORT
76
#include "PortAudio/PortAudioDevice.hpp"
77
#elif RETRO_AUDIODEVICE_MINI
78
#include "MiniAudio/MiniAudioDevice.hpp"
79
#elif RETRO_AUDIODEVICE_SDL2
80
#include "SDL2/SDL2AudioDevice.hpp"
81
#elif RETRO_AUDIODEVICE_OBOE
82
#include "Oboe/OboeAudioDevice.hpp"
83
#endif
84
85
namespace RSDK
86
{
87
88
inline uint16 GetSfx(const char *sfxName)
89
{
90
RETRO_HASH_MD5(hash);
91
GEN_HASH_MD5(sfxName, hash);
92
93
for (int32 s = 0; s < SFX_COUNT; ++s) {
94
if (HASH_MATCH_MD5(sfxList[s].hash, hash))
95
return s;
96
}
97
98
return -1;
99
}
100
int32 PlaySfx(uint16 sfx, uint32 loopPoint, uint32 priority);
101
inline void StopSfx(uint16 sfx)
102
{
103
#if !RETRO_USE_ORIGINAL_CODE
104
LockAudioDevice();
105
#endif
106
107
for (int32 i = 0; i < CHANNEL_COUNT; ++i) {
108
if (channels[i].soundID == sfx) {
109
MEM_ZERO(channels[i]);
110
channels[i].soundID = -1;
111
channels[i].state = CHANNEL_IDLE;
112
}
113
}
114
115
#if !RETRO_USE_ORIGINAL_CODE
116
UnlockAudioDevice();
117
#endif
118
}
119
120
#if RETRO_REV0U
121
inline void StopAllSfx()
122
{
123
#if !RETRO_USE_ORIGINAL_CODE
124
LockAudioDevice();
125
#endif
126
127
for (int32 i = 0; i < CHANNEL_COUNT; ++i) {
128
if (channels[i].state == CHANNEL_SFX) {
129
MEM_ZERO(channels[i]);
130
channels[i].soundID = -1;
131
channels[i].state = CHANNEL_IDLE;
132
}
133
}
134
135
#if !RETRO_USE_ORIGINAL_CODE
136
UnlockAudioDevice();
137
#endif
138
}
139
#endif
140
141
void SetChannelAttributes(uint8 channel, float volume, float panning, float speed);
142
143
inline void StopChannel(uint32 channel)
144
{
145
if (channel < CHANNEL_COUNT) {
146
if (channels[channel].state != CHANNEL_LOADING_STREAM)
147
channels[channel].state = CHANNEL_IDLE;
148
}
149
}
150
151
inline void PauseChannel(uint32 channel)
152
{
153
if (channel < CHANNEL_COUNT) {
154
if (channels[channel].state != CHANNEL_LOADING_STREAM)
155
channels[channel].state |= CHANNEL_PAUSED;
156
}
157
}
158
159
inline void ResumeChannel(uint32 channel)
160
{
161
if (channel < CHANNEL_COUNT) {
162
if (channels[channel].state != CHANNEL_LOADING_STREAM)
163
channels[channel].state &= ~CHANNEL_PAUSED;
164
}
165
}
166
167
inline void PauseSound()
168
{
169
for (int32 c = 0; c < CHANNEL_COUNT; ++c) PauseChannel(c);
170
}
171
172
inline void ResumeSound()
173
{
174
for (int32 c = 0; c < CHANNEL_COUNT; ++c) ResumeChannel(c);
175
}
176
177
inline bool32 SfxPlaying(uint16 sfx)
178
{
179
for (int32 c = 0; c < CHANNEL_COUNT; ++c) {
180
if (channels[c].state == CHANNEL_SFX && channels[c].soundID == sfx)
181
return true;
182
}
183
return false;
184
}
185
186
inline bool32 ChannelActive(uint32 channel)
187
{
188
if (channel >= CHANNEL_COUNT)
189
return false;
190
else
191
return (channels[channel].state & 0x3F) != CHANNEL_IDLE;
192
}
193
194
uint32 GetChannelPos(uint32 channel);
195
double GetVideoStreamPos();
196
197
void ClearStageSfx();
198
#if RETRO_USE_MOD_LOADER
199
void ClearGlobalSfx();
200
#endif
201
202
#if RETRO_REV0U
203
#include "Legacy/AudioLegacy.hpp"
204
#endif
205
206
} // namespace RSDK
207
208
#endif
209
210