summaryrefslogtreecommitdiffstats
path: root/arm-wt-22k/host_src/eas_main.c
blob: fed600ea604d924edb6d8c1ea38deb41b7768da1 (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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*----------------------------------------------------------------------------
 *
 * File: 
 * eas_main.c
 *
 * Contents and purpose:
 * The entry point and high-level functions for the EAS Synthesizer test
 * harness.
 *			
 * Copyright Sonic Network Inc. 2004

 * 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: 775 $
 *   $Date: 2007-07-20 10:11:11 -0700 (Fri, 20 Jul 2007) $
 *----------------------------------------------------------------------------
*/

#ifdef _lint
#include "lint_stdlib.h"
#else
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#endif

#include "eas.h"
#include "eas_wave.h"
#include "eas_report.h"

/* determines how many EAS buffers to fill a host buffer */
#define NUM_BUFFERS			8

/* default file to play if no filename is specified on the command line */
static const char defaultTestFile[] = "test.mid";

EAS_I32 polyphony;

/* prototypes for helper functions */
static void StrCopy(char *dest, const char *src, EAS_I32 size);
static EAS_BOOL ChangeFileExt(char *str, const char *ext, EAS_I32 size);
static EAS_RESULT PlayFile (EAS_DATA_HANDLE easData, const char* filename, const char* outputFile, const S_EAS_LIB_CONFIG *pLibConfig, void *buffer, EAS_I32 bufferSize);
static EAS_BOOL EASLibraryCheck (const S_EAS_LIB_CONFIG *pLibConfig);

/* main is defined after playfile to avoid the need for two passes through lint */

/*----------------------------------------------------------------------------
 * PlayFile()
 *----------------------------------------------------------------------------
 * Purpose: 
 * This function plays the file requested by filename
 * 
 * Inputs: 
 *			
 * Outputs:
 *
 *----------------------------------------------------------------------------
*/

static EAS_RESULT PlayFile (EAS_DATA_HANDLE easData, const char* filename, const char* outputFile, const S_EAS_LIB_CONFIG *pLibConfig, void *buffer, EAS_I32 bufferSize)
{
	EAS_HANDLE handle;
	EAS_RESULT result, reportResult;
	EAS_I32 count;
	EAS_STATE state;
	EAS_I32 playTime;
	char waveFilename[256];
	WAVE_FILE *wFile;
	EAS_INT i;
	EAS_PCM *p;
	EAS_FILE file;

	/* determine the name of the output file */
	wFile = NULL;
	if (outputFile == NULL)
	{
		StrCopy(waveFilename, filename, sizeof(waveFilename));
		if (!ChangeFileExt(waveFilename, "wav", sizeof(waveFilename)))
		{
			{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Error in output filename %s\n", waveFilename); */ }
			return EAS_FAILURE;
		}
		outputFile = waveFilename;
	}

	/* call EAS library to open file */
	file.path = filename;
	file.fd = 0;
	if ((reportResult = EAS_OpenFile(easData, &file, &handle)) != EAS_SUCCESS)
	{
		{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "EAS_OpenFile returned %ld\n", reportResult); */ }
		return reportResult;
	}

	/* prepare to play the file */
	if ((result = EAS_Prepare(easData, handle)) != EAS_SUCCESS)
	{
		{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "EAS_Prepare returned %ld\n", result); */ }
		reportResult = result;
	}

	/* get play length */
	if ((result = EAS_ParseMetaData(easData, handle, &playTime)) != EAS_SUCCESS)
	{
		{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "EAS_ParseMetaData returned %ld\n", result); */ }
		return result;
	}
	EAS_ReportEx(_EAS_SEVERITY_NOFILTER, 0xe624f4d9, 0x00000005 , playTime / 1000, playTime % 1000);

	if (reportResult == EAS_SUCCESS)
	{
		/* create the output file */
		wFile = WaveFileCreate(outputFile, pLibConfig->numChannels, pLibConfig->sampleRate, sizeof(EAS_PCM) * 8);
		if (!wFile)
		{
			{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Unable to create output file %s\n", waveFilename); */ }
			reportResult = EAS_FAILURE;
		}
	}

	/* rendering loop */
	while (reportResult == EAS_SUCCESS)
	{

		/* we may render several buffers here to fill one host buffer */
		for (i = 0, p = buffer; i < NUM_BUFFERS; i++, p+= pLibConfig->mixBufferSize * pLibConfig->numChannels)
		{
			
			/* get the current time */
			if ((result = EAS_GetLocation(easData, handle, &playTime)) != EAS_SUCCESS)
			{
				{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "EAS_GetLocation returned %d\n",result); */ }
				if (reportResult == EAS_SUCCESS)
					reportResult = result;
				break;
			}
			{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Parser time: %d.%03d\n", playTime / 1000, playTime % 1000); */ }

			/* render a buffer of audio */
			if ((result = EAS_Render(easData, p, pLibConfig->mixBufferSize, &count)) != EAS_SUCCESS)
			{
				{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "EAS_Render returned %d\n",result); */ }
				if (reportResult == EAS_SUCCESS)
					reportResult = result;
			}
		}

		if (result == EAS_SUCCESS)
		{
			/* write it to the wave file */
			if (WaveFileWrite(wFile, buffer, bufferSize) != bufferSize)
			{
				{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "WaveFileWrite failed\n"); */ }
				reportResult = EAS_FAILURE;
			}
		}

		if (reportResult == EAS_SUCCESS)
		{
			/* check stream state */
			if ((result = EAS_State(easData, handle, &state)) != EAS_SUCCESS)
			{
				{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "EAS_State returned %d\n", result); */ }
				reportResult = result;
			}

			/* is playback complete */
			if ((state == EAS_STATE_STOPPED) || (state == EAS_STATE_ERROR))
				break;
		}
	}

	/* close the output file */
	if (wFile)
	{
		if (!WaveFileClose(wFile))
		{
			{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Error closing wave file %s\n", waveFilename); */ }
			if (reportResult == EAS_SUCCESS)
				result = EAS_FAILURE;
		}
	}
	
	/* close the input file */
	if ((result = EAS_CloseFile(easData,handle)) != EAS_SUCCESS)
	{
		{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "EAS_Close returned %ld\n", result); */ }
		if (reportResult == EAS_SUCCESS)
			result = EAS_FAILURE;
	}

	return reportResult;
} /* end PlayFile */

