示例#1
0
static void playback(char *name)
{
	int ofs;
	size_t dta;
	ssize_t dtawave;

	pbrec_count = LLONG_MAX;
	fdcount = 0;
	if (!name || !strcmp(name, "-"))
		return;
	if ((fd = open64(name, O_RDONLY, 0)) == -1) {
		perror(name);
		exit(EXIT_FAILURE);
	}
	// read bytes for WAVE-header
	if ((dtawave = test_wavefile(fd, audiobuf, dta)) >= 0) {
		pbrec_count = calc_count();
		printf("will play %s", name);
		playback_go(fd, dtawave, pbrec_count, name);
	}
	if (fd != 0)
		close(fd);
}
/* playing raw data, this proc handels WAVE files and
   .VOCs (as one block) */
static int
do_play (int fd, int loaded, u_long count, int rtype, char *name)
{
  int l, real_l;
  u_long c;
  char one_chn = 0;
  char to_8 = 0;
  int tmps;

  sync_dsp ();
  tmps = samplesize;
  ioctl (audio, SNDCTL_DSP_SETFMT, &tmps);
  if (tmps != samplesize)
    {
      fprintf (stderr, "Unable to set %d bit sample size", samplesize);
      if (samplesize == 16)
	{
	  samplesize = 8;
	  ioctl (audio, SNDCTL_DSP_SETFMT, &samplesize);
	  if (samplesize != 8)
	    {
	      fprintf (stderr, "Unable to set 8 bit sample size!\n");
	      return -1;
	    }
	  fprintf (stderr, "; playing 8 bit\n");
	  to_8 = 1;
	}
      else
	{
	  fprintf (stderr, "\n");
	  return -1;
	}
    }
#ifdef OSS_VERSION
  if (ioctl (audio, SNDCTL_DSP_STEREO, &dsp_stereo) < 0)
    {
#else
  if (dsp_stereo != ioctl (audio, SNDCTL_DSP_STEREO, dsp_stereo))
    {
#endif
      fprintf (stderr, "Can't play in Stereo; playing only one channel\n");
      dsp_stereo = MODE_MONO;
      one_chn = 1;
    }
  if (set_dsp_speed (&dsp_speed) < 0)
    return -1;

  abuf_size = 512;

  while (count)
    {
      c = count;

      if (c > abuf_size)
	c = abuf_size;

      if ((l = read (fd, (char *) audiobuf + loaded, c - loaded)) > 0)
	{
	  l += loaded;
	  loaded = 0;		/* correct the count; ugly but ... */
	  real_l = (one_chn
		    || to_8) ? one_channel (audiobuf, l, one_chn, to_8) : l;

	  /* change byte order if necessary */
	  if (convert && (samplesize == 16))
	    {
	      long i;

	      for (i = 0; i < real_l; i += 2)
		*((short *) (audiobuf + i)) =
		  htons (*((short *) (audiobuf + i)));
	    }

	  if (write (audio, (char *) audiobuf, real_l) != real_l)
	    {
	      perror (AUDIO);
	      return -1;
	    }
	  count -= l;
	}
      else
	{
	  if (l == -1)
	    {
	      perror (name);
	      return -1;
	    }
	  count = 0;		/* Stop */
	}
    }				/* while (count) */

  return 0;
}

/*
  let's play)
*/
static int
player (char *name)
{
  int fd, ofs;

  if (!name)
    {
      fd = 0;
      name = "stdin";
    }
  else if ((fd = open (name, O_RDONLY, 0)) == -1)
    {
      perror (name);
      return -1;
    }
  /* Read the smallest header first, then the
     missing bytes for the next, etc. */

  /* read SND-header */
  read (fd, (char *) audiobuf, sizeof (SndHeader));
  if (test_sndfile (audiobuf, fd) >= 0)
    {
      if (do_play (fd, 0, count, SND_FMT, name) < 0)
	return -1;
    }

  else
    {
      /* read VOC-Header */
      read (fd, (char *) audiobuf + sizeof (SndHeader),
	    sizeof (VocHeader) - sizeof (SndHeader));
      if ((ofs = test_vocfile (audiobuf)) >= 0)
	{
	  if (vplay (fd, ofs, name) < 0)
	    return -1;
	}

      else
	{
	  /* read bytes for WAVE-header */
	  read (fd, (char *) audiobuf + sizeof (VocHeader),
		sizeof (WaveHeader) - sizeof (VocHeader));
	  if (test_wavefile (audiobuf) >= 0)
	    {
	      if (do_play (fd, 0, count, WAVE_FMT, name) < 0)
		return -1;
	    }
	  else
	    {
	      /* should be raw data */
	      init_raw_data ();
	      count = calc_count ();
	      if (do_play (fd, sizeof (WaveHeader), count, RAW_DATA, name) <
		  0)
		return -1;
	    }
	}
    }
  if (fd != 0)
    close (fd);
  return 0;
}