2 * Copyright (C) 2000-2004 James Courtier-Dutton
3 * Copyright (C) 2005 Nathan Hurst
5 * This file is part of the speaker-test tool.
7 * This small program sends a simple sinusoidal wave to your speakers.
9 * speaker-test is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * speaker-test is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
24 * Main program by James Courtier-Dutton (including some source code fragments from the alsa project.)
25 * Some cleanup from Daniel Caujolle-Bert <segfault@club-internet.fr>
26 * Pink noise option added Nathan Hurst,
27 * based on generator by Phil Burk (pink.c)
28 * ST-2095 noise option added Rick Sayre,
29 * based on generator specified by SMPTE ST-2095:1-2015
30 * Also switched to stable harmonic oscillator for sine
33 * 0.0.9 Added support for ST-2095 band-limited pink noise output, switched to harmonic oscillator for sine
35 * 0.0.8 Added support for pink noise output.
37 * 0.0.7 Added support for more than 6 channels.
39 * 0.0.6 Added support for different sample formats.
41 * $Id: speaker_test.c,v 1.00 2003/11/26 19:43:38 jcdutton Exp $
58 #define ALSA_PCM_NEW_HW_PARAMS_API
59 #define ALSA_PCM_NEW_SW_PARAMS_API
60 #include <alsa/asoundlib.h>
67 #include "os_compat.h"
73 #ifdef SND_CHMAP_API_VERSION
74 #define CONFIG_SUPPORT_CHMAP 1
85 #define MAX_CHANNELS 16
87 #if __BYTE_ORDER == __LITTLE_ENDIAN
88 #define COMPOSE_ID(a,b,c,d) ((a) | ((b)<<8) | ((c)<<16) | ((d)<<24))
89 #define LE_SHORT(v) (v)
91 #define BE_SHORT(v) bswap_16(v)
92 #define BE_INT(v) bswap_32(v)
93 #else /* __BIG_ENDIAN */
94 #define COMPOSE_ID(a,b,c,d) ((d) | ((c)<<8) | ((b)<<16) | ((a)<<24))
95 #define LE_SHORT(v) bswap_16(v)
96 #define LE_INT(v) bswap_32(v)
97 #define BE_SHORT(v) (v)
101 #define ARRAY_SIZE(x) (int)(sizeof(x)/sizeof(x[0]))
103 static char *device = "default"; /* playback device */
104 static snd_pcm_format_t format = SND_PCM_FORMAT_S16; /* sample format */
105 static unsigned int rate = 48000; /* stream rate */
106 static unsigned int channels = 1; /* count of channels */
107 static unsigned int speaker = 0; /* count of channels */
108 static unsigned int buffer_time = 0; /* ring buffer length in us */
109 static unsigned int period_time = UINT_MAX; /* period time in us */
110 static unsigned int nperiods = 4; /* number of periods */
111 static double freq = 440.0; /* sinusoidal wave frequency in Hz */
112 static int test_type = TEST_PINK_NOISE; /* Test type. 1 = noise, 2 = sine wave */
113 static float generator_scale = 0.8; /* Scale to use for sine volume */
114 static snd_pcm_uframes_t buffer_size;
115 static snd_pcm_uframes_t period_size;
116 static const char *given_test_wav_file = NULL;
117 static char *wav_file_dir = SOUNDSDIR;
118 static int debug = 0;
119 static int force_frequency = 0;
120 static int in_aborting = 0;
121 static snd_pcm_t *pcm_handle = NULL;
123 #ifdef CONFIG_SUPPORT_CHMAP
124 static snd_pcm_chmap_t *channel_map;
125 static int channel_map_set;
126 static int *ordered_channels;
129 static const char *const channel_name[MAX_CHANNELS] = {
130 /* 0 */ N_("Front Left"),
131 /* 1 */ N_("Front Right"),
132 /* 2 */ N_("Rear Left"),
133 /* 3 */ N_("Rear Right"),
134 /* 4 */ N_("Center"),
136 /* 6 */ N_("Side Left"),
137 /* 7 */ N_("Side Right"),
138 /* 8 */ N_("Channel 9"),
139 /* 9 */ N_("Channel 10"),
140 /* 10 */ N_("Channel 11"),
141 /* 11 */ N_("Channel 12"),
142 /* 12 */ N_("Channel 13"),
143 /* 13 */ N_("Channel 14"),
144 /* 14 */ N_("Channel 15"),
145 /* 15 */ N_("Channel 16")
148 static const int channels4[] = {
154 static const int channels6[] = {
162 static const int channels8[] = {
173 #ifdef CONFIG_SUPPORT_CHMAP
174 /* circular clockwise and bottom-to-top order */
175 static const int channel_order[] = {
176 [SND_CHMAP_FLW] = 10,
178 [SND_CHMAP_TFL] = 30,
179 [SND_CHMAP_FLC] = 40,
180 [SND_CHMAP_TFLC] = 50,
182 [SND_CHMAP_TFC] = 70,
183 [SND_CHMAP_FRC] = 80,
184 [SND_CHMAP_TFRC] = 90,
185 [SND_CHMAP_FR] = 100,
186 [SND_CHMAP_TFR] = 110,
187 [SND_CHMAP_FRW] = 120,
188 [SND_CHMAP_SR] = 130,
189 [SND_CHMAP_TSR] = 140,
190 [SND_CHMAP_RR] = 150,
191 [SND_CHMAP_TRR] = 160,
192 [SND_CHMAP_RRC] = 170,
193 [SND_CHMAP_RC] = 180,
194 [SND_CHMAP_TRC] = 190,
195 [SND_CHMAP_RLC] = 200,
196 [SND_CHMAP_RL] = 210,
197 [SND_CHMAP_TRL] = 220,
198 [SND_CHMAP_SL] = 230,
199 [SND_CHMAP_TSL] = 240,
200 [SND_CHMAP_BC] = 250,
201 [SND_CHMAP_TC] = 260,
202 [SND_CHMAP_LLFE] = 270,
203 [SND_CHMAP_LFE] = 280,
204 [SND_CHMAP_RLFE] = 290,
205 /* not in table = 10000 */
206 [SND_CHMAP_UNKNOWN] = 20000,
207 [SND_CHMAP_NA] = 30000,
210 static int chpos_cmp(const void *chnum1p, const void *chnum2p)
212 int chnum1 = *(int *)chnum1p;
213 int chnum2 = *(int *)chnum2p;
214 int chpos1 = channel_map->pos[chnum1];
215 int chpos2 = channel_map->pos[chnum2];
219 if (chpos1 < ARRAY_SIZE(channel_order) && channel_order[chpos1])
220 weight1 = channel_order[chpos1];
221 if (chpos2 < ARRAY_SIZE(channel_order) && channel_order[chpos2])
222 weight2 = channel_order[chpos2];
224 if (weight1 == weight2) {
225 /* order by channel number if both have the same position (e.g. UNKNOWN)
226 * or if neither is in channel_order[] */
227 return chnum1 - chnum2;
230 /* order according to channel_order[] */
231 return weight1 - weight2;
234 static int *order_channels(void)
236 /* create a (playback order => channel number) table with channels ordered
237 * according to channel_order[] values */
241 ordered_chs = calloc(channel_map->channels, sizeof(*ordered_chs));
245 for (i = 0; i < channel_map->channels; i++)
248 qsort(ordered_chs, channel_map->channels, sizeof(*ordered_chs), chpos_cmp);
254 static int get_speaker_channel(int chn)
256 #ifdef CONFIG_SUPPORT_CHMAP
257 if (channel_map_set || (ordered_channels && (unsigned int)chn >= channel_map->channels))
259 if (ordered_channels)
260 return ordered_channels[chn];
265 chn = channels4[chn];
268 chn = channels6[chn];
271 chn = channels8[chn];
278 static const char *get_channel_name(int chn)
280 #ifdef CONFIG_SUPPORT_CHMAP
282 const char *name = NULL;
283 if ((unsigned int)chn < channel_map->channels)
284 name = snd_pcm_chmap_long_name(channel_map->pos[chn]);
285 return name ? name : "Unknown";
288 return gettext(channel_name[chn]);
291 static const int supported_formats[] = {
293 SND_PCM_FORMAT_S16_LE,
294 SND_PCM_FORMAT_S16_BE,
295 SND_PCM_FORMAT_FLOAT_LE,
296 SND_PCM_FORMAT_S24_3LE,
297 SND_PCM_FORMAT_S24_3BE,
298 SND_PCM_FORMAT_S24_LE,
299 SND_PCM_FORMAT_S24_BE,
300 SND_PCM_FORMAT_S32_LE,
301 SND_PCM_FORMAT_S32_BE,
310 static void do_generate(uint8_t *frames, int channel, int count,
311 value_t (*generate)(void *), void *arg)
315 int8_t *samp8 = (int8_t*) frames;
316 int16_t *samp16 = (int16_t*) frames;
317 int32_t *samp32 = (int32_t*) frames;
318 float *samp_f = (float*) frames;
320 while (count-- > 0) {
321 for(chn=0;chn<channels;chn++) {
322 if (chn==(unsigned int)channel) {
329 case SND_PCM_FORMAT_S8:
330 *samp8++ = res.i >> 24;
332 case SND_PCM_FORMAT_S16_LE:
333 *samp16++ = LE_SHORT(res.i >> 16);
335 case SND_PCM_FORMAT_S16_BE:
336 *samp16++ = BE_SHORT(res.i >> 16);
338 case SND_PCM_FORMAT_FLOAT_LE:
341 case SND_PCM_FORMAT_S24_3LE:
343 *samp8++ = LE_INT(res.i);
344 *samp8++ = LE_INT(res.i) >> 8;
345 *samp8++ = LE_INT(res.i) >> 16;
347 case SND_PCM_FORMAT_S24_3BE:
349 *samp8++ = BE_INT(res.i);
350 *samp8++ = BE_INT(res.i) >> 8;
351 *samp8++ = BE_INT(res.i) >> 16;
353 case SND_PCM_FORMAT_S24_LE:
355 *samp8++ = LE_INT(res.i);
356 *samp8++ = LE_INT(res.i) >> 8;
357 *samp8++ = LE_INT(res.i) >> 16;
360 case SND_PCM_FORMAT_S24_BE:
363 *samp8++ = BE_INT(res.i);
364 *samp8++ = BE_INT(res.i) >> 8;
365 *samp8++ = BE_INT(res.i) >> 16;
367 case SND_PCM_FORMAT_S32_LE:
368 *samp32++ = LE_INT(res.i);
370 case SND_PCM_FORMAT_S32_BE:
371 *samp32++ = BE_INT(res.i);
389 static void init_sine(sine_t *sine)
391 // symplectic integration for fast, stable harmonic oscillator
392 sine->a = 2.0*M_PI * freq / rate;
397 static value_t generate_sine(void *arg)
402 res.f = sine->s * generator_scale;
403 if (format != SND_PCM_FORMAT_FLOAT_LE)
404 res.i = res.f * INT32_MAX;
406 // update the oscillator
407 sine->c -= sine->a * sine->s;
408 sine->s += sine->a * sine->c;
412 /* Pink noise is a better test than sine wave because we can tell
413 * where pink noise is coming from more easily that a sine wave.
415 static value_t generate_pink_noise(void *arg)
417 pink_noise_t *pink = arg;
420 res.f = generate_pink_noise_sample(pink) * generator_scale;
421 if (format != SND_PCM_FORMAT_FLOAT_LE)
422 res.i = res.f * INT32_MAX;
426 /* Band-Limited Pink Noise, per SMPTE ST 2095-1
427 * beyond speaker localization, this can be used for setting loudness to standard
429 static value_t generate_st2095_noise(void *arg)
431 st2095_noise_t *st2095 = arg;
434 res.f = generate_st2095_noise_sample(st2095);
435 if (format != SND_PCM_FORMAT_FLOAT_LE)
436 res.i = res.f * INT32_MAX;
443 static value_t generate_pattern(void *arg)
448 *(int *)arg = res.i + 1;
449 if (format != SND_PCM_FORMAT_FLOAT_LE)
450 res.f = (float)res.i / (float)INT32_MAX;
454 static int set_hwparams(snd_pcm_t *handle, snd_pcm_hw_params_t *params, snd_pcm_access_t access) {
457 snd_pcm_uframes_t period_size_min;
458 snd_pcm_uframes_t period_size_max;
459 snd_pcm_uframes_t buffer_size_min;
460 snd_pcm_uframes_t buffer_size_max;
462 /* choose all parameters */
463 err = snd_pcm_hw_params_any(handle, params);
465 fprintf(stderr, _("Broken configuration for playback: no configurations available: %s\n"), snd_strerror(err));
469 /* set the interleaved read/write format */
470 err = snd_pcm_hw_params_set_access(handle, params, access);
472 fprintf(stderr, _("Access type not available for playback: %s\n"), snd_strerror(err));
476 /* set the sample format */
477 err = snd_pcm_hw_params_set_format(handle, params, format);
479 fprintf(stderr, _("Sample format not available for playback: %s\n"), snd_strerror(err));
483 /* set the count of channels */
484 err = snd_pcm_hw_params_set_channels(handle, params, channels);
486 fprintf(stderr, _("Channels count (%i) not available for playbacks: %s\n"), channels, snd_strerror(err));
490 /* set the stream rate */
492 err = snd_pcm_hw_params_set_rate(handle, params, rate, 0);
494 fprintf(stderr, _("Rate %iHz not available for playback: %s\n"), rate, snd_strerror(err));
499 fprintf(stderr, _("Rate doesn't match (requested %iHz, get %iHz, err %d)\n"), rate, rrate, err);
503 printf(_("Rate set to %iHz (requested %iHz)\n"), rrate, rate);
504 /* set the buffer time */
505 err = snd_pcm_hw_params_get_buffer_size_min(params, &buffer_size_min);
506 err = snd_pcm_hw_params_get_buffer_size_max(params, &buffer_size_max);
507 err = snd_pcm_hw_params_get_period_size_min(params, &period_size_min, NULL);
508 err = snd_pcm_hw_params_get_period_size_max(params, &period_size_max, NULL);
509 printf(_("Buffer size range from %lu to %lu\n"),buffer_size_min, buffer_size_max);
510 printf(_("Period size range from %lu to %lu\n"),period_size_min, period_size_max);
511 if (period_time > 0) {
512 unsigned int tmp = period_time;
513 if (period_time > 0 && period_time < UINT_MAX)
514 printf(_("Requested period time %u us\n"), period_time);
516 tmp = 250000; /* 0.25 second */
517 err = snd_pcm_hw_params_set_period_time_near(handle, params, &tmp, NULL);
519 fprintf(stderr, _("Unable to set period time %u us for playback: %s\n"),
520 tmp, snd_strerror(err));
524 if (buffer_time > 0) {
525 printf(_("Requested buffer time %u us\n"), buffer_time);
526 err = snd_pcm_hw_params_set_buffer_time_near(handle, params, &buffer_time, NULL);
528 fprintf(stderr, _("Unable to set buffer time %u us for playback: %s\n"),
529 buffer_time, snd_strerror(err));
533 if (! buffer_time && ! period_time) {
534 buffer_size = buffer_size_max;
536 buffer_size = (buffer_size / nperiods) * nperiods;
537 printf(_("Using max buffer size %lu\n"), buffer_size);
538 err = snd_pcm_hw_params_set_buffer_size_near(handle, params, &buffer_size);
540 fprintf(stderr, _("Unable to set buffer size %lu for playback: %s\n"),
541 buffer_size, snd_strerror(err));
545 if (! buffer_time || ! period_time) {
546 printf(_("Periods = %u\n"), nperiods);
547 err = snd_pcm_hw_params_set_periods_near(handle, params, &nperiods, NULL);
549 fprintf(stderr, _("Unable to set nperiods %u for playback: %s\n"),
550 nperiods, snd_strerror(err));
555 /* write the parameters to device */
556 err = snd_pcm_hw_params(handle, params);
558 fprintf(stderr, _("Unable to set hw params for playback: %s\n"), snd_strerror(err));
562 snd_pcm_hw_params_get_buffer_size(params, &buffer_size);
563 snd_pcm_hw_params_get_period_size(params, &period_size, NULL);
564 printf(_("was set period_size = %lu\n"),period_size);
565 printf(_("was set buffer_size = %lu\n"),buffer_size);
566 if (2*period_size > buffer_size) {
567 fprintf(stderr, _("buffer to small, could not use\n"));
574 static int set_swparams(snd_pcm_t *handle, snd_pcm_sw_params_t *swparams) {
577 /* get the current swparams */
578 err = snd_pcm_sw_params_current(handle, swparams);
580 fprintf(stderr, _("Unable to determine current swparams for playback: %s\n"), snd_strerror(err));
584 /* start the transfer when a buffer is full */
585 err = snd_pcm_sw_params_set_start_threshold(handle, swparams, buffer_size);
587 fprintf(stderr, _("Unable to set start threshold mode for playback: %s\n"), snd_strerror(err));
591 /* allow the transfer when at least period_size frames can be processed */
592 err = snd_pcm_sw_params_set_avail_min(handle, swparams, period_size);
594 fprintf(stderr, _("Unable to set avail min for playback: %s\n"), snd_strerror(err));
598 /* write the parameters to the playback device */
599 err = snd_pcm_sw_params(handle, swparams);
601 fprintf(stderr, _("Unable to set sw params for playback: %s\n"), snd_strerror(err));
608 #ifdef CONFIG_SUPPORT_CHMAP
609 static int config_chmap(snd_pcm_t *handle, const char *mapstr)
614 channel_map = snd_pcm_chmap_parse_string(mapstr);
616 fprintf(stderr, _("Unable to parse channel map string: %s\n"), mapstr);
619 err = snd_pcm_set_chmap(handle, channel_map);
621 fprintf(stderr, _("Unable to set channel map: %s\n"), mapstr);
628 channel_map = snd_pcm_get_chmap(handle);
630 /* create a channel order table for default layouts */
632 ordered_channels = order_channels();
639 * Underrun and suspend recovery
642 static int xrun_recovery(snd_pcm_t *handle, int err) {
643 if (err == -EPIPE) { /* under-run */
644 err = snd_pcm_prepare(handle);
646 fprintf(stderr, _("Can't recovery from underrun, prepare failed: %s\n"), snd_strerror(err));
649 else if (err == -ESTRPIPE) {
651 while ((err = snd_pcm_resume(handle)) == -EAGAIN)
652 sleep(1); /* wait until the suspend flag is released */
655 err = snd_pcm_prepare(handle);
657 fprintf(stderr, _("Can't recovery from suspend, prepare failed: %s\n"), snd_strerror(err));
670 static const char *wav_file[MAX_CHANNELS];
671 static int wav_file_size[MAX_CHANNELS];
687 uint32_t bytes_per_sec;
688 uint16_t sample_size;
689 uint16_t sample_bits;
697 #define WAV_RIFF COMPOSE_ID('R','I','F','F')
698 #define WAV_WAVE COMPOSE_ID('W','A','V','E')
699 #define WAV_FMT COMPOSE_ID('f','m','t',' ')
700 #define WAV_DATA COMPOSE_ID('d','a','t','a')
701 #define WAV_PCM_CODE 1
703 static const char *search_for_file(const char *name)
708 file = malloc(strlen(wav_file_dir) + strlen(name) + 2);
710 sprintf(file, "%s/%s", wav_file_dir, name);
714 static int check_wav_file(int channel, const char *name)
716 struct wave_header header;
719 wav_file[channel] = search_for_file(name);
720 if (! wav_file[channel]) {
721 fprintf(stderr, _("No enough memory\n"));
725 if ((fd = open(wav_file[channel], O_RDONLY)) < 0) {
726 fprintf(stderr, _("Cannot open WAV file %s\n"), wav_file[channel]);
729 if (read(fd, &header, sizeof(header)) < (int)sizeof(header)) {
730 fprintf(stderr, _("Invalid WAV file %s\n"), wav_file[channel]);
734 if (header.hdr.magic != WAV_RIFF || header.hdr.type != WAV_WAVE) {
735 fprintf(stderr, _("Not a WAV file: %s\n"), wav_file[channel]);
738 if (header.body.format != LE_SHORT(WAV_PCM_CODE)) {
739 fprintf(stderr, _("Unsupported WAV format %d for %s\n"),
740 LE_SHORT(header.body.format), wav_file[channel]);
743 if (header.body.channels != LE_SHORT(1)) {
744 fprintf(stderr, _("%s is not a mono stream (%d channels)\n"),
745 wav_file[channel], LE_SHORT(header.body.channels));
748 if (header.body.rate != LE_INT(rate)) {
749 fprintf(stderr, _("Sample rate doesn't match (%d) for %s\n"),
750 LE_INT(header.body.rate), wav_file[channel]);
753 if (header.body.sample_bits != LE_SHORT(16)) {
754 fprintf(stderr, _("Unsupported sample format bits %d for %s\n"),
755 LE_SHORT(header.body.sample_bits), wav_file[channel]);
758 if (header.chunk.type != WAV_DATA) {
759 fprintf(stderr, _("Invalid WAV file %s\n"), wav_file[channel]);
762 wav_file_size[channel] = LE_INT(header.chunk.length);
771 static int setup_wav_file(int chn)
773 static const char *const wavs[MAX_CHANNELS] = {
779 "Rear_Center.wav", /* FIXME: should be "Bass" or so */
792 if (given_test_wav_file)
793 return check_wav_file(chn, given_test_wav_file);
795 #ifdef CONFIG_SUPPORT_CHMAP
796 if (channel_map && (unsigned int)chn < channel_map->channels) {
797 int channel = channel_map->pos[chn] - SND_CHMAP_FL;
798 if (channel >= 0 && channel < MAX_CHANNELS)
799 return check_wav_file(chn, wavs[channel]);
803 return check_wav_file(chn, wavs[chn]);
806 static int read_wav(uint16_t *buf, int channel, int offset, int bufsize)
808 static FILE *wavfp = NULL;
814 if (! wav_file[channel]) {
815 fprintf(stderr, _("Undefined channel %d\n"), channel);
819 if (offset >= wav_file_size[channel])
820 return 0; /* finished */
825 wavfp = fopen(wav_file[channel], "r");
828 if (fseek(wavfp, sizeof(struct wave_header), SEEK_SET) < 0)
831 if (offset + bufsize > wav_file_size[channel])
832 bufsize = wav_file_size[channel] - offset;
834 for (size = 0; size < bufsize; size += 2) {
836 for (chn = 0; chn < channels; chn++) {
837 if (chn == (unsigned int)channel) {
838 if (fread(buf, 2, 1, wavfp) != 1)
851 * Transfer method - write only
854 static int write_buffer(snd_pcm_t *handle, uint8_t *ptr, int cptr)
858 while (cptr > 0 && !in_aborting) {
860 err = snd_pcm_writei(handle, ptr, cptr);
866 fprintf(stderr, _("Write error: %d,%s\n"), err, snd_strerror(err));
867 if ((err = xrun_recovery(handle, err)) < 0) {
868 fprintf(stderr, _("xrun_recovery failed: %d,%s\n"), err, snd_strerror(err));
871 break; /* skip one period */
874 ptr += snd_pcm_frames_to_bytes(handle, err);
882 static pink_noise_t pink;
883 static st2095_noise_t st2095;
885 static void init_loop(void)
888 case TEST_ST2095_NOISE:
889 initialize_st2095_noise(&st2095, rate);
891 case TEST_PINK_NOISE:
892 initialize_pink_noise(&pink, 16);
903 static int write_loop(snd_pcm_t *handle, int channel, int periods, uint8_t *frames)
910 if (test_type == TEST_WAV) {
911 int bufsize = snd_pcm_frames_to_bytes(handle, period_size);
913 while ((err = read_wav((uint16_t *)frames, channel, cnt, bufsize)) > 0 && !in_aborting) {
915 if ((err = write_buffer(handle, frames,
916 snd_pcm_bytes_to_frames(handle, err * channels))) < 0)
919 if (buffer_size > cnt && !in_aborting) {
920 snd_pcm_drain(handle);
921 snd_pcm_prepare(handle);
930 for(n = 0; n < periods && !in_aborting; n++) {
931 if (test_type == TEST_PINK_NOISE)
932 do_generate(frames, channel, period_size, generate_pink_noise, &pink);
933 else if (test_type == TEST_PATTERN)
934 do_generate(frames, channel, period_size, generate_pattern, &pattern);
935 else if (test_type == TEST_ST2095_NOISE) {
936 reset_st2095_noise_measurement(&st2095);
937 do_generate(frames, channel, period_size, generate_st2095_noise, &st2095);
938 printf(_("\tSMPTE ST-2095 noise batch was %2.2fdB RMS\n"),
939 compute_st2095_noise_measurement(&st2095, period_size));
941 do_generate(frames, channel, period_size, generate_sine, &sine);
943 if ((err = write_buffer(handle, frames, period_size)) < 0)
946 if (buffer_size > n * period_size && !in_aborting) {
947 snd_pcm_drain(handle);
948 snd_pcm_prepare(handle);
953 static int prg_exit(int code)
956 snd_pcm_close(pcm_handle);
961 static void signal_handler(int sig)
969 snd_pcm_abort(pcm_handle);
970 if (sig == SIGABRT) {
972 prg_exit(EXIT_FAILURE);
974 signal(sig, signal_handler);
977 static void help(void)
982 _("Usage: speaker-test [OPTION]... \n"
984 "-D,--device playback device\n"
985 "-r,--rate stream rate in Hz\n"
986 "-c,--channels count of channels in stream\n"
987 "-f,--frequency sine wave frequency in Hz\n"
988 "-F,--format sample format\n"
989 "-b,--buffer ring buffer size in us\n"
990 "-p,--period period size in us\n"
991 "-P,--nperiods number of periods\n"
992 "-t,--test pink=use pink noise, sine=use sine wave, st2095=use SMPTE ST-2095 noise, wav=WAV file\n"
993 "-l,--nloops specify number of loops to test, 0 = infinite\n"
994 "-s,--speaker single speaker test. Values 1=Left, 2=right, etc\n"
995 "-w,--wavfile Use the given WAV file as a test sound\n"
996 "-W,--wavdir Specify the directory containing WAV files\n"
997 "-m,--chmap Specify the channel map to override\n"
998 "-X,--force-frequency force frequencies outside the 30-8000hz range\n"
999 "-S,--scale Scale of generated test tones in percent (default=80)\n"
1001 printf(_("Recognized sample formats are:"));
1002 for (fmt = supported_formats; *fmt >= 0; fmt++) {
1003 const char *s = snd_pcm_format_name(*fmt);
1011 int main(int argc, char *argv[]) {
1014 snd_pcm_hw_params_t *hwparams;
1015 snd_pcm_sw_params_t *swparams;
1019 double time1,time2,time3;
1020 unsigned int n, nloops;
1021 struct timeval tv1,tv2;
1022 int speakeroptset = 0;
1023 #ifdef CONFIG_SUPPORT_CHMAP
1024 const char *chmap = NULL;
1027 static const struct option long_option[] = {
1028 {"help", 0, NULL, 'h'},
1029 {"device", 1, NULL, 'D'},
1030 {"rate", 1, NULL, 'r'},
1031 {"channels", 1, NULL, 'c'},
1032 {"frequency", 1, NULL, 'f'},
1033 {"format", 1, NULL, 'F'},
1034 {"buffer", 1, NULL, 'b'},
1035 {"period", 1, NULL, 'p'},
1036 {"nperiods", 1, NULL, 'P'},
1037 {"test", 1, NULL, 't'},
1038 {"nloops", 1, NULL, 'l'},
1039 {"speaker", 1, NULL, 's'},
1040 {"wavfile", 1, NULL, 'w'},
1041 {"wavdir", 1, NULL, 'W'},
1042 {"debug", 0, NULL, 'd'},
1043 {"force-frequency", 0, NULL, 'X'},
1044 {"scale", 1, NULL, 'S'},
1045 #ifdef CONFIG_SUPPORT_CHMAP
1046 {"chmap", 1, NULL, 'm'},
1048 {NULL, 0, NULL, 0 },
1052 setlocale(LC_ALL, "");
1053 textdomain(PACKAGE);
1056 snd_pcm_hw_params_alloca(&hwparams);
1057 snd_pcm_sw_params_alloca(&swparams);
1062 printf("\nspeaker-test %s\n\n", SND_UTIL_VERSION_STR);
1066 if ((c = getopt_long(argc, argv, "hD:r:c:f:F:b:p:P:t:l:s:w:W:d:XS:"
1067 #ifdef CONFIG_SUPPORT_CHMAP
1070 , long_option, NULL)) < 0)
1078 device = strdup(optarg);
1081 format = snd_pcm_format_value(optarg);
1082 for (fmt = supported_formats; *fmt >= 0; fmt++)
1086 fprintf(stderr, "Format %s is not supported...\n", snd_pcm_format_name(format));
1091 rate = atoi(optarg);
1092 rate = rate < 4000 ? 4000 : rate;
1093 rate = rate > 768000 ? 768000 : rate;
1096 channels = atoi(optarg);
1097 channels = channels < 1 ? 1 : channels;
1098 channels = channels > 1024 ? 1024 : channels;
1101 freq = atof(optarg);
1104 buffer_time = atoi(optarg);
1105 buffer_time = buffer_time > 100000000 ? 100000000 : buffer_time;
1108 period_time = atoi(optarg);
1109 period_time = period_time > 100000000 ? 100000000 : period_time;
1112 nperiods = atoi(optarg);
1113 if (nperiods < 2 || nperiods > 1024) {
1114 fprintf(stderr, _("Invalid number of periods %d\n"), nperiods);
1120 test_type = TEST_PINK_NOISE;
1121 else if (*optarg == 's') {
1122 if (optarg[1] == 'i')
1123 test_type = TEST_SINE;
1124 else if (optarg[1] == 't')
1125 test_type = TEST_ST2095_NOISE;
1127 fprintf(stderr, _("Invalid test type %s\n"), optarg);
1130 } else if (*optarg == 'w')
1131 test_type = TEST_WAV;
1132 else if (*optarg == 't')
1133 test_type = TEST_PATTERN;
1134 else if (isdigit(*optarg)) {
1135 test_type = atoi(optarg);
1136 if (test_type < TEST_PINK_NOISE || test_type > TEST_PATTERN) {
1137 fprintf(stderr, _("Invalid test type %s\n"), optarg);
1141 fprintf(stderr, _("Invalid test type %s\n"), optarg);
1146 nloops = atoi(optarg);
1149 speaker = atoi(optarg);
1150 speaker = speaker < 1 ? 0 : speaker;
1154 given_test_wav_file = optarg;
1157 wav_file_dir = optarg;
1163 force_frequency = 1;
1165 #ifdef CONFIG_SUPPORT_CHMAP
1171 generator_scale = atoi(optarg) / 100.0;
1174 fprintf(stderr, _("Unknown option '%c'\n"), c);
1185 if (speakeroptset) {
1186 speaker = speaker > channels ? 0 : speaker;
1188 fprintf(stderr, _("Invalid parameter for -s option.\n"));
1193 if (!force_frequency) {
1194 freq = freq < 30.0 ? 30.0 : freq;
1195 freq = freq > 8000.0 ? 8000.0 : freq;
1197 freq = freq < 1.0 ? 1.0 : freq;
1200 if (test_type == TEST_WAV)
1201 format = SND_PCM_FORMAT_S16_LE; /* fixed format */
1203 printf(_("Playback device is %s\n"), device);
1204 printf(_("Stream parameters are %iHz, %s, %i channels\n"), rate, snd_pcm_format_name(format), channels);
1205 switch (test_type) {
1206 case TEST_ST2095_NOISE:
1207 printf(_("Using SMPTE ST-2095 -18.5dB AES FS band-limited pink noise\n"));
1209 case TEST_PINK_NOISE:
1210 printf(_("Using 16 octaves of pink noise\n"));
1213 printf(_("Sine wave rate is %.4fHz\n"), freq);
1216 printf(_("WAV file(s)\n"));
1221 signal(SIGINT, signal_handler);
1222 signal(SIGTERM, signal_handler);
1223 signal(SIGABRT, signal_handler);
1225 if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
1226 printf(_("Playback open error: %d,%s\n"), err,snd_strerror(err));
1227 prg_exit(EXIT_FAILURE);
1229 pcm_handle = handle;
1231 if ((err = set_hwparams(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
1232 printf(_("Setting of hwparams failed: %s\n"), snd_strerror(err));
1233 prg_exit(EXIT_FAILURE);
1235 if ((err = set_swparams(handle, swparams)) < 0) {
1236 printf(_("Setting of swparams failed: %s\n"), snd_strerror(err));
1237 prg_exit(EXIT_FAILURE);
1240 #ifdef CONFIG_SUPPORT_CHMAP
1241 err = config_chmap(handle, chmap);
1243 prg_exit(EXIT_FAILURE);
1248 err = snd_output_stdio_attach(&log, stderr, 0);
1250 snd_pcm_dump(handle, log);
1251 snd_output_close(log);
1255 frames = malloc(snd_pcm_frames_to_bytes(handle, period_size));
1256 if (frames == NULL) {
1257 fprintf(stderr, _("No enough memory\n"));
1258 prg_exit(EXIT_FAILURE);
1265 if (test_type == TEST_WAV) {
1266 for (chn = 0; chn < channels; chn++) {
1267 if (setup_wav_file(get_speaker_channel(chn)) < 0)
1268 prg_exit(EXIT_FAILURE);
1272 for (n = 0; (! nloops || n < nloops) && !in_aborting; n++) {
1274 gettimeofday(&tv1, NULL);
1275 for(chn = 0; chn < channels; chn++) {
1276 int channel = get_speaker_channel(chn);
1277 printf(" %d - %s\n", channel, get_channel_name(channel));
1279 err = write_loop(handle, channel, ((rate*3)/period_size), frames);
1282 fprintf(stderr, _("Transfer failed: %s\n"), snd_strerror(err));
1283 prg_exit(EXIT_SUCCESS);
1286 gettimeofday(&tv2, NULL);
1287 time1 = (double)tv1.tv_sec + ((double)tv1.tv_usec / 1000000.0);
1288 time2 = (double)tv2.tv_sec + ((double)tv2.tv_usec / 1000000.0);
1289 time3 = time2 - time1;
1290 printf(_("Time per period = %lf\n"), time3 );
1293 chn = get_speaker_channel(speaker - 1);
1295 if (test_type == TEST_WAV) {
1296 if (setup_wav_file(chn) < 0)
1297 prg_exit(EXIT_FAILURE);
1300 printf(" - %s\n", get_channel_name(chn));
1301 err = write_loop(handle, chn, ((rate*5)/period_size), frames);
1304 fprintf(stderr, _("Transfer failed: %s\n"), snd_strerror(err));
1308 snd_pcm_drain(handle);
1311 #ifdef CONFIG_SUPPORT_CHMAP
1312 free(ordered_channels);
1315 return prg_exit(EXIT_SUCCESS);