instfile.c++ 25 KB
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
//====================================================================
// instfile.c++
//
// Copyright 1993, Silicon Graphics, Inc.
// All Rights Reserved.
//
// This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
// the contents of this file may not be disclosed to third parties, copied or
// duplicated in any form, in whole or in part, without the prior written
// permission of Silicon Graphics, Inc.
//
// RESTRICTED RIGHTS LEGEND:
// Use, duplication or disclosure by the Government is subject to restrictions
// as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
// and Computer Software clause at DFARS 252.227-7013, and/or in similar or
// successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
// rights reserved under the Copyright Laws of the United States.
//====================================================================

#include <assert.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "instfile.h"
#include "sccvt.h"

SCInstrumentFile::SCInstrumentFile(char *fileName, char *mode)
{
    file        = fopen(name, mode);
    comment     = 0;
    scDebug     = 0;

    file = fopen(fileName, mode);
    assert(file);
    
    // save away the file name
    name = (char *)malloc(strlen(fileName)+1);
    strcpy(name, fileName);

    // save the directory name
    dir = (char *)malloc(strlen(fileName) + 1);
    strcpy(dir, fileName);
    dir = dirname(dir);
    
    FileHeader fhdr;
    fread(&fhdr, sizeof(FileHeader), 1, file);
}

SCInstrumentFile::~SCInstrumentFile() 
{
    if (comment)
        free(comment);
}

void SCInstrumentFile::parseAnno(ChunkHeader *hdr)
{
    if (hdr->ckSize) {
        // The annotation chunk is ignored in the conversion
        comment = (char *)malloc((int)hdr->ckSize);
    
        fread(comment, 1, (int)hdr->ckSize, file);
        p2cstri(comment);

        if (scDebug) {
            printf("\t%s\n", comment);
        }
    } else {
        comment = 0;
    }
}

void SCInstrumentFile::parseKeyP(ChunkHeader * /*hdr*/)
{
    short       lastHighNote = 0;
    ICKeyMap    *lastMap = 0;
    ICKeyMap    *map = 0;
    
    short numKeyGroups;
    fread(&numKeyGroups, sizeof(short), 1, file);

    short keygroupParamSize;
    fread(&keygroupParamSize, sizeof(short), 1, file);

    short velZoneParamSize;
    fread(&velZoneParamSize, sizeof(short), 1, file);

    if (scDebug) {
        printf("\tnum key groups = %d\n", numKeyGroups);
        printf("\tkeygroupParamSize = %d\n", keygroupParamSize);
        printf("\tvelZoneParamSize = %d\n", velZoneParamSize);
    }

    int i;
    for (i = 0; i < numKeyGroups; i++) {
        lastMap = 0;
        
        short highNoteLimit;
        fread(&highNoteLimit, sizeof(short), 1, file);
                        
        short groupPanning;
        fread(&groupPanning, sizeof(short), 1, file);

        short numZonesInGroup;
        fread(&numZonesInGroup, sizeof(short), 1, file);

        if (scDebug) {
            printf("\thighNoteLimit = %d\n", highNoteLimit);
            printf("\tgroupPanning = %d\n", groupPanning);
            printf("\tnumZonesInGroup = %d\n", numZonesInGroup);
            printf("\n");
        }

        int j;
        for (j = 0; j < numZonesInGroup; j++) {
            long leftfileIndex;
            fread(&leftfileIndex, sizeof(long), 1, file);
            assert(leftfileIndex <= soundCount);
            
            ICSoundFile *sfile = sounds[leftfileIndex];
            map = sfile->GetKeyMap();
            ICSound *sound = sfile->GetSound();
            
            long rightfileIndex;
            fread(&rightfileIndex, sizeof(long), 1, file);

            if (rightfileIndex != -1) {
                printf("warning: right file ignored\n");
            }
            
            SCellVelocityZoneRec vzr;
            fread(&vzr, sizeof(vzr), 1, file);

            //------------------------------------------------------------
            // Sound Pan:
            // Note SC pan is centered at 0. MIDI and IC pan is centered
            // at 64. SC ranges from -99 to +99, MIDI from 0 to 127
            //------------------------------------------------------------
            int pan = scPan2alPan(groupPanning);
            sound->SetPan(pan);
            
            //------------------------------------------------------------
            // Sample Volume
            // 0-99:0=-50dB,99=0dB
            // 0.5 db per step (0 to 99)
            //------------------------------------------------------------
            int vol = scVol2alVol(vzr.SampVolume);
            sound->SetVol((short)vol);
            
            //------------------------------------------------------------
            // Sample Direction
            //------------------------------------------------------------
            if (vzr.SampDirection != 0) {
                printf("warning: reverse playback not supported\n");
            }
            
            //------------------------------------------------------------
            // Sample Start (ignored)
            // You could set the start value in the ICSoundFile so that
            // it would start from this offset
            //------------------------------------------------------------
            if (vzr.SampStart != 0) {
                printf("warning: sample start offset not supported\n");
            }
            
            //------------------------------------------------------------
            // Velocity ranges
            //------------------------------------------------------------
            int vel = vzr.Threshold << 3;
            if (lastMap) {
                lastMap->SetVelMax(vel);
            }
            map->SetVelMin(vel+1);
            map->SetVelMax(0x7f);   // this will be reset if there are more
            
            //------------------------------------------------------------
            // Sample note ranges
            //------------------------------------------------------------
            map->SetKeyBase(vzr.SampBaseNote);
            map->SetKeyMin(lastHighNote+1);
            map->SetKeyMax(highNoteLimit);

            lastHighNote = highNoteLimit;
            lastMap = map;

            if (scDebug) {
                printf("\t\tleftfileIndex = %d\n", leftfileIndex);
                printf("\t\trightfileIndex = %d\n",rightfileIndex);
                printf("\t\tThreshold %d\n", vzr.Threshold);
                printf("\t\tSampVolume %d\n", vzr.SampVolume);
                printf("\t\tSampDirection %d\n", vzr.SampDirection);
                printf("\t\tSampStart %d\n", vzr.SampStart);
                printf("\t\tSampBaseNote %d\n", vzr.SampBaseNote);
                printf("\n");
            }
        }
        
    }
}

