home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Sound / SoX / Source / dat.c < prev    next >
C/C++ Source or Header  |  1999-07-18  |  3KB  |  134 lines

  1.  
  2. /*
  3.  * July 5, 1991
  4.  * Copyright 1991 Lance Norskog And Sundry Contributors
  5.  * This source code is freely redistributable and may be used for
  6.  * any purpose.  This copyright notice must be maintained. 
  7.  * Lance Norskog And Sundry Contributors are not responsible for 
  8.  * the consequences of using this software.
  9.  */
  10.  
  11. /*
  12.  * Sound Tools text format file.  Tom Littlejohn, March 93.
  13.  *
  14.  * Reads/writes sound files as text for use with fft and graph.
  15.  *
  16.  * June 28, 93: force output to mono.
  17.  *
  18.  * September 24, 1998: cbagwell - Set up extra output format info so that 
  19.  * reports are accurate.  Also warn user when forcing to mono.
  20.  *
  21.  */
  22.  
  23. #include "st.h"
  24. #include "libst.h"
  25.  
  26. /* Private data for dat file */
  27. typedef struct dat {
  28.     double timevalue, deltat;
  29. } *dat_t;
  30.  
  31. LONG roundoff(x)
  32. double x;
  33. {
  34.     if (x < 0.0) return(x - 0.5);
  35.     else return(x + 0.5);
  36.     }
  37.  
  38. void
  39. datstartread(ft)
  40. ft_t ft;
  41. {
  42.    char inpstr[82];
  43.    char sc;
  44.  
  45.    while (ft->info.rate == 0) {
  46.       fgets(inpstr,82,ft->fp);
  47.       sscanf(inpstr," %c",&sc);
  48.       if (sc != ';') fail("Cannot determine sample rate.");
  49. #ifdef __alpha__
  50.       sscanf(inpstr," ; Sample Rate %d", &ft->info.rate);
  51. #else
  52.       sscanf(inpstr," ; Sample Rate %ld",&ft->info.rate);
  53. #endif
  54.       }
  55.  
  56.    /* size and style are really not necessary except to satisfy caller. */
  57.  
  58.    ft->info.size = DOUBLE;
  59.    ft->info.style = SIGN2;
  60. }
  61.  
  62. void
  63. datstartwrite(ft)
  64. ft_t ft;
  65. {
  66.    dat_t dat = (dat_t) ft->priv;
  67.    double srate;
  68.  
  69.    if (ft->info.channels > 1)
  70.    {
  71.         report("Can only create .dat files with one channel.");
  72.     report("Forcing output to 1 channel.");
  73.     ft->info.channels = 1;
  74.    }
  75.    
  76.    ft->info.size = DOUBLE;
  77.    ft->info.style = SIGN2;
  78.    dat->timevalue = 0.0;
  79.    srate = ft->info.rate;
  80.    dat->deltat = 1.0 / srate;
  81. #ifdef __alpha__
  82.    fprintf(ft->fp,"; Sample Rate %d\015\n", ft->info.rate);
  83. #else
  84.    fprintf(ft->fp,"; Sample Rate %ld\015\n",ft->info.rate);
  85. #endif
  86. }
  87.  
  88. LONG datread(ft, buf, nsamp)
  89. ft_t ft;
  90. LONG *buf, nsamp;
  91. {
  92.     char inpstr[82];
  93.     double sampval;
  94.     int retc;
  95.     int done = 0;
  96.     char sc;
  97.  
  98.     while (done < nsamp) {
  99.         do {
  100.           fgets(inpstr,82,ft->fp);
  101.           if (feof(ft->fp)) {
  102.         return (done);
  103.       }
  104.           sscanf(inpstr," %c",&sc);
  105.           }
  106.           while(sc == ';');  /* eliminate comments */
  107.         retc = sscanf(inpstr,"%*s %lg",&sampval);
  108.         if (retc != 1) fail("Unable to read sample.");
  109.         *buf++ = roundoff(sampval * 2.147483648e9);
  110.         ++done;
  111.     }
  112.     return (done);
  113. }
  114.  
  115. void
  116. datwrite(ft, buf, nsamp)
  117. ft_t ft;
  118. LONG *buf, nsamp;
  119. {
  120.     dat_t dat = (dat_t) ft->priv;
  121.     int done = 0;
  122.     double sampval;
  123.  
  124.     while(done < nsamp) {
  125.        sampval = *buf++ ;
  126.        sampval = sampval / 2.147483648e9;  /* normalize to approx 1.0 */
  127.        fprintf(ft->fp," %15.8g  %15.8g \015\n",dat->timevalue,sampval);
  128.        dat->timevalue += dat->deltat;
  129.        done++;
  130.        }
  131.     return;
  132. }
  133.  
  134.