ftp.nice.ch/pub/next/unix/audio/sndTools.1.00.s.tar.gz#/sndTools/SNDplay.c

This is SNDplay.c in view mode; [Download] [Up]

/*
 * SNDplay 
 *
 * Version 1.0 Written 1994 by Amir Guindehi
 *
 * Plays: Any .snd file and any .snd.gz files
 *
 */


#include <sound/sound.h>
#include <stdio.h>
#include <libc.h>

#define VERSION "SNDplay 1.0 (01.04.94) written by Amir Guindehi.\n"
#define USAGE   "\nUsage: SNDplay [-vh] file1 {file2 ...}\n" \
                  "       file = name[.snd[.gz]]\n\n" 

#define HELP VERSION USAGE \
        "  -v : Switch to verbose mode.\n" \
        "  -h : This help text.\n"

#define DEFAULT_RATE 22050
#define GZIP_PATH "/usr/bin/gunzip -c"

int verbose = 0, errflag = 0;
char rmfile[1024] = "\0";

void sig_handler(int sig)
{
  signal(sig, sig_handler);

  if (verbose) 
    fprintf(stderr, "Got signal %d.\n", sig);

  switch (sig)
  {
    case SIGTERM:
    case SIGQUIT:
    case SIGINT:
      if (rmfile) 
      {
        if (verbose) 
          fprintf(stdout, "Unlinking temporary uncompressed sound.\n");
        unlink(rmfile);
      }
      exit(0);
  }
  return;
}

int playfile(char *name)
{
  SNDSoundStruct *sound;
  int err;

  if (verbose) fprintf(stdout, "Allocating sound structure.\n");
  err = SNDAlloc(&sound, 100000, SND_FORMAT_LINEAR_16, DEFAULT_RATE, 1, 0);
  if (err) 
  { 
    fprintf(stderr,"playfile: Failed to allocate sound structure.\n");
    exit(1);
  }

  if (verbose) fprintf(stdout, "Reading sound file '%s'.\n", name);
  err = SNDReadSoundfile(name, &sound);
  if (err) 
  { 
    fprintf(stderr,"playfile: Failed to load sound.\n");
    exit(1);
  }

  if (rmfile) 
  {
    if (verbose) 
      fprintf(stdout, "Unlinking temporary uncompressed sound.\n");
    unlink(rmfile);
    *rmfile = '\0';
  }

  if (verbose) fprintf(stdout, "Waiting for empty sound queue ...\n");
  err = SNDWait(0);
  if (err)
  {
    fprintf(stderr, "playfile: %s\n", SNDSoundError(err));
    exit(1);
  }

  if (verbose) fprintf(stdout, "Start playing ...\n");
  if (verbose) 
  { 
    fprintf(stdout, "SND-Type: %d samples, %d sampling rate, %d channels.\n",
            SNDSampleCount(sound), sound->samplingRate, sound->channelCount);
    fprintf(stdout, "SND-Encoding: ");
    switch (sound->dataFormat)
    {
      case SND_FORMAT_UNSPECIFIED : fprintf(stdout, "Unspecified\n"); break;
      case SND_FORMAT_MULAW_8     : fprintf(stdout, "Mulaw 8\n"); break;
      case SND_FORMAT_LINEAR_8    : fprintf(stdout, "Linear 8\n"); break;
      case SND_FORMAT_LINEAR_16   : fprintf(stdout, "Linear 16\n"); break;
      case SND_FORMAT_LINEAR_24   : fprintf(stdout, "Linear 24\n"); break;
      case SND_FORMAT_LINEAR_32   : fprintf(stdout, "Linear 32\n"); break;
      case SND_FORMAT_FLOAT       : fprintf(stdout, "Float\n"); break;
      case SND_FORMAT_DOUBLE      : fprintf(stdout, "Double\n"); break;
      case SND_FORMAT_INDIRECT    : fprintf(stdout, "Indirect\n"); break;
      case SND_FORMAT_DISPLAY     : fprintf(stdout, "Display\n"); break;
      case SND_FORMAT_MULAW_SQUELCH: fprintf(stdout, "Mulaw squelch\n"); break;
      case SND_FORMAT_EMPHASIZED  : fprintf(stdout, "Emphasized\n"); break;
      case SND_FORMAT_COMPRESSED  : fprintf(stdout, "Compressed\n"); break;
      case SND_FORMAT_COMPRESSED_EMPHASIZED: fprintf(stdout, "Compressed emphasized\n"); break;
      default : fprintf(stdout, "Unknown format.\n"); break;
    }
  }
  err = SNDStartPlaying(sound, 1, 5, 0, 0, 0);
  if (err)
  {
    fprintf(stderr, "playfile: %s\n", SNDSoundError(err));
    exit(1);
  }

  if (verbose) fprintf(stdout, "Waiting for end of sound ...\n");
  err = SNDWait(1);
  if (err)
  {
    fprintf(stderr, "playfile: %s\n", SNDSoundError(err));
    exit(1);
  }

  if (verbose) fprintf(stdout, "Freeing sound structure.\n");
  SNDFree(sound);
  return 0;
}

int main(int argc, char **argv)
{
  int c, i;
  extern int optind;

  while ((c = getopt(argc, argv, "vh")) != EOF)
    switch (c)
    {
      case 'h':
        fprintf(stdout, HELP);
        exit(0);
      case 'v':
        verbose++;
        break;
      default:
        errflag++;
        break;
    }

  if (errflag || (argc-optind < 1)) 
  {
    fprintf(stderr, VERSION USAGE);
    exit(2);
  }

  if (verbose) fprintf(stdout, VERSION);

  signal(SIGQUIT, sig_handler);
  signal(SIGTERM, sig_handler);
  signal(SIGINT, sig_handler);

  for (i=optind; i<argc; i++) 
  {
    char name[1024];
    char *p;

    *rmfile = '\0';
    strcpy(name, argv[i]);

    if (p = strchr(name,'.'))
    {
      char *s;
 
      if (s = strrchr(p,'.'))
      {
        /* A *.*.* file ... test for .snd.gz */
        if ((!strcmp(s,".gz")) || (!strcmp(s,".z")) || (!strcmp(s,".Z")))
        {
          char buf[1024];
          int pid;
          /* a .gz file -> uncompress it */
          if (verbose) 
            fprintf(stdout, "Gzip compressed ... will uncompress.\n");
          pid = getpid();
          sprintf(buf, GZIP_PATH " %s > /tmp/tmp%d.snd", 
                  name, pid);
          system(buf);
          sprintf(name,"/tmp/tmp%d.snd", pid);
          strcpy(rmfile, name);
        }
      }
      else
      { 
       /* A *.* file ... test for .snd */
      }
    }
    else strcat(name, ".snd");
    playfile(name);
  }
  exit(0);
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.