Merge pull request #717 from 0lvin/ogg_sound

Add ogg/vorbis sound for effects
This commit is contained in:
Yamagi 2021-06-25 10:18:41 +02:00 committed by GitHub
commit d9d5552662
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 140 additions and 11 deletions

View file

@ -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);
}
}
}

View file

@ -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

View file

@ -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,19 +421,20 @@ 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;
}
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("OGG_PlayTrack: '%s' is not a valid Ogg Vorbis file (error %i).\n", ogg_tracks[trackNo], res);
fclose(f);
Com_Printf("%s: '%s' is not a valid Ogg Vorbis file (error %i).\n", __func__, ogg_tracks[trackNo], res);
return;
}
@ -709,3 +710,64 @@ 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 && ogg_file->channels > 0)
{
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;
/* 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) * ogg_file->channels);
/* load sampleas to buffer */
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->samples)
{
info->samples = read_samples;
}
/* 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);
}

View file

@ -205,6 +205,46 @@ S_IsSilencedMuzzleFlash(const wavinfo_t* info, const void* raw_data, const char*
return true;
}
static void
S_LoadVorbis(const char *path, const 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);
if (len < 5)
{
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, ".ogg", sizeof(filename));
OGG_LoadAsWav(filename, info, buffer);
}
/*
* Loads one sample into memory
*/
@ -212,10 +252,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 +290,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 +310,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)
{

View file

@ -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.