void SCInstrumentFile::parseKeyS(ChunkHeader *)
{
    ICSoundFile *sfile;
    
    long numSamples;
    fread(&numSamples, sizeof(long), 1, file);
                
    long sampleRecSize;
    fread(&sampleRecSize, sizeof(long), 1, file);
                
    long numSampleBytes;
    fread(&numSampleBytes, sizeof(long), 1, file);

    if (scDebug) {
        printf("\tnumSamples = %d\n", numSamples);
        printf("\tsampleRecSize = %d\n", sampleRecSize);
        printf("\tnumSampleBytes = %d\n", numSampleBytes);
    }

    int i;
    for (i = 0; i < numSamples; i ++) {

        long fileIndex;
        fread(&fileIndex, sizeof(long), 1, file);
        assert(fileIndex <= soundCount);
        sfile = sounds[fileIndex];
        if (!sfile) {
            char tmp[64];
            fread(tmp, (int)(sampleRecSize - sizeof(fileIndex)), 1, file);
            continue;
        }
        
        //------------------------------------------------------------
        // Sd2f region id (ignore)
        //------------------------------------------------------------
        long regionID;
        fread(&regionID, sizeof(long), 1, file);
                        
        //------------------------------------------------------------
        // channel this region refers to ie 1 = left, 2 = right (ignore)
        //------------------------------------------------------------
        short audioChannel;
        fread(&audioChannel, sizeof(short), 1, file);
        if (audioChannel != 1) {
            printf("warning: assuming left channel for file %s\n",
                   sfile->GetName());
        }
                        
        //------------------------------------------------------------
        // starting frame in sample region
        //------------------------------------------------------------
        long startFrame;
        fread(&startFrame, sizeof(long), 1, file);
        
        //------------------------------------------------------------
        // ending frame in sample region
        //------------------------------------------------------------
        long stopFrame;
        fread(&stopFrame, sizeof(long), 1, file);
        
        //------------------------------------------------------------
        // Name of sample (ignore)
        //------------------------------------------------------------
        Str31 sampleName;       
        fread(&sampleName, sizeof(Str31), 1, file);
	
        //------------------------------------------------------------
        //Sample Start playback offset from start of region (ignore)
        //------------------------------------------------------------
        long sampleStart;
        fread(&sampleStart, sizeof(long), 1, file);
        if (sampleStart) {
            printf("warning: sample start offset ignored for file %s\n",
                   sfile->GetName());
        }
        
        //------------------------------------------------------------
        // Fine tuning information for this sound file
        // 63 = no tuning adjustment ???
        //------------------------------------------------------------
        short sampleFineTune; 
        fread(&sampleFineTune, sizeof(short), 1, file);

        int tune = scTuning2Cents(sampleFineTune);
        ICKeyMap *keymap = sfile->GetKeyMap();
        keymap->SetDetune(tune);
        
        //------------------------------------------------------------
        // Loop1 information
        //------------------------------------------------------------
        short loop1Enabled;
        fread(&loop1Enabled, sizeof(short), 1, file);
                        
        short loop1FineTune;
        fread(&loop1FineTune, sizeof(short), 1, file);
	
        if (loop1Enabled && (loop1FineTune != 63)) {
            printf("warning: loop1 fine tuning (%d) ignored for file %s\n",
                   loop1FineTune, sfile->GetName());
        }

        //------------------------------------------------------------
        // Loop2 information
        // if only one loop in the file, SC assumes that it is a
        // release loop
        //------------------------------------------------------------
        short loop2Enabled;
        fread(&loop2Enabled, sizeof(short), 1, file);
                        
        short loop2FineTune;
        fread(&loop2FineTune, sizeof(short), 1, file);

        if (loop2Enabled && (loop2FineTune != 63)) {
            printf("warning: loop2 fine tuning (%d) ignored for file %s\n",
                   loop1FineTune, sfile->GetName());
        }
        
        //------------------------------------------------------------
        // debugging info
        //------------------------------------------------------------
        if (scDebug) {
            printf("\n");
            printf("\t\tfileIndex %d\n", fileIndex);
            printf("\t\tregionID %d\n", regionID);
            printf("\t\taudioChannel %d\n", audioChannel);
            printf("\t\tstartFrame %d\n", startFrame);
            printf("\t\tstopFrame %d\n", stopFrame);
            printf("\t\tsampleName %s\n", p2cstri((char *)&sampleName));
            printf("\t\tsampleStart %d\n", sampleStart);
            printf("\t\tsampleFineTune %d\n", sampleFineTune);
            printf("\t\tloop1Enabled %d\n", loop1Enabled);
            printf("\t\tloop1FineTune %d\n", loop1FineTune);
            printf("\t\tloop2Enabled %d\n", loop2Enabled);
            printf("\t\tloop2FineTune %d\n", loop2FineTune);
        }               
    }
}

