Better circular buffer

This commit is contained in:
John Alanbrook 2022-07-04 00:24:53 +00:00
parent f67e48bdcb
commit c9f42184ef
5 changed files with 98 additions and 133 deletions

View file

@ -1,52 +1,43 @@
#include "circbuf.h" #include "circbuf.h"
#include "assert.h"
struct circbuf circbuf_init(size_t size, int len) struct circbuf circbuf_init(size_t size, int len)
{ {
struct circbuf new; struct circbuf new;
new.size = size;
new.len = len; new.len = len;
new.data = calloc(size, len); new.data = calloc(sizeof(short), new.len);
new.head = new.tail = new.data; new.read = new.write = 0;
return new; return new;
} }
void cbuf_append(struct circbuf *buf, void *data, int n) int cbuf_size(struct circbuf *buf) {
{ return buf->write - buf->read;
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;
} }
/* int cbuf_empty(struct circbuf *buf) {
size_t cpbytes = n * buf->size; return buf->read == buf->write;
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);
}
*/
} }
void *cbuf_take(struct circbuf *buf) int cbuf_full(struct circbuf *buf) {
{ return cbuf_size(buf) == buf->len;
void *ret = buf->tail;
if (buf->tail == buf->data + buf->len*buf->size) buf->tail = buf->data;
else buf->tail += buf->size;
return ret;
} }
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++)];
}

View file

@ -2,22 +2,18 @@
#define CIRCBUF_H #define CIRCBUF_H
#include <stdio.h> #include <stdio.h>
#include <stdint.h>
struct circbuf { struct circbuf {
void *head; int16_t *data;
void *tail; uint32_t read;
void *data; uint32_t write;
size_t size;
int len; int len;
}; };
struct circbuf circbuf_init(size_t size, int len); struct circbuf circbuf_init(size_t size, int len);
void cbuf_append(struct circbuf *buf, void *data, int n); void cbuf_push(struct circbuf *buf, short data);
void *cbuf_take(struct circbuf *buf); short cbuf_shift(struct circbuf *buf);
#endif #endif

View file

@ -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) static void render_audio(plm_t * mpeg, plm_samples_t * samples, void *user)
{ {
struct datastream *ds = user; struct datastream *ds = user;
int size = sizeof(float) * samples->count * 2;
play_raw(ds->audio_device, samples->interleaved, 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); plm_set_audio_stream(ds->plm, 0);
// Adjust the audio lead time according to the audio_spec buffer size // 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; ds->playing = true;
} }

View file

@ -15,53 +15,60 @@
#include "circbuf.h" #include "circbuf.h"
//ma_engine engine;
const char *audioDriver; const char *audioDriver;
struct sound *mus_cur; struct sound *mus_cur;
//ma_sound_group mus_grp;
typedef struct #define BUSFRAMES 15000
{
float left_phase;
float right_phase;
} paTestData;
struct circbuf vidbuf; 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; drmp3 mp3;
float inbuf[4096*2]; struct wav mwav;
float filtbuf[3763*2]; 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) 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. */ /* Cast data passed through stream to our structure. */
short *out = (short*)outputBuffer; short *out = (short*)outputBuffer;
/*
int f = 0; int f = 0;
static int interp = 0; static int interp = 0;
for (int i = 0; i < framesPerBuffer; i++) { 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; for (int i = 0; i < framesPerBuffer; i++) {
a[1] += *(short*)cbuf_take(&vidbuf) * 5; //short a[2] = {0, 0};
*(out++) = a[0]; //a[0] += *(short*)cbuf_shift(&vidbuf) * 5;
*(out++) = a[1]; //a[1] += *(short*)cbuf_shift(&vidbuf) * 5;
*(out++) = cbuf_shift(&vidbuf) * 5;
*(out++) = cbuf_shift(&vidbuf) * 5;
} }
return 0; return 0;
@ -75,33 +82,29 @@ void check_pa_err(PaError e)
} }
} }
static paTestData data;
static PaStream *stream_def; static PaStream *stream_def;
void sound_init() void sound_init()
{ {
vidbuf = circbuf_init(sizeof(short), 163864); vidbuf = circbuf_init(sizeof(short), 262144);
drwav wav; mwav.data = drwav_open_file_and_read_pcm_frames_s16("sounds/alert.wav", &mwav.ch, &mwav.samplerate, &mwav.frames, NULL);
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)); printf("Loaded wav: ch %i, sr %i, fr %i.\n", mwav.ch, mwav.samplerate, mwav.frames);
//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);
wavsound.w = &mwav;
wavsound.loop = 1;
wavsound.play = 1;
/*
if (!drmp3_init_file(&mp3, "sounds/circus.mp3", NULL)) { if (!drmp3_init_file(&mp3, "sounds/circus.mp3", NULL)) {
YughError("Could not open mp3.",0); YughError("Could not open mp3.",0);
} }
printf("CIrcus mp3 channels: %ui, samplerate: %ui\n", mp3.channels, mp3.sampleRate); printf("CIrcus mp3 channels: %ui, samplerate: %ui\n", mp3.channels, mp3.sampleRate);
*/
PaError err = Pa_Initialize(); PaError err = Pa_Initialize();
check_pa_err(err); check_pa_err(err);
@ -112,7 +115,7 @@ void sound_init()
for (int i = 0; i < numDevices; i++) { for (int i = 0; i < numDevices; i++) {
deviceInfo = Pa_GetDeviceInfo(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; PaStreamParameters outparams;
@ -127,54 +130,14 @@ void sound_init()
*/ */
//err = Pa_OpenStream(&stream_def, NULL, &outparams, 48000, 4096, paNoFlag, patestCallback, &data); //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); check_pa_err(err);
err = Pa_StartStream(stream_def); err = Pa_StartStream(stream_def);
check_pa_err(err); 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) void audio_open(const char *device)
@ -271,12 +234,15 @@ void audio_init()
void play_raw(int device, void *data, int size) void play_raw(int device, void *data, int size)
{ {
float *d = data; float *d = data;
short t[size]; short t;
for (int i = 0; i < size; i++) { 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++) { for (int i = 0; i < size; i++) {
short temp = (short)(d[i] * 32767); short temp = (short)(d[i] * 32767);

View file

@ -17,10 +17,23 @@ enum MUS {
struct sound { struct sound {
int sound; int sound;
int loop;
int mono;
int fin;
int frame;
int play;
struct wav *w;
enum MUS state; enum MUS state;
unsigned char volume; unsigned char volume;
}; };
struct wav {
int ch;
int samplerate;
int frames;
void *data;
};
extern const char *audioDriver; extern const char *audioDriver;
void sound_init(); void sound_init();