diff --git a/source/engine/circbuf.c b/source/engine/circbuf.c index 7e1c2c9..2dcbd59 100644 --- a/source/engine/circbuf.c +++ b/source/engine/circbuf.c @@ -1,52 +1,43 @@ #include "circbuf.h" - - +#include "assert.h" struct circbuf circbuf_init(size_t size, int len) { struct circbuf new; - new.size = size; new.len = len; - new.data = calloc(size, len); - new.head = new.tail = new.data; + new.data = calloc(sizeof(short), new.len); + new.read = new.write = 0; return new; } -void cbuf_append(struct circbuf *buf, void *data, int n) -{ - - for (int i = 0; i < n; i++) { - memcpy(buf->head, data+(buf->size*i), buf->size); - - buf->head += buf->size; - - if (buf ->head == buf->data + buf->size*buf->len) buf->head = buf->data; - } - -/* - size_t cpbytes = n * buf->size; - size_t rembytes = (buf->size * buf->len) - (buf->head - buf->data); - - if (cpbytes <= rembytes) - memcpy(buf->head, data, cpbytes); - else { - memcpy(buf->head, data, rembytes); - cpbytes -= rembytes; - buf->head = buf->data; - memcpy(buf->head, data+rembytes, cpbytes); - } -*/ - +int cbuf_size(struct circbuf *buf) { + return buf->write - buf->read; +} + +int cbuf_empty(struct circbuf *buf) { + return buf->read == buf->write; +} + +int cbuf_full(struct circbuf *buf) { + return cbuf_size(buf) == buf->len; +} + +uint32_t cbuf_mask(struct circbuf *buf, uint32_t n) { + return n & (buf->len-1); +} + +void cbuf_push(struct circbuf *buf, short data) { + assert(!cbuf_full(buf)); + + buf->data[cbuf_mask(buf,buf->write++)] = data; +} + +short cbuf_shift(struct circbuf *buf) { + assert(!cbuf_empty(buf)); + return buf->data[cbuf_mask(buf, buf->read++)]; } -void *cbuf_take(struct circbuf *buf) -{ - void *ret = buf->tail; - if (buf->tail == buf->data + buf->len*buf->size) buf->tail = buf->data; - else buf->tail += buf->size; - return ret; -} \ No newline at end of file diff --git a/source/engine/circbuf.h b/source/engine/circbuf.h index fc09ffd..d3741d4 100644 --- a/source/engine/circbuf.h +++ b/source/engine/circbuf.h @@ -2,22 +2,18 @@ #define CIRCBUF_H #include +#include struct circbuf { - void *head; - void *tail; - void *data; - size_t size; + int16_t *data; + uint32_t read; + uint32_t write; int len; }; struct circbuf circbuf_init(size_t size, int len); -void cbuf_append(struct circbuf *buf, void *data, int n); -void *cbuf_take(struct circbuf *buf); - - - - +void cbuf_push(struct circbuf *buf, short data); +short cbuf_shift(struct circbuf *buf); #endif \ No newline at end of file diff --git a/source/engine/datastream.c b/source/engine/datastream.c index baa7043..6aa1fb0 100755 --- a/source/engine/datastream.c +++ b/source/engine/datastream.c @@ -31,7 +31,6 @@ static void render_frame(plm_t * mpeg, plm_frame_t * frame, void *user) static void render_audio(plm_t * mpeg, plm_samples_t * samples, void *user) { struct datastream *ds = user; - int size = sizeof(float) * samples->count * 2; play_raw(ds->audio_device, samples->interleaved, samples->count * 2); } @@ -71,7 +70,7 @@ void ds_openvideo(struct datastream *ds, const char *video, const char *adriver) plm_set_audio_stream(ds->plm, 0); // Adjust the audio lead time according to the audio_spec buffer size - //plm_set_audio_lead_time(ds->plm, 4096/48000); + plm_set_audio_lead_time(ds->plm, 4096/48000); ds->playing = true; } diff --git a/source/engine/sound.c b/source/engine/sound.c index 7a637b1..c5fbc4f 100755 --- a/source/engine/sound.c +++ b/source/engine/sound.c @@ -15,53 +15,60 @@ #include "circbuf.h" -//ma_engine engine; const char *audioDriver; struct sound *mus_cur; -//ma_sound_group mus_grp; -typedef struct -{ - float left_phase; - float right_phase; -} paTestData; +#define BUSFRAMES 15000 struct circbuf vidbuf; -short HalfSecond[22400]; -short *shorthead; - - - unsigned int ch; - unsigned int srate; - drwav_uint64 curcmf = 0; - drwav_uint64 totalpcmf; - float *psamps; - drmp3 mp3; - float inbuf[4096*2]; - float filtbuf[3763*2]; +struct wav mwav; +struct sound wavsound; + +short *get_sound_frame(struct sound *s, int sr) { + s->frame = (s->frame+3) % s->w->frames; + return &s->w->data[s->frame]; +} + +int vidplaying = 0; 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. */ short *out = (short*)outputBuffer; - +/* int f = 0; static int interp = 0; for (int i = 0; i < framesPerBuffer; i++) { - short a[2] = {0, 0}; + *(out++) = *(short*)(mwav.data++); + *(out++) = *(short*)(mwav.data++); + } + */ +/* + if (wavsound.play) { + for (int i = 0; i < framesPerBuffer; i++) { + short *f = get_sound_frame(&wavsound, 48000); + out[i*2] += f[0]; + out[i*2+1] += f[1]; + } + } + */ + if (!vidplaying) return 0; - a[0] += *(short*)cbuf_take(&vidbuf) * 5; - a[1] += *(short*)cbuf_take(&vidbuf) * 5; + for (int i = 0; i < framesPerBuffer; i++) { + //short a[2] = {0, 0}; - *(out++) = a[0]; - *(out++) = a[1]; + //a[0] += *(short*)cbuf_shift(&vidbuf) * 5; + //a[1] += *(short*)cbuf_shift(&vidbuf) * 5; + + *(out++) = cbuf_shift(&vidbuf) * 5; + *(out++) = cbuf_shift(&vidbuf) * 5; } return 0; @@ -75,33 +82,29 @@ void check_pa_err(PaError e) } } -static paTestData data; static PaStream *stream_def; + + void sound_init() { - vidbuf = circbuf_init(sizeof(short), 163864); + vidbuf = circbuf_init(sizeof(short), 262144); - drwav wav; - if (!drwav_init_file(&wav, "sounds/alert.wav", NULL)) { - YughError("Could not open wav.",0); - } + mwav.data = drwav_open_file_and_read_pcm_frames_s16("sounds/alert.wav", &mwav.ch, &mwav.samplerate, &mwav.frames, NULL); - //drwav_int32 *wavdec = malloc(wav.totalPCMFrameCount * wav.channels * sizeof(drwav_int32)); - //size_t samps = drwav_read_pcm_frames_s32(&wav, wav.totalPCMFrameCount, wavdec); - - - psamps = drwav_open_file_and_read_pcm_frames_s16("sounds/alert.wav", &ch, &srate, &totalpcmf, NULL); - - printf("WAV is: %i channels, %i samplerate, %l frames.\n", ch, srate, totalpcmf); + printf("Loaded wav: ch %i, sr %i, fr %i.\n", mwav.ch, mwav.samplerate, mwav.frames); + wavsound.w = &mwav; + wavsound.loop = 1; + wavsound.play = 1; +/* 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); @@ -112,7 +115,7 @@ void sound_init() for (int i = 0; i < numDevices; i++) { deviceInfo = Pa_GetDeviceInfo(i); - printf("Device %i: channels %i, sample rate %f, name %s\n", i, deviceInfo->maxOutputChannels, deviceInfo->defaultSampleRate, deviceInfo->name); + // printf("Device %i: channels %i, sample rate %f, name %s\n", i, deviceInfo->maxOutputChannels, deviceInfo->defaultSampleRate, deviceInfo->name); } PaStreamParameters outparams; @@ -127,54 +130,14 @@ void sound_init() */ //err = Pa_OpenStream(&stream_def, NULL, &outparams, 48000, 4096, paNoFlag, patestCallback, &data); - err = Pa_OpenDefaultStream(&stream_def, 0, 2, paInt16, 48000, 4096, patestCallback, NULL); + err = Pa_OpenDefaultStream(&stream_def, 0, 2, paInt16, 48000, 256, patestCallback, NULL); check_pa_err(err); err = Pa_StartStream(stream_def); check_pa_err(err); - //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); - cnf.playback.format = ma_format_f32; - cnf.playback.channels = 2; - cnf.sampleRate = 48000; - cnf.dataCallback = data_callback; - - result = ma_device_init(NULL, &cnf, &device); - if (result != MA_SUCCESS) { - YughError("Did not initialize audio playback!!",0); - } - - - result = ma_device_start(&device); - if (result != MA_SUCCESS) { - printf("Failed to start playback device.\n"); - } -*/ - -/* - ma_engine_config enginecnf = ma_engine_config_init(); - enginecnf.pDevice = &device; - - ma_result result = ma_engine_init(&enginecnf, &engine); - if (result != MA_SUCCESS) { - YughError("Miniaudio did not start properly.",1); - exit(1); - } - - ma_sound_group_init(&engine, 0, NULL, &mus_grp); -*/ } void audio_open(const char *device) @@ -271,12 +234,15 @@ void audio_init() void play_raw(int device, void *data, int size) { + float *d = data; - short t[size]; + short t; for (int i = 0; i < size; i++) { - t[i] = d[i]*32767; + t = (short)(d[i]*32767); + cbuf_push(&vidbuf, t); } - cbuf_append(&vidbuf, t, size); + + vidplaying = 1; /* for (int i = 0; i < size; i++) { short temp = (short)(d[i] * 32767); diff --git a/source/engine/sound.h b/source/engine/sound.h index 06a47d8..81cb8b5 100755 --- a/source/engine/sound.h +++ b/source/engine/sound.h @@ -17,10 +17,23 @@ enum MUS { struct sound { int sound; + int loop; + int mono; + int fin; + int frame; + int play; + struct wav *w; enum MUS state; unsigned char volume; }; +struct wav { + int ch; + int samplerate; + int frames; + void *data; +}; + extern const char *audioDriver; void sound_init();