prosperon/source/engine/sound/sound.c

359 lines
8.2 KiB
C
Raw Normal View History

2022-01-19 16:43:21 -06:00
#include "sound.h"
2023-05-12 13:22:05 -05:00
#include "limits.h"
2022-06-30 10:31:23 -05:00
#include "log.h"
2022-07-04 12:47:21 -05:00
#include "math.h"
2022-07-10 11:32:21 -05:00
#include "music.h"
2023-05-12 13:22:05 -05:00
#include "resources.h"
#include "string.h"
#include "time.h"
#include <stdlib.h>
2023-12-18 17:12:05 -06:00
#include "pthread.h"
#include "debug.h"
2024-01-14 10:24:31 -06:00
#include "jsffi.h"
2023-12-18 17:12:05 -06:00
pthread_mutex_t soundrun = PTHREAD_MUTEX_INITIALIZER;
2022-06-23 02:34:51 -05:00
#include "samplerate.h"
2023-01-15 11:16:25 -06:00
#include "stb_ds.h"
2022-07-04 14:19:52 -05:00
#include "dsp.h"
2023-11-27 14:29:55 -06:00
#define POCKETMOD_IMPLEMENTATION
#include "pocketmod.h"
2022-07-04 14:19:52 -05:00
#include "sokol/sokol_audio.h"
2022-07-03 11:28:44 -05:00
#define TSF_NO_STDIO
2022-07-10 13:04:24 -05:00
#define TSF_IMPLEMENTATION
#include "tsf.h"
2022-06-23 02:34:51 -05:00
#define TML_NO_STDIO
2022-07-10 13:04:24 -05:00
#define TML_IMPLEMENTATION
#include "tml.h"
2022-07-05 17:24:58 -05:00
#define DR_WAV_NO_STDIO
2023-08-30 18:22:32 -05:00
#define DR_WAV_IMPLEMENTATION
#include "dr_wav.h"
#ifndef NFLAC
2023-08-30 18:22:32 -05:00
#define DR_FLAC_IMPLEMENTATION
#define DR_FLAC_NO_STDIO
2023-08-30 18:22:32 -05:00
#include "dr_flac.h"
#endif
2023-08-30 18:22:32 -05:00
#ifndef NMP3
#define DR_MP3_NO_STDIO
2023-08-30 18:22:32 -05:00
#define DR_MP3_IMPLEMENTATION
#include "dr_mp3.h"
#endif
2023-08-30 18:22:32 -05:00
#ifndef NQOA
#define QOA_NO_STDIO
#define QOA_IMPLEMENTATION
#include "qoa.h"
#endif
2023-01-15 11:16:25 -06:00
static struct {
2023-05-12 13:22:05 -05:00
char *key;
struct wav *value;
2023-01-15 11:16:25 -06:00
} *wavhash = NULL;
2023-11-27 14:29:55 -06:00
void change_channels(struct wav *w, int ch) {
if (w->ch == ch) return;
soundbyte *data = w->data;
int samples = ch * w->frames;
2023-08-30 18:22:32 -05:00
soundbyte *new = malloc(sizeof(soundbyte) * samples);
2023-05-12 13:22:05 -05:00
2023-11-27 14:29:55 -06:00
if (ch > w->ch) {
2023-05-12 13:22:05 -05:00
/* Sets all new channels equal to the first one */
2023-11-27 14:29:55 -06:00
for (int i = 0; i < w->frames; i++) {
2023-05-12 13:22:05 -05:00
for (int j = 0; j < ch; j++)
new[i * ch + j] = data[i];
}
2023-05-12 13:22:05 -05:00
} else {
/* Simple method; just use first N channels present in wav */
2023-11-27 14:29:55 -06:00
for (int i = 0; i < w->frames; i++)
2023-05-12 13:22:05 -05:00
for (int j = 0; j < ch; j++)
new[i * ch + j] = data[i * ch + j];
}
2022-07-12 15:43:02 -05:00
2023-11-27 14:29:55 -06:00
free(w->data);
w->data = new;
}
2022-07-12 15:43:02 -05:00
2023-11-27 14:29:55 -06:00
void resample(soundbyte *in, soundbyte *out, int in_frames, int out_frames, int channels)
{
float ratio = (float)in_frames/out_frames;
2023-05-12 13:22:05 -05:00
SRC_DATA ssrc;
2023-11-27 14:29:55 -06:00
ssrc.data_in = in;
ssrc.data_out = out;
ssrc.input_frames = in_frames;
ssrc.output_frames = out_frames;
2023-05-12 13:22:05 -05:00
ssrc.src_ratio = ratio;
2023-11-27 14:29:55 -06:00
int err = src_simple(&ssrc, SRC_LINEAR, channels);
if (err)
2023-08-30 18:22:32 -05:00
YughError("Resampling error code %d: %s", err, src_strerror(err));
2022-07-05 15:12:48 -05:00
}
2023-11-27 14:29:55 -06:00
void change_samplerate(struct wav *w, int rate) {
if (rate == w->samplerate) return;
float ratio = (float)rate / w->samplerate;
int outframes = w->frames * ratio;
soundbyte *resampled = malloc(w->ch*outframes*sizeof(soundbyte));
resample(w->data, resampled, w->frames, outframes, w->ch);
free(w->data);
w->data = resampled;
w->frames = outframes;
w->samplerate = rate;
}
2022-07-03 00:43:42 -05:00
2023-12-18 17:12:05 -06:00
void push_sound(soundbyte *buffer, int frames, int chan) {
pthread_mutex_lock(&soundrun);
2023-11-27 14:29:55 -06:00
set_soundbytes(buffer, dsp_node_out(masterbus), frames*chan);
2023-12-18 17:12:05 -06:00
pthread_mutex_unlock(&soundrun);
2023-11-27 14:29:55 -06:00
}
2022-07-12 15:43:02 -05:00
2023-11-27 14:29:55 -06:00
void filter_mod(pocketmod_context *mod, soundbyte *buffer, int frames)
{
pocketmod_render(mod, buffer, frames*CHANNELS*sizeof(soundbyte));
2022-07-04 12:47:21 -05:00
}
2022-07-03 00:43:42 -05:00
2023-11-27 14:29:55 -06:00
dsp_node *dsp_mod(const char *path)
{
size_t modsize;
2023-11-27 14:29:55 -06:00
void *data = slurp_file(path, &modsize);
pocketmod_context *mod = malloc(sizeof(*mod));
pocketmod_init(mod, data, modsize, SAMPLERATE);
2023-11-28 22:48:32 -06:00
return make_node(mod, filter_mod, NULL);
}
2022-07-03 00:43:42 -05:00
2023-05-12 13:22:05 -05:00
void sound_init() {
2023-11-27 14:29:55 -06:00
dsp_init();
saudio_setup(&(saudio_desc){
.stream_cb = push_sound,
.sample_rate = SAMPLERATE,
.num_channels = CHANNELS,
.buffer_frames = BUF_FRAMES,
.logger.func = sg_logging,
});
2022-01-19 16:43:21 -06:00
}
2023-11-27 14:29:55 -06:00
typedef struct {
int channels;
int samplerate;
void *f;
} stream;
void mp3_filter(stream *mp3, soundbyte *buffer, int frames)
{
if (mp3->samplerate == SAMPLERATE) {
drmp3_read_pcm_frames_f32(mp3->f, frames, buffer);
return;
}
int in_frames = (float)mp3->samplerate/SAMPLERATE;
soundbyte *decode = malloc(sizeof(*decode)*in_frames*mp3->channels);
drmp3_read_pcm_frames_f32(mp3->f, in_frames, decode);
resample(decode, buffer, in_frames, frames, CHANNELS);
}
2023-05-12 13:22:05 -05:00
struct wav *make_sound(const char *wav) {
int index = shgeti(wavhash, wav);
if (index != -1)
return wavhash[index].value;
2023-08-30 18:22:32 -05:00
char *ext = strrchr(wav, '.')+1;
if(!ext) {
YughWarn("No extension detected for %s.", wav);
return NULL;
}
2023-01-15 11:16:25 -06:00
2023-11-27 14:29:55 -06:00
struct wav *mwav = malloc(sizeof(*mwav));
size_t rawlen;
void *raw = slurp_file(wav, &rawlen);
if (!raw) {
YughError("Could not find file %s.", wav);
2023-10-09 13:03:12 -05:00
return NULL;
}
2023-08-30 18:22:32 -05:00
if (!strcmp(ext, "wav"))
2023-11-27 14:29:55 -06:00
mwav->data = drwav_open_memory_and_read_pcm_frames_f32(raw, rawlen, &mwav->ch, &mwav->samplerate, &mwav->frames, NULL);
else if (!strcmp(ext, "flac")) {
#ifndef NFLAC
2023-11-27 14:29:55 -06:00
mwav->data = drflac_open_memory_and_read_pcm_frames_f32(raw, rawlen, &mwav->ch, &mwav->samplerate, &mwav->frames, NULL);
#else
YughWarn("Could not load %s because Primum was built without FLAC support.", wav);
#endif
}
else if (!strcmp(ext, "mp3")) {
#ifndef NMP3
2023-08-30 18:22:32 -05:00
drmp3_config cnf;
2023-11-27 14:29:55 -06:00
mwav->data = drmp3_open_memory_and_read_pcm_frames_f32(raw, rawlen, &cnf, &mwav->frames, NULL);
mwav->ch = cnf.channels;
mwav->samplerate = cnf.sampleRate;
#else
YughWarn("Could not load %s because Primum was built without MP3 support.", wav);
#endif
}
else if (!strcmp(ext, "qoa")) {
#ifndef NQOA
qoa_desc qoa;
short *qoa_data = qoa_decode(raw, rawlen, &qoa);
2023-11-27 14:29:55 -06:00
mwav->ch = qoa.channels;
mwav->samplerate = qoa.samplerate;
mwav->frames = qoa.samples;
mwav->data = malloc(sizeof(soundbyte) * mwav->frames * mwav->ch);
src_short_to_float_array(qoa_data, mwav->data, mwav->frames*mwav->ch);
free(qoa_data);
#else
YughWarn("Could not load %s because Primum was built without QOA support.", wav);
#endif
2023-08-30 18:22:32 -05:00
} else {
YughWarn("File with unknown type '%s'.", wav);
free (raw);
2023-11-27 14:29:55 -06:00
free(mwav);
2023-08-30 18:22:32 -05:00
return NULL;
}
free(raw);
2022-07-03 00:43:42 -05:00
2023-11-27 14:29:55 -06:00
change_samplerate(mwav, SAMPLERATE);
change_channels(mwav, CHANNELS);
2023-05-12 13:22:05 -05:00
if (shlen(wavhash) == 0) sh_new_arena(wavhash);
2023-11-27 14:29:55 -06:00
shput(wavhash, wav, mwav);
2023-01-15 11:16:25 -06:00
2023-11-27 14:29:55 -06:00
return mwav;
2023-01-15 11:16:25 -06:00
}
void save_qoa(char *file)
{
wav *wav = make_sound(file);
qoa_desc q;
q.channels = wav->ch;
q.samples = wav->frames;
q.samplerate = wav->samplerate;
unsigned int len;
void *raw = qoa_encode(wav->data, &q, &len);
file = str_replace_ext(file, ".qoa");
slurp_write(raw, file, len);
free(raw);
free_sound(wav);
}
2023-05-12 13:22:05 -05:00
void free_sound(const char *wav) {
struct wav *w = shget(wavhash, wav);
if (w == NULL) return;
2023-01-15 11:16:25 -06:00
2023-05-12 13:22:05 -05:00
free(w->data);
free(w);
shdel(wavhash, wav);
2022-01-19 16:43:21 -06:00
}
2023-11-27 14:29:55 -06:00
void sound_fillbuf(struct sound *s, soundbyte *buf, int n) {
int frames = s->data->frames - s->frame;
if (frames == 0) return;
int end = 0;
if (frames > n)
frames = n;
else
end = 1;
2023-11-28 22:48:32 -06:00
if (s->timescale != 1) {
src_callback_read(s->src, s->timescale, frames, buf);
return;
}
2023-11-27 14:29:55 -06:00
soundbyte *in = s->data->data;
for (int i = 0; i < frames; i++) {
for (int j = 0; j < CHANNELS; j++)
buf[i * CHANNELS + j] = in[s->frame*CHANNELS + j];
2023-11-28 22:48:32 -06:00
s->frame++;
2023-11-27 14:29:55 -06:00
}
if(end) {
if (s->loop)
s->frame = 0;
2024-01-14 10:24:31 -06:00
script_call_sym(s->hook,0,NULL);
2023-11-27 14:29:55 -06:00
}
2022-07-19 15:13:15 -05:00
}
2023-11-27 14:29:55 -06:00
void free_source(struct sound *s)
{
JS_FreeValue(js, s->hook);
2023-11-28 22:48:32 -06:00
src_delete(s->src);
2023-05-12 13:22:05 -05:00
free(s);
}
2023-12-11 16:59:59 -06:00
static long src_cb(struct sound *s, float **data)
2023-11-28 22:48:32 -06:00
{
long needed = BUF_FRAMES/s->timescale;
*data = s->data->data+s->frame;
s->frame += needed;
return needed;
}
struct dsp_node *dsp_source(const char *path)
2023-11-27 14:29:55 -06:00
{
struct sound *self = malloc(sizeof(*self));
self->frame = 0;
2023-11-27 14:29:55 -06:00
self->data = make_sound(path);
self->loop = false;
2023-11-28 22:48:32 -06:00
self->src = src_callback_new(src_cb, SRC_SINC_MEDIUM_QUALITY, 2, NULL, self);
self->timescale = 1;
2023-11-27 14:29:55 -06:00
self->hook = JS_UNDEFINED;
2023-11-28 22:48:32 -06:00
dsp_node *n = make_node(self, sound_fillbuf, free_source);
2023-11-27 14:29:55 -06:00
return n;
2022-07-12 15:43:02 -05:00
}
2023-05-12 13:22:05 -05:00
int sound_finished(const struct sound *s) {
return s->frame == s->data->frames;
2022-07-12 15:43:02 -05:00
}
2023-05-12 13:22:05 -05:00
struct mp3 make_music(const char *mp3) {
// drmp3 new;
// if (!drmp3_init_file(&new, mp3, NULL)) {
// YughError("Could not open mp3 file %s.", mp3);
// }
2022-01-19 16:43:21 -06:00
2023-05-12 13:22:05 -05:00
struct mp3 newmp3 = {};
return newmp3;
2022-02-06 10:14:57 -06:00
}
2023-05-12 13:22:05 -05:00
float short2db(short val) {
return 20 * log10(abs(val) / SHRT_MAX);
2022-07-11 23:21:57 -05:00
}
2023-05-12 13:22:05 -05:00
short db2short(float db) {
return pow(10, db / 20.f) * SHRT_MAX;
2022-07-11 23:21:57 -05:00
}
2023-05-12 13:22:05 -05:00
short short_gain(short val, float db) {
return (short)(pow(10, db / 20.f) * val);
2022-07-11 23:21:57 -05:00
}
2023-11-27 14:29:55 -06:00
float float2db(float val) { return 20 * log10(fabsf(val)); }
float db2float(float db) { return pow(10, db/20); }
float fgain(float val, float db) {
return pow(10,db/20.f)*val;
}
2023-05-12 13:22:05 -05:00
float pct2db(float pct) {
if (pct <= 0) return -72.f;
2022-07-11 23:21:57 -05:00
2023-05-12 13:22:05 -05:00
return 10 * log2(pct);
2022-07-12 15:43:02 -05:00
}
2023-05-12 13:22:05 -05:00
float pct2mult(float pct) {
if (pct <= 0) return 0.f;
2022-07-12 15:43:02 -05:00
2023-05-12 13:22:05 -05:00
return pow(10, 0.5 * log2(pct));
}