void SCInstrumentFile::parseFILP(ChunkHeader *)
{
    char str[256];
    
    long numRecords;
    fread(&numRecords, sizeof(numRecords), 1, file);
    this->soundCount = (int) numRecords;
    
    long recordSize;
    fread(&recordSize, sizeof(recordSize), 1, file);

    if (scDebug) {
        printf("\tnum records = %d\n", numRecords);                
        printf("\trecord size = %d\n", recordSize);
        printf("\n");
    }

    // allocate an array to hold the sound references
    sounds = (ICSoundFile **)calloc((int)numRecords, sizeof(ICSound *));
    assert(sounds);
    
    int i;
    for (i = 0; i < numRecords; i++) {
        FILP fp;
        fread(&fp, sizeof(fp), 1, file);

        p2cstri(fp.volume);
        p2cstri(fp.file);        
        
        if (fp.type == 'AIFF') {  // only aiff files at this point
            char str[256];
            
            unixName(fp.file);
            makePath(str, dir, fp.file);
            strcat(str, ".aifc");
            
            // ### should check for reused sounds
            ICSoundFile *sfile = new ICSoundFile(str);
            id *idptr = symtab->Enter(0, str, strlen(str));
            symtab->DeclareGlobal(idptr, sfile);
            wavtab->Append(sfile);
                                
            objtab->Append(sfile->GetKeyMap());
            ICWave *w = sfile->GetWaveTable();
            objtab->Append(w);

            ICLoop *l = w->GetLoop();
            objtab->Append(l);

            ICBook *b = w->GetBook();
            objtab->Append(b);

            ICSound *sound = sfile->GetSound();      // add it to the table
            objtab->Append(sound);

            sound->SetEnvelope(&envelope);
            
            instrument.AddSound(sound, i);
            sounds[i] = sfile;
            
        } else { // ### warn the user
            printf("error: %s is not an AIFF file, it is %.4s\n",
                   fp.file, &fp.type);
            exit(-1);
        }

        if (scDebug) {
            printf("\tvolume:\t%s\n", fp.volume);
            printf("\tdirID:\t%d\n", fp.dirID);                
            printf("\tfile:\t%s\n", fp.file);
            printf("\ttype:\t%.4s\n", &fp.type);
            printf("\n");
        }
    }
}

