summaryrefslogtreecommitdiffstats
path: root/arm-hybrid-22k/lib_src/eas_ota.c
blob: fb81d62dface92093896e883afbd19ccb2c4e5ec (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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
/*----------------------------------------------------------------------------
 *
 * File: 
 * eas_ota.c
 *
 * Contents and purpose:
 * OTA parser
 *
 * 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: 795 $
 *   $Date: 2007-08-01 00:14:45 -0700 (Wed, 01 Aug 2007) $
 *----------------------------------------------------------------------------
*/

#include "eas_data.h"
#include "eas_miditypes.h"
#include "eas_parser.h"
#include "eas_report.h"
#include "eas_host.h"
#include "eas_midi.h"
#include "eas_config.h"
#include "eas_vm_protos.h"
#include "eas_otadata.h"

/* increase gain for mono ringtones */
#define OTA_GAIN_OFFSET				8

/* file definitions */
#define OTA_RINGTONE 				0x25
#define OTA_SOUND 					0x1d
#define OTA_UNICODE					0x22

/* song type definitions */
#define OTA_BASIC_SONG_TYPE			0x01
#define OTA_TEMPORARY_SONG_TYPE		0x02

/* instruction ID coding */
#define OTA_PATTERN_HEADER_ID		0x00
#define OTA_NOTE_INST_ID			0x01
#define OTA_SCALE_INST_ID			0x02
#define OTA_STYLE_INST_ID			0x03
#define OTA_TEMPO_INST_ID			0x04
#define OTA_VOLUME_INST_ID			0x05

/* note durations */
#define OTA_NORMAL_DURATION			0x00
#define OTA_DOTTED_NOTE				0x01
#define OTA_DOUBLE_DOTTED_NOTE		0x02
#define OTA_TRIPLET_NOTE			0x03

/* loop count value for infinite loop */
#define OTA_INFINITE_LOOP			0x0f

/* length of 32nd note in 1/256ths of a msec for 63 BPM tempo */
#define DEFAULT_TICK_CONV			30476

/* default channel and program for OTA playback */
#define OTA_CHANNEL					0
#define OTA_PROGRAM					80
#define OTA_VEL_MUL					4
#define OTA_VEL_OFS					67
#define OTA_VEL_DEFAULT				95

/* multiplier for fixed point triplet conversion */
#define TRIPLET_MULTIPLIER			683
#define TRIPLET_SHIFT				10

/* local prototypes */
static EAS_RESULT OTA_CheckFileType (S_EAS_DATA *pEASData, EAS_FILE_HANDLE fileHandle, EAS_VOID_PTR *ppHandle, EAS_I32 offset);
static EAS_RESULT OTA_Prepare (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData);
static EAS_RESULT OTA_Time (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_U32 *pTime);
static EAS_RESULT OTA_Event (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_INT parserMode);
static EAS_RESULT OTA_State (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_STATE *pState);
static EAS_RESULT OTA_Close (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData);
static EAS_RESULT OTA_Reset (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData);
static EAS_RESULT OTA_Pause (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData);
static EAS_RESULT OTA_Resume (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData);
static EAS_RESULT OTA_SetData (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_I32 param, EAS_I32 value);
static EAS_RESULT OTA_GetData (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_I32 param, EAS_I32 *pValue);
static EAS_RESULT OTA_ParseHeader (S_EAS_DATA *pEASData, S_OTA_DATA* pData);
static EAS_RESULT OTA_FetchBitField (EAS_HW_DATA_HANDLE hwInstData, S_OTA_DATA *pData, EAS_I32 numBits, EAS_U8 *pValue);
static EAS_RESULT OTA_SavePosition (EAS_HW_DATA_HANDLE hwInstData, S_OTA_DATA *pData, S_OTA_LOC *pLoc);
static EAS_RESULT OTA_RestorePosition (EAS_HW_DATA_HANDLE hwInstData, S_OTA_DATA *pData, S_OTA_LOC *pLoc);


/*----------------------------------------------------------------------------
 *
 * EAS_OTA_Parser
 *
 * This structure contains the functional interface for the OTA parser 
 *----------------------------------------------------------------------------
*/
const S_FILE_PARSER_INTERFACE EAS_OTA_Parser =
{
	OTA_CheckFileType,
	OTA_Prepare,
	OTA_Time,
	OTA_Event,
	OTA_State,
	OTA_Close,
	OTA_Reset,
	OTA_Pause,
	OTA_Resume,
	NULL,
	OTA_SetData,
	OTA_GetData,
	NULL
};

/*----------------------------------------------------------------------------
 *
 * bpmTable
 *
 * BPM conversion table. Converts bpm values to 256ths of a millisecond for a 32nd note 
 *----------------------------------------------------------------------------
*/
static const EAS_U32 bpmTable[32] =
{
	76800, 68571, 61935, 54857, 
	48000, 42667, 38400, 34286,
	30476, 27429, 24000, 21333,
	19200, 17143, 15360, 13714,
	12000, 10667, 9600, 8533,
	7680, 6737, 6000, 5408,
	4800, 4267, 3840, 3398,
	3024, 2685, 2400, 2133
};

/*----------------------------------------------------------------------------
 * OTA_CheckFileType()
 *----------------------------------------------------------------------------
 * Purpose: 
 * Check the file type to see if we can parse it
 *
 * Inputs:
 * pEASData			- pointer to overall EAS data structure
 * handle			- pointer to file handle
 *		
 * Outputs:
 * 
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
static EAS_RESULT OTA_CheckFileType (S_EAS_DATA *pEASData, EAS_FILE_HANDLE fileHandle, EAS_VOID_PTR *ppHandle, EAS_I32 offset)
{
	S_OTA_DATA* pData;
	EAS_RESULT result;
	EAS_INT cmdLen;
	EAS_INT state;
	EAS_U8 temp;
	
	/* read the first byte, should be command length */
	*ppHandle = NULL;
	if ((result = EAS_HWGetByte(pEASData->hwInstData, fileHandle, &temp)) != EAS_SUCCESS)
		return result;

	/* read all the commands */
	cmdLen = temp;
	state = 0;
	while (cmdLen--)
	{
	
		/* read the command, upper 7 bits */	
		if ((result = EAS_HWGetByte(pEASData->hwInstData, fileHandle, &temp)) != EAS_SUCCESS)
			return result;
		temp = temp >> 1;

		if (state == 0)
		{
			if (temp != OTA_RINGTONE)
				break;
			state++;
		}
		else
		{
	
			if (temp == OTA_SOUND)
			{
				
				/* check for static memory allocation */
				if (pEASData->staticMemoryModel)
					pData = EAS_CMEnumData(EAS_CM_OTA_DATA);
				else
					pData = EAS_HWMalloc(pEASData->hwInstData, sizeof(S_OTA_DATA));
				if (!pData)
				{
					{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Malloc failed in OTA_Prepare\n"); */ }
					return EAS_ERROR_MALLOC_FAILED;
				}
				EAS_HWMemSet(pData, 0, sizeof(S_OTA_DATA));

				/* return a pointer to the instance data */
				pData->fileHandle = fileHandle;
				pData->fileOffset = offset;
				pData->state = EAS_STATE_OPEN;
				*ppHandle = pData;
				break;
			}

			if (temp != OTA_UNICODE)
				break;
		}
	}

	/* not recognized */
	return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 * OTA_Prepare()
 *----------------------------------------------------------------------------
 * Purpose: 
 * Prepare to parse the file. Allocates instance data (or uses static allocation for
 * static memory model). 
 *
 * Inputs:
 * pEASData			- pointer to overall EAS data structure
 * handle			- pointer to file handle
 *		
 * Outputs:
 * 
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
static EAS_RESULT OTA_Prepare (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData)
{
	S_OTA_DATA* pData;
	EAS_RESULT result;

	/* check for valid state */
	pData = (S_OTA_DATA*) pInstData;
	if (pData->state != EAS_STATE_OPEN)
		return EAS_ERROR_NOT_VALID_IN_THIS_STATE;

	/* instantiate a synthesizer */
	if ((result = VMInitMIDI(pEASData, &pData->pSynth)) != EAS_SUCCESS)
	{
		{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "VMInitMIDI returned %d\n", result); */ }
		return result;
	}
		
	pData->state = EAS_STATE_ERROR;
	if ((result = OTA_ParseHeader(pEASData, pData)) != EAS_SUCCESS)
		return result;

	pData->state = EAS_STATE_READY;
	return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 * OTA_Time()
 *----------------------------------------------------------------------------
 * Purpose: 
 * Returns the time of the next event in msecs
 *
 * Inputs:
 * pEASData			- pointer to overall EAS data structure
 * handle			- pointer to file handle
 * pTime			- pointer to variable to hold time of next event (in msecs)
 *		
 * Outputs:
 * 
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, pEASData) common decoder interface - pEASData not used */
static EAS_RESULT OTA_Time (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_U32 *pTime)
{
	S_OTA_DATA *pData;
	
	pData = (S_OTA_DATA*) pInstData;

	/* return time in milliseconds */
	/*lint -e{704} use shift instead of division */
	*pTime = pData->time >> 8;
	return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 * OTA_Event()
 *----------------------------------------------------------------------------
 * Purpose: 
 * Parse the next event in the file
 *
 * Inputs:
 * pEASData			- pointer to overall EAS data structure
 * handle			- pointer to file handle
 *		
 * Outputs:
 * 
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
static EAS_RESULT OTA_Event (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_INT parserMode)
{
	S_OTA_DATA* pData;
	EAS_RESULT result;
	EAS_U32 duration;
	EAS_U8 temp;

	pData = (S_OTA_DATA*) pInstData;
	if (pData->state >= EAS_STATE_OPEN)
		return EAS_SUCCESS;
	
	/* initialize MIDI channel when the track starts playing */
	if (pData->time == 0)
	{
		/* set program to square lead */
		if (parserMode != eParserModeMetaData)
			VMProgramChange(pEASData->pVoiceMgr, pData->pSynth, OTA_CHANNEL, OTA_PROGRAM);

		/* set channel volume to max */
		if (parserMode != eParserModeMetaData)
			VMControlChange(pEASData->pVoiceMgr, pData->pSynth, OTA_CHANNEL, 7, 127);
	}

	/* check for end of note */
	if (pData->note)
	{
		/* stop the note */
		VMStopNote(pEASData->pVoiceMgr, pData->pSynth, OTA_CHANNEL, pData->note, 0);
		pData->note = 0;

		/* check for rest between notes */
		if (pData->restTicks)
		{
			pData->time += (EAS_I32) pData->restTicks;
			pData->restTicks = 0;
			return EAS_SUCCESS;
		}
	}

	/* if not in a pattern, read the pattern header */
	while (pData->current.patternLen == 0)
	{

		/* check for loop - don't do infinite loops when locating */
		if (pData->loopCount && ((parserMode == eParserModePlay) || (pData->loopCount != OTA_INFINITE_LOOP)))
		{
			/* if not infinite loop, decrement loop count */
			if (pData->loopCount != OTA_INFINITE_LOOP)
				pData->loopCount--;
			
			/* back to start of pattern*/
			if ((result = OTA_RestorePosition(pEASData->hwInstData, pData, &pData->patterns[pData->currentPattern])) != EAS_SUCCESS)
				return result;
		}

		/* if no previous position to restore, continue forward */
		else if (pData->restore.fileOffset < 0)
		{
		
			/* check for end of song */
			if (pData->numPatterns == 0)
			{
				pData->state = EAS_STATE_STOPPING;
				VMReleaseAllVoices(pEASData->pVoiceMgr, pData->pSynth);
				return EAS_SUCCESS;
			}

			/* read the next pattern header */
			if ((result = OTA_FetchBitField(pEASData->hwInstData, pData, 3, &temp)) != EAS_SUCCESS)
				return result;
			if (temp != OTA_PATTERN_HEADER_ID)
			{
				{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Expected OTA pattern header\n"); */ }
				return EAS_ERROR_FILE_FORMAT;
			}

			/* get the pattern ID */
			if ((result = OTA_FetchBitField(pEASData->hwInstData, pData, 2, &pData->currentPattern)) != EAS_SUCCESS)
				return result;

			/* get the loop count */
			if ((result = OTA_FetchBitField(pEASData->hwInstData, pData, 4, &pData->loopCount)) != EAS_SUCCESS)
				return result;

			/* get the pattern length */
			if ((result = OTA_FetchBitField(pEASData->hwInstData, pData, 8, &pData->current.patternLen)) != EAS_SUCCESS)
				return result;

			/* if pattern definition, save the current position */
			if (pData->current.patternLen)
			{
				if ((result = OTA_SavePosition(pEASData->hwInstData, pData, &pData->patterns[pData->currentPattern])) != EAS_SUCCESS)
					return result;
			}
				
			/* if pattern length is zero, repeat a previous pattern */
			else
			{
				/* make sure it's a valid pattern */
				if (pData->patterns[pData->currentPattern].fileOffset < 0)
				{
					{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "OTA pattern error, invalid pattern specified\n"); */ }
					return EAS_ERROR_FILE_FORMAT;
				}

				/* save current position and data */
				if ((result = OTA_SavePosition(pEASData->hwInstData, pData, &pData->restore)) != EAS_SUCCESS)
					return result;

				/* seek to the pattern in the file */
				if ((result = OTA_RestorePosition(pEASData->hwInstData, pData, &pData->patterns[pData->currentPattern])) != EAS_SUCCESS)
					return result;
			}

			/* decrement pattern count */
			pData->numPatterns--;
		}

		/* restore previous position */
		else
		{
			if ((result = OTA_RestorePosition(pEASData->hwInstData, pData, &pData->restore)) != EAS_SUCCESS)
				return result;
		}
	}

	/* get the next event */
	if ((result = OTA_FetchBitField(pEASData->hwInstData, pData, 3, &temp)) != EAS_SUCCESS)
		return result;

	switch (temp)
	{
		case OTA_NOTE_INST_ID:
			/* fetch note value */
			if ((result = OTA_FetchBitField(pEASData->hwInstData, pData, 4, &pData->note)) != EAS_SUCCESS)
				return result;
			
			/* fetch note duration */
			if ((result = OTA_FetchBitField(pEASData->hwInstData, pData, 3, &temp)) != EAS_SUCCESS)
				return result;
			duration = pData->tick * (0x20 >> temp); 
			
			/* fetch note duration modifier */
			if ((result = OTA_FetchBitField(pEASData->hwInstData, pData, 2, &temp)) != EAS_SUCCESS)
				return result;
			switch (temp)
			{
				case OTA_NORMAL_DURATION:
					break;
					
				case OTA_DOTTED_NOTE:
					duration += duration >> 1;
					break;

				case OTA_DOUBLE_DOTTED_NOTE:
					duration += (duration >> 1) + (duration >> 2);
					break;

				case OTA_TRIPLET_NOTE:
					duration = (duration * TRIPLET_MULTIPLIER) >> TRIPLET_SHIFT;
					break;

				default:
					{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Unrecognized note duration ignored\n"); */ }
					break;
			}

			/* check for note */
			if (pData->note)
			{
				
				/* determine note length based on style */
				switch (pData->style)
				{
					case 0:
						pData->restTicks = duration >> 4;
						break;
					case 1:
						pData->restTicks = 0;
						break;
					case 2:
						pData->restTicks = duration >> 1;
						break;
					default:
						{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Unrecognized note style ignored\n"); */ }
				}

				/* add octave */
				pData->note += pData->octave;
				if (parserMode == eParserModePlay)
					VMStartNote(pEASData->pVoiceMgr, pData->pSynth, OTA_CHANNEL, pData->note, pData->velocity);
				pData->time += (EAS_I32) duration - (EAS_I32) pData->restTicks;
			}
			
			/* this is a rest */
			else
				pData->time += (EAS_I32) duration;
			break;
			
		case OTA_SCALE_INST_ID:
			/* fetch octave */
			if ((result = OTA_FetchBitField(pEASData->hwInstData, pData, 2, &temp)) != EAS_SUCCESS)
				return result;
			pData->octave = (EAS_U8) (temp * 12 + 59);
			break;

		case OTA_STYLE_INST_ID:
			/* fetch note style */
			if ((result = OTA_FetchBitField(pEASData->hwInstData, pData, 2, &pData->style)) != EAS_SUCCESS)
				return result;
			break;

		case OTA_TEMPO_INST_ID:
			/* fetch tempo */
			if ((result = OTA_FetchBitField(pEASData->hwInstData, pData, 5, &temp)) != EAS_SUCCESS)
				return result;
			pData->tick = bpmTable[temp];
			break;

		case OTA_VOLUME_INST_ID:
			/* fetch volume */
			if ((result = OTA_FetchBitField(pEASData->hwInstData, pData, 4, &temp)) != EAS_SUCCESS)
				return result;
			pData->velocity = temp ? (EAS_U8) (temp * OTA_VEL_MUL + OTA_VEL_OFS) : 0;
			break;

		default:
			{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Unexpected instruction ID in OTA stream\n"); */ }
			return EAS_ERROR_FILE_FORMAT;
	}

	/* decrement pattern length */
	pData->current.patternLen--;
	return EAS_SUCCESS;
}
		
/*----------------------------------------------------------------------------
 * OTA_State()
 *----------------------------------------------------------------------------
 * Purpose: 
 * Returns the current state of the stream
 *
 * Inputs:
 * pEASData			- pointer to overall EAS data structure
 * handle			- pointer to file handle
 * pState			- pointer to variable to store state
 *		
 * Outputs:
 * 
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, pEASData) common decoder interface - pEASData not used */
static EAS_RESULT OTA_State (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_I32 *pState)
{
	S_OTA_DATA* pData;

	/* establish pointer to instance data */
	pData = (S_OTA_DATA*) pInstData;

	/* if stopping, check to see if synth voices are active */
	if (pData->state == EAS_STATE_STOPPING)
	{
		if (VMActiveVoices(pData->pSynth) == 0)
			pData->state = EAS_STATE_STOPPED;
	}
	
	if (pData->state == EAS_STATE_PAUSING)
	{
		if (VMActiveVoices(pData->pSynth) == 0)
			pData->state = EAS_STATE_PAUSED;
	}
	
	/* return current state */
	*pState = pData->state;
	return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 * OTA_Close()
 *----------------------------------------------------------------------------
 * Purpose: 
 * Close the file and clean up
 *
 * Inputs:
 * pEASData			- pointer to overall EAS data structure
 * handle			- pointer to file handle
 *		
 * Outputs:
 * 
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
static EAS_RESULT OTA_Close (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData)
{
	S_OTA_DATA* pData;
	EAS_RESULT result;

	pData = (S_OTA_DATA*) pInstData;	

	/* close the file */
	if ((result = EAS_HWCloseFile(pEASData->hwInstData, pData->fileHandle)) != EAS_SUCCESS)
			return result;

	/* free the synth */
	if (pData->pSynth != NULL)
		VMMIDIShutdown(pEASData, pData->pSynth);

	/* if using dynamic memory, free it */
	if (!pEASData->staticMemoryModel)
		EAS_HWFree(pEASData->hwInstData, pData);

	return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 * OTA_Reset()
 *----------------------------------------------------------------------------
 * Purpose: 
 * Reset the sequencer. Used for locating backwards in the file.
 *
 * Inputs:
 * pEASData			- pointer to overall EAS data structure
 * handle			- pointer to file handle
 *		
 * Outputs:
 * 
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
static EAS_RESULT OTA_Reset (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData)
{
	S_OTA_DATA* pData;
	EAS_RESULT result;

	pData = (S_OTA_DATA*) pInstData;

	/* reset the synth */
	VMReset(pEASData->pVoiceMgr, pData->pSynth, EAS_TRUE);
	pData->note = 0;

	/* reset file position and re-parse header */
	pData->state = EAS_STATE_ERROR;
	if ((result = OTA_ParseHeader (pEASData,  pData)) != EAS_SUCCESS)
		return result;

	pData->state = EAS_STATE_READY;
	return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 * OTA_Pause()
 *----------------------------------------------------------------------------
 * Purpose: 
 * Pauses the sequencer. Mutes all voices and sets state to pause.
 *
 * Inputs:
 * pEASData			- pointer to overall EAS data structure
 * handle			- pointer to file handle
 *		
 * Outputs:
 * 
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
static EAS_RESULT OTA_Pause (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData)
{
	S_OTA_DATA *pData;

	/* can't pause a stopped stream */
	pData = (S_OTA_DATA*) pInstData;
	if (pData->state == EAS_STATE_STOPPED)
		return EAS_ERROR_ALREADY_STOPPED;

	/* mute the synthesizer */
	VMMuteAllVoices(pEASData->pVoiceMgr, pData->pSynth);
	pData->state = EAS_STATE_PAUSING;	
	return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 * OTA_Resume()
 *----------------------------------------------------------------------------
 * Purpose: 
 * Resume playing after a pause, sets state back to playing.
 *
 * Inputs:
 * pEASData			- pointer to overall EAS data structure
 * handle			- pointer to file handle
 *		
 * Outputs:
 * 
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, pEASData) common decoder interface - pEASData not used */
static EAS_RESULT OTA_Resume (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData)
{
	S_OTA_DATA *pData;

	/* can't resume a stopped stream */
	pData = (S_OTA_DATA*) pInstData;
	if (pData->state == EAS_STATE_STOPPED)
		return EAS_ERROR_ALREADY_STOPPED;

	/* nothing to do but resume playback */
	pData->state = EAS_STATE_PLAY;	
	return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 * OTA_SetData()
 *----------------------------------------------------------------------------
 * Purpose: 
 * Return file type
 *
 * Inputs:
 * pEASData			- pointer to overall EAS data structure
 * handle			- pointer to file handle
 *		
 * Outputs:
 * 
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, pEASData) common decoder interface - pEASData not used */
static EAS_RESULT OTA_SetData (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_I32 param, EAS_I32 value)
{
	S_OTA_DATA *pData;

	pData = (S_OTA_DATA *) pInstData;
	switch (param)
	{

		/* set metadata callback */
		case PARSER_DATA_METADATA_CB:
			EAS_HWMemCpy(&pData->metadata, (void*) value, sizeof(S_METADATA_CB));
			break;

		default:
			return EAS_ERROR_INVALID_PARAMETER;
	}

	return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 * OTA_GetData()
 *----------------------------------------------------------------------------
 * Purpose: 
 * Return file type
 *
 * Inputs:
 * pEASData			- pointer to overall EAS data structure
 * handle			- pointer to file handle
 *		
 * Outputs:
 * 
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, pEASData) common decoder interface - pEASData not used */
static EAS_RESULT OTA_GetData (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_I32 param, EAS_I32 *pValue)
{
	S_OTA_DATA *pData;

	pData = (S_OTA_DATA*) pInstData;
	switch (param)
	{
		/* return file type as OTA */
		case PARSER_DATA_FILE_TYPE:
			*pValue = EAS_FILE_OTA;
			break;

#if 0			
		/* set transposition */
		case PARSER_DATA_TRANSPOSITION:
			*pValue = pData->transposition;
			break;
#endif

		case PARSER_DATA_SYNTH_HANDLE:
			*pValue = (EAS_I32) pData->pSynth;
			break;

		case PARSER_DATA_GAIN_OFFSET:
			*pValue = OTA_GAIN_OFFSET;
			break;
			
		default:
			return EAS_ERROR_INVALID_PARAMETER;
	}			
	return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 * OTA_ParseHeader()
 *----------------------------------------------------------------------------
 * Purpose: 
 * Prepare to parse the file. Allocates instance data (or uses static allocation for
 * static memory model). 
 *
 * Inputs:
 * pEASData			- pointer to overall EAS data structure
 * handle			- pointer to file handle
 *		
 * Outputs:
 * 
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
static EAS_RESULT OTA_ParseHeader (S_EAS_DATA *pEASData, S_OTA_DATA* pData)
{
	EAS_RESULT result;
	EAS_INT i;
	EAS_INT state;
	EAS_U8 temp;
	EAS_U8 titleLen;

	/* initialize some data */
	pData->flags = 0;
	pData->time = 0;
	pData->tick = DEFAULT_TICK_CONV;
	pData->patterns[0].fileOffset = pData->patterns[1].fileOffset =
		pData->patterns[2].fileOffset = pData->patterns[3].fileOffset = -1;
	pData->current.bitCount = 0;
	pData->current.patternLen = 0;
	pData->loopCount = 0;
	pData->restore.fileOffset = -1;
	pData->note = 0;
	pData->restTicks = 0;
	pData->velocity = OTA_VEL_DEFAULT;
	pData->style = 0;
	pData->octave = 59;

	/* seek to start of data */
	if ((result = EAS_HWFileSeek(pEASData->hwInstData, pData->fileHandle, pData->fileOffset)) != EAS_SUCCESS)
		return result;

	/* read the first byte, should be command length */
	if ((result = EAS_HWGetByte(pEASData->hwInstData, pData->fileHandle, &temp)) != EAS_SUCCESS)
		return result;

	/* read all the commands */
	i = temp;
	state = 0;
	while (i--)
	{

		/* fetch command, always starts on byte boundary */
		pData->current.bitCount = 0;
		if ((result = OTA_FetchBitField(pEASData->hwInstData, pData, 7, &temp)) != EAS_SUCCESS)
			return result;

		if (state == 0)
		{
			if (temp != OTA_RINGTONE)
			{
				{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Expected OTA Ring Tone Programming type\n"); */ }
				return EAS_ERROR_FILE_FORMAT;
			}
			state++;
		}
		else
		{
	
			if (temp == OTA_SOUND)
				break;

			if (temp == OTA_UNICODE)
				pData->flags |= OTA_FLAGS_UNICODE;
			else
			{
				{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Expected OTA Sound or Unicode type\n"); */ }
				return EAS_ERROR_FILE_FORMAT;
			}
		}
	}

	/* get song type */
	if ((result = OTA_FetchBitField(pEASData->hwInstData, pData, 3, &temp)) != EAS_SUCCESS)
		return result;

	/* check for basic song type */
	if (temp == OTA_BASIC_SONG_TYPE)
	{
		/* fetch title length */
		if ((result = OTA_FetchBitField(pEASData->hwInstData, pData, 4, &titleLen)) != EAS_SUCCESS)
			return result;

		/* if unicode, double the length */
		if (pData->flags & OTA_FLAGS_UNICODE)
			titleLen = (EAS_U8) (titleLen << 1);

		/* zero the metadata buffer */
		if (pData->metadata.buffer)
			EAS_HWMemSet(pData->metadata.buffer, 0, pData->metadata.bufferSize);
	
		/* read the song title */
		for (i = 0; i < titleLen; i++)
		{
			/* fetch character */
			if ((result = OTA_FetchBitField(pEASData->hwInstData, pData, 8, &temp)) != EAS_SUCCESS)
				return result;
			
			/* check for metadata callback */
			if (pData->metadata.callback)
			{
				if (i < (pData->metadata.bufferSize - 1))
					pData->metadata.buffer[i] = (char) temp;
			}
		}

		/* if host has registered callback, call it now */
		if (pData->metadata.callback)
			(*pData->metadata.callback)(EAS_METADATA_TITLE, pData->metadata.buffer, pData->metadata.pUserData);
	}

	/* must be temporary song */
	else if (temp != OTA_TEMPORARY_SONG_TYPE)
	{
		{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Expected OTA basic or temporary song type\n"); */ }
		return EAS_ERROR_FILE_FORMAT;
	}

	/* get the song length */
	if ((result = OTA_FetchBitField(pEASData->hwInstData, pData, 8, &pData->numPatterns)) != EAS_SUCCESS)
		return result;

	/* sanity check */
	if (pData->numPatterns == 0)
	{
		{ /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "OTA number of patterns is zero\n"); */ }
		return EAS_ERROR_FILE_FORMAT;
	}

	/* at start of first pattern */
	return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 * OTA_FetchBitField()
 *----------------------------------------------------------------------------
 * Purpose: 
 * Fetch a specified number of bits from the input stream
 *
 * Inputs:
 * 
 *		
 * Outputs:
 * 
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
static EAS_RESULT OTA_FetchBitField (EAS_HW_DATA_HANDLE hwInstData, S_OTA_DATA *pData, EAS_I32 numBits, EAS_U8 *pValue)
{
	EAS_RESULT result;
	EAS_I32 bitsLeft;
	EAS_U8 value;

	value = 0;
	
	/* do we have enough bits? */
	bitsLeft = pData->current.bitCount - numBits;

	/* not enough bits, assemble them from 2 characters */
	if (bitsLeft < 0)
	{
		/* grab the remaining bits from the previous byte */
		if (pData->current.bitCount)
			/*lint -e{504,734} this is a legitimate shift operation */
			value = pData->current.dataByte << -bitsLeft;

		/* read the next byte */
		if ((result = EAS_HWGetByte(hwInstData, pData->fileHandle, &pData->current.dataByte)) != EAS_SUCCESS)
			return result;
		bitsLeft += 8;
	}

	/* more bits than needed? */
	if (bitsLeft > 0)
	{
		value |= pData->current.dataByte >> bitsLeft;
		pData->current.bitCount = (EAS_U8) bitsLeft;
		pData->current.dataByte = pData->current.dataByte & (0xff >> (8 - bitsLeft));
	}

	/* exactly the right number of bits */
	else
	{
		value |= pData->current.dataByte;
		pData->current.bitCount = 0;
	}

	*pValue = value;
	return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 * OTA_SavePosition()
 *----------------------------------------------------------------------------
 * Purpose: 
 * 
 *
 * Inputs:
 * 
 *		
 * Outputs:
 * 
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
static EAS_RESULT OTA_SavePosition (EAS_HW_DATA_HANDLE hwInstData, S_OTA_DATA *pData, S_OTA_LOC *pLoc)
{
	EAS_HWMemCpy(pLoc, &pData->current, sizeof(S_OTA_LOC));
	return EAS_HWFilePos(hwInstData, pData->fileHandle, &pLoc->fileOffset);
}

/*----------------------------------------------------------------------------
 * OTA_RestorePosition()
 *----------------------------------------------------------------------------
 * Purpose: 
 * 
 *
 * Inputs:
 * 
 *		
 * Outputs:
 * 
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
static EAS_RESULT OTA_RestorePosition (EAS_HW_DATA_HANDLE hwInstData, S_OTA_DATA *pData, S_OTA_LOC *pLoc)
{
	EAS_HWMemCpy(&pData->current, pLoc, sizeof(S_OTA_LOC));
	pData->restore.fileOffset = -1;
	return EAS_HWFileSeek(hwInstData, pData->fileHandle, pLoc->fileOffset);
}