1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
#include "HMAS.h"
#define MINIAUDIO_IMPLEMENTATION
#ifdef __SWITCH__
#define MA_NO_RUNTIME_LINKING
#endif
#include "audio/miniaudio.h"
#include <spdlog/spdlog.h>
#include "port/Engine.h"
#include "sounds.h"
HMAS::HMAS() {
ma_result result;
ma_engine_config engine = ma_engine_config_init();
engine.channels = 2;
engine.noDevice = MA_TRUE;
engine.sampleRate = GameEngine_GetSampleRate();
result = ma_engine_init(&engine, &gAudioEngine);
if (result != MA_SUCCESS) {
SPDLOG_ERROR("Failed to initialize audio engine: {}", ma_result_description(result));
return;
}
}
void HMAS::RegisterSound(HMAS_AudioId id, const std::string& filePath, HMAS_Info info) {
if (gRegistry.find(id) != gRegistry.end()) {
SPDLOG_WARN("Sound with ID {} already registered", static_cast<int>(id));
return;
}
ma_result result = ma_sound_init_from_file(&gAudioEngine, filePath.c_str(), 0, NULL, NULL, &gRegistry[id].sound);
if (result != MA_SUCCESS) {
SPDLOG_ERROR("Failed to load sound from file {}: {}", filePath, ma_result_description(result));
return;
}
if(info.loop.start != -1 && info.loop.end != -1) {
ma_data_source* source = ma_sound_get_data_source(&gRegistry[id].sound);
ma_data_source_set_loop_point_in_pcm_frames(source, info.loop.start, info.loop.end);
}
gRegistry[id].info = info;
SPDLOG_INFO("Sound with ID {} registered from file {}", static_cast<int>(id), filePath);
}
void HMAS::RegisterSound(HMAS_AudioId id, uint8_t* data, uint32_t size, HMAS_Info info) {
if (gRegistry.find(id) != gRegistry.end()) {
SPDLOG_WARN("Sound with ID {} already registered", static_cast<int>(id));
return;
}
ma_decoder_config config = ma_decoder_config_init(ma_format_f32, ma_engine_get_channels(&gAudioEngine), ma_engine_get_sample_rate(&gAudioEngine));
ma_result result = ma_decoder_init_memory(data, size, &config, &gRegistry[id].decoder);
if (result != MA_SUCCESS) {
SPDLOG_ERROR("Failed to initialize decoder from memory: {}", ma_result_description(result));
return;
}
if(info.loop.start != -1 && info.loop.end != -1) {
ma_data_source_set_loop_point_in_pcm_frames(&gRegistry[id].decoder, info.loop.start, info.loop.end);
}
result = ma_sound_init_from_data_source(&gAudioEngine, &gRegistry[id].decoder, 0, NULL, &gRegistry[id].sound);
if (result != MA_SUCCESS) {
SPDLOG_ERROR("Failed to load sound from memory: {}", ma_result_description(result));
return;
}
gRegistry[id].info = info;
SPDLOG_INFO("Sound with ID {} registered from memory buffer", static_cast<int>(id));
}
void HMAS::Play(HMAS_ChannelId channelId, HMAS_AudioId id, bool loop) {
if(channelId == HMAS_ChannelId::HMAS_MUSIC){
this->Stop(channelId);
}
auto& sample = gRegistry[id];
float pitch = 1.0f;
float volume = channelId == HMAS_ChannelId::HMAS_MUSIC ? 0.9f : 1.0f;
ma_sound_set_pitch(&sample.sound, pitch);
ma_sound_set_volume(&sample.sound, volume);
ma_sound_set_looping(&sample.sound, loop);
ma_sound_seek_to_pcm_frame(&sample.sound, 0);
ma_result result = ma_sound_start(&sample.sound);
if (result != MA_SUCCESS) {
SPDLOG_ERROR("Failed to start sound: {}", ma_result_description(result));
return;
}
auto channel = &this->gChannelSound[channelId];
channel->sound = &sample.sound;
channel->cursor = 0;
channel->pitch = pitch;
channel->volume = volume;
}
void HMAS::Stop(HMAS_ChannelId channelId) {
auto channel = &this->gChannelSound[channelId];
if (channel->sound == nullptr) {
return;
}
ma_sound_stop(channel->sound);
channel->sound = nullptr;
channel->cursor = 0;
channel->pitch = 1.0f;
channel->volume = 1.0f;
}
bool HMAS::IsPlaying(HMAS_ChannelId channelId) {
auto channel = &this->gChannelSound[channelId];
if (channel->sound == nullptr) {
return false;
}
return ma_sound_is_playing(channel->sound);
}
void HMAS::SetPitch(HMAS_ChannelId channelId, float pitch) {
auto channel = &this->gChannelSound[channelId];
if (channel->sound == nullptr) {
return;
}
ma_sound_set_pitch(channel->sound, pitch);
channel->pitch = pitch;
}
void HMAS::SetVolume(HMAS_ChannelId channelId, float volume) {
auto channel = &this->gChannelSound[channelId];
if (channel->sound == nullptr) {
return;
}
ma_sound_set_volume(channel->sound, volume);
channel->volume = volume;
}
void HMAS::SetPause(HMAS_ChannelId channelId, bool pause) {
auto channel = &this->gChannelSound[channelId];
if (channel->sound == nullptr) {
return;
}
if(pause) {
ma_sound_get_cursor_in_pcm_frames(channel->sound, &channel->cursor);
ma_sound_stop(channel->sound);
} else {
ma_result result = ma_sound_start(channel->sound);
if (result != MA_SUCCESS) {
SPDLOG_ERROR("Failed to resume sound: {}", ma_result_description(result));
return;
}
ma_sound_seek_to_pcm_frame(channel->sound, channel->cursor);
}
}
void HMAS::AddEffect(HMAS_ChannelId channelId, HMAS_EffectType type, HMAS_EffectTransition transition, uint32_t frames, float target) {
auto& channel = gChannelSound[channelId];
channel.effects.push_back({type, transition, frames, target});
}
bool HMAS::IsIDRegistered(HMAS_AudioId id) {
return gRegistry.find(id) != gRegistry.end();
}
void HMAS::ProcessEffects() {
for (size_t i = 0; i < sizeof(gChannelSound) / sizeof(gChannelSound[0]); i++){
auto& channel = gChannelSound[i];
if (channel.sound == nullptr) {
continue;
}
if (channel.effects.empty()) {
continue;
}
auto& effect = channel.effects.front();
if (effect.numFrames > 0) {
effect.numFrames--;
switch (effect.type) {
case HMAS_EffectType::HMAS_EFFECT_VOLUME: {
float volume = effect.transition == HMAS_EffectTransition::HMAS_LINEAR ?
Lerp(channel.volume, effect.target, 1.0f / effect.numFrames) : effect.target;
this->SetVolume((HMAS_ChannelId) i, std::max(0.0f, volume));
break;
}
case HMAS_EffectType::HMAS_EFFECT_PITCH: {
float pitch = effect.transition == HMAS_EffectTransition::HMAS_LINEAR ?
Lerp(channel.pitch, effect.target, 1.0f / effect.numFrames) : effect.target;
this->SetPitch((HMAS_ChannelId) i, std::max(0.1f, pitch));
break;
}
case HMAS_EffectType::HMAS_EFFECT_PAUSE: {
this->SetPause((HMAS_ChannelId) i, effect.target < 0.5f);
break;
}
case HMAS_EffectType::HMAS_EFFECT_STOP: {
this->Stop((HMAS_ChannelId) i);
break;
}
default:
SPDLOG_WARN("Unknown effect type: {}", static_cast<int>(effect.type));
break;
}
} else {
SPDLOG_DEBUG("Removing effect: Type={}, Frames={}, Target={}", static_cast<int>(effect.type), effect.numFrames, effect.target);
channel.effects.erase(channel.effects.begin());
}
}
}
void HMAS::CreateBuffer(uint8_t *samples, uint32_t bufferSizeInBytes) {
this->ProcessEffects();
ma_uint32 bufferSizeInFrames = bufferSizeInBytes / ma_get_bytes_per_frame(ma_format_f32, ma_engine_get_channels(&gAudioEngine));
ma_engine_read_pcm_frames(&gAudioEngine, samples, bufferSizeInFrames, NULL);
}
HMAS::~HMAS() {
for (auto& pair : gRegistry) {
ma_sound_uninit(&pair.second.sound);
}
gRegistry.clear();
ma_engine_uninit(&gAudioEngine);
}
// Expose C API for HMAS
extern "C" void HMAS_Play(HMAS_ChannelId channelId, HMAS_AudioId id, bool loop) {
GameEngine::Instance->gHMAS->Play(channelId, id, loop);
}
extern "C" void HMAS_Stop(HMAS_ChannelId channelId) {
GameEngine::Instance->gHMAS->Stop(channelId);
}
extern "C" bool HMAS_IsPlaying(HMAS_ChannelId channelId) {
return GameEngine::Instance->gHMAS->IsPlaying(channelId);
}
extern "C" void HMAS_SetPitch(HMAS_ChannelId channelId, float pitch) {
GameEngine::Instance->gHMAS->SetPitch(channelId, pitch);
}
extern "C" void HMAS_SetVolume(HMAS_ChannelId channelId, float volume) {
GameEngine::Instance->gHMAS->SetVolume(channelId, volume);
}
extern "C" void HMAS_SetPause(HMAS_ChannelId channelId, bool pause) {
GameEngine::Instance->gHMAS->SetPause(channelId, pause);
}
extern "C" void HMAS_AddEffect(HMAS_ChannelId channelId, HMAS_EffectType type, HMAS_EffectTransition transition, uint32_t frames, float target) {
GameEngine::Instance->gHMAS->AddEffect(channelId, type, transition, frames, target);
}
extern "C" bool HMAS_IsIDRegistered(HMAS_AudioId id) {
return GameEngine::Instance->gHMAS->IsIDRegistered(id);
}
|