audio change

This commit is contained in:
John Alanbrook 2023-03-24 19:01:01 +00:00
parent a170d88ccc
commit 5ca3629406
12 changed files with 93044 additions and 92 deletions

View file

@ -73,18 +73,18 @@ SEM = 0.0.1
COM != git rev-parse --short HEAD COM != git rev-parse --short HEAD
VER = $(SEM)-$(COM) VER = $(SEM)-$(COM)
COMPILER_FLAGS = $(includeflag) $(QFLAGS) -MD $(WARNING_FLAGS) -DCP_USE_DOUBLES=0 -DTINYSPLINE_FLOAT_PRECISION -DDUK_USE_SYMBOL_BUILTIN -DVER=\"$(VER)\" -DINFO=\"$(INFO)\" -c $< -o $@ COMPILER_FLAGS = $(includeflag) $(QFLAGS) -MD $(WARNING_FLAGS) -DCP_USE_DOUBLES=0 -DTINYSPLINE_FLOAT_PRECISION -DDUK_USE_SYMBOL_BUILTIN -DVER=\"$(VER)\" -DINFO=\"$(INFO)\" -march=native -std=c99 -c $< -o $@
LIBPATH = -L$(BIN) LIBPATH = -L$(BIN)
ifeq ($(OS), WIN32) ifeq ($(OS), WIN32)
LINKER_FLAGS = $(QFLAGS) -static LINKER_FLAGS = $(QFLAGS) -static
ELIBS = engine ucrt yughc portaudio glfw3 opengl32 gdi32 ws2_32 ole32 winmm setupapi m ELIBS = engine ucrt yughc glfw3 opengl32 gdi32 ws2_32 ole32 winmm setupapi m
CLIBS = CLIBS =
EXT = .exe EXT = .exe
else else
LINKER_FLAGS = $(QFLAGS) -L/usr/local/lib -rdynamic LINKER_FLAGS = $(QFLAGS) -L/usr/local/lib -rdynamic
ELIBS = engine pthread yughc portaudio asound glfw3 c m dl ELIBS = engine pthread yughc glfw3 c m dl
CLIBS = CLIBS =
endif endif

View file

