tests: add some options

Use S16_LE as the sampleformat, add option for S32_LE
Add support for setting samplerate and channels.
This commit is contained in:
Wim Taymans 2022-03-13 15:07:23 +01:00
parent 845c9e1075
commit cec8898740

View file

@ -25,6 +25,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
#include <getopt.h>
#include <math.h>
#include <sys/timerfd.h>
@ -42,6 +43,8 @@
#define BW_PERIOD (NSEC_PER_SEC * 3)
struct state {
const char *device;
unsigned int format;
unsigned int rate;
unsigned int channels;
snd_pcm_uframes_t period;
@ -77,27 +80,39 @@ static int set_timeout(struct state *state, uint64_t time)
} \
}
#define LOOP(type,areas,scale) { \
uint32_t i, j; \
type *samples, v; \
samples = (type*)((uint8_t*)areas[0].addr + (areas[0].first + offset*areas[0].step) / 8); \
for (i = 0; i < frames; i++) { \
state->accumulator += M_PI_M2 * 440 / state->rate; \
if (state->accumulator >= M_PI_M2) \
state->accumulator -= M_PI_M2; \
v = sin(state->accumulator) * scale; \
for (j = 0; j < state->channels; j++) \
*samples++ = v; \
} \
}
static int write_period(struct state *state)
{
snd_pcm_uframes_t frames = state->period;
snd_pcm_uframes_t offset;
const snd_pcm_channel_area_t* areas;
uint32_t i, j;
int32_t *samples, v;
snd_pcm_mmap_begin(state->hndl, &areas, &offset, &frames);
samples = (int32_t*)((uint8_t*)areas[0].addr + (areas[0].first + offset*areas[0].step) / 8);
for (i = 0; i < frames; i++) {
state->accumulator += M_PI_M2 * 440 / state->rate;
if (state->accumulator >= M_PI_M2)
state->accumulator -= M_PI_M2;
v = sin(state->accumulator) * 0x7fffffff;
for (j = 0; j < state->channels; j++)
*samples++ = v;
switch (state->format) {
case SND_PCM_FORMAT_S32_LE:
LOOP(int32_t, areas, 0x7fffffff);
break;
case SND_PCM_FORMAT_S16_LE:
LOOP(int16_t, areas, 0x7fff);
break;
default:
break;
}
snd_pcm_mmap_commit(state->hndl, offset, frames) ;
return 0;
@ -156,27 +171,89 @@ static int on_timer_wakeup(struct state *state)
return 0;
}
static unsigned int format_from_string(const char *str)
{
if (strcmp(str, "S32_LE") == 0)
return SND_PCM_FORMAT_S32_LE;
else if (strcmp(str, "S32_BE") == 0)
return SND_PCM_FORMAT_S32_BE;
else if (strcmp(str, "S24_LE") == 0)
return SND_PCM_FORMAT_S24_LE;
else if (strcmp(str, "S24_BE") == 0)
return SND_PCM_FORMAT_S24_BE;
else if (strcmp(str, "S24_3LE") == 0)
return SND_PCM_FORMAT_S24_3LE;
else if (strcmp(str, "S24_3_BE") == 0)
return SND_PCM_FORMAT_S24_3BE;
else if (strcmp(str, "S16_LE") == 0)
return SND_PCM_FORMAT_S16_LE;
else if (strcmp(str, "S16_BE") == 0)
return SND_PCM_FORMAT_S16_BE;
return 0;
}
static void show_help(const char *name, bool error)
{
fprintf(error ? stderr : stdout, "%s [options]\n"
" -h, --help Show this help\n"
" -D, --device device name (default %s)\n",
name, DEFAULT_DEVICE);
}
int main(int argc, char *argv[])
{
struct state state = { 0, };
const char *device = DEFAULT_DEVICE;
snd_pcm_hw_params_t *hparams;
snd_pcm_sw_params_t *sparams;
struct timespec now;
CHECK(snd_pcm_open(&state.hndl, device, SND_PCM_STREAM_PLAYBACK, 0), "open %s failed", device);
char c;
static const struct option long_options[] = {
{ "help", no_argument, NULL, 'h' },
{ "device", required_argument, NULL, 'D' },
{ "format", required_argument, NULL, 'f' },
{ "rate", required_argument, NULL, 'r' },
{ "channels", required_argument, NULL, 'c' },
{ NULL, 0, NULL, 0}
};
state.device = DEFAULT_DEVICE;
state.format = SND_PCM_FORMAT_S16_LE;
state.rate = 44100;
state.channels = 2;
state.period = 1024;
while ((c = getopt_long(argc, argv, "hD:f:r:c:", long_options, NULL)) != -1) {
switch (c) {
case 'h':
show_help(argv[0], false);
return 0;
case 'D':
state.device = optarg;
break;
case 'f':
state.format = format_from_string(optarg);
break;
case 'r':
state.rate = atoi(optarg);
break;
case 'c':
state.channels = atoi(optarg);
break;
default:
show_help(argv[0], true);
return -1;
}
}
CHECK(snd_pcm_open(&state.hndl, state.device, SND_PCM_STREAM_PLAYBACK, 0),
"open %s failed", state.device);
/* hw params */
snd_pcm_hw_params_alloca(&hparams);
snd_pcm_hw_params_any(state.hndl, hparams);
CHECK(snd_pcm_hw_params_set_access(state.hndl, hparams,
SND_PCM_ACCESS_MMAP_INTERLEAVED), "set interleaved");
CHECK(snd_pcm_hw_params_set_format(state.hndl, hparams,
SND_PCM_FORMAT_S32_LE), "set format");
state.format), "set format");
CHECK(snd_pcm_hw_params_set_channels_near(state.hndl, hparams,
&state.channels), "set channels");
CHECK(snd_pcm_hw_params_set_rate_near(state.hndl, hparams,
@ -186,7 +263,7 @@ int main(int argc, char *argv[])
CHECK(snd_pcm_hw_params_get_buffer_size(hparams, &state.buffer_frames), "get_buffer_size_max");
fprintf(stdout, "opened format:%s rate:%u channels:%u\n",
snd_pcm_format_name(SND_PCM_FORMAT_S32_LE),
snd_pcm_format_name(state.format),
state.rate, state.channels);
snd_pcm_sw_params_alloca(&sparams);