From 68f146a132954db19c352db4168101e8f016a67f Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 23 Jun 2013 13:50:07 +0000 Subject: encapsulate sio read buffer --- sio.c | 17 ++++++++++++++--- sio.h | 2 +- spectrogram.c | 8 +------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/sio.c b/sio.c index 8776930..e3d8657 100644 --- a/sio.c +++ b/sio.c @@ -22,6 +22,8 @@ struct sio { struct sio_hdl *sio; struct sio_par par; + int16_t *buffer; + size_t bufsz; }; struct sio * @@ -56,6 +58,12 @@ init_sio(int rchan, int bits, int sig) sio->par.sig != sig) errx(1, "unsupported audio params"); + sio->bufsz = sio->par.rchan * sio->par.round * sizeof(int16_t); + sio->buffer = malloc(sio->bufsz); + + if (!sio->buffer) + errx(1, "malloc failed"); + sio_start(sio->sio); return sio; @@ -67,10 +75,12 @@ get_round(struct sio *sio) return sio->par.round; } -int -read_sio(struct sio *sio, int16_t *buffer, size_t sz) +int16_t * +read_sio(struct sio *sio) { int done = 0; + int16_t *buffer = sio->buffer; + size_t sz = sio->bufsz; do { done += sio_read(sio->sio, buffer, sz); @@ -80,7 +90,7 @@ read_sio(struct sio *sio, int16_t *buffer, size_t sz) sz -= done; } while (sz); - return done; + return sio->buffer; } void @@ -88,5 +98,6 @@ del_sio(struct sio *sio) { sio_stop(sio->sio); sio_close(sio->sio); + free(sio->buffer); free(sio); } diff --git a/sio.h b/sio.h index c771dd1..14ceaf6 100644 --- a/sio.h +++ b/sio.h @@ -22,7 +22,7 @@ struct sio; struct sio *init_sio(int, int, int); unsigned int get_round(struct sio *); -int read_sio(struct sio *, int16_t *, size_t); +int16_t *read_sio(struct sio *); void del_sio(struct sio *); #endif diff --git a/spectrogram.c b/spectrogram.c index 58b5181..4f882d4 100644 --- a/spectrogram.c +++ b/spectrogram.c @@ -60,7 +60,6 @@ int die = 0; struct data { int16_t *buffer; - size_t bufsz; double *left; double *right; int *left_shadow; @@ -332,11 +331,6 @@ main(int argc, char **argv) XSelectInput(dsp, win, ExposureMask|KeyPressMask); XMapWindow(dsp, win); - data.bufsz = RCHAN * delta * sizeof(int16_t); /* par.rchan */ - data.buffer = malloc(data.bufsz); - if (!data.buffer) - errx(1, "malloc failed"); - data.left = calloc(delta, sizeof(double)); data.right = calloc(delta, sizeof(double)); data.left_shadow = calloc(delta, sizeof(int)); @@ -358,7 +352,7 @@ main(int argc, char **argv) fft = init_fft(delta); while (!die) { - read_sio(sio, data.buffer, data.bufsz); + data.buffer = read_sio(sio); dofft(fft, data.buffer, data.left, 0); dofft(fft, data.buffer, data.right, 1); draw(&data); -- cgit v1.2.3