summaryrefslogtreecommitdiffstats
path: root/arm-wt-22k/lib_src/eas_xmf.c
blob: 0a924250494d6d1e0776d8ca45a4f6e002b9fdfd (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
/*----------------------------------------------------------------------------
 *
 * File:
 * eas_xmf.c
 *  5
 * Contents and purpose:
 * XMF File 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: 501 $
 *   $Date: 2006-12-11 17:53:36 -0800 (Mon, 11 Dec 2006) $
 *----------------------------------------------------------------------------
*/

#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_xmf.h"
#include "eas_xmfdata.h"
#include "eas_config.h"
#include "eas_vm_protos.h"
#include "eas_mdls.h"
#include "eas_smf.h"


/* XMF header file type */
#define XMF_IDENTIFIER          0x584d465f
#define XMF_VERSION_2_00        0x322e3030
#define XMF_FILE_TYPE           0x00000002
#define XMF_SPEC_LEVEL          0x00000001
#define XMF_RIFF_CHUNK          0x52494646
#define XMF_RIFF_DLS            0x444c5320
#define XMF_SMF_CHUNK           0x4d546864

/* local prototypes */
static EAS_RESULT XMF_CheckFileType (S_EAS_DATA *pEASData, EAS_FILE_HANDLE fileHandle, EAS_VOID_PTR *ppHandle, EAS_I32 offset);
static EAS_RESULT XMF_Prepare (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData);
static EAS_RESULT XMF_Time (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_U32 *pTime);
static EAS_RESULT XMF_Event (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_INT parserMode);
static EAS_RESULT XMF_State (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_STATE *pState);
static EAS_RESULT XMF_Close (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData);
static EAS_RESULT XMF_Reset (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData);
static EAS_RESULT XMF_Pause (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData);
static EAS_RESULT XMF_Resume (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData);
static EAS_RESULT XMF_SetData (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_I32 param, EAS_I32 value);
static EAS_RESULT XMF_GetData (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_I32 param, EAS_I32 *pValue);
static EAS_RESULT XMF_FindFileContents (EAS_HW_DATA_HANDLE hwInstData, S_XMF_DATA *pXMFData);
static EAS_RESULT XMF_ReadNode (EAS_HW_DATA_HANDLE hwInstData, S_XMF_DATA *pXMFData, EAS_I32 nodeOffset, EAS_I32 *pLength);
static EAS_RESULT XMF_ReadVLQ (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE fileHandle, EAS_I32 *value);


/*----------------------------------------------------------------------------
 *
 * XMF_Parser
 *
 * This structure contains the functional interface for the XMF parser
 *----------------------------------------------------------------------------
*/
const S_FILE_PARSER_INTERFACE EAS_XMF_Parser =
{
    XMF_CheckFileType,
    XMF_Prepare,
    XMF_Time,
    XMF_Event,
    XMF_State,
    XMF_Close,
    XMF_Reset,
    XMF_Pause,
    XMF_Resume,
    NULL,
    XMF_SetData,
    XMF_GetData,
    NULL
};

/*----------------------------------------------------------------------------
 * XMF_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 XMF_CheckFileType (S_EAS_DATA *pEASData, EAS_FILE_HANDLE fileHandle, EAS_VOID_PTR *ppHandle, EAS_I32 offset)
{
    S_XMF_DATA *pXMFData;
    EAS_RESULT result;
    EAS_U32 temp;

    /* assume we don't recognize it initially */
    *ppHandle = NULL;

    /* read the file identifier */
    if ((result = EAS_HWGetDWord(pEASData->hwInstData, fileHandle, &temp, EAS_TRUE))  != EAS_SUCCESS)
        return result;
    if (temp != XMF_IDENTIFIER)
        return EAS_SUCCESS;

    /* read the version */
    if ((result = EAS_HWGetDWord(pEASData->hwInstData, fileHandle, &temp, EAS_TRUE))  != EAS_SUCCESS)
        return result;
    if (temp != XMF_VERSION_2_00)
    {
        { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "XMF file version was 0x%08x, expected 0x%08x\n", temp, XMF_VERSION_2_00); */ }
        return EAS_SUCCESS;
    }

    /* read the file type */
    if ((result = EAS_HWGetDWord(pEASData->hwInstData, fileHandle, &temp, EAS_TRUE))  != EAS_SUCCESS)
        return result;
    if (temp != XMF_FILE_TYPE)
    {
        { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "XMF file type was 0x%08x, expected 0x%08x\n", temp, XMF_FILE_TYPE); */ }
        return EAS_SUCCESS;
    }

    /* read the spec level */
    if ((result = EAS_HWGetDWord(pEASData->hwInstData, fileHandle, &temp, EAS_TRUE))  != EAS_SUCCESS)
        return result;
    if (temp != XMF_SPEC_LEVEL)
    {
        { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "XMF file spec was 0x%08x, expected 0x%08x\n", temp, XMF_SPEC_LEVEL); */ }
        return EAS_SUCCESS;
    }

    /* check for static memory allocation */
    if (pEASData->staticMemoryModel)
        pXMFData = EAS_CMEnumData(EAS_CM_XMF_DATA);
    else
        pXMFData = EAS_HWMalloc(pEASData->hwInstData, sizeof(S_XMF_DATA));
    if (!pXMFData)
        return EAS_ERROR_MALLOC_FAILED;

    /* zero the memory to insure complete initialization */
    EAS_HWMemSet((void *)pXMFData,0, sizeof(S_XMF_DATA));

    pXMFData->fileHandle = fileHandle;
    pXMFData->fileOffset = offset;
    *ppHandle = pXMFData;

    /* locate the SMF and DLS contents */
    if ((result = XMF_FindFileContents(pEASData->hwInstData, pXMFData)) != EAS_SUCCESS)
    {
        { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "No SMF data found in XMF file\n"); */ }
        return result;
    }

    /* let the SMF parser take over */
    if ((result = EAS_HWFileSeek(pEASData->hwInstData, fileHandle, pXMFData->midiOffset)) != EAS_SUCCESS)
        return result;
    return SMF_CheckFileType(pEASData, fileHandle, &pXMFData->pSMFData, pXMFData->midiOffset);
}

