ALSA: Add environment variable to delay input

This helps to avoid corrupted date in rx buffer.

Sorry: No more info, because I really don't know why this happens.
This commit is contained in:
Andreas Eversberg 2022-05-26 17:41:34 +02:00
parent e453c10169
commit fd81881922
1 changed files with 11 additions and 8 deletions

View File

@ -29,6 +29,8 @@
#include "sound.h" #include "sound.h"
#endif #endif
static int KEEP_FRAMES=8; /* minimum frames not to read, to prevent reading from buffer before data has been received (seems to be a bug in ALSA) */
typedef struct sound { typedef struct sound {
snd_pcm_t *phandle, *chandle; snd_pcm_t *phandle, *chandle;
int pchannels, cchannels; int pchannels, cchannels;
@ -190,6 +192,7 @@ static void dev_close(sound_t *sound)
void *sound_open(const char *audiodev, double __attribute__((unused)) *tx_frequency, double __attribute__((unused)) *rx_frequency, int __attribute__((unused)) *am, int channels, double __attribute__((unused)) paging_frequency, int samplerate, int __attribute((unused)) buffer_size, double __attribute__((unused)) interval, double max_deviation, double __attribute__((unused)) max_modulation, double __attribute__((unused)) modulation_index) void *sound_open(const char *audiodev, double __attribute__((unused)) *tx_frequency, double __attribute__((unused)) *rx_frequency, int __attribute__((unused)) *am, int channels, double __attribute__((unused)) paging_frequency, int samplerate, int __attribute((unused)) buffer_size, double __attribute__((unused)) interval, double max_deviation, double __attribute__((unused)) max_modulation, double __attribute__((unused)) modulation_index)
{ {
sound_t *sound; sound_t *sound;
const char *env;
int rc; int rc;
if (channels < 1 || channels > 2) { if (channels < 1 || channels > 2) {
@ -229,6 +232,11 @@ void *sound_open(const char *audiodev, double __attribute__((unused)) *tx_freque
} }
#endif #endif
if ((env = getenv("KEEP_FRAMES"))) {
KEEP_FRAMES = atoi(env);
PDEBUG(DSOUND, DEBUG_NOTICE, "KEEP %d samples in RX buffer, to prevent corrupt read.\n", KEEP_FRAMES);
}
return sound; return sound;
error: error:
@ -387,8 +395,6 @@ int sound_write(void *inst, sample_t **samples, uint8_t __attribute__((unused))
return rc; return rc;
} }
#define KEEP_FRAMES 8 /* minimum frames not to read, due to bug in ALSA */
int sound_read(void *inst, sample_t **samples, int num, int channels, double __attribute__((unused)) *rf_level_db) int sound_read(void *inst, sample_t **samples, int num, int channels, double __attribute__((unused)) *rf_level_db)
{ {
sound_t *sound = (sound_t *)inst; sound_t *sound = (sound_t *)inst;
@ -399,9 +405,6 @@ int sound_read(void *inst, sample_t **samples, int num, int channels, double __a
int in, rc; int in, rc;
int i, ii; int i, ii;
/* make valgrind happy, because snd_pcm_readi() does not seem to initially fill buffer with values */
memset(buff, 0, sizeof(buff));
/* get samples in rx buffer */ /* get samples in rx buffer */
in = snd_pcm_avail(sound->chandle); in = snd_pcm_avail(sound->chandle);
/* if not more than KEEP_FRAMES frames available, try next time */ /* if not more than KEEP_FRAMES frames available, try next time */
@ -413,8 +416,10 @@ int sound_read(void *inst, sample_t **samples, int num, int channels, double __a
if (in > num) if (in > num)
in = num; in = num;
rc = snd_pcm_readi(sound->chandle, buff, in); /* make valgrind happy, because snd_pcm_readi() does not seem to initially fill buffer with values */
memset(buff, 0, sizeof(*buff) * sound->cchannels * in);
rc = snd_pcm_readi(sound->chandle, buff, in);
if (rc < 0) { if (rc < 0) {
if (errno == EAGAIN) if (errno == EAGAIN)
return 0; return 0;
@ -430,10 +435,8 @@ int sound_read(void *inst, sample_t **samples, int num, int channels, double __a
} }
return rc; return rc;
} }
if (rc == 0) if (rc == 0)
return rc; return rc;
if (sound->cchannels == 2) { if (sound->cchannels == 2) {
if (channels < 2) { if (channels < 2) {
for (i = 0, ii = 0; i < rc; i++) { for (i = 0, ii = 0; i < rc; i++) {