@ -98,13 +98,14 @@ cpVect inflatepoint(cpVect a, cpVect b, cpVect c, float d)
} }
void inflatepoints(cpVect *r, cpVect *p, float d, int n) void inflatepoints(cpVect *r, cpVect *p, float d, int n)
{ if (d == 0) { {
if (d == 0) {
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
r[i] = p[i]; r[i] = p[i];
return; return;
} }
if (cpveql(p[0], p[n-1])) { if (cpveql(p[0], p[n-1])) {
r[0] = inflatepoint(p[n-2],p[0],p[1],d); r[0] = inflatepoint(p[n-2],p[0],p[1],d);
r[n-1] = r[0]; r[n-1] = r[0];

View file

@ -10,6 +10,8 @@
#define LOG_ERROR 2 #define LOG_ERROR 2
#define LOG_CRITICAL 3 #define LOG_CRITICAL 3
#define M_PI 3.14
extern char lastlog[]; extern char lastlog[];
extern char consolelog[]; extern char consolelog[];

View file

@ -3,6 +3,7 @@
#include "script.h" #include "script.h"
#include "string.h" #include "string.h"
#include "debug.h"
#include "window.h" #include "window.h"
#include "editor.h" #include "editor.h"
#include "engine.h" #include "engine.h"
@ -26,6 +27,8 @@
#include "stb_ds.h" #include "stb_ds.h"
#include <ftw.h> #include <ftw.h>
#include "miniaudio.h"
#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"
#define BYTE_TO_BINARY(byte) \ #define BYTE_TO_BINARY(byte) \
(byte & 0x80 ? '1' : '0'), \ (byte & 0x80 ? '1' : '0'), \
@ -163,7 +166,7 @@ duk_idx_t vecarr2duk(duk_context *duk, cpVect *points, int n)
{ {
duk_idx_t arr = duk_push_array(duk); duk_idx_t arr = duk_push_array(duk);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
duk_idx_t varr = vect2duk(points[i]); vect2duk(points[i]);
duk_put_prop_index(duk, arr, i); duk_put_prop_index(duk, arr, i);
} }
@ -371,38 +374,37 @@ duk_ret_t duk_spline_cmd(duk_context *duk)
YughCritical("Spline creation error %d: %s", status.code, status.message); YughCritical("Spline creation error %d: %s", status.code, status.message);
} }
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
duk_get_prop_index(duk, 4, i); duk_get_prop_index(duk, 4, i);
points[i] = duk2vec2(duk, -1); points[i] = duk2vec2(duk, -1);
duk_pop(duk); duk_pop(duk);
} }
ts_bspline_set_control_points(&spline, points, NULL); ts_bspline_set_control_points(&spline, points, NULL);
int nsamples = duk_to_int(duk, 5); size_t nsamples = duk_to_int(duk, 5);
cpVect samples[nsamples]; cpVect samples[nsamples];
int rsamples; static_assert(sizeof(tsReal)*2 == sizeof(cpVect));
size_t rsamples;
ts_bspline_sample(&spline, nsamples, &samples, &rsamples, NULL); ts_bspline_sample(&spline, nsamples, &samples, &rsamples, NULL);
int arridx = duk_push_array(duk); int arridx = duk_push_array(duk);
duk_require_stack(duk, nsamples*3); duk_require_stack(duk, nsamples*3);
for (int i = 0; i < nsamples; i++) { for (int i = 0; i < nsamples; i++) {
int pidx = duk_push_array(duk); int pidx = duk_push_array(duk);
duk_push_number(duk, samples[i].x); duk_push_number(duk, samples[i].x);
duk_put_prop_index(duk, pidx, 0); duk_put_prop_index(duk, pidx, 0);
duk_push_number(duk, samples[i].y); duk_push_number(duk, samples[i].y);
duk_put_prop_index(duk, pidx, 1); duk_put_prop_index(duk, pidx, 1);
duk_put_prop_index(duk, arridx, i); duk_put_prop_index(duk, arridx, i);
} }
ts_bspline_free(&spline); ts_bspline_free(&spline);
return 1; return 1;
} }
void ints2duk(int *ints) void ints2duk(int *ints)
@ -587,7 +589,7 @@ duk_ret_t duk_cmd(duk_context *duk) {
break; break;
case 14: case 14:
play_oneshot(make_sound(duk_to_string(duk, 1))); mini_sound(duk_to_string(duk,1));
break; break;
case 15: case 15:
@ -607,7 +609,7 @@ duk_ret_t duk_cmd(duk_context *duk) {
break; break;
case 19: case 19:
mix_master_vol(duk_to_number(duk, 1)); mini_master(duk_to_number(duk,1));
break; break;
case 20: case 20:
@ -882,6 +884,22 @@ duk_ret_t duk_cmd(duk_context *duk) {
case 86: case 86:
ints2duk(phys2d_query_box_points(duk2vec2(duk, 1), duk2vec2(duk, 2), duk2cpvec2arr(duk,3), duk_to_int(duk,4))); ints2duk(phys2d_query_box_points(duk2vec2(duk, 1), duk2vec2(duk, 2), duk2cpvec2arr(duk,3), duk_to_int(duk,4)));
return 1; return 1;
case 87:
mini_music_play(duk_to_string(duk,1));
return 0;
case 88:
mini_music_pause();
return 0;
case 89:
mini_music_stop();
return 0;
case 90:
window_set_icon(duk_to_string(duk,1));
break;
} }
return 0; return 0;
@ -1353,22 +1371,19 @@ duk_ret_t duk_inflate_cpv(duk_context *duk)
{ {
cpVect *points = duk2cpvec2arr(duk,0); cpVect *points = duk2cpvec2arr(duk,0);
int n = duk_to_int(duk,1); int n = duk_to_int(duk,1);
float d = duk_to_number(duk,2); double d = duk_to_number(duk,2);
cpVect inflate_out[n]; cpVect inflate_out[n];
cpVect inflate_in[n]; cpVect inflate_in[n];
inflatepoints(inflate_out, points, d, n); inflatepoints(inflate_out, points, d, n);
inflatepoints(inflate_in, points, -d, n); inflatepoints(inflate_in, points, -d, n);
vecarr2duk(duk,inflate_out,n);
return 1;
duk_idx_t arr = duk_push_array(duk); duk_idx_t arr = duk_push_array(duk);
duk_idx_t out = vecarr2duk(duk, inflate_out, n); duk_idx_t out = vecarr2duk(duk, inflate_out, n);
duk_put_prop_index(duk,arr, out); duk_put_prop_index(duk,arr, 0);
duk_idx_t in = vecarr2duk(duk, inflate_in, n); duk_idx_t in = vecarr2duk(duk, inflate_in, n);
duk_put_prop_index(duk,arr,in); duk_put_prop_index(duk,arr,1);
return 1; return 1;
} }
@ -1450,7 +1465,7 @@ void ffi_load()
DUK_FUNC(cursor_text,5); DUK_FUNC(cursor_text,5);
DUK_FUNC(gui_img, 2); DUK_FUNC(gui_img, 2);
DUK_FUNC(inflate_cpv, 2); DUK_FUNC(inflate_cpv, 3);
DUK_FUNC(anim, 2); DUK_FUNC(anim, 2);
} }