/*----------------------------------------------------------------------------
 * XMF_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 XMF_Prepare (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData)
{
    S_XMF_DATA* pXMFData;
    EAS_RESULT result;

    /* parse DLS collection */
    pXMFData = (S_XMF_DATA*) pInstData;
    if (pXMFData->dlsOffset != 0)
    {
        if ((result = DLSParser(pEASData->hwInstData, pXMFData->fileHandle, pXMFData->dlsOffset, &pXMFData->pDLS)) != EAS_SUCCESS)
        {
            { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "Error converting XMF DLS data\n"); */ }
            return result;
        }
    }

    /* Prepare the SMF parser */
    if ((result = SMF_Prepare(pEASData, pXMFData->pSMFData)) != EAS_SUCCESS)
        return result;

    /* if no DLS file, skip this step */
    if (pXMFData->pDLS == NULL)
        return EAS_SUCCESS;

    /* tell the synth to use the DLS collection */
    result = VMSetDLSLib(((S_SMF_DATA*) pXMFData->pSMFData)->pSynth, pXMFData->pDLS);
    if (result == EAS_SUCCESS)
    {
        DLSAddRef(pXMFData->pDLS);
        VMInitializeAllChannels(pEASData->pVoiceMgr, ((S_SMF_DATA*) pXMFData->pSMFData)->pSynth);
    }
    return result;
}

/*----------------------------------------------------------------------------
 * XMF_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:
 *
 *----------------------------------------------------------------------------
*/
static EAS_RESULT XMF_Time (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_U32 *pTime)
{
    return SMF_Time(pEASData, ((S_XMF_DATA*) pInstData)->pSMFData, pTime);
}

/*----------------------------------------------------------------------------
 * XMF_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 XMF_Event (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_INT parserMode)
{
    return SMF_Event(pEASData, ((S_XMF_DATA*) pInstData)->pSMFData, parserMode);
}

/*----------------------------------------------------------------------------
 * XMF_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:
 *
 *----------------------------------------------------------------------------
*/
static EAS_RESULT XMF_State (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_I32 *pState)
{
    return SMF_State(pEASData, ((S_XMF_DATA*) pInstData)->pSMFData, pState);
}

