From 3e2641d65342931667f7a4d0d1ef46c8065e9266 Mon Sep 17 00:00:00 2001 From: Claudio Matsuoka Date: Sun, 12 Feb 2012 11:45:43 -0200 Subject: [PATCH] [xmp] Call ALSA using sound driver structure Signed-off-by: Claudio Matsuoka --- src/main.c | 17 ++++++++++------- src/sound.h | 10 ++++++---- src/sound_alsa.c | 38 ++++++++++++++++++++++++++++++++++---- 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/src/main.c b/src/main.c index 0d12eac..c9ce849 100644 --- a/src/main.c +++ b/src/main.c @@ -8,10 +8,13 @@ #include #include #include -//#include "sound.h" +#include "sound.h" #include "common.h" extern int optind; +extern struct sound_driver sound_alsa; + +struct sound_driver *sound = &sound_alsa; static void cleanup(int sig) { @@ -21,7 +24,7 @@ static void cleanup(int sig) signal(SIGFPE, SIG_DFL); signal(SIGSEGV, SIG_DFL); - sound_deinit(); + sound->deinit(); reset_tty(); signal(sig, SIG_DFL); @@ -66,7 +69,7 @@ int main(int argc, char **argv) shuffle(argc - optind + 1, &argv[optind - 1]); } - if (sound_init(44100, 2) < 0) { + if (sound->init(44100, 2) < 0) { fprintf(stderr, "%s: can't initialize sound\n", argv[0]); exit(EXIT_FAILURE); } @@ -115,7 +118,7 @@ int main(int argc, char **argv) break; info_frame(&mi, new_mod); - sound_play(mi.buffer, mi.buffer_size); + sound->play(mi.buffer, mi.buffer_size); new_mod = 0; options.start = 0; @@ -124,14 +127,14 @@ int main(int argc, char **argv) xmp_player_end(ctx); } + sound->flush(); xmp_release_module(ctx); printf("\n"); } - xmp_free_context(ctx); + xmp_free_context(ctx); reset_tty(); - - sound_deinit(); + sound->deinit(); exit(EXIT_SUCCESS); } diff --git a/src/sound.h b/src/sound.h index f38cdb5..40a2c01 100644 --- a/src/sound.h +++ b/src/sound.h @@ -3,9 +3,11 @@ struct sound_driver { char *id; char *description; char **help; - int (*init) (); - void (*deinit) (); - void (*pause) (); - void (*resume) (); + int (*init)(int, int); + void (*deinit)(); + void (*play)(void *, int); + void (*flush)(); + void (*pause)(); + void (*resume)(); struct list_head *next; }; diff --git a/src/sound_alsa.c b/src/sound_alsa.c index 61f3f99..c873913 100644 --- a/src/sound_alsa.c +++ b/src/sound_alsa.c @@ -6,7 +6,7 @@ static snd_pcm_t *pcm_handle; -int sound_init(int sampling_rate, int channels) +static int init(int sampling_rate, int channels) { snd_pcm_hw_params_t *hwparams; int ret; @@ -51,7 +51,7 @@ int sound_init(int sampling_rate, int channels) return 0; } -void sound_play(void *b, int i) +static void play(void *b, int i) { int frames; @@ -61,10 +61,40 @@ void sound_play(void *b, int i) } } -void sound_deinit() +static void deinit() { - /* snd_pcm_drain(pcm_handle); */ snd_pcm_close(pcm_handle); } +static void flush() +{ + snd_pcm_drain(pcm_handle); +} + +static void onpause() +{ +} + +static void onresume() +{ +} + + +static char *help[] = { + "buffer=num", "Set the ALSA buffer time in milliseconds", + "period=num", "Set the ALSA period time in milliseconds", + "card ", "Select sound card to use", + NULL +}; +struct sound_driver sound_alsa = { + "alsa", + "ALSA pcm audio", + help, + init, + deinit, + play, + flush, + onpause, + onresume +};