void SCInstrumentFile::parseEnvP(ChunkHeader *)
{
    short numEnvelopes;
    fread(&numEnvelopes, sizeof(short), 1, file);
                    
    short envelopeSize;
    fread(&envelopeSize, sizeof(short), 1, file);
                    
    int i;
    for (i = 0; i < numEnvelopes; i++) {
        SPEnvelopeRec er;
        fread(&er, sizeof(er), 1, file);

        if (i == 3) {
            //--------------------------------------------------
            // envelope 3 is hardwired to amplitude is SC
            //--------------------------------------------------
            ALMicroTime attackTime = scTime2alTime(er.Attack);
            envelope.SetAttackTime(attackTime);

            int attackVol = scVol2alVol(er.Amplitude);
            envelope.SetAttackVol(attackVol);

            ALMicroTime decayTime = scTime2alTime(er.Decay);
            envelope.SetDecayTime(decayTime);

            int decayVol = scVol2alVol(er.SustainLevel);
            envelope.SetDecayVol(decayVol);
            
            if (er.SustainLevel != er.SustainDecay) {
                printf("warning: envelope 3 sustain level != sustain decay ");
                printf("in file %s\n", name);
            }
            
            ALMicroTime releaseTime = scTime2alTime(er.Release);
            envelope.SetReleaseTime(releaseTime);
            
            envelope.SetReleaseVol(0);

            if (er.GateTime) {
                printf("warning: envelope 3 gate time ignored in %s\n", name);
            }

            if (er.TimeTrack) {
                printf("warning: envelope 3 time track ignored in %s\n", name);
            }
            
            objtab->Append(&envelope);
        }
        
        if (scDebug) {
            printf("\tAttack:\t%d\n", er.Attack);
            printf("\tDecay:\t%d\n", er.Decay);
            printf("\tSustainLevel:\t%d\n", er.SustainLevel);
            printf("\tSustainDecay:\t%d\n", er.SustainDecay);
            printf("\tRelease:\t%d\n", er.Release);
            printf("\tGateTime:\t%d\n", er.GateTime);
            printf("\tTimeTrack:\t%d\n", er.TimeTrack);
            printf("\tAmplitude:\t%d\n", er.Amplitude);
            printf("\n");
        }
    }
}

