summaryrefslogtreecommitdiff
path: root/arm-wt-22k/lib_src/eas_public.c
blob: b3161f0720fc012ca7696aa7c04d5d997be4de8d (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
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
/*----------------------------------------------------------------------------
 *
 * File:
 * eas_public.c
 *
 * Contents and purpose:
 * Contains EAS library public interface
 *
 * 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: 842 $
 *   $Date: 2007-08-23 14:32:31 -0700 (Thu, 23 Aug 2007) $
 *----------------------------------------------------------------------------
*/

#define LOG_TAG "Sonivox"
#include "log/log.h"

#include "eas_synthcfg.h"
#include "eas.h"
#include "eas_config.h"
#include "eas_host.h"
#include "eas_report.h"
#include "eas_data.h"
#include "eas_parser.h"
#include "eas_pcm.h"
#include "eas_midi.h"
#include "eas_mixer.h"
#include "eas_build.h"
#include "eas_vm_protos.h"
#include "eas_math.h"

#ifdef JET_INTERFACE
#include "jet_data.h"
#endif

#ifdef DLS_SYNTHESIZER
#include "eas_mdls.h"
#endif

/* number of events to parse before calling EAS_HWYield function */
#define YIELD_EVENT_COUNT       10

/*----------------------------------------------------------------------------
 * easLibConfig
 *
 * This structure is available through the EAS public interface to allow
 * the user to check the configuration of the library.
 *----------------------------------------------------------------------------
*/
static const S_EAS_LIB_CONFIG easLibConfig =
{
    LIB_VERSION,
#ifdef _CHECKED_BUILD
    EAS_TRUE,
#else
    EAS_FALSE,
#endif
    MAX_SYNTH_VOICES,
    NUM_OUTPUT_CHANNELS,
    _OUTPUT_SAMPLE_RATE,
    BUFFER_SIZE_IN_MONO_SAMPLES,
#ifdef _FILTER_ENABLED
    EAS_TRUE,
#else
    EAS_FALSE,
#endif
    _BUILD_TIME_,
    _BUILD_VERSION_
};

/* local prototypes */
static EAS_RESULT EAS_ParseEvents (S_EAS_DATA *pEASData, S_EAS_STREAM *pStream, EAS_U32 endTime, EAS_INT parseMode);

/*----------------------------------------------------------------------------
 * EAS_SetStreamParameter
 *----------------------------------------------------------------------------
 * Sets the specified parameter in the stream. Allows access to
 * customizable settings within the individual file parsers.
 *----------------------------------------------------------------------------
 * pEASData         - pointer to EAS persistent data object
 * pStream          - stream handle
 * param            - enumerated parameter (see eas_parser.h)
 * value            - new value
 *----------------------------------------------------------------------------
*/
EAS_RESULT EAS_SetStreamParameter (S_EAS_DATA *pEASData, EAS_HANDLE pStream, EAS_I32 param, EAS_I32 value)
{
    S_FILE_PARSER_INTERFACE *pParserModule;

    pParserModule = (S_FILE_PARSER_INTERFACE*) pStream->pParserModule;
    if (pParserModule->pfSetData)
        return (*pParserModule->pfSetData)(pEASData, pStream->handle, param, value);
    return EAS_ERROR_FEATURE_NOT_AVAILABLE;
}

/*----------------------------------------------------------------------------
 * EAS_GetStreamParameter
 *----------------------------------------------------------------------------
 * Sets the specified parameter in the stream. Allows access to
 * customizable settings within the individual file parsers.
 *----------------------------------------------------------------------------
 * pEASData         - pointer to EAS persistent data object
 * pStream          - stream handle
 * param            - enumerated parameter (see eas_parser.h)
 * pValue           - pointer to variable to receive current setting
 *----------------------------------------------------------------------------
*/
EAS_RESULT EAS_GetStreamParameter (S_EAS_DATA *pEASData, EAS_HANDLE pStream, EAS_I32 param, EAS_I32 *pValue)
{
    S_FILE_PARSER_INTERFACE *pParserModule;

    pParserModule = (S_FILE_PARSER_INTERFACE*) pStream->pParserModule;
    if (pParserModule->pfGetData)
        return (*pParserModule->pfGetData)(pEASData, pStream->handle, param, pValue);
    return EAS_ERROR_FEATURE_NOT_AVAILABLE;
}

/*----------------------------------------------------------------------------
 * EAS_StreamReady()
 *----------------------------------------------------------------------------
 * This routine sets common parameters like transpose, volume, etc.
 * First, it attempts to use the parser EAS_SetStreamParameter interface. If that
 * fails, it attempts to get the synth handle from the parser and
 * set the parameter directly on the synth. This eliminates duplicate
 * code in the parser.
 *----------------------------------------------------------------------------
*/
EAS_BOOL EAS_StreamReady (S_EAS_DATA *pEASData, EAS_HANDLE pStream)
{
    S_FILE_PARSER_INTERFACE *pParserModule;
    EAS_STATE state;

    pParserModule = (S_FILE_PARSER_INTERFACE*) pStream->pParserModule;
    if (pParserModule->pfState(pEASData, pStream->handle, &state) != EAS_SUCCESS)
        return EAS_FALSE;
    return (state < EAS_STATE_OPEN);
}

/*----------------------------------------------------------------------------
 * EAS_IntSetStrmParam()
 *----------------------------------------------------------------------------
 * This routine sets common parameters like transpose, volume, etc.
 * First, it attempts to use the parser EAS_SetStreamParameter interface. If that
 * fails, it attempts to get the synth handle from the parser and
 * set the parameter directly on the synth. This eliminates duplicate
 * code in the parser.
 *----------------------------------------------------------------------------
*/
EAS_RESULT EAS_IntSetStrmParam (S_EAS_DATA *pEASData, EAS_HANDLE pStream, EAS_INT param, EAS_I32 value)
{
    S_SYNTH *pSynth;

    /* try to set the parameter using stream interface */
    if (EAS_SetStreamParameter(pEASData, pStream, param, value) == EAS_SUCCESS)
        return EAS_SUCCESS;

    /* get a pointer to the synth object and set it directly */
    /*lint -e{740} we are cheating by passing a pointer through this interface */
    if (EAS_GetStreamParameter(pEASData, pStream, PARSER_DATA_SYNTH_HANDLE, (EAS_I32*) &pSynth) != EAS_SUCCESS)
        return EAS_ERROR_INVALID_PARAMETER;

    if (pSynth == NULL)
        return EAS_ERROR_INVALID_PARAMETER;

    switch (param)
    {

#ifdef DLS_SYNTHESIZER
        case PARSER_DATA_DLS_COLLECTION:
            {
                EAS_RESULT result = VMSetDLSLib(pSynth, (EAS_DLSLIB_HANDLE) value);
                if (result == EAS_SUCCESS)
                {
                    DLSAddRef((S_DLS*) value);
                    VMInitializeAllChannels(pEASData->pVoiceMgr, pSynth);
                }
                return result;
            }
#endif

        case PARSER_DATA_EAS_LIBRARY:
            return VMSetEASLib(pSynth, (EAS_SNDLIB_HANDLE) value);

        case PARSER_DATA_POLYPHONY:
            return VMSetPolyphony(pEASData->pVoiceMgr, pSynth, value);

        case PARSER_DATA_PRIORITY:
            return VMSetPriority(pEASData->pVoiceMgr, pSynth, value);

        case PARSER_DATA_TRANSPOSITION:
            VMSetTranposition(pSynth, value);
            break;

        case PARSER_DATA_VOLUME:
            VMSetVolume(pSynth, (EAS_U16) value);
            break;

        default:
            { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Invalid paramter %d in call to EAS_IntSetStrmParam", param); */ }
            return EAS_ERROR_INVALID_PARAMETER;
    }

    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 * EAS_AllocateStream()
 *----------------------------------------------------------------------------
 * Purpose:
 * Allocates a stream handle
 *
 * Inputs:
 *
 * Outputs:
 *
 *----------------------------------------------------------------------------
*/
static EAS_INT EAS_AllocateStream (EAS_DATA_HANDLE pEASData)
{
    EAS_INT streamNum;

    /* check for static allocation, only one stream allowed */
    if (pEASData->staticMemoryModel)
    {
        if (pEASData->streams[0].handle != NULL)
        {
            { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Attempt to open multiple streams in static model\n"); */ }
            return -1;
        }
        return 0;
    }

    /* dynamic model */
    for (streamNum = 0; streamNum < MAX_NUMBER_STREAMS; streamNum++)
        if (pEASData->streams[streamNum].handle == NULL)
            break;
    if (streamNum == MAX_NUMBER_STREAMS)
    {
        { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Exceeded maximum number of open streams\n"); */ }
        return -1;
    }
    return streamNum;
}

/*----------------------------------------------------------------------------
 * EAS_InitStream()
 *----------------------------------------------------------------------------
 * Purpose:
 * Initialize a stream
 *
 * Inputs:
 *
 * Outputs:
 *
 *----------------------------------------------------------------------------
*/
static void EAS_InitStream (S_EAS_STREAM *pStream, EAS_VOID_PTR pParserModule, EAS_VOID_PTR streamHandle)
{
    pStream->pParserModule = pParserModule;
    pStream->handle = streamHandle;
    pStream->time = 0;
    pStream->frameLength = AUDIO_FRAME_LENGTH;
    pStream->repeatCount = 0;
    pStream->volume = DEFAULT_STREAM_VOLUME;
    pStream->streamFlags = 0;
}

/*----------------------------------------------------------------------------
 * EAS_Config()
 *----------------------------------------------------------------------------
 * Purpose:
 * Returns a pointer to a structure containing the configuration options
 * in this library build.
 *
 * Inputs:
 *
 * Outputs:
 *
 *----------------------------------------------------------------------------
*/
EAS_PUBLIC const S_EAS_LIB_CONFIG *EAS_Config (void)
{
    return &easLibConfig;
}

/*----------------------------------------------------------------------------
 * EAS_Init()
 *----------------------------------------------------------------------------
 * Purpose:
 * Initialize the synthesizer library
 *
 * Inputs:
 *  ppEASData       - pointer to data handle variable for this instance
 *
 * Outputs:
 *
 *----------------------------------------------------------------------------
*/
EAS_PUBLIC EAS_RESULT EAS_Init (EAS_DATA_HANDLE *ppEASData)
{
    EAS_HW_DATA_HANDLE pHWInstData;
    EAS_RESULT result;
    S_EAS_DATA *pEASData;
    EAS_INT module;
    EAS_BOOL staticMemoryModel;

    /* get the memory model */
    staticMemoryModel = EAS_CMStaticMemoryModel();

    /* initialize the host wrapper interface */
    *ppEASData = NULL;
    if ((result = EAS_HWInit(&pHWInstData)) != EAS_SUCCESS)
        return result;

    /* check Configuration Module for S_EAS_DATA allocation */
    if (staticMemoryModel)
        pEASData = EAS_CMEnumData(EAS_CM_EAS_DATA);
    else
        pEASData = EAS_HWMalloc(pHWInstData, sizeof(S_EAS_DATA));
    if (!pEASData)
    {
        { /* dpp: EAS_ReportEx(_EAS_SEVERITY_FATAL, "Failed to allocate EAS library memory\n"); */ }
        return EAS_ERROR_MALLOC_FAILED;
    }

    /* initialize some data */
    EAS_HWMemSet(pEASData, 0, sizeof(S_EAS_DATA));
    pEASData->staticMemoryModel = (EAS_BOOL8) staticMemoryModel;
    pEASData->hwInstData = pHWInstData;
    pEASData->renderTime = 0;

    /* set header search flag */
#ifdef FILE_HEADER_SEARCH
    pEASData->searchHeaderFlag = EAS_TRUE;
#endif

    /* initalize parameters */
    EAS_SetVolume(pEASData, NULL, DEFAULT_VOLUME);

#ifdef _METRICS_ENABLED
    /* initalize the metrics module */
    pEASData->pMetricsModule = EAS_CMEnumOptModules(EAS_MODULE_METRICS);
    if (pEASData->pMetricsModule != NULL)
    {
        if ((result = (*pEASData->pMetricsModule->pfInit)(pEASData, &pEASData->pMetricsData)) != EAS_SUCCESS)
        {
            { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Error %ld initializing metrics module\n", result); */ }
            return result;
        }
    }
#endif

    /* initailize the voice manager & synthesizer */
    if ((result = VMInitialize(pEASData)) != EAS_SUCCESS)
        return result;

    /* initialize mix engine */
    if ((result = EAS_MixEngineInit(pEASData)) != EAS_SUCCESS)
    {
        { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Error %ld starting up mix engine\n", result); */ }
        return result;
    }

    /* initialize effects modules */
    for (module = 0; module < NUM_EFFECTS_MODULES; module++)
    {
        pEASData->effectsModules[module].effect = EAS_CMEnumFXModules(module);
        if (pEASData->effectsModules[module].effect != NULL)
        {
            if ((result = (*pEASData->effectsModules[module].effect->pfInit)(pEASData, &pEASData->effectsModules[module].effectData)) != EAS_SUCCESS)
            {
                { /* dpp: EAS_ReportEx(_EAS_SEVERITY_FATAL, "Initialization of effects module %d returned %d\n", module, result); */ }
                return result;
            }
        }
    }

    /* initialize PCM engine */
    if ((result = EAS_PEInit(pEASData)) != EAS_SUCCESS)
    {
        { /* dpp: EAS_ReportEx(_EAS_SEVERITY_FATAL, "EAS_PEInit failed with error code %ld\n", result); */ }
        return result;
    }

    /* return instance data pointer to host */
    *ppEASData = pEASData;

    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 * EAS_Shutdown()
 *----------------------------------------------------------------------------
 * Purpose:
 * Shuts down the library. Deallocates any memory associated with the
 * synthesizer (dynamic memory model only)
 *
 * Inputs:
 *  pEASData        - handle to data for this instance
 *
 * Outputs:
 *
 *----------------------------------------------------------------------------
*/
EAS_PUBLIC EAS_RESULT EAS_Shutdown (EAS_DATA_HANDLE pEASData)
{
    /* check for NULL handle */
    if (!pEASData)
        return EAS_ERROR_HANDLE_INTEGRITY;

    /* establish pointers */
    EAS_HW_DATA_HANDLE hwInstData = pEASData->hwInstData;

    /* if there are streams open, close them */
    EAS_RESULT reportResult = EAS_SUCCESS;

    EAS_RESULT result;
    EAS_INT i;
    for (i = 0; i < MAX_NUMBER_STREAMS; i++)
    {
        if (pEASData->streams[i].pParserModule && pEASData->streams[i].handle)
        {
            if ((result = (*((S_FILE_PARSER_INTERFACE*)(pEASData->streams[i].pParserModule))->pfClose)(pEASData, pEASData->streams[i].handle)) != EAS_SUCCESS)
            {
                { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Error %ld shutting down parser module\n", result); */ }
                reportResult = result;
            }
        }
    }

    /* shutdown PCM engine */
    if ((result = EAS_PEShutdown(pEASData)) != EAS_SUCCESS)
    {
        { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Error %ld shutting down PCM engine\n", result); */ }
        if (reportResult == EAS_SUCCESS)
            reportResult = result;
    }

    /* shutdown mix engine */
    if ((result = EAS_MixEngineShutdown(pEASData)) != EAS_SUCCESS)
    {
        { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Error %ld shutting down mix engine\n", result); */ }
        if (reportResult == EAS_SUCCESS)
            reportResult = result;
    }

    /* shutdown effects modules */
    for (i = 0; i < NUM_EFFECTS_MODULES; i++)
    {
        if (pEASData->effectsModules[i].effect)
        {
            if ((result = (*pEASData->effectsModules[i].effect->pfShutdown)(pEASData, pEASData->effectsModules[i].effectData)) != EAS_SUCCESS)
            {
                { /* dpp: EAS_ReportEx(_EAS_SEVERITY_FATAL, "Shutdown of effects module %d returned %d\n", i, result); */ }
                if (reportResult == EAS_SUCCESS)
                    reportResult = result;
            }
        }
    }

    /* shutdown the voice manager & synthesizer */
    VMShutdown(pEASData);

#ifdef _METRICS_ENABLED
    /* shutdown the metrics module */
    if (pEASData->pMetricsModule != NULL)
    {
        if ((result = (*pEASData->pMetricsModule->pfShutdown)(pEASData, pEASData->pMetricsData)) != EAS_SUCCESS)
        {
            { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Error %ld shutting down metrics module\n", result); */ }
            if (reportResult == EAS_SUCCESS)
                reportResult = result;
        }
    }
#endif

    /* release allocated memory */
    if (!pEASData->staticMemoryModel)
        EAS_HWFree(hwInstData, pEASData);

    /* shutdown host wrappers */
    if (hwInstData)
    {
        if ((result = EAS_HWShutdown(hwInstData)) != EAS_SUCCESS)
        {
            { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Error %ld shutting down host wrappers\n", result); */ }
            if (reportResult == EAS_SUCCESS)
                reportResult = result;
        }
    }

    return reportResult;
}

#ifdef JET_INTERFACE
/*----------------------------------------------------------------------------
 * EAS_OpenJETStream()
 *----------------------------------------------------------------------------
 * Private interface for JET to open an SMF stream with an offset
 *----------------------------------------------------------------------------
*/
EAS_RESULT EAS_OpenJETStream (EAS_DATA_HANDLE pEASData, EAS_FILE_HANDLE fileHandle, EAS_I32 offset, EAS_HANDLE *ppStream)
{
    EAS_RESULT result;
    EAS_VOID_PTR streamHandle;
    S_FILE_PARSER_INTERFACE *pParserModule;
    EAS_INT streamNum;

    /* allocate a stream */
    if ((streamNum = EAS_AllocateStream(pEASData)) < 0)
        return EAS_ERROR_MAX_STREAMS_OPEN;

    /* check Configuration Module for SMF parser */
    *ppStream = NULL;
    streamHandle = NULL;
    pParserModule = (S_FILE_PARSER_INTERFACE *) EAS_CMEnumModules(0);
    if (pParserModule == NULL)
        return EAS_ERROR_UNRECOGNIZED_FORMAT;

    /* see if SMF parser recognizes the file */
    if ((result = (*pParserModule->pfCheckFileType)(pEASData, fileHandle, &streamHandle, offset)) != EAS_SUCCESS)
    {
        { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "CheckFileType returned error %ld\n", result); */ }
        return result;
    }

    /* parser recognized the file, return the handle */
    if (streamHandle)
    {
        EAS_InitStream(&pEASData->streams[streamNum], pParserModule, streamHandle);
        *ppStream = &pEASData->streams[streamNum];
        return EAS_SUCCESS;
    }

    return EAS_ERROR_UNRECOGNIZED_FORMAT;
}
#endif

/*----------------------------------------------------------------------------
 * EAS_OpenFile()
 *----------------------------------------------------------------------------
 * Purpose:
 * Opens a file for audio playback.
 *
 * Inputs:
 * pEASData         - pointer to overall EAS data structure
 * pHandle          - pointer to file handle
 *
 * Outputs:
 *
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
EAS_PUBLIC EAS_RESULT EAS_OpenFile (EAS_DATA_HANDLE pEASData, EAS_FILE_LOCATOR locator, EAS_HANDLE *ppStream)
{
    EAS_RESULT result;
    EAS_FILE_HANDLE fileHandle;
    EAS_VOID_PTR streamHandle;
    S_FILE_PARSER_INTERFACE *pParserModule;
    EAS_INT streamNum;
    EAS_INT moduleNum;

    /* open the file */
    if ((result = EAS_HWOpenFile(pEASData->hwInstData, locator, &fileHandle, EAS_FILE_READ)) != EAS_SUCCESS)
        return result;

    /* allocate a stream */
    if ((streamNum = EAS_AllocateStream(pEASData)) < 0)
    {
        /* Closing the opened file as stream allocation failed */
        EAS_HWCloseFile(pEASData->hwInstData, fileHandle);
        return EAS_ERROR_MAX_STREAMS_OPEN;
    }
    /* check Configuration Module for file parsers */
    pParserModule = NULL;
    *ppStream = NULL;
    streamHandle = NULL;
    for (moduleNum = 0; ; moduleNum++)
    {
        pParserModule = (S_FILE_PARSER_INTERFACE *) EAS_CMEnumModules(moduleNum);
        if (pParserModule == NULL)
            break;

        /* see if this parser recognizes it */
        if ((result = (*pParserModule->pfCheckFileType)(pEASData, fileHandle, &streamHandle, 0L)) != EAS_SUCCESS)
        {
            /* Closing the opened file as file type check failed */
            EAS_HWCloseFile(pEASData->hwInstData, fileHandle);

            { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "CheckFileType returned error %ld\n", result); */ }
            return result;
        }

        /* parser recognized the file, return the handle */
        if (streamHandle)
        {

            /* save the parser pointer and file handle */
            EAS_InitStream(&pEASData->streams[streamNum], pParserModule, streamHandle);
            *ppStream = &pEASData->streams[streamNum];
            return EAS_SUCCESS;
        }

        /* rewind the file for the next parser */
        if ((result = EAS_HWFileSeek(pEASData->hwInstData, fileHandle, 0L)) != EAS_SUCCESS)
        {
            /* Closing the opened file as file seek failed */
            EAS_HWCloseFile(pEASData->hwInstData, fileHandle);

            return result;
         }
    }

    /* no parser was able to recognize the file, close it and return an error */
    EAS_HWCloseFile(pEASData->hwInstData, fileHandle);
    { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "No parser recognized the requested file\n"); */ }
    return EAS_ERROR_UNRECOGNIZED_FORMAT;
}

/*----------------------------------------------------------------------------
 * EAS_Prepare()
 *----------------------------------------------------------------------------
 * Purpose:
 * Prepares the synthesizer to play the file or stream. Parses the first
 * frame of data from the file and arms the synthesizer.
 *
 * Inputs:
 * pEASData         - pointer to overall EAS data structure
 * handle           - file or stream handle
 *
 * Outputs:
 *
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
EAS_PUBLIC EAS_RESULT EAS_Prepare (EAS_DATA_HANDLE pEASData, EAS_HANDLE pStream)
{
    S_FILE_PARSER_INTERFACE *pParserModule;
    EAS_STATE state;
    EAS_RESULT result;

    pParserModule = (S_FILE_PARSER_INTERFACE*) pStream->pParserModule;
    if (pParserModule == NULL)
        return EAS_ERROR_FEATURE_NOT_AVAILABLE;

    /* check for valid state */
    result = pParserModule->pfState(pEASData, pStream->handle, &state);
    if (result == EAS_SUCCESS)
    {
        /* prepare the stream */
        if (state == EAS_STATE_OPEN)
        {
            pParserModule = (S_FILE_PARSER_INTERFACE*) pStream->pParserModule;
            result = (*pParserModule->pfPrepare)(pEASData, pStream->handle);

            /* set volume */
            if (result == EAS_SUCCESS)
                result = EAS_SetVolume(pEASData, pStream, pStream->volume);
        }
        else
            result = EAS_ERROR_NOT_VALID_IN_THIS_STATE;

    }

    return result;
}

/*----------------------------------------------------------------------------
 * EAS_Render()
 *----------------------------------------------------------------------------
 * Purpose:
 * Parse the Midi data and render PCM audio data.
 *
 * Inputs:
 *  pEASData        - buffer for internal EAS data
 *  pOut            - output buffer pointer
 *  nNumRequested   - requested num samples to generate
 *  pnNumGenerated  - actual number of samples generated
 *
 * Outputs:
 *  EAS_SUCCESS if PCM data was successfully rendered
 *
 *----------------------------------------------------------------------------
*/
EAS_PUBLIC EAS_RESULT EAS_Render (EAS_DATA_HANDLE pEASData, EAS_PCM *pOut, EAS_I32 numRequested, EAS_I32 *pNumGenerated)
{
    S_FILE_PARSER_INTERFACE *pParserModule;
    EAS_RESULT result;
    EAS_I32 voicesRendered;
    EAS_STATE parserState;
    EAS_INT streamNum;

    /* assume no samples generated and reset workload */
    *pNumGenerated = 0;
    VMInitWorkload(pEASData->pVoiceMgr);

    /* no support for other buffer sizes yet */
    if (numRequested != BUFFER_SIZE_IN_MONO_SAMPLES)
    {
        { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "This library supports only %ld samples in buffer, host requested %ld samples\n",
            (EAS_I32) BUFFER_SIZE_IN_MONO_SAMPLES, numRequested); */ }
        return EAS_BUFFER_SIZE_MISMATCH;
    }

#ifdef _METRICS_ENABLED
    /* start performance counter */
    if (pEASData->pMetricsData)
        (*pEASData->pMetricsModule->pfStartTimer)(pEASData->pMetricsData, EAS_PM_TOTAL_TIME);
#endif

    /* prep the frame buffer, do mix engine prep only if TRUE */
#ifdef _SPLIT_ARCHITECTURE
    if (VMStartFrame(pEASData))
        EAS_MixEnginePrep(pEASData, numRequested);
#else
    /* prep the mix engine */
    EAS_MixEnginePrep(pEASData, numRequested);
#endif

    /* save the output buffer pointer */
    pEASData->pOutputAudioBuffer = pOut;


#ifdef _METRICS_ENABLED
        /* start performance counter */
        if (pEASData->pMetricsData)
            (*pEASData->pMetricsModule->pfStartTimer)(pEASData->pMetricsData, EAS_PM_PARSE_TIME);
#endif

    /* if we haven't finished parsing from last time, do it now */
    /* need to parse another frame of events before we render again */
    for (streamNum = 0; streamNum < MAX_NUMBER_STREAMS; streamNum++)
    {
        /* clear the locate flag */
        pEASData->streams[streamNum].streamFlags &= ~STREAM_FLAGS_LOCATE;

        if (pEASData->streams[streamNum].pParserModule)
        {

            /* establish pointer to parser module */
            pParserModule = pEASData->streams[streamNum].pParserModule;

#ifdef JET_INTERFACE
            /* handle pause */
            if (pEASData->streams[streamNum].streamFlags & STREAM_FLAGS_PAUSE)
            {
                if (pParserModule->pfPause)
                    result = pParserModule->pfPause(pEASData, pEASData->streams[streamNum].handle);
                pEASData->streams[streamNum].streamFlags &= ~STREAM_FLAGS_PAUSE;
            }
#endif

            /* get current state */
            if ((result = (*pParserModule->pfState)(pEASData, pEASData->streams[streamNum].handle, &parserState)) != EAS_SUCCESS)
                return result;

#ifdef JET_INTERFACE
            /* handle resume */
            if (parserState == EAS_STATE_PAUSED)
            {
                if (pEASData->streams[streamNum].streamFlags & STREAM_FLAGS_RESUME)
                {
                    if (pParserModule->pfResume)
                        result = pParserModule->pfResume(pEASData, pEASData->streams[streamNum].handle);
                    pEASData->streams[streamNum].streamFlags &= ~STREAM_FLAGS_RESUME;
                }
            }
#endif

            /* if necessary, parse stream */
            if ((pEASData->streams[streamNum].streamFlags & STREAM_FLAGS_PARSED) == 0)
                if ((result = EAS_ParseEvents(pEASData, &pEASData->streams[streamNum], pEASData->streams[streamNum].time + pEASData->streams[streamNum].frameLength, eParserModePlay)) != EAS_SUCCESS)
                    return result;

            /* check for an early abort */
            if ((pEASData->streams[streamNum].streamFlags) == 0)
            {

#ifdef _METRICS_ENABLED
                /* stop performance counter */
                if (pEASData->pMetricsData)
                    (*pEASData->pMetricsModule->pfStartTimer)(pEASData->pMetricsData, EAS_PM_TOTAL_TIME);
#endif

                return EAS_SUCCESS;
            }

            /* check for repeat */
            if (pEASData->streams[streamNum].repeatCount)
            {

                /* check for stopped state */
                if ((result = (*pParserModule->pfState)(pEASData, pEASData->streams[streamNum].handle, &parserState)) != EAS_SUCCESS)
                    return result;
                if (parserState == EAS_STATE_STOPPED)
                {

                    /* decrement repeat count, unless it is negative */
                    if (pEASData->streams[streamNum].repeatCount > 0)
                        pEASData->streams[streamNum].repeatCount--;

                    /* reset the parser */
                    if ((result = (*pParserModule->pfReset)(pEASData, pEASData->streams[streamNum].handle)) != EAS_SUCCESS)
                        return result;
                    pEASData->streams[streamNum].time = 0;
                }
            }
        }
    }

#ifdef _METRICS_ENABLED
    /* stop performance counter */
    if (pEASData->pMetricsData)
        (void)(*pEASData->pMetricsModule->pfStopTimer)(pEASData->pMetricsData, EAS_PM_PARSE_TIME);
#endif

#ifdef _METRICS_ENABLED
    /* start the render timer */
    if (pEASData->pMetricsData)
        (*pEASData->pMetricsModule->pfStartTimer)(pEASData->pMetricsData, EAS_PM_RENDER_TIME);
#endif

    /* render audio */
    if ((result = VMRender(pEASData->pVoiceMgr, BUFFER_SIZE_IN_MONO_SAMPLES, pEASData->pMixBuffer, &voicesRendered)) != EAS_SUCCESS)
    {
        { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "pfRender function returned error %ld\n", result); */ }
        return result;
    }

#ifdef _METRICS_ENABLED
    /* stop the render timer */
    if (pEASData->pMetricsData) {
        (*pEASData->pMetricsModule->pfIncrementCounter)(pEASData->pMetricsData, EAS_PM_FRAME_COUNT, 1);
        (void)(*pEASData->pMetricsModule->pfStopTimer)(pEASData->pMetricsData, EAS_PM_RENDER_TIME);
        (*pEASData->pMetricsModule->pfIncrementCounter)(pEASData->pMetricsData, EAS_PM_TOTAL_VOICE_COUNT, (EAS_U32) voicesRendered);
        (void)(*pEASData->pMetricsModule->pfRecordMaxValue)(pEASData->pMetricsData, EAS_PM_MAX_VOICES, (EAS_U32) voicesRendered);
    }
#endif

    //2 Do we really need frameParsed?
    /* need to parse another frame of events before we render again */
    for (streamNum = 0; streamNum < MAX_NUMBER_STREAMS; streamNum++)
        if (pEASData->streams[streamNum].pParserModule != NULL)
            pEASData->streams[streamNum].streamFlags &= ~STREAM_FLAGS_PARSED;

#ifdef _METRICS_ENABLED
    /* start performance counter */
    if (pEASData->pMetricsData)
        (*pEASData->pMetricsModule->pfStartTimer)(pEASData->pMetricsData, EAS_PM_STREAM_TIME);
#endif

#ifdef _METRICS_ENABLED
    /* stop the stream timer */
    if (pEASData->pMetricsData)
        (void)(*pEASData->pMetricsModule->pfStopTimer)(pEASData->pMetricsData, EAS_PM_STREAM_TIME);
#endif

#ifdef _METRICS_ENABLED
    /* start the post timer */
    if (pEASData->pMetricsData)
        (*pEASData->pMetricsModule->pfStartTimer)(pEASData->pMetricsData, EAS_PM_POST_TIME);
#endif

    /* for split architecture, send DSP vectors.  Do post only if return is TRUE */
#ifdef _SPLIT_ARCHITECTURE
    if (VMEndFrame(pEASData))
    {
        /* now do post-processing */
        EAS_MixEnginePost(pEASData, numRequested);
        *pNumGenerated = numRequested;
    }
#else
    /* now do post-processing */
    EAS_MixEnginePost(pEASData, numRequested);
    *pNumGenerated = numRequested;
#endif

#ifdef _METRICS_ENABLED
    /* stop the post timer */
    if (pEASData->pMetricsData)
        (void)(*pEASData->pMetricsModule->pfStopTimer)(pEASData->pMetricsData, EAS_PM_POST_TIME);
#endif

    /* advance render time */
    pEASData->renderTime += AUDIO_FRAME_LENGTH;

#if 0
    /* dump workload for debug */
    if (pEASData->pVoiceMgr->workload)
        { /* dpp: EAS_ReportEx(_EAS_SEVERITY_DETAIL, "Workload = %d\n", pEASData->pVoiceMgr->workload); */ }
#endif

#ifdef _METRICS_ENABLED
    /* stop performance counter */
    if (pEASData->pMetricsData)
    {
        PERF_TIMER temp;
        temp = (*pEASData->pMetricsModule->pfStopTimer)(pEASData->pMetricsData, EAS_PM_TOTAL_TIME);

        /* if max render time, record the number of voices and time */
        if ((*pEASData->pMetricsModule->pfRecordMaxValue)
            (pEASData->pMetricsData, EAS_PM_MAX_CYCLES, (EAS_U32) temp))
        {
            (*pEASData->pMetricsModule->pfRecordValue)(pEASData->pMetricsData, EAS_PM_MAX_CYCLES_VOICES, (EAS_U32) voicesRendered);
            (*pEASData->pMetricsModule->pfRecordValue)(pEASData->pMetricsData, EAS_PM_MAX_CYCLES_TIME, (EAS_I32) (pEASData->renderTime >> 8));
        }
    }
#endif

#ifdef JET_INTERFACE
    /* let JET to do its thing */
    if (pEASData->jetHandle != NULL)
    {
        result = JET_Process(pEASData);
        if (result != EAS_SUCCESS)
            return result;
    }
#endif

    return EAS_SUCCESS;
}

#ifdef JET_INTERFACE
/*----------------------------------------------------------------------------
 * EAS_SetTransposition)
 *----------------------------------------------------------------------------
 * Purpose:
 * Sets the key tranposition for the synthesizer. Transposes all
 * melodic instruments by the specified amount. Range is limited
 * to +/-12 semitones.
 *
 * Inputs:
 *  pEASData        - handle to data for this instance
 *  handle          - handle to stream
 *  transposition   - +/-12 semitones
 *
 * Outputs:
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
EAS_PUBLIC EAS_RESULT EAS_SetTransposition (EAS_DATA_HANDLE pEASData, EAS_HANDLE pStream, EAS_I32 transposition)
{

    /* check range */
    if ((transposition < -12) || (transposition > 12))
        return EAS_ERROR_INVALID_PARAMETER;

    if (!EAS_StreamReady(pEASData, pStream))
        return EAS_ERROR_NOT_VALID_IN_THIS_STATE;
    return EAS_IntSetStrmParam(pEASData, pStream, PARSER_DATA_TRANSPOSITION, transposition);
}
#endif

/*----------------------------------------------------------------------------
 * EAS_ParseEvents()
 *----------------------------------------------------------------------------
 * Purpose:
 * Parse events in the current streams until the desired time is reached.
 *
 * Inputs:
 *  pEASData        - buffer for internal EAS data
 *  endTime         - stop parsing if this time is reached
 *  parseMode       - play, locate, or metadata
 *
 * Outputs:
 *  EAS_SUCCESS if PCM data was successfully rendered
 *
 *----------------------------------------------------------------------------
*/
static EAS_RESULT EAS_ParseEvents (S_EAS_DATA *pEASData, EAS_HANDLE pStream, EAS_U32 endTime, EAS_INT parseMode)
{
    S_FILE_PARSER_INTERFACE *pParserModule;
    EAS_RESULT result;
    EAS_I32 parserState;
    EAS_BOOL done;
    EAS_INT yieldCount = YIELD_EVENT_COUNT;
    EAS_U32 time = 0;

    // This constant is the maximum number of events that can be processed in a single time slice.
    // A typical ringtone will contain a few events per time slice.
    // Extremely dense ringtones might go up to 50 events.
    // If we see this many events then the file is probably stuck in an infinite loop
    // and should be aborted.
    static const EAS_INT MAX_EVENT_COUNT = 100000;
    EAS_INT eventCount = 0;

    /* does this parser have a time function? */
    pParserModule = pStream->pParserModule;
    if (pParserModule->pfTime == NULL)
    {
        /* check state */
        if ((result = (*pParserModule->pfState)(pEASData, pStream->handle, &parserState)) != EAS_SUCCESS)
            return result;
        /* if play state, advance time */
        if ((parserState >= EAS_STATE_READY) && (parserState <= EAS_STATE_PAUSING))
            pStream->time += pStream->frameLength;
        done = EAS_TRUE;
    }

    /* assume we're not done, in case we abort out */
    else
    {
        pStream->streamFlags &= ~STREAM_FLAGS_PARSED;
        done = EAS_FALSE;
    }

    while (!done)
    {

        /* check for stopped state */
        if ((result = (*pParserModule->pfState)(pEASData, pStream->handle, &parserState)) != EAS_SUCCESS)
            return result;
        if (parserState > EAS_STATE_PLAY)
        {
            /* save current time if we're not in play mode */
            if (parseMode != eParserModePlay)
                pStream->time = time << 8;
            done = EAS_TRUE;
            break;
        }

        /* get the next event time */
        if (pParserModule->pfTime)
        {
            if ((result = (*pParserModule->pfTime)(pEASData, pStream->handle, &time)) != EAS_SUCCESS)
                return result;

            /* if next event is within this frame, parse it */
            if (time < (endTime >> 8))
            {

                /* parse the next event */
                if (pParserModule->pfEvent) {
                    if ((result = (*pParserModule->pfEvent)(pEASData, pStream->handle, parseMode))
                            != EAS_SUCCESS) {
                        ALOGE("%s() pfEvent returned %ld", __func__, result);
                        return result;
                    }
                }

                // An infinite loop within a ringtone file can cause this function
                // to loop forever.  Try to detect that and return an error.
                // Only check when playing. Otherwise a very large file could be rejected
                // when scanning the entire file in a single call to this function.
                // OTA files will only do infinite loops when in eParserModePlay.
                if (++eventCount >= MAX_EVENT_COUNT && parseMode == eParserModePlay) {
                    ALOGE("%s() aborting, %d events. Infinite loop in song file?!",
                            __func__, eventCount);
                    android_errorWriteLog(0x534e4554, "68664359");
                    return EAS_ERROR_FILE_POS;
                }
            }

            /* no more events in this frame, advance time */
            else
            {
                pStream->time = endTime;
                done = EAS_TRUE;
            }
        }

        /* check for max workload exceeded */
        if (VMCheckWorkload(pEASData->pVoiceMgr))
        {
            /* stop even though we may not have parsed
             * all the events in this frame. The parser will try to
             * catch up on the next frame.
             */
            break;
        }

        /* give host a chance for an early abort */
        if (--yieldCount == 0)
        {
            if (EAS_HWYield(pEASData->hwInstData))
                break;
            yieldCount = YIELD_EVENT_COUNT;
        }
    }

    /* if no early abort, parsing is complete for this frame */
    if (done)
        pStream->streamFlags |= STREAM_FLAGS_PARSED;

    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 * EAS_ParseMetaData()
 *----------------------------------------------------------------------------
 * Purpose:
 *
 *
 * Inputs:
 * pEASData         - pointer to overall EAS data structure
 * handle           - file or stream handle
 * playLength       - pointer to variable to store the play length (in msecs)
 *
 * Outputs:
 *
 *
 * Side Effects:
 *                  - resets the parser to the start of the file
 *----------------------------------------------------------------------------
*/
EAS_PUBLIC EAS_RESULT EAS_ParseMetaData (EAS_DATA_HANDLE pEASData, EAS_HANDLE pStream, EAS_I32 *playLength)
{
    S_FILE_PARSER_INTERFACE *pParserModule;
    EAS_RESULT result;
    EAS_STATE state;

    pParserModule = (S_FILE_PARSER_INTERFACE*) pStream->pParserModule;
    if (pParserModule == NULL)
        return EAS_ERROR_FEATURE_NOT_AVAILABLE;

    /* check parser state */
    if ((result = (*pParserModule->pfState)(pEASData, pStream->handle, &state)) != EAS_SUCCESS)
        return result;
    if (state >= EAS_STATE_OPEN)
        return EAS_ERROR_NOT_VALID_IN_THIS_STATE;

    /* if parser has metadata function, use that */
    if (pParserModule->pfGetMetaData != NULL)
        return pParserModule->pfGetMetaData(pEASData, pStream->handle, playLength);

    /* reset the parser to the beginning */
    if ((result = (*pParserModule->pfReset)(pEASData, pStream->handle)) != EAS_SUCCESS)
        return result;

    /* parse the file to end */
    pStream->time = 0;
    VMInitWorkload(pEASData->pVoiceMgr);
    if ((result = EAS_ParseEvents(pEASData, pStream, 0x7fffffff, eParserModeMetaData)) != EAS_SUCCESS)
        return result;

    /* get the parser time */
    if ((result = EAS_GetLocation(pEASData, pStream, playLength)) != EAS_SUCCESS)
        return result;

    /* reset the parser to the beginning */
    pStream->time = 0;
    return (*pParserModule->pfReset)(pEASData, pStream->handle);
}

/*----------------------------------------------------------------------------
 * EAS_CloseFile()
 *----------------------------------------------------------------------------
 * Purpose:
 * Closes an audio file or stream. Playback should have either paused or
 * completed (EAS_State returns EAS_PAUSED or EAS_STOPPED).
 *
 * Inputs:
 * pEASData         - pointer to overall EAS data structure
 * handle           - file or stream handle
 *
 * Outputs:
 *
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
EAS_PUBLIC EAS_RESULT EAS_CloseFile (EAS_DATA_HANDLE pEASData, EAS_HANDLE pStream)
{
    S_FILE_PARSER_INTERFACE *pParserModule;
    EAS_RESULT result;

    /* call the close function */
    pParserModule = (S_FILE_PARSER_INTERFACE*) pStream->pParserModule;
    if (pParserModule == NULL)
        return EAS_ERROR_FEATURE_NOT_AVAILABLE;

    result = (*pParserModule->pfClose)(pEASData, pStream->handle);

    /* clear the handle and parser interface pointer */
    pStream->handle = NULL;
    pStream->pParserModule = NULL;
    return result;
}

/*----------------------------------------------------------------------------
 * EAS_State()
 *----------------------------------------------------------------------------
 * Purpose:
 * Returns the state of an audio file or stream.
 *
 * Inputs:
 * pEASData         - pointer to overall EAS data structure
 * handle           - file or stream handle
 *
 * Outputs:
 *
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
EAS_PUBLIC EAS_RESULT EAS_State (EAS_DATA_HANDLE pEASData, EAS_HANDLE pStream, EAS_STATE *pState)
{
    S_FILE_PARSER_INTERFACE *pParserModule;
    EAS_RESULT result;

    /* call the parser to return state */
    pParserModule = (S_FILE_PARSER_INTERFACE*) pStream->pParserModule;
    if (pParserModule == NULL)
        return EAS_ERROR_FEATURE_NOT_AVAILABLE;

    if ((result = (*pParserModule->pfState)(pEASData, pStream->handle, pState)) != EAS_SUCCESS)
        return result;

    /* if repeat count is set for this parser, mask the stopped state from the application */
    if (pStream->repeatCount && (*pState == EAS_STATE_STOPPED))
        *pState = EAS_STATE_PLAY;

    /* if we're not paused or pausing, we don't need to hide state from host */
    if (*pState != EAS_STATE_PAUSED && *pState != EAS_STATE_PAUSING)
        return EAS_SUCCESS;

    /* if stream is about to be paused, report it as paused */
    if (pStream->streamFlags & STREAM_FLAGS_PAUSE)
    {
        if (pStream->streamFlags & STREAM_FLAGS_LOCATE)
            *pState = EAS_STATE_PAUSED;
        else
            *pState = EAS_STATE_PAUSING;
    }

    /* if stream is about to resume, report it as playing */
    if (pStream->streamFlags & STREAM_FLAGS_RESUME)
        *pState = EAS_STATE_PLAY;

    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 * EAS_SetVolume()
 *----------------------------------------------------------------------------
 * Purpose:
 * Set the master gain for the mix engine in 1dB increments
 *
 * Inputs:
 * pEASData         - pointer to overall EAS data structure
 * volume           - the desired master gain (100 is max)
 * handle           - file or stream handle
 *
 * Outputs:
 *
 *
 * Side Effects:
 * overrides any previously set master volume from sysex
 *
 *----------------------------------------------------------------------------
*/
EAS_PUBLIC EAS_RESULT EAS_SetVolume (EAS_DATA_HANDLE pEASData, EAS_HANDLE pStream, EAS_I32 volume)
{
    EAS_I16 gain;

    /* check range */
    if ((volume < 0) || (volume > EAS_MAX_VOLUME))
        return EAS_ERROR_PARAMETER_RANGE;

    /* stream volume */
    if (pStream != NULL)
    {
        EAS_I32 gainOffset;
        EAS_RESULT result;

        if (!EAS_StreamReady(pEASData, pStream))
            return EAS_ERROR_NOT_VALID_IN_THIS_STATE;

        /* get gain offset */
        pStream->volume = (EAS_U8) volume;
        result = EAS_GetStreamParameter(pEASData, pStream, PARSER_DATA_GAIN_OFFSET, &gainOffset);
        if (result == EAS_SUCCESS)
            volume += gainOffset;

        /* set stream volume */
        gain = EAS_VolumeToGain(volume - STREAM_VOLUME_HEADROOM);

        /* convert to linear scalar */
        return EAS_IntSetStrmParam(pEASData, pStream, PARSER_DATA_VOLUME, gain);
    }

    /* master volume */
    pEASData->masterVolume = (EAS_U8) volume;
#if (NUM_OUTPUT_CHANNELS == 1)
    /* leave 3dB headroom for mono output */
    volume -= 3;
#endif

    gain = EAS_VolumeToGain(volume - STREAM_VOLUME_HEADROOM);
    pEASData->masterGain = gain;
    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 * EAS_Locate()
 *----------------------------------------------------------------------------
 * Purpose:
 * Locate into the file associated with the handle.
 *
 * Inputs:
 * pEASData - pointer to overall EAS data structure
 * handle           - file handle
 * milliseconds     - playback offset from start of file in milliseconds
 *
 * Outputs:
 *
 *
 * Side Effects:
 * the actual offset will be quantized to the closest update period, typically
 * a resolution of 5.9ms. Notes that are started prior to this time will not
 * sound. Any notes currently playing will be shut off.
 *
 *----------------------------------------------------------------------------
*/
EAS_PUBLIC EAS_RESULT EAS_Locate (EAS_DATA_HANDLE pEASData, EAS_HANDLE pStream, EAS_I32 milliseconds, EAS_BOOL offset)
{
    S_FILE_PARSER_INTERFACE *pParserModule;
    EAS_RESULT result;
    EAS_U32 requestedTime;
    EAS_STATE state;

    /* get pointer to parser function table */
    pParserModule = (S_FILE_PARSER_INTERFACE*) pStream->pParserModule;
    if (pParserModule == NULL)
        return EAS_ERROR_FEATURE_NOT_AVAILABLE;

    if ((result = (*pParserModule->pfState)(pEASData, pStream->handle, &state)) != EAS_SUCCESS)
        return result;
    if (state >= EAS_STATE_OPEN)
        return EAS_ERROR_NOT_VALID_IN_THIS_STATE;

    /* handle offset and limit to start of file */
    /*lint -e{704} use shift for performance*/
    if (offset)
        milliseconds += (EAS_I32) pStream->time >> 8;
    if (milliseconds < 0)
        milliseconds = 0;

    /* check to see if the request is different from the current time */
    requestedTime = (EAS_U32) milliseconds;
    if (requestedTime == (pStream->time >> 8))
        return EAS_SUCCESS;

    /* set the locate flag */
    pStream->streamFlags |= STREAM_FLAGS_LOCATE;

    /* use the parser locate function, if available */
    if (pParserModule->pfLocate != NULL)
    {
        EAS_BOOL parserLocate = EAS_FALSE;
        result = pParserModule->pfLocate(pEASData, pStream->handle, (EAS_I32) requestedTime, &parserLocate);
        if (!parserLocate)
        {
            if (result == EAS_SUCCESS)
                pStream->time = requestedTime << 8;
            return result;
        }
    }

    /* if we were paused and not going to resume, set pause request flag */
    if (((state == EAS_STATE_PAUSING) || (state == EAS_STATE_PAUSED)) && ((pStream->streamFlags & STREAM_FLAGS_RESUME) == 0))
        pStream->streamFlags |= STREAM_FLAGS_PAUSE;

    /* reset the synth and parser */
    if ((result = (*pParserModule->pfReset)(pEASData, pStream->handle)) != EAS_SUCCESS)
        return result;
    pStream->time = 0;

    /* locating forward, clear parsed flag and parse data until we get to the requested location */
    if ((result = EAS_ParseEvents(pEASData, pStream, requestedTime << 8, eParserModeLocate)) != EAS_SUCCESS)
        return result;

    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 * EAS_GetLocation()
 *----------------------------------------------------------------------------
 * Purpose:
 * Returns the current playback offset
 *
 * Inputs:
 * pEASData         - pointer to overall EAS data structure
 * handle           - file handle
 *
 * Outputs:
 * The offset in milliseconds from the start of the current sequence, quantized
 * to the nearest update period. Actual resolution is typically 5.9 ms.
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, pEASData) reserved for future use */
EAS_PUBLIC EAS_RESULT EAS_GetLocation (EAS_DATA_HANDLE pEASData, EAS_HANDLE pStream, EAS_I32 *pTime)
{
    if (!EAS_StreamReady(pEASData, pStream))
        return EAS_ERROR_NOT_VALID_IN_THIS_STATE;

    *pTime = pStream->time >> 8;
    return EAS_SUCCESS;
}

#ifdef JET_INTERFACE
/*----------------------------------------------------------------------------
 * EAS_Pause()
 *----------------------------------------------------------------------------
 * Purpose:
 * Pauses the playback of the data associated with this handle. The audio
 * is gracefully ramped down to prevent clicks and pops. It may take several
 * buffers of audio before the audio is muted.
 *
 * Inputs:
 * psEASData        - pointer to overall EAS data structure
 * handle           - file or stream handle
 *
 * Outputs:
 *
 *
 * Side Effects:
 *
 *
 *----------------------------------------------------------------------------
*/
EAS_PUBLIC EAS_RESULT EAS_Pause (EAS_DATA_HANDLE pEASData, EAS_HANDLE pStream)
{
    S_FILE_PARSER_INTERFACE *pParserModule;
    EAS_STATE state;
    EAS_RESULT result;

    pParserModule = (S_FILE_PARSER_INTERFACE*) pStream->pParserModule;
    if (pParserModule == NULL)
        return EAS_ERROR_FEATURE_NOT_AVAILABLE;

    /* check for valid state */
    result = pParserModule->pfState(pEASData, pStream->handle, &state);
    if (result == EAS_SUCCESS)
    {
        if ((state != EAS_STATE_PLAY) && (state != EAS_STATE_READY) && ((pStream->streamFlags & STREAM_FLAGS_RESUME) == 0))
            return EAS_ERROR_NOT_VALID_IN_THIS_STATE;

        /* make sure parser implements pause */
        if (pParserModule->pfPause == NULL)
            result = EAS_ERROR_NOT_IMPLEMENTED;

        /* clear resume flag */
        pStream->streamFlags &= ~STREAM_FLAGS_RESUME;

        /* set pause flag */
        pStream->streamFlags |= STREAM_FLAGS_PAUSE;

#if 0
        /* pause the stream */
        if (pParserModule->pfPause)
            result = pParserModule->pfPause(pEASData, pStream->handle);
        else
            result = EAS_ERROR_NOT_IMPLEMENTED;
#endif
    }

    return result;
}

/*----------------------------------------------------------------------------
 * EAS_Resume()
 *----------------------------------------------------------------------------
 * Purpose:
 * Resumes the playback of the data associated with this handle. The audio
 * is gracefully ramped up to prevent clicks and pops.
 *
 * Inputs:
 * psEASData        - pointer to overall EAS data structure
 * handle           - file or stream handle
 *
 * Outputs:
 *
 *
 * Side Effects:
 *
 *
 *----------------------------------------------------------------------------
*/
EAS_PUBLIC EAS_RESULT EAS_Resume (EAS_DATA_HANDLE pEASData, EAS_HANDLE pStream)
{
    S_FILE_PARSER_INTERFACE *pParserModule;
    EAS_STATE state;
    EAS_RESULT result;

    pParserModule = (S_FILE_PARSER_INTERFACE*) pStream->pParserModule;
    if (pParserModule == NULL)
        return EAS_ERROR_FEATURE_NOT_AVAILABLE;

    /* check for valid state */
    result = pParserModule->pfState(pEASData, pStream->handle, &state);
    if (result == EAS_SUCCESS)
    {
        if ((state != EAS_STATE_PAUSED) && (state != EAS_STATE_PAUSING) && ((pStream->streamFlags & STREAM_FLAGS_PAUSE) == 0))
            return EAS_ERROR_NOT_VALID_IN_THIS_STATE;

        /* make sure parser implements this function */
        if (pParserModule->pfResume == NULL)
            result = EAS_ERROR_NOT_IMPLEMENTED;

        /* clear pause flag */
        pStream->streamFlags &= ~STREAM_FLAGS_PAUSE;

        /* set resume flag */
        pStream->streamFlags |= STREAM_FLAGS_RESUME;

#if 0
        /* resume the stream */
        if (pParserModule->pfResume)
            result = pParserModule->pfResume(pEASData, pStream->handle);
        else
            result = EAS_ERROR_NOT_IMPLEMENTED;
#endif
    }

    return result;
}
#endif

/*----------------------------------------------------------------------------
 * EAS_SetParameter()
 *----------------------------------------------------------------------------
 * Purpose:
 * Set the parameter of a module. See E_MODULES for a list of modules
 * and the header files of the modules for a list of parameters.
 *
 * Inputs:
 * psEASData        - pointer to overall EAS data structure
 * handle           - file or stream handle
 * module           - enumerated module number
 * param            - enumerated parameter number
 * value            - new parameter value
 *
 * Outputs:
 *
 *
 * Side Effects:
 *
 *
 *----------------------------------------------------------------------------
*/
EAS_PUBLIC EAS_RESULT EAS_SetParameter (EAS_DATA_HANDLE pEASData, EAS_I32 module, EAS_I32 param, EAS_I32 value)
{

    if (module >= NUM_EFFECTS_MODULES)
        return EAS_ERROR_INVALID_MODULE;

    if (pEASData->effectsModules[module].effectData == NULL)
        return EAS_ERROR_INVALID_MODULE;

    return (*pEASData->effectsModules[module].effect->pFSetParam)
        (pEASData->effectsModules[module].effectData, param, value);
}

#ifdef _METRICS_ENABLED
/*----------------------------------------------------------------------------
 * EAS_MetricsReport()
 *----------------------------------------------------------------------------
 * Purpose:
 * Displays the current metrics through the metrics interface.
 *
 * Inputs:
 * p                - instance data handle
 *
 * Outputs:
 *
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
EAS_PUBLIC EAS_RESULT EAS_MetricsReport (EAS_DATA_HANDLE pEASData)
{
    if (!pEASData->pMetricsModule)
        return EAS_ERROR_INVALID_MODULE;

    return (*pEASData->pMetricsModule->pfReport)(pEASData->pMetricsData);
}

/*----------------------------------------------------------------------------
 * EAS_MetricsReset()
 *----------------------------------------------------------------------------
 * Purpose:
 * Resets the metrics.
 *
 * Inputs:
 * p                - instance data handle
 *
 * Outputs:
 *
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
EAS_PUBLIC EAS_RESULT EAS_MetricsReset (EAS_DATA_HANDLE pEASData)
{

    if (!pEASData->pMetricsModule)
        return EAS_ERROR_INVALID_MODULE;

    return (*pEASData->pMetricsModule->pfReset)(pEASData->pMetricsData);
}
#endif

#ifdef FILE_HEADER_SEARCH
/*----------------------------------------------------------------------------
 * EAS_SearchFile
 *----------------------------------------------------------------------------
 * Search file for specific sequence starting at current file
 * position. Returns offset to start of sequence.
 *
 * Inputs:
 * pEASData         - pointer to EAS persistent data object
 * fileHandle       - file handle
 * searchString     - pointer to search sequence
 * len              - length of search sequence
 * pOffset          - pointer to variable to store offset to sequence
 *
 * Returns EAS_EOF if end-of-file is reached
 *----------------------------------------------------------------------------
*/
EAS_RESULT EAS_SearchFile (S_EAS_DATA *pEASData, EAS_FILE_HANDLE fileHandle, const EAS_U8 *searchString, EAS_I32 len, EAS_I32 *pOffset)
{
    EAS_RESULT result;
    EAS_INT index;
    EAS_U8 c;

    *pOffset = -1;
    index = 0;
    for (;;)
    {
        result = EAS_HWGetByte(pEASData->hwInstData, fileHandle, &c);
        if (result != EAS_SUCCESS)
            return result;
        if (c == searchString[index])
        {
            index++;
            if (index == 4)
            {
                result = EAS_HWFilePos(pEASData->hwInstData, fileHandle, pOffset);
                if (result != EAS_SUCCESS)
                    return result;
                *pOffset -= len;
                break;
            }
        }
        else
            index = 0;
    }
    return EAS_SUCCESS;
}
#endif