libbank.h 14.9 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
//====================================================================
// libbank.h
//
// 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.
//====================================================================

#pragma once

#include <libaudio.h>
#include <stdio.h>
#include <audiotools.h>

//----------------------------------------------------------------------
// misc stuff
//----------------------------------------------------------------------

#ifndef MIN
#define	MIN(a,b) (((a)<(b))?(a):(b))
#endif

#ifndef MAX
#define	MAX(a,b) (((a)>(b))?(a):(b))
#endif

#define ICBANK          AL_BANK_CLASS
#define ICINST          AL_INST_CLASS
#define ICSOUND         AL_SOUND_CLASS
#define ICKEYMAP        AL_KEYMAP_CLASS
#define ICWAVE          AL_WAVETBL_CLASS
#define ICLOOP          AL_LOOP_CLASS
#define ICBANKFILE      AL_BNKFILE_CLASS
#define ICENVELOPE      AL_ENVLP_CLASS
#define ICBOOK          AL_BOOK_CLASS
#define ICSOUNDFILE     AL_SNDFILE_CLASS

char *icStrip(char *dest, char *src);


//----------------------------------------------------------------------
// error stuff
//----------------------------------------------------------------------
typedef enum {
    IC_INTERNAL_ERR = 0,
    IC_MISC_ERR,
    IC_PARAMETER_ERR,
    IC_TYPE_ERR,
    IC_FILE_ERR,
    IC_SOURCE_FMT_ERR,    
    IC_MAX_ERR
} ICError;

typedef void (*ICErrFunc)(ICError errtype, char *msg);
extern ICErrFunc icErrFunc;

char *icErrStr(char *msg, char *msg1, char *msg2);

#define FailIf(condition, error)        \
            if (condition) {            \
                (*icErrFunc)(error, 0); \
                return; }


#define FailIfMsg(condition, error, msg)  \
            if (condition) {            \
                (*icErrFunc)(error, msg); \
                return; }

extern int objectID;


//----------------------------------------------------------------------
// list stuff
//----------------------------------------------------------------------
class Node {
  public:
    Node        *next;          // must be first
    void        *data;
    int         flags;

    Node();
};

class List {
  public:    
    Node        *head;
    Node        *tail;
    int         count;    

    List();
    
    void Append(void *data);
    void Prepend(void *data);
    Node *Remove(Node *node, Node *prev);
    Node *DeleteFromList(Node *node, List *list);
};

class id {
  public:
    id *	next;		// link to next id
    char        *name;
    int         lextype;
};

//----------------------------------------------------------------------
// declaration stuff
//----------------------------------------------------------------------
class ICDecl
{
  protected:
    id  *declid;
    int size;           /* size of object in bytes      */
    int objid;          /* ic object id                 */
    int declclass;

  public:
    ICDecl();
    void        SetDeclID(id *did)      { declid = did;}
    char        *GetName()              { return declid->name; }
};

class ICIntDecl : public ICDecl
{
  public:
    int value;
    ICIntDecl(int val) { value = val;}
};

class ICObject : public ICDecl
{
  protected:
    void                *thing;
    int                 refCount;
    int                 usedFlag;
    void                *offset;        /* offset in the file image     */  
    
  public:
    
    ICObject();

    int                 GetObjectID()           { return objid;         }
    int                 GetObjectClass()        { return declclass;     }
    int                 GetRefCount()           { return refCount;      }
    int                 GetUsedFlag()           { return usedFlag;      }
    int                 GetObjectOffset()       { return (int)offset;   }
    void                IncRefCount();
    void                DecRefCount();

    virtual int         Size(void);
    virtual void        Print(void);
    virtual void        Write(FILE *f);
    virtual int         LayOut(int offset);
    virtual void        Assemble(void);
    virtual void        Prune(void);
    virtual void        MarkUsed(void);    
};

class ICBook : public ICObject
{
  protected:
    ALADPCMBook *bookp;
    short       *book;
    int         order;
    int         npredictors;
    
  public:
    ICBook();
    ICBook(short *bookPtr, int order, int npredictors);

    void        SetBookData(short *bookPtr, int order, int npredictors);
    
    virtual void        Assemble();
    virtual void        Print();
};

//----------------------------------------------------------------------
// loop stuff
//----------------------------------------------------------------------
typedef short ICLoopState[ADPCMFSIZE];

char *ReadPString(FILE *ifile);

class ICLoop : public ICObject
{
  protected:
    ALADPCMloop *loop;
    u32         start;
    u32         end;
    u32         count;
    ICLoopState state;

  public:

    ICLoop();
    virtual ~ICLoop();
    
    void    SetPoints(u32 start, u32 end, u32 count, ICLoopState state);
    void    Assemble();
    void    Print();
};

