diff options
Diffstat (limited to 'src/audio')
-rw-r--r-- | src/audio/CMakeLists.txt | 5 | ||||
-rw-r--r-- | src/audio/audio.c | 74 | ||||
-rw-r--r-- | src/audio/audio.h | 8 |
3 files changed, 87 insertions, 0 deletions
diff --git a/src/audio/CMakeLists.txt b/src/audio/CMakeLists.txt new file mode 100644 index 0000000..41644fb --- /dev/null +++ b/src/audio/CMakeLists.txt @@ -0,0 +1,5 @@ +project(honey_engine) + +target_sources(honey PUBLIC + ${CMAKE_CURRENT_LIST_DIR}/audio.c +) diff --git a/src/audio/audio.c b/src/audio/audio.c new file mode 100644 index 0000000..45d11c2 --- /dev/null +++ b/src/audio/audio.c @@ -0,0 +1,74 @@ +#include <lua.h> +#include <honeysuckle.h> + +#define STB_VORBIS_HEADER_ONLY +#include <stb_vorbis.c> +#define MINIAUDIO_IMPLEMENTATION +#include <miniaudio.h> +#undef STB_VORBIS_HEADER_ONLY +#include <stb_vorbis.c> + +/* stb_vorbis defines this for some reason? */ +#undef L + +#include "audio.h" + + +int audio_engine_init(lua_State *L); +int audio_engine_uninit(lua_State *L); +int audio_engine_play_sound(lua_State *L); + + +void setup_audio(lua_State *L, int honey_tbl) +{ + hs_create_table(L, + hs_str_cfunc("engine_init", audio_engine_init), + hs_str_cfunc("engine_uninit", audio_engine_uninit), + hs_str_cfunc("engine_play_sound", audio_engine_play_sound), + + /* ma_result values */ + hs_str_int("MA_SUCCESS", MA_SUCCESS), + ); + lua_setfield(L, honey_tbl, "audio"); +} + + +int audio_engine_init(lua_State *L) +{ + ma_engine *engine = malloc(sizeof(ma_engine)); + if (engine == NULL) + hs_throw_error(L, "failed to allocate memory for engine"); + + ma_result result = ma_engine_init(NULL, engine); + if (result != MA_SUCCESS) + hs_throw_error(L, "failed to initialize audio engine: %d", result); + + lua_pushlightuserdata(L, engine); + return 1; +} + + +int audio_engine_uninit(lua_State *L) +{ + ma_engine *engine; + void *engine_ptr; + hs_parse_args(L, hs_light(engine_ptr)); + engine = engine_ptr; + + ma_engine_uninit(engine); + free(engine); + return 0; +} + + +int audio_engine_play_sound(lua_State *L) +{ + ma_engine *engine; + void *engine_ptr; + char *filename; + hs_parse_args(L, hs_light(engine_ptr), hs_str(filename)); + engine = engine_ptr; + + ma_engine_play_sound(engine, filename, NULL); + return 0; +} diff --git a/src/audio/audio.h b/src/audio/audio.h new file mode 100644 index 0000000..1aa7f43 --- /dev/null +++ b/src/audio/audio.h @@ -0,0 +1,8 @@ +#ifndef HONEY_AUDIO_H +#define HONEY_AUDIO_H + +#include <lua.h> + +void setup_audio(lua_State *L, int honey_tbl); + +#endif |