engine: soundlib: implemented sound looping feature for Ogg Opus

This commit is contained in:
SNMetamorph 2024-12-01 19:21:57 +04:00 committed by Alibek Omarov
parent 770054daaf
commit fa152bef20

View file

@ -75,6 +75,23 @@ static const OpusFileCallbacks op_callbacks_fs = {
================================================================= =================================================================
*/ */
static void Sound_ScanOpusComments( const OggOpusFile *of )
{
const char *value;
const OpusTags *tags = op_tags( of, -1 );
if( tags )
{
if(( value = opus_tags_query( tags, "LOOPSTART", 0 ))) {
sound.loopstart = Q_atoi( value );
SetBits( sound.flags, SOUND_LOOPED );
}
else if(( value = opus_tags_query( tags, "LOOP_START", 0 ))) {
sound.loopstart = Q_atoi( value );
SetBits( sound.flags, SOUND_LOOPED );
}
}
}
qboolean Sound_LoadOggOpus( const char *name, const byte *buffer, fs_offset_t filesize ) qboolean Sound_LoadOggOpus( const char *name, const byte *buffer, fs_offset_t filesize )
{ {
long ret; long ret;
@ -105,7 +122,6 @@ qboolean Sound_LoadOggOpus( const char *name, const byte *buffer, fs_offset_t fi
sound.rate = 48000; sound.rate = 48000;
sound.width = 2; // always 16-bit PCM sound.width = 2; // always 16-bit PCM
sound.type = WF_PCMDATA; sound.type = WF_PCMDATA;
sound.flags = SOUND_RESAMPLE;
sound.samples = op_pcm_total( of, -1 ); sound.samples = op_pcm_total( of, -1 );
sound.size = sound.samples * sound.width * sound.channels; sound.size = sound.samples * sound.width * sound.channels;
sound.wav = (byte *)Mem_Calloc( host.soundpool, sound.size ); sound.wav = (byte *)Mem_Calloc( host.soundpool, sound.size );
@ -116,6 +132,9 @@ qboolean Sound_LoadOggOpus( const char *name, const byte *buffer, fs_offset_t fi
return false; return false;
} }
SetBits( sound.flags, SOUND_RESAMPLE );
Sound_ScanOpusComments( of );
while(( ret = op_read( of, (opus_int16*)(sound.wav + written), (sound.size - written) / sound.width, NULL )) != 0 ) while(( ret = op_read( of, (opus_int16*)(sound.wav + written), (sound.size - written) / sound.width, NULL )) != 0 )
{ {
if( ret < 0 ) { if( ret < 0 ) {