simplify Sounds code to avoid global StringsBuffer

This commit is contained in:
UnknownShadow200 2021-08-08 13:09:02 +10:00
parent 233d8cb53c
commit d9662de0a8

View File

@ -33,7 +33,6 @@ void Audio_SetSounds(int volume) {
void Audio_PlayDigSound(cc_uint8 type) { } void Audio_PlayDigSound(cc_uint8 type) { }
void Audio_PlayStepSound(cc_uint8 type) { } void Audio_PlayStepSound(cc_uint8 type) { }
#else #else
static struct StringsBuffer files;
static const cc_string audio_dir = String_FromConst("audio"); static const cc_string audio_dir = String_FromConst("audio");
static void ApplyVolume(cc_int16* samples, int count, int volume) { static void ApplyVolume(cc_int16* samples, int count, int volume) {
@ -56,7 +55,7 @@ static void ApplyVolume(cc_int16* samples, int count, int volume) {
} }
} }
static int LoadVolume(const char* volKey, const char* boolKey) { static int GetVolume(const char* volKey, const char* boolKey) {
int volume = Options_GetInt(volKey, 0, 100, 0); int volume = Options_GetInt(volKey, 0, 100, 0);
if (volume) return volume; if (volume) return volume;
@ -674,11 +673,7 @@ struct SoundGroup {
int count; int count;
struct Sound sounds[AUDIO_MAX_SOUNDS]; struct Sound sounds[AUDIO_MAX_SOUNDS];
}; };
struct Soundboard { struct SoundGroup groups[SOUND_COUNT]; };
struct Soundboard {
RNGState rnd; cc_bool inited;
struct SoundGroup groups[SOUND_COUNT];
};
#define WAV_FourCC(a, b, c, d) (((cc_uint32)a << 24) | ((cc_uint32)b << 16) | ((cc_uint32)c << 8) | (cc_uint32)d) #define WAV_FourCC(a, b, c, d) (((cc_uint32)a << 24) | ((cc_uint32)b << 16) | ((cc_uint32)c << 8) | (cc_uint32)d)
#define WAV_FMT_SIZE 16 #define WAV_FMT_SIZE 16
@ -752,47 +747,43 @@ static struct SoundGroup* Soundboard_Find(struct Soundboard* board, const cc_str
return NULL; return NULL;
} }
static void Soundboard_Init(struct Soundboard* board, const cc_string* boardName) { static void Soundboard_Load(struct Soundboard* board, const cc_string* boardName, const cc_string* file) {
cc_string file, name;
struct SoundGroup* group; struct SoundGroup* group;
struct Sound* snd; struct Sound* snd;
cc_string name;
cc_result res; cc_result res;
int i, dotIndex; int dotIndex;
board->inited = true;
for (i = 0; i < files.count; i++) { /* dig_grass1.wav -> dig_grass1 */
file = StringsBuffer_UNSAFE_Get(&files, i); name = *file;
name = file; dotIndex = String_LastIndexOf(&name, '.');
if (dotIndex >= 0) name.length = dotIndex;
if (!String_CaselessStarts(&name, boardName)) return;
/* dig_grass1.wav -> dig_grass1 */ /* Convert dig_grass1 to grass */
dotIndex = String_LastIndexOf(&name, '.'); name = String_UNSAFE_SubstringAt(&name, boardName->length);
if (dotIndex >= 0) name.length = dotIndex; name = String_UNSAFE_Substring(&name, 0, name.length - 1);
if (!String_CaselessStarts(&name, boardName)) continue;
/* Convert dig_grass1 to grass */ group = Soundboard_Find(board, &name);
name = String_UNSAFE_SubstringAt(&name, boardName->length); if (!group) {
name = String_UNSAFE_Substring(&name, 0, name.length - 1); Chat_Add1("&cUnknown sound group '%s'", &name); return;
group = Soundboard_Find(board, &name);
if (!group) {
Chat_Add1("&cUnknown sound group '%s'", &name); continue;
}
if (group->count == Array_Elems(group->sounds)) {
Chat_AddRaw("&cCannot have more than 10 sounds in a group"); continue;
}
snd = &group->sounds[group->count];
res = Sound_ReadWave(&file, snd);
if (res) {
Logger_SysWarn2(res, "decoding", &file);
Mem_Free(snd->data);
snd->data = NULL;
snd->size = 0;
} else { group->count++; }
} }
if (group->count == Array_Elems(group->sounds)) {
Chat_AddRaw("&cCannot have more than 10 sounds in a group"); return;
}
snd = &group->sounds[group->count];
res = Sound_ReadWave(file, snd);
if (res) {
Logger_SysWarn2(res, "decoding", file);
Mem_Free(snd->data);
snd->data = NULL;
snd->size = 0;
} else { group->count++; }
} }
static RNGState sounds_rnd;
static const struct Sound* Soundboard_PickRandom(struct Soundboard* board, cc_uint8 type) { static const struct Sound* Soundboard_PickRandom(struct Soundboard* board, cc_uint8 type) {
struct SoundGroup* group; struct SoundGroup* group;
int idx; int idx;
@ -803,7 +794,7 @@ static const struct Sound* Soundboard_PickRandom(struct Soundboard* board, cc_ui
group = &board->groups[type]; group = &board->groups[type];
if (!group->count) return NULL; if (!group->count) return NULL;
idx = Random_Next(&board->rnd, group->count); idx = Random_Next(&sounds_rnd, group->count);
return &group->sounds[idx]; return &group->sounds[idx];
} }
@ -908,13 +899,21 @@ static void Sounds_FreeOutputs(struct AudioContext* outputs) {
} }
} }
static void Sounds_Start(void) { static void Sounds_LoadFile(const cc_string* path, void* obj) {
static const cc_string dig = String_FromConst("dig_"); static const cc_string dig = String_FromConst("dig_");
static const cc_string step = String_FromConst("step_"); static const cc_string step = String_FromConst("step_");
cc_string file = *path;
if (digBoard.inited || stepBoard.inited) return; Utils_UNSAFE_TrimFirstDirectory(&file);
Soundboard_Init(&digBoard, &dig); Soundboard_Load(&digBoard, &dig, &file);
Soundboard_Init(&stepBoard, &step); Soundboard_Load(&stepBoard, &step, &file);
}
static cc_bool sounds_loaded;
static void Sounds_Start(void) {
if (sounds_loaded) return;
sounds_loaded = true;
Directory_Enum(&audio_dir, NULL, Sounds_LoadFile);
} }
static void Sounds_Stop(void) { static void Sounds_Stop(void) {
@ -922,16 +921,8 @@ static void Sounds_Stop(void) {
Sounds_FreeOutputs(stereoOutputs); Sounds_FreeOutputs(stereoOutputs);
} }
static void Audio_FilesCallback(const cc_string* path, void* obj) {
cc_string relPath = *path;
Utils_UNSAFE_TrimFirstDirectory(&relPath);
StringsBuffer_Add(&files, &relPath);
}
static void Sounds_Init(void) { static void Sounds_Init(void) {
int volume; int volume = GetVolume(OPT_SOUND_VOLUME, OPT_USE_SOUND);
Directory_Enum(&audio_dir, NULL, Audio_FilesCallback);
volume = LoadVolume(OPT_SOUND_VOLUME, OPT_USE_SOUND);
Audio_SetSounds(volume); Audio_SetSounds(volume);
Event_Register_(&UserEvents.BlockChanged, NULL, Audio_PlayBlockSound); Event_Register_(&UserEvents.BlockChanged, NULL, Audio_PlayBlockSound);
} }
@ -1058,31 +1049,31 @@ cleanup:
} }
static void Music_AddFile(const cc_string* path, void* obj) { static void Music_AddFile(const cc_string* path, void* obj) {
struct StringsBuffer* FILES = (struct StringsBuffer*)obj; struct StringsBuffer* files = (struct StringsBuffer*)obj;
static const cc_string ogg = String_FromConst(".ogg"); static const cc_string ogg = String_FromConst(".ogg");
if (!String_CaselessEnds(path, &ogg)) return; if (!String_CaselessEnds(path, &ogg)) return;
StringsBuffer_Add(FILES, path); StringsBuffer_Add(files, path);
} }
static void Music_RunLoop(void) { static void Music_RunLoop(void) {
struct StringsBuffer FILES; struct StringsBuffer files;
cc_string path; cc_string path;
RNGState rnd; RNGState rnd;
struct Stream stream; struct Stream stream;
int idx, delay; int idx, delay;
cc_result res = 0; cc_result res = 0;
StringsBuffer_SetLengthBits(&FILES, STRINGSBUFFER_DEF_LEN_SHIFT); StringsBuffer_SetLengthBits(&files, STRINGSBUFFER_DEF_LEN_SHIFT);
StringsBuffer_Init(&FILES); StringsBuffer_Init(&files);
Directory_Enum(&audio_dir, &FILES, Music_AddFile); Directory_Enum(&audio_dir, &files, Music_AddFile);
Random_SeedFromCurrentTime(&rnd); Random_SeedFromCurrentTime(&rnd);
Audio_Init(&music_ctx, AUDIO_MAX_BUFFERS); Audio_Init(&music_ctx, AUDIO_MAX_BUFFERS);
while (!music_stopping && FILES.count) { while (!music_stopping && files.count) {
idx = Random_Next(&rnd, FILES.count); idx = Random_Next(&rnd, files.count);
path = StringsBuffer_UNSAFE_Get(&FILES, idx); path = StringsBuffer_UNSAFE_Get(&files, idx);
Platform_Log1("playing music file: %s", &path); Platform_Log1("playing music file: %s", &path);
res = Stream_OpenFile(&stream, &path); res = Stream_OpenFile(&stream, &path);
@ -1107,7 +1098,7 @@ static void Music_RunLoop(void) {
Audio_MusicVolume = 0; Audio_MusicVolume = 0;
} }
Audio_Close(&music_ctx); Audio_Close(&music_ctx);
StringsBuffer_Clear(&FILES); StringsBuffer_Clear(&files);
if (music_joining) return; if (music_joining) return;
Thread_Detach(music_thread); Thread_Detach(music_thread);
@ -1143,7 +1134,7 @@ static void Music_Init(void) {
music_maxDelay = Options_GetInt(OPT_MAX_MUSIC_DELAY, 0, 3600, 420) * MILLIS_PER_SEC; music_maxDelay = Options_GetInt(OPT_MAX_MUSIC_DELAY, 0, 3600, 420) * MILLIS_PER_SEC;
music_waitable = Waitable_Create(); music_waitable = Waitable_Create();
volume = LoadVolume(OPT_MUSIC_VOLUME, OPT_USE_MUSIC); volume = GetVolume(OPT_MUSIC_VOLUME, OPT_USE_MUSIC);
Audio_SetMusic(volume); Audio_SetMusic(volume);
} }