Wave and more sound functions.

This commit is contained in:
jussi
2022-05-31 18:05:44 +03:00
parent b1bb77e139
commit c106785ae5
8 changed files with 828 additions and 71 deletions

View File

@@ -13,6 +13,16 @@ static bool validSound( size_t id ) {
}
}
static bool validWave( size_t id ) {
if ( id < 0 || state->waveCount < id || state->waves[ id ] == NULL ) {
TraceLog( LOG_WARNING, "%s %d", "Invalid wave", id );
return false;
}
else {
return true;
}
}
static void checkSoundRealloc( int i ) {
if ( i == state->soundCount ) {
state->soundCount++;
@@ -28,8 +38,47 @@ static void checkSoundRealloc( int i ) {
}
}
static void checkWaveRealloc( int i ) {
if ( i == state->waveCount ) {
state->waveCount++;
}
if ( state->waveCount == state->waveAlloc ) {
state->waveAlloc += ALLOC_PAGE_SIZE;
state->waves = realloc( state->waves, state->waveAlloc * sizeof( Wave* ) );
for ( i = state->waveCount; i < state->waveAlloc; i++ ) {
state->waves[i] = NULL;
}
}
}
/*
## Audio - Sounds
## Audio - Audio device management
*/
/*
> success = RL_SetMasterVolume( float volume )
Set master volume ( listener )
- Failure return false
- Success return true
*/
int laudioSetMasterVolume( lua_State *L ) {
if ( !lua_isnumber( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_SetMasterVolume( float volume )" );
lua_pushboolean( L, false );
return 1;
}
SetMasterVolume( lua_tonumber( L, -1 ) );
lua_pushboolean( L, true );
return 1;
}
/*
## Audio - Wave/Sound Loading
*/
/*
@@ -67,6 +116,287 @@ int laudioLoadSound( lua_State *L ) {
return 1;
}
/*
> wave = RL_LoadWave( string fileName )
Load wave data from file
- Failure return -1
- Success return int
*/
int laudioLoadWave( lua_State *L ) {
if ( !lua_isstring( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_LoadWave( string fileName )" );
lua_pushinteger( L, -1 );
return 1;
}
if ( FileExists( lua_tostring( L, -1 ) ) ) {
int i = 0;
for ( i = 0; i < state->waveCount; i++ ) {
if ( state->waves[i] == NULL ) {
break;
}
}
state->waves[i] = malloc( sizeof( Wave ) );
*state->waves[i] = LoadWave( lua_tostring( L, -1 ) );
lua_pushinteger( L, i );
checkWaveRealloc( i );
}
else {
lua_pushinteger( L, -1 );
}
return 1;
}
/*
> sound = RL_LoadSoundFromWave( Wave wave )
Load sound from wave data
- Failure return -1
- Success return int
*/
int laudioLoadSoundFromWave( lua_State *L ) {
if ( !lua_isnumber( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_LoadSoundFromWave( Wave wave )" );
lua_pushinteger( L, -1 );
return 1;
}
size_t waveId = lua_tointeger( L, -1 );
if ( !validWave( waveId ) ) {
lua_pushinteger( L, -1 );
return 1;
}
int i = 0;
for ( i = 0; i < state->soundCount; i++ ) {
if ( state->sounds[i] == NULL ) {
break;
}
}
state->sounds[i] = malloc( sizeof( Sound ) );
*state->sounds[i] = LoadSoundFromWave( *state->waves[ waveId ] );
lua_pushinteger( L, i );
checkSoundRealloc( i );
return 1;
}
/*
> success = RL_UnloadSound( Sound sound )
Unload sound
- Failure return false
- Success return true
*/
int laudioUnloadSound( lua_State *L ) {
if ( !lua_isnumber( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_UnloadSound( Sound sound )" );
lua_pushboolean( L, false );
return 1;
}
size_t id = lua_tointeger( L, -1 );
if ( !validSound( id ) ) {
lua_pushboolean( L, false );
return 1;
}
UnloadSound( *state->sounds[ id ] );
state->sounds[ id ] = NULL;
lua_pushboolean( L, true );
return 1;
}
/*
> success = RL_UnloadWave( Wave wave )
Unload wave data
- Failure return false
- Success return true
*/
int laudioUnloadWave( lua_State *L ) {
if ( !lua_isnumber( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_UnloadWave( Wave wave )" );
lua_pushboolean( L, false );
return 1;
}
size_t id = lua_tointeger( L, -1 );
if ( !validWave( id ) ) {
lua_pushboolean( L, false );
return 1;
}
UnloadWave( *state->waves[ id ] );
state->waves[ id ] = NULL;
lua_pushboolean( L, true );
return 1;
}
/*
> success = RL_ExportWave( Wave wave, string fileName )
Export wave data to file, returns true on success
- Failure return false
- Success return true
*/
int laudioExportWave( lua_State *L ) {
if ( !lua_isnumber( L, -2 ) || !lua_isstring( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_ExportWave( Wave wave, string fileName )" );
lua_pushboolean( L, false );
return 1;
}
size_t id = lua_tointeger( L, -2 );
if ( !validWave( id ) ) {
lua_pushboolean( L, false );
return 1;
}
lua_pushboolean( L, ExportWave( *state->waves[ id ], lua_tostring( L, -1 ) ) );
return 1;
}
/*
> success = RL_ExportWaveAsCode( Wave wave, string fileName )
Export wave sample data to code (.h), returns true on success
- Failure return false
- Success return true
*/
int laudioExportWaveAsCode( lua_State *L ) {
if ( !lua_isnumber( L, -2 ) || !lua_isstring( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_ExportWaveAsCode( Wave wave, string fileName )" );
lua_pushboolean( L, false );
return 1;
}
size_t id = lua_tointeger( L, -2 );
if ( !validWave( id ) ) {
lua_pushboolean( L, false );
return 1;
}
lua_pushboolean( L, ExportWaveAsCode( *state->waves[ id ], lua_tostring( L, -1 ) ) );
return 1;
}
/*
## Audio - Wave/Sound management
*/
/*
> success = RL_PlaySound( Sound sound )
Play a sound
- Failure return false
- Success return true
*/
int laudioPlaySound( lua_State *L ) {
if ( !lua_isnumber( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_PlaySound( Sound sound )" );
lua_pushboolean( L, false );
return 1;
}
size_t soundId = lua_tointeger( L, -1 );
if ( !validSound( soundId ) ) {
lua_pushboolean( L, false );
return 1;
}
PlaySound( *state->sounds[ soundId ] );
lua_pushboolean( L, true );
return 1;
}
/*
> success = RL_StopSound( Sound sound )
Stop playing a sound
- Failure return false
- Success return true
*/
int laudioStopSound( lua_State *L ) {
if ( !lua_isnumber( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_StopSound( Sound sound )" );
lua_pushboolean( L, false );
return 1;
}
size_t soundId = lua_tointeger( L, -1 );
if ( !validSound( soundId ) ) {
lua_pushboolean( L, false );
return 1;
}
StopSound( *state->sounds[ soundId ] );
lua_pushboolean( L, true );
return 1;
}
/*
> success = RL_PauseSound( Sound sound )
Pause a sound
- Failure return false
- Success return true
*/
int laudioPauseSound( lua_State *L ) {
if ( !lua_isnumber( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_PauseSound( Sound sound )" );
lua_pushboolean( L, false );
return 1;
}
size_t soundId = lua_tointeger( L, -1 );
if ( !validSound( soundId ) ) {
lua_pushboolean( L, false );
return 1;
}
PauseSound( *state->sounds[ soundId ] );
lua_pushboolean( L, true );
return 1;
}
/*
> success = RL_ResumeSound( Sound sound )
Resume a paused sound
- Failure return false
- Success return true
*/
int laudioResumeSound( lua_State *L ) {
if ( !lua_isnumber( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_ResumeSound( Sound sound )" );
lua_pushboolean( L, false );
return 1;
}
size_t soundId = lua_tointeger( L, -1 );
if ( !validSound( soundId ) ) {
lua_pushboolean( L, false );
return 1;
}
ResumeSound( *state->sounds[ soundId ] );
lua_pushboolean( L, true );
return 1;
}
/*
> success = RL_PlaySoundMulti( Sound sound )
@@ -81,16 +411,67 @@ int laudioPlaySoundMulti( lua_State *L ) {
lua_pushboolean( L, false );
return 1;
}
if ( !validSound( lua_tointeger( L, -1 ) ) ) {
size_t soundId = lua_tointeger( L, -1 );
if ( !validSound( soundId ) ) {
lua_pushboolean( L, false );
return 1;
}
PlaySoundMulti( *state->sounds[ lua_tointeger( L, -1 ) ] );
PlaySoundMulti( *state->sounds[ soundId ] );
lua_pushboolean( L, true );
return 1;
}
/*
> RL_StopSoundMulti()
Stop any sound playing ( using multichannel buffer pool )
*/
int laudioStopSoundMulti( lua_State *L ) {
StopSoundMulti();
return 0;
}
/*
> count = RL_GetSoundsPlaying()
Get number of sounds playing in the multichannel
- Success return int
*/
int laudioGetSoundsPlaying( lua_State *L ) {
lua_pushinteger( L, GetSoundsPlaying() );
return 1;
}
/*
> playing = RL_IsSoundPlaying( Sound sound )
Check if a sound is currently playing
- Failure return nil
- Success return bool
*/
int laudioIsSoundPlaying( lua_State *L ) {
if ( !lua_isnumber( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_IsSoundPlaying( Sound sound )" );
lua_pushnil( L );
return 1;
}
size_t soundId = lua_tointeger( L, -1 );
if ( !validSound( soundId ) ) {
lua_pushnil( L );
return 1;
}
lua_pushboolean( L, IsSoundPlaying( *state->sounds[ soundId ] ) );
return 1;
}
/*
> success = RL_SetSoundVolume( Sound sound, float volume )
@@ -140,34 +521,99 @@ int laudioSetSoundPitch( lua_State *L ) {
}
/*
> success = RL_UnloadSound( Sound sound )
> success = RL_WaveFormat( Wave wave, int sampleRate, int sampleSize, int channels )
Unload sound
Convert wave data to desired format
- Failure return false
- Success return true
*/
int laudioUnloadSound( lua_State *L ) {
if ( !lua_isnumber( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_UnloadSound( Sound sound )" );
int laudioWaveFormat( lua_State *L ) {
if ( !lua_isnumber( L, -4 ) || !lua_isnumber( L, -3 ) || !lua_isnumber( L, -2 ) || !lua_isnumber( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_WaveFormat( Wave wave, int sampleRate, int sampleSize, int channels )" );
lua_pushboolean( L, false );
return 1;
}
size_t id = lua_tointeger( L, -1 );
int channels = lua_tointeger( L, -1 );
int sampleSize = lua_tointeger( L, -2 );
int sampleRate = lua_tointeger( L, -3 );
size_t waveId = lua_tointeger( L, -4 );
if ( !validSound( id ) ) {
if ( !validWave( waveId ) ) {
lua_pushboolean( L, false );
return 1;
}
UnloadSound( *state->sounds[ id ] );
state->sounds[ id ] = NULL;
WaveFormat( state->waves[ waveId ], sampleRate, sampleSize, channels );
lua_pushboolean( L, true );
return 1;
}
/*
## Audio - Music
> wave = RL_WaveCopy( Wave wave )
Copy a wave to a new wave
- Failure return -1
- Success return int
*/
int laudioWaveCopy( lua_State *L ) {
if ( !lua_isnumber( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_WaveCopy( Wave wave )" );
lua_pushinteger( L, -1 );
return 1;
}
size_t waveId = lua_tointeger( L, -1 );
if ( !validWave( waveId ) ) {
lua_pushnil( L );
return 1;
}
int i = 0;
for ( i = 0; i < state->waveCount; i++ ) {
if ( state->waves[i] == NULL ) {
break;
}
}
state->waves[i] = malloc( sizeof( Wave ) );
*state->waves[i] = WaveCopy( *state->waves[ waveId ] );
lua_pushinteger( L, i );
checkWaveRealloc( i );
return 1;
}
/*
> success = RL_WaveCrop( Wave wave, int initSample, int finalSample )
Crop a wave to defined samples range
- Failure return false
- Success return true
*/
int laudioWaveCrop( lua_State *L ) {
if ( !lua_isnumber( L, -3 ) || !lua_isnumber( L, -2 ) || !lua_isnumber( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_WaveCrop( Wave wave, int initSample, int finalSample )" );
lua_pushboolean( L, false );
return 1;
}
int finalSample = lua_tointeger( L, -1 );
int initSample = lua_tointeger( L, -2 );
size_t waveId = lua_tointeger( L, -3 );
if ( !validWave( waveId ) ) {
lua_pushboolean( L, false );
return 1;
}
WaveCrop( state->waves[ waveId ], initSample, finalSample );
lua_pushboolean( L, true );
return 1;
}
/*
## Audio - Music management
*/
/*
@@ -198,51 +644,18 @@ int laudioLoadMusicStream( lua_State *L ) {
}
/*
> PlayMusicStream()
> RL_PlayMusicStream()
Start music playing
*/
int laudioPlayMusicStream( lua_State *L ) {
PlayMusicStream( state->music );
return 1;
return 0;
}
/*
> StopMusicStream()
Stop music playing
*/
int laudioStopMusicStream( lua_State *L ) {
StopMusicStream( state->music );
return 1;
}
/*
> PauseMusicStream()
Pause music playing
*/
int laudioPauseMusicStream( lua_State *L ) {
PauseMusicStream( state->music );
return 1;
}
/*
> ResumeMusicStream()
Resume playing paused music
*/
int laudioResumeMusicStream( lua_State *L ) {
ResumeMusicStream( state->music );
return 1;
}
/*
> playing = PlayMusicStream()
> playing = RL_PlayMusicStream()
Check if music is playing
@@ -254,6 +667,59 @@ int laudioIsMusicStreamPlaying( lua_State *L ) {
return 1;
}
/*
> RL_StopMusicStream()
Stop music playing
*/
int laudioStopMusicStream( lua_State *L ) {
StopMusicStream( state->music );
return 0;
}
/*
> RL_PauseMusicStream()
Pause music playing
*/
int laudioPauseMusicStream( lua_State *L ) {
PauseMusicStream( state->music );
return 0;
}
/*
> RL_ResumeMusicStream()
Resume playing paused music
*/
int laudioResumeMusicStream( lua_State *L ) {
ResumeMusicStream( state->music );
return 0;
}
/*
> success = RL_SeekMusicStream( float position )
Seek music to a position ( in seconds )
- Failure return false
- Success return true
*/
int laudioSeekMusicStream( lua_State *L ) {
if ( !lua_isnumber( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_SeekMusicStream( float position )" );
lua_pushboolean( L, false );
return 1;
}
SeekMusicStream( state->music, lua_tonumber( L, -1 ) );
lua_pushboolean( L, true );
return 1;
}
/*
> success = RL_SetMusicVolume( float volume )
@@ -273,3 +739,49 @@ int laudioSetMusicVolume( lua_State *L ) {
return 1;
}
/*
> success = RL_SetMusicPitch( float pitch )
Set pitch for a music ( 1.0 is base level )
- Failure return false
- Success return true
*/
int laudioSetMusicPitch( lua_State *L ) {
if ( !lua_isnumber( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_SetMusicPitch( float pitch )" );
lua_pushboolean( L, false );
return 1;
}
SetMusicPitch( state->music, lua_tonumber( L, -1 ) );
lua_pushboolean( L, true );
return 1;
}
/*
> length = RL_GetMusicTimeLength()
Get music time length ( in seconds )
- Success return float
*/
int laudioGetMusicTimeLength( lua_State *L ) {
lua_pushnumber( L, GetMusicTimeLength( state->music ) );
return 1;
}
/*
> played = RL_GetMusicTimePlayed()
Get current music time played ( in seconds )
- Success return float
*/
int laudioGetMusicTimePlayed( lua_State *L ) {
lua_pushnumber( L, GetMusicTimePlayed( state->music ) );
return 1;
}

View File

@@ -836,20 +836,41 @@ void luaRegister() {
lua_register( L, "RL_MeasureText", ltextMeasureText );
/* Audio. */
/* Sound. */
/* Audio device management. */
lua_register( L, "RL_SetMasterVolume", laudioSetMasterVolume );
/* Wave/Sound Loading. */
lua_register( L, "RL_LoadSound", laudioLoadSound );
lua_register( L, "RL_LoadWave", laudioLoadWave );
lua_register( L, "RL_LoadSoundFromWave", laudioLoadSoundFromWave );
lua_register( L, "RL_UnloadSound", laudioUnloadSound );
lua_register( L, "RL_UnloadWave", laudioUnloadWave );
lua_register( L, "RL_ExportWave", laudioExportWave );
lua_register( L, "RL_ExportWaveAsCode", laudioExportWaveAsCode );
/* Wave/Sound management */
lua_register( L, "RL_PlaySound", laudioPlaySound );
lua_register( L, "RL_StopSound", laudioStopSound );
lua_register( L, "RL_PauseSound", laudioPauseSound );
lua_register( L, "RL_ResumeSound", laudioResumeSound );
lua_register( L, "RL_PlaySoundMulti", laudioPlaySoundMulti );
lua_register( L, "RL_StopSoundMulti", laudioStopSoundMulti );
lua_register( L, "RL_GetSoundsPlaying", laudioGetSoundsPlaying );
lua_register( L, "RL_IsSoundPlaying", laudioIsSoundPlaying );
lua_register( L, "RL_SetSoundVolume", laudioSetSoundVolume );
lua_register( L, "RL_SetSoundPitch", laudioSetSoundPitch );
lua_register( L, "RL_UnloadSound", laudioUnloadSound );
/* Music. */
lua_register( L, "RL_WaveFormat", laudioWaveFormat );
lua_register( L, "RL_WaveCopy", laudioWaveCopy );
lua_register( L, "RL_WaveCrop", laudioWaveCrop );
/* Music management. */
lua_register( L, "RL_LoadMusicStream", laudioLoadMusicStream );
lua_register( L, "RL_PlayMusicStream", laudioPlayMusicStream );
lua_register( L, "RL_IsMusicStreamPlaying", laudioIsMusicStreamPlaying );
lua_register( L, "RL_StopMusicStream", laudioStopMusicStream );
lua_register( L, "RL_PauseMusicStream", laudioPauseMusicStream );
lua_register( L, "RL_ResumeMusicStream", laudioResumeMusicStream );
lua_register( L, "RL_IsMusicStreamPlaying", laudioIsMusicStreamPlaying );
lua_register( L, "RL_SetMusicVolume", laudioSetMusicVolume );
lua_register( L, "RL_SetMusicPitch", laudioSetMusicPitch );
lua_register( L, "RL_GetMusicTimeLength", laudioGetMusicTimeLength );
lua_register( L, "RL_GetMusicTimePlayed", laudioGetMusicTimePlayed );
/* Math. */
/* Utils. */

View File

@@ -33,6 +33,10 @@ bool stateInit( const char *exePath ) {
state->fontAlloc = ALLOC_PAGE_SIZE;
state->fontCount = 1;
state->fonts = malloc( state->fontAlloc * sizeof( Font* ) );
/* Wavess. */
state->waveAlloc = ALLOC_PAGE_SIZE;
state->waveCount = 0;
state->waves = malloc( state->waveAlloc * sizeof( Wave* ) );
/* Sounds. */
state->soundAlloc = ALLOC_PAGE_SIZE;
state->soundCount = 0;
@@ -74,6 +78,7 @@ bool stateInit( const char *exePath ) {
state->images[i] = NULL;
state->textures[i] = NULL;
state->renderTextures[i] = NULL;
state->waves[i] = NULL;
state->sounds[i] = NULL;
state->camera2Ds[i] = NULL;
state->camera3Ds[i] = NULL;
@@ -136,6 +141,12 @@ void stateFree() {
free( state->fonts[i] );
}
}
for ( int i = 0; i < state->waveCount; ++i ) {
if ( state->waves[i] != NULL ) {
UnloadWave( *state->waves[i] );
free( state->waves[i] );
}
}
for ( int i = 0; i < state->soundCount; ++i ) {
if ( state->sounds[i] != NULL ) {
UnloadSound( *state->sounds[i] );
@@ -208,6 +219,7 @@ void stateFree() {
free( state->textures );
free( state->renderTextures );
free( state->fonts );
free( state->waves );
free( state->sounds );
free( state->camera2Ds );
free( state->camera3Ds );
@@ -217,5 +229,6 @@ void stateFree() {
free( state->animations );
free( state->shaders );
free( state->lights );
free( state->exePath );
free( state );
}