X-Git-Url: http://git.tuebingen.mpg.de/?a=blobdiff_plain;f=play.c;h=0a53331b75cca0d5b98b3558f0241302516e11c7;hb=026647beaed61d9d7ae4ec9fa076735a98036d4d;hp=689747e900b6636672d6fc8d06679829fd272189;hpb=c4048f1764be328fb5515df6c31704eb7199b6aa;p=paraslash.git diff --git a/play.c b/play.c index 689747e9..0a53331b 100644 --- a/play.c +++ b/play.c @@ -25,6 +25,7 @@ #define WAV_HEADER_LEN 44 #include /* gettimeofday */ #include "para.h" +#include "fd.h" #include "play.cmdline.h" #include @@ -60,12 +61,21 @@ do { if (EXP) \ while (0) static snd_pcm_t *handle; -static unsigned char *audiobuf; static snd_pcm_uframes_t chunk_size; +static unsigned char *audiobuf; static size_t bytes_per_frame; static struct timeval *start_time; static struct gengetopt_args_info conf; +void para_log(__a_unused int ll, const char* fmt,...) +{ + va_list argp; + + va_start(argp, fmt); + vfprintf(stderr, fmt, argp); + va_end(argp); +} + /* * read_wav_header - read WAV_HEADER_LEN bytes from stdin to audio buffer * @@ -84,11 +94,11 @@ static void read_wav_header(void) } /* - * set_alsa_params - Prepare the PCM handle for writing + * open and prepare the PCM handle for writing * * Install PCM software and hardware configuration. Exit on errors. */ -static void set_alsa_params(void) +static int alsa_init(void) { snd_pcm_hw_params_t *hwparams; snd_pcm_sw_params_t *swparams; @@ -96,6 +106,18 @@ static void set_alsa_params(void) stop_threshold; unsigned buffer_time = 0; int err; + snd_pcm_info_t *info; + snd_output_t *log; + + snd_pcm_info_alloca(&info); + if (snd_output_stdio_attach(&log, stderr, 0) < 0) + EXIT(E_LOG); + err = snd_pcm_open(&handle, conf.device_arg, + SND_PCM_STREAM_PLAYBACK, 0); + if (err < 0) + EXIT(E_PCM_OPEN); + if ((err = snd_pcm_info(handle, info)) < 0) + EXIT(E_SND_PCM_INFO); snd_pcm_hw_params_alloca(&hwparams); snd_pcm_sw_params_alloca(&swparams); @@ -114,7 +136,7 @@ static void set_alsa_params(void) err = snd_pcm_hw_params_get_buffer_time_max(hwparams, &buffer_time, 0); if (err < 0 || !buffer_time) EXIT(E_GET_BUFFER_TIME); - fprintf(stderr, "buffer time: %d\n", buffer_time); + PARA_DEBUG_LOG("buffer time: %d\n", buffer_time); if (snd_pcm_hw_params_set_buffer_time_near(handle, hwparams, &buffer_time, 0) < 0) EXIT(E_SET_BUFFER_TIME); @@ -122,7 +144,7 @@ static void set_alsa_params(void) EXIT(E_HW_PARAMS); snd_pcm_hw_params_get_period_size(hwparams, &chunk_size, 0); snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size); - fprintf(stderr, "buffer size: %lu, period_size: %lu\n", buffer_size, chunk_size); + PARA_DEBUG_LOG("buffer size: %lu, period_size: %lu\n", buffer_size, chunk_size); if (chunk_size == buffer_size) EXIT(E_BAD_PERIOD); snd_pcm_sw_params_current(handle, swparams); @@ -147,24 +169,26 @@ static void set_alsa_params(void) if (snd_pcm_sw_params(handle, swparams) < 0) EXIT(E_SW_PARAMS); bytes_per_frame = snd_pcm_format_physical_width(FORMAT) * conf.channels_arg / 8; + return chunk_size * bytes_per_frame; } -/* - * pcm_write - push out pcm frames +/** + * push out pcm frames * \param data pointer do data to be written - * \param count number of frames + * \param nbytes number of bytes (not frames) * * \return Number of bytes written. Exit on errors. */ -static snd_pcm_sframes_t pcm_write(u_char *data, size_t count) +int alsa_write(u_char *data, size_t nbytes) { + size_t frames = nbytes / bytes_per_frame; snd_pcm_sframes_t r, result = 0; - while (count > 0) { + while (frames > 0) { /* write interleaved frames */ - r = snd_pcm_writei(handle, data, count); + r = snd_pcm_writei(handle, data, frames); if (r < 0) - fprintf(stderr, "write error: %s\n", snd_strerror(r)); - if (r == -EAGAIN || (r >= 0 && r < count)) + PARA_ERROR_LOG("write error: %s\n", snd_strerror(r)); + if (r == -EAGAIN || (r >= 0 && r < frames)) snd_pcm_wait(handle, 1); else if (r == -EPIPE) snd_pcm_prepare(handle); @@ -172,11 +196,18 @@ static snd_pcm_sframes_t pcm_write(u_char *data, size_t count) EXIT(E_WRITE); if (r > 0) { result += r; - count -= r; + frames -= r; data += r * bytes_per_frame; } } - return result; + return result * bytes_per_frame; +} + +void alsa_shutdown(void) +{ + snd_pcm_drain(handle); + snd_pcm_close(handle); + snd_config_update_free_global(); } /* @@ -209,9 +240,8 @@ static int start_time_in_future(struct timeval *diff) */ static void do_initial_delay(struct timeval *delay) { -// fprintf(stderr, "sleeping %lums\n", tv2ms(delay)); do - select(1, NULL, NULL, NULL, delay); + para_select(1, NULL, NULL, delay); while (start_time_in_future(delay)); } @@ -225,13 +255,12 @@ static void do_initial_delay(struct timeval *delay) */ static void play_pcm(size_t loaded) { - size_t chunk_bytes, bufsize, written = 0, prebuf_size; + size_t bufsize, written = 0, prebuf_size; ssize_t ret; unsigned char *p; struct timeval delay; + int chunk_bytes = alsa_init(); - set_alsa_params(); - chunk_bytes = chunk_size * bytes_per_frame; bufsize = (conf.bufsize_arg * 1024 / chunk_bytes) * chunk_bytes; audiobuf = realloc(audiobuf, bufsize); if (!audiobuf) @@ -248,16 +277,13 @@ again: } p = audiobuf; while (loaded >= chunk_bytes) { -// fprintf(stderr, "write (loaded = %d)\n", loaded); - ret = pcm_write(p, chunk_size) * bytes_per_frame; + ret = alsa_write(p, chunk_bytes); p += ret; written += ret; loaded -= ret; } - if (loaded && p != audiobuf) { -// fprintf(stderr, "memcpy: %d@%d\n", loaded, p - audiobuf); - memcpy(audiobuf, p, loaded); - } + if (loaded && p != audiobuf) + memmove(audiobuf, p, loaded); read: ret = read(STDIN_FILENO, audiobuf + loaded, bufsize - loaded); if (ret < 0) @@ -266,7 +292,7 @@ read: loaded += ret; goto again; } - snd_pcm_drain(handle); + alsa_shutdown(); } /* @@ -282,17 +308,12 @@ static size_t check_wave(void) return WAV_HEADER_LEN; conf.channels_arg = (unsigned) a[22]; conf.sample_rate_arg = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24); -// fprintf(stderr, "channels: %d, rate: %d\n", conf.channels_arg, -// conf.sample_rate_arg); return 0; } int main(int argc, char *argv[]) { - snd_pcm_info_t *info; - snd_output_t *log; struct timeval tv; - int err; cmdline_parser(argc, argv, &conf); if (conf.start_time_given) { @@ -301,22 +322,9 @@ int main(int argc, char *argv[]) EXIT(E_SYNTAX); start_time = &tv; } -// fprintf(stderr, "argc=%d, argv[1]=%s\n",argc, argv[1]); - snd_pcm_info_alloca(&info); - if (snd_output_stdio_attach(&log, stderr, 0) < 0) - EXIT(E_LOG); - err = snd_pcm_open(&handle, conf.device_arg, - SND_PCM_STREAM_PLAYBACK, 0); - if (err < 0) - EXIT(E_PCM_OPEN); - if ((err = snd_pcm_info(handle, info)) < 0) - EXIT(E_SND_PCM_INFO); audiobuf = malloc(WAV_HEADER_LEN); read_wav_header(); play_pcm(check_wave()); - snd_pcm_close(handle); free(audiobuf); -// snd_output_close(log); - snd_config_update_free_global(); return EXIT_SUCCESS; }