aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2014-09-04 15:02:24 +0000
committerDimitri Sokolyuk <demon@dim13.org>2014-09-04 15:02:24 +0000
commit4ea9f451d622f121668f41ad8a59eadbbdc786ff (patch)
tree458119ee112e5b7255615dfe60513cd16fc64b72
parent967d75bb678cc3b4140462830851e28762726115 (diff)
update alsa plugin
-rw-r--r--alsa.c37
-rw-r--r--sio.h1
2 files changed, 23 insertions, 15 deletions
diff --git a/alsa.c b/alsa.c
index 051ab59..7d310a2 100644
--- a/alsa.c
+++ b/alsa.c
@@ -23,20 +23,22 @@
#include "sio.h"
#define STEREO 2
-#define RATE 44100
+#define RATE 48000
+#define FPS 25
struct sio {
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
int16_t *buffer;
- size_t bufsz;
- snd_pcm_uframes_t round;
+ unsigned int samples;
};
struct sio *
init_sio(void)
{
struct sio *sio;
+ snd_pcm_uframes_t round;
+ unsigned int rate;
int rc;
sio = malloc(sizeof(struct sio));
@@ -53,39 +55,44 @@ init_sio(void)
snd_pcm_hw_params_set_format(sio->handle, sio->params,
SND_PCM_FORMAT_S16_LE);
snd_pcm_hw_params_set_channels(sio->handle, sio->params, STEREO);
- //snd_pcm_hw_params_set_rate(sio->handle, sio->params, RATE, 0);
- //snd_pcm_hw_params_set_period_size(sio->handle, sio->params, round, 0);
+ snd_pcm_hw_params_set_rate(sio->handle, sio->params, RATE, 0);
rc = snd_pcm_hw_params(sio->handle, sio->params);
if (rc < 0)
errx(1, "unable to set hw parameters: %s", snd_strerror(rc));
- /* FIXME */
- snd_pcm_hw_params_get_period_size(sio->params, &sio->round, NULL);
- //snd_pcm_hw_params_get_rate(sio->handle, sio->params, &sio->rate, 0);
+ snd_pcm_hw_params_get_period_size(sio->params, &round, NULL);
+ snd_pcm_hw_params_get_rate(sio->params, &rate, 0);
snd_pcm_hw_params_free(sio->params);
snd_pcm_prepare(sio->handle);
- sio->bufsz = sio->round * STEREO * sizeof(int16_t);
- sio->buffer = malloc(sio->bufsz);
+ sio->samples = rate / FPS;
+ sio->samples -= sio->samples % round;
+ sio->buffer = calloc(sio->samples * STEREO, sizeof(int16_t));
assert(sio->buffer);
return sio;
}
+unsigned int
+max_samples_sio(struct sio *sio)
+{
+ return sio->samples;
+}
+
int16_t *
read_sio(struct sio *sio, unsigned int n)
{
- int rc;
+ snd_pcm_sframes_t rc;
- rc = snd_pcm_readi(sio->handle, sio->buffer, sio->round);
- if (rc != sio->round) {
- warnx("error read from audio interface: %s", snd_strerror(rc));
+ rc = snd_pcm_readi(sio->handle, sio->buffer, sio->samples);
+ if (rc != sio->samples) {
+ warnx("audio read error: %s", snd_strerror(rc));
if (rc == -EPIPE)
snd_pcm_prepare(sio->handle);
}
- return sio->buffer;
+ return sio->buffer + sio->samples - n;
}
void
diff --git a/sio.h b/sio.h
index 29dfea8..775b222 100644
--- a/sio.h
+++ b/sio.h
@@ -23,6 +23,7 @@ struct sio;
__BEGIN_DECLS
struct sio *init_sio(void);
int16_t *read_sio(struct sio *, unsigned int);
+unsigned int max_samples_sio(struct sio *);
void free_sio(struct sio *);
__END_DECLS