prosperon/source/engine/sound/sound.c

328 lines
7.9 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"
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
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
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_IMPLEMENTATION
#include "qoa.h"
#endif
void short_to_float_array(const short *in, float *out, int frames, int channels)
{
for (int i = 0; i < frames * channels; i++)
out[i] = (float)in[i] / 32768.0f;
}
2023-01-15 11:16:25 -06:00
void float_to_short_array(float *in, short *out, int frames, int channels)
{
for (int i = 0; i < frames*channels; i++)
out[i] = (float)in[i]*32768;
}
void change_channels(struct pcm *w, int ch) {
2023-11-27 14:29:55 -06:00
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
void resample_pcm(soundbyte *in, soundbyte *out, int in_frames, int out_frames, int channels)
2023-11-27 14:29:55 -06:00
{
float ratio = (float)in_frames / out_frames;
for (int i = 0; i < out_frames; i++) {
// Find the position in the input buffer.
float in_pos = i * ratio;
int in_index = (int)in_pos; // Get the integer part of the position.
float frac = in_pos - in_index; // Get the fractional part for interpolation.
for (int ch = 0; ch < channels; ch++) {
// Linear interpolation between two input samples.
soundbyte sample1 = in[in_index * channels + ch];
soundbyte sample2 = in[(in_index + 1) * channels + ch];
out[i * channels + ch] = (soundbyte)((1.0f - frac) * sample1 + frac * sample2);
}
}
2022-07-05 15:12:48 -05:00
}
void change_samplerate(struct pcm *w, int rate) {
2023-11-27 14:29:55 -06:00
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_pcm(w->data, resampled, w->frames, outframes, w->ch);
2023-11-27 14:29:55 -06:00
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,
});
2024-04-10 16:21:46 -05:00
SAMPLERATE = saudio_sample_rate();
CHANNELS = saudio_channels();
BUF_FRAMES = saudio_buffer_frames();
2022-01-19 16:43:21 -06:00
}
struct pcm *make_pcm(const char *wav) {
if (!wav) return NULL;
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
size_t rawlen;
void *raw = slurp_file(wav, &rawlen);
if (!raw) {
YughWarn("Could not find file %s.", wav);
2023-10-09 13:03:12 -05:00
return NULL;
}
struct pcm *mwav = malloc(sizeof(*mwav));
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->ch;
2023-11-27 14:29:55 -06:00
mwav->data = malloc(sizeof(soundbyte) * mwav->frames * mwav->ch);
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
return mwav;
2023-01-15 11:16:25 -06:00
}
void pcm_format(pcm *pcm, int samplerate, int channels)
{
change_samplerate(pcm, samplerate);
change_channels(pcm, channels);
}
void save_qoa(char *file, pcm *pcm)
{
qoa_desc q;
short *out = malloc(sizeof(short)*pcm->ch*pcm->frames);
float_to_short_array(pcm->data, out, pcm->frames, pcm->ch);
q.channels = pcm->ch;
q.samples = pcm->frames;
q.samplerate = pcm->samplerate;
int encoded = qoa_write(file, out, &q);
free(out);
}
void save_wav(char *file, pcm *pcm)
{
drwav wav;
drwav_data_format fmt = {0};
fmt.format = DR_WAVE_FORMAT_PCM;
fmt.channels = pcm->ch;
fmt.sampleRate = pcm->samplerate;
fmt.bitsPerSample = 32;
drwav_int32 *out = malloc(sizeof(*out)*pcm->ch*pcm->frames);
drwav_f32_to_s32(out, pcm->data, pcm->frames*pcm->ch);
drwav_init_file_write_sequential_pcm_frames(&wav, file, &fmt, pcm->frames, NULL);
drwav_write_pcm_frames(&wav, pcm->frames, out);
drwav_uninit(&wav);
free(out);
}
2023-01-15 11:16:25 -06:00
void pcm_free(pcm *pcm)
{
free(pcm->data);
free(pcm);
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
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;
}
2022-07-19 15:13:15 -05:00
}
2023-11-27 14:29:55 -06:00
void free_source(struct sound *s)
{
2023-05-12 13:22:05 -05:00
free(s);
}
struct dsp_node *dsp_source(pcm *pcm)
2023-11-27 14:29:55 -06:00
{
if (!pcm) return NULL;
struct sound *self = malloc(sizeof(*self));
self->frame = 0;
self->data = pcm;
2023-11-27 14:29:55 -06:00
self->loop = false;
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
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));
}