File: sndrtool.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 (217 lines) | stat: -rw-r--r-- 5,154 bytes parent folder | download
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * Sounder/Sndtool format handler: W V Neisius, February 1992
 *
 * June 28, 93: force output to mono.
 * 
 * March 3, 1999 - cbagwell@sprynet.com
 *   Forced extra comment fields to zero.
 */

#include <math.h>
#include <string.h>
#include <stdio.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>	/* For SEEK_* defines if not found in stdio */
#endif

#include "st.h"

/* Private data used by writer */
struct sndpriv {
        ULONG nsamples;
};

#ifndef	SEEK_CUR
#define	SEEK_CUR	1
#endif

void  sndtwriteheader(P2(ft_t ft,LONG nsamples));
void rawwrite(P3(ft_t, LONG *, LONG));

/*======================================================================*/
/*                         SNDSTARTREAD                                */
/*======================================================================*/

void sndtstartread(ft)
ft_t ft;
{
char buf[97];

LONG rate;

	int littlendian = 1;
	char *endptr;

	/* Needed for rawread() */
	rawstartread(ft);

	endptr = (char *) &littlendian;
	/* sndt is in little endian format so 
	 * swap bytes on big endian machines.
	 */
	if (!*endptr)
	{
		ft->swap = ft->swap ? 0 : 1;
	}

	rate = 0;

/* determine file type */
        /* if first 5 bytes == SOUND then this is probably a sndtool sound */
        /* if first word (16 bits) == 0 
         and second word is between 4000 & 25000 then this is sounder sound */
        /* otherwise, its probably raw, not handled here */

if (fread(buf, 1, 2, ft->fp) != 2)
	fail("SND: unexpected EOF");
if (strncmp(buf,"\0\0",2) == 0)
	{
	/* sounder */
	rate = rshort(ft);
	if (rate < 4000 || rate > 25000 )
		fail ("SND: sample rate out of range");
	fseek(ft->fp,4,SEEK_CUR);
	}
else
	{
	/* sndtool ? */
	fread(&buf[2],1,6,ft->fp);
	if (strncmp(buf,"SOUND",5))
		fail ("SND: unrecognized SND format");
	fseek(ft->fp,12,SEEK_CUR);
	rate = rshort(ft);
	fseek(ft->fp,6,SEEK_CUR);
	if (fread(buf,1,96,ft->fp) != 96)
		fail ("SND: unexpected EOF in SND header");
	report ("%s",buf);
	}

ft->info.channels = 1;
ft->info.rate = rate;
ft->info.style = UNSIGNED;
ft->info.size = BYTE;

}

/*======================================================================*/
/*                         SNDTSTARTWRITE                               */
/*======================================================================*/
void sndtstartwrite(ft)
ft_t ft;
{
struct sndpriv *p = (struct sndpriv *) ft->priv;

	int littlendian = 1;
	char *endptr;

	/* Needed for rawwrite() */
	rawstartwrite(ft);

	endptr = (char *) &littlendian;
	/* sndt is in little endian format so
	 * swap bytes on big endian machines
	 */
	if (!*endptr)
	{
		ft->swap = ft->swap ? 0 : 1;
	}

/* write header */
ft->info.channels = 1;
ft->info.style = UNSIGNED;
ft->info.size = BYTE;
p->nsamples = 0;
sndtwriteheader(ft, 0);

}
/*======================================================================*/
/*                         SNDRSTARTWRITE                               */
/*======================================================================*/
void sndrstartwrite(ft)
ft_t ft;
{
	int littlendian = 1;
	char *endptr;

	/* Needed for rawread() */
	rawstartread(ft);

	endptr = (char *) &littlendian;
	/* sndr is in little endian format so
	 * swap bytes on big endian machines
	 */
	if (!*endptr)
	{
		ft->swap = ft->swap ? 0 : 1;
	}

/* write header */
ft->info.channels = 1;
ft->info.style = UNSIGNED;
ft->info.size = BYTE;

/* sounder header */
wshort (ft,0); /* sample size code */
wshort (ft,(int) ft->info.rate);     /* sample rate */
wshort (ft,10);        /* volume */
wshort (ft,4); /* shift */
}

/*======================================================================*/
/*                         SNDTWRITE                                     */
/*======================================================================*/

void sndtwrite(ft, buf, len)
ft_t ft;
LONG *buf, len;
{
	struct sndpriv *p = (struct sndpriv *) ft->priv;
	p->nsamples += len;
	rawwrite(ft, buf, len);
}

/*======================================================================*/
/*                         SNDTSTOPWRITE                                */
/*======================================================================*/

void sndtstopwrite(ft)
ft_t ft;
{
	struct sndpriv *p = (struct sndpriv *) ft->priv;

	/* Flush remaining buffer out */
	rawstopwrite(ft);

	/* fixup file sizes in header */
	if (fseek(ft->fp, 0L, 0) != 0)
		fail("can't rewind output file to rewrite SND header");
	sndtwriteheader(ft, p->nsamples);
}

/*======================================================================*/
/*                         SNDTWRITEHEADER                              */
/*======================================================================*/
void sndtwriteheader(ft,nsamples)
ft_t ft;
LONG nsamples;
{
char name_buf[97];

/* sndtool header */
fputs ("SOUND",ft->fp); /* magic */
fputc (0x1a,ft->fp);
wshort (ft,(LONG)0);  /* hGSound */
wlong (ft,nsamples);
wlong (ft,(LONG)0);
wlong (ft,nsamples);
wshort (ft,(int) ft->info.rate);
wshort (ft,0);
wshort (ft,10);
wshort (ft,4);
memset (name_buf, 0, 96);
sprintf (name_buf,"%s - File created by Sound Exchange",ft->filename);
fwrite (name_buf, 1, 96, ft->fp);
}