|
Packit |
4a16fb |
/*
|
|
Packit |
4a16fb |
* This small demo sends a simple sinusoidal wave to your speakers.
|
|
Packit |
4a16fb |
*/
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
#include <stdio.h>
|
|
Packit |
4a16fb |
#include <stdlib.h>
|
|
Packit |
4a16fb |
#include <string.h>
|
|
Packit |
4a16fb |
#include <sched.h>
|
|
Packit |
4a16fb |
#include <errno.h>
|
|
Packit |
4a16fb |
#include <getopt.h>
|
|
Packit |
4a16fb |
#include "../include/asoundlib.h"
|
|
Packit |
4a16fb |
#include <sys/time.h>
|
|
Packit |
4a16fb |
#include <math.h>
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
static char *device = "plughw:0,0"; /* playback device */
|
|
Packit |
4a16fb |
static snd_pcm_format_t format = SND_PCM_FORMAT_S16; /* sample format */
|
|
Packit |
4a16fb |
static unsigned int rate = 44100; /* stream rate */
|
|
Packit |
4a16fb |
static unsigned int channels = 1; /* count of channels */
|
|
Packit |
4a16fb |
static unsigned int buffer_time = 500000; /* ring buffer length in us */
|
|
Packit |
4a16fb |
static unsigned int period_time = 100000; /* period time in us */
|
|
Packit |
4a16fb |
static double freq = 440; /* sinusoidal wave frequency in Hz */
|
|
Packit |
4a16fb |
static int verbose = 0; /* verbose flag */
|
|
Packit |
4a16fb |
static int resample = 1; /* enable alsa-lib resampling */
|
|
Packit |
4a16fb |
static int period_event = 0; /* produce poll event after each period */
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
static snd_pcm_sframes_t buffer_size;
|
|
Packit |
4a16fb |
static snd_pcm_sframes_t period_size;
|
|
Packit |
4a16fb |
static snd_output_t *output = NULL;
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
static void generate_sine(const snd_pcm_channel_area_t *areas,
|
|
Packit |
4a16fb |
snd_pcm_uframes_t offset,
|
|
Packit |
4a16fb |
int count, double *_phase)
|
|
Packit |
4a16fb |
{
|
|
Packit |
4a16fb |
static double max_phase = 2. * M_PI;
|
|
Packit |
4a16fb |
double phase = *_phase;
|
|
Packit |
4a16fb |
double step = max_phase*freq/(double)rate;
|
|
Packit |
4a16fb |
unsigned char *samples[channels];
|
|
Packit |
4a16fb |
int steps[channels];
|
|
Packit |
4a16fb |
unsigned int chn;
|
|
Packit |
4a16fb |
int format_bits = snd_pcm_format_width(format);
|
|
Packit |
4a16fb |
unsigned int maxval = (1 << (format_bits - 1)) - 1;
|
|
Packit |
4a16fb |
int bps = format_bits / 8; /* bytes per sample */
|
|
Packit |
4a16fb |
int phys_bps = snd_pcm_format_physical_width(format) / 8;
|
|
Packit |
4a16fb |
int big_endian = snd_pcm_format_big_endian(format) == 1;
|
|
Packit |
4a16fb |
int to_unsigned = snd_pcm_format_unsigned(format) == 1;
|
|
Packit |
4a16fb |
int is_float = (format == SND_PCM_FORMAT_FLOAT_LE ||
|
|
Packit |
4a16fb |
format == SND_PCM_FORMAT_FLOAT_BE);
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
/* verify and prepare the contents of areas */
|
|
Packit |
4a16fb |
for (chn = 0; chn < channels; chn++) {
|
|
Packit |
4a16fb |
if ((areas[chn].first % 8) != 0) {
|
|
Packit |
4a16fb |
printf("areas[%u].first == %u, aborting...\n", chn, areas[chn].first);
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
samples[chn] = /*(signed short *)*/(((unsigned char *)areas[chn].addr) + (areas[chn].first / 8));
|
|
Packit |
4a16fb |
if ((areas[chn].step % 16) != 0) {
|
|
Packit |
4a16fb |
printf("areas[%u].step == %u, aborting...\n", chn, areas[chn].step);
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
steps[chn] = areas[chn].step / 8;
|
|
Packit |
4a16fb |
samples[chn] += offset * steps[chn];
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
/* fill the channel areas */
|
|
Packit |
4a16fb |
while (count-- > 0) {
|
|
Packit |
4a16fb |
union {
|
|
Packit |
4a16fb |
float f;
|
|
Packit |
4a16fb |
int i;
|
|
Packit |
4a16fb |
} fval;
|
|
Packit |
4a16fb |
int res, i;
|
|
Packit |
4a16fb |
if (is_float) {
|
|
Packit |
4a16fb |
fval.f = sin(phase);
|
|
Packit |
4a16fb |
res = fval.i;
|
|
Packit |
4a16fb |
} else
|
|
Packit |
4a16fb |
res = sin(phase) * maxval;
|
|
Packit |
4a16fb |
if (to_unsigned)
|
|
Packit |
4a16fb |
res ^= 1U << (format_bits - 1);
|
|
Packit |
4a16fb |
for (chn = 0; chn < channels; chn++) {
|
|
Packit |
4a16fb |
/* Generate data in native endian format */
|
|
Packit |
4a16fb |
if (big_endian) {
|
|
Packit |
4a16fb |
for (i = 0; i < bps; i++)
|
|
Packit |
4a16fb |
*(samples[chn] + phys_bps - 1 - i) = (res >> i * 8) & 0xff;
|
|
Packit |
4a16fb |
} else {
|
|
Packit |
4a16fb |
for (i = 0; i < bps; i++)
|
|
Packit |
4a16fb |
*(samples[chn] + i) = (res >> i * 8) & 0xff;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
samples[chn] += steps[chn];
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
phase += step;
|
|
Packit |
4a16fb |
if (phase >= max_phase)
|
|
Packit |
4a16fb |
phase -= max_phase;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
*_phase = phase;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
static int set_hwparams(snd_pcm_t *handle,
|
|
Packit |
4a16fb |
snd_pcm_hw_params_t *params,
|
|
Packit |
4a16fb |
snd_pcm_access_t access)
|
|
Packit |
4a16fb |
{
|
|
Packit |
4a16fb |
unsigned int rrate;
|
|
Packit |
4a16fb |
snd_pcm_uframes_t size;
|
|
Packit |
4a16fb |
int err, dir;
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
/* choose all parameters */
|
|
Packit |
4a16fb |
err = snd_pcm_hw_params_any(handle, params);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("Broken configuration for playback: no configurations available: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
return err;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
/* set hardware resampling */
|
|
Packit |
4a16fb |
err = snd_pcm_hw_params_set_rate_resample(handle, params, resample);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("Resampling setup failed for playback: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
return err;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
/* set the interleaved read/write format */
|
|
Packit |
4a16fb |
err = snd_pcm_hw_params_set_access(handle, params, access);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("Access type not available for playback: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
return err;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
/* set the sample format */
|
|
Packit |
4a16fb |
err = snd_pcm_hw_params_set_format(handle, params, format);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("Sample format not available for playback: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
return err;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
/* set the count of channels */
|
|
Packit |
4a16fb |
err = snd_pcm_hw_params_set_channels(handle, params, channels);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("Channels count (%u) not available for playbacks: %s\n", channels, snd_strerror(err));
|
|
Packit |
4a16fb |
return err;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
/* set the stream rate */
|
|
Packit |
4a16fb |
rrate = rate;
|
|
Packit |
4a16fb |
err = snd_pcm_hw_params_set_rate_near(handle, params, &rrate, 0);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("Rate %uHz not available for playback: %s\n", rate, snd_strerror(err));
|
|
Packit |
4a16fb |
return err;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
if (rrate != rate) {
|
|
Packit |
4a16fb |
printf("Rate doesn't match (requested %uHz, get %iHz)\n", rate, err);
|
|
Packit |
4a16fb |
return -EINVAL;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
/* set the buffer time */
|
|
Packit |
4a16fb |
err = snd_pcm_hw_params_set_buffer_time_near(handle, params, &buffer_time, &dir;;
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("Unable to set buffer time %u for playback: %s\n", buffer_time, snd_strerror(err));
|
|
Packit |
4a16fb |
return err;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
err = snd_pcm_hw_params_get_buffer_size(params, &size);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("Unable to get buffer size for playback: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
return err;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
buffer_size = size;
|
|
Packit |
4a16fb |
/* set the period time */
|
|
Packit |
4a16fb |
err = snd_pcm_hw_params_set_period_time_near(handle, params, &period_time, &dir;;
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("Unable to set period time %u for playback: %s\n", period_time, snd_strerror(err));
|
|
Packit |
4a16fb |
return err;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
err = snd_pcm_hw_params_get_period_size(params, &size, &dir;;
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("Unable to get period size for playback: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
return err;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
period_size = size;
|
|
Packit |
4a16fb |
/* write the parameters to device */
|
|
Packit |
4a16fb |
err = snd_pcm_hw_params(handle, params);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("Unable to set hw params for playback: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
return err;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
return 0;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
static int set_swparams(snd_pcm_t *handle, snd_pcm_sw_params_t *swparams)
|
|
Packit |
4a16fb |
{
|
|
Packit |
4a16fb |
int err;
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
/* get the current swparams */
|
|
Packit |
4a16fb |
err = snd_pcm_sw_params_current(handle, swparams);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("Unable to determine current swparams for playback: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
return err;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
/* start the transfer when the buffer is almost full: */
|
|
Packit |
4a16fb |
/* (buffer_size / avail_min) * avail_min */
|
|
Packit |
4a16fb |
err = snd_pcm_sw_params_set_start_threshold(handle, swparams, (buffer_size / period_size) * period_size);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("Unable to set start threshold mode for playback: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
return err;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
/* allow the transfer when at least period_size samples can be processed */
|
|
Packit |
4a16fb |
/* or disable this mechanism when period event is enabled (aka interrupt like style processing) */
|
|
Packit |
4a16fb |
err = snd_pcm_sw_params_set_avail_min(handle, swparams, period_event ? buffer_size : period_size);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("Unable to set avail min for playback: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
return err;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
/* enable period events when requested */
|
|
Packit |
4a16fb |
if (period_event) {
|
|
Packit |
4a16fb |
err = snd_pcm_sw_params_set_period_event(handle, swparams, 1);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("Unable to set period event: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
return err;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
/* write the parameters to the playback device */
|
|
Packit |
4a16fb |
err = snd_pcm_sw_params(handle, swparams);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("Unable to set sw params for playback: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
return err;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
return 0;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
/*
|
|
Packit |
4a16fb |
* Underrun and suspend recovery
|
|
Packit |
4a16fb |
*/
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
static int xrun_recovery(snd_pcm_t *handle, int err)
|
|
Packit |
4a16fb |
{
|
|
Packit |
4a16fb |
if (verbose)
|
|
Packit |
4a16fb |
printf("stream recovery\n");
|
|
Packit |
4a16fb |
if (err == -EPIPE) { /* under-run */
|
|
Packit |
4a16fb |
err = snd_pcm_prepare(handle);
|
|
Packit |
4a16fb |
if (err < 0)
|
|
Packit |
4a16fb |
printf("Can't recovery from underrun, prepare failed: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
return 0;
|
|
Packit |
4a16fb |
} else if (err == -ESTRPIPE) {
|
|
Packit |
4a16fb |
while ((err = snd_pcm_resume(handle)) == -EAGAIN)
|
|
Packit |
4a16fb |
sleep(1); /* wait until the suspend flag is released */
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
err = snd_pcm_prepare(handle);
|
|
Packit |
4a16fb |
if (err < 0)
|
|
Packit |
4a16fb |
printf("Can't recovery from suspend, prepare failed: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
return 0;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
return err;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
/*
|
|
Packit |
4a16fb |
* Transfer method - write only
|
|
Packit |
4a16fb |
*/
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
static int write_loop(snd_pcm_t *handle,
|
|
Packit |
4a16fb |
signed short *samples,
|
|
Packit |
4a16fb |
snd_pcm_channel_area_t *areas)
|
|
Packit |
4a16fb |
{
|
|
Packit |
4a16fb |
double phase = 0;
|
|
Packit |
4a16fb |
signed short *ptr;
|
|
Packit |
4a16fb |
int err, cptr;
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
while (1) {
|
|
Packit |
4a16fb |
generate_sine(areas, 0, period_size, &phase);
|
|
Packit |
4a16fb |
ptr = samples;
|
|
Packit |
4a16fb |
cptr = period_size;
|
|
Packit |
4a16fb |
while (cptr > 0) {
|
|
Packit |
4a16fb |
err = snd_pcm_writei(handle, ptr, cptr);
|
|
Packit |
4a16fb |
if (err == -EAGAIN)
|
|
Packit |
4a16fb |
continue;
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
if (xrun_recovery(handle, err) < 0) {
|
|
Packit |
4a16fb |
printf("Write error: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
break; /* skip one period */
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
ptr += err * channels;
|
|
Packit |
4a16fb |
cptr -= err;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
/*
|
|
Packit |
4a16fb |
* Transfer method - write and wait for room in buffer using poll
|
|
Packit |
4a16fb |
*/
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
static int wait_for_poll(snd_pcm_t *handle, struct pollfd *ufds, unsigned int count)
|
|
Packit |
4a16fb |
{
|
|
Packit |
4a16fb |
unsigned short revents;
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
while (1) {
|
|
Packit |
4a16fb |
poll(ufds, count, -1);
|
|
Packit |
4a16fb |
snd_pcm_poll_descriptors_revents(handle, ufds, count, &revents);
|
|
Packit |
4a16fb |
if (revents & POLLERR)
|
|
Packit |
4a16fb |
return -EIO;
|
|
Packit |
4a16fb |
if (revents & POLLOUT)
|
|
Packit |
4a16fb |
return 0;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
static int write_and_poll_loop(snd_pcm_t *handle,
|
|
Packit |
4a16fb |
signed short *samples,
|
|
Packit |
4a16fb |
snd_pcm_channel_area_t *areas)
|
|
Packit |
4a16fb |
{
|
|
Packit |
4a16fb |
struct pollfd *ufds;
|
|
Packit |
4a16fb |
double phase = 0;
|
|
Packit |
4a16fb |
signed short *ptr;
|
|
Packit |
4a16fb |
int err, count, cptr, init;
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
count = snd_pcm_poll_descriptors_count (handle);
|
|
Packit |
4a16fb |
if (count <= 0) {
|
|
Packit |
4a16fb |
printf("Invalid poll descriptors count\n");
|
|
Packit |
4a16fb |
return count;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
ufds = malloc(sizeof(struct pollfd) * count);
|
|
Packit |
4a16fb |
if (ufds == NULL) {
|
|
Packit |
4a16fb |
printf("No enough memory\n");
|
|
Packit |
4a16fb |
return -ENOMEM;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
if ((err = snd_pcm_poll_descriptors(handle, ufds, count)) < 0) {
|
|
Packit |
4a16fb |
printf("Unable to obtain poll descriptors for playback: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
return err;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
init = 1;
|
|
Packit |
4a16fb |
while (1) {
|
|
Packit |
4a16fb |
if (!init) {
|
|
Packit |
4a16fb |
err = wait_for_poll(handle, ufds, count);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
if (snd_pcm_state(handle) == SND_PCM_STATE_XRUN ||
|
|
Packit |
4a16fb |
snd_pcm_state(handle) == SND_PCM_STATE_SUSPENDED) {
|
|
Packit |
4a16fb |
err = snd_pcm_state(handle) == SND_PCM_STATE_XRUN ? -EPIPE : -ESTRPIPE;
|
|
Packit |
4a16fb |
if (xrun_recovery(handle, err) < 0) {
|
|
Packit |
4a16fb |
printf("Write error: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
init = 1;
|
|
Packit |
4a16fb |
} else {
|
|
Packit |
4a16fb |
printf("Wait for poll failed\n");
|
|
Packit |
4a16fb |
return err;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
generate_sine(areas, 0, period_size, &phase);
|
|
Packit |
4a16fb |
ptr = samples;
|
|
Packit |
4a16fb |
cptr = period_size;
|
|
Packit |
4a16fb |
while (cptr > 0) {
|
|
Packit |
4a16fb |
err = snd_pcm_writei(handle, ptr, cptr);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
if (xrun_recovery(handle, err) < 0) {
|
|
Packit |
4a16fb |
printf("Write error: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
init = 1;
|
|
Packit |
4a16fb |
break; /* skip one period */
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
if (snd_pcm_state(handle) == SND_PCM_STATE_RUNNING)
|
|
Packit |
4a16fb |
init = 0;
|
|
Packit |
4a16fb |
ptr += err * channels;
|
|
Packit |
4a16fb |
cptr -= err;
|
|
Packit |
4a16fb |
if (cptr == 0)
|
|
Packit |
4a16fb |
break;
|
|
Packit |
4a16fb |
/* it is possible, that the initial buffer cannot store */
|
|
Packit |
4a16fb |
/* all data from the last period, so wait awhile */
|
|
Packit |
4a16fb |
err = wait_for_poll(handle, ufds, count);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
if (snd_pcm_state(handle) == SND_PCM_STATE_XRUN ||
|
|
Packit |
4a16fb |
snd_pcm_state(handle) == SND_PCM_STATE_SUSPENDED) {
|
|
Packit |
4a16fb |
err = snd_pcm_state(handle) == SND_PCM_STATE_XRUN ? -EPIPE : -ESTRPIPE;
|
|
Packit |
4a16fb |
if (xrun_recovery(handle, err) < 0) {
|
|
Packit |
4a16fb |
printf("Write error: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
init = 1;
|
|
Packit |
4a16fb |
} else {
|
|
Packit |
4a16fb |
printf("Wait for poll failed\n");
|
|
Packit |
4a16fb |
return err;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
/*
|
|
Packit |
4a16fb |
* Transfer method - asynchronous notification
|
|
Packit |
4a16fb |
*/
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
struct async_private_data {
|
|
Packit |
4a16fb |
signed short *samples;
|
|
Packit |
4a16fb |
snd_pcm_channel_area_t *areas;
|
|
Packit |
4a16fb |
double phase;
|
|
Packit |
4a16fb |
};
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
static void async_callback(snd_async_handler_t *ahandler)
|
|
Packit |
4a16fb |
{
|
|
Packit |
4a16fb |
snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler);
|
|
Packit |
4a16fb |
struct async_private_data *data = snd_async_handler_get_callback_private(ahandler);
|
|
Packit |
4a16fb |
signed short *samples = data->samples;
|
|
Packit |
4a16fb |
snd_pcm_channel_area_t *areas = data->areas;
|
|
Packit |
4a16fb |
snd_pcm_sframes_t avail;
|
|
Packit |
4a16fb |
int err;
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
avail = snd_pcm_avail_update(handle);
|
|
Packit |
4a16fb |
while (avail >= period_size) {
|
|
Packit |
4a16fb |
generate_sine(areas, 0, period_size, &data->phase);
|
|
Packit |
4a16fb |
err = snd_pcm_writei(handle, samples, period_size);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("Write error: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
if (err != period_size) {
|
|
Packit |
4a16fb |
printf("Write error: written %i expected %li\n", err, period_size);
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
avail = snd_pcm_avail_update(handle);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
static int async_loop(snd_pcm_t *handle,
|
|
Packit |
4a16fb |
signed short *samples,
|
|
Packit |
4a16fb |
snd_pcm_channel_area_t *areas)
|
|
Packit |
4a16fb |
{
|
|
Packit |
4a16fb |
struct async_private_data data;
|
|
Packit |
4a16fb |
snd_async_handler_t *ahandler;
|
|
Packit |
4a16fb |
int err, count;
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
data.samples = samples;
|
|
Packit |
4a16fb |
data.areas = areas;
|
|
Packit |
4a16fb |
data.phase = 0;
|
|
Packit |
4a16fb |
err = snd_async_add_pcm_handler(&ahandler, handle, async_callback, &data);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("Unable to register async handler\n");
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
for (count = 0; count < 2; count++) {
|
|
Packit |
4a16fb |
generate_sine(areas, 0, period_size, &data.phase);
|
|
Packit |
4a16fb |
err = snd_pcm_writei(handle, samples, period_size);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("Initial write error: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
if (err != period_size) {
|
|
Packit |
4a16fb |
printf("Initial write error: written %i expected %li\n", err, period_size);
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
if (snd_pcm_state(handle) == SND_PCM_STATE_PREPARED) {
|
|
Packit |
4a16fb |
err = snd_pcm_start(handle);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("Start error: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
/* because all other work is done in the signal handler,
|
|
Packit |
4a16fb |
suspend the process */
|
|
Packit |
4a16fb |
while (1) {
|
|
Packit |
4a16fb |
sleep(1);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
/*
|
|
Packit |
4a16fb |
* Transfer method - asynchronous notification + direct write
|
|
Packit |
4a16fb |
*/
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
static void async_direct_callback(snd_async_handler_t *ahandler)
|
|
Packit |
4a16fb |
{
|
|
Packit |
4a16fb |
snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler);
|
|
Packit |
4a16fb |
struct async_private_data *data = snd_async_handler_get_callback_private(ahandler);
|
|
Packit |
4a16fb |
const snd_pcm_channel_area_t *my_areas;
|
|
Packit |
4a16fb |
snd_pcm_uframes_t offset, frames, size;
|
|
Packit |
4a16fb |
snd_pcm_sframes_t avail, commitres;
|
|
Packit |
4a16fb |
snd_pcm_state_t state;
|
|
Packit |
4a16fb |
int first = 0, err;
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
while (1) {
|
|
Packit |
4a16fb |
state = snd_pcm_state(handle);
|
|
Packit |
4a16fb |
if (state == SND_PCM_STATE_XRUN) {
|
|
Packit |
4a16fb |
err = xrun_recovery(handle, -EPIPE);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("XRUN recovery failed: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
first = 1;
|
|
Packit |
4a16fb |
} else if (state == SND_PCM_STATE_SUSPENDED) {
|
|
Packit |
4a16fb |
err = xrun_recovery(handle, -ESTRPIPE);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("SUSPEND recovery failed: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
avail = snd_pcm_avail_update(handle);
|
|
Packit |
4a16fb |
if (avail < 0) {
|
|
Packit |
4a16fb |
err = xrun_recovery(handle, avail);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("avail update failed: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
first = 1;
|
|
Packit |
4a16fb |
continue;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
if (avail < period_size) {
|
|
Packit |
4a16fb |
if (first) {
|
|
Packit |
4a16fb |
first = 0;
|
|
Packit |
4a16fb |
err = snd_pcm_start(handle);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("Start error: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
} else {
|
|
Packit |
4a16fb |
break;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
continue;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
size = period_size;
|
|
Packit |
4a16fb |
while (size > 0) {
|
|
Packit |
4a16fb |
frames = size;
|
|
Packit |
4a16fb |
err = snd_pcm_mmap_begin(handle, &my_areas, &offset, &frames);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
if ((err = xrun_recovery(handle, err)) < 0) {
|
|
Packit |
4a16fb |
printf("MMAP begin avail error: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
first = 1;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
generate_sine(my_areas, offset, frames, &data->phase);
|
|
Packit |
4a16fb |
commitres = snd_pcm_mmap_commit(handle, offset, frames);
|
|
Packit |
4a16fb |
if (commitres < 0 || (snd_pcm_uframes_t)commitres != frames) {
|
|
Packit |
4a16fb |
if ((err = xrun_recovery(handle, commitres >= 0 ? -EPIPE : commitres)) < 0) {
|
|
Packit |
4a16fb |
printf("MMAP commit error: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
first = 1;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
size -= frames;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
static int async_direct_loop(snd_pcm_t *handle,
|
|
Packit |
4a16fb |
signed short *samples ATTRIBUTE_UNUSED,
|
|
Packit |
4a16fb |
snd_pcm_channel_area_t *areas ATTRIBUTE_UNUSED)
|
|
Packit |
4a16fb |
{
|
|
Packit |
4a16fb |
struct async_private_data data;
|
|
Packit |
4a16fb |
snd_async_handler_t *ahandler;
|
|
Packit |
4a16fb |
const snd_pcm_channel_area_t *my_areas;
|
|
Packit |
4a16fb |
snd_pcm_uframes_t offset, frames, size;
|
|
Packit |
4a16fb |
snd_pcm_sframes_t commitres;
|
|
Packit |
4a16fb |
int err, count;
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
data.samples = NULL; /* we do not require the global sample area for direct write */
|
|
Packit |
4a16fb |
data.areas = NULL; /* we do not require the global areas for direct write */
|
|
Packit |
4a16fb |
data.phase = 0;
|
|
Packit |
4a16fb |
err = snd_async_add_pcm_handler(&ahandler, handle, async_direct_callback, &data);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("Unable to register async handler\n");
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
for (count = 0; count < 2; count++) {
|
|
Packit |
4a16fb |
size = period_size;
|
|
Packit |
4a16fb |
while (size > 0) {
|
|
Packit |
4a16fb |
frames = size;
|
|
Packit |
4a16fb |
err = snd_pcm_mmap_begin(handle, &my_areas, &offset, &frames);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
if ((err = xrun_recovery(handle, err)) < 0) {
|
|
Packit |
4a16fb |
printf("MMAP begin avail error: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
generate_sine(my_areas, offset, frames, &data.phase);
|
|
Packit |
4a16fb |
commitres = snd_pcm_mmap_commit(handle, offset, frames);
|
|
Packit |
4a16fb |
if (commitres < 0 || (snd_pcm_uframes_t)commitres != frames) {
|
|
Packit |
4a16fb |
if ((err = xrun_recovery(handle, commitres >= 0 ? -EPIPE : commitres)) < 0) {
|
|
Packit |
4a16fb |
printf("MMAP commit error: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
size -= frames;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
err = snd_pcm_start(handle);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("Start error: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
/* because all other work is done in the signal handler,
|
|
Packit |
4a16fb |
suspend the process */
|
|
Packit |
4a16fb |
while (1) {
|
|
Packit |
4a16fb |
sleep(1);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
/*
|
|
Packit |
4a16fb |
* Transfer method - direct write only
|
|
Packit |
4a16fb |
*/
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
static int direct_loop(snd_pcm_t *handle,
|
|
Packit |
4a16fb |
signed short *samples ATTRIBUTE_UNUSED,
|
|
Packit |
4a16fb |
snd_pcm_channel_area_t *areas ATTRIBUTE_UNUSED)
|
|
Packit |
4a16fb |
{
|
|
Packit |
4a16fb |
double phase = 0;
|
|
Packit |
4a16fb |
const snd_pcm_channel_area_t *my_areas;
|
|
Packit |
4a16fb |
snd_pcm_uframes_t offset, frames, size;
|
|
Packit |
4a16fb |
snd_pcm_sframes_t avail, commitres;
|
|
Packit |
4a16fb |
snd_pcm_state_t state;
|
|
Packit |
4a16fb |
int err, first = 1;
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
while (1) {
|
|
Packit |
4a16fb |
state = snd_pcm_state(handle);
|
|
Packit |
4a16fb |
if (state == SND_PCM_STATE_XRUN) {
|
|
Packit |
4a16fb |
err = xrun_recovery(handle, -EPIPE);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("XRUN recovery failed: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
return err;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
first = 1;
|
|
Packit |
4a16fb |
} else if (state == SND_PCM_STATE_SUSPENDED) {
|
|
Packit |
4a16fb |
err = xrun_recovery(handle, -ESTRPIPE);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("SUSPEND recovery failed: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
return err;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
avail = snd_pcm_avail_update(handle);
|
|
Packit |
4a16fb |
if (avail < 0) {
|
|
Packit |
4a16fb |
err = xrun_recovery(handle, avail);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("avail update failed: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
return err;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
first = 1;
|
|
Packit |
4a16fb |
continue;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
if (avail < period_size) {
|
|
Packit |
4a16fb |
if (first) {
|
|
Packit |
4a16fb |
first = 0;
|
|
Packit |
4a16fb |
err = snd_pcm_start(handle);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("Start error: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
} else {
|
|
Packit |
4a16fb |
err = snd_pcm_wait(handle, -1);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
if ((err = xrun_recovery(handle, err)) < 0) {
|
|
Packit |
4a16fb |
printf("snd_pcm_wait error: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
first = 1;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
continue;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
size = period_size;
|
|
Packit |
4a16fb |
while (size > 0) {
|
|
Packit |
4a16fb |
frames = size;
|
|
Packit |
4a16fb |
err = snd_pcm_mmap_begin(handle, &my_areas, &offset, &frames);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
if ((err = xrun_recovery(handle, err)) < 0) {
|
|
Packit |
4a16fb |
printf("MMAP begin avail error: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
first = 1;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
generate_sine(my_areas, offset, frames, &phase);
|
|
Packit |
4a16fb |
commitres = snd_pcm_mmap_commit(handle, offset, frames);
|
|
Packit |
4a16fb |
if (commitres < 0 || (snd_pcm_uframes_t)commitres != frames) {
|
|
Packit |
4a16fb |
if ((err = xrun_recovery(handle, commitres >= 0 ? -EPIPE : commitres)) < 0) {
|
|
Packit |
4a16fb |
printf("MMAP commit error: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
first = 1;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
size -= frames;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
/*
|
|
Packit |
4a16fb |
* Transfer method - direct write only using mmap_write functions
|
|
Packit |
4a16fb |
*/
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
static int direct_write_loop(snd_pcm_t *handle,
|
|
Packit |
4a16fb |
signed short *samples,
|
|
Packit |
4a16fb |
snd_pcm_channel_area_t *areas)
|
|
Packit |
4a16fb |
{
|
|
Packit |
4a16fb |
double phase = 0;
|
|
Packit |
4a16fb |
signed short *ptr;
|
|
Packit |
4a16fb |
int err, cptr;
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
while (1) {
|
|
Packit |
4a16fb |
generate_sine(areas, 0, period_size, &phase);
|
|
Packit |
4a16fb |
ptr = samples;
|
|
Packit |
4a16fb |
cptr = period_size;
|
|
Packit |
4a16fb |
while (cptr > 0) {
|
|
Packit |
4a16fb |
err = snd_pcm_mmap_writei(handle, ptr, cptr);
|
|
Packit |
4a16fb |
if (err == -EAGAIN)
|
|
Packit |
4a16fb |
continue;
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
if (xrun_recovery(handle, err) < 0) {
|
|
Packit |
4a16fb |
printf("Write error: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
break; /* skip one period */
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
ptr += err * channels;
|
|
Packit |
4a16fb |
cptr -= err;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
/*
|
|
Packit |
4a16fb |
*
|
|
Packit |
4a16fb |
*/
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
struct transfer_method {
|
|
Packit |
4a16fb |
const char *name;
|
|
Packit |
4a16fb |
snd_pcm_access_t access;
|
|
Packit |
4a16fb |
int (*transfer_loop)(snd_pcm_t *handle,
|
|
Packit |
4a16fb |
signed short *samples,
|
|
Packit |
4a16fb |
snd_pcm_channel_area_t *areas);
|
|
Packit |
4a16fb |
};
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
static struct transfer_method transfer_methods[] = {
|
|
Packit |
4a16fb |
{ "write", SND_PCM_ACCESS_RW_INTERLEAVED, write_loop },
|
|
Packit |
4a16fb |
{ "write_and_poll", SND_PCM_ACCESS_RW_INTERLEAVED, write_and_poll_loop },
|
|
Packit |
4a16fb |
{ "async", SND_PCM_ACCESS_RW_INTERLEAVED, async_loop },
|
|
Packit |
4a16fb |
{ "async_direct", SND_PCM_ACCESS_MMAP_INTERLEAVED, async_direct_loop },
|
|
Packit |
4a16fb |
{ "direct_interleaved", SND_PCM_ACCESS_MMAP_INTERLEAVED, direct_loop },
|
|
Packit |
4a16fb |
{ "direct_noninterleaved", SND_PCM_ACCESS_MMAP_NONINTERLEAVED, direct_loop },
|
|
Packit |
4a16fb |
{ "direct_write", SND_PCM_ACCESS_MMAP_INTERLEAVED, direct_write_loop },
|
|
Packit |
4a16fb |
{ NULL, SND_PCM_ACCESS_RW_INTERLEAVED, NULL }
|
|
Packit |
4a16fb |
};
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
static void help(void)
|
|
Packit |
4a16fb |
{
|
|
Packit |
4a16fb |
int k;
|
|
Packit |
4a16fb |
printf(
|
|
Packit |
4a16fb |
"Usage: pcm [OPTION]... [FILE]...\n"
|
|
Packit |
4a16fb |
"-h,--help help\n"
|
|
Packit |
4a16fb |
"-D,--device playback device\n"
|
|
Packit |
4a16fb |
"-r,--rate stream rate in Hz\n"
|
|
Packit |
4a16fb |
"-c,--channels count of channels in stream\n"
|
|
Packit |
4a16fb |
"-f,--frequency sine wave frequency in Hz\n"
|
|
Packit |
4a16fb |
"-b,--buffer ring buffer size in us\n"
|
|
Packit |
4a16fb |
"-p,--period period size in us\n"
|
|
Packit |
4a16fb |
"-m,--method transfer method\n"
|
|
Packit |
4a16fb |
"-o,--format sample format\n"
|
|
Packit |
4a16fb |
"-v,--verbose show the PCM setup parameters\n"
|
|
Packit |
4a16fb |
"-n,--noresample do not resample\n"
|
|
Packit |
4a16fb |
"-e,--pevent enable poll event after each period\n"
|
|
Packit |
4a16fb |
"\n");
|
|
Packit |
4a16fb |
printf("Recognized sample formats are:");
|
|
Packit |
4a16fb |
for (k = 0; k < SND_PCM_FORMAT_LAST; ++k) {
|
|
Packit |
4a16fb |
const char *s = snd_pcm_format_name(k);
|
|
Packit |
4a16fb |
if (s)
|
|
Packit |
4a16fb |
printf(" %s", s);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
printf("\n");
|
|
Packit |
4a16fb |
printf("Recognized transfer methods are:");
|
|
Packit |
4a16fb |
for (k = 0; transfer_methods[k].name; k++)
|
|
Packit |
4a16fb |
printf(" %s", transfer_methods[k].name);
|
|
Packit |
4a16fb |
printf("\n");
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
int main(int argc, char *argv[])
|
|
Packit |
4a16fb |
{
|
|
Packit |
4a16fb |
struct option long_option[] =
|
|
Packit |
4a16fb |
{
|
|
Packit |
4a16fb |
{"help", 0, NULL, 'h'},
|
|
Packit |
4a16fb |
{"device", 1, NULL, 'D'},
|
|
Packit |
4a16fb |
{"rate", 1, NULL, 'r'},
|
|
Packit |
4a16fb |
{"channels", 1, NULL, 'c'},
|
|
Packit |
4a16fb |
{"frequency", 1, NULL, 'f'},
|
|
Packit |
4a16fb |
{"buffer", 1, NULL, 'b'},
|
|
Packit |
4a16fb |
{"period", 1, NULL, 'p'},
|
|
Packit |
4a16fb |
{"method", 1, NULL, 'm'},
|
|
Packit |
4a16fb |
{"format", 1, NULL, 'o'},
|
|
Packit |
4a16fb |
{"verbose", 1, NULL, 'v'},
|
|
Packit |
4a16fb |
{"noresample", 1, NULL, 'n'},
|
|
Packit |
4a16fb |
{"pevent", 1, NULL, 'e'},
|
|
Packit |
4a16fb |
{NULL, 0, NULL, 0},
|
|
Packit |
4a16fb |
};
|
|
Packit |
4a16fb |
snd_pcm_t *handle;
|
|
Packit |
4a16fb |
int err, morehelp;
|
|
Packit |
4a16fb |
snd_pcm_hw_params_t *hwparams;
|
|
Packit |
4a16fb |
snd_pcm_sw_params_t *swparams;
|
|
Packit |
4a16fb |
int method = 0;
|
|
Packit |
4a16fb |
signed short *samples;
|
|
Packit |
4a16fb |
unsigned int chn;
|
|
Packit |
4a16fb |
snd_pcm_channel_area_t *areas;
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
snd_pcm_hw_params_alloca(&hwparams);
|
|
Packit |
4a16fb |
snd_pcm_sw_params_alloca(&swparams);
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
morehelp = 0;
|
|
Packit |
4a16fb |
while (1) {
|
|
Packit |
4a16fb |
int c;
|
|
Packit |
4a16fb |
if ((c = getopt_long(argc, argv, "hD:r:c:f:b:p:m:o:vne", long_option, NULL)) < 0)
|
|
Packit |
4a16fb |
break;
|
|
Packit |
4a16fb |
switch (c) {
|
|
Packit |
4a16fb |
case 'h':
|
|
Packit |
4a16fb |
morehelp++;
|
|
Packit |
4a16fb |
break;
|
|
Packit |
4a16fb |
case 'D':
|
|
Packit |
4a16fb |
device = strdup(optarg);
|
|
Packit |
4a16fb |
break;
|
|
Packit |
4a16fb |
case 'r':
|
|
Packit |
4a16fb |
rate = atoi(optarg);
|
|
Packit |
4a16fb |
rate = rate < 4000 ? 4000 : rate;
|
|
Packit |
4a16fb |
rate = rate > 196000 ? 196000 : rate;
|
|
Packit |
4a16fb |
break;
|
|
Packit |
4a16fb |
case 'c':
|
|
Packit |
4a16fb |
channels = atoi(optarg);
|
|
Packit |
4a16fb |
channels = channels < 1 ? 1 : channels;
|
|
Packit |
4a16fb |
channels = channels > 1024 ? 1024 : channels;
|
|
Packit |
4a16fb |
break;
|
|
Packit |
4a16fb |
case 'f':
|
|
Packit |
4a16fb |
freq = atoi(optarg);
|
|
Packit |
4a16fb |
freq = freq < 50 ? 50 : freq;
|
|
Packit |
4a16fb |
freq = freq > 5000 ? 5000 : freq;
|
|
Packit |
4a16fb |
break;
|
|
Packit |
4a16fb |
case 'b':
|
|
Packit |
4a16fb |
buffer_time = atoi(optarg);
|
|
Packit |
4a16fb |
buffer_time = buffer_time < 1000 ? 1000 : buffer_time;
|
|
Packit |
4a16fb |
buffer_time = buffer_time > 1000000 ? 1000000 : buffer_time;
|
|
Packit |
4a16fb |
break;
|
|
Packit |
4a16fb |
case 'p':
|
|
Packit |
4a16fb |
period_time = atoi(optarg);
|
|
Packit |
4a16fb |
period_time = period_time < 1000 ? 1000 : period_time;
|
|
Packit |
4a16fb |
period_time = period_time > 1000000 ? 1000000 : period_time;
|
|
Packit |
4a16fb |
break;
|
|
Packit |
4a16fb |
case 'm':
|
|
Packit |
4a16fb |
for (method = 0; transfer_methods[method].name; method++)
|
|
Packit |
4a16fb |
if (!strcasecmp(transfer_methods[method].name, optarg))
|
|
Packit |
4a16fb |
break;
|
|
Packit |
4a16fb |
if (transfer_methods[method].name == NULL)
|
|
Packit |
4a16fb |
method = 0;
|
|
Packit |
4a16fb |
break;
|
|
Packit |
4a16fb |
case 'o':
|
|
Packit |
4a16fb |
for (format = 0; format < SND_PCM_FORMAT_LAST; format++) {
|
|
Packit |
4a16fb |
const char *format_name = snd_pcm_format_name(format);
|
|
Packit |
4a16fb |
if (format_name)
|
|
Packit |
4a16fb |
if (!strcasecmp(format_name, optarg))
|
|
Packit |
4a16fb |
break;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
if (format == SND_PCM_FORMAT_LAST)
|
|
Packit |
4a16fb |
format = SND_PCM_FORMAT_S16;
|
|
Packit |
4a16fb |
if (!snd_pcm_format_linear(format) &&
|
|
Packit |
4a16fb |
!(format == SND_PCM_FORMAT_FLOAT_LE ||
|
|
Packit |
4a16fb |
format == SND_PCM_FORMAT_FLOAT_BE)) {
|
|
Packit |
4a16fb |
printf("Invalid (non-linear/float) format %s\n",
|
|
Packit |
4a16fb |
optarg);
|
|
Packit |
4a16fb |
return 1;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
break;
|
|
Packit |
4a16fb |
case 'v':
|
|
Packit |
4a16fb |
verbose = 1;
|
|
Packit |
4a16fb |
break;
|
|
Packit |
4a16fb |
case 'n':
|
|
Packit |
4a16fb |
resample = 0;
|
|
Packit |
4a16fb |
break;
|
|
Packit |
4a16fb |
case 'e':
|
|
Packit |
4a16fb |
period_event = 1;
|
|
Packit |
4a16fb |
break;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
if (morehelp) {
|
|
Packit |
4a16fb |
help();
|
|
Packit |
4a16fb |
return 0;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
err = snd_output_stdio_attach(&output, stdout, 0);
|
|
Packit |
4a16fb |
if (err < 0) {
|
|
Packit |
4a16fb |
printf("Output failed: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
return 0;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
printf("Playback device is %s\n", device);
|
|
Packit |
4a16fb |
printf("Stream parameters are %uHz, %s, %u channels\n", rate, snd_pcm_format_name(format), channels);
|
|
Packit |
4a16fb |
printf("Sine wave rate is %.4fHz\n", freq);
|
|
Packit |
4a16fb |
printf("Using transfer method: %s\n", transfer_methods[method].name);
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
|
|
Packit |
4a16fb |
printf("Playback open error: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
return 0;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
if ((err = set_hwparams(handle, hwparams, transfer_methods[method].access)) < 0) {
|
|
Packit |
4a16fb |
printf("Setting of hwparams failed: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
if ((err = set_swparams(handle, swparams)) < 0) {
|
|
Packit |
4a16fb |
printf("Setting of swparams failed: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
if (verbose > 0)
|
|
Packit |
4a16fb |
snd_pcm_dump(handle, output);
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
samples = malloc((period_size * channels * snd_pcm_format_physical_width(format)) / 8);
|
|
Packit |
4a16fb |
if (samples == NULL) {
|
|
Packit |
4a16fb |
printf("No enough memory\n");
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
areas = calloc(channels, sizeof(snd_pcm_channel_area_t));
|
|
Packit |
4a16fb |
if (areas == NULL) {
|
|
Packit |
4a16fb |
printf("No enough memory\n");
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
for (chn = 0; chn < channels; chn++) {
|
|
Packit |
4a16fb |
areas[chn].addr = samples;
|
|
Packit |
4a16fb |
areas[chn].first = chn * snd_pcm_format_physical_width(format);
|
|
Packit |
4a16fb |
areas[chn].step = channels * snd_pcm_format_physical_width(format);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
err = transfer_methods[method].transfer_loop(handle, samples, areas);
|
|
Packit |
4a16fb |
if (err < 0)
|
|
Packit |
4a16fb |
printf("Transfer failed: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
free(areas);
|
|
Packit |
4a16fb |
free(samples);
|
|
Packit |
4a16fb |
snd_pcm_close(handle);
|
|
Packit |
4a16fb |
return 0;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|