/*----------------------------------------------------------------------------
 * main()
 *----------------------------------------------------------------------------
 * Purpose: The entry point for the EAS sample application
 * 
 * Inputs: 
 *			
 * Outputs:
 *
 *----------------------------------------------------------------------------
*/
int main( int argc, char **argv )
{
	EAS_DATA_HANDLE easData;
	const S_EAS_LIB_CONFIG *pLibConfig;
	void *buffer;
	EAS_RESULT result, playResult;
	EAS_I32 bufferSize;
	int i;
	int temp;
	FILE *debugFile;
	char *outputFile = NULL;

	/* set the error reporting level */
	EAS_SetDebugLevel(_EAS_SEVERITY_INFO);
	debugFile = NULL;

	/* process command-line arguments */
	for (i = 1; i < argc; i++)
	{
		/* check for switch */
		if (argv[i][0] == '-')
		{
			switch (argv[i][1])
			{
			case 'd':
				temp = argv[i][2];
				if ((temp >= '0') || (temp <= '9'))
					EAS_SetDebugLevel(temp);
				else
					{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Invalid debug level %d\n", temp); */ }
				break;
			case 'f':
				if ((debugFile = fopen(&argv[i][2],"w")) == NULL)
					{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Unable to create debug file %s\n", &argv[i][2]); */ }
				else
					EAS_SetDebugFile(debugFile, EAS_TRUE);
				break;
			case 'o':
				outputFile = &argv[i][2];
				break;
			case 'p':
				polyphony = atoi(&argv[i][2]);
				if (polyphony < 1)
					polyphony = 1;
				{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_INFO, "Polyphony set to %d\n", polyphony); */ }
				break;
			default:
				break;
			}
			continue;
		}
	}

	/* assume success */
	playResult = EAS_SUCCESS;

	/* get the library configuration */
	pLibConfig = EAS_Config();
	if (!EASLibraryCheck(pLibConfig))
		return -1;
	if (polyphony > pLibConfig->maxVoices)
		polyphony = pLibConfig->maxVoices;

	/* calculate buffer size */
	bufferSize = pLibConfig->mixBufferSize * pLibConfig->numChannels * (EAS_I32)sizeof(EAS_PCM) * NUM_BUFFERS;

	/* allocate output buffer memory */
	buffer = malloc((EAS_U32)bufferSize);
	if (!buffer)
	{
		{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_FATAL, "Error allocating memory for audio buffer\n"); */ }
		return EAS_FAILURE;
	}

	/* initialize the EAS library */
	polyphony = pLibConfig->maxVoices;
	if ((result = EAS_Init(&easData)) != EAS_SUCCESS)
	{
		{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_FATAL, "EAS_Init returned %ld - aborting!\n", result); */ }
		free(buffer);
		return result;
	}

	/*
	 * Some debugging environments don't allow for passed parameters.
	 * In this case, just play the default MIDI file "test.mid"
	 */
	if (argc < 2)
	{
		{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_INFO, "Playing '%s'\n", defaultTestFile); */ }
		if ((playResult = PlayFile(easData, defaultTestFile, NULL, pLibConfig, buffer, bufferSize)) != EAS_SUCCESS)
		{
			{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Error %d playing file %s\n", playResult, defaultTestFile); */ }
		}
	}
	/* iterate through the list of files to be played */
	else
	{
		for (i = 1; i < argc; i++)
		{
			/* check for switch */
			if (argv[i][0] != '-')
			{

				{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_INFO, "Playing '%s'\n", argv[i]); */ }
				if ((playResult = PlayFile(easData, argv[i], outputFile, pLibConfig, buffer, bufferSize)) != EAS_SUCCESS)
				{
					{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Error %d playing file %s\n", playResult, argv[i]); */ }
					break;
				}
			}
		}
	}

	/* shutdown the EAS library */
	if ((result = EAS_Shutdown(easData)) != EAS_SUCCESS)
	{
		{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_FATAL, "EAS_Shutdown returned %ld\n", result); */ }
	}

	/* free the output buffer */
	free(buffer);

	/* close the debug file */
	if (debugFile)
		fclose(debugFile);

	/* play errors take precedence over shutdown errors */
	if (playResult != EAS_SUCCESS)
		return playResult;
	return result;
} /* end main */

