corrected level fn registering; initial move to sokol_audio w/ 32bit float sound [57d84de51f509035]

This commit is contained in:
John Alanbrook 2023-08-29 22:11:36 +00:00
parent a3c6292eb2
commit 6dbfb2afd3
15 changed files with 108 additions and 190303 deletions

View file

@ -84,7 +84,7 @@ ifeq ($(OS), WIN32)
EXT = .exe
else
LINKER_FLAGS = $(QFLAGS) -L/usr/local/lib -pthread -rdynamic
ELIBS = engine pthread quickjs glfw3 GL c m dl
ELIBS = engine pthread quickjs glfw3 asound GL c m dl
CLIBS =
endif

View file

@ -31,8 +31,6 @@
#include "HandmadeMath.h"
#include "miniaudio.h"
static JSValue globalThis;
#define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c"
@ -1179,7 +1177,7 @@ JSValue duk_sys_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *ar
break;
case 2:
sim_stop();
sim_pause();
break;
case 3:

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -157,9 +157,9 @@ JSValue script_compile(const char *file)
{
const char *script = slurp_text(file);
char strbuf[strlen(script)+50];
sprintf(strbuf, "(function(){%s})", script);
sprintf(strbuf, "(function(){\n%s\n})", script);
JSValue fn = JS_Eval(js, strbuf, strlen(script), file, JS_EVAL_FLAGS);
JSValue fn = JS_Eval(js, strbuf, strlen(strbuf), file, JS_EVAL_FLAGS);
free(script);
return fn;

View file

@ -16,7 +16,8 @@
#include "dsp.h"
#include "mix.h"
#include "miniaudio.h"
#define SOKOL_AUDIO_IMPL
#include "sokol/sokol_audio.h"
#define TSF_IMPLEMENTATION
#include "tsf.h"
@ -97,17 +98,18 @@ void wav_norm_gain(struct wav *w, double lv) {
}
}
static ma_engine *engine;
void push_sound(float *buffer, int frames, int chan)
{
bus_fill_buffers(buffer, frames*chan);
}
void sound_init() {
ma_result result;
engine = malloc(sizeof(*engine));
result = ma_engine_init(NULL, engine);
if (result != MA_SUCCESS) {
return;
}
return;
saudio_setup(&(saudio_desc){
.stream_cb = push_sound,
.sample_rate = SAMPLERATE,
.num_channels = CHANNELS,
.buffer_frames = BUF_FRAMES,
});
mixer_init();
}
@ -156,32 +158,31 @@ struct soundstream *soundstream_make() {
}
void mini_sound(char *path) {
ma_engine_play_sound(engine, path, NULL);
//ma_engine_play_sound(engine, path, NULL);
}
static ma_sound music_sound;
void mini_music_play(char *path) {
ma_sound_uninit(&music_sound);
int result = ma_sound_init_from_file(engine, path, MA_SOUND_FLAG_NO_SPATIALIZATION, NULL, NULL, &music_sound);
/* int result = ma_sound_init_from_file(engine, path, MA_SOUND_FLAG_NO_SPATIALIZATION, NULL, NULL, &music_sound);
if (result != MA_SUCCESS) {
YughInfo("Could not load music at path: %s", path);
}
YughInfo("Loading %s...", path);
ma_sound_start(&music_sound);
*/
}
void mini_music_pause() {
ma_sound_stop(&music_sound);
// ma_sound_stop(&music_sound);
}
void mini_music_stop() {
ma_sound_stop(&music_sound);
// ma_sound_stop(&music_sound);
}
void mini_master(float v) {
ma_engine_set_volume(engine, v);
// ma_engine_set_volume(engine, v);
}
void kill_oneshot(struct sound *s) {

View file

@ -3,6 +3,8 @@
struct circbuf;
typedef float soundbyte;
struct Mix_Chunk {
int i;
};

View file

@ -12,9 +12,7 @@ static struct bus bus[256];
static int first = 0; /* First bus available */
static int first_on = -1; /* First bus to fill buffer with */
short mastermix[BUF_FRAMES*CHANNELS];
static int initted = 0;
soundbyte mastermix[BUF_FRAMES*CHANNELS];
static float master_volume = 1.f;
@ -32,13 +30,9 @@ void mixer_init() {
}
bus[255].next = -1;
initted = 1;
}
struct bus *first_free_bus(struct dsp_filter in) {
// assert(initted);
for (int i = 0; i < 255; i++)
if (!bus[i].on) {
bus[i].on = 1;
@ -79,10 +73,10 @@ void bus_free(struct bus *b)
b->on = 0;
}
void bus_fill_buffers(short *master, int n) {
void bus_fill_buffers(soundbyte *master, int n) {
int curbus = first_on;
// if (curbus == -1) return;
memset(master, 0, BUF_FRAMES*CHANNELS*sizeof(short));
memset(master, 0, BUF_FRAMES*CHANNELS*sizeof(soundbyte));
for (int i = 0; i < 255; i++) {
if (!bus[i].on) continue;

View file

@ -2,13 +2,11 @@
#define MIX_H
#include "dsp.h"
struct sound;
#include "sound.h"
struct bus {
struct dsp_filter in;
short buf[BUF_FRAMES*CHANNELS];
soundbyte buf[BUF_FRAMES*CHANNELS];
float gain;
int on;
int next; /* Next available bus */
@ -16,12 +14,12 @@ struct bus {
int id;
};
extern short mastermix[BUF_FRAMES*CHANNELS];
extern soundbyte mastermix[BUF_FRAMES*CHANNELS];
void mixer_init();
struct bus *first_free_bus(struct dsp_filter in);
void bus_fill_buffers(short *master, int n);
void bus_fill_buffers(soundbyte *master, int n);
/* Set volume between 0 and 100% */
void mix_master_vol(float v);

View file

@ -16,15 +16,12 @@ float music_pan = 0.f;
void dsp_midi_fillbuf(struct dsp_midi_song *song, void *out, int n)
{
short *o = (short*)out;
soundbyte *o = (soundbyte*)out;
tml_message *midi = song->midi;
for (int i = 0; i < n; i += TSF_BLOCK) {
while (midi && song->time >= midi->time) {
switch (midi->type)
{
switch (midi->type) {
case TML_PROGRAM_CHANGE:
tsf_channel_set_presetnumber(song->sf, midi->channel, midi->program, (midi->channel == 9));
break;
@ -46,19 +43,16 @@ void dsp_midi_fillbuf(struct dsp_midi_song *song, void *out, int n)
break;
}
midi = midi->next;
}
tsf_render_short(song->sf, o, TSF_BLOCK, 0);
tsf_render_float(song->sf, o, TSF_BLOCK, 0);
o += TSF_BLOCK*CHANNELS;
song->time += TSF_BLOCK * (1000.f/SAMPLERATE);
}
song->midi = midi;
dsp_pan(&music_pan, out, n);
// dsp_pan(&music_pan, out, n);
}
struct bus *musicbus;
@ -88,9 +82,9 @@ void play_song(const char *midi, const char *sf)
cursong.data = &gsong;
cursong.filter = dsp_midi_fillbuf;
musicbus = first_free_bus(cursong);
YughWarn("DID IT");
}
void music_play()
{

View file

@ -48,7 +48,7 @@ double renderMS = 1 / 165.f;
double physMS = 1 / 165.f;
double updateMS = 1 / 165.f;
static int sim_play = 0;
double lastTick = 0.0;
static int phys_step = 0;
@ -59,10 +59,11 @@ static double framems[FPSBUF];
int framei = 0;
int fps;
#define SIM_STOP 0
#define SIM_PLAY 1
#define SIM_PAUSE 2
#define SIM_STEP 3
#define SIM_PLAY 0
#define SIM_PAUSE 1
#define SIM_STEP 2
static int sim_play = SIM_PLAY;
#ifdef __TINYC__
int backtrace(void **buffer, int size) {
@ -282,7 +283,6 @@ int frame_fps() {
int sim_playing() { return sim_play == SIM_PLAY; }
int sim_paused() { return sim_play == SIM_PAUSE; }
int sim_stopped() { return sim_play == SIM_STOP; }
void sim_start() {
sim_play = SIM_PLAY;
@ -292,11 +292,6 @@ void sim_pause() {
sim_play = SIM_PAUSE;
}
void sim_stop() {
/* Revert starting state of everything from sim_start */
sim_play = SIM_STOP;
}
int phys_stepping() { return sim_play == SIM_STEP; }
void sim_step() {

View file

@ -477,8 +477,7 @@ Object.defineProperty(Array.prototype, 'lerp', {
}
});
Object.defineProperty(Object.prototype, 'lerp', {
value: function(to, t) {
Object.lerp = function(to, t) {
var self = this;
var obj = {};
@ -487,8 +486,7 @@ Object.defineProperty(Object.prototype, 'lerp', {
});
return obj;
}
});
};
/* MATH EXTENSIONS */
Object.defineProperty(Number.prototype, 'lerp', {
@ -498,6 +496,12 @@ Object.defineProperty(Number.prototype, 'lerp', {
}
});
Math.lerp = function(from, to, t) {
var v = (to - from) * t + from;
v = Math.clamp(v, from, to);
return v;
}
Math.clamp = function (x, l, h) { return x > h ? h : x < l ? l : x; }
Math.random_range = function(min,max) { return Math.random() * (max-min) + min; };
@ -521,6 +525,7 @@ Math.angledist = function (a1, a2) {
return wrap;
};
Math.angledist.doc = "Find the shortest angle between two angles.";
Math.deg2rad = function(deg) { return deg * 0.0174533; };
Math.rad2deg = function(rad) { return rad / 0.0174533; };

View file

@ -255,6 +255,18 @@ var Time = {
set updateMS(x) { cmd(6, x); },
set physMS(x) { cmd(7, x); },
set renderMS(x) { cmd(5, x); },
pause() {
Time.timescale = 0;
},
play() {
if (!Time.stash) {
Log.warn("Tried to resume time without calling Time.pause first.");
return;
}
Time.timescale = Time.stash;
},
};
Player.players[0].control(DebugControls);

View file

@ -702,6 +702,10 @@ var animation = {
},
};
var Audio = {
};
var Music = {
play(path) {
Log.info("Playing " + path);
@ -721,12 +725,13 @@ var Music = {
};
var Sound = {
sounds: [],
play(file) {
var s = Object.create(sound);
s.path = file;
s.play();
// this.id = cmd(14,file);
return s;
// var s = Object.create(Sound);
// s.path = file;
// s.play();
this.id = cmd(14,file);
//return s;
},
music(midi, sf) {
@ -1033,8 +1038,8 @@ var Register = {
entries = entries.filter(function(f) { return fn === f; });
}
n.broadcast = function() {
entries.forEach(x => x[0].call(x[1]));
n.broadcast = function(...args) {
entries.forEach(x => x[0].call(x[1], ...args));
}
n.clear = function() {
@ -1271,7 +1276,9 @@ var Game = {
stop()
{
sys_cmd(2);
Game.pause();
/* And return to editor .. */
Log.warn("Stopping not implemented. Paused, and go to editor.");
},
step()
@ -1320,19 +1327,18 @@ var Level = {
},
run() {
var objs = this.objects.slice();
var scene = {};
var self = this;
// TODO: If an object does not have a varname, give it one based on its parent
objs.forEach(function(x) {
this.objects.forEach(function(x) {
if (x.hasOwn('varname')) {
scene[x.varname] = x;
this[x.varname] = x;
}
},this);
cmd(123, this.scriptfile, self);
var fn = compile(this.scriptfile);
fn.call(this);
if (typeof this.update === 'function')
Register.update.register(this.update, this);
@ -1801,6 +1807,7 @@ World.load = function(lvl) {
World.loaded.kill();
World.loaded = World.spawn(lvl);
return World.loaded;
};
var gameobjects = {};