summaryrefslogtreecommitdiff
path: root/src/audio/audio.c
blob: 45d11c2c879ffee76309a901ded117296e60798f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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;
}