Blame speaker-test/speaker-test.c

Packit Service a9274b
/*
Packit Service a9274b
 * Copyright (C) 2000-2004 James Courtier-Dutton
Packit Service a9274b
 * Copyright (C) 2005 Nathan Hurst
Packit Service a9274b
 *
Packit Service a9274b
 * This file is part of the speaker-test tool.
Packit Service a9274b
 *
Packit Service a9274b
 * This small program sends a simple sinusoidal wave to your speakers.
Packit Service a9274b
 *
Packit Service a9274b
 * speaker-test is free software; you can redistribute it and/or modify
Packit Service a9274b
 * it under the terms of the GNU General Public License as published by
Packit Service a9274b
 * the Free Software Foundation; either version 2 of the License, or
Packit Service a9274b
 * (at your option) any later version.
Packit Service a9274b
 *
Packit Service a9274b
 * speaker-test is distributed in the hope that it will be useful,
Packit Service a9274b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service a9274b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service a9274b
 * GNU General Public License for more details.
Packit Service a9274b
 *
Packit Service a9274b
 * You should have received a copy of the GNU General Public License
Packit Service a9274b
 * along with this program; if not, write to the Free Software
Packit Service a9274b
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
Packit Service a9274b
 *
Packit Service a9274b
 *
Packit Service a9274b
 * Main program by James Courtier-Dutton (including some source code fragments from the alsa project.)
Packit Service a9274b
 * Some cleanup from Daniel Caujolle-Bert <segfault@club-internet.fr>
Packit Service a9274b
 * Pink noise option added Nathan Hurst, 
Packit Service a9274b
 *   based on generator by Phil Burk (pink.c)
Packit Service a9274b
 *
Packit Service a9274b
 * Changelog:
Packit Service a9274b
 *   0.0.8 Added support for pink noise output.
Packit Service a9274b
 * Changelog:
Packit Service a9274b
 *   0.0.7 Added support for more than 6 channels.
Packit Service a9274b
 * Changelog:
Packit Service a9274b
 *   0.0.6 Added support for different sample formats.
Packit Service a9274b
 *
Packit Service a9274b
 * $Id: speaker_test.c,v 1.00 2003/11/26 19:43:38 jcdutton Exp $
Packit Service a9274b
 */
