From bf286dc952981369de7f834936ed023446dc780d Mon Sep 17 00:00:00 2001 From: Denis Pauk Date: Tue, 1 Jun 2021 21:25:21 +0300 Subject: [PATCH 1/5] 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) { From fbde7b753f00b3372abbd7f41649341761e89431 Mon Sep 17 00:00:00 2001 From: Denis Pauk Date: Tue, 1 Jun 2021 22:36:36 +0300 Subject: [PATCH 2/5] fclose is not required with close_on_free=true --- src/client/sound/ogg.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/client/sound/ogg.c b/src/client/sound/ogg.c index f64f44d8..71cd1cf1 100644 --- a/src/client/sound/ogg.c +++ b/src/client/sound/ogg.c @@ -428,12 +428,13 @@ OGG_PlayTrack(int trackNo) } int res = 0; + + // fclose is not required on error with close_on_free=true ogg_file = stb_vorbis_open_file(f, true, &res, NULL); if (res != 0) { Com_Printf("%s: '%s' is not a valid Ogg Vorbis file (error %i).\n", __func__, ogg_tracks[trackNo], res); - fclose(f); return; } From d554b89730918953fbf06ff9e3c67ea945fcd139 Mon Sep 17 00:00:00 2001 From: Denis Pauk Date: Wed, 2 Jun 2021 22:00:54 +0300 Subject: [PATCH 3/5] sound: cleanup ogg to wav code --- src/client/sound/ogg.c | 16 +++++++++------- src/client/sound/sound.c | 17 +++++------------ 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/src/client/sound/ogg.c b/src/client/sound/ogg.c index 71cd1cf1..9b83244b 100644 --- a/src/client/sound/ogg.c +++ b/src/client/sound/ogg.c @@ -728,7 +728,7 @@ OGG_LoadAsWav(char *filename, wavinfo_t *info, void **buffer) /* load vorbis file from memory */ ogg_file = stb_vorbis_open_memory(temp_buffer, size, &res, NULL); - if (!res) + if (!res && ogg_file->channels > 0) { int read_samples = 0; @@ -737,22 +737,24 @@ OGG_LoadAsWav(char *filename, wavinfo_t *info, void **buffer) info->width = 2; info->channels = ogg_file->channels; info->loopstart = -1; - info->samples = stb_vorbis_stream_length_in_samples(ogg_file); + /* return length * channels */ + info->samples = stb_vorbis_stream_length_in_samples(ogg_file) / ogg_file->channels; info->dataofs = 0; /* alloc memory for uncompressed wav */ - final_buffer = Z_Malloc(info->samples * sizeof(short)); + final_buffer = Z_Malloc(info->samples * sizeof(short) * ogg_file->channels); /* load sampleas to buffer */ - read_samples = stb_vorbis_get_samples_short_interleaved(ogg_file, info->channels, final_buffer, - info->samples); + read_samples = stb_vorbis_get_samples_short_interleaved( + ogg_file, info->channels, final_buffer, + info->samples * ogg_file->channels); if (read_samples > 0) { /* fix sample list size*/ - if (read_samples * info->channels < info->samples) + if (read_samples < info->samples) { - info->samples = read_samples * info->channels; + info->samples = read_samples; } /* copy to final result */ diff --git a/src/client/sound/sound.c b/src/client/sound/sound.c index a9aabdf1..7f029835 100644 --- a/src/client/sound/sound.c +++ b/src/client/sound/sound.c @@ -206,7 +206,7 @@ S_IsSilencedMuzzleFlash(const wavinfo_t* info, const void* raw_data, const char* } static void -S_LoadVorbis(char *path, char* name, wavinfo_t *info, void **buffer) +S_LoadVorbis(const char *path, const char* name, wavinfo_t *info, void **buffer) { int len; char namewe[256]; @@ -227,27 +227,20 @@ S_LoadVorbis(char *path, char* name, wavinfo_t *info, void **buffer) 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; - } + /* Remove the extension */ + memset(namewe, 0, sizeof(namewe)); + memcpy(namewe, path, len - (strlen(ext) + 1)); /* Combine with ogg */ Q_strlcpy(filename, namewe, sizeof(filename)); /* Add the extension */ - Q_strlcat(filename, ".", sizeof(filename)); - Q_strlcat(filename, "ogg", sizeof(filename)); + Q_strlcat(filename, ".ogg", sizeof(filename)); OGG_LoadAsWav(filename, info, buffer); } From 7be959894bd91b45a478cac5bba128d380adc8ed Mon Sep 17 00:00:00 2001 From: Denis Pauk Date: Sun, 13 Jun 2021 15:37:29 +0300 Subject: [PATCH 4/5] fix line duplication --- src/client/vid/vid.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/client/vid/vid.c b/src/client/vid/vid.c index 5a902f67..87edcdd9 100644 --- a/src/client/vid/vid.c +++ b/src/client/vid/vid.c @@ -444,7 +444,6 @@ VID_LoadRenderer(void) ri.Vid_GetModeInfo = VID_GetModeInfo; ri.Vid_MenuInit = VID_MenuInit; ri.Vid_WriteScreenshot = VID_WriteScreenshot; - ri.Vid_WriteScreenshot = VID_WriteScreenshot; ri.Vid_RequestRestart = VID_RequestRestart; // Exchange our export struct with the renderers import struct. From 408e7d9a992f8e499cd7f190f9281344a7ddfe8b Mon Sep 17 00:00:00 2001 From: Denis Pauk Date: Sun, 13 Jun 2021 15:52:48 +0300 Subject: [PATCH 5/5] Scale down model preview icon is too big --- src/client/menu/menu.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/client/menu/menu.c b/src/client/menu/menu.c index 5efd661b..d5891804 100644 --- a/src/client/menu/menu.c +++ b/src/client/menu/menu.c @@ -4760,6 +4760,8 @@ PlayerConfig_MenuDraw(void) { entity_t entity; char scratch[MAX_QPATH]; + int w, h; + float picscale; memset(&entity, 0, sizeof(entity)); @@ -4804,7 +4806,17 @@ PlayerConfig_MenuDraw(void) s_pmi[s_player_model_box.curvalue].directory, s_pmi[s_player_model_box.curvalue].skindisplaynames[ s_player_skin_box.curvalue]); - Draw_PicScaled(s_player_config_menu.x - 40*scale, refdef.y, scratch, scale); + + Draw_GetPicSize(&w, &h, scratch); + picscale = scale; + if (w != -1 && h != -1) + { + if (h > 32) + { + picscale = (picscale * 32) / h; + } + Draw_PicScaled(s_player_config_menu.x - 40*scale, refdef.y, scratch, picscale); + } } }