⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 speaker-test.c

📁 ALSA驱动的一些调试测试工具
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (C) 2000-2004 James Courtier-Dutton * Copyright (C) 2005 Nathan Hurst * * This file is part of the speaker-test tool. * * This small program sends a simple sinusoidal wave to your speakers. * * speaker-test is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * speaker-test is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA * * * Main program by James Courtier-Dutton (including some source code fragments from the alsa project.) * Some cleanup from Daniel Caujolle-Bert <segfault@club-internet.fr> * Pink noise option added Nathan Hurst,  *   based on generator by Phil Burk (pink.c) * * Changelog: *   0.0.8 Added support for pink noise output. * Changelog: *   0.0.7 Added support for more than 6 channels. * Changelog: *   0.0.6 Added support for different sample formats. * * $Id: speaker_test.c,v 1.00 2003/11/26 19:43:38 jcdutton Exp $ */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sched.h>#include <errno.h>#include <getopt.h>#include <inttypes.h>#include <ctype.h>#include <byteswap.h>#define ALSA_PCM_NEW_HW_PARAMS_API#define ALSA_PCM_NEW_SW_PARAMS_API#include <alsa/asoundlib.h>#include <sys/time.h>#include <math.h>#include "pink.h"#include "aconfig.h"#include "gettext.h"#include "version.h"#ifdef ENABLE_NLS#include <locale.h>#endifenum {  TEST_PINK_NOISE = 1,  TEST_SINE,  TEST_WAV};#define MAX_CHANNELS	16static char              *device      = "default";       /* playback device */static snd_pcm_format_t   format      = SND_PCM_FORMAT_S16; /* sample format */static unsigned int       rate        = 48000;	            /* stream rate */static unsigned int       channels    = 1;	            /* count of channels */static unsigned int       speaker     = 0;	            /* count of channels */static unsigned int       buffer_time = 0;	            /* ring buffer length in us */static unsigned int       period_time = 0;	            /* period time in us */static unsigned int       nperiods    = 4;                  /* number of periods */static double             freq        = 440;                /* sinusoidal wave frequency in Hz */static int                test_type   = TEST_PINK_NOISE;    /* Test type. 1 = noise, 2 = sine wave */static pink_noise_t pink;static snd_pcm_uframes_t  buffer_size;static snd_pcm_uframes_t  period_size;static const char *given_test_wav_file = NULL;static char *wav_file_dir = SOUNDSDIR;static const char *const channel_name[MAX_CHANNELS] = {  N_("Front Left"),  N_("Front Right"),  N_("Rear Left"),  N_("Rear Right"),  N_("Center"),  N_("LFE"),  N_("Side Left"),  N_("Side Right"),  N_("Channel 9"),  N_("Channel 10"),  N_("Channel 11"),  N_("Channel 12"),  N_("Channel 13"),  N_("Channel 14"),  N_("Channel 15"),  N_("Channel 16")};static const int	channels4[] = {  0,  1,  3,  2};static const int	channels6[] = {  0,  4,  1,  3,  2,  5}; static const int	channels8[] = {  0,  4,  1,  7,  3,  2,  6,  5}; static void generate_sine(uint8_t *frames, int channel, int count, double *_phase) {  double phase = *_phase;  double max_phase = 1.0 / freq;  double step = 1.0 / (double)rate;  double res;  float fres;  int    chn;  int32_t  ires;  int8_t *samp8 = (int8_t*) frames;  int16_t *samp16 = (int16_t*) frames;  int32_t *samp32 = (int32_t*) frames;  float   *samp_f = (float*) frames;  while (count-- > 0) {    //res = sin((phase * 2 * M_PI) / max_phase - M_PI) * 32767;    //res = sin((phase * 2 * M_PI) / max_phase - M_PI) * 32767;    //res = (sin((phase * 2 * M_PI) / max_phase - M_PI)) * 0x03fffffff; /* Don't use MAX volume */    //if (res > 0) res = 10000;    //if (res < 0) res = -10000;    /* printf("%e\n",res); */    //ires = res;    //ires = ((16 - (count & 0xf)) <<24);    //ires = 0;    for(chn=0;chn<channels;chn++) {      switch (format) {      case SND_PCM_FORMAT_S8:        if (chn==channel) {          res = (sin((phase * 2 * M_PI) / max_phase - M_PI)) * 0x03fffffff; /* Don't use MAX volume */          ires = res;          *samp8++ = ires >> 24;        } else {          *samp8++ = 0;        }        break;      case SND_PCM_FORMAT_S16_LE:        if (chn==channel) {          res = (sin((phase * 2 * M_PI) / max_phase - M_PI)) * 0x03fffffff; /* Don't use MAX volume */          ires = res;#if __BYTE_ORDER == __LITTLE_ENDIAN          *samp16++ = ires >> 16;#elif __BYTE_ORDER == __BIG_ENDIAN          *samp16++ = bswap_16(ires >> 16);#endif        } else {          *samp16++ = 0;        }        break;      case SND_PCM_FORMAT_S16_BE:        if (chn==channel) {          res = (sin((phase * 2 * M_PI) / max_phase - M_PI)) * 0x03fffffff; /* Don't use MAX volume */          ires = res;#if __BYTE_ORDER == __BIG_ENDIAN          *samp16++ = ires >> 16;#elif __BYTE_ORDER == __LITTLE_ENDIAN          *samp16++ = bswap_16(ires >> 16);#endif        } else {          *samp16++ = 0;        }        break;      case SND_PCM_FORMAT_FLOAT_LE:        if (chn==channel) {          res = (sin((phase * 2 * M_PI) / max_phase - M_PI)) * 0.75 ; /* Don't use MAX volume */          fres = res;	  *samp_f++ = fres;	  //*samp32++ = 0xF2345678;	//printf("res=%lf, ires=%d 0x%x, samp32=0x%x\n",res,ires, ires, samp32[-1]);        } else {	  //*samp32++ = ires+0x10000;	  //*samp32++ = ires;	  *samp_f++ = 0.0;        }        break;      case SND_PCM_FORMAT_S32_LE:        if (chn==channel) {          res = (sin((phase * 2 * M_PI) / max_phase - M_PI)) * 0x03fffffff; /* Don't use MAX volume */          ires = res;#if __BYTE_ORDER == __LITTLE_ENDIAN          *samp32++ = ires;#elif __BYTE_ORDER == __BIG_ENDIAN          *samp32++ = bswap_32(ires);#endif        } else {          *samp32++ = 0;        }        break;      case SND_PCM_FORMAT_S32_BE:        if (chn==channel) {          res = (sin((phase * 2 * M_PI) / max_phase - M_PI)) * 0x03fffffff; /* Don't use MAX volume */          ires = res;#if __BYTE_ORDER == __BIG_ENDIAN          *samp32++ = ires;#elif __BYTE_ORDER == __LITTLE_ENDIAN          *samp32++ = bswap_32(ires);#endif        } else {          *samp32++ = 0;        }        break;      default:        ;      }    }    phase += step;    if (phase >= max_phase)      phase -= max_phase;  }  *_phase = phase;}/* Pink noise is a better test than sine wave because we can tell * where pink noise is coming from more easily that a sine wave. */static void generate_pink_noise( uint8_t *frames, int channel, int count) {  double   res;  int      chn;  int32_t  ires;  int8_t  *samp8 = (int8_t*) frames;  int16_t *samp16 = (int16_t*) frames;  int32_t *samp32 = (int32_t*) frames;  while (count-- > 0) {    for(chn=0;chn<channels;chn++) {      switch (format) {      case SND_PCM_FORMAT_S8:        if (chn==channel) {	  res = generate_pink_noise_sample(&pink) * 0x03fffffff; /* Don't use MAX volume */	  ires = res;	  *samp8++ = ires >> 24;        } else {	  *samp8++ = 0;        }        break;      case SND_PCM_FORMAT_S16_LE:        if (chn==channel) {	  res = generate_pink_noise_sample(&pink) * 0x03fffffff; /* Don't use MAX volume */	  ires = res;#if __BYTE_ORDER == __LITTLE_ENDIAN          *samp16++ = ires >> 16;#elif __BYTE_ORDER == __BIG_ENDIAN          *samp16++ = bswap_16(ires >> 16);#endif        } else {	  *samp16++ = 0;        }        break;      case SND_PCM_FORMAT_S16_BE:        if (chn==channel) {          res = generate_pink_noise_sample(&pink) * 0x03fffffff; /* Don't use MAX volume */          ires = res;#if __BYTE_ORDER == __BIG_ENDIAN          *samp16++ = ires >> 16;#elif __BYTE_ORDER == __LITTLE_ENDIAN          *samp16++ = bswap_16(ires >> 16);#endif        } else {          *samp16++ = 0;        }        break;      case SND_PCM_FORMAT_S32_LE:        if (chn==channel) {          res = generate_pink_noise_sample(&pink) * 0x03fffffff; /* Don't use MAX volume */          ires = res;#if __BYTE_ORDER == __LITTLE_ENDIAN          *samp32++ = ires;#elif __BYTE_ORDER == __BIG_ENDIAN          *samp32++ = bswap_32(ires);#endif        } else {          *samp32++ = 0;        }        break;      case SND_PCM_FORMAT_S32_BE:        if (chn==channel) {	  res = generate_pink_noise_sample(&pink) * 0x03fffffff; /* Don't use MAX volume */	  ires = res;#if __BYTE_ORDER == __BIG_ENDIAN	  *samp32++ = ires;#elif __BYTE_ORDER == __LITTLE_ENDIAN          *samp32++ = bswap_32(ires);#endif        } else {	  *samp32++ = 0;        }        break;      default:        ;      }    }  }}static int set_hwparams(snd_pcm_t *handle, snd_pcm_hw_params_t *params, snd_pcm_access_t access) {  unsigned int rrate;  int          err;  snd_pcm_uframes_t     period_size_min;  snd_pcm_uframes_t     period_size_max;  snd_pcm_uframes_t     buffer_size_min;  snd_pcm_uframes_t     buffer_size_max;  /* choose all parameters */  err = snd_pcm_hw_params_any(handle, params);  if (err < 0) {    fprintf(stderr, _("Broken configuration for playback: no configurations available: %s\n"), snd_strerror(err));    return err;  }  /* set the interleaved read/write format */  err = snd_pcm_hw_params_set_access(handle, params, access);  if (err < 0) {    fprintf(stderr, _("Access type not available for playback: %s\n"), snd_strerror(err));    return err;  }  /* set the sample format */  err = snd_pcm_hw_params_set_format(handle, params, format);  if (err < 0) {    fprintf(stderr, _("Sample format not available for playback: %s\n"), snd_strerror(err));    return err;  }  /* set the count of channels */  err = snd_pcm_hw_params_set_channels(handle, params, channels);  if (err < 0) {    fprintf(stderr, _("Channels count (%i) not available for playbacks: %s\n"), channels, snd_strerror(err));    return err;  }  /* set the stream rate */  rrate = rate;  err = snd_pcm_hw_params_set_rate(handle, params, rate, 0);  if (err < 0) {    fprintf(stderr, _("Rate %iHz not available for playback: %s\n"), rate, snd_strerror(err));    return err;  }  if (rrate != rate) {    fprintf(stderr, _("Rate doesn't match (requested %iHz, get %iHz, err %d)\n"), rate, rrate, err);    return -EINVAL;  }  printf(_("Rate set to %iHz (requested %iHz)\n"), rrate, rate);  /* set the buffer time */  err = snd_pcm_hw_params_get_buffer_size_min(params, &buffer_size_min);  err = snd_pcm_hw_params_get_buffer_size_max(params, &buffer_size_max);  err = snd_pcm_hw_params_get_period_size_min(params, &period_size_min, NULL);  err = snd_pcm_hw_params_get_period_size_max(params, &period_size_max, NULL);  printf(_("Buffer size range from %lu to %lu\n"),buffer_size_min, buffer_size_max);  printf(_("Period size range from %lu to %lu\n"),period_size_min, period_size_max);  if (period_time > 0) {    printf(_("Requested period time %u us\n"), period_time);    err = snd_pcm_hw_params_set_period_time_near(handle, params, &period_time, NULL);    if (err < 0) {      fprintf(stderr, _("Unable to set period time %u us for playback: %s\n"),	     period_time, snd_strerror(err));      return err;    }  }  if (buffer_time > 0) {    printf(_("Requested buffer time %u us\n"), buffer_time);    err = snd_pcm_hw_params_set_buffer_time_near(handle, params, &buffer_time, NULL);    if (err < 0) {      fprintf(stderr, _("Unable to set buffer time %u us for playback: %s\n"),	     buffer_time, snd_strerror(err));      return err;    }  }  if (! buffer_time && ! period_time) {    buffer_size = buffer_size_max;    if (! period_time)      buffer_size = (buffer_size / nperiods) * nperiods;    printf(_("Using max buffer size %lu\n"), buffer_size);    err = snd_pcm_hw_params_set_buffer_size_near(handle, params, &buffer_size);    if (err < 0) {      fprintf(stderr, _("Unable to set buffer size %lu for playback: %s\n"),	     buffer_size, snd_strerror(err));      return err;    }  }  if (! buffer_time || ! period_time) {    printf(_("Periods = %u\n"), nperiods);    err = snd_pcm_hw_params_set_periods_near(handle, params, &nperiods, NULL);    if (err < 0) {      fprintf(stderr, _("Unable to set nperiods %u for playback: %s\n"),	     nperiods, snd_strerror(err));      return err;    }  }  /* write the parameters to device */  err = snd_pcm_hw_params(handle, params);  if (err < 0) {    fprintf(stderr, _("Unable to set hw params for playback: %s\n"), snd_strerror(err));    return err;  }  snd_pcm_hw_params_get_buffer_size(params, &buffer_size);  snd_pcm_hw_params_get_period_size(params, &period_size, NULL);  printf(_("was set period_size = %lu\n"),period_size);  printf(_("was set buffer_size = %lu\n"),buffer_size);  if (2*period_size > buffer_size) {    fprintf(stderr, _("buffer to small, could not use\n"));    return -EINVAL;  }  return 0;}static int set_swparams(snd_pcm_t *handle, snd_pcm_sw_params_t *swparams) {  int err;  /* get the current swparams */  err = snd_pcm_sw_params_current(handle, swparams);  if (err < 0) {    fprintf(stderr, _("Unable to determine current swparams for playback: %s\n"), snd_strerror(err));    return err;  }  /* start the transfer when a buffer is full */  err = snd_pcm_sw_params_set_start_threshold(handle, swparams, buffer_size);  if (err < 0) {    fprintf(stderr, _("Unable to set start threshold mode for playback: %s\n"), snd_strerror(err));    return err;  }  /* allow the transfer when at least period_size frames can be processed */  err = snd_pcm_sw_params_set_avail_min(handle, swparams, period_size);  if (err < 0) {    fprintf(stderr, _("Unable to set avail min for playback: %s\n"), snd_strerror(err));    return err;  }  /* write the parameters to the playback device */  err = snd_pcm_sw_params(handle, swparams);  if (err < 0) {    fprintf(stderr, _("Unable to set sw params for playback: %s\n"), snd_strerror(err));    return err;  }  return 0;}/* *   Underrun and suspend recovery */static int xrun_recovery(snd_pcm_t *handle, int err) {  if (err == -EPIPE) {	/* under-run */    err = snd_pcm_prepare(handle);    if (err < 0)      fprintf(stderr, _("Can't recovery from underrun, prepare failed: %s\n"), snd_strerror(err));    return 0;  }   else if (err == -ESTRPIPE) {    while ((err = snd_pcm_resume(handle)) == -EAGAIN)      sleep(1);	/* wait until the suspend flag is released */    if (err < 0) {      err = snd_pcm_prepare(handle);      if (err < 0)        fprintf(stderr, _("Can't recovery from suspend, prepare failed: %s\n"), snd_strerror(err));    }    return 0;  }  return err;}/* * Handle WAV files */static const char *wav_file[MAX_CHANNELS];static int wav_file_size[MAX_CHANNELS];struct wave_header {  struct {    uint32_t magic;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -