summaryrefslogtreecommitdiffstats
path: root/arm-wt-22k/host_src/eas_wave.c
blob: 02fed6e89beef6b3ba0574fe7130e74aee1027a2 (plain)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*----------------------------------------------------------------------------
 *
 * File: 
 * eas_wave.c
 *
 * Contents and purpose:
 * This module contains .WAV file functions for the EAS synthesizer
 * test harness.  
 *			
 * Copyright Sonic Network Inc. 2005

 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *----------------------------------------------------------------------------
 * Revision Control:
 *   $Revision: 658 $
 *   $Date: 2007-04-24 13:35:49 -0700 (Tue, 24 Apr 2007) $
 *----------------------------------------------------------------------------
*/

/* lint complaints about most C library headers, so we use our own during lint step */
#ifdef _lint
#include "lint_stdlib.h"
#else
#include <stdio.h>
#include <stdlib.h>
#endif

#include "eas_wave.h"

/* .WAV file format tags */
const EAS_U32 riffTag = 0x46464952;
const EAS_U32 waveTag = 0x45564157;
const EAS_U32 fmtTag = 0x20746d66;
const EAS_U32 dataTag = 0x61746164;

#ifdef _BIG_ENDIAN
/*----------------------------------------------------------------------------
 * FlipDWord()
 *----------------------------------------------------------------------------
 * Purpose: Endian flip a DWORD for big-endian processors
 * 
 * Inputs: 
 *			
 * Outputs:
 *
 *----------------------------------------------------------------------------
*/
static void FlipDWord (EAS_U32 *pValue)
{
	EAS_U8 *p;
	EAS_U32 temp;

	p = (EAS_U8*) pValue;
	temp = (((((p[3] << 8) | p[2]) << 8) | p[1]) << 8) | p[0];
	*pValue = temp;
}

/*----------------------------------------------------------------------------
 * FlipWord()
 *----------------------------------------------------------------------------
 * Purpose: Endian flip a WORD for big-endian processors
 * 
 * Inputs: 
 *			
 * Outputs:
 *
 *----------------------------------------------------------------------------
*/
static void FlipWord (EAS_U16 *pValue)
{
	EAS_U8 *p;
	EAS_U16 temp;

	p = (EAS_U8*) pValue;
	temp = (p[1] << 8) | p[0];
	*pValue = temp;
}

/*----------------------------------------------------------------------------
 * FlipWaveHeader()
 *----------------------------------------------------------------------------
 * Purpose: Endian flip the wave header for big-endian processors
 * 
 * Inputs: 
 *			
 * Outputs:
 *
 *----------------------------------------------------------------------------
*/
static void FlipWaveHeader (WAVE_HEADER *p)
{

	FlipDWord(&p->nRiffTag);
	FlipDWord(&p->nRiffSize);
	FlipDWord(&p->nWaveTag);
	FlipDWord(&p->nFmtTag);
	FlipDWord(&p->nFmtSize);
	FlipDWord(&p->nDataTag);
	FlipDWord(&p->nDataSize);
	FlipWord(&p->fc.wFormatTag);
	FlipWord(&p->fc.nChannels);
	FlipDWord(&p->fc.nSamplesPerSec);
	FlipDWord(&p->fc.nAvgBytesPerSec);
	FlipWord(&p->fc.nBlockAlign);
	FlipWord(&p->fc.wBitsPerSample);
	
}
#endif

/*----------------------------------------------------------------------------
 * WaveFileCreate()
 *----------------------------------------------------------------------------
 * Purpose: Opens a wave file for writing and writes the header
 * 
 * Inputs: 
 *			
 * Outputs:
 *
 *----------------------------------------------------------------------------
*/

WAVE_FILE *WaveFileCreate (const char *filename, EAS_I32 nChannels, EAS_I32 nSamplesPerSec, EAS_I32 wBitsPerSample)
{
	WAVE_FILE *wFile;

	/* allocate memory */
	wFile = malloc(sizeof(WAVE_FILE));
	if (!wFile)
		return NULL;
	wFile->write = EAS_TRUE;

	/* create the file */
	wFile->file = fopen(filename,"wb");
	if (!wFile->file)
	{
		free(wFile);
		return NULL;
	}

	/* initialize PCM format .WAV file header */
	wFile->wh.nRiffTag = riffTag;
	wFile->wh.nRiffSize = sizeof(WAVE_HEADER) - 8;
	wFile->wh.nWaveTag = waveTag;
	wFile->wh.nFmtTag = fmtTag;
	wFile->wh.nFmtSize = sizeof(FMT_CHUNK);

	/* initalize 'fmt' chunk */
	wFile->wh.fc.wFormatTag = 1;
	wFile->wh.fc.nChannels = (EAS_U16) nChannels;
	wFile->wh.fc.nSamplesPerSec = (EAS_U32) nSamplesPerSec;
	wFile->wh.fc.wBitsPerSample = (EAS_U16) wBitsPerSample;
	wFile->wh.fc.nBlockAlign = (EAS_U16) (nChannels * (EAS_U16) (wBitsPerSample / 8));
	wFile->wh.fc.nAvgBytesPerSec = wFile->wh.fc.nBlockAlign * (EAS_U32) nSamplesPerSec;

	/* initialize 'data' chunk */
	wFile->wh.nDataTag = dataTag;
	wFile->wh.nDataSize = 0;

#ifdef _BIG_ENDIAN
	FlipWaveHeader(&wFile->wh);
#endif

	/* write the header */
	if (fwrite(&wFile->wh, sizeof(WAVE_HEADER), 1, wFile->file) != 1)
	{
		fclose(wFile->file);
		free(wFile);
		return NULL;
	}

#ifdef _BIG_ENDIAN
	FlipWaveHeader(&wFile->wh);
#endif

	/* return the file handle */
	return wFile;
} /* end WaveFileCreate */

/*----------------------------------------------------------------------------
 * WaveFileWrite()
 *----------------------------------------------------------------------------
 * Purpose: Writes data to the wave file
 * 
 * Inputs: 
 *			
 * Outputs:
 *
 *----------------------------------------------------------------------------
*/
EAS_I32 WaveFileWrite (WAVE_FILE *wFile, void *buffer, EAS_I32 n)
{
	EAS_I32 count;

	/* make sure we have an open file */
	if (wFile == NULL)
	{
		return 0;
	}

#ifdef _BIG_ENDIAN
	{
		EAS_I32 i;
		EAS_U16 *p;
		p = buffer;
		i = n >> 1;
		while (i--)
			FlipWord(p++);
	}
#endif	
	
	/* write the data */
	count = (EAS_I32) fwrite(buffer, 1, (size_t) n, wFile->file);

	/* add the number of bytes written */
	wFile->wh.nRiffSize += (EAS_U32) count;
	wFile->wh.nDataSize += (EAS_U32) count;

	/* return the count of bytes written */
	return count;
} /* end WriteWaveHeader */

/*----------------------------------------------------------------------------
 * WaveFileClose()
 *----------------------------------------------------------------------------
 * Purpose: Opens a wave file for writing and writes the header
 * 
 * Inputs: 
 *			
 * Outputs:
 *
 *----------------------------------------------------------------------------
*/

EAS_BOOL WaveFileClose (WAVE_FILE *wFile)
{
	EAS_I32 count = 1;

	/* return to beginning of file and write the header */
	if (wFile->write)
	{
		if (fseek(wFile->file, 0L, SEEK_SET) == 0)
		{
			
#ifdef _BIG_ENDIAN
			FlipWaveHeader(&wFile->wh);
#endif
			count = (EAS_I32) fwrite(&wFile->wh, sizeof(WAVE_HEADER), 1, wFile->file);
#ifdef _BIG_ENDIAN
			FlipWaveHeader(&wFile->wh);
#endif
		}
	}
	
	/* close the file */
	if (fclose(wFile->file) != 0)
		count = 0;

	/* free the memory */
	free(wFile);

	/* return the file handle */
	return (count == 1 ? EAS_TRUE : EAS_FALSE);
} /* end WaveFileClose */

