mirror of
https://github.com/alsa-project/alsa-utils
synced 2024-11-08 20:05:42 +01:00
alsabat: use general data generator function
Use general data generator to replace local function, so other modules can reuse the data generator rather than re-implement it. Signed-off-by: Lu, Han <han.lu@intel.com> Signed-off-by: Takashi Iwai <tiwai@suse.de>
This commit is contained in:
parent
eac9b149b4
commit
c49a6180c8
3 changed files with 53 additions and 56 deletions
57
bat/alsa.c
57
bat/alsa.c
|
@ -16,7 +16,6 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
#include <pthread.h>
|
||||
#include <errno.h>
|
||||
|
@ -28,7 +27,6 @@
|
|||
|
||||
#include "common.h"
|
||||
#include "alsa.h"
|
||||
#include "bat-signal.h"
|
||||
|
||||
struct pcm_container {
|
||||
snd_pcm_t *handle;
|
||||
|
@ -206,59 +204,6 @@ static int set_snd_pcm_params(struct bat *bat, struct pcm_container *sndpcm)
|
|||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Generate buffer to be played either from input file or from generated data
|
||||
* Return value
|
||||
* <0 error
|
||||
* 0 ok
|
||||
* >0 break
|
||||
*/
|
||||
static int generate_input_data(struct pcm_container *sndpcm, int bytes,
|
||||
struct bat *bat)
|
||||
{
|
||||
int err;
|
||||
static int load;
|
||||
int frames = bytes * 8 / sndpcm->frame_bits;
|
||||
|
||||
if (bat->playback.file != NULL) {
|
||||
/* From input file */
|
||||
load = 0;
|
||||
|
||||
while (1) {
|
||||
err = fread(sndpcm->buffer + load, 1,
|
||||
bytes - load, bat->fp);
|
||||
if (0 == err) {
|
||||
if (feof(bat->fp)) {
|
||||
fprintf(bat->log,
|
||||
_("End of playing.\n"));
|
||||
return 1;
|
||||
}
|
||||
} else if (err < bytes - load) {
|
||||
if (ferror(bat->fp)) {
|
||||
fprintf(bat->err, _("Read file error"));
|
||||
fprintf(bat->err, _(": %d\n"), err);
|
||||
return -EIO;
|
||||
}
|
||||
load += err;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/* Generate sine wave */
|
||||
if ((bat->sinus_duration) && (load > bat->sinus_duration))
|
||||
return 1;
|
||||
|
||||
err = generate_sine_wave(bat, frames, (void *)sndpcm->buffer);
|
||||
if (err != 0)
|
||||
return err;
|
||||
|
||||
load += frames;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int write_to_pcm(const struct pcm_container *sndpcm,
|
||||
int frames, struct bat *bat)
|
||||
{
|
||||
|
@ -315,7 +260,7 @@ static int write_to_pcm_loop(struct pcm_container *sndpcm, struct bat *bat)
|
|||
}
|
||||
|
||||
while (1) {
|
||||
err = generate_input_data(sndpcm, bytes, bat);
|
||||
err = generate_input_data(bat, sndpcm->buffer, bytes, frames);
|
||||
if (err != 0)
|
||||
break;
|
||||
|
||||
|
|
51
bat/common.c
51
bat/common.c
|
@ -23,6 +23,7 @@
|
|||
|
||||
#include "common.h"
|
||||
#include "alsa.h"
|
||||
#include "bat-signal.h"
|
||||
|
||||
int retval_play;
|
||||
int retval_record;
|
||||
|
@ -211,3 +212,53 @@ int update_wav_header(struct bat *bat, FILE *fp, int bytes)
|
|||
|
||||
return err;
|
||||
}
|
||||
|
||||
/*
|
||||
* Generate buffer to be played either from input file or from generated data
|
||||
* Return value
|
||||
* <0 error
|
||||
* 0 ok
|
||||
* >0 break
|
||||
*/
|
||||
int generate_input_data(struct bat *bat, void *buffer, int bytes, int frames)
|
||||
{
|
||||
int err;
|
||||
static int load;
|
||||
|
||||
if (bat->playback.file != NULL) {
|
||||
/* From input file */
|
||||
load = 0;
|
||||
|
||||
while (1) {
|
||||
err = fread(buffer + load, 1, bytes - load, bat->fp);
|
||||
if (0 == err) {
|
||||
if (feof(bat->fp)) {
|
||||
fprintf(bat->log,
|
||||
_("End of playing.\n"));
|
||||
return 1;
|
||||
}
|
||||
} else if (err < bytes - load) {
|
||||
if (ferror(bat->fp)) {
|
||||
fprintf(bat->err, _("Read file error"));
|
||||
fprintf(bat->err, _(": %d\n"), err);
|
||||
return -EIO;
|
||||
}
|
||||
load += err;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/* Generate sine wave */
|
||||
if ((bat->sinus_duration) && (load > bat->sinus_duration))
|
||||
return 1;
|
||||
|
||||
err = generate_sine_wave(bat, frames, buffer);
|
||||
if (err != 0)
|
||||
return err;
|
||||
|
||||
load += frames;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -184,3 +184,4 @@ void prepare_wav_info(struct wav_container *, struct bat *);
|
|||
int read_wav_header(struct bat *, char *, FILE *, bool);
|
||||
int write_wav_header(FILE *, struct wav_container *, struct bat *);
|
||||
int update_wav_header(struct bat *, FILE *, int);
|
||||
int generate_input_data(struct bat *, void *, int, int);
|
||||
|
|
Loading…
Reference in a new issue