prosperon/source/engine/sound.c

343 lines
7.5 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"
2022-07-28 14:07:20 -05:00
#include "stb_vorbis.h"
2023-05-12 13:22:05 -05:00
#include "string.h"
#include "time.h"
#include <stdlib.h>
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-05-12 13:22:05 -05:00
#include "mix.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
#define QOA_NO_STDIO
#define QOA_IMPLEMENTATION
#include "qoa.h"
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-05-12 13:22:05 -05:00
static struct wav change_channels(struct wav w, int ch) {
2023-08-30 18:22:32 -05:00
soundbyte *data = w.data;
2023-05-12 13:22:05 -05:00
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
if (ch > w.ch) {
/* Sets all new channels equal to the first one */
for (int i = 0; i < w.frames; i++) {
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 */
for (int i = 0; i < w.frames; i++)
for (int j = 0; j < ch; j++)
new[i * ch + j] = data[i * ch + j];
}
2022-07-12 15:43:02 -05:00
2023-05-12 13:22:05 -05:00
free(w.data);
w.data = new;
return w;
}
2022-07-12 15:43:02 -05:00
2023-05-12 13:22:05 -05:00
static struct wav change_samplerate(struct wav w, int rate) {
float ratio = (float)rate / w.samplerate;
int outframes = w.frames * ratio;
SRC_DATA ssrc;
2023-08-30 18:22:32 -05:00
soundbyte *resampled = calloc(w.ch*outframes,sizeof(soundbyte));
2022-07-05 15:12:48 -05:00
2023-08-30 18:22:32 -05:00
ssrc.data_in = w.data;
2023-05-12 13:22:05 -05:00
ssrc.data_out = resampled;
ssrc.input_frames = w.frames;
ssrc.output_frames = outframes;
ssrc.src_ratio = ratio;
2023-08-30 18:22:32 -05:00
int err = src_simple(&ssrc, SRC_LINEAR, w.ch);
if (err) {
YughError("Resampling error code %d: %s", err, src_strerror(err));
free(resampled);
return w;
}
2023-05-12 13:22:05 -05:00
free(w.data);
2023-08-30 18:22:32 -05:00
w.data = resampled;
w.frames = outframes;
2023-05-12 13:22:05 -05:00
w.samplerate = rate;
2022-07-05 15:12:48 -05:00
2023-05-12 13:22:05 -05:00
return w;
2022-07-05 15:12:48 -05:00
}
2023-05-12 13:22:05 -05:00
void wav_norm_gain(struct wav *w, double lv) {
short tarmax = db2short(lv);
short max = 0;
short *s = w->data;
for (int i = 0; i < w->frames; i++) {
for (int j = 0; j < w->ch; j++) {
max = (abs(s[i * w->ch + j]) > max) ? abs(s[i * w->ch + j]) : max;
2022-07-04 12:47:21 -05:00
}
2023-05-12 13:22:05 -05:00
}
2022-07-03 00:43:42 -05:00
2023-05-12 13:22:05 -05:00
float mult = (float)max / tarmax;
2022-07-12 15:43:02 -05:00
2023-05-12 13:22:05 -05:00
for (int i = 0; i < w->frames; i++) {
for (int j = 0; j < w->ch; j++) {
s[i * w->ch + j] *= mult;
2022-07-12 15:43:02 -05:00
}
2023-05-12 13:22:05 -05:00
}
2022-07-04 12:47:21 -05:00
}
2022-07-03 00:43:42 -05:00
2023-08-30 18:22:32 -05:00
void push_sound(soundbyte *buffer, int frames, int chan)
{
bus_fill_buffers(buffer, frames*chan);
}
2022-07-03 00:43:42 -05:00
2023-05-12 13:22:05 -05:00
void sound_init() {
2023-10-12 17:05:49 -05:00
mixer_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-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-05-12 13:22:05 -05:00
struct wav mwav;
long 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"))
mwav.data = drwav_open_memory_and_read_pcm_frames_f32(raw, rawlen, &mwav.ch, &mwav.samplerate, &mwav.frames, NULL);
#ifndef NFLAC
else if (!strcmp(ext, "flac"))
mwav.data = drflac_open_memory_and_read_pcm_frames_f32(raw, rawlen, &mwav.ch, &mwav.samplerate, &mwav.frames, NULL);
#endif
#ifndef NMP3
else if (!strcmp(ext, "mp3")) {
2023-08-30 18:22:32 -05:00
drmp3_config cnf;
mwav.data = drmp3_open_memory_and_read_pcm_frames_f32(raw, rawlen, &cnf, &mwav.frames, NULL);
2023-08-30 18:22:32 -05:00
mwav.ch = cnf.channels;
mwav.samplerate = cnf.sampleRate;
}
#endif
else if (!strcmp(ext, "qoa")) {
qoa_desc qoa;
short *qoa_data = qoa_decode(raw, rawlen, &qoa);
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);
2023-08-30 18:22:32 -05:00
} else {
YughWarn("Cannot process file type '%s'.", ext);
free (raw);
2023-08-30 18:22:32 -05:00
return NULL;
}
free(raw);
2022-07-03 00:43:42 -05:00
2023-09-11 02:46:12 -05:00
if (mwav.samplerate != SAMPLERATE)
2023-08-30 18:22:32 -05:00
mwav = change_samplerate(mwav, SAMPLERATE);
2022-01-19 16:43:21 -06:00
2023-09-11 02:46:12 -05:00
if (mwav.ch != CHANNELS)
2023-05-12 13:22:05 -05:00
mwav = change_channels(mwav, CHANNELS);
2023-05-12 13:22:05 -05:00
mwav.gain = 1.f;
struct wav *newwav = malloc(sizeof(*newwav));
*newwav = mwav;
if (shlen(wavhash) == 0) sh_new_arena(wavhash);
shput(wavhash, wav, newwav);
2023-01-15 11:16:25 -06:00
2023-05-12 13:22:05 -05:00
return newwav;
2023-01-15 11:16:25 -06:00
}
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-05-12 13:22:05 -05:00
struct soundstream *soundstream_make() {
struct soundstream *new = malloc(sizeof(*new));
2023-08-30 18:22:32 -05:00
new->buf = circbuf_make(sizeof(short), BUF_FRAMES * CHANNELS * 2);
2023-05-12 13:22:05 -05:00
return new;
2022-07-19 15:13:15 -05:00
}
2023-05-12 13:22:05 -05:00
void kill_oneshot(struct sound *s) {
free(s);
}
2023-03-24 14:01:01 -05:00
void play_oneshot(struct wav *wav) {
2023-05-12 13:22:05 -05:00
struct sound *new = malloc(sizeof(*new));
new->data = wav;
new->bus = first_free_bus(dsp_filter(new, sound_fillbuf));
new->playing = 1;
new->loop = 0;
new->frame = 0;
new->endcb = kill_oneshot;
2023-01-15 11:16:25 -06:00
}
2023-05-12 13:22:05 -05:00
struct sound *play_sound(struct wav *wav) {
struct sound *new = calloc(1, sizeof(*new));
new->data = wav;
new->bus = first_free_bus(dsp_filter(new, sound_fillbuf));
new->playing = 1;
new->loop = 0;
new->frame = 0;
new->endcb = kill_oneshot;
2023-05-12 13:22:05 -05:00
return new;
2022-01-19 16:43:21 -06:00
}
2023-05-12 13:22:05 -05:00
int sound_playing(const struct sound *s) {
return !sound_paused(s);
2022-07-12 15:43:02 -05:00
}
2023-05-12 13:22:05 -05:00
int sound_paused(const struct sound *s) {
return s->bus == NULL;
2022-07-12 15:43:02 -05:00
}
2023-05-12 13:22:05 -05:00
void sound_pause(struct sound *s) {
if (s->bus == NULL) return;
2023-05-12 13:22:05 -05:00
bus_free(s->bus);
s->bus = NULL;
2022-07-12 15:43:02 -05:00
}
2023-05-12 13:22:05 -05:00
void sound_resume(struct sound *s) {
if (s->bus != NULL) return;
2023-05-12 13:22:05 -05:00
s->bus = first_free_bus(dsp_filter(s, sound_fillbuf));
2022-07-12 15:43:02 -05:00
}
2023-05-12 13:22:05 -05:00
void sound_stop(struct sound *s) {
sound_pause(s);
2023-05-12 13:22:05 -05:00
s->frame = 0;
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
int sound_stopped(const struct sound *s) {
return s->bus == NULL;
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
void close_audio_device(int device) {
2022-02-06 10:14:57 -06:00
}
2023-05-12 13:22:05 -05:00
int open_device(const char *adriver) {
return 0;
2022-02-06 10:14:57 -06:00
}
2022-07-06 17:17:06 -05:00
2023-08-30 18:22:32 -05:00
void sound_fillbuf(struct sound *s, soundbyte *buf, int n) {
2023-05-12 13:22:05 -05:00
float gainmult = pct2mult(s->data->gain);
2022-07-12 15:43:02 -05:00
2023-08-30 18:22:32 -05:00
soundbyte *in = s->data->data;
2023-05-12 13:22:05 -05:00
for (int i = 0; i < n; i++) {
for (int j = 0; j < CHANNELS; j++)
2023-08-30 18:22:32 -05:00
buf[i * CHANNELS + j] = in[s->frame*CHANNELS + j] * gainmult;
2023-05-12 13:22:05 -05:00
s->frame++;
2023-08-30 18:22:32 -05:00
2023-05-12 13:22:05 -05:00
if (s->frame == s->data->frames) {
sound_stop(s);
2023-05-12 13:22:05 -05:00
s->endcb(s);
return;
2022-07-06 17:17:06 -05:00
}
2023-05-12 13:22:05 -05:00
}
2022-07-06 17:17:06 -05:00
}
2023-08-30 18:22:32 -05:00
void mp3_fillbuf(struct sound *s, soundbyte *buf, int n) {
2022-07-12 15:43:02 -05:00
}
2023-08-30 18:22:32 -05:00
void soundstream_fillbuf(struct soundstream *s, soundbyte *buf, int n) {
2023-05-30 11:39:22 -05:00
int max = 1;//s->buf->write - s->buf->read;
2023-05-12 13:22:05 -05:00
int lim = (max < n * CHANNELS) ? max : n * CHANNELS;
for (int i = 0; i < lim; i++) {
2023-05-30 11:39:22 -05:00
// buf[i] = cbuf_shift(s->buf);
2023-05-12 13:22:05 -05:00
}
2022-07-06 17:17:06 -05: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-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));
}