Packit Service a9274b
Packit Service a9274b
#include <stdio.h>
Packit Service a9274b
#include <stdlib.h>
Packit Service a9274b
#include <string.h>
Packit Service a9274b
#include <sched.h>
Packit Service a9274b
#include <errno.h>
Packit Service a9274b
#include <getopt.h>
Packit Service a9274b
#include <inttypes.h>
Packit Service a9274b
#include <ctype.h>
Packit Service a9274b
#include <byteswap.h>
Packit Service a9274b
#include <signal.h>
Packit Service a9274b
Packit Service a9274b
#define ALSA_PCM_NEW_HW_PARAMS_API
Packit Service a9274b
#define ALSA_PCM_NEW_SW_PARAMS_API
Packit Service a9274b
#include <alsa/asoundlib.h>
Packit Service a9274b
#include <sys/time.h>
Packit Service a9274b
#include <math.h>
Packit Service a9274b
#include "pink.h"
Packit Service a9274b
#include "aconfig.h"
Packit Service a9274b
#include "gettext.h"
Packit Service a9274b
#include "version.h"
Packit Service a9274b
Packit Service a9274b
#ifdef ENABLE_NLS
Packit Service a9274b
#include <locale.h>
Packit Service a9274b
#endif
Packit Service a9274b
Packit Service a9274b
#ifdef SND_CHMAP_API_VERSION
Packit Service a9274b
#define CONFIG_SUPPORT_CHMAP	1
Packit Service a9274b
#endif
Packit Service a9274b
Packit Service a9274b
enum {
Packit Service a9274b
  TEST_PINK_NOISE = 1,
Packit Service a9274b
  TEST_SINE,
Packit Service a9274b
  TEST_WAV,
Packit Service a9274b
  TEST_PATTERN,
Packit Service a9274b
};
Packit Service a9274b
Packit Service a9274b
#define MAX_CHANNELS	16
Packit Service a9274b
Packit Service a9274b
#if __BYTE_ORDER == __LITTLE_ENDIAN
Packit Service a9274b
#define COMPOSE_ID(a,b,c,d)	((a) | ((b)<<8) | ((c)<<16) | ((d)<<24))
Packit Service a9274b
#define LE_SHORT(v)		(v)
Packit Service a9274b
#define LE_INT(v)		(v)
Packit Service a9274b
#define BE_SHORT(v)		bswap_16(v)
Packit Service a9274b
#define BE_INT(v)		bswap_32(v)
Packit Service a9274b
#else /* __BIG_ENDIAN */
Packit Service a9274b
#define COMPOSE_ID(a,b,c,d)	((d) | ((c)<<8) | ((b)<<16) | ((a)<<24))
Packit Service a9274b
#define LE_SHORT(v)		bswap_16(v)
Packit Service a9274b
#define LE_INT(v)		bswap_32(v)
Packit Service a9274b
#define BE_SHORT(v)		(v)
Packit Service a9274b
#define BE_INT(v)		(v)
Packit Service a9274b
#endif
Packit Service a9274b
Packit Service a9274b
#define ARRAY_SIZE(x) (int)(sizeof(x)/sizeof(x[0]))
Packit Service a9274b
Packit Service a9274b
static char              *device      = "default";       /* playback device */
Packit Service a9274b
static snd_pcm_format_t   format      = SND_PCM_FORMAT_S16; /* sample format */
Packit Service a9274b
static unsigned int       rate        = 48000;	            /* stream rate */
Packit Service a9274b
static unsigned int       channels    = 1;	            /* count of channels */
Packit Service a9274b
static unsigned int       speaker     = 0;	            /* count of channels */
Packit Service a9274b
static unsigned int       buffer_time = 0;	            /* ring buffer length in us */
Packit Service a9274b
static unsigned int       period_time = 0;	            /* period time in us */
Packit Service a9274b
static unsigned int       nperiods    = 4;                  /* number of periods */
Packit Service a9274b
static double             freq        = 440.0;              /* sinusoidal wave frequency in Hz */
Packit Service a9274b
static int                test_type   = TEST_PINK_NOISE;    /* Test type. 1 = noise, 2 = sine wave */
Packit Service a9274b
static float              generator_scale  = 0.8;           /* Scale to use for sine volume */
Packit Service a9274b
static snd_pcm_uframes_t  buffer_size;
Packit Service a9274b
static snd_pcm_uframes_t  period_size;
Packit Service a9274b
static const char *given_test_wav_file = NULL;
Packit Service a9274b
static char *wav_file_dir = SOUNDSDIR;
Packit Service a9274b
static int debug = 0;
Packit Service a9274b
static int force_frequency = 0;
Packit Service a9274b
static int in_aborting = 0;
Packit Service a9274b
static snd_pcm_t *pcm_handle = NULL;
Packit Service a9274b
Packit Service a9274b
#ifdef CONFIG_SUPPORT_CHMAP
Packit Service a9274b
static snd_pcm_chmap_t *channel_map;
Packit Service a9274b
static int channel_map_set;
Packit Service a9274b
static int *ordered_channels;
Packit Service a9274b
#endif
Packit Service a9274b
Packit Service a9274b
static const char *const channel_name[MAX_CHANNELS] = {
Packit Service a9274b
  /*  0 */ N_("Front Left"),
Packit Service a9274b
  /*  1 */ N_("Front Right"),
Packit Service a9274b
  /*  2 */ N_("Rear Left"),
Packit Service a9274b
  /*  3 */ N_("Rear Right"),
Packit Service a9274b
  /*  4 */ N_("Center"),
Packit Service a9274b
  /*  5 */ N_("LFE"),
Packit Service a9274b
  /*  6 */ N_("Side Left"),
Packit Service a9274b
  /*  7 */ N_("Side Right"),
Packit Service a9274b
  /*  8 */ N_("Channel 9"),
Packit Service a9274b
  /*  9 */ N_("Channel 10"),
Packit Service a9274b
  /* 10 */ N_("Channel 11"),
Packit Service a9274b
  /* 11 */ N_("Channel 12"),
Packit Service a9274b
  /* 12 */ N_("Channel 13"),
Packit Service a9274b
  /* 13 */ N_("Channel 14"),
Packit Service a9274b
  /* 14 */ N_("Channel 15"),
Packit Service a9274b
  /* 15 */ N_("Channel 16")
Packit Service a9274b
};
Packit Service a9274b
Packit Service a9274b
static const int	channels4[] = {
Packit Service a9274b
  0, /* Front Left  */
Packit Service a9274b
  1, /* Front Right */
Packit Service a9274b
  3, /* Rear Right  */
Packit Service a9274b
  2, /* Rear Left   */
Packit Service a9274b
};
Packit Service a9274b
static const int	channels6[] = {
Packit Service a9274b
  0, /* Front Left  */
Packit Service a9274b
  4, /* Center      */
Packit Service a9274b
  1, /* Front Right */
Packit Service a9274b
  3, /* Rear Right  */
Packit Service a9274b
  2, /* Rear Left   */
Packit Service a9274b
  5, /* LFE         */
Packit Service a9274b
};
Packit Service a9274b
static const int	channels8[] = {
Packit Service a9274b
  0, /* Front Left  */
Packit Service a9274b
  4, /* Center      */
Packit Service a9274b
  1, /* Front Right */
Packit Service a9274b
  7, /* Side Right  */
Packit Service a9274b
  3, /* Rear Right  */
Packit Service a9274b
  2, /* Rear Left   */
Packit Service a9274b
  6, /* Side Left   */
Packit Service a9274b
  5, /* LFE         */
Packit Service a9274b
};
Packit Service a9274b
Packit Service a9274b
#ifdef CONFIG_SUPPORT_CHMAP
Packit Service a9274b
/* circular clockwise and bottom-to-top order */
Packit Service a9274b
static const int channel_order[] = {
Packit Service a9274b
  [SND_CHMAP_FLW]  =  10,
Packit Service a9274b
  [SND_CHMAP_FL]   =  20,
Packit Service a9274b
  [SND_CHMAP_TFL]  =  30,
Packit Service a9274b
  [SND_CHMAP_FLC]  =  40,
Packit Service a9274b
  [SND_CHMAP_TFLC] =  50,
Packit Service a9274b
  [SND_CHMAP_FC]   =  60,
Packit Service a9274b
  [SND_CHMAP_TFC]  =  70,
Packit Service a9274b
  [SND_CHMAP_FRC]  =  80,
Packit Service a9274b
  [SND_CHMAP_TFRC] =  90,
Packit Service a9274b
  [SND_CHMAP_FR]   = 100,
Packit Service a9274b
  [SND_CHMAP_TFR]  = 110,
Packit Service a9274b
  [SND_CHMAP_FRW]  = 120,
Packit Service a9274b
  [SND_CHMAP_SR]   = 130,
Packit Service a9274b
  [SND_CHMAP_TSR]  = 140,
Packit Service a9274b
  [SND_CHMAP_RR]   = 150,
Packit Service a9274b
  [SND_CHMAP_TRR]  = 160,
Packit Service a9274b
  [SND_CHMAP_RRC]  = 170,
Packit Service a9274b
  [SND_CHMAP_RC]   = 180,
Packit Service a9274b
  [SND_CHMAP_TRC]  = 190,
Packit Service a9274b
  [SND_CHMAP_RLC]  = 200,
Packit Service a9274b
  [SND_CHMAP_RL]   = 210,
Packit Service a9274b
  [SND_CHMAP_TRL]  = 220,
Packit Service a9274b
  [SND_CHMAP_SL]   = 230,
Packit Service a9274b
  [SND_CHMAP_TSL]  = 240,
Packit Service a9274b
  [SND_CHMAP_BC]   = 250,
Packit Service a9274b
  [SND_CHMAP_TC]   = 260,
Packit Service a9274b
  [SND_CHMAP_LLFE] = 270,
Packit Service a9274b
  [SND_CHMAP_LFE]  = 280,
Packit Service a9274b
  [SND_CHMAP_RLFE] = 290,
Packit Service a9274b
  /* not in table  = 10000 */
Packit Service a9274b
  [SND_CHMAP_UNKNOWN] = 20000,
Packit Service a9274b
  [SND_CHMAP_NA]      = 30000,
Packit Service a9274b
};
Packit Service a9274b
Packit Service a9274b
static int chpos_cmp(const void *chnum1p, const void *chnum2p)
Packit Service a9274b
{
Packit Service a9274b
  int chnum1 = *(int *)chnum1p;
Packit Service a9274b
  int chnum2 = *(int *)chnum2p;
Packit Service a9274b
  int chpos1 = channel_map->pos[chnum1];
Packit Service a9274b
  int chpos2 = channel_map->pos[chnum2];
Packit Service a9274b
  int weight1 = 10000;
Packit Service a9274b
  int weight2 = 10000;
Packit Service a9274b
Packit Service a9274b
  if (chpos1 < ARRAY_SIZE(channel_order) && channel_order[chpos1])
Packit Service a9274b
    weight1 = channel_order[chpos1];
Packit Service a9274b
  if (chpos2 < ARRAY_SIZE(channel_order) && channel_order[chpos2])
Packit Service a9274b
    weight2 = channel_order[chpos2];
Packit Service a9274b
Packit Service a9274b
  if (weight1 == weight2) {
Packit Service a9274b
    /* order by channel number if both have the same position (e.g. UNKNOWN)
Packit Service a9274b
     * or if neither is in channel_order[] */
Packit Service a9274b
    return chnum1 - chnum2;
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  /* order according to channel_order[] */
Packit Service a9274b
  return weight1 - weight2;
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static int *order_channels(void)
Packit Service a9274b
{
Packit Service a9274b
  /* create a (playback order => channel number) table with channels ordered
Packit Service a9274b
   * according to channel_order[] values */
Packit Service a9274b
  int i;
Packit Service a9274b
  int *ordered_chs;
Packit Service a9274b
Packit Service a9274b
  ordered_chs = calloc(channel_map->channels, sizeof(*ordered_chs));
Packit Service a9274b
  if (!ordered_chs)
Packit Service a9274b
    return NULL;
Packit Service a9274b
Packit Service a9274b
  for (i = 0; i < channel_map->channels; i++)
Packit Service a9274b
    ordered_chs[i] = i;
Packit Service a9274b
Packit Service a9274b
  qsort(ordered_chs, channel_map->channels, sizeof(*ordered_chs), chpos_cmp);
Packit Service a9274b
Packit Service a9274b
  return ordered_chs;
Packit Service a9274b
}
Packit Service a9274b
#endif
Packit Service a9274b
Packit Service a9274b
static int get_speaker_channel(int chn)
Packit Service a9274b
{
Packit Service a9274b
#ifdef CONFIG_SUPPORT_CHMAP
Packit Service a9274b
  if (channel_map_set || (ordered_channels && chn >= channel_map->channels))
Packit Service a9274b
    return chn;
Packit Service a9274b
  if (ordered_channels)
Packit Service a9274b
    return ordered_channels[chn];
Packit Service a9274b
#endif
Packit Service a9274b
Packit Service a9274b
  switch (channels) {
Packit Service a9274b
  case 4:
Packit Service a9274b
    chn = channels4[chn];
Packit Service a9274b
    break;
Packit Service a9274b
  case 6:
Packit Service a9274b
    chn = channels6[chn];
Packit Service a9274b
    break;
Packit Service a9274b
  case 8:
Packit Service a9274b
    chn = channels8[chn];
Packit Service a9274b
    break;
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  return chn;
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static const char *get_channel_name(int chn)
Packit Service a9274b
{
Packit Service a9274b
#ifdef CONFIG_SUPPORT_CHMAP
Packit Service a9274b
  if (channel_map) {
Packit Service a9274b
    const char *name = NULL;
Packit Service a9274b
    if (chn < channel_map->channels)
Packit Service a9274b
      name = snd_pcm_chmap_long_name(channel_map->pos[chn]);
Packit Service a9274b
    return name ? name : "Unknown";
Packit Service a9274b
  }
Packit Service a9274b
#endif
Packit Service a9274b
  return gettext(channel_name[chn]);
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static const int	supported_formats[] = {
Packit Service a9274b
  SND_PCM_FORMAT_S8,
Packit Service a9274b
  SND_PCM_FORMAT_S16_LE,
Packit Service a9274b
  SND_PCM_FORMAT_S16_BE,
Packit Service a9274b
  SND_PCM_FORMAT_FLOAT_LE,
Packit Service a9274b
  SND_PCM_FORMAT_S24_3LE,
Packit Service a9274b
  SND_PCM_FORMAT_S24_3BE,
Packit Service a9274b
  SND_PCM_FORMAT_S32_LE,
Packit Service a9274b
  SND_PCM_FORMAT_S32_BE,
Packit Service a9274b
  -1
Packit Service a9274b
};
Packit Service a9274b
Packit Service a9274b
typedef union {
Packit Service a9274b
  float f;
Packit Service a9274b
  int32_t i;
Packit Service a9274b
} value_t;
Packit Service a9274b
Packit Service a9274b
static void do_generate(uint8_t *frames, int channel, int count,
Packit Service a9274b
			value_t (*generate)(void *), void *arg)
Packit Service a9274b
{
Packit Service a9274b
  value_t res;
Packit Service a9274b
  int    chn;
Packit Service a9274b
  int8_t *samp8 = (int8_t*) frames;
Packit Service a9274b
  int16_t *samp16 = (int16_t*) frames;
Packit Service a9274b
  int32_t *samp32 = (int32_t*) frames;
Packit Service a9274b
  float   *samp_f = (float*) frames;
Packit Service a9274b
Packit Service a9274b
  while (count-- > 0) {
Packit Service a9274b
    for(chn=0;chn
Packit Service a9274b
      if (chn==channel) {
Packit Service a9274b
	res = generate(arg);
Packit Service a9274b
      } else {
Packit Service a9274b
	res.i = 0;
Packit Service a9274b
      }
Packit Service a9274b
Packit Service a9274b
      switch (format) {
Packit Service a9274b
      case SND_PCM_FORMAT_S8:
Packit Service a9274b
	*samp8++ = res.i >> 24;
Packit Service a9274b
        break;
Packit Service a9274b
      case SND_PCM_FORMAT_S16_LE:
Packit Service a9274b
	*samp16++ = LE_SHORT(res.i >> 16);
Packit Service a9274b
        break;
Packit Service a9274b
      case SND_PCM_FORMAT_S16_BE:
Packit Service a9274b
	*samp16++ = BE_SHORT(res.i >> 16);
Packit Service a9274b
        break;
Packit Service a9274b
      case SND_PCM_FORMAT_FLOAT_LE:
Packit Service a9274b
	*samp_f++ = res.f;
Packit Service a9274b
        break;
Packit Service a9274b
      case SND_PCM_FORMAT_S24_3LE:
Packit Service a9274b
        res.i >>= 8;
Packit Service a9274b
        *samp8++ = LE_INT(res.i);
Packit Service a9274b
        *samp8++ = LE_INT(res.i) >> 8;
Packit Service a9274b
        *samp8++ = LE_INT(res.i) >> 16;
Packit Service a9274b
        break;
Packit Service a9274b
      case SND_PCM_FORMAT_S24_3BE:
Packit Service a9274b
        res.i >>= 8;
Packit Service a9274b
        *samp8++ = BE_INT(res.i);
Packit Service a9274b
        *samp8++ = BE_INT(res.i) >> 8;
Packit Service a9274b
        *samp8++ = BE_INT(res.i) >> 16;
Packit Service a9274b
        break;
Packit Service a9274b
      case SND_PCM_FORMAT_S32_LE:
Packit Service a9274b
	*samp32++ = LE_INT(res.i);
Packit Service a9274b
        break;
Packit Service a9274b
      case SND_PCM_FORMAT_S32_BE:
Packit Service a9274b
	*samp32++ = BE_INT(res.i);
Packit Service a9274b
        break;
Packit Service a9274b
      default:
Packit Service a9274b
        ;
Packit Service a9274b
      }
Packit Service a9274b
    }
Packit Service a9274b
  }
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
/*
Packit Service a9274b
 * Sine generator
Packit Service a9274b
 */
Packit Service a9274b
typedef struct {
Packit Service a9274b
  double phase;
Packit Service a9274b
  double max_phase;
Packit Service a9274b
  double step;
Packit Service a9274b
} sine_t;
Packit Service a9274b
Packit Service a9274b
static void init_sine(sine_t *sine)
Packit Service a9274b
{
Packit Service a9274b
  sine->phase = 0;
Packit Service a9274b
  sine->max_phase = 1.0 / freq;
Packit Service a9274b
  sine->step = 1.0 / (double)rate;
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static value_t generate_sine(void *arg)
Packit Service a9274b
{
Packit Service a9274b
  sine_t *sine = arg;
Packit Service a9274b
  value_t res;
Packit Service a9274b
Packit Service a9274b
  res.f = sin((sine->phase * 2 * M_PI) / sine->max_phase - M_PI);
Packit Service a9274b
  res.f *= generator_scale;
Packit Service a9274b
  if (format != SND_PCM_FORMAT_FLOAT_LE)
Packit Service a9274b
    res.i = res.f * INT32_MAX;
Packit Service a9274b
  sine->phase += sine->step;
Packit Service a9274b
  if (sine->phase >= sine->max_phase)
Packit Service a9274b
    sine->phase -= sine->max_phase;
Packit Service a9274b
  return res;
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
/* Pink noise is a better test than sine wave because we can tell
Packit Service a9274b
 * where pink noise is coming from more easily that a sine wave.
Packit Service a9274b
 */
Packit Service a9274b
static value_t generate_pink_noise(void *arg)
Packit Service a9274b
{
Packit Service a9274b
  pink_noise_t *pink = arg;
Packit Service a9274b
  value_t res;
Packit Service a9274b
Packit Service a9274b
  res.f = generate_pink_noise_sample(pink) * generator_scale;
Packit Service a9274b
  if (format != SND_PCM_FORMAT_FLOAT_LE)
Packit Service a9274b
    res.i = res.f * INT32_MAX;
Packit Service a9274b
  return res;
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
/*
Packit Service a9274b
 * useful for tests
Packit Service a9274b
 */
Packit Service a9274b
static value_t generate_pattern(void *arg)
Packit Service a9274b
{
Packit Service a9274b
  value_t res;
Packit Service a9274b
Packit Service a9274b
  res.i = *(int *)arg;
Packit Service a9274b
  *(int *)arg = res.i + 1;
Packit Service a9274b
  if (format != SND_PCM_FORMAT_FLOAT_LE)
Packit Service a9274b
    res.f = (float)res.i / (float)INT32_MAX;
Packit Service a9274b
  return res;
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static int set_hwparams(snd_pcm_t *handle, snd_pcm_hw_params_t *params, snd_pcm_access_t access) {
Packit Service a9274b
  unsigned int rrate;
Packit Service a9274b
  int          err;
Packit Service a9274b
  snd_pcm_uframes_t     period_size_min;
Packit Service a9274b
  snd_pcm_uframes_t     period_size_max;
Packit Service a9274b
  snd_pcm_uframes_t     buffer_size_min;
Packit Service a9274b
  snd_pcm_uframes_t     buffer_size_max;
Packit Service a9274b
Packit Service a9274b
  /* choose all parameters */
Packit Service a9274b
  err = snd_pcm_hw_params_any(handle, params);
Packit Service a9274b
  if (err < 0) {
Packit Service a9274b
    fprintf(stderr, _("Broken configuration for playback: no configurations available: %s\n"), snd_strerror(err));
Packit Service a9274b
    return err;
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  /* set the interleaved read/write format */
Packit Service a9274b
  err = snd_pcm_hw_params_set_access(handle, params, access);
Packit Service a9274b
  if (err < 0) {
Packit Service a9274b
    fprintf(stderr, _("Access type not available for playback: %s\n"), snd_strerror(err));
Packit Service a9274b
    return err;
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  /* set the sample format */
Packit Service a9274b
  err = snd_pcm_hw_params_set_format(handle, params, format);
Packit Service a9274b
  if (err < 0) {
Packit Service a9274b
    fprintf(stderr, _("Sample format not available for playback: %s\n"), snd_strerror(err));
Packit Service a9274b
    return err;
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  /* set the count of channels */
Packit Service a9274b
  err = snd_pcm_hw_params_set_channels(handle, params, channels);
Packit Service a9274b
  if (err < 0) {
Packit Service a9274b
    fprintf(stderr, _("Channels count (%i) not available for playbacks: %s\n"), channels, snd_strerror(err));
Packit Service a9274b
    return err;
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  /* set the stream rate */
Packit Service a9274b
  rrate = rate;
Packit Service a9274b
  err = snd_pcm_hw_params_set_rate(handle, params, rate, 0);
Packit Service a9274b
  if (err < 0) {
Packit Service a9274b
    fprintf(stderr, _("Rate %iHz not available for playback: %s\n"), rate, snd_strerror(err));
Packit Service a9274b
    return err;
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  if (rrate != rate) {
Packit Service a9274b
    fprintf(stderr, _("Rate doesn't match (requested %iHz, get %iHz, err %d)\n"), rate, rrate, err);
Packit Service a9274b
    return -EINVAL;
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  printf(_("Rate set to %iHz (requested %iHz)\n"), rrate, rate);
Packit Service a9274b
  /* set the buffer time */
Packit Service a9274b
  err = snd_pcm_hw_params_get_buffer_size_min(params, &buffer_size_min);
Packit Service a9274b
  err = snd_pcm_hw_params_get_buffer_size_max(params, &buffer_size_max);
Packit Service a9274b
  err = snd_pcm_hw_params_get_period_size_min(params, &period_size_min, NULL);
Packit Service a9274b
  err = snd_pcm_hw_params_get_period_size_max(params, &period_size_max, NULL);
Packit Service a9274b
  printf(_("Buffer size range from %lu to %lu\n"),buffer_size_min, buffer_size_max);
Packit Service a9274b
  printf(_("Period size range from %lu to %lu\n"),period_size_min, period_size_max);
Packit Service a9274b
  if (period_time > 0) {
Packit Service a9274b
    printf(_("Requested period time %u us\n"), period_time);
Packit Service a9274b
    err = snd_pcm_hw_params_set_period_time_near(handle, params, &period_time, NULL);
Packit Service a9274b
    if (err < 0) {
Packit Service a9274b
      fprintf(stderr, _("Unable to set period time %u us for playback: %s\n"),
Packit Service a9274b
	     period_time, snd_strerror(err));
Packit Service a9274b
      return err;
Packit Service a9274b
    }
Packit Service a9274b
  }
Packit Service a9274b
  if (buffer_time > 0) {
Packit Service a9274b
    printf(_("Requested buffer time %u us\n"), buffer_time);
Packit Service a9274b
    err = snd_pcm_hw_params_set_buffer_time_near(handle, params, &buffer_time, NULL);
Packit Service a9274b
    if (err < 0) {
Packit Service a9274b
      fprintf(stderr, _("Unable to set buffer time %u us for playback: %s\n"),
Packit Service a9274b
	     buffer_time, snd_strerror(err));
Packit Service a9274b
      return err;
Packit Service a9274b
    }
Packit Service a9274b
  }
Packit Service a9274b
  if (! buffer_time && ! period_time) {
Packit Service a9274b
    buffer_size = buffer_size_max;
Packit Service a9274b
    if (! period_time)
Packit Service a9274b
      buffer_size = (buffer_size / nperiods) * nperiods;
Packit Service a9274b
    printf(_("Using max buffer size %lu\n"), buffer_size);
Packit Service a9274b
    err = snd_pcm_hw_params_set_buffer_size_near(handle, params, &buffer_size);
Packit Service a9274b
    if (err < 0) {
Packit Service a9274b
      fprintf(stderr, _("Unable to set buffer size %lu for playback: %s\n"),
Packit Service a9274b
	     buffer_size, snd_strerror(err));
Packit Service a9274b
      return err;
Packit Service a9274b
    }
Packit Service a9274b
  }
Packit Service a9274b
  if (! buffer_time || ! period_time) {
Packit Service a9274b
    printf(_("Periods = %u\n"), nperiods);
Packit Service a9274b
    err = snd_pcm_hw_params_set_periods_near(handle, params, &nperiods, NULL);
Packit Service a9274b
    if (err < 0) {
Packit Service a9274b
      fprintf(stderr, _("Unable to set nperiods %u for playback: %s\n"),
Packit Service a9274b
	     nperiods, snd_strerror(err));
Packit Service a9274b
      return err;
Packit Service a9274b
    }
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  /* write the parameters to device */
Packit Service a9274b
  err = snd_pcm_hw_params(handle, params);
Packit Service a9274b
  if (err < 0) {
Packit Service a9274b
    fprintf(stderr, _("Unable to set hw params for playback: %s\n"), snd_strerror(err));
Packit Service a9274b
    return err;
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  snd_pcm_hw_params_get_buffer_size(params, &buffer_size);
Packit Service a9274b
  snd_pcm_hw_params_get_period_size(params, &period_size, NULL);
Packit Service a9274b
  printf(_("was set period_size = %lu\n"),period_size);
Packit Service a9274b
  printf(_("was set buffer_size = %lu\n"),buffer_size);
Packit Service a9274b
  if (2*period_size > buffer_size) {
Packit Service a9274b
    fprintf(stderr, _("buffer to small, could not use\n"));
Packit Service a9274b
    return -EINVAL;
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  return 0;
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static int set_swparams(snd_pcm_t *handle, snd_pcm_sw_params_t *swparams) {
Packit Service a9274b
  int err;
Packit Service a9274b
Packit Service a9274b
  /* get the current swparams */
Packit Service a9274b
  err = snd_pcm_sw_params_current(handle, swparams);
Packit Service a9274b
  if (err < 0) {
Packit Service a9274b
    fprintf(stderr, _("Unable to determine current swparams for playback: %s\n"), snd_strerror(err));
Packit Service a9274b
    return err;
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  /* start the transfer when a buffer is full */
Packit Service a9274b
  err = snd_pcm_sw_params_set_start_threshold(handle, swparams, buffer_size);
Packit Service a9274b
  if (err < 0) {
Packit Service a9274b
    fprintf(stderr, _("Unable to set start threshold mode for playback: %s\n"), snd_strerror(err));
Packit Service a9274b
    return err;
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  /* allow the transfer when at least period_size frames can be processed */
Packit Service a9274b
  err = snd_pcm_sw_params_set_avail_min(handle, swparams, period_size);
Packit Service a9274b
  if (err < 0) {
Packit Service a9274b
    fprintf(stderr, _("Unable to set avail min for playback: %s\n"), snd_strerror(err));
Packit Service a9274b
    return err;
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  /* write the parameters to the playback device */
Packit Service a9274b
  err = snd_pcm_sw_params(handle, swparams);
Packit Service a9274b
  if (err < 0) {
Packit Service a9274b
    fprintf(stderr, _("Unable to set sw params for playback: %s\n"), snd_strerror(err));
Packit Service a9274b
    return err;
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  return 0;
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
#ifdef CONFIG_SUPPORT_CHMAP
Packit Service a9274b
static int config_chmap(snd_pcm_t *handle, const char *mapstr)
Packit Service a9274b
{
Packit Service a9274b
  int err;
Packit Service a9274b
Packit Service a9274b
  if (mapstr) {
Packit Service a9274b
    channel_map = snd_pcm_chmap_parse_string(mapstr);
Packit Service a9274b
    if (!channel_map) {
Packit Service a9274b
      fprintf(stderr, _("Unable to parse channel map string: %s\n"), mapstr);
Packit Service a9274b
      return -EINVAL;
Packit Service a9274b
    }
Packit Service a9274b
    err = snd_pcm_set_chmap(handle, channel_map);
Packit Service a9274b
    if (err < 0) {
Packit Service a9274b
      fprintf(stderr, _("Unable to set channel map: %s\n"), mapstr);
Packit Service a9274b
      return err;
Packit Service a9274b
    }
Packit Service a9274b
    channel_map_set = 1;
Packit Service a9274b
    return 0;
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  channel_map = snd_pcm_get_chmap(handle);
Packit Service a9274b
Packit Service a9274b
  /* create a channel order table for default layouts */
Packit Service a9274b
  if (channel_map)
Packit Service a9274b
    ordered_channels = order_channels();
Packit Service a9274b
Packit Service a9274b
  return 0;
Packit Service a9274b
}
Packit Service a9274b
#endif
Packit Service a9274b
Packit Service a9274b
/*
Packit Service a9274b
 *   Underrun and suspend recovery
Packit Service a9274b
 */
Packit Service a9274b
Packit Service a9274b
static int xrun_recovery(snd_pcm_t *handle, int err) {
Packit Service a9274b
  if (err == -EPIPE) {	/* under-run */
Packit Service a9274b
    err = snd_pcm_prepare(handle);
Packit Service a9274b
    if (err < 0)
Packit Service a9274b
      fprintf(stderr, _("Can't recovery from underrun, prepare failed: %s\n"), snd_strerror(err));
Packit Service a9274b
    return 0;
Packit Service a9274b
  } 
Packit Service a9274b
  else if (err == -ESTRPIPE) {
Packit Service a9274b
Packit Service a9274b
    while ((err = snd_pcm_resume(handle)) == -EAGAIN)
Packit Service a9274b
      sleep(1);	/* wait until the suspend flag is released */
Packit Service a9274b
Packit Service a9274b
    if (err < 0) {
Packit Service a9274b
      err = snd_pcm_prepare(handle);
Packit Service a9274b
      if (err < 0)
Packit Service a9274b
        fprintf(stderr, _("Can't recovery from suspend, prepare failed: %s\n"), snd_strerror(err));
Packit Service a9274b
    }
Packit Service a9274b
Packit Service a9274b
    return 0;
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  return err;
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
/*
Packit Service a9274b
 * Handle WAV files
Packit Service a9274b
 */
Packit Service a9274b
Packit Service a9274b
static const char *wav_file[MAX_CHANNELS];
Packit Service a9274b
static int wav_file_size[MAX_CHANNELS];
Packit Service a9274b
Packit Service a9274b
struct wave_header {
Packit Service a9274b
  struct {
Packit Service a9274b
    uint32_t magic;
Packit Service a9274b
    uint32_t length;
Packit Service a9274b
    uint32_t type;
Packit Service a9274b
  } hdr;
Packit Service a9274b
  struct {
Packit Service a9274b
    uint32_t type;
Packit Service a9274b
    uint32_t length;
Packit Service a9274b
  } chunk1;
Packit Service a9274b
  struct {
Packit Service a9274b
    uint16_t format;
Packit Service a9274b
    uint16_t channels;
Packit Service a9274b
    uint32_t rate;
Packit Service a9274b
    uint32_t bytes_per_sec;
Packit Service a9274b
    uint16_t sample_size;
Packit Service a9274b
    uint16_t sample_bits;
Packit Service a9274b
  } body;
Packit Service a9274b
  struct {
Packit Service a9274b
    uint32_t type;
Packit Service a9274b
    uint32_t length;
Packit Service a9274b
  } chunk;
Packit Service a9274b
};
Packit Service a9274b
Packit Service a9274b
#define WAV_RIFF		COMPOSE_ID('R','I','F','F')
Packit Service a9274b
#define WAV_WAVE		COMPOSE_ID('W','A','V','E')
Packit Service a9274b
#define WAV_FMT			COMPOSE_ID('f','m','t',' ')
Packit Service a9274b
#define WAV_DATA		COMPOSE_ID('d','a','t','a')
Packit Service a9274b
#define WAV_PCM_CODE		1
Packit Service a9274b
Packit Service a9274b
static const char *search_for_file(const char *name)
Packit Service a9274b
{
Packit Service a9274b
  char *file;
Packit Service a9274b
  if (*name == '/')
Packit Service a9274b
    return strdup(name);
Packit Service a9274b
  file = malloc(strlen(wav_file_dir) + strlen(name) + 2);
Packit Service a9274b
  if (file)
Packit Service a9274b
    sprintf(file, "%s/%s", wav_file_dir, name);
Packit Service a9274b
  return file;
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static int check_wav_file(int channel, const char *name)
Packit Service a9274b
{
Packit Service a9274b
  struct wave_header header;
Packit Service a9274b
  int fd;
Packit Service a9274b
Packit Service a9274b
  wav_file[channel] = search_for_file(name);
Packit Service a9274b
  if (! wav_file[channel]) {
Packit Service a9274b
    fprintf(stderr, _("No enough memory\n"));
Packit Service a9274b
    return -ENOMEM;
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  if ((fd = open(wav_file[channel], O_RDONLY)) < 0) {
Packit Service a9274b
    fprintf(stderr, _("Cannot open WAV file %s\n"), wav_file[channel]);
Packit Service a9274b
    return -EINVAL;
Packit Service a9274b
  }
Packit Service a9274b
  if (read(fd, &header, sizeof(header)) < (int)sizeof(header)) {
Packit Service a9274b
    fprintf(stderr, _("Invalid WAV file %s\n"), wav_file[channel]);
Packit Service a9274b
    goto error;
Packit Service a9274b
  }
Packit Service a9274b
  
Packit Service a9274b
  if (header.hdr.magic != WAV_RIFF || header.hdr.type != WAV_WAVE) {
Packit Service a9274b
    fprintf(stderr, _("Not a WAV file: %s\n"), wav_file[channel]);
Packit Service a9274b
    goto error;
Packit Service a9274b
  }
Packit Service a9274b
  if (header.body.format != LE_SHORT(WAV_PCM_CODE)) {
Packit Service a9274b
    fprintf(stderr, _("Unsupported WAV format %d for %s\n"),
Packit Service a9274b
	    LE_SHORT(header.body.format), wav_file[channel]);
Packit Service a9274b
    goto error;
Packit Service a9274b
  }
Packit Service a9274b
  if (header.body.channels != LE_SHORT(1)) {
Packit Service a9274b
    fprintf(stderr, _("%s is not a mono stream (%d channels)\n"),
Packit Service a9274b
	    wav_file[channel], LE_SHORT(header.body.channels)); 
Packit Service a9274b
    goto error;
Packit Service a9274b
  }
Packit Service a9274b
  if (header.body.rate != LE_INT(rate)) {
Packit Service a9274b
    fprintf(stderr, _("Sample rate doesn't match (%d) for %s\n"),
Packit Service a9274b
	    LE_INT(header.body.rate), wav_file[channel]);
Packit Service a9274b
    goto error;
Packit Service a9274b
  }
Packit Service a9274b
  if (header.body.sample_bits != LE_SHORT(16)) {
Packit Service a9274b
    fprintf(stderr, _("Unsupported sample format bits %d for %s\n"),
Packit Service a9274b
	    LE_SHORT(header.body.sample_bits), wav_file[channel]);
Packit Service a9274b
    goto error;
Packit Service a9274b
  }
Packit Service a9274b
  if (header.chunk.type != WAV_DATA) {
Packit Service a9274b
    fprintf(stderr, _("Invalid WAV file %s\n"), wav_file[channel]);
Packit Service a9274b
    goto error;
Packit Service a9274b
  }
Packit Service a9274b
  wav_file_size[channel] = LE_INT(header.chunk.length);
Packit Service a9274b
  close(fd);
Packit Service a9274b
  return 0;
Packit Service a9274b
Packit Service a9274b
 error:
Packit Service a9274b
  close(fd);
Packit Service a9274b
  return -EINVAL;
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static int setup_wav_file(int chn)
Packit Service a9274b
{
Packit Service a9274b
  static const char *const wavs[MAX_CHANNELS] = {
Packit Service a9274b
    "Front_Left.wav",
Packit Service a9274b
    "Front_Right.wav",
Packit Service a9274b
    "Rear_Left.wav",
Packit Service a9274b
    "Rear_Right.wav",
Packit Service a9274b
    "Front_Center.wav",
Packit Service a9274b
    "Rear_Center.wav", /* FIXME: should be "Bass" or so */
Packit Service a9274b
    "Side_Left.wav",
Packit Service a9274b
    "Side_Right.wav",
Packit Service a9274b
    "Channel_9.wav",
Packit Service a9274b
    "Channel_10.wav",
Packit Service a9274b
    "Channel_11.wav",
Packit Service a9274b
    "Channel_12.wav",
Packit Service a9274b
    "Channel_13.wav",
Packit Service a9274b
    "Channel_14.wav",
Packit Service a9274b
    "Channel_15.wav",
Packit Service a9274b
    "Channel_16.wav"
Packit Service a9274b
  };
Packit Service a9274b
Packit Service a9274b
  if (given_test_wav_file)
Packit Service a9274b
    return check_wav_file(chn, given_test_wav_file);
Packit Service a9274b
Packit Service a9274b
#ifdef CONFIG_SUPPORT_CHMAP
Packit Service a9274b
  if (channel_map && chn < channel_map->channels) {
Packit Service a9274b
    int channel = channel_map->pos[chn] - SND_CHMAP_FL;
Packit Service a9274b
    if (channel >= 0 && channel < MAX_CHANNELS)
Packit Service a9274b
      return check_wav_file(chn, wavs[channel]);
Packit Service a9274b
  }
Packit Service a9274b
#endif
Packit Service a9274b
Packit Service a9274b
  return check_wav_file(chn, wavs[chn]);
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static int read_wav(uint16_t *buf, int channel, int offset, int bufsize)
Packit Service a9274b
{
Packit Service a9274b
  static FILE *wavfp = NULL;
Packit Service a9274b
  int size;
Packit Service a9274b
Packit Service a9274b
  if (in_aborting)
Packit Service a9274b
    return -EFAULT;
Packit Service a9274b
Packit Service a9274b
  if (! wav_file[channel]) {
Packit Service a9274b
    fprintf(stderr, _("Undefined channel %d\n"), channel);
Packit Service a9274b
    return -EINVAL;
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  if (offset >= wav_file_size[channel])
Packit Service a9274b
   return 0; /* finished */
Packit Service a9274b
Packit Service a9274b
  if (! offset) {
Packit Service a9274b
    if (wavfp)
Packit Service a9274b
      fclose(wavfp);
Packit Service a9274b
    wavfp = fopen(wav_file[channel], "r");
Packit Service a9274b
    if (! wavfp)
Packit Service a9274b
      return -errno;
Packit Service a9274b
    if (fseek(wavfp, sizeof(struct wave_header), SEEK_SET) < 0)
Packit Service a9274b
      return -errno;
Packit Service a9274b
  }
Packit Service a9274b
  if (offset + bufsize > wav_file_size[channel])
Packit Service a9274b
    bufsize = wav_file_size[channel] - offset;
Packit Service a9274b
  bufsize /= channels;
Packit Service a9274b
  for (size = 0; size < bufsize; size += 2) {
Packit Service a9274b
    int chn;
Packit Service a9274b
    for (chn = 0; chn < channels; chn++) {
Packit Service a9274b
      if (chn == channel) {
Packit Service a9274b
	if (fread(buf, 2, 1, wavfp) != 1)
Packit Service a9274b
	  return size;
Packit Service a9274b
      }
Packit Service a9274b
      else
Packit Service a9274b
	*buf = 0;
Packit Service a9274b
      buf++;
Packit Service a9274b
    }
Packit Service a9274b
  }
Packit Service a9274b
  return size;
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
Packit Service a9274b
/*
Packit Service a9274b
 *   Transfer method - write only
Packit Service a9274b
 */
Packit Service a9274b
Packit Service a9274b
static int write_buffer(snd_pcm_t *handle, uint8_t *ptr, int cptr)
Packit Service a9274b
{
Packit Service a9274b
  int err;
Packit Service a9274b
Packit Service a9274b
  while (cptr > 0 && !in_aborting) {
Packit Service a9274b
Packit Service a9274b
    err = snd_pcm_writei(handle, ptr, cptr);
Packit Service a9274b
Packit Service a9274b
    if (err == -EAGAIN)
Packit Service a9274b
      continue;
Packit Service a9274b
Packit Service a9274b
    if (err < 0) {
Packit Service a9274b
      fprintf(stderr, _("Write error: %d,%s\n"), err, snd_strerror(err));
Packit Service a9274b
      if ((err = xrun_recovery(handle, err)) < 0) {
Packit Service a9274b
	fprintf(stderr, _("xrun_recovery failed: %d,%s\n"), err, snd_strerror(err));
Packit Service a9274b
	return err;
Packit Service a9274b
      }
Packit Service a9274b
      break;	/* skip one period */
Packit Service a9274b
    }
Packit Service a9274b
Packit Service a9274b
    ptr += snd_pcm_frames_to_bytes(handle, err);
Packit Service a9274b
    cptr -= err;
Packit Service a9274b
  }
Packit Service a9274b
  return 0;
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static int pattern;
Packit Service a9274b
static sine_t sine;
Packit Service a9274b
static pink_noise_t pink;
Packit Service a9274b
Packit Service a9274b
static void init_loop(void)
Packit Service a9274b
{
Packit Service a9274b
  switch (test_type) {
Packit Service a9274b
  case TEST_PINK_NOISE:
Packit Service a9274b
    initialize_pink_noise(&pink, 16);
Packit Service a9274b
    break;
Packit Service a9274b
  case TEST_SINE:
Packit Service a9274b
    init_sine(&sine);
Packit Service a9274b
    break;
Packit Service a9274b
  case TEST_PATTERN:
Packit Service a9274b
    pattern = 0;
Packit Service a9274b
    break;
Packit Service a9274b
  }
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static int write_loop(snd_pcm_t *handle, int channel, int periods, uint8_t *frames)
Packit Service a9274b
{
Packit Service a9274b
  int    err, n;
Packit Service a9274b
Packit Service a9274b
  fflush(stdout);
Packit Service a9274b
  if (test_type == TEST_WAV) {
Packit Service a9274b
    int bufsize = snd_pcm_frames_to_bytes(handle, period_size);
Packit Service a9274b
    n = 0;
Packit Service a9274b
    while ((err = read_wav((uint16_t *)frames, channel, n, bufsize)) > 0 && !in_aborting) {
Packit Service a9274b
      n += err;
Packit Service a9274b
      if ((err = write_buffer(handle, frames,
Packit Service a9274b
			      snd_pcm_bytes_to_frames(handle, err * channels))) < 0)
Packit Service a9274b
	break;
Packit Service a9274b
    }
Packit Service a9274b
    if (buffer_size > n && !in_aborting) {
Packit Service a9274b
      snd_pcm_drain(handle);
Packit Service a9274b
      snd_pcm_prepare(handle);
Packit Service a9274b
    }
Packit Service a9274b
    return err;
Packit Service a9274b
  }
Packit Service a9274b
    
Packit Service a9274b
Packit Service a9274b
  if (periods <= 0)
Packit Service a9274b
    periods = 1;
Packit Service a9274b
Packit Service a9274b
  for(n = 0; n < periods && !in_aborting; n++) {
Packit Service a9274b
    if (test_type == TEST_PINK_NOISE)
Packit Service a9274b
      do_generate(frames, channel, period_size, generate_pink_noise, &pink);
Packit Service a9274b
    else if (test_type == TEST_PATTERN)
Packit Service a9274b
      do_generate(frames, channel, period_size, generate_pattern, &pattern);
Packit Service a9274b
    else
Packit Service a9274b
      do_generate(frames, channel, period_size, generate_sine, &sine);
Packit Service a9274b
Packit Service a9274b
    if ((err = write_buffer(handle, frames, period_size)) < 0)
Packit Service a9274b
      return err;
Packit Service a9274b
  }
Packit Service a9274b
  if (buffer_size > n * period_size && !in_aborting) {
Packit Service a9274b
    snd_pcm_drain(handle);
Packit Service a9274b
    snd_pcm_prepare(handle);
Packit Service a9274b
  }
Packit Service a9274b
  return 0;
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static int prg_exit(int code)
Packit Service a9274b
{
Packit Service a9274b
  if (pcm_handle)
Packit Service a9274b
    snd_pcm_close(pcm_handle);
Packit Service a9274b
  exit(code);
Packit Service a9274b
  return code;
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static void signal_handler(int sig)
Packit Service a9274b
{
Packit Service a9274b
  if (in_aborting)
Packit Service a9274b
    return;
Packit Service a9274b
Packit Service a9274b
  in_aborting = 1;
Packit Service a9274b
Packit Service a9274b
  if (pcm_handle)
Packit Service a9274b
    snd_pcm_abort(pcm_handle);
Packit Service a9274b
  if (sig == SIGABRT) {
Packit Service a9274b
    pcm_handle = NULL;
Packit Service a9274b
    prg_exit(EXIT_FAILURE);
Packit Service a9274b
  }
Packit Service a9274b
  signal(sig, signal_handler);
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static void help(void)
Packit Service a9274b
{
Packit Service a9274b
  const int *fmt;
Packit Service a9274b
Packit Service a9274b
  printf(
Packit Service a9274b
	 _("Usage: speaker-test [OPTION]... \n"
Packit Service a9274b
	   "-h,--help	help\n"
Packit Service a9274b
	   "-D,--device	playback device\n"
Packit Service a9274b
	   "-r,--rate	stream rate in Hz\n"
Packit Service a9274b
	   "-c,--channels	count of channels in stream\n"
Packit Service a9274b
	   "-f,--frequency	sine wave frequency in Hz\n"
Packit Service a9274b
	   "-F,--format	sample format\n"
Packit Service a9274b
	   "-b,--buffer	ring buffer size in us\n"
Packit Service a9274b
	   "-p,--period	period size in us\n"
Packit Service a9274b
	   "-P,--nperiods	number of periods\n"
Packit Service a9274b
	   "-t,--test	pink=use pink noise, sine=use sine wave, wav=WAV file\n"
Packit Service a9274b
	   "-l,--nloops	specify number of loops to test, 0 = infinite\n"
Packit Service a9274b
	   "-s,--speaker	single speaker test. Values 1=Left, 2=right, etc\n"
Packit Service a9274b
	   "-w,--wavfile	Use the given WAV file as a test sound\n"
Packit Service a9274b
	   "-W,--wavdir	Specify the directory containing WAV files\n"
Packit Service a9274b
	   "-m,--chmap	Specify the channel map to override\n"
Packit Service a9274b
	   "-X,--force-frequency	force frequencies outside the 30-8000hz range\n"
Packit Service a9274b
	   "-S,--scale	Scale of generated test tones in percent (default=80)\n"
Packit Service a9274b
	   "\n"));
Packit Service a9274b
  printf(_("Recognized sample formats are:"));
Packit Service a9274b
  for (fmt = supported_formats; *fmt >= 0; fmt++) {
Packit Service a9274b
    const char *s = snd_pcm_format_name(*fmt);
Packit Service a9274b
    if (s)
Packit Service a9274b
      printf(" %s", s);
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  printf("\n\n");
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
int main(int argc, char *argv[]) {
Packit Service a9274b
  snd_pcm_t            *handle;
Packit Service a9274b
  int                   err, morehelp;
Packit Service a9274b
  snd_pcm_hw_params_t  *hwparams;
Packit Service a9274b
  snd_pcm_sw_params_t  *swparams;
Packit Service a9274b
  uint8_t              *frames;
Packit Service a9274b
  int                   chn;
Packit Service a9274b
  const int	       *fmt;
Packit Service a9274b
  double		time1,time2,time3;
Packit Service a9274b
  unsigned int		n, nloops;
Packit Service a9274b
  struct   timeval	tv1,tv2;
Packit Service a9274b
  int			speakeroptset = 0;
Packit Service a9274b
#ifdef CONFIG_SUPPORT_CHMAP
Packit Service a9274b
  const char *chmap = NULL;
Packit Service a9274b
#endif
Packit Service a9274b
Packit Service a9274b
  static const struct option long_option[] = {
Packit Service a9274b
    {"help",      0, NULL, 'h'},
Packit Service a9274b
    {"device",    1, NULL, 'D'},
Packit Service a9274b
    {"rate",      1, NULL, 'r'},
Packit Service a9274b
    {"channels",  1, NULL, 'c'},
Packit Service a9274b
    {"frequency", 1, NULL, 'f'},
Packit Service a9274b
    {"format",    1, NULL, 'F'},
Packit Service a9274b
    {"buffer",    1, NULL, 'b'},
Packit Service a9274b
    {"period",    1, NULL, 'p'},
Packit Service a9274b
    {"nperiods",  1, NULL, 'P'},
Packit Service a9274b
    {"test",      1, NULL, 't'},
Packit Service a9274b
    {"nloops",    1, NULL, 'l'},
Packit Service a9274b
    {"speaker",   1, NULL, 's'},
Packit Service a9274b
    {"wavfile",   1, NULL, 'w'},
Packit Service a9274b
    {"wavdir",    1, NULL, 'W'},
Packit Service a9274b
    {"debug",	  0, NULL, 'd'},
Packit Service a9274b
    {"force-frequency",	  0, NULL, 'X'},
Packit Service a9274b
    {"scale",	  1, NULL, 'S'},
Packit Service a9274b
#ifdef CONFIG_SUPPORT_CHMAP
Packit Service a9274b
    {"chmap",	  1, NULL, 'm'},
Packit Service a9274b
#endif
Packit Service a9274b
    {NULL,        0, NULL, 0  },
Packit Service a9274b
  };
Packit Service a9274b
Packit Service a9274b
#ifdef ENABLE_NLS
Packit Service a9274b
  setlocale(LC_ALL, "");
Packit Service a9274b
  textdomain(PACKAGE);
Packit Service a9274b
#endif
Packit Service a9274b
Packit Service a9274b
  snd_pcm_hw_params_alloca(&hwparams);
Packit Service a9274b
  snd_pcm_sw_params_alloca(&swparams);
Packit Service a9274b
 
Packit Service a9274b
  nloops = 0;
Packit Service a9274b
  morehelp = 0;
Packit Service a9274b
Packit Service a9274b
  printf("\nspeaker-test %s\n\n", SND_UTIL_VERSION_STR);
Packit Service a9274b
  while (1) {
Packit Service a9274b
    int c;
Packit Service a9274b
    
Packit Service a9274b
    if ((c = getopt_long(argc, argv, "hD:r:c:f:F:b:p:P:t:l:s:w:W:d:XS:"
Packit Service a9274b
#ifdef CONFIG_SUPPORT_CHMAP
Packit Service a9274b
			 "m:"
Packit Service a9274b
#endif
Packit Service a9274b
			 , long_option, NULL)) < 0)
Packit Service a9274b
      break;
Packit Service a9274b
    
Packit Service a9274b
    switch (c) {
Packit Service a9274b
    case 'h':
Packit Service a9274b
      morehelp++;
Packit Service a9274b
      break;
Packit Service a9274b
    case 'D':
Packit Service a9274b
      device = strdup(optarg);
Packit Service a9274b
      break;
Packit Service a9274b
    case 'F':
Packit Service a9274b
      format = snd_pcm_format_value(optarg);
Packit Service a9274b
      for (fmt = supported_formats; *fmt >= 0; fmt++)
Packit Service a9274b
        if (*fmt == format)
Packit Service a9274b
          break;
Packit Service a9274b
      if (*fmt < 0) {
Packit Service a9274b
        fprintf(stderr, "Format %s is not supported...\n", snd_pcm_format_name(format));
Packit Service a9274b
        exit(EXIT_FAILURE);
Packit Service a9274b
      }
Packit Service a9274b
      break;
Packit Service a9274b
    case 'r':
Packit Service a9274b
      rate = atoi(optarg);
Packit Service a9274b
      rate = rate < 4000 ? 4000 : rate;
Packit Service a9274b
      rate = rate > 768000 ? 768000 : rate;
Packit Service a9274b
      break;
Packit Service a9274b
    case 'c':
Packit Service a9274b
      channels = atoi(optarg);
Packit Service a9274b
      channels = channels < 1 ? 1 : channels;
Packit Service a9274b
      channels = channels > 1024 ? 1024 : channels;
Packit Service a9274b
      break;
Packit Service a9274b
    case 'f':
Packit Service a9274b
      freq = atof(optarg);
Packit Service a9274b
      break;
Packit Service a9274b
    case 'b':
Packit Service a9274b
      buffer_time = atoi(optarg);
Packit Service a9274b
      buffer_time = buffer_time > 1000000 ? 1000000 : buffer_time;
Packit Service a9274b
      break;
Packit Service a9274b
    case 'p':
Packit Service a9274b
      period_time = atoi(optarg);
Packit Service a9274b
      period_time = period_time > 1000000 ? 1000000 : period_time;
Packit Service a9274b
      break;
Packit Service a9274b
    case 'P':
Packit Service a9274b
      nperiods = atoi(optarg);
Packit Service a9274b
      if (nperiods < 2 || nperiods > 1024) {
Packit Service a9274b
	fprintf(stderr, _("Invalid number of periods %d\n"), nperiods);
Packit Service a9274b
	exit(1);
Packit Service a9274b
      }
Packit Service a9274b
      break;
Packit Service a9274b
    case 't':
Packit Service a9274b
      if (*optarg == 'p')
Packit Service a9274b
	test_type = TEST_PINK_NOISE;
Packit Service a9274b
      else if (*optarg == 's')
Packit Service a9274b
	test_type = TEST_SINE;
Packit Service a9274b
      else if (*optarg == 'w')
Packit Service a9274b
	test_type = TEST_WAV;
Packit Service a9274b
      else if (*optarg == 't')
Packit Service a9274b
	test_type = TEST_PATTERN;
Packit Service a9274b
      else if (isdigit(*optarg)) {
Packit Service a9274b
	test_type = atoi(optarg);
Packit Service a9274b
	if (test_type < TEST_PINK_NOISE || test_type > TEST_PATTERN) {
Packit Service a9274b
	  fprintf(stderr, _("Invalid test type %s\n"), optarg);
Packit Service a9274b
	  exit(1);
Packit Service a9274b
	}
Packit Service a9274b
      } else {
Packit Service a9274b
	fprintf(stderr, _("Invalid test type %s\n"), optarg);
Packit Service a9274b
	exit(1);
Packit Service a9274b
      }
Packit Service a9274b
      break;
Packit Service a9274b
    case 'l':
Packit Service a9274b
      nloops = atoi(optarg);
Packit Service a9274b
      break;
Packit Service a9274b
    case 's':
Packit Service a9274b
      speaker = atoi(optarg);
Packit Service a9274b
      speaker = speaker < 1 ? 0 : speaker;
Packit Service a9274b
      speakeroptset = 1;
Packit Service a9274b
      break;
Packit Service a9274b
    case 'w':
Packit Service a9274b
      given_test_wav_file = optarg;
Packit Service a9274b
      break;
Packit Service a9274b
    case 'W':
Packit Service a9274b
      wav_file_dir = optarg;
Packit Service a9274b
      break;
Packit Service a9274b
    case 'd':
Packit Service a9274b
      debug = 1;
Packit Service a9274b
      break;
Packit Service a9274b
    case 'X':
Packit Service a9274b
      force_frequency = 1;
Packit Service a9274b
      break;
Packit Service a9274b
#ifdef CONFIG_SUPPORT_CHMAP
Packit Service a9274b
    case 'm':
Packit Service a9274b
      chmap = optarg;
Packit Service a9274b
      break;
Packit Service a9274b
#endif
Packit Service a9274b
    case 'S':
Packit Service a9274b
      generator_scale = atoi(optarg) / 100.0;
Packit Service a9274b
      break;
Packit Service a9274b
    default:
Packit Service a9274b
      fprintf(stderr, _("Unknown option '%c'\n"), c);
Packit Service a9274b
      exit(EXIT_FAILURE);
Packit Service a9274b
      break;
Packit Service a9274b
    }
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  if (morehelp) {
Packit Service a9274b
    help();
Packit Service a9274b
    exit(EXIT_SUCCESS);
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  if (speakeroptset) {
Packit Service a9274b
    speaker = speaker > channels ? 0 : speaker;
Packit Service a9274b
    if (speaker==0) {
Packit Service a9274b
      fprintf(stderr, _("Invalid parameter for -s option.\n"));
Packit Service a9274b
      exit(EXIT_FAILURE);
Packit Service a9274b
    }
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  if (!force_frequency) {
Packit Service a9274b
    freq = freq < 30.0 ? 30.0 : freq;
Packit Service a9274b
    freq = freq > 8000.0 ? 8000.0 : freq;
Packit Service a9274b
  } else {
Packit Service a9274b
    freq = freq < 1.0 ? 1.0 : freq;
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  if (test_type == TEST_WAV)
Packit Service a9274b
    format = SND_PCM_FORMAT_S16_LE; /* fixed format */
Packit Service a9274b
Packit Service a9274b
  printf(_("Playback device is %s\n"), device);
Packit Service a9274b
  printf(_("Stream parameters are %iHz, %s, %i channels\n"), rate, snd_pcm_format_name(format), channels);
Packit Service a9274b
  switch (test_type) {
Packit Service a9274b
  case TEST_PINK_NOISE:
Packit Service a9274b
    printf(_("Using 16 octaves of pink noise\n"));
Packit Service a9274b
    break;
Packit Service a9274b
  case TEST_SINE:
Packit Service a9274b
    printf(_("Sine wave rate is %.4fHz\n"), freq);
Packit Service a9274b
    break;
Packit Service a9274b
  case TEST_WAV:
Packit Service a9274b
    printf(_("WAV file(s)\n"));
Packit Service a9274b
    break;
Packit Service a9274b
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  signal(SIGINT, signal_handler);
Packit Service a9274b
  signal(SIGTERM, signal_handler);
Packit Service a9274b
  signal(SIGABRT, signal_handler);
Packit Service a9274b
Packit Service a9274b
  if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
Packit Service a9274b
    printf(_("Playback open error: %d,%s\n"), err,snd_strerror(err));
Packit Service a9274b
    prg_exit(EXIT_FAILURE);
Packit Service a9274b
  }
Packit Service a9274b
  pcm_handle = handle;
Packit Service a9274b
Packit Service a9274b
  if ((err = set_hwparams(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
Packit Service a9274b
    printf(_("Setting of hwparams failed: %s\n"), snd_strerror(err));
Packit Service a9274b
    prg_exit(EXIT_FAILURE);
Packit Service a9274b
  }
Packit Service a9274b
  if ((err = set_swparams(handle, swparams)) < 0) {
Packit Service a9274b
    printf(_("Setting of swparams failed: %s\n"), snd_strerror(err));
Packit Service a9274b
    prg_exit(EXIT_FAILURE);
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
#ifdef CONFIG_SUPPORT_CHMAP
Packit Service a9274b
  err = config_chmap(handle, chmap);
Packit Service a9274b
  if (err < 0)
Packit Service a9274b
    prg_exit(EXIT_FAILURE);
Packit Service a9274b
#endif
Packit Service a9274b
Packit Service a9274b
  if (debug) {
Packit Service a9274b
    snd_output_t *log;
Packit Service a9274b
    err = snd_output_stdio_attach(&log, stderr, 0);
Packit Service a9274b
    if (err >= 0) {
Packit Service a9274b
      snd_pcm_dump(handle, log);
Packit Service a9274b
      snd_output_close(log);
Packit Service a9274b
    }
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  frames = malloc(snd_pcm_frames_to_bytes(handle, period_size));
Packit Service a9274b
  if (frames == NULL) {
Packit Service a9274b
    fprintf(stderr, _("No enough memory\n"));
Packit Service a9274b
    prg_exit(EXIT_FAILURE);
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  init_loop();
Packit Service a9274b
Packit Service a9274b
  if (speaker==0) {
Packit Service a9274b
Packit Service a9274b
    if (test_type == TEST_WAV) {
Packit Service a9274b
      for (chn = 0; chn < channels; chn++) {
Packit Service a9274b
	if (setup_wav_file(get_speaker_channel(chn)) < 0)
Packit Service a9274b
	  prg_exit(EXIT_FAILURE);
Packit Service a9274b
      }
Packit Service a9274b
    }
Packit Service a9274b
Packit Service a9274b
    for (n = 0; (! nloops || n < nloops) && !in_aborting; n++) {
Packit Service a9274b
Packit Service a9274b
      gettimeofday(&tv1, NULL);
Packit Service a9274b
      for(chn = 0; chn < channels; chn++) {
Packit Service a9274b
	int channel = get_speaker_channel(chn);
Packit Service a9274b
        printf(" %d - %s\n", channel, get_channel_name(channel));
Packit Service a9274b
Packit Service a9274b
        err = write_loop(handle, channel, ((rate*3)/period_size), frames);
Packit Service a9274b
Packit Service a9274b
        if (err < 0) {
Packit Service a9274b
          fprintf(stderr, _("Transfer failed: %s\n"), snd_strerror(err));
Packit Service a9274b
          prg_exit(EXIT_SUCCESS);
Packit Service a9274b
        }
Packit Service a9274b
      }
Packit Service a9274b
      gettimeofday(&tv2, NULL);
Packit Service a9274b
      time1 = (double)tv1.tv_sec + ((double)tv1.tv_usec / 1000000.0);
Packit Service a9274b
      time2 = (double)tv2.tv_sec + ((double)tv2.tv_usec / 1000000.0);
Packit Service a9274b
      time3 = time2 - time1;
Packit Service a9274b
      printf(_("Time per period = %lf\n"), time3 );
Packit Service a9274b
    }
Packit Service a9274b
  } else {
Packit Service a9274b
    chn = get_speaker_channel(speaker - 1);
Packit Service a9274b
Packit Service a9274b
    if (test_type == TEST_WAV) {
Packit Service a9274b
      if (setup_wav_file(chn) < 0)
Packit Service a9274b
	prg_exit(EXIT_FAILURE);
Packit Service a9274b
    }
Packit Service a9274b
Packit Service a9274b
    printf("  - %s\n", get_channel_name(chn));
Packit Service a9274b
    err = write_loop(handle, chn, ((rate*5)/period_size), frames);
Packit Service a9274b
Packit Service a9274b
    if (err < 0) {
Packit Service a9274b
      fprintf(stderr, _("Transfer failed: %s\n"), snd_strerror(err));
Packit Service a9274b
    }
Packit Service a9274b
  }
Packit Service a9274b
Packit Service a9274b
  snd_pcm_drain(handle);
Packit Service a9274b
Packit Service a9274b
  free(frames);
Packit Service a9274b
#ifdef CONFIG_SUPPORT_CHMAP
Packit Service a9274b
  free(ordered_channels);
Packit Service a9274b
#endif
Packit Service a9274b
Packit Service a9274b
  return prg_exit(EXIT_SUCCESS);
Packit Service a9274b
}