void SCInstrumentFile::parseGenP(ChunkHeader *)
{
                    
    SPGenParmsRec gp;
    fread(&gp, sizeof(gp), 1, file);

    midiChannel = gp.MIDIChannel;

    int coarseTuning = 12*gp.PitchOctave + gp.PitchSemitone;
    instrument.SetCoarseTuning(coarseTuning);
    
    int fineTuning = scTuning2Cents(gp.DetuneAmount);
    instrument.SetFineTuning(fineTuning);
    
    if (gp.DetuneMode) {
        printf("warning: equal detune not supported in %s\n", name);
    }

    if (!gp.KeyTrack) {
        printf("warning: key tracking can't be disabled in %s\n", name);
    }

    if (gp.KeyAssignMode) {
        printf("warning: mono mode not supported in %s\n", name);
    }
    
    instrument.SetPriority(gp.SoundPriorityLevel);

    if (gp.OverlapAmount) {
        printf("warning: Overlap not supported in %s\n", name);
    }
    
    if (gp.CrossFade) {
        printf("warning: Instrument Crossfade not supported in %s\n", name);
    }

    int pan = scPan2alPan(gp.Panning);
    instrument.SetPan(pan);

    int vol = scVol2alVol(gp.Volume);
    instrument.SetVol(vol);
    
    int range = gp.PitchByPitchWheel;
    if (range < 0) {
        printf("warning: negative Pitch Wheel valuesnot supported in %s\n",
               name);
    } else {
        instrument.SetPitchBendRange(range);
    }
    
    if (gp.DisableMIDIVolume) {
        printf("warning: can't disable MIDI pan & volume in %s\n", name);
    }
    
    if (scDebug) {
        printf("\tInstrumentSoundFlag %d\n", gp.InstrumentSoundFlag);
        printf("\tMIDIChannel %d\n", gp.MIDIChannel);
        printf("\tMIDILowNoteLimit %d\n", gp.MIDILowNoteLimit);
        printf("\tMIDIHiNoteLimit %d\n", gp.MIDIHiNoteLimit);

        printf("\tPitchOctave %d\n", gp.PitchOctave);
        printf("\tPitchSemitone %d\n", gp.PitchSemitone);
        printf("\tDetuneAmount %d\n", gp.DetuneAmount);
        printf("\tDetuneMode %d\n", gp.DetuneMode);

        printf("\tKeyTrack %d\n", gp.KeyTrack);
        printf("\tKeyAssignMode %d\n", gp.KeyAssignMode);
        printf("\tSoundPriorityLevel %d\n", gp.SoundPriorityLevel);
        printf("\tOverlapAmount %d\n", gp.OverlapAmount);
        printf("\tCrossFade %d\n", gp.CrossFade);

        printf("\tSend1BusSelect %d\n", gp.Send1BusSelect);
        printf("\tSend1PrePostFader %d\n", gp.Send1PrePostFader);
        printf("\tSend1Volume %d\n", gp.Send1Volume);
        printf("\tSend2BusSelect %d\n", gp.Send2BusSelect);
        printf("\tSend2PrePostFader %d\n", gp.Send2PrePostFader);
        printf("\tSend2Volume %d\n", gp.Send2Volume);
        printf("\tLeftPanBusSelect %d\n", gp.LeftPanBusSelect);
        printf("\tRightPanBusSelect %d\n", gp.RightPanBusSelect);

        printf("\tPanning %d\n", gp.Panning);
        printf("\tMute %d\n", gp.Mute);
        printf("\tVolume %d\n", gp.Volume);
        printf("\tMIDIControllerANum %d\n", gp.MIDIControllerANum);
        printf("\tMIDIControllerBNum %d\n", gp.MIDIControllerBNum);
        printf("\tPitchByPitchWheel %d\n", gp.PitchByPitchWheel);
        printf("\tVolumeByVelocity %d\n", gp.VolumeByVelocity);
        printf("\tDisableMIDIVolume %d\n", gp.DisableMIDIVolume);
    }
}

void SCInstrumentFile::parseLFOP(ChunkHeader *)
{
    SPLFORec lfo;
                    
    short numLFO;
    fread(&numLFO, sizeof(short), 1, file);
                    
    short LFOSize;
    fread(&LFOSize, sizeof(short), 1, file);

    if (scDebug) {
        printf("\tnumLFO = %d\n", numLFO);
        printf("\tLFOSize = %d\n", LFOSize);
    }
    
    int i;
    for (i = 0; i < numLFO; i++) {
        fread(&lfo, sizeof(lfo), 1, file);

        if (scDebug) {            
            printf("\n");
            printf("\tSpeed\t\t%d\n", lfo.Speed);
            printf("\tAmplitude\t%d\n", lfo.Amplitude);
            printf("\tTrigger\t\t%d\n", lfo.Trigger);
            printf("\tWaveShape\t%d\n", lfo.WaveShape);
        }        
    }
}

void SCInstrumentFile::parseRamp(ChunkHeader *)
{
    SCellRampGeneratorRec rg;

    short numRamps;
    fread(&numRamps, sizeof(short), 1, file);
                    
    short rampSize;
    fread(&rampSize, sizeof(short), 1, file);

    if (scDebug) {
        printf("\tnumRamps\t%d\n", numRamps);
        printf("\trampSize\t%d\n", rampSize);
    }
                    
    int i;
    for (i = 0; i < numRamps; i++){
        fread(&rg, sizeof(rg), 1, file);

        if (scDebug) {
            printf("\n");
            printf("\tRampTime\t%d\n", rg.RampTime);
        }
    }
}

