now alsa playback is also limited by seconds

master
Ahmet Inan 2011-09-09 11:32:36 +02:00
rodzic 3f194c5f09
commit 3ad05589aa
3 zmienionych plików z 14 dodań i 4 usunięć

14
alsa.c
Wyświetl plik

@ -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;
}

2
alsa.h
Wyświetl plik

@ -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

2
pcm.c
Wyświetl plik

@ -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;