sound: cleanup ogg to wav code

This commit is contained in:
Denis Pauk 2021-06-02 22:00:54 +03:00
parent fbde7b753f
commit d554b89730
2 changed files with 14 additions and 19 deletions

View file

@ -728,7 +728,7 @@ OGG_LoadAsWav(char *filename, wavinfo_t *info, void **buffer)
/* load vorbis file from memory */ /* load vorbis file from memory */
ogg_file = stb_vorbis_open_memory(temp_buffer, size, &res, NULL); ogg_file = stb_vorbis_open_memory(temp_buffer, size, &res, NULL);
if (!res) if (!res && ogg_file->channels > 0)
{ {
int read_samples = 0; int read_samples = 0;
@ -737,22 +737,24 @@ OGG_LoadAsWav(char *filename, wavinfo_t *info, void **buffer)
info->width = 2; info->width = 2;
info->channels = ogg_file->channels; info->channels = ogg_file->channels;
info->loopstart = -1; 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; info->dataofs = 0;
/* alloc memory for uncompressed wav */ /* 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 */ /* load sampleas to buffer */
read_samples = stb_vorbis_get_samples_short_interleaved(ogg_file, info->channels, final_buffer, read_samples = stb_vorbis_get_samples_short_interleaved(
info->samples); ogg_file, info->channels, final_buffer,
info->samples * ogg_file->channels);
if (read_samples > 0) if (read_samples > 0)
{ {
/* fix sample list size*/ /* 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 */ /* copy to final result */

View file

@ -206,7 +206,7 @@ S_IsSilencedMuzzleFlash(const wavinfo_t* info, const void* raw_data, const char*
} }
static void 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; int len;
char namewe[256]; char namewe[256];
@ -227,27 +227,20 @@ S_LoadVorbis(char *path, char* name, wavinfo_t *info, void **buffer)
len = strlen(path); len = strlen(path);
/* Remove the extension */
memset(namewe, 0, 256);
memcpy(namewe, path, len - (strlen(ext) + 1));
if (len < 5) if (len < 5)
{ {
return; return;
} }
if (strcmp(ext, "wav")) /* Remove the extension */
{ memset(namewe, 0, sizeof(namewe));
/* Non wav? */ memcpy(namewe, path, len - (strlen(ext) + 1));
return;
}
/* Combine with ogg */ /* Combine with ogg */
Q_strlcpy(filename, namewe, sizeof(filename)); Q_strlcpy(filename, namewe, sizeof(filename));
/* Add the extension */ /* 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); OGG_LoadAsWav(filename, info, buffer);
} }