From 3ad05589aab3ab066930760786167dea846fefcd Mon Sep 17 00:00:00 2001 From: Ahmet Inan Date: Fri, 9 Sep 2011 11:32:36 +0200 Subject: [PATCH] now alsa playback is also limited by seconds --- alsa.c | 14 ++++++++++++-- alsa.h | 2 +- pcm.c | 2 +- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/alsa.c b/alsa.c index 22dc874..091e301 100644 --- a/alsa.c +++ b/alsa.c @@ -18,6 +18,8 @@ typedef struct { int (*channels)(pcm_t *); int (*rw)(struct pcm *, short *, int); snd_pcm_t *pcm; + int index; + int frames; int r; int c; } alsa_t; @@ -32,7 +34,10 @@ void close_alsa(pcm_t *pcm) void info_alsa(pcm_t *pcm) { alsa_t *alsa = (alsa_t *)pcm; - fprintf(stderr, "%d channel(s), %d rate\n", alsa->c, alsa->r); + if (alsa->frames) + fprintf(stderr, "%d channel(s), %d rate, %.2f seconds\n", alsa->c, alsa->r, (float)alsa->frames / (float)alsa->r); + else + fprintf(stderr, "%d channel(s), %d rate\n", alsa->c, alsa->r); } int rate_alsa(pcm_t *pcm) { @@ -60,6 +65,9 @@ int read_alsa(pcm_t *pcm, short *buff, int frames) int write_alsa(pcm_t *pcm, short *buff, int frames) { alsa_t *alsa = (alsa_t *)pcm; + if (alsa->frames && (alsa->index + frames) > alsa->frames) + return 0; + alsa->index += frames; int got = 0; while (0 < frames) { while ((got = snd_pcm_writei(alsa->pcm, buff, frames)) < 0) @@ -144,7 +152,7 @@ int open_alsa_read(pcm_t **p, char *name) return 1; } -int open_alsa_write(pcm_t **p, char *name, int rate, int channels) +int open_alsa_write(pcm_t **p, char *name, int rate, int channels, float seconds) { alsa_t *alsa = (alsa_t *)malloc(sizeof(alsa_t)); alsa->close = close_alsa; @@ -210,6 +218,8 @@ int open_alsa_write(pcm_t **p, char *name, int rate, int channels) alsa->pcm = pcm; alsa->r = rate; alsa->c = channels; + alsa->frames = seconds * rate; + alsa->index = 0; *p = (pcm_t *)alsa; return 1; } diff --git a/alsa.h b/alsa.h index d10f78b..57991a8 100644 --- a/alsa.h +++ b/alsa.h @@ -10,6 +10,6 @@ You should have received a copy of the CC0 Public Domain Dedication along with t #define ALSA_H #include "pcm.h" int open_alsa_read(pcm_t **, char *); -int open_alsa_write(pcm_t **, char *, int, int); +int open_alsa_write(pcm_t **, char *, int, int, float); #endif diff --git a/pcm.c b/pcm.c index 32dab4e..5515761 100644 --- a/pcm.c +++ b/pcm.c @@ -56,7 +56,7 @@ int open_pcm_read(pcm_t **p, char *name) int open_pcm_write(pcm_t **p, char *name, int rate, int channels, float seconds) { if (strstr(name, "plughw:") == name || strstr(name, "hw:") == name || strstr(name, "default") == name) - return open_alsa_write(p, name, rate, channels); + return open_alsa_write(p, name, rate, channels, seconds); if (strstr(name, ".wav") == (name + (strlen(name) - strlen(".wav")))) return open_wav_write(p, name, rate, channels, seconds); return 0;