File: dat.c

package info (click to toggle)
sox 12.16-6
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,180 kB
  • ctags: 1,466
  • sloc: ansic: 16,658; sh: 2,071; makefile: 126
file content (133 lines) | stat: -rw-r--r-- 2,893 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

/*
 * July 5, 1991
 * Copyright 1991 Lance Norskog And Sundry Contributors
 * This source code is freely redistributable and may be used for
 * any purpose.  This copyright notice must be maintained. 
 * Lance Norskog And Sundry Contributors are not responsible for 
 * the consequences of using this software.
 */

/*
 * Sound Tools text format file.  Tom Littlejohn, March 93.
 *
 * Reads/writes sound files as text for use with fft and graph.
 *
 * June 28, 93: force output to mono.
 *
 * September 24, 1998: cbagwell - Set up extra output format info so that 
 * reports are accurate.  Also warn user when forcing to mono.
 *
 */

#include "st.h"
#include "libst.h"

/* Private data for dat file */
typedef struct dat {
	double timevalue, deltat;
} *dat_t;

LONG roundoff(x)
double x;
{
    if (x < 0.0) return(x - 0.5);
    else return(x + 0.5);
    }

void
datstartread(ft)
ft_t ft;
{
   char inpstr[82];
   char sc;

   while (ft->info.rate == 0) {
      fgets(inpstr,82,ft->fp);
      sscanf(inpstr," %c",&sc);
      if (sc != ';') fail("Cannot determine sample rate.");
#ifdef __alpha__
      sscanf(inpstr," ; Sample Rate %d", &ft->info.rate);
#else
      sscanf(inpstr," ; Sample Rate %ld",&ft->info.rate);
#endif
      }

   /* size and style are really not necessary except to satisfy caller. */

   ft->info.size = DOUBLE;
   ft->info.style = SIGN2;
}

void
datstartwrite(ft)
ft_t ft;
{
   dat_t dat = (dat_t) ft->priv;
   double srate;

   if (ft->info.channels > 1)
   {
        report("Can only create .dat files with one channel.");
	report("Forcing output to 1 channel.");
	ft->info.channels = 1;
   }
   
   ft->info.size = DOUBLE;
   ft->info.style = SIGN2;
   dat->timevalue = 0.0;
   srate = ft->info.rate;
   dat->deltat = 1.0 / srate;
#ifdef __alpha__
   fprintf(ft->fp,"; Sample Rate %d\015\n", ft->info.rate);
#else
   fprintf(ft->fp,"; Sample Rate %ld\015\n",ft->info.rate);
#endif
}

LONG datread(ft, buf, nsamp)
ft_t ft;
LONG *buf, nsamp;
{
    char inpstr[82];
    double sampval;
    int retc;
    int done = 0;
    char sc;

    while (done < nsamp) {
        do {
          fgets(inpstr,82,ft->fp);
          if (feof(ft->fp)) {
		return (done);
	  }
          sscanf(inpstr," %c",&sc);
          }
          while(sc == ';');  /* eliminate comments */
        retc = sscanf(inpstr,"%*s %lg",&sampval);
        if (retc != 1) fail("Unable to read sample.");
        *buf++ = roundoff(sampval * 2.147483648e9);
        ++done;
    }
	return (done);
}

void
datwrite(ft, buf, nsamp)
ft_t ft;
LONG *buf, nsamp;
{
    dat_t dat = (dat_t) ft->priv;
    int done = 0;
    double sampval;

    while(done < nsamp) {
       sampval = *buf++ ;
       sampval = sampval / 2.147483648e9;  /* normalize to approx 1.0 */
       fprintf(ft->fp," %15.8g  %15.8g \015\n",dat->timevalue,sampval);
       dat->timevalue += dat->deltat;
       done++;
       }
    return;
}