prosperon/source/engine/sound.c

311 lines
6.6 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
2023-03-24 14:01:01 -05:00
#include "miniaudio.h"
2022-07-03 11:28:44 -05:00
2022-07-10 13:04:24 -05:00
#define TSF_IMPLEMENTATION
#include "tsf.h"
2022-06-23 02:34:51 -05:00
2022-07-10 13:04:24 -05:00
#define TML_IMPLEMENTATION
#include "tml.h"
2022-07-05 17:24:58 -05:00
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) {
short *data = w.data;
int samples = ch * w.frames;
short *new = malloc(sizeof(short) * samples);
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;
float floatdata[w.frames * w.ch];
src_short_to_float_array(w.data, floatdata, w.frames * w.ch);
float resampled[w.ch * outframes];
2022-07-05 15:12:48 -05:00
2023-05-12 13:22:05 -05:00
ssrc.data_in = floatdata;
ssrc.data_out = resampled;
ssrc.input_frames = w.frames;
ssrc.output_frames = outframes;
ssrc.src_ratio = ratio;
2023-05-12 13:22:05 -05:00
src_simple(&ssrc, SRC_SINC_BEST_QUALITY, w.ch);
2023-05-12 13:22:05 -05:00
short *newdata = malloc(sizeof(short) * outframes * w.ch);
src_float_to_short_array(resampled, newdata, outframes * w.ch);
2022-07-05 15:12:48 -05:00
2023-05-12 13:22:05 -05:00
free(w.data);
w.data = newdata;
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-03-24 14:01:01 -05:00
static ma_engine *engine;
2022-07-03 00:43:42 -05:00
2023-05-12 13:22:05 -05:00
void sound_init() {
2023-03-24 14:01:01 -05:00
ma_result result;
engine = malloc(sizeof(*engine));
result = ma_engine_init(NULL, engine);
if (result != MA_SUCCESS) {
return;
}
return;
2023-05-12 13:22:05 -05:00
2023-03-24 14:01:01 -05:00
mixer_init();
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-01-15 11:16:25 -06:00
2023-05-12 13:22:05 -05:00
struct wav mwav;
// mwav.data = drwav_open_file_and_read_pcm_frames_s16(wav, &mwav.ch, &mwav.samplerate, &mwav.frames, NULL);
2022-07-03 00:43:42 -05:00
2023-05-12 13:22:05 -05:00
if (mwav.samplerate != SAMPLERATE) {
YughInfo("Changing samplerate of %s from %d to %d.", wav, mwav.samplerate, SAMPLERATE);
// mwav = change_samplerate(mwav, SAMPLERATE);
}
2022-01-19 16:43:21 -06:00
2023-05-12 13:22:05 -05:00
if (mwav.ch != CHANNELS) {
YughInfo("Changing channels of %s from %d to %d.", wav, mwav.ch, CHANNELS);
mwav = change_channels(mwav, CHANNELS);
}
2023-05-12 13:22:05 -05:00
mwav.gain = 1.f;
2022-01-19 16:43:21 -06:00
2023-05-12 13:22:05 -05:00
struct wav *newwav = malloc(sizeof(*newwav));
*newwav = mwav;
2023-01-15 11:16:25 -06:00
2023-05-12 13:22:05 -05:00
if (shlen(wavhash) == 0) sh_new_arena(wavhash);
2023-01-15 11:16:25 -06:00
2023-05-12 13:22:05 -05:00
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-05-30 11:39:22 -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 mini_sound(char *path) {
2023-03-24 14:01:01 -05:00
ma_engine_play_sound(engine, path, NULL);
}
static ma_sound music_sound;
2023-05-12 13:22:05 -05:00
void mini_music_play(char *path) {
2023-04-07 12:52:35 -05:00
ma_sound_uninit(&music_sound);
2023-03-24 14:01:01 -05:00
int result = ma_sound_init_from_file(engine, path, MA_SOUND_FLAG_NO_SPATIALIZATION, NULL, NULL, &music_sound);
if (result != MA_SUCCESS) {
2023-04-07 12:52:35 -05:00
YughInfo("Could not load music at path: %s", path);
2023-03-24 14:01:01 -05:00
}
2023-04-07 12:52:35 -05:00
YughInfo("Loading %s...", path);
2023-03-24 14:01:01 -05:00
ma_sound_start(&music_sound);
}
2023-05-12 13:22:05 -05:00
void mini_music_pause() {
2023-03-24 14:01:01 -05:00
ma_sound_stop(&music_sound);
}
2023-05-12 13:22:05 -05:00
void mini_music_stop() {
2023-03-24 14:01:01 -05:00
ma_sound_stop(&music_sound);
}
2023-05-12 13:22:05 -05:00
void mini_master(float v) {
2023-03-24 14:01:01 -05:00
ma_engine_set_volume(engine, v);
}
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;
2022-01-19 16:43:21 -06:00
2023-05-12 13:22:05 -05:00
new->bus = first_free_bus(dsp_filter(new, sound_fillbuf));
new->playing = 1;
2022-01-19 16:43:21 -06:00
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 s->playing;
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->playing && s->frame < s->data->frames);
2022-07-12 15:43:02 -05:00
}
2023-05-12 13:22:05 -05:00
void sound_pause(struct sound *s) {
s->playing = 0;
bus_free(s->bus);
2022-07-12 15:43:02 -05:00
}
2023-05-12 13:22:05 -05:00
void sound_resume(struct sound *s) {
s->playing = 1;
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) {
s->playing = 0;
s->frame = 0;
bus_free(s->bus);
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->playing && 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->playing && s->frame == 0;
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-05-12 13:22:05 -05:00
void sound_fillbuf(struct sound *s, short *buf, int n) {
float gainmult = pct2mult(s->data->gain);
2022-07-12 15:43:02 -05:00
2023-05-12 13:22:05 -05:00
short *in = s->data->data;
for (int i = 0; i < n; i++) {
for (int j = 0; j < CHANNELS; j++)
buf[i * CHANNELS + j] = in[s->frame + j] * gainmult;
s->frame++;
if (s->frame == s->data->frames) {
2022-07-12 15:43:02 -05:00
2023-05-12 13:22:05 -05:00
bus_free(s->bus);
s->bus = NULL;
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-05-12 13:22:05 -05:00
void mp3_fillbuf(struct sound *s, short *buf, int n) {
2022-07-12 15:43:02 -05:00
}
2023-05-12 13:22:05 -05:00
void soundstream_fillbuf(struct soundstream *s, short *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));
}