X-Git-Url: http://git.tuebingen.mpg.de/?a=blobdiff_plain;f=play.c;h=c9cc56abb5374831979cb510bcc89ed337425257;hb=002e302f18b73e57e8a5ff5e9dc97c68bb85216d;hp=38203b401c710f4629285ad81b74c67d4eb9c8f0;hpb=cda80f7d3b0a8adf778028ac5a33496b7123ef8b;p=paraslash.git diff --git a/play.c b/play.c index 38203b40..c9cc56ab 100644 --- a/play.c +++ b/play.c @@ -33,9 +33,30 @@ #define FORMAT SND_PCM_FORMAT_S16_LE -static snd_pcm_t *handle; +struct private_alsa_data { + snd_pcm_t *handle; + size_t bytes_per_frame; +}; + +struct writer_node { + struct writer *writer; + void *private_data; +}; + +struct writer { + int (*open)(struct writer_node **); + int (*write)(char *data, size_t nbytes, struct writer_node *); + void (*close)(struct writer_node *); + void (*shutdown)(struct writer_node *); +}; + +#define NUM_WRITERS 1 +static struct writer writers[NUM_WRITERS]; +#define FOR_EACH_WRITER(i) for (i = 0; i < NUM_WRITERS, i++) +static struct writer_node **writer_nodes; + + static unsigned char *audiobuf; -static size_t bytes_per_frame; static struct timeval *start_time; static struct gengetopt_args_info conf; @@ -74,7 +95,7 @@ static int read_wav_header(void) * * Install PCM software and hardware configuration. Exit on errors. */ -static int alsa_init(void) +static int alsa_open(void **private_data) { snd_pcm_hw_params_t *hwparams; snd_pcm_sw_params_t *swparams; @@ -85,68 +106,72 @@ static int alsa_init(void) snd_pcm_info_t *info; snd_output_t *log; snd_pcm_uframes_t period_size; + struct private_alsa_data *pad = para_malloc(sizeof(struct private_alsa_data)); + *private_data = pad; snd_pcm_info_alloca(&info); if (snd_output_stdio_attach(&log, stderr, 0) < 0) return -E_ALSA_LOG; - err = snd_pcm_open(&handle, conf.device_arg, + err = snd_pcm_open(&pad->handle, conf.device_arg, SND_PCM_STREAM_PLAYBACK, 0); if (err < 0) return -E_PCM_OPEN; - if ((err = snd_pcm_info(handle, info)) < 0) + if ((err = snd_pcm_info(pad->handle, info)) < 0) return -E_SND_PCM_INFO; snd_pcm_hw_params_alloca(&hwparams); snd_pcm_sw_params_alloca(&swparams); - if (snd_pcm_hw_params_any(handle, hwparams) < 0) + if (snd_pcm_hw_params_any(pad->handle, hwparams) < 0) return -E_BROKEN_CONF; - if (snd_pcm_hw_params_set_access(handle, hwparams, + if (snd_pcm_hw_params_set_access(pad->handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) return -E_ACCESS_TYPE; - if (snd_pcm_hw_params_set_format(handle, hwparams, FORMAT) < 0) + if (snd_pcm_hw_params_set_format(pad->handle, hwparams, FORMAT) < 0) return -E_SAMPLE_FORMAT; - if (snd_pcm_hw_params_set_channels(handle, hwparams, conf.channels_arg) < 0) + if (snd_pcm_hw_params_set_channels(pad->handle, hwparams, + conf.channels_arg) < 0) return -E_CHANNEL_COUNT; - if (snd_pcm_hw_params_set_rate_near(handle, hwparams, + if (snd_pcm_hw_params_set_rate_near(pad->handle, hwparams, (unsigned int*) &conf.sample_rate_arg, 0) < 0) return -E_SET_RATE; err = snd_pcm_hw_params_get_buffer_time_max(hwparams, &buffer_time, 0); if (err < 0 || !buffer_time) return -E_GET_BUFFER_TIME; PARA_DEBUG_LOG("buffer time: %d\n", buffer_time); - if (snd_pcm_hw_params_set_buffer_time_near(handle, hwparams, + if (snd_pcm_hw_params_set_buffer_time_near(pad->handle, hwparams, &buffer_time, 0) < 0) return -E_SET_BUFFER_TIME; - if (snd_pcm_hw_params(handle, hwparams) < 0) + if (snd_pcm_hw_params(pad->handle, hwparams) < 0) return -E_HW_PARAMS; snd_pcm_hw_params_get_period_size(hwparams, &period_size, 0); snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size); PARA_DEBUG_LOG("buffer size: %lu, period_size: %lu\n", buffer_size, period_size); if (period_size == buffer_size) return -E_BAD_PERIOD; - snd_pcm_sw_params_current(handle, swparams); + snd_pcm_sw_params_current(pad->handle, swparams); err = snd_pcm_sw_params_get_xfer_align(swparams, &xfer_align); if (err < 0 || !xfer_align) return -E_GET_XFER; -// snd_pcm_sw_params_set_sleep_min(handle, swparams, 0); - snd_pcm_sw_params_set_avail_min(handle, swparams, period_size); + snd_pcm_sw_params_set_avail_min(pad->handle, swparams, period_size); /* round to closest transfer boundary */ start_threshold = (buffer_size / xfer_align) * xfer_align; if (start_threshold < 1) start_threshold = 1; - if (snd_pcm_sw_params_set_start_threshold(handle, swparams, + if (snd_pcm_sw_params_set_start_threshold(pad->handle, swparams, start_threshold) < 0) return -E_START_THRESHOLD; stop_threshold = buffer_size; - if (snd_pcm_sw_params_set_stop_threshold(handle, swparams, + if (snd_pcm_sw_params_set_stop_threshold(pad->handle, swparams, stop_threshold) < 0) return -E_STOP_THRESHOLD; - if (snd_pcm_sw_params_set_xfer_align(handle, swparams, xfer_align) < 0) + if (snd_pcm_sw_params_set_xfer_align(pad->handle, swparams, + xfer_align) < 0) return -E_SET_XFER; - if (snd_pcm_sw_params(handle, swparams) < 0) + if (snd_pcm_sw_params(pad->handle, swparams) < 0) return -E_SW_PARAMS; - bytes_per_frame = snd_pcm_format_physical_width(FORMAT) * conf.channels_arg / 8; - return period_size * bytes_per_frame; + pad->bytes_per_frame = snd_pcm_format_physical_width(FORMAT) + * conf.channels_arg / 8; + return period_size * pad->bytes_per_frame; } /** @@ -156,37 +181,51 @@ static int alsa_init(void) * * \return Number of bytes written, -E_ALSA_WRITE on errors. */ -static int alsa_write(u_char *data, size_t nbytes) +static int alsa_write(char *data, size_t nbytes, void *private_data) { - size_t frames = nbytes / bytes_per_frame; + struct private_alsa_data *pad = private_data; + size_t frames = nbytes / pad->bytes_per_frame; + unsigned char *d = data; snd_pcm_sframes_t r, result = 0; + while (frames > 0) { /* write interleaved frames */ - r = snd_pcm_writei(handle, data, frames); + r = snd_pcm_writei(pad->handle, d, frames); if (r < 0) PARA_ERROR_LOG("write error: %s\n", snd_strerror(r)); if (r == -EAGAIN || (r >= 0 && r < frames)) - snd_pcm_wait(handle, 1); + snd_pcm_wait(pad->handle, 1); else if (r == -EPIPE) - snd_pcm_prepare(handle); + snd_pcm_prepare(pad->handle); else if (r < 0) return -E_ALSA_WRITE; if (r > 0) { result += r; frames -= r; - data += r * bytes_per_frame; + d += r * pad->bytes_per_frame; } } - return result * bytes_per_frame; + return result * pad->bytes_per_frame; } -static void alsa_shutdown(void) +static void alsa_close(void *private_data) { - snd_pcm_drain(handle); - snd_pcm_close(handle); + struct private_alsa_data *pad = private_data; + snd_pcm_drain(pad->handle); + snd_pcm_close(pad->handle); snd_config_update_free_global(); + free(pad); } +void alsa_writer_init(struct writer *w) +{ + w->open = alsa_open; + w->write = alsa_write; + w->close = alsa_close; + w->shutdown = NULL; /* nothing to do */ +} + + /** * check if current time is later than start_time * \param diff pointer to write remaining time to @@ -237,7 +276,8 @@ static int play_pcm(size_t loaded) size_t bufsize, written = 0, prebuf_size; unsigned char *p; struct timeval delay; - int chunk_bytes, ret = alsa_init(); + void *private_data; + int chunk_bytes, ret = alsa_open(&private_data); if (ret < 0) goto out; @@ -256,7 +296,7 @@ again: } p = audiobuf; while (loaded >= chunk_bytes) { - ret = alsa_write(p, chunk_bytes); + ret = alsa_write(p, chunk_bytes, private_data); if (ret < 0) goto out; p += ret; @@ -277,7 +317,7 @@ read: } ret = 1; out: - alsa_shutdown(); + alsa_close(private_data); return ret; } @@ -310,6 +350,11 @@ int main(int argc, char *argv[]) goto out; start_time = &tv; } + /* call init for each supported writer */ + alsa_writer_init(&writers[0]); + /* one for each given writer */ + writer_nodes = para_calloc(2 * sizeof(struct writer_node)); + audiobuf = para_malloc(WAV_HEADER_LEN); ret = read_wav_header(); if (ret < 0)