From bf286dc952981369de7f834936ed023446dc780d Mon Sep 17 00:00:00 2001 From: Denis Pauk Date: Tue, 1 Jun 2021 21:25:21 +0300 Subject: [PATCH] Add ogg/vorbis sound for effects --- src/client/sound/header/vorbis.h | 3 ++ src/client/sound/ogg.c | 67 +++++++++++++++++++++++++++++-- src/client/sound/sound.c | 68 ++++++++++++++++++++++++++++++-- 3 files changed, 130 insertions(+), 8 deletions(-) diff --git a/src/client/sound/header/vorbis.h b/src/client/sound/header/vorbis.h index 8a3777bd..e5fb5d3b 100644 --- a/src/client/sound/header/vorbis.h +++ b/src/client/sound/header/vorbis.h @@ -25,6 +25,8 @@ #ifndef CL_SOUND_VORBIS_H #define CL_SOUND_VORBIS_H +#include "local.h" + typedef enum { PLAY, @@ -40,5 +42,6 @@ void OGG_SaveState(void); void OGG_Shutdown(void); void OGG_Stop(void); void OGG_Stream(void); +void OGG_LoadAsWav(char *filename, wavinfo_t *info, void **buffer); #endif diff --git a/src/client/sound/ogg.c b/src/client/sound/ogg.c index 7888a95d..f64f44d8 100644 --- a/src/client/sound/ogg.c +++ b/src/client/sound/ogg.c @@ -387,13 +387,13 @@ OGG_PlayTrack(int trackNo) if ((trackNo < 2) || (trackNo > ogg_maxfileindex)) { - Com_Printf("OGG_PlayTrack: %d out of range.\n", trackNo); + Com_Printf("%s: %d out of range.\n", __func__, trackNo); return; } if(ogg_tracks[trackNo] == NULL) { - Com_Printf("OGG_PlayTrack: Don't have a .ogg file for track %d\n", trackNo); + Com_Printf("%s: Don't have a .ogg file for track %d\n", __func__, trackNo); } /* Check running music. */ @@ -421,7 +421,7 @@ OGG_PlayTrack(int trackNo) if (f == NULL) { - Com_Printf("OGG_PlayTrack: could not open file %s for track %d: %s.\n", ogg_tracks[trackNo], trackNo, strerror(errno)); + Com_Printf("%s: could not open file %s for track %d: %s.\n", __func__, ogg_tracks[trackNo], trackNo, strerror(errno)); ogg_tracks[trackNo] = NULL; return; @@ -432,7 +432,7 @@ OGG_PlayTrack(int trackNo) if (res != 0) { - Com_Printf("OGG_PlayTrack: '%s' is not a valid Ogg Vorbis file (error %i).\n", ogg_tracks[trackNo], res); + Com_Printf("%s: '%s' is not a valid Ogg Vorbis file (error %i).\n", __func__, ogg_tracks[trackNo], res); fclose(f); return; @@ -709,3 +709,62 @@ OGG_Shutdown(void) ogg_started = false; } + +void +OGG_LoadAsWav(char *filename, wavinfo_t *info, void **buffer) +{ + void * temp_buffer = NULL; + int size = FS_LoadFile(filename, &temp_buffer); + short *final_buffer = NULL; + stb_vorbis * ogg_file = NULL; + int res = 0; + + if (!temp_buffer) + { + /* no such file */ + return; + } + + /* load vorbis file from memory */ + ogg_file = stb_vorbis_open_memory(temp_buffer, size, &res, NULL); + if (!res) + { + int read_samples = 0; + + /* fill in wav structure */ + info->rate = ogg_file->sample_rate; + info->width = 2; + info->channels = ogg_file->channels; + info->loopstart = -1; + info->samples = stb_vorbis_stream_length_in_samples(ogg_file); + info->dataofs = 0; + + /* alloc memory for uncompressed wav */ + final_buffer = Z_Malloc(info->samples * sizeof(short)); + + /* load sampleas to buffer */ + read_samples = stb_vorbis_get_samples_short_interleaved(ogg_file, info->channels, final_buffer, + info->samples); + + if (read_samples > 0) + { + /* fix sample list size*/ + if (read_samples * info->channels < info->samples) + { + info->samples = read_samples * info->channels; + } + + /* copy to final result */ + *buffer = final_buffer; + } + else + { + /* something is going wrong */ + Z_Free(final_buffer); + final_buffer = NULL; + } + + stb_vorbis_close(ogg_file); + } + FS_FreeFile(temp_buffer); +} diff --git a/src/client/sound/sound.c b/src/client/sound/sound.c index 4fabb6a8..a9aabdf1 100644 --- a/src/client/sound/sound.c +++ b/src/client/sound/sound.c @@ -205,6 +205,53 @@ S_IsSilencedMuzzleFlash(const wavinfo_t* info, const void* raw_data, const char* return true; } +static void +S_LoadVorbis(char *path, char* name, wavinfo_t *info, void **buffer) +{ + int len; + char namewe[256]; + char filename[MAX_QPATH]; + const char* ext; + + if (!path) + { + return; + } + + ext = COM_FileExtension(path); + if(!ext[0]) + { + /* file has no extension */ + return; + } + + len = strlen(path); + + /* Remove the extension */ + memset(namewe, 0, 256); + memcpy(namewe, path, len - (strlen(ext) + 1)); + + if (len < 5) + { + return; + } + + if (strcmp(ext, "wav")) + { + /* Non wav? */ + return; + } + + /* Combine with ogg */ + Q_strlcpy(filename, namewe, sizeof(filename)); + + /* Add the extension */ + Q_strlcat(filename, ".", sizeof(filename)); + Q_strlcat(filename, "ogg", sizeof(filename)); + + OGG_LoadAsWav(filename, info, buffer); +} + /* * Loads one sample into memory */ @@ -212,10 +259,9 @@ sfxcache_t * S_LoadSound(sfx_t *s) { char namebuffer[MAX_QPATH]; - byte *data; + byte *data = NULL; wavinfo_t info; sfxcache_t *sc; - int size; char *name; if (s->name[0] == '*') @@ -251,7 +297,18 @@ S_LoadSound(sfx_t *s) Com_sprintf(namebuffer, sizeof(namebuffer), "sound/%s", name); } - size = FS_LoadFile(namebuffer, (void **)&data); + S_LoadVorbis(namebuffer, s->name, &info, (void **)&data); + + // can't load ogg file + if (!data) + { + int size = FS_LoadFile(namebuffer, (void **)&data); + + if (data) + { + info = GetWavinfo(s->name, data, size); + } + } if (!data) { @@ -260,7 +317,10 @@ S_LoadSound(sfx_t *s) return NULL; } - info = GetWavinfo(s->name, data, size); + /* + Com_Printf("%s: rate:%d\n\twidth:%d\n\tchannels:%d\n\tloopstart:%d\n\tsamples:%d\n\tdataofs:%d\n", + s->name, info.rate, info.width, info.channels, info.loopstart, info.samples, info.dataofs); + */ if (info.channels < 1 || info.channels > 2) {