prosperon/source/engine/sound.c

333 lines
7 KiB
C
Raw Normal View History

2022-01-19 16:43:21 -06:00
#include "sound.h"
2022-02-06 10:14:57 -06:00
#include "resources.h"
2022-06-28 18:51:21 -05:00
#include <stdlib.h>
2022-06-30 10:31:23 -05:00
#include "log.h"
2022-07-03 00:43:42 -05:00
#include "string.h"
2022-07-04 12:47:21 -05:00
#include "math.h"
#include "limits.h"
#include "time.h"
2022-07-10 11:32:21 -05:00
#include "music.h"
2022-07-28 14:07:20 -05:00
#include "stb_vorbis.h"
2022-06-23 02:34:51 -05:00
2022-07-19 15:13:15 -05:00
2022-07-05 15:12:48 -05:00
#include "SDL2/SDL.h"
2022-07-05 17:24:58 -05:00
#include "mix.h"
2022-07-04 14:19:52 -05:00
#include "dsp.h"
2022-07-03 00:43:42 -05:00
#define DR_WAV_IMPLEMENTATION
#include "dr_wav.h"
#define DR_MP3_IMPLEMENTATION
#include "dr_mp3.h"
#include "portaudio.h"
2022-07-03 11:28:44 -05:00
#include "circbuf.h"
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
2022-01-19 16:43:21 -06:00
const char *audioDriver;
2022-07-12 15:43:02 -05:00
void new_samplerate(short *in, short *out, int n, int ch, int sr_in, int sr_out)
{
SDL_AudioStream *stream = SDL_NewAudioStream(AUDIO_S16, ch, sr_in, AUDIO_S16, ch, sr_out);
SDL_AudioStreamPut(stream, in, n * ch * sizeof(short));
SDL_AudioStreamGet(stream, out, n * ch * sizeof(short));
SDL_FreeAudioStream(stream);
}
2022-07-05 15:12:48 -05:00
struct wav change_samplerate(struct wav w, int rate)
{
2022-07-19 15:13:15 -05:00
//int samples = sizeof(short) * w.ch * w.frames;
//short *new = malloc(samples);
//new_samplerate(w.data, new,
2022-07-12 15:43:02 -05:00
2022-07-05 17:24:58 -05:00
SDL_AudioStream *stream = SDL_NewAudioStream(AUDIO_S16, w.ch, w.samplerate, AUDIO_S16, w.ch, rate);
SDL_AudioStreamPut(stream, w.data, w.frames*w.ch*sizeof(short));
2022-07-05 15:12:48 -05:00
2022-07-05 17:24:58 -05:00
int oldframes = w.frames;
w.frames *= (float)rate/w.samplerate;
2022-07-19 15:13:15 -05:00
int samples = sizeof(short) * w.ch * w.frames;
2022-07-05 15:12:48 -05:00
w.samplerate = rate;
2022-07-05 17:24:58 -05:00
short *new = malloc(samples);
SDL_AudioStreamGet(stream, new, samples);
2022-07-05 15:12:48 -05:00
2022-07-05 17:24:58 -05:00
free(w.data);
2022-07-05 15:12:48 -05:00
w.data = new;
2022-07-05 17:24:58 -05:00
SDL_FreeAudioStream(stream);
2022-07-05 15:12:48 -05:00
2022-07-05 17:24:58 -05:00
return w;
2022-07-05 15:12:48 -05:00
}
2022-07-03 00:43:42 -05:00
static int patestCallback(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
{
2022-07-03 11:28:44 -05:00
short *out = (short*)outputBuffer;
2022-07-04 12:47:21 -05:00
2022-07-06 17:17:06 -05:00
bus_fill_buffers(outputBuffer, framesPerBuffer);
2022-07-05 17:24:58 -05:00
2022-07-03 00:43:42 -05:00
return 0;
}
void check_pa_err(PaError e)
2022-06-23 02:34:51 -05:00
{
2022-07-03 00:43:42 -05:00
if (e != paNoError) {
YughError("PA Error: %s", Pa_GetErrorText(e));
exit(1);
}
2022-06-23 02:34:51 -05:00
}
2022-01-19 16:43:21 -06:00
2022-07-03 00:43:42 -05:00
static PaStream *stream_def;
2022-07-12 15:43:02 -05:00
void wav_norm_gain(struct wav *w, double lv)
2022-07-04 12:47:21 -05:00
{
2022-07-11 23:21:57 -05:00
short tarmax = db2short(lv);
2022-07-04 12:47:21 -05:00
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-03 00:43:42 -05:00
2022-07-12 15:43:02 -05:00
float mult = (float)max / tarmax;
for (int i = 0; i < w->frames; i++) {
for (int j = 0; j < w->ch; j++) {
s[i*w->ch + j] *= mult;
}
}
2022-07-04 12:47:21 -05:00
}
2022-07-03 00:43:42 -05:00
2022-07-06 17:17:06 -05:00
struct osc sin600;
struct osc sin20;
struct dsp_ammod dspammod;
struct dsp_delay dspdel;
struct wav s600wav;
struct sound s600wavsound;
2022-07-03 19:24:53 -05:00
void sound_init()
{
2022-07-06 17:17:06 -05:00
PaError err = Pa_Initialize();
2022-07-03 00:43:42 -05:00
check_pa_err(err);
int numDevices = Pa_GetDeviceCount();
const PaDeviceInfo *deviceInfo;
for (int i = 0; i < numDevices; i++) {
deviceInfo = Pa_GetDeviceInfo(i);
2022-07-03 19:24:53 -05:00
// printf("Device %i: channels %i, sample rate %f, name %s\n", i, deviceInfo->maxOutputChannels, deviceInfo->defaultSampleRate, deviceInfo->name);
2022-07-03 00:43:42 -05:00
}
PaStreamParameters outparams;
2022-07-03 11:28:44 -05:00
/*
2022-07-03 00:43:42 -05:00
outparams.channelCount = 2;
2022-07-03 11:28:44 -05:00
outparams.device = 19;
outparams.sampleFormat = paInt16;
2022-07-03 00:43:42 -05:00
outparams.suggestedLatency = Pa_GetDeviceInfo(outparams.device)->defaultLowOutputLatency;
outparams.hostApiSpecificStreamInfo = NULL;
2022-07-03 11:28:44 -05:00
*/
2022-07-03 00:43:42 -05:00
2022-07-03 11:28:44 -05:00
//err = Pa_OpenStream(&stream_def, NULL, &outparams, 48000, 4096, paNoFlag, patestCallback, &data);
2022-07-06 17:17:06 -05:00
err = Pa_OpenDefaultStream(&stream_def, 0, 2, paInt16, SAMPLERATE, BUF_FRAMES, patestCallback, NULL);
2022-07-03 00:43:42 -05:00
check_pa_err(err);
2022-07-02 03:40:50 -05:00
2022-07-03 00:43:42 -05:00
err = Pa_StartStream(stream_def);
check_pa_err(err);
2022-01-19 16:43:21 -06:00
}
void audio_open(const char *device)
{
2022-06-22 20:39:18 -05:00
//Mix_OpenAudioDevice(44100, MIX_DEFAULT_FORMAT, 2, 2048, device, 0);
2022-01-19 16:43:21 -06:00
}
void audio_close()
{
2022-06-22 20:39:18 -05:00
//Mix_CloseAudio();
2022-01-19 16:43:21 -06:00
}
2022-07-12 01:28:41 -05:00
struct wav make_sound(const char *wav)
2022-01-19 16:43:21 -06:00
{
2022-07-12 01:28:41 -05:00
struct wav mwav;
mwav.data = drwav_open_file_and_read_pcm_frames_s16("sounds/alert.wav", &mwav.ch, &mwav.samplerate, &mwav.frames, NULL);
2022-07-03 00:43:42 -05:00
2022-07-12 01:28:41 -05:00
if (mwav.samplerate != SAMPLERATE) {
mwav = change_samplerate(mwav, 48000);
2022-06-23 02:34:51 -05:00
}
2022-01-19 16:43:21 -06:00
2022-07-12 15:43:02 -05:00
mwav.gain = 1.f;
2022-01-19 16:43:21 -06:00
2022-07-12 01:28:41 -05:00
return mwav;
2022-01-19 16:43:21 -06:00
}
2022-07-19 15:13:15 -05:00
struct soundstream *soundstream_make()
{
struct soundstream *new = malloc(sizeof(*new));
new->buf = circbuf_make(sizeof(short), BUF_FRAMES*CHANNELS*2);
return new;
}
2022-07-12 15:43:02 -05:00
struct sound *play_sound(struct wav *wav)
2022-01-19 16:43:21 -06:00
{
2022-07-12 15:43:02 -05:00
struct sound *new = calloc(1, sizeof(*new));
2022-07-19 15:13:15 -05:00
new->data = wav;
2022-01-19 16:43:21 -06:00
2022-07-12 15:43:02 -05:00
new->bus = first_free_bus(dsp_filter(new, sound_fillbuf));
new->playing = 1;
2022-01-19 16:43:21 -06:00
2022-07-12 01:28:41 -05:00
return new;
2022-01-19 16:43:21 -06:00
}
2022-07-12 15:43:02 -05:00
int sound_playing(const struct sound *s)
{
2022-07-19 15:13:15 -05:00
return s->playing;
2022-07-12 15:43:02 -05:00
}
int sound_paused(const struct sound *s)
{
2022-07-19 15:13:15 -05:00
return (!s->playing && s->frame < s->data->frames);
2022-07-12 15:43:02 -05:00
}
void sound_pause(struct sound *s)
{
s->playing = 0;
bus_free(s->bus);
}
void sound_resume(struct sound *s)
{
s->playing = 1;
s->bus = first_free_bus(dsp_filter(s, sound_fillbuf));
}
void sound_stop(struct sound *s)
{
s->playing = 0;
s->frame = 0;
bus_free(s->bus);
}
2022-07-19 15:13:15 -05:00
int sound_finished(const struct sound *s)
2022-07-12 15:43:02 -05:00
{
return !s->playing && s->frame == s->data->frames;
}
2022-07-19 15:13:15 -05:00
int sound_stopped(const struct sound *s)
2022-07-12 15:43:02 -05:00
{
2022-07-19 15:13:15 -05:00
return !s->playing && s->frame == 0;
2022-07-12 15:43:02 -05:00
}
2022-07-12 01:28:41 -05:00
struct music make_music(const char *mp3)
2022-01-19 16:43:21 -06:00
{
2022-07-12 01:28:41 -05:00
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
}
void audio_init()
{
2022-06-22 20:39:18 -05:00
//audioDriver = SDL_GetAudioDeviceName(0,0);
2022-02-06 10:14:57 -06:00
}
void close_audio_device(int device)
{
2022-06-22 20:39:18 -05:00
//SDL_CloseAudioDevice(device);
2022-02-06 10:14:57 -06:00
}
int open_device(const char *adriver)
{
2022-06-23 02:34:51 -05:00
2022-06-22 20:39:18 -05:00
/*
2022-02-06 10:14:57 -06:00
SDL_AudioSpec audio_spec;
SDL_memset(&audio_spec, 0, sizeof(audio_spec));
2022-07-06 17:17:06 -05:00
audio_spec.freq = SAMPLERATE;
2022-02-06 10:14:57 -06:00
audio_spec.format = AUDIO_F32;
audio_spec.channels = 2;
2022-07-06 17:17:06 -05:00
audio_spec.samples = BUF_FRAMES;
2022-02-06 10:14:57 -06:00
int dev = (int) SDL_OpenAudioDevice(adriver, 0, &audio_spec, NULL, 0);
SDL_PauseAudioDevice(dev, 0);
2022-06-22 20:39:18 -05:00
2022-02-06 10:14:57 -06:00
return dev;
2022-06-22 20:39:18 -05:00
*/
return 0;
2022-02-06 10:14:57 -06:00
}
2022-07-06 17:17:06 -05:00
void sound_fillbuf(struct sound *s, short *buf, int n)
{
2022-07-12 15:43:02 -05:00
float gainmult = pct2mult(s->data->gain);
2022-07-06 17:17:06 -05:00
short *in = s->data->data;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2; j++) {
2022-07-12 15:43:02 -05:00
buf[i*2+j] = in[s->frame+j] * gainmult;
2022-07-06 17:17:06 -05:00
}
2022-07-12 15:43:02 -05:00
2022-07-06 17:17:06 -05:00
s->frame++;
if (s->frame == s->data->frames) {
2022-07-12 15:43:02 -05:00
2022-07-06 17:17:06 -05:00
if (s->loop > 0) {
s->loop--;
2022-07-12 15:43:02 -05:00
s->frame = 0;
} else {
bus_free(s->bus);
2022-07-06 17:17:06 -05:00
}
}
}
}
2022-07-12 15:43:02 -05:00
void mp3_fillbuf(struct sound *s, short *buf, int n)
{
}
2022-07-19 15:13:15 -05:00
2022-07-06 17:17:06 -05:00
void soundstream_fillbuf(struct soundstream *s, short *buf, int n)
{
2022-07-19 15:13:15 -05:00
int max = s->buf->write - s->buf->read;
2022-07-08 13:54:45 -05:00
int lim = (max < n*CHANNELS) ? max : n*CHANNELS;
for (int i = 0; i < lim; i++) {
buf[i] = cbuf_shift(&s->buf);
2022-07-06 17:17:06 -05:00
}
}
2022-07-11 23:21:57 -05:00
float short2db(short val)
{
return 20*log10(abs((double)val) / SHRT_MAX);
}
short db2short(float db)
{
return pow(10, db/20.f) * SHRT_MAX;
}
short short_gain(short val, float db)
{
return (short)(pow(10, db/20.f) * val);
}
float pct2db(float pct)
{
if (pct <= 0) return -72.f;
return 10*log2(pct);
2022-07-12 15:43:02 -05:00
}
float pct2mult(float pct)
{
if (pct <= 0) return 0.f;
return pow(10, 0.5*log2(pct));
2022-07-11 23:21:57 -05:00
}