void SCInstrumentFile::parseTrkP(ChunkHeader *)
{
    SCellTrackGeneratorRec tg;
                    
    short numTrackers;
    fread(&numTrackers, sizeof(short), 1, file);
                    
    short trackerSize;
    fread(&trackerSize, sizeof(short), 1, file);
    assert(trackerSize == sizeof(tg));

    if (scDebug) {
        printf("\tnumTrackers\t%d\n", numTrackers);
        printf("\ttrackerSize\t%d\n", trackerSize);
    }

    int i;
    for (i = 0; i < numTrackers; i++){
        fread(&tg, sizeof(tg), 1, file);

        if (scDebug) {
            printf("\tInput\t%d\n", tg.Input);
            printBuffer((char *)&tg.TrackPoints, kSCellIINumTrackPoints);
        }
    }
}

void SCInstrumentFile::parseModP(ChunkHeader *)
{
    SPModPathRec mp;
    short numModPaths;
    fread(&numModPaths, sizeof(short), 1, file);
                    
    short modPathSize;
    fread(&modPathSize, sizeof(short), 1, file);

    if (scDebug) {
        printf("\tnumModPaths\t%d\n", numModPaths);
        printf("\tmodPathSize\t%d\n", modPathSize);
    }

    int i;
    for (i = 0; i < numModPaths; i++) {
        fread(&mp, sizeof(mp), 1, file);

        if (scDebug) {
            printf("\n");
            printf("\tGate\t%d\n", mp.Gate);
            printf("\tSource\t%d\n", mp.Source);
            printf("\tDestination\t%d\n", mp.Destination);
            printf("\tAmplitude\t%d\n", mp.Amplitude);
        }
    }
}

void SCInstrumentFile::Parse()
{
    objtab->Append(&instrument);
    objtab->Append(&envelope);
    
    int done = 0;
    while (!done) {

        ChunkHeader hdr;
        int num = fread(&hdr, sizeof(ChunkHeader), 1, file);
        if (num < 1) {
            done = 1;
            break;
        }

        long offset = ftell(file);
        
        if (scDebug) {
            printf("%.4s 0x%x\n", &hdr.ckID, hdr.ckSize);
        }
        
        switch (hdr.ckID) {
            case ('ANNO'):
                this->parseAnno(&hdr);
                break;

            case ('KeyP'):
                this->parseKeyP(&hdr);
                break;
                
            case ('KeyS'):
                this->parseKeyS(&hdr);
                break;
                
            case ('FILP'):
                this->parseFILP(&hdr);
                break;
                
            case ('EnvP'):
                this->parseEnvP(&hdr);
                break;
                
            case ('GenP'):
                this->parseGenP(&hdr);
                break;
                
            case ('LFOP'):
                this->parseLFOP(&hdr);
                break;

            case ('Ramp'):
                this->parseRamp(&hdr);
                break;
                
            case ('TrkP'):
                this->parseTrkP(&hdr);
                break;
                
            case ('ModP'):
                this->parseModP(&hdr);
                break;
                
            case ('FVER'):
                break;
                
            default:
                printf("unknown chunk type\n");
                break;
        }

        fseek(file, offset + hdr.ckSize, SEEK_SET);
    }
}

short SCInstrumentFile::scTuning2Cents(short tuning) 
{
    // -99 to 99 maps to -1/4 tone to 1/4 tone
    return 0;
}

short SCInstrumentFile::scVol2alVol(short scVol)
{
    // 0.5 db per step. ### this should be more accurate
    int tmp = (scVol*128)/100;
    
    return (short)tmp;
}

ALPan SCInstrumentFile::scPan2alPan(short scPan)
{
    long pan = (scPan*64)/100 + AL_PAN_CENTER;
    
//    return (ALPan) pan;
    return 0;
}

ALMicroTime SCInstrumentFile::scTime2alTime(short scTime)
{
    return 0;
}

void SCInstrumentFile::ListFiles()
{
    int i;

    printf("--- Files in %s ---\n", name);
    
    for (i = 0; i < soundCount; i++){
        if (sounds[i] != 0) {
            ICSoundFile *sfile = sounds[i];
            printf("\t%s\n", sfile->GetName());
        }
    }
}

void SCInstrumentFile::unixName(char *str)
{
    char *c = str;
    
    for(; *str != 0; str++) {
        if (*str == ' ')
            *str = '_';
    }
}

void SCInstrumentFile::makePath(char *out, char *dir, char *fName)
{
    out[0] = 0;
    strcpy(out, dir);
    strcat(out, "/");
    strcat(out, fName);
}