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-06-23 02:34:51 -05:00
|
|
|
|
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-03 00:43:42 -05:00
|
|
|
//ma_engine engine;
|
2022-06-23 02:34:51 -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
|
|
|
//ma_sound_group mus_grp;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
float left_phase;
|
|
|
|
float right_phase;
|
|
|
|
} paTestData;
|
|
|
|
|
2022-07-03 11:28:44 -05:00
|
|
|
struct circbuf vidbuf;
|
2022-07-03 00:43:42 -05:00
|
|
|
|
|
|
|
short HalfSecond[22400];
|
|
|
|
short *shorthead;
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int ch;
|
|
|
|
unsigned int srate;
|
|
|
|
drwav_uint64 curcmf = 0;
|
|
|
|
drwav_uint64 totalpcmf;
|
|
|
|
float *psamps;
|
|
|
|
|
|
|
|
drmp3 mp3;
|
|
|
|
|
2022-07-03 11:28:44 -05:00
|
|
|
float inbuf[4096*2];
|
|
|
|
float filtbuf[3763*2];
|
|
|
|
|
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-03 00:43:42 -05:00
|
|
|
|
|
|
|
int f = 0;
|
|
|
|
|
2022-07-03 11:28:44 -05:00
|
|
|
static int interp = 0;
|
2022-07-03 00:43:42 -05:00
|
|
|
|
|
|
|
for (int i = 0; i < framesPerBuffer; i++) {
|
2022-07-03 11:28:44 -05:00
|
|
|
short a[2] = {0, 0};
|
2022-07-03 00:43:42 -05:00
|
|
|
|
2022-07-03 11:28:44 -05:00
|
|
|
a[0] += *(short*)cbuf_take(&vidbuf) * 5;
|
|
|
|
a[1] += *(short*)cbuf_take(&vidbuf) * 5;
|
2022-07-03 00:43:42 -05:00
|
|
|
|
|
|
|
*(out++) = a[0];
|
|
|
|
*(out++) = a[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
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 paTestData data;
|
|
|
|
static PaStream *stream_def;
|
|
|
|
|
2022-01-19 16:43:21 -06:00
|
|
|
void sound_init()
|
|
|
|
{
|
2022-07-03 11:28:44 -05:00
|
|
|
vidbuf = circbuf_init(sizeof(short), 163864);
|
2022-07-03 00:43:42 -05:00
|
|
|
|
|
|
|
drwav wav;
|
|
|
|
if (!drwav_init_file(&wav, "sounds/alert.wav", NULL)) {
|
|
|
|
YughError("Could not open wav.",0);
|
|
|
|
}
|
|
|
|
|
|
|
|
//drwav_int32 *wavdec = malloc(wav.totalPCMFrameCount * wav.channels * sizeof(drwav_int32));
|
|
|
|
//size_t samps = drwav_read_pcm_frames_s32(&wav, wav.totalPCMFrameCount, wavdec);
|
|
|
|
|
|
|
|
|
2022-07-03 11:28:44 -05:00
|
|
|
psamps = drwav_open_file_and_read_pcm_frames_s16("sounds/alert.wav", &ch, &srate, &totalpcmf, NULL);
|
2022-07-03 00:43:42 -05:00
|
|
|
|
|
|
|
printf("WAV is: %i channels, %i samplerate, %l frames.\n", ch, srate, totalpcmf);
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
PaError err = Pa_Initialize();
|
|
|
|
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 11:28:44 -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);
|
|
|
|
err = Pa_OpenDefaultStream(&stream_def, 0, 2, paInt16, 48000, 4096, 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-07-03 00:43:42 -05:00
|
|
|
//Pa_Sleep(1000);
|
|
|
|
|
|
|
|
//check_pa_err(Pa_StopStream(stream));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
ma_result result;
|
|
|
|
ma_device device;
|
|
|
|
ma_device_config cnf;
|
|
|
|
|
|
|
|
cnf = ma_device_config_init(ma_device_type_playback);
|
2022-06-23 02:34:51 -05:00
|
|
|
cnf.playback.format = ma_format_f32;
|
2022-07-02 03:40:50 -05:00
|
|
|
cnf.playback.channels = 2;
|
|
|
|
cnf.sampleRate = 48000;
|
2022-06-23 02:34:51 -05:00
|
|
|
cnf.dataCallback = data_callback;
|
|
|
|
|
2022-07-03 00:43:42 -05:00
|
|
|
result = ma_device_init(NULL, &cnf, &device);
|
|
|
|
if (result != MA_SUCCESS) {
|
2022-07-02 03:40:50 -05:00
|
|
|
YughError("Did not initialize audio playback!!",0);
|
|
|
|
}
|
|
|
|
|
2022-06-23 02:34:51 -05:00
|
|
|
|
2022-07-03 00:43:42 -05:00
|
|
|
result = ma_device_start(&device);
|
|
|
|
if (result != MA_SUCCESS) {
|
2022-07-02 03:40:50 -05:00
|
|
|
printf("Failed to start playback device.\n");
|
|
|
|
}
|
2022-07-03 00:43:42 -05:00
|
|
|
*/
|
2022-06-23 02:34:51 -05:00
|
|
|
|
2022-07-03 00:43:42 -05:00
|
|
|
/*
|
2022-07-02 03:40:50 -05:00
|
|
|
ma_engine_config enginecnf = ma_engine_config_init();
|
|
|
|
enginecnf.pDevice = &device;
|
|
|
|
|
|
|
|
ma_result result = ma_engine_init(&enginecnf, &engine);
|
2022-06-23 02:34:51 -05:00
|
|
|
if (result != MA_SUCCESS) {
|
2022-06-30 10:31:23 -05:00
|
|
|
YughError("Miniaudio did not start properly.",1);
|
|
|
|
exit(1);
|
2022-06-23 02:34:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ma_sound_group_init(&engine, 0, NULL, &mus_grp);
|
2022-07-03 00:43:42 -05:00
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void play_sound(struct sound *sound)
|
|
|
|
{
|
2022-06-23 02:34:51 -05:00
|
|
|
//ma_sound_set_volume(&sound->sound, (float)sound->volume/127);
|
2022-07-03 00:43:42 -05:00
|
|
|
// ma_sound_start(&sound->sound);
|
|
|
|
// sound->state = MUS_PLAY;
|
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 play_raw(int device, void *data, int size)
|
|
|
|
{
|
2022-07-03 00:43:42 -05:00
|
|
|
float *d = data;
|
2022-07-03 11:28:44 -05:00
|
|
|
short t[size];
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
t[i] = d[i]*32767;
|
|
|
|
}
|
|
|
|
cbuf_append(&vidbuf, t, size);
|
|
|
|
/*
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
short temp = (short)(d[i] * 32767);
|
|
|
|
cbuf_append(&vidbuf, &temp, 1);
|
2022-07-03 00:43:42 -05:00
|
|
|
}
|
2022-07-03 11:28:44 -05:00
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void clear_raw(int device)
|
|
|
|
{
|
2022-06-22 20:39:18 -05:00
|
|
|
//SDL_ClearQueuedAudio(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));
|
|
|
|
audio_spec.freq = 48000;
|
|
|
|
audio_spec.format = AUDIO_F32;
|
|
|
|
audio_spec.channels = 2;
|
|
|
|
audio_spec.samples = 4096;
|
|
|
|
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
|
|
|
}
|