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-06-23 02:34:51 -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-06-23 02:34:51 -05:00
|
|
|
struct sound *mus_cur;
|
2022-07-03 00:43:42 -05:00
|
|
|
|
|
|
|
|
2022-07-03 11:28:44 -05:00
|
|
|
struct circbuf vidbuf;
|
2022-07-03 00:43:42 -05:00
|
|
|
|
2022-07-03 19:24:53 -05:00
|
|
|
drmp3 mp3;
|
2022-07-03 00:43:42 -05:00
|
|
|
|
2022-07-03 19:24:53 -05:00
|
|
|
struct wav mwav;
|
|
|
|
struct sound wavsound;
|
2022-07-03 00:43:42 -05:00
|
|
|
|
2022-07-05 17:24:58 -05:00
|
|
|
struct wav sin440;
|
|
|
|
struct sound a440;
|
|
|
|
|
2022-07-03 00:43:42 -05:00
|
|
|
|
2022-07-03 19:24:53 -05:00
|
|
|
int vidplaying = 0;
|
2022-07-03 11:28:44 -05:00
|
|
|
|
2022-07-05 15:12:48 -05:00
|
|
|
struct wav change_samplerate(struct wav w, int rate)
|
|
|
|
{
|
2022-07-05 17:24:58 -05:00
|
|
|
printf("Going from sr %i to sr %i.\n", w.samplerate, rate);
|
|
|
|
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-05 15:12:48 -05:00
|
|
|
|
2022-07-05 17:24:58 -05:00
|
|
|
printf("Went from %i to %i frames.\n", oldframes, w.frames);
|
2022-07-05 15:12:48 -05:00
|
|
|
w.samplerate = rate;
|
2022-07-05 17:24:58 -05:00
|
|
|
int samples = sizeof(short)*w.ch*w.frames;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
/* Cast data passed through stream to our structure. */
|
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-04 12:47:21 -05:00
|
|
|
void normalize_gain(struct wav *w, double lv)
|
|
|
|
{
|
|
|
|
short tarmax = pow(10, lv/20.f) * SHRT_MAX;
|
|
|
|
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-04 12:47:21 -05:00
|
|
|
w->gain = log10((float)tarmax/max) * 20;
|
|
|
|
}
|
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()
|
|
|
|
{
|
|
|
|
vidbuf = circbuf_init(sizeof(short), 262144);
|
2022-07-03 00:43:42 -05:00
|
|
|
|
2022-07-03 19:24:53 -05:00
|
|
|
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-04 12:47:21 -05:00
|
|
|
mwav.gain = 0;
|
|
|
|
|
2022-07-03 19:24:53 -05:00
|
|
|
printf("Loaded wav: ch %i, sr %i, fr %i.\n", mwav.ch, mwav.samplerate, mwav.frames);
|
2022-07-03 00:43:42 -05:00
|
|
|
|
2022-07-05 17:24:58 -05:00
|
|
|
// mwav = change_samplerate(mwav, 48000);
|
2022-07-04 14:19:52 -05:00
|
|
|
|
2022-07-05 15:12:48 -05:00
|
|
|
//mwav = gen_square(1, 150, 48000, 2);
|
2022-07-04 12:47:21 -05:00
|
|
|
|
|
|
|
wavsound.data = &mwav;
|
2022-07-03 19:24:53 -05:00
|
|
|
wavsound.loop = 1;
|
2022-07-03 00:43:42 -05:00
|
|
|
|
2022-07-04 12:47:21 -05:00
|
|
|
normalize_gain(&mwav, -3);
|
|
|
|
|
2022-07-05 17:24:58 -05:00
|
|
|
//play_sound(&wavsound);
|
|
|
|
|
2022-07-06 17:17:06 -05:00
|
|
|
sin440 = gen_sine(0.4f, 30, SAMPLERATE, 2);
|
2022-07-05 17:24:58 -05:00
|
|
|
a440.data = &sin440;
|
|
|
|
a440.loop = 1;
|
2022-07-06 17:17:06 -05:00
|
|
|
//play_sound(&a440);
|
2022-07-05 17:24:58 -05:00
|
|
|
|
|
|
|
printf("Playing wav with %i frames.\n", wavsound.data->frames);
|
|
|
|
|
2022-07-06 17:17:06 -05:00
|
|
|
sin600.f = tri_phasor;
|
|
|
|
sin600.p = phasor_make(SAMPLERATE, 200);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sin20.f = square_phasor;
|
|
|
|
sin20.p.sr = SAMPLERATE;
|
|
|
|
sin20.p.freq = 4;
|
|
|
|
|
|
|
|
s600wav = gen_sine(0.6f, 600, SAMPLERATE, CHANNELS);
|
|
|
|
|
|
|
|
s600wavsound.loop = -1;
|
|
|
|
s600wavsound.data = &s600wav;
|
|
|
|
|
|
|
|
|
|
|
|
struct dsp_filter s600;
|
|
|
|
s600.data = &s600wavsound;
|
|
|
|
s600.filter = sound_fillbuf;
|
|
|
|
|
|
|
|
struct dsp_filter s20;
|
|
|
|
s20.data = &sin20;
|
|
|
|
s20.filter = osc_fillbuf;
|
|
|
|
|
|
|
|
struct dsp_filter am_filter;
|
|
|
|
|
|
|
|
|
2022-07-10 17:59:15 -05:00
|
|
|
|
2022-07-06 17:17:06 -05:00
|
|
|
|
|
|
|
am_filter.filter = am_mod;
|
|
|
|
am_filter.data = &dspammod;
|
|
|
|
|
|
|
|
dspdel = dsp_delay_make(150);
|
|
|
|
dspdel.in = am_filter;
|
|
|
|
struct dsp_filter del_filter;
|
|
|
|
del_filter.filter = dsp_delay_filbuf;
|
|
|
|
del_filter.data = &dspdel;
|
|
|
|
|
2022-07-10 17:59:15 -05:00
|
|
|
struct dsp_filter ad = make_adsr(50, 200, 500, 100);
|
|
|
|
|
|
|
|
dspammod.ina = s600;
|
|
|
|
dspammod.inb = ad;
|
|
|
|
|
|
|
|
first_free_bus(am_filter);
|
2022-07-08 13:54:45 -05:00
|
|
|
|
|
|
|
struct dsp_filter wn;
|
|
|
|
wn.filter = gen_pinknoise;
|
|
|
|
//first_free_bus(wn);
|
2022-07-06 17:17:06 -05:00
|
|
|
|
2022-07-03 19:24:53 -05:00
|
|
|
/*
|
2022-07-03 00:43:42 -05:00
|
|
|
if (!drmp3_init_file(&mp3, "sounds/circus.mp3", NULL)) {
|
|
|
|
YughError("Could not open mp3.",0);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("CIrcus mp3 channels: %ui, samplerate: %ui\n", mp3.channels, mp3.sampleRate);
|
2022-07-03 19:24:53 -05:00
|
|
|
*/
|
2022-07-03 00:43:42 -05:00
|
|
|
|
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-07-02 03:40:50 -05:00
|
|
|
|
|
|
|
|
2022-06-23 02:34:51 -05:00
|
|
|
|
2022-07-09 21:46:23 -05:00
|
|
|
|
2022-07-10 11:32:21 -05:00
|
|
|
play_song("", "");
|
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
|
|
|
}
|
|
|
|
|
|
|
|
struct sound *make_sound(const char *wav)
|
|
|
|
{
|
2022-07-03 00:43:42 -05:00
|
|
|
|
2022-01-19 16:43:21 -06:00
|
|
|
struct sound *new = calloc(1, sizeof(struct sound));
|
2022-07-03 00:43:42 -05:00
|
|
|
/* ma_result res = ma_sound_init_from_file(&engine, wav, 0, NULL, NULL, &new->sound);
|
2022-06-23 02:34:51 -05:00
|
|
|
|
|
|
|
if (res != MA_SUCCESS) {
|
|
|
|
printf("HONO!!!!");
|
|
|
|
}
|
2022-07-03 00:43:42 -05:00
|
|
|
*/
|
2022-01-19 16:43:21 -06:00
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2022-06-23 02:34:51 -05:00
|
|
|
struct sound *make_music(const char *ogg)
|
2022-01-19 16:43:21 -06:00
|
|
|
{
|
2022-07-03 00:43:42 -05:00
|
|
|
|
2022-06-23 02:34:51 -05:00
|
|
|
struct sound *sound = calloc(1, sizeof(struct sound));
|
2022-07-03 00:43:42 -05:00
|
|
|
// ma_result res = ma_sound_init_from_file(&engine, ogg, 0, NULL, &mus_grp, &sound->sound);
|
2022-01-19 16:43:21 -06:00
|
|
|
|
|
|
|
return sound;
|
|
|
|
}
|
|
|
|
|
2022-07-05 17:24:58 -05:00
|
|
|
|
2022-01-19 16:43:21 -06:00
|
|
|
|
|
|
|
void play_music(struct sound *music)
|
|
|
|
{
|
2022-07-03 00:43:42 -05:00
|
|
|
//ma_sound_start(&music->sound);
|
|
|
|
//music->state = MUS_PLAY;
|
|
|
|
// mus_cur = music;
|
2022-01-19 16:43:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void music_set(struct sound *music)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void music_volume(unsigned char vol)
|
|
|
|
{
|
2022-07-03 00:43:42 -05:00
|
|
|
// ma_sound_group_set_volume(&mus_grp, (float)vol/127);
|
2022-01-19 16:43:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
int music_playing()
|
|
|
|
{
|
2022-07-03 00:43:42 -05:00
|
|
|
//return ma_sound_is_playing(&mus_cur->sound);
|
|
|
|
return 0;
|
2022-01-19 16:43:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
int music_paused()
|
|
|
|
{
|
2022-07-03 00:43:42 -05:00
|
|
|
// return mus_cur->state == MUS_PAUSE;
|
|
|
|
return 0;
|
2022-01-19 16:43:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void music_resume()
|
|
|
|
{
|
2022-07-03 00:43:42 -05:00
|
|
|
// ma_sound_start(&mus_cur->sound);
|
2022-01-19 16:43:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void music_pause()
|
|
|
|
{
|
2022-07-03 00:43:42 -05:00
|
|
|
//ma_sound_stop(&mus_cur->sound);
|
|
|
|
//mus_cur->state = MUS_PAUSE;
|
2022-01-19 16:43:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void music_stop()
|
|
|
|
{
|
2022-07-03 00:43:42 -05:00
|
|
|
// ma_sound_stop(&mus_cur->sound);
|
|
|
|
// mus_cur->state = MUS_STOP;
|
|
|
|
// ma_sound_seek_to_pcm_frame(&mus_cur->sound, 0);
|
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 play_sound(struct sound *sound)
|
|
|
|
{
|
|
|
|
sound->frame = 0;
|
|
|
|
//struct bus *b = first_free_bus(sound, sound_fillbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void sound_fillbuf(struct sound *s, short *buf, int n)
|
|
|
|
{
|
|
|
|
short *in = s->data->data;
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
for (int j = 0; j < 2; j++) {
|
|
|
|
buf[i*2+j] = in[s->frame+j];
|
|
|
|
}
|
|
|
|
s->frame++;
|
|
|
|
if (s->frame == s->data->frames) {
|
|
|
|
s->frame = 0;
|
|
|
|
if (s->loop > 0) {
|
|
|
|
s->loop--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct soundstream soundstream_make()
|
|
|
|
{
|
|
|
|
struct soundstream new;
|
|
|
|
new.buf = circbuf_init(sizeof(short), BUF_FRAMES*CHANNELS*2);
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
void soundstream_fillbuf(struct soundstream *s, short *buf, int n)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|