Okay, time to test this stuff

This commit is contained in:
rnlf
2017-01-22 01:59:08 +01:00
parent 5f6daa497f
commit d14cf3ac74
8 changed files with 5516 additions and 2 deletions

View File

@@ -104,6 +104,7 @@ function love.run()
love.graphics.clear()
if love.draw then love.draw() end
love.graphics.present()
love.mixer.mix()
end
end

5399
src/lib/stb/stb_vorbis.c Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -25,6 +25,7 @@ typedef struct {
#define LUAOBJ_TYPE_IMAGE (1 << 0)
#define LUAOBJ_TYPE_QUAD (1 << 1)
#define LUAOBJ_TYPE_FONT (1 << 2)
#define LUAOBJ_TYPE_SOUND (1 << 3)
int luaobj_newclass(lua_State *L, const char *name, const char *extends,

View File

@@ -26,6 +26,8 @@ int luaopen_graphics(lua_State *L);
int luaopen_timer(lua_State *L);
int luaopen_keyboard(lua_State *L);
int luaopen_mouse(lua_State *L);
int luaopen_mixer(lua_State *L);
int luaopen_sound(lua_State *L);
int luaopen_love(lua_State *L) {
int i;
@@ -59,6 +61,8 @@ int luaopen_love(lua_State *L) {
{ "timer", luaopen_timer },
{ "keyboard", luaopen_keyboard },
{ "mouse", luaopen_mouse },
{ "mixer", luaopen_mixer },
{ "sound", luaopen_mixer },
{ 0 },
};
for (i = 0; mods[i].name; i++) {

21
src/modules/l_mixer.c Normal file
View File

@@ -0,0 +1,21 @@
#include "mixer.h"
#include "luaobj.h"
int l_mixer_mix(lua_State *L) {
mixer_mix();
return 0;
}
int luaopen_mixer(lua_State *L) {
luaL_Reg reg[] = {
{ "mix", l_mixer_mix },
{ 0, 0 }
};
luaL_newlib(L, reg);
return 1;
}

47
src/modules/l_sound.c Normal file
View File

@@ -0,0 +1,47 @@
#include "luaobj.h"
#include "sound.h"
#include "mixer.h"
#define CLASS_TYPE LUAOBJ_TYPE_SOUND
#define CLASS_NAME "Sound"
int l_sound_new(lua_State *L) {
const char *filename = luaL_checkstring(L, 1);
sound_t *self = luaobj_newudata(L, sizeof(*self));
luaobj_setclass(L, CLASS_TYPE, CLASS_NAME);
const char *err = sound_init(self, filename);
if (err) luaL_error(L, err);
return 1;
}
int l_sound_gc(lua_State *L) {
sound_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
sound_deinit(self);
return 0;
}
int l_sound_play(lua_State *L) {
sound_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
mixer_play(self);
return 0;
}
int luaopen_sound(lua_State *L) {
luaL_Reg reg[] = {
{ "new", l_sound_new },
{ "__gc", l_sound_gc },
{ "play", l_sound_play },
{ 0, 0 }
};
luaobj_newclass(L, CLASS_NAME, NULL, l_sound_new, reg);
return 1;
}

View File

@@ -1,14 +1,50 @@
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "sound.h"
#include "lib/dmt/dmt.h"
#define STB_VORBIS_HEADER_ONLY 1
#include "lib/stb/stb_vorbis.c"
int sound_init(sound_t *self, int samples) {
int sound_initSilence(sound_t *self, int samples) {
self->sampleCount = samples;
self->samples = (int16_t*)dmt_malloc(samples * sizeof(int16_t));
return 0;
}
char const* sound_init(sound_t *self, char const* filename) {
short *buffer;
int channels;
int samplingRate;
char const* err = NULL;
int len = stb_vorbis_decode_filename(filename, &channels, &samplingRate, &buffer);
if(len == -1) {
return "could not decode Vorbis file";
}
if(channels != 1) {
err = "only single channel audio files supported";
goto fail;
}
if(samplingRate != 22050) {
err = "only 22050Hz audio files suported";
goto fail;
}
int bufSize = len * sizeof(int16_t);
self->samples = (int16_t*)dmt_malloc(bufSize);
memcpy(self->samples, buffer, bufSize);
fail:
free(buffer);
return err;
}
void sound_deinit(sound_t *self) {
dmt_free(self->samples);
}

View File

@@ -2,12 +2,17 @@
#define SOUND_H
#define SOUND_ERROR_DECODE 1
#define SOUND_ERROR_CHANNEL_COUNT 2
#define SOUND_ERROR_SAMPLING_RATE 3
typedef struct {
int sampleCount;
int16_t *samples;
} sound_t;
int sound_init(sound_t *self, int samples);
int sound_initSilence(sound_t *self, int samples);
char const* sound_init(sound_t *self, char const* filename);
void sound_deinit(sound_t *self);