#define pounds(a) { *((int16 *)point)=LE_SHORT(a); point+=2; } #define poundl(a) { *((int32 *)point)=LE_LONG(a); point+=4; } 
#define pounds(a) { int16 x = LE_SHORT(a); memcpy(point, &x, 2); point+=2; } #define poundl(a) { int32 x = LE_LONG(a);  memcpy(point, &x, 4); point+=4; } 
int main(int argc, char **argv){  int32    note=DEFAULT_NOTE,    root=DEFAULT_ROOT,    verbosity=0,    samplerate=-1,    datasize=-1,    junklength, c;  int    infd=STDIN_FILENO;  unsigned char     thing[4096],    *point,     modes=0;  while ((c=getopt(argc, argv, "r:n:v"))>0)    switch(c)      {      case 'r': root=atol(optarg); break;      case 'n': root=freq_table[atoi(optarg) & 127]; break;      case 'u': note=atol(optarg); break;      case 'v': verbosity++; break;      default: return 1;      }  if (optind==argc-1)    {      if ((infd=open(argv[optind], O_RDONLY))<0)	{	  perror("wav2pat: I can't open your WAVE");	  return 3;	}    }  else if (optind!=argc)    {      fprintf(stderr, 	      "Usage: wav2pat [-r RootFreq | -n Note] [-u Note] [-v] [filename]\n");      return 1;    }  /* check out the putative RIFF WAVE on stdin */  if (read(infd, thing, 12)<=0)    {      perror("wav2pat: I can't read your WAVE");      return 3;    }  if (memcmp(thing, "RIFF", 4) || memcmp(thing+8, "WAVE", 4))    {      fprintf(stderr, "wav2pat: I want a RIFF WAVE on stdin!\n");      return 2;    }  while (datasize==-1)    {      if (read(infd, thing, 8)!=8)	{	  perror("wav2pat: "		 "Your WAVE ran out before I got to the interesting bits");	  return 3;	}      junklength=LE_LONG(*((int32 *)(thing+4)));      /* This Microsoft file format is designed to be impossible to	 parse correctly if one doesn't have the full specification.	 If you have a wave with an INFO "chunk", you lose. Thank you	 for playing. */      if (!memcmp(thing, "fmt ", 4))	{	  if (junklength > 4096)	    {	      fprintf(stderr, "wav2pat: "		      "WAVEs with %ld-byte format blocks make me throw up!\n",		      junklength);	      return 2;	    }	  if (read(infd, thing, junklength) != junklength)	    {	      perror("wav2pat: Your WAVE is mangled");	      return 3;	    }	  if (LE_SHORT(*((int16 *)(thing))) != 1)	    {	      fprintf(stderr, "wav2pat: I don't understand your WAVE. "		      "It has a type %d format chunk!\n",		      LE_SHORT(*((int16 *)(thing))));	      return 2;	    }	  if (LE_SHORT(*((int16 *)(thing + 2))) != 1)	    {	      fprintf(stderr, "wav2pat: This WAVE has %d channels! " 		      "There can be only one!\n",		      LE_SHORT(*((int16 *)(thing + 2))));	      return 2;	    }	  samplerate=LE_LONG(*((int32 *)(thing + 4)));	  switch(LE_SHORT(*((int16 *)(thing + 14))))	    {	    case 8: modes |= MODES_UNSIGNED; break;	    case 16: modes |= MODES_16BIT; break;	    default:	      fprintf(stderr, "wav2pat: Ack! Ppthbth! %d-bit samples!",		      LE_SHORT(*((int16 *)(thing + 14))));	      return 2;	    }	  if (verbosity)	    fprintf(stderr, "wav2pat: This is a %d-bit, %ld Hz WAVE\n",		    (LE_SHORT(*((int16 *)(thing + 14)))),		    samplerate);	}      else if (!memcmp(thing, "data", 4))	{	  if (samplerate==-1)	    {	      fprintf(stderr, "wav2pat: Your WAVE has no format information before data!\n");	      return 2;	    }	  if (verbosity)	    fprintf(stderr, "wav2pat: It has %ld bytes of data\n", junklength);	  datasize=junklength;	}      else	{	  if (verbosity)	    fprintf(stderr, "wav2pat: "		    "Your WAVE has a %ld-byte chunk called `%4.4s'\n",		    junklength, thing);	  /* It's cool to pad chunks with NULs to align them on             half-word boundaries. */	  if (junklength & 1)	    junklength++;	  while (junklength>0)	    {	      if ((c=read(infd, thing, (junklength>4096) ? 4096 : junklength)) 		  <= 0)		{		  perror("wav2pat: Now your WAVE has run out of data");		  return 3;		}	      junklength -= c;	    }	}    }  /* hammer together something that looks like a GUS patch header */#define pound(a) *point++=(a);#define pounds(a) { *((int16 *)point)=LE_SHORT(a); point+=2; }#define poundl(a) { *((int32 *)point)=LE_LONG(a); point+=4; }#define bounce(a) point += a;    memset(thing, 0, 335);  point=thing;  /* header */  memcpy(point, "GF1PATCH110", 12);  point += 12;  /* Gravis ID */  memcpy(point, "ID#000002", 10);  point += 10;    /* description */  strcpy(point, "Copyleft 1995 EWE&U Conductions and one Retreated Gravi\032");  point += 60;  pound(1); /* instruments */  pound(14); /* voices */  pound(0); /* channels */  pounds(1); /* waveforms */  pounds(127); /* master volume */  poundl(datasize); /* data size */  bounce(36); /* reserved */  pounds(1); /* instrument # */  strcpy(point, "Bleahnoise"); /* instrument name */  point += 16;  poundl(datasize); /* instrument size */  pound(1); /* layers */  bounce(40); /* reserved */  pound(0); /* layer duplicate */  pound(0); /* layer */  poundl(datasize); /* layer size */  pound(1); /* samples */  bounce(40); /* reserved */    strcpy(point, "bleah"); /* wave name */  point += 7;  pound(0); /* fractions */  poundl(datasize); /* wave size */  poundl(0); /* loop start */  poundl(datasize); /* loop end */  pounds(samplerate); /* sample rate */  poundl(8176); /* low freq */  poundl(12543854); /* high freq */  poundl(root); /* root freq */  pounds(512); /* tune */  pound(7);  /* balance */  pound(63); /* envelope rates */  pound(63);  pound(63);  pound(63);  pound(63);  pound(63);  pound(240); /* envelope offsets */  pound(240);  pound(240);  pound(240);  pound(240);  pound(240);  pound(0); /* tremolo sweep */  pound(0); /* tremolo rate */  pound(0); /* tremolo depth */  pound(0); /* vibrato sweep */  pound(0); /* vibrato rate */  pound(0); /* vibrato depth */  pound(modes); /* modes */  pounds(note); /* scale freq */  pounds(1024); /* scale factor */  bounce(36); /* reserved */  write(STDOUT_FILENO, thing, 335);  /* wave data */  while (datasize>0)    {      if ((c=read(infd, thing, (datasize>4096) ? 4096 : datasize)) 	  <= 0)	{	  perror("wav2pat: I can't read data");	  return 3;	}      write(STDOUT_FILENO, thing, c);      datasize -= c;    }  /* be courteous */  if (infd != STDIN_FILENO)    close(infd);  return 0;}
4357	/local/tlutelli/issta_data/temp/c/2005_temp/2005/4357/cb293c4705e0c7b93d3269c59bcf9bbcab7f6278/wav2pat.c/clean/wav2pat.c