class ICWave : public ICObject
{
  protected:  
    ALWaveTable *wave;
    char        *base;
    int         len;
    char        type;
    ICLoop      *loop;
    ICBook      *book;
    
  public:
    ICWave();
    virtual ~ICWave();
    
    void        SetName(char *fname);
    void        SetTable(char *base, int len);
    void        SetBase(char *base);

    int         GetLen() { return this->len; }
    void        SetLen(int len);
    void        SetType(int type);
    
    void        SetLoop(ICLoop *l);
    ICLoop      *GetLoop() { return loop; }
    
    void        SetBook(ICBook *b);
    ICBook      *GetBook() { return book; }
    
    virtual void        Assemble(void);
    virtual void        Print(void);
    virtual void        Prune(void);
    virtual void        MarkUsed(void);
};

class ICEnvelope : public ICObject
{
  protected:
    ALEnvelope  *envelope;
    ALMicroTime attackTime;
    short       attackVol;
    ALMicroTime decayTime;
    short       decayVol;
    ALMicroTime releaseTime;

  public:
    ICEnvelope();

    void    SetAttackTime(ALMicroTime t);
    void    SetAttackVol(short v);
    void    SetDecayTime(ALMicroTime t);
    void    SetDecayVol(short v);
    void    SetReleaseTime(ALMicroTime t);

    virtual void        Assemble(void);
    virtual void        Print(void);
};

class ICKeyMap : public ICObject
{
  public:
    ALKeyMap    *map;           /* file representation          */
    char        velocityMin;
    char        velocityMax;
    char        keyMin;
    char        keyMax;
    char        keyBase;
    char        detune;

    ICKeyMap();
    
    void    SetKeyMin(int min);
    void    SetKeyMax(int max);
    void    SetKeyBase(int base);
    void    SetVelMin(int value);	
    void    SetVelMax(int value);	
    void    SetDetune(int value);	

    virtual void        Assemble(void);
    virtual void        Print(void);
};

class ICSound : public ICObject
{
  protected:
    ALSound     *sound;    
    char        samplePan;
    char        sampleVolume;
    ICEnvelope  *envelope;
    ICKeyMap    *keymap;
    ICWave      *wave;

  public:
    ICSound();
    ICSound(char *file);
    
    void    SetPan(int pan);
    void    SetVol(int vol);

    void        SetKeyMap(ICKeyMap *map);
    ICKeyMap    *GetKeyMap() { return keymap;}
    
    void        SetEnvelope(ICEnvelope *env);
    ICEnvelope  *GetEnvelope() { return envelope;}

    void        SetWaveTable(ICWave *w);
    void        MarkUsed(void);
    
    virtual void        Assemble(void);
    virtual void        Print(void);
    virtual void        Prune(void);
    
};

class ICInst : public ICObject
{
  protected:
    ALInstrument        *inst;
    List                soundList;
    char                volume;
    char                pan;
    char                priority;
    char                tremType;
    char                tremRate;
    char                tremDepth;
    char                tremDelay;
    char                vibType;
    char                vibRate;
    char                vibDepth;
    char                vibDelay;
    int                 soundCount;
    s16                 bendRange;
    int                 sortSounds;
    
  public:
    ICInst();
    
    void        AddSound(ICSound *s, int number);
    void        SetPan(int pan);
    void        SetVol(int vol);
    void        SetPriority(char prior);
    void        SetPitchBendRange(s16 range);
    void        SetTremType(int type);
    void        SetTremRate(int rate);
    void        SetTremDepth(int depth);
    void        SetTremDelay(int delay);
    void        SetVibType(int type);
    void        SetVibRate(int rate);
    void        SetVibDepth(int depth);
    void        SetVibDelay(int delay);
    void        SetSort(int doSort);
    void        ThinSounds(void);
    
    virtual void        Assemble(void);
    virtual void        Print(void);
    virtual void        Prune(void);
    virtual void        MarkUsed(char key);
};


//----------------------------------------------------------------------
// bank object
//----------------------------------------------------------------------
class ICBank : public ICObject
{
  protected:
    ALBank      *bank;          /* file representation          */
    List        progList;       /* list of assigned instruments */
    int         maxPrograms;    /* maximum number of programs   */
    int         sampleRate;     /* sample rate that the bank is designed for */
    ICInst      *percussion;
    int         percActive;

    
  public:
    ICBank();
    
    void   AddProgram(ICInst *i, int number);
    void   SetPercussionDefault(ICInst *i);
    void   SetSampleRate(int srate);
    void   ThinPrograms(void);
    ICInst *GetPercussion(void);
    