/*----------------------------------------------------------------------------
 * XMF_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 XMF_Close (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData)
{
    S_XMF_DATA* pXMFData;
    EAS_RESULT result;

    pXMFData = (S_XMF_DATA *)pInstData;

    /* close the SMF stream, it will close the file handle */
    if ((result = SMF_Close(pEASData, pXMFData->pSMFData)) != EAS_SUCCESS)
        return result;

    if (pXMFData->pDLS)
        DLSCleanup(pEASData->hwInstData, pXMFData->pDLS);

    /* if using dynamic memory, free it */
    if (!pEASData->staticMemoryModel)
    {
        /* free the instance data */
        EAS_HWFree(pEASData->hwInstData, pXMFData);
    }

    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 * XMF_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 XMF_Reset (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData)
{
    return SMF_Reset(pEASData, ((S_XMF_DATA*) pInstData)->pSMFData);
}

/*----------------------------------------------------------------------------
 * XMF_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 XMF_Pause (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData)
{
    return SMF_Pause(pEASData, ((S_XMF_DATA*) pInstData)->pSMFData);
}

/*----------------------------------------------------------------------------
 * XMF_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:
 *
 *----------------------------------------------------------------------------
*/
static EAS_RESULT XMF_Resume (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData)
{
    return SMF_Resume(pEASData, ((S_XMF_DATA*) pInstData)->pSMFData);
}

