summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt12
-rw-r--r--demo/example_sound.oggbin0 -> 1089524 bytes
-rw-r--r--demo/honey.lua10
-rw-r--r--src/audio/CMakeLists.txt5
-rw-r--r--src/audio/audio.c74
-rw-r--r--src/audio/audio.h8
-rw-r--r--src/main.c10
7 files changed, 110 insertions, 9 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index b428197..03e227b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -17,6 +17,7 @@ include_directories(
${LIB_ROOT}/honeysuckle/src
${LIB_ROOT}/glfw-3.3.8/include
${LIB_ROOT}/cglm/include
+ ${LIB_ROOT}/miniaudio
)
# disable byte-alignment in cglm
@@ -42,7 +43,7 @@ set(LIBRARIES lua5.1 honeysuckle glfw cargs)
if (WIN32)
set(LIBRARIES ${LIBRARIES} opengl32)
else()
- set(LIBRARIES ${LIBRARIES} GL dl m)
+ set(LIBRARIES ${LIBRARIES} GL dl pthread m)
endif()
target_link_libraries(honey ${LIBRARIES})
@@ -59,10 +60,11 @@ set_target_properties(test PROPERTIES
target_link_libraries(test lua5.1 honeysuckle glfw dl)
+add_subdirectory(${SRC_ROOT}/audio)
add_subdirectory(${SRC_ROOT}/gl)
-add_subdirectory(${SRC_ROOT}/logging)
-add_subdirectory(${SRC_ROOT}/image)
-add_subdirectory(${SRC_ROOT}/util)
-add_subdirectory(${SRC_ROOT}/test)
add_subdirectory(${SRC_ROOT}/glm)
+add_subdirectory(${SRC_ROOT}/image)
+add_subdirectory(${SRC_ROOT}/logging)
add_subdirectory(${SRC_ROOT}/options)
+add_subdirectory(${SRC_ROOT}/test)
+add_subdirectory(${SRC_ROOT}/util)
diff --git a/demo/example_sound.ogg b/demo/example_sound.ogg
new file mode 100644
index 0000000..5f97755
--- /dev/null
+++ b/demo/example_sound.ogg
Binary files differ
diff --git a/demo/honey.lua b/demo/honey.lua
index 9d007e5..f7b4392 100644
--- a/demo/honey.lua
+++ b/demo/honey.lua
@@ -2,6 +2,12 @@ local gl = honey.gl
local window = honey.window
+--===== initialize audio =====--
+
+local engine = honey.audio.engine_init()
+honey.audio.engine_play_sound(engine, 'example_sound.ogg')
+
+
--====== initialize opengl ======--
gl.Init()
@@ -196,5 +202,9 @@ while not window.shouldClose(w) do
window.swapBuffers(w)
window.pollEvents()
end
+
+--===== shut down =====--
+
window.destroy(w)
gl.Terminate()
+honey.audio.engine_uninit(engine)
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
diff --git a/src/main.c b/src/main.c
index f73da4c..e11a0c6 100644
--- a/src/main.c
+++ b/src/main.c
@@ -2,11 +2,12 @@
#include <lauxlib.h>
#include <lualib.h>
#include <honeysuckle.h>
+#include "audio/audio.h"
#include "gl/gl.h"
-#include "image/image.h"
#include "glm/glm.h"
-#include "options/options.h"
+#include "image/image.h"
#include "logging/logging.h"
+#include "options/options.h"
int main(int argc, char **argv)
@@ -24,11 +25,12 @@ int main(int argc, char **argv)
/* load honey bindings */
lua_createtable(L, 0, 2);
int honey_index = lua_gettop(L);
+ setup_audio(L, honey_index);
setup_gl(L, honey_index);
- setup_window(L, honey_index);
- setup_image(L, honey_index);
setup_glm(L, honey_index);
+ setup_image(L, honey_index);
setup_logging(L, honey_index);
+ setup_window(L, honey_index);
lua_setglobal(L, "honey");
/* load main script */