    virtual void        Assemble();
    virtual void        Print(void);
    virtual void        Prune(void);
    virtual void        MarkUsed(char program, char key);    
};

class ICBankFile : public ICObject
{
  protected:
    ALBankFile  *bankfile;
    List        bankList;
    int         bankCount;
    int         version;

  public:
    ICBankFile();
    
    void    AddBank(ICBank *b, int number);
    ICBank* GetBank(int number);
    
    virtual void        Assemble(void);
    virtual void        Print(void);
    virtual void        Prune(void);
};

class ICObjTab
{
  protected:
    List objList;
    int  writeCount;    // number of objects written to the bank file
    
  public:
    int     compact;
    
    ICObjTab();
    
    void Append(ICObject *o);
    
    void Print();
    void Write(FILE *f);
    void WriteSymFile(FILE *f);
    void LayOutFile();
    void AssembleFile();
};

class ICWavTab
{
  protected:
    List objList;

  public:
    int     compact;
    
    ICWavTab();
    
    void Append(ICObject *o);
    
    void Print();
    void LayOutFile();
    void AssembleFile();
    void Write(FILE *f);
};

//----------------------------------------------------------------------
// symbol table stuff
//----------------------------------------------------------------------
class ste
{
  public:
    id          *name;
    ICDecl	*decl;
    ste         *prev;
};

class ICSymTab
{
  protected:
    ste	*scopestack[100];	/* stack of saved scopes */
    ste	**stacktop;

    ste	**globalscope;
    
    int offsetstack[100];       /* ??? */
    int *offsettop;
    
  public:
    ICSymTab();
    
    void        Declare(struct id *idptr, ICDecl *declptr);
    void        DeclareGlobal(struct id *idptr, ICDecl *declptr);

    ICDecl      *TestGlobalDecl(id *idptr);
    void        CheckRedecl(id *idptr);
    
    id          *FindName(ICDecl*thedecl);
    ICDecl      *FindCurrentDecl(id *idptr);
    ICDecl      *FindDecl(id *idptr, ste *steptr);
    
    int         InGlobalScope();

    void        PushScope();
    ste         *PopScope();
    
    id          *Enter(int lextype, char *str, int len);
};

class ICSoundFile : public ICObject
{
  protected:
    FILE        *sourceFile;
    ICKeyMap    *keymap;
    ICWave      *wave;
    char        *fileName;
    ICSound     *sound;
    long        dataOffset;
    
  public:
    ICSoundFile(char *name);

    ICKeyMap    *GetKeyMap() { return keymap;}
    ICWave      *GetWaveTable() { return wave; }
    char        *GetName() {return fileName; }
    ICSound     *GetSound() { return sound; }
    
    virtual void Assemble();
    virtual void Print();
    virtual void Prune(void);
    virtual void Write(FILE *file);
};



//----------------------------------------------------------------------
// sequence file stuff
//----------------------------------------------------------------------

#define  IC_NOTE_TYPE   1
#define  IC_PROG_TYPE   2

class ICSeq
{
  protected:
    char         *seqFname; /* the name of the file */
    FILE         *seqFile;  /* the file pointer */
    ALSeqFile    *seqBank;  /* the data pointer */
    char         ChanProgMap[16];    
  public:
    void           loadFile(char *fname);
    void           ResetChanMap(void);
    virtual int    SetSeq(int number);
    virtual int    NextSeqNote(void);
#ifdef OLD_STUFF
    virtual int    NextSeqNote(ICSeqNote /* *theNote */);
#endif
};

//typedef struct {
//    u8  channel;
//    u8  key;
//} ICCNote;

class ICCSeq : public ICSeq
{
  protected:
    ALCMidiHdr  *base;             /* ptr to start of sequence data         */
    int         validTracks;       /* set of flags, showing valid tracks    */
    char        *curLoc[16];       /* ptr to current track location,        */
                                   /* may point to next event, or may point */
                                   /* to a backup code                      */
    char        *curBUPtr[16];     /* ptr to next event if in backup mode   */
    char        curBULen[16];      /* if > 0, then in backup mode           */
    char        lastStatus[16];    /* for running status                    */
//    ICCNote   nextNote[16];      /* Note buffers, one for each track      */

    int         readVarLen(int track);
    char        getTrackByte(int track);
    
  public:
    virtual int SetSeq(int number);
    virtual int NextSeqNote(char *program, char *key);
};

class ICMSeq : public ICSeq
{
  protected:
    char    *seqPtr;
    int     validSeq;
    char    lastStatus;

    char    read8(void);
    short   read16(void);
    int     read32(void);
    int     readVarLen(void);

  public:
    virtual int SetSeq(int number);
    virtual int NextSeqNote(char *program, char *key);
};