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 EXT = .exe
else else
LINKER_FLAGS = $(QFLAGS) -L/usr/local/lib -pthread -rdynamic 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 = CLIBS =
endif endif

View file

@ -31,8 +31,6 @@
#include "HandmadeMath.h" #include "HandmadeMath.h"
#include "miniaudio.h"
static JSValue globalThis; static JSValue globalThis;
#define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c" #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; break;
case 2: case 2:
sim_stop(); sim_pause();
break; break;
case 3: 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); const char *script = slurp_text(file);
char strbuf[strlen(script)+50]; 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); free(script);
return fn; return fn;

View file

@ -16,7 +16,8 @@
#include "dsp.h" #include "dsp.h"
#include "mix.h" #include "mix.h"
#include "miniaudio.h" #define SOKOL_AUDIO_IMPL
#include "sokol/sokol_audio.h"
#define TSF_IMPLEMENTATION #define TSF_IMPLEMENTATION
#include "tsf.h" #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() { void sound_init() {
ma_result result; saudio_setup(&(saudio_desc){
engine = malloc(sizeof(*engine)); .stream_cb = push_sound,
result = ma_engine_init(NULL, engine); .sample_rate = SAMPLERATE,
if (result != MA_SUCCESS) { .num_channels = CHANNELS,
return; .buffer_frames = BUF_FRAMES,
} });
return;
mixer_init(); mixer_init();
} }
@ -156,32 +158,31 @@ struct soundstream *soundstream_make() {
} }
void mini_sound(char *path) { 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) { 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) { if (result != MA_SUCCESS) {
YughInfo("Could not load music at path: %s", path); YughInfo("Could not load music at path: %s", path);
} }
YughInfo("Loading %s...", path); YughInfo("Loading %s...", path);
ma_sound_start(&music_sound); ma_sound_start(&music_sound);
*/
} }
void mini_music_pause() { void mini_music_pause() {
ma_sound_stop(&music_sound); // ma_sound_stop(&music_sound);
} }
void mini_music_stop() { void mini_music_stop() {
ma_sound_stop(&music_sound); // ma_sound_stop(&music_sound);
} }
void mini_master(float v) { void mini_master(float v) {
ma_engine_set_volume(engine, v); // ma_engine_set_volume(engine, v);
} }
void kill_oneshot(struct sound *s) { void kill_oneshot(struct sound *s) {

View file

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

View file

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

View file

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

View file

@ -16,49 +16,43 @@ float music_pan = 0.f;
void dsp_midi_fillbuf(struct dsp_midi_song *song, void *out, int n) 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; tml_message *midi = song->midi;
for (int i = 0; i < n; i += TSF_BLOCK) { for (int i = 0; i < n; i += TSF_BLOCK) {
while (midi && song->time >= midi->time) {
switch (midi->type) {
case TML_PROGRAM_CHANGE:
tsf_channel_set_presetnumber(song->sf, midi->channel, midi->program, (midi->channel == 9));
break;
while (midi && song->time >= midi->time) { case TML_NOTE_ON:
tsf_channel_note_on(song->sf, midi->channel, midi->key, midi->velocity / 127.f);
break;
switch (midi->type) case TML_NOTE_OFF:
{ tsf_channel_note_off(song->sf, midi->channel, midi->key);
case TML_PROGRAM_CHANGE: break;
tsf_channel_set_presetnumber(song->sf, midi->channel, midi->program, (midi->channel == 9));
break;
case TML_NOTE_ON: case TML_PITCH_BEND:
tsf_channel_note_on(song->sf, midi->channel, midi->key, midi->velocity / 127.f); tsf_channel_set_pitchwheel(song->sf, midi->channel, midi->pitch_bend);
break; break;
case TML_NOTE_OFF: case TML_CONTROL_CHANGE:
tsf_channel_note_off(song->sf, midi->channel, midi->key); tsf_channel_midi_control(song->sf, midi->channel, midi->control, midi->control_value);
break; break;
}
case TML_PITCH_BEND: midi = midi->next;
tsf_channel_set_pitchwheel(song->sf, midi->channel, midi->pitch_bend);
break;
case TML_CONTROL_CHANGE:
tsf_channel_midi_control(song->sf, midi->channel, midi->control, midi->control_value);
break;
}
midi = midi->next;
}
tsf_render_short(song->sf, o, TSF_BLOCK, 0);
o += TSF_BLOCK*CHANNELS;
song->time += TSF_BLOCK * (1000.f/SAMPLERATE);
} }
tsf_render_float(song->sf, o, TSF_BLOCK, 0);
o += TSF_BLOCK*CHANNELS;
song->time += TSF_BLOCK * (1000.f/SAMPLERATE);
}
song->midi = midi; song->midi = midi;
dsp_pan(&music_pan, out, n); // dsp_pan(&music_pan, out, n);
} }
struct bus *musicbus; struct bus *musicbus;
@ -88,9 +82,9 @@ void play_song(const char *midi, const char *sf)
cursong.data = &gsong; cursong.data = &gsong;
cursong.filter = dsp_midi_fillbuf; cursong.filter = dsp_midi_fillbuf;
musicbus = first_free_bus(cursong); musicbus = first_free_bus(cursong);
YughWarn("DID IT");
} }
void music_play() void music_play()
{ {

View file

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

View file

@ -477,8 +477,7 @@ Object.defineProperty(Array.prototype, 'lerp', {
} }
}); });
Object.defineProperty(Object.prototype, 'lerp', { Object.lerp = function(to, t) {
value: function(to, t) {
var self = this; var self = this;
var obj = {}; var obj = {};
@ -487,8 +486,7 @@ Object.defineProperty(Object.prototype, 'lerp', {
}); });
return obj; return obj;
} };
});
/* MATH EXTENSIONS */ /* MATH EXTENSIONS */
Object.defineProperty(Number.prototype, 'lerp', { 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.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; }; Math.random_range = function(min,max) { return Math.random() * (max-min) + min; };
@ -521,6 +525,7 @@ Math.angledist = function (a1, a2) {
return wrap; return wrap;
}; };
Math.angledist.doc = "Find the shortest angle between two angles.";
Math.deg2rad = function(deg) { return deg * 0.0174533; }; Math.deg2rad = function(deg) { return deg * 0.0174533; };
Math.rad2deg = function(rad) { return rad / 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 updateMS(x) { cmd(6, x); },
set physMS(x) { cmd(7, x); }, set physMS(x) { cmd(7, x); },
set renderMS(x) { cmd(5, 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); Player.players[0].control(DebugControls);

View file

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