/*----------------------------------------------------------------------------
 * XMF_SetData()
 *----------------------------------------------------------------------------
 * Purpose:
 * Sets the playback rate of the underlying SMF file
 *
 * Inputs:
 * pEASData         - pointer to overall EAS data structure
 * handle           - pointer to file handle
 * rate             - rate (28-bit fraction)
 *
 * Outputs:
 *
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
static EAS_RESULT XMF_SetData (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_I32 param, EAS_I32 value)
{
    return SMF_SetData(pEASData, ((S_XMF_DATA*) pInstData)->pSMFData, param, value);
}

/*----------------------------------------------------------------------------
 * XMF_GetData()
 *----------------------------------------------------------------------------
 * Purpose:
 * Gets the file type
 *
 * Inputs:
 * pEASData         - pointer to overall EAS data structure
 * handle           - pointer to file handle
 * rate             - rate (28-bit fraction)
 *
 * Outputs:
 *
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
static EAS_RESULT XMF_GetData (S_EAS_DATA *pEASData, EAS_VOID_PTR pInstData, EAS_I32 param, EAS_I32 *pValue)
{
    EAS_RESULT result;

    /* call SMF parser to get value */
    if ((result = SMF_GetData(pEASData, ((S_XMF_DATA*) pInstData)->pSMFData, param, pValue)) != EAS_SUCCESS)
        return result;

    /* special case for file type */
    if (param == PARSER_DATA_FILE_TYPE)
    {
        if (*pValue == EAS_FILE_SMF0)
            *pValue = EAS_FILE_XMF0;
        else if (*pValue == EAS_FILE_SMF1)
            *pValue = EAS_FILE_XMF1;
    }

    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 * XMF_FindFileContents()
 *----------------------------------------------------------------------------
 * Purpose:
 * Finds SMF data and DLS data in XMF file, and remembers offset for each.
 * If more than one is found, uses the first one found of each.
 * Makes assumptions about the format of a mobile XMF file
 *
 * Inputs:
 * pEASData         - pointer to overall EAS data structure
 * pXMFData         - pointer to XMF parser instance data
 * handle           - pointer to file handle
 *
 * Outputs:
 *
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
static EAS_RESULT XMF_FindFileContents (EAS_HW_DATA_HANDLE hwInstData, S_XMF_DATA *pXMFData)
{
    EAS_RESULT result;
    EAS_I32 value;
    EAS_I32 length;

    /* initialize offsets */
    pXMFData->dlsOffset = pXMFData->midiOffset = 0;

    /* read file length, ignore it for now */
    if ((result = XMF_ReadVLQ(hwInstData, pXMFData->fileHandle, &value)) != EAS_SUCCESS)
        return result;

    /* read MetaDataTypesTable length and skip over it */
    if ((result = XMF_ReadVLQ(hwInstData, pXMFData->fileHandle, &value)) != EAS_SUCCESS)
        return result;
    if ((result = EAS_HWFileSeekOfs(hwInstData, pXMFData->fileHandle, value)) != EAS_SUCCESS)
        return result;

    /* get TreeStart offset and jump to it */
    if ((result = XMF_ReadVLQ(hwInstData, pXMFData->fileHandle, &value)) != EAS_SUCCESS)
        return result;
    if ((result = XMF_ReadNode(hwInstData, pXMFData, value, &length)) != EAS_SUCCESS)
        return result;

    /* check for SMF data */
    if (pXMFData->midiOffset == 0)
    {
        { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "No SMF data found in XMF file\n"); */ }
        return EAS_ERROR_FILE_FORMAT;
    }

    /* check for SFM in wrong order */
    if ((pXMFData->dlsOffset > 0) && (pXMFData->midiOffset < pXMFData->dlsOffset))
        { /* dpp: EAS_ReportEx(_EAS_SEVERITY_WARNING, "DLS data must precede SMF data in Mobile XMF file\n"); */ }

    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 * XMF_ReadNode()
 *----------------------------------------------------------------------------
 * Purpose:
 *
 * Inputs:
 *
 * Outputs:
 *
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
static EAS_RESULT XMF_ReadNode (EAS_HW_DATA_HANDLE hwInstData, S_XMF_DATA *pXMFData, EAS_I32 nodeOffset, EAS_I32 *pLength)
{
    EAS_RESULT result;
    EAS_I32 refType;
    EAS_I32 numItems;
    EAS_I32 offset;
    EAS_I32 length;
    EAS_I32 headerLength;
    EAS_U32 chunkType;

    /* seek to start of node */
    if ((result = EAS_HWFileSeek(hwInstData, pXMFData->fileHandle, nodeOffset)) != EAS_SUCCESS)
        return result;

    /* get node length */
    if ((result = XMF_ReadVLQ(hwInstData, pXMFData->fileHandle, pLength)) != EAS_SUCCESS)
        return result;

    /* get number of contained items */
    if ((result = XMF_ReadVLQ(hwInstData, pXMFData->fileHandle, &numItems)) != EAS_SUCCESS)
        return result;

    /* get node header length */
    if ((result = XMF_ReadVLQ(hwInstData, pXMFData->fileHandle, &headerLength)) != EAS_SUCCESS)
        return result;

    /* get metadata length */
    if ((result = XMF_ReadVLQ(hwInstData, pXMFData->fileHandle, &length)) != EAS_SUCCESS)
        return result;

    /* get the current location */
    if ((result = EAS_HWFilePos(hwInstData, pXMFData->fileHandle, &offset)) != EAS_SUCCESS)
        return result;

    /* skip to node contents */
    if ((result = EAS_HWFileSeek(hwInstData, pXMFData->fileHandle, nodeOffset + headerLength)) != EAS_SUCCESS)
        return result;

    /* get reference type */
    if ((result = XMF_ReadVLQ(hwInstData, pXMFData->fileHandle, &refType)) != EAS_SUCCESS)
        return result;

    /* get the current location */
    if ((result = EAS_HWFilePos(hwInstData, pXMFData->fileHandle, &offset)) != EAS_SUCCESS)
        return result;

    /* process file node */
    if (numItems == 0)

    {
        /* if in-file resource, find out where it is and jump to it */
        if (refType == 2)
        {
            if ((result = XMF_ReadVLQ(hwInstData, pXMFData->fileHandle, &offset)) != EAS_SUCCESS)
                return result;
            offset += pXMFData->fileOffset;
            if ((result = EAS_HWFileSeek(hwInstData, pXMFData->fileHandle, offset)) != EAS_SUCCESS)
                return result;
        }

        /* or else it must be an inline resource */
        else if (refType != 1)
        {
            { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Unexpected reference type %d\n", refType); */ }
            return EAS_ERROR_FILE_FORMAT;
        }

        /* get the chunk type */
        if ((result = EAS_HWGetDWord(hwInstData, pXMFData->fileHandle, &chunkType, EAS_TRUE)) != EAS_SUCCESS)
            return result;

        /* found a RIFF chunk, check for DLS type */
        if (chunkType == XMF_RIFF_CHUNK)
        {
            /* skip length */
            if ((result = EAS_HWFileSeekOfs(hwInstData, pXMFData->fileHandle, sizeof(EAS_I32))) != EAS_SUCCESS)
                return result;

            /* get RIFF file type */
            if ((result = EAS_HWGetDWord(hwInstData, pXMFData->fileHandle, &chunkType, EAS_TRUE)) != EAS_SUCCESS)
                return result;
            if (chunkType == XMF_RIFF_DLS)
                pXMFData->dlsOffset = offset;
        }

        /* found an SMF chunk */
        else if (chunkType == XMF_SMF_CHUNK)
            pXMFData->midiOffset = offset;
    }

    /* folder node, process the items in the list */
    else
    {
        for ( ; numItems > 0; numItems--)
        {
            /* process this item */
            if ((result = XMF_ReadNode(hwInstData, pXMFData, offset, &length)) != EAS_SUCCESS)
                return result;

            /* seek to start of next item */
            offset += length;
            if ((result = EAS_HWFileSeek(hwInstData, pXMFData->fileHandle, offset)) != EAS_SUCCESS)
                return result;
        }
    }

    return EAS_SUCCESS;
}

#if 0
/*----------------------------------------------------------------------------
 * XMF_FindFileContents()
 *----------------------------------------------------------------------------
 * Purpose:
 * Finds SMF data and DLS data in XMF file, and remembers offset for each.
 * If more than one is found, uses the first one found of each.
 * Makes assumptions about the format of a mobile XMF file
 *
 * Inputs:
 * pEASData         - pointer to overall EAS data structure
 * pXMFData         - pointer to XMF parser instance data
 * handle           - pointer to file handle
 *
 * Outputs:
 *
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
static EAS_RESULT XMF_FindFileContents(S_EAS_DATA *pEASData, S_XMF_DATA *pXMFData, EAS_FILE_HANDLE fileHandle)
{
    EAS_RESULT result;
    EAS_I32 offset;
    EAS_I32 value;
    EAS_I32 numItems;
    EAS_I32 length;
    EAS_CHAR id[4];
    EAS_I32 location;

    /* init dls offset, so that we know we haven't found a dls chunk yet */
    pXMFData->dlsOffset = 0;

    /* read file length, ignore it for now */
    if ((result = XMF_ReadVLQ(pEASData, fileHandle, &value)) != EAS_SUCCESS)
        return result;

    /* read MetaDataTypesTable length and skip over it */
    if ((result = XMF_ReadVLQ(pEASData, fileHandle, &value)) != EAS_SUCCESS)
        return result;
    if ((result = EAS_HWFileSeekOfs(pEASData, fileHandle, value)) != EAS_SUCCESS)
        return result;

    /* get TreeStart offset and jump to it */
    if ((result = XMF_ReadVLQ(pEASData, fileHandle, &offset)) != EAS_SUCCESS)
        return result;
    if ((result = EAS_HWFileSeek(pEASData->hwInstData, fileHandle, offset)) != EAS_SUCCESS)
        return result;

    /* read node length, ignore it for now */
    if ((result = XMF_ReadVLQ(pEASData, fileHandle, &value)) != EAS_SUCCESS)
        return result;

    /* read number of contained items */
    if ((result = XMF_ReadVLQ(pEASData, fileHandle, &numItems)) != EAS_SUCCESS)
        return result;

    /*read node header length */
    if ((result = XMF_ReadVLQ(pEASData, fileHandle, &value)) != EAS_SUCCESS)
        return result;

    /*go to the node offset */
    if ((result = EAS_HWFileSeek(pEASData->hwInstData, fileHandle, offset + value)) != EAS_SUCCESS)
        return result;

    /* read Reference Type */
    if ((result = XMF_ReadVLQ(pEASData, fileHandle, &value)) != EAS_SUCCESS)
        return result;

    /* make sure it is an in-line resource, for now */
    if (value != 1)
    {
        { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Problem parsing XMF file tree\n"); */ }
        return EAS_FAILURE;
    }

    /* parse through the list of items */
    while (numItems > 0)
    {
        /*get current offset */
        if ((result = EAS_HWFilePos(pEASData->hwInstData, fileHandle, &offset)) != EAS_SUCCESS)
            return result;

        /*read node length */
        if ((result = XMF_ReadVLQ(pEASData, fileHandle, &length)) != EAS_SUCCESS)
            return result;

        /* read number of items */
        if ((result = XMF_ReadVLQ(pEASData, fileHandle, &value)) != EAS_SUCCESS)
            return result;

        /* make sure not a folder */
        if (value != 0)
        {
            { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Problem parsing XMF file node\n"); */ }
            return EAS_FAILURE;
        }

        /* read offset to resource and jump to it */
        if ((result = XMF_ReadVLQ(pEASData, fileHandle, &value)) != EAS_SUCCESS)
            return result;
        if ((result = EAS_HWFileSeek(pEASData->hwInstData, fileHandle, offset + value)) != EAS_SUCCESS)
            return result;

        /* read Reference Type */
        if ((result = XMF_ReadVLQ(pEASData, fileHandle, &value)) != EAS_SUCCESS)
            return result;

        /* make sure it is an in-line resource */
        if (value != 1)
        {
            { /* dpp: EAS_ReportEx(_EAS_SEVERITY_ERROR, "Problem parsing XMF file node\n"); */ }
            return EAS_FAILURE;
        }

        /* get current offset as a possible location for SMF file or DLS file */
        if ((result = EAS_HWFilePos(pEASData->hwInstData, fileHandle, &location)) != EAS_SUCCESS)
            return result;

        /* read four bytes */
        if ((result = EAS_HWReadFile(pEASData->hwInstData, fileHandle, id, sizeof(id), &value)) != EAS_SUCCESS)
            return result;

        /* check if DLS */
        if (pXMFData->dlsOffset == 0 && id[0] == 'R' && id[1] == 'I' && id[2] == 'F' && id[3] == 'F')
        {
            //remember offset
            pXMFData->dlsOffset = location;
        }

        /* else check if SMF */
        else if (id[0] == 'M' && id[1] == 'T' && id[2] == 'h' && id[3] == 'd')
        {
            //remember offset
            pXMFData->midiOffset = location;

            //we are done
            return EAS_SUCCESS;
        }

        //one less item
        numItems--;

        //if more data, go to the next item
        if (numItems >0)
        {
            if ((result = EAS_HWFileSeek(pEASData->hwInstData, fileHandle, offset + length)) != EAS_SUCCESS)
                return result;
        }
    }

    return EAS_FAILURE;

}
#endif

/*----------------------------------------------------------------------------
 * XMF_ReadVLQ()
 *----------------------------------------------------------------------------
 * Purpose:
 * Reads a VLQ encoded value from the file referenced by fileHandle
 *
 * Inputs:
 * pEASData         - pointer to overall EAS data structure
 * fileHandle       - pointer to file handle
 *
 * Outputs:
 * value            - pointer to the value decoded from the VLQ data
 *
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
static EAS_RESULT XMF_ReadVLQ (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE fileHandle, EAS_I32 *value)
{
    EAS_RESULT result;
    EAS_U8 c;

    *value = 0;

    if ((result = EAS_HWGetByte(hwInstData, fileHandle, &c)) != EAS_SUCCESS)
        return result;

    while (c > 0x7F)
    {
        /*lint -e{703} shift for performance */
        *value = (*value << 7) | (c & 0x7F);

        if ((result = EAS_HWGetByte(hwInstData, fileHandle, &c)) != EAS_SUCCESS)
            return result;
    }

    /*lint -e{703} shift for performance */
    *value = (*value << 7) | c;

    return EAS_SUCCESS;
}