From 47875bb2282e68784b0ec357eb20bf996bd38666 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Wed, 10 Sep 2014 18:28:29 +0000 Subject: reduce even more --- alsa.c | 68 ++++++++++++++++++++++++++++++-------------------------------- sio.c | 72 ++++++++++++++++++++++++++++++++---------------------------------- 2 files changed, 68 insertions(+), 72 deletions(-) diff --git a/alsa.c b/alsa.c index 9eb381c..099f885 100644 --- a/alsa.c +++ b/alsa.c @@ -26,12 +26,10 @@ #define RATE 48000 #define FPS 25 -struct alsa { - snd_pcm_t *hdl; - snd_pcm_hw_params_t *par; - int16_t *buffer; - unsigned int samples; -} alsa; +static snd_pcm_t *hdl; +static snd_pcm_hw_params_t *par; +static int16_t *buffer; +static unsigned int samples; int init_sio(void) @@ -40,34 +38,34 @@ init_sio(void) unsigned int rate; int rc; - rc = snd_pcm_open(&alsa.hdl, "default", SND_PCM_STREAM_CAPTURE, 0); + rc = snd_pcm_open(&hdl, "default", SND_PCM_STREAM_CAPTURE, 0); if (rc < 0) errx(1, "unable to open pcm device: %s", snd_strerror(rc)); - snd_pcm_hw_params_malloc(&alsa.par); - snd_pcm_hw_params_any(alsa.hdl, alsa.par); - snd_pcm_hw_params_set_access(alsa.hdl, alsa.par, + snd_pcm_hw_params_malloc(&par); + snd_pcm_hw_params_any(hdl, par); + snd_pcm_hw_params_set_access(hdl, par, SND_PCM_ACCESS_RW_INTERLEAVED); - snd_pcm_hw_params_set_format(alsa.hdl, alsa.par, + snd_pcm_hw_params_set_format(hdl, par, SND_PCM_FORMAT_S16_LE); - snd_pcm_hw_params_set_channels(alsa.hdl, alsa.par, STEREO); - snd_pcm_hw_params_set_rate(alsa.hdl, alsa.par, RATE, 0); + snd_pcm_hw_params_set_channels(hdl, par, STEREO); + snd_pcm_hw_params_set_rate(hdl, par, RATE, 0); - rc = snd_pcm_hw_params(alsa.hdl, alsa.par); + rc = snd_pcm_hw_params(hdl, par); if (rc < 0) errx(1, "unable to set hw parameters: %s", snd_strerror(rc)); - snd_pcm_hw_params_get_period_size(alsa.par, &round, NULL); - snd_pcm_hw_params_get_rate(alsa.par, &rate, 0); - snd_pcm_hw_params_free(alsa.par); - snd_pcm_prepare(alsa.hdl); - - alsa.samples = rate / FPS; - warnx("min samples: %d", alsa.samples); - alsa.samples -= alsa.samples % round - round; - warnx("max samples: %d", alsa.samples); - alsa.buffer = calloc(alsa.samples * STEREO, sizeof(int16_t)); - assert(alsa.buffer); + snd_pcm_hw_params_get_period_size(par, &round, NULL); + snd_pcm_hw_params_get_rate(par, &rate, 0); + snd_pcm_hw_params_free(par); + snd_pcm_prepare(hdl); + + samples = rate / FPS; + warnx("min samples: %d", samples); + samples -= samples % round - round; + warnx("max samples: %d", samples); + buffer = calloc(samples * STEREO, sizeof(int16_t)); + assert(buffer); return 0; } @@ -75,7 +73,7 @@ init_sio(void) unsigned int max_samples_sio(void) { - return alsa.samples; + return samples; } int16_t * @@ -83,23 +81,23 @@ read_sio(size_t n) { snd_pcm_sframes_t rc; - if (n > alsa.samples) - n = alsa.samples; + if (n > samples) + n = samples; - rc = snd_pcm_readi(alsa.hdl, alsa.buffer, alsa.samples); - if (rc != alsa.samples) { + rc = snd_pcm_readi(hdl, buffer, samples); + if (rc != samples) { warnx("audio read error: %s", snd_strerror(rc)); if (rc == -EPIPE) - snd_pcm_prepare(alsa.hdl); + snd_pcm_prepare(hdl); } - return alsa.buffer + alsa.samples - n; + return buffer + samples - n; } void free_sio(void) { - snd_pcm_drain(alsa.hdl); - snd_pcm_close(alsa.hdl); - free(alsa.buffer); + snd_pcm_drain(hdl); + snd_pcm_close(hdl); + free(buffer); } diff --git a/sio.c b/sio.c index 2d04600..31e947a 100644 --- a/sio.c +++ b/sio.c @@ -27,46 +27,44 @@ #define SIGNED 1 #define FPS 25 -struct sio { - struct sio_hdl *hdl; - struct sio_par par; - int16_t *buffer; - unsigned int samples; -} sio; +static struct sio_hdl *hdl; +static struct sio_par par; +static int16_t *buffer; +static unsigned int samples; int init_sio(void) { - sio.hdl = sio_open(SIO_DEVANY, SIO_REC, 0); - if (!sio.hdl) + hdl = sio_open(SIO_DEVANY, SIO_REC, 0); + if (!hdl) errx(1, "cannot connect to sound server, is it running?"); - sio_initpar(&sio.par); + sio_initpar(&par); - sio.par.rchan = STEREO; - sio.par.bits = BITS; - sio.par.le = SIO_LE_NATIVE; - sio.par.sig = SIGNED; + par.rchan = STEREO; + par.bits = BITS; + par.le = SIO_LE_NATIVE; + par.sig = SIGNED; - if (!sio_setpar(sio.hdl, &sio.par)) + if (!sio_setpar(hdl, &par)) errx(1, "SIO set params failed"); - if (!sio_getpar(sio.hdl, &sio.par)) + if (!sio_getpar(hdl, &par)) errx(1, "SIO get params failed"); - if (sio.par.rchan != STEREO || - sio.par.bits != BITS || - sio.par.le != SIO_LE_NATIVE || - sio.par.sig != SIGNED) + if (par.rchan != STEREO || + par.bits != BITS || + par.le != SIO_LE_NATIVE || + par.sig != SIGNED) errx(1, "unsupported audio params"); - sio.samples = sio.par.rate / FPS; - warnx("min samples: %d", sio.samples); - sio.samples -= sio.samples % sio.par.round - sio.par.round; - warnx("max samples: %d", sio.samples); - sio.buffer = calloc(sio.samples * sio.par.rchan, sizeof(int16_t)); - assert(sio.buffer); + samples = par.rate / FPS; + warnx("min samples: %d", samples); + samples -= samples % par.round - par.round; + warnx("max samples: %d", samples); + buffer = calloc(samples * par.rchan, sizeof(int16_t)); + assert(buffer); - return sio_start(sio.hdl); + return sio_start(hdl); } unsigned int @@ -77,23 +75,23 @@ max_samples_sio(void) * with 1920 at 25 fps and 48000 Hz or * with 1764 at 25 fps and 44100 Hz it shall fit on most screens */ - return sio.samples; + return samples; } int16_t * read_sio(size_t n) { int done; - char *buffer = (char *)sio.buffer; - size_t bufsz = sio.samples * sio.par.rchan * sizeof(int16_t); - size_t rndsz = n * sio.par.rchan * sizeof(int16_t); + char *p = (char *)buffer; + size_t bufsz = samples * par.rchan * sizeof(int16_t); + size_t rndsz = n * par.rchan * sizeof(int16_t); if (rndsz > bufsz) rndsz = bufsz; - for (done = 0; bufsz > 0; buffer += done, bufsz -= done) { - done = sio_read(sio.hdl, buffer, bufsz); - if (sio_eof(sio.hdl)) + for (done = 0; bufsz > 0; p += done, bufsz -= done) { + done = sio_read(hdl, p, bufsz); + if (sio_eof(hdl)) errx(1, "SIO EOF"); } @@ -101,13 +99,13 @@ read_sio(size_t n) * return a pointer to the latest ROUND samples (the most recent * ones) to minimize latency between picture and sound */ - return (int16_t *)(buffer - rndsz); + return (int16_t *)(p - rndsz); } void free_sio(struct sio *sio) { - sio_stop(sio.hdl); - sio_close(sio.hdl); - free(sio.buffer); + sio_stop(hdl); + sio_close(hdl); + free(buffer); } -- cgit v1.2.3