added config option to remove ogg-vorbis support

This commit is contained in:
Andre Leiradella 2015-05-03 19:31:13 -03:00
parent e96dcf0f20
commit 3bb15b9d74
3 changed files with 79 additions and 60 deletions

View File

@ -29,6 +29,9 @@ value must be used when rle-encoding images with rlrle.lua.
#define RL_OGG_INCREMENT 32768
#endif
/* Undef to remove ogg-vorbis support. */
#define RL_OGG_VORBIS
/* The number of userdata entries in other structures. */
#ifndef RL_USERDATA_COUNT
#define RL_USERDATA_COUNT 2

View File

@ -10,6 +10,8 @@
#include <rl_endian.c>
/*---------------------------------------------------------------------------*/
#ifdef RL_OGG_VORBIS
// #define STB_VORBIS_NO_CRT
#define STB_VORBIS_NO_STDIO
#define STB_VORBIS_NO_PUSHDATA_API
@ -23,6 +25,8 @@
#define alloca( a ) 0
#include <stb_vorbis.c>
#endif
/*---------------------------------------------------------------------------*/
typedef struct
@ -38,6 +42,7 @@ voice_t;
static int16_t audio_buffer[ RL_SAMPLES_PER_FRAME * 2 ];
static voice_t voices[ RL_MAX_VOICES ];
#ifdef RL_OGG_VORBIS
static stb_vorbis* ogg_stream;
static stb_vorbis_alloc ogg_alloc;
static int16_t ogg_pcm[ 4096 ];
@ -45,6 +50,7 @@ static int ogg_position;
static int ogg_available;
static int ogg_repeat;
static rl_soundstop_t ogg_stop_cb;
#endif
void rl_sound_init( void )
{
@ -53,16 +59,20 @@ void rl_sound_init( void )
voices[ i ].sound = NULL;
}
#ifdef RL_OGG_VORBIS
ogg_stream = NULL;
#endif
}
void rl_sound_done( void )
{
#ifdef RL_OGG_VORBIS
if ( ogg_stream )
{
stb_vorbis_close( ogg_stream );
rl_free( ogg_alloc.alloc_buffer );
}
#endif
}
rl_sound_t* rl_sound_create( const void* data, size_t size, int stereo )
@ -152,6 +162,7 @@ void rl_sound_stop_all( void )
}
}
#ifdef RL_OGG_VORBIS
int rl_sound_play_ogg( const void* data, size_t size, int repeat, rl_soundstop_t stop_cb )
{
if ( !ogg_stream )
@ -219,6 +230,67 @@ void rl_sound_stop_ogg( void )
}
}
static void ogg_mix( int32_t* buffer )
{
if ( ogg_stream )
{
int buf_free = RL_SAMPLES_PER_FRAME * 2;
if ( ogg_position == ogg_available )
{
again:
ogg_available = stb_vorbis_get_frame_short_interleaved( ogg_stream, 2, ogg_pcm, sizeof( ogg_pcm ) / sizeof( ogg_pcm[ 0 ] ) );
if ( !ogg_available )
{
if ( ogg_repeat )
{
stb_vorbis_seek_start( ogg_stream );
goto again;
}
else
{
if ( ogg_stop_cb )
{
ogg_stop_cb( NULL );
}
stb_vorbis_close( ogg_stream );
ogg_stream = NULL;
return;
}
}
ogg_available *= 2;
ogg_position = 0;
}
const int16_t* pcm = ogg_pcm + ogg_position;
if ( ogg_available < buf_free )
{
for ( int i = ogg_available; i != 0; --i )
{
*buffer++ += *pcm++;
}
buf_free -= ogg_available;
goto again;
}
else
{
for ( int i = buf_free; i != 0; --i )
{
*buffer++ += *pcm++;
}
ogg_position += buf_free;
ogg_available -= buf_free;
}
}
}
#endif /* RL_OGG_VORBIS */
static void mix( int32_t* buffer, voice_t* voice )
{
int buf_free = RL_SAMPLES_PER_FRAME * 2;
@ -303,66 +375,6 @@ again:
}
}
static void ogg_mix( int32_t* buffer )
{
if ( ogg_stream )
{
int buf_free = RL_SAMPLES_PER_FRAME * 2;
if ( ogg_position == ogg_available )
{
again:
ogg_available = stb_vorbis_get_frame_short_interleaved( ogg_stream, 2, ogg_pcm, sizeof( ogg_pcm ) / sizeof( ogg_pcm[ 0 ] ) );
if ( !ogg_available )
{
if ( ogg_repeat )
{
stb_vorbis_seek_start( ogg_stream );
goto again;
}
else
{
if ( ogg_stop_cb )
{
ogg_stop_cb( NULL );
}
stb_vorbis_close( ogg_stream );
ogg_stream = NULL;
return;
}
}
ogg_available *= 2;
ogg_position = 0;
}
const int16_t* pcm = ogg_pcm + ogg_position;
if ( ogg_available < buf_free )
{
for ( int i = ogg_available; i != 0; --i )
{
*buffer++ += *pcm++;
}
buf_free -= ogg_available;
goto again;
}
else
{
for ( int i = buf_free; i != 0; --i )
{
*buffer++ += *pcm++;
}
ogg_position += buf_free;
ogg_available -= buf_free;
}
}
}
const int16_t* rl_sound_mix( void )
{
int32_t buffer[ RL_SAMPLES_PER_FRAME * 2 ];
@ -382,7 +394,9 @@ const int16_t* rl_sound_mix( void )
}
while ( voice < end );
#ifdef RL_OGG_VORBIS
ogg_mix( buffer );
#endif
for ( int i = 0; i < RL_SAMPLES_PER_FRAME * 2; i++ )
{

View File

@ -31,8 +31,10 @@ void rl_sound_stop( int index );
void rl_sound_stop_all( void );
#ifdef RL_OGG_VORBIS
int rl_sound_play_ogg( const void* data, size_t size, int repeat, rl_soundstop_t stop_cb );
void rl_sound_stop_ogg( void );
#endif
const int16_t* rl_sound_mix( void );