92927
source/engine/miniaudio.h Normal file

File diff suppressed because it is too large Load diff

View file

@ -16,15 +16,7 @@
#include "mix.h" #include "mix.h"
#include "dsp.h" #include "dsp.h"
#define DR_WAV_IMPLEMENTATION #include "miniaudio.h"
#include "dr_wav.h"
#define DR_MP3_IMPLEMENTATION
#include "dr_mp3.h"
#include "portaudio.h"
#include "circbuf.h"
#define TSF_IMPLEMENTATION #define TSF_IMPLEMENTATION
#include "tsf.h" #include "tsf.h"
@ -88,23 +80,6 @@ static struct wav change_samplerate(struct wav w, int rate)
return w; return w;
} }
static int patestCallback(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
{
bus_fill_buffers(outputBuffer, framesPerBuffer);
return 0;
}
void check_pa_err(PaError e)
{
if (e != paNoError) {
YughError("PA Error: %s", Pa_GetErrorText(e));
exit(1);
}
}
static PaStream *stream_def;
void wav_norm_gain(struct wav *w, double lv) void wav_norm_gain(struct wav *w, double lv)
{ {
short tarmax = db2short(lv); short tarmax = db2short(lv);
@ -125,29 +100,19 @@ void wav_norm_gain(struct wav *w, double lv)
} }
} }
void print_devices() static ma_engine *engine;
{
int numDevices = Pa_GetDeviceCount();
const PaDeviceInfo *deviceInfo;
for (int i = 0; i < numDevices; i++) {
deviceInfo = Pa_GetDeviceInfo(i);
YughInfo("Device %i: channels %i, sample rate %f, name %s\n", i, deviceInfo->maxOutputChannels, deviceInfo->defaultSampleRate, deviceInfo->name);
}
}
void sound_init() void sound_init()
{ {
mixer_init(); ma_result result;
PaError err = Pa_Initialize(); engine = malloc(sizeof(*engine));
check_pa_err(err); result = ma_engine_init(NULL, engine);
if (result != MA_SUCCESS) {
err = Pa_OpenDefaultStream(&stream_def, 0, CHANNELS, paInt16, SAMPLERATE, BUF_FRAMES, patestCallback, NULL); return;
check_pa_err(err); }
return;
err = Pa_StartStream(stream_def);
check_pa_err(err); mixer_init();
} }
struct wav *make_sound(const char *wav) struct wav *make_sound(const char *wav)
@ -197,12 +162,43 @@ struct soundstream *soundstream_make()
return new; return new;
} }
void mini_sound(char *path)
{
ma_engine_play_sound(engine, path, NULL);
}
static ma_sound music_sound;
void mini_music_play(char *path)
{
int result = ma_sound_init_from_file(engine, path, MA_SOUND_FLAG_NO_SPATIALIZATION, NULL, NULL, &music_sound);
if (result != MA_SUCCESS) {
YughInfo("DID NOT LOAD SOUND!");
}
ma_sound_start(&music_sound);
}
void mini_music_pause()
{
ma_sound_stop(&music_sound);
}
void mini_music_stop()
{
ma_sound_stop(&music_sound);
}
void mini_master(float v)
{
ma_engine_set_volume(engine, v);
}
void kill_oneshot(struct sound *s) void kill_oneshot(struct sound *s)
{ {
free(s); free(s);
} }
void play_oneshot(struct wav *wav) { void play_oneshot(struct wav *wav) {
struct sound *new = malloc(sizeof(*new)); struct sound *new = malloc(sizeof(*new));
new->data = wav; new->data = wav;
new->bus = first_free_bus(dsp_filter(new, sound_fillbuf)); new->bus = first_free_bus(dsp_filter(new, sound_fillbuf));
@ -210,6 +206,7 @@ void play_oneshot(struct wav *wav) {
new->loop=0; new->loop=0;
new->frame = 0; new->frame = 0;
new->endcb = kill_oneshot; new->endcb = kill_oneshot;
} }
struct sound *play_sound(struct wav *wav) struct sound *play_sound(struct wav *wav)
@ -263,10 +260,10 @@ int sound_stopped(const struct sound *s)
struct mp3 make_music(const char *mp3) struct mp3 make_music(const char *mp3)
{ {
drmp3 new; // drmp3 new;
if (!drmp3_init_file(&new, mp3, NULL)) { // if (!drmp3_init_file(&new, mp3, NULL)) {
YughError("Could not open mp3 file %s.", mp3); // YughError("Could not open mp3 file %s.", mp3);
} // }
struct mp3 newmp3 = {}; struct mp3 newmp3 = {};
return newmp3; return newmp3;

View file

@ -56,6 +56,12 @@ void audio_close();
void sound_fillbuf(struct sound *s, short *buf, int n); void sound_fillbuf(struct sound *s, short *buf, int n);
void mini_sound(char *path);
void mini_master(float v);
void mini_music_play(char *path);
void mini_music_pause();
void mini_music_stop();
struct wav *make_sound(const char *wav); struct wav *make_sound(const char *wav);
void free_sound(const char *wav); void free_sound(const char *wav);
void wav_norm_gain(struct wav *w, double lv); void wav_norm_gain(struct wav *w, double lv);

View file

@ -37,7 +37,7 @@ void mixer_init() {
} }
struct bus *first_free_bus(struct dsp_filter in) { struct bus *first_free_bus(struct dsp_filter in) {
assert(initted); // assert(initted);
for (int i = 0; i < 255; i++) for (int i = 0; i < 255; i++)
if (!bus[i].on) { if (!bus[i].on) {

View file

@ -52,6 +52,7 @@ struct Texture *texture_pullfromfile(const char *path)
YughError("STBI failed to load file %s with message: %s", path, stbi_failure_reason()); YughError("STBI failed to load file %s with message: %s", path, stbi_failure_reason());
return NULL; return NULL;
} }
tex->data = data;
glGenTextures(1, &tex->id); glGenTextures(1, &tex->id);
@ -96,7 +97,7 @@ struct Texture *texture_pullfromfile(const char *path)
stbi_image_free(data); // stbi_image_free(data);
if (shlen(texhash) == 0) if (shlen(texhash) == 0)
sh_new_arena(texhash); sh_new_arena(texhash);

View file

@ -61,7 +61,7 @@ struct Texture {
unsigned int id; /* ID reference for the GPU memory location of the texture */ unsigned int id; /* ID reference for the GPU memory location of the texture */
int width; int width;
int height; int height;
unsigned char *data;
struct TextureOptions opts; struct TextureOptions opts;
struct TexAnim anim; struct TexAnim anim;
}; };

View file

@ -118,7 +118,8 @@ struct window *MakeSDLWindow(const char *name, int width, int height, uint32_t f
void window_set_icon(const char *png) void window_set_icon(const char *png)
{ {
icon = texture_pullfromfile(png); icon = texture_pullfromfile(png);
window_seticon(mainwin, icon);
} }
void window_destroy(struct window *w) void window_destroy(struct window *w)
@ -263,7 +264,7 @@ void window_seticon(struct window *w, struct Texture *icon)
static GLFWimage images[1]; static GLFWimage images[1];
images[0].width = icon->width; images[0].width = icon->width;
images[0].height = icon->height; images[0].height = icon->height;
//images[0].pixels = icon->data; images[0].pixels = icon->data;
glfwSetWindowIcon(w->window, 1, images); glfwSetWindowIcon(w->window, 1, images);
} }

View file

@ -8,6 +8,10 @@
#include "openglrender.h" #include "openglrender.h"
#include "gameobject.h" #include "gameobject.h"
#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"
#include "timer.h" #include "timer.h"
#include "script.h" #include "script.h"
@ -162,8 +166,6 @@ int main(int argc, char **args) {
renderMS = 1.0/vidmode->refreshRate; renderMS = 1.0/vidmode->refreshRate;
window_set_icon("icon.png");
if (ed) if (ed)
script_dofile("editor.js"); script_dofile("editor.js");
else else