2018-07-06 03:23:46 +00:00
# include "api.h"
# include "data/soundData.h"
2020-11-26 21:14:02 +00:00
# include "data/blob.h"
2020-12-16 15:07:29 +00:00
# include "core/ref.h"
2020-12-19 22:17:16 +00:00
# include "core/util.h"
2020-12-16 15:07:29 +00:00
# include <stdlib.h>
2020-11-26 21:14:02 +00:00
static int l_lovrSoundDataGetBlob ( lua_State * L ) {
SoundData * soundData = luax_checktype ( L , 1 , SoundData ) ;
Blob * blob = soundData - > blob ;
luax_pushtype ( L , Blob , blob ) ;
return 1 ;
}
2018-07-06 03:23:46 +00:00
2021-02-04 18:25:06 +00:00
static int l_lovrSoundDataGetFrameCount ( lua_State * L ) {
2020-12-16 15:07:29 +00:00
SoundData * soundData = luax_checktype ( L , 1 , SoundData ) ;
2021-02-04 18:25:06 +00:00
uint32_t frames = lovrSoundDataGetFrameCount ( soundData ) ;
lua_pushinteger ( L , frames ) ;
2020-12-16 15:07:29 +00:00
return 1 ;
}
static const char * format2string ( SampleFormat f ) { return f = = SAMPLE_I16 ? " i16 " : " f32 " ; }
// soundData:read(dest, {size}, {offset}) -> framesRead
// soundData:read({size}) -> framesRead
static int l_lovrSoundDataRead ( lua_State * L ) {
//struct SoundData* soundData, uint32_t offset, uint32_t count, void* data
SoundData * source = luax_checktype ( L , 1 , SoundData ) ;
int index = 2 ;
SoundData * dest = luax_totype ( L , index , SoundData ) ;
2020-12-16 15:47:51 +00:00
if ( dest ) index + + ;
2021-02-04 18:25:06 +00:00
size_t frameCount = lua_type ( L , index ) = = LUA_TNUMBER ? lua_tointeger ( L , index + + ) : lovrSoundDataGetFrameCount ( source ) ;
2020-12-16 15:07:29 +00:00
size_t offset = dest ? luaL_optinteger ( L , index , 0 ) : 0 ;
bool shouldRelease = false ;
if ( dest = = NULL ) {
dest = lovrSoundDataCreateRaw ( frameCount , source - > channels , source - > sampleRate , source - > format , NULL ) ;
shouldRelease = true ;
} else {
lovrAssert ( dest - > channels = = source - > channels , " Source (%d) and destination (%d) channel counts must match " , source - > channels , dest - > channels ) ;
lovrAssert ( dest - > sampleRate = = source - > sampleRate , " Source (%d) and destination (%d) sample rates must match " , source - > sampleRate , dest - > sampleRate ) ;
lovrAssert ( dest - > format = = source - > format , " Source (%s) and destination (%s) formats must match " , format2string ( source - > format ) , format2string ( dest - > format ) ) ;
lovrAssert ( offset + frameCount < = dest - > frames , " Tried to write samples past the end of a SoundData buffer " ) ;
lovrAssert ( dest - > blob , " Can't read into a stream destination " ) ;
}
size_t outFrames = source - > read ( source , offset , frameCount , dest - > blob - > data ) ;
dest - > frames = outFrames ;
dest - > blob - > size = outFrames * SampleFormatBytesPerFrame ( dest - > channels , dest - > format ) ;
luax_pushtype ( L , SoundData , dest ) ;
if ( shouldRelease ) lovrRelease ( SoundData , dest ) ;
return 1 ;
}
2020-12-10 12:29:42 +00:00
static int l_lovrSoundDataAppend ( lua_State * L ) {
SoundData * soundData = luax_checktype ( L , 1 , SoundData ) ;
Blob * blob = luax_totype ( L , 2 , Blob ) ;
SoundData * sound = luax_totype ( L , 2 , SoundData ) ;
2020-12-15 09:16:58 +00:00
size_t appendedSamples = 0 ;
2020-12-10 12:29:42 +00:00
if ( sound ) {
appendedSamples = lovrSoundDataStreamAppendSound ( soundData , sound ) ;
} else if ( blob ) {
appendedSamples = lovrSoundDataStreamAppendBlob ( soundData , blob ) ;
2020-12-15 09:32:00 +00:00
} else {
luaL_argerror ( L , 2 , " Invalid blob appended " ) ;
2020-12-10 12:29:42 +00:00
}
lua_pushinteger ( L , appendedSamples ) ;
return 1 ;
}
2020-12-10 13:23:35 +00:00
static int l_lovrSoundDataSetSample ( lua_State * L ) {
SoundData * soundData = luax_checktype ( L , 1 , SoundData ) ;
int index = luaL_checkinteger ( L , 2 ) ;
float value = luax_checkfloat ( L , 3 ) ;
lovrSoundDataSetSample ( soundData , index , value ) ;
return 0 ;
}
2018-07-06 03:23:46 +00:00
const luaL_Reg lovrSoundData [ ] = {
2020-12-03 16:07:55 +00:00
{ " getBlob " , l_lovrSoundDataGetBlob } ,
2021-02-04 18:25:06 +00:00
{ " getFrameCount " , l_lovrSoundDataGetFrameCount } ,
2020-12-16 15:07:29 +00:00
{ " read " , l_lovrSoundDataRead } ,
2020-12-10 12:29:42 +00:00
{ " append " , l_lovrSoundDataAppend } ,
2020-12-10 13:23:35 +00:00
{ " setSample " , l_lovrSoundDataSetSample } ,
2020-12-03 16:07:55 +00:00
{ NULL , NULL }
2018-07-06 03:23:46 +00:00
} ;