#ifdef _WAVE_FILE_READ
#ifdef _BIG_ENDIAN
#error "WaveFileOpen not currently supported on big-endian processors"
#endif
/*----------------------------------------------------------------------------
 * WaveFileOpen()
 *----------------------------------------------------------------------------
 * Purpose: Opens a wave file for reading and reads the header
 * 
 * Inputs: 
 *			
 * Outputs:
 *
 *----------------------------------------------------------------------------
*/

WAVE_FILE *WaveFileOpen (const char *filename)
{
	WAVE_FILE *wFile;
	struct
	{
		EAS_U32 tag;
		EAS_U32 size;
	} chunk;
	EAS_U32 tag;
	EAS_I32 startChunkPos;
	EAS_INT state;
	EAS_BOOL done;

	/* allocate memory */
	wFile = malloc(sizeof(WAVE_FILE));
	if (!wFile)
		return NULL;

	/* open the file */
	wFile->write = EAS_FALSE;
	wFile->file = fopen(filename,"rb");
	if (!wFile->file)
	{
		free(wFile);
		return NULL;
	}

	/* make lint happy */
	chunk.tag = chunk.size = 0;
	startChunkPos = 0;

	/* read the RIFF tag and file size */
	state = 0;
	done = EAS_FALSE;
	while (!done)
	{

		switch(state)
		{
			/* read the RIFF tag */
			case 0:
				if (fread(&chunk, sizeof(chunk), 1, wFile->file) != 1)
					done = EAS_TRUE;
				else
				{
					if (chunk.tag != riffTag)
						done = EAS_TRUE;
					else
						state++;
				}
				break;

			/* read the WAVE tag */
			case 1:
				if (fread(&tag, sizeof(tag), 1, wFile->file) != 1)
					done = EAS_TRUE;
				else
				{
					if (tag != waveTag)
						done = EAS_TRUE;
					else
						state++;
				}
				break;

			/* looking for fmt chunk */
			case 2:
				if (fread(&chunk, sizeof(chunk), 1, wFile->file) != 1)
					done = EAS_TRUE;
				else
				{
					startChunkPos = ftell(wFile->file);
						
					/* not fmt tag, skip it */
					if (chunk.tag != fmtTag)
						fseek(wFile->file, startChunkPos + (EAS_I32) chunk.size, SEEK_SET);
					else
						state++;
				}
				break;

			/* read fmt chunk */
			case 3:
				if (fread(&wFile->wh.fc, sizeof(FMT_CHUNK), 1, wFile->file) != 1)
					done = EAS_TRUE;
				else
				{
					fseek(wFile->file, startChunkPos + (EAS_I32) chunk.size, SEEK_SET);
					state++;				
				}
				break;
				
			/* looking for data chunk */
			case 4:
				if (fread(&chunk, sizeof(chunk), 1, wFile->file) != 1)
					done = EAS_TRUE;
				else
				{
					startChunkPos = ftell(wFile->file);
						
					/* not data tag, skip it */
					if (chunk.tag != dataTag)
						fseek(wFile->file, startChunkPos + (EAS_I32) chunk.size, SEEK_SET);
					else
					{
						wFile->dataSize = chunk.size;
						state++;
						done = EAS_TRUE;
					}
				}
				break;
				
			default:
				done = EAS_TRUE;
				break;
		}
	}

	/* if not final state, an error occurred */
	if (state != 5)
	{
		fclose(wFile->file);
		free(wFile);
		return NULL;
	}

	/* return the file handle */
	return wFile;
} /* end WaveFileOpen */
#endif