/*----------------------------------------------------------------------------
 * StrCopy()
 *----------------------------------------------------------------------------
 * Purpose: 
 * Safe string copy
 * 
 * Inputs: 
 *			
 * Outputs:
 *
 *----------------------------------------------------------------------------
*/
static void StrCopy(char *dest, const char *src, EAS_I32 size)
{
	int len;

	strncpy(dest, src, (size_t) size-1);
	len = (int) strlen(src);
	if (len < size)
		dest[len] = 0;
} /* end StrCopy */

/*----------------------------------------------------------------------------
 * ChangeFileExt()
 *----------------------------------------------------------------------------
 * Purpose: 
 * Changes the file extension of a filename
 * 
 * Inputs: 
 *			
 * Outputs:
 *
 *----------------------------------------------------------------------------
*/
static EAS_BOOL ChangeFileExt(char *str, const char *ext, EAS_I32 size)
{
	char *p;

	/* find the extension, if any */
	p = strrchr(str,'.');
	if (!p)
	{
		if ((EAS_I32)(strlen(str) + 5) > size)
			return EAS_FALSE;
		strcat(str,".");
		strcat(str,ext);
		return EAS_TRUE;
	}

	/* make sure there's room for the extension */
	p++;
	*p = 0;
	if ((EAS_I32)(strlen(str) + 4) > size)
		return EAS_FALSE;
	strcat(str,ext);
	return EAS_TRUE;
} /* end ChangeFileExt */

/*----------------------------------------------------------------------------
 * EASLibraryCheck()
 *----------------------------------------------------------------------------
 * Purpose: 
 * Displays the library version and checks it against the header
 * file used to build this code.
 *
 * Inputs:
 * pLibConfig		- library configuration retrieved from the library
 *		
 * Outputs:
 * returns EAS_TRUE if matched
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
static EAS_BOOL EASLibraryCheck (const S_EAS_LIB_CONFIG *pLibConfig)
{

	/* display the library version */
	{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_INFO, "EAS Library Version %d.%d.%d.%d\n",
		pLibConfig->libVersion >> 24,
		(pLibConfig->libVersion >> 16) & 0x0f,
		(pLibConfig->libVersion >> 8) & 0x0f,
		pLibConfig->libVersion & 0x0f); */ }

	/* display some info about the library build */
	if (pLibConfig->checkedVersion)
		{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_INFO, "\tChecked library\n"); */ }
	{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_INFO, "\tMaximum polyphony: %d\n", pLibConfig->maxVoices); */ }
	{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_INFO, "\tNumber of channels: %d\n", pLibConfig->numChannels); */ }
	{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_INFO, "\tSample rate: %d\n", pLibConfig->sampleRate); */ }
	{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_INFO, "\tMix buffer size: %d\n", pLibConfig->mixBufferSize); */ }
	if (pLibConfig->filterEnabled)
		{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_INFO, "\tFilter enabled\n"); */ }
#ifndef _WIN32_WCE
	{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_INFO, "\tLibrary Build Timestamp: %s", ctime((time_t*)&pLibConfig->buildTimeStamp)); */ }
#endif
	{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_INFO, "\tLibrary Build ID: %s\n", pLibConfig->buildGUID); */ }
		
	/* check it against the header file used to build this code */
	/*lint -e{778} constant expression used for display purposes may evaluate to zero */
	if (LIB_VERSION != pLibConfig->libVersion)
	{
		{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_FATAL, "Library version does not match header files. EAS Header Version %d.%d.%d.%d\n", 
			LIB_VERSION >> 24,
			(LIB_VERSION >> 16) & 0x0f,
			(LIB_VERSION >> 8) & 0x0f,
			LIB_VERSION & 0x0f); */ }
		return EAS_FALSE;
	}
	return EAS_TRUE;
} /* end EASLibraryCheck */