wavefile.c 15.3 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
#include <libaudio.h> 
#include <stdio.h>
#include <stdlib.h>
#include <bstring.h>
#include <string.h>
#include <u64gio.h>
#include <unistd.h>
#include "midiApp.h"
#include "midiDmon.h"
#include "midiGlobals.h"

#ifdef SYSEX_IMPL

#include <sysex.h>
#include <synthcommon.h>



#define IFF_ID_FORM             'FORM'
#define IFF_ID_AIFC             'AIFC'
#define IFF_ID_AIFF             'AIFF'
#define IFF_ID_COMM             'COMM'
#define IFF_ID_SSND             'SSND'
#define IFF_ID_INST             'INST'
#define IFF_ID_APPL             'APPL'
#define IFF_ID_MARK             'MARK'
#define CODE_NAME               "VADPCMCODES"
#define LOOP_NAME               "VADPCMLOOPS"
#define VERSION                 1

#define NoLooping               0
#define ForwardLooping          1
#define ForwardBackwardLooping  2

typedef struct {
    unsigned long       ckID;
    long                ckSize;
} ChunkHeader;

typedef struct {
    unsigned long       ckID;
    long                ckSize;
    unsigned long       formType;
} Chunk;

typedef struct {
    unsigned long       offset;
    unsigned long       blockSize;
} SoundDataChunk;

typedef struct {
    short               numChannels;
    unsigned short      numFramesH;
    unsigned short      numFramesL;
    short               sampleSize;
    char                sampleRate[10];
    unsigned short      compressionTypeH;        /* Separated into HL to prevent alignment */
    unsigned short      compressionTypeL;        /* note: string follows */
} CommonChunk;

typedef short MarkerID;

typedef struct {
    MarkerID            id;
    unsigned short      positionH;              /* Separated into HL to prevent alignment */
    unsigned short      positionL;              /* note: pstring follows */
} Marker;

int lookupMarker(long *sample, MarkerID loopPoint, Marker *markers, int nmarkers);

typedef struct {
    short               playMode;
    MarkerID            beginLoop;
    MarkerID            endLoop;
} Loop;

typedef struct {
    char                baseNote;
    char                detune;
    char                lowNote;
    char                highNote;
    char                lowVelocity;
    char                highVelocity;
    short               gain;
    Loop                sustainLoop;
    Loop                releaseLoop;
} InstrumentChunk;

extern int  errno;

int     gLoopCt,gBookCt;

char *ReadPString(FILE *ifile);

int ReadAIFCFile(char *fname,ALWaveTable *wvTblPtr)
{
    int         i,num,mirrorSize,done=0,retVal;
    long        waveLen,offset,readLen;
    long        skip,applSig;
    ChunkHeader Header;
    Chunk       FormChunk;
    CommonChunk comm;
    char        *ChunkName;
    FILE        *sourceFile;
    u8          *smpBuf;
    ALADPCMloop *loopPtr;
    ALRawLoop   *rawLoopPtr;
    ALADPCMBook *bookPtr;
    short       order;
    short       npredictors;
    short       version;
    short       nloops;
    short       nMarkers;
    long        start;
    long        end;
    int         bookSize;                            
    ALSymObj    *symObjPtr;
    Marker              *markers;
    SoundDataChunk      ssndData;
    InstrumentChunk     inst;
    char                loopName[16];
    char                bookName[16];
    u32                 numBytes,smpOffset;
    unsigned long       compressionType;

    sourceFile = fopen(fname, "r");
    if(!sourceFile)
    {
        fprintf(stderr,"%s: Unable to open sound file %s\n",gAppName,fname);
        fprintf(stderr,"%s: errno = %d\n",gAppName,errno);
        return -1;
    }

    fread(&FormChunk, sizeof(FormChunk), 1, sourceFile);
    
    if ((FormChunk.ckID != IFF_ID_FORM) ||
        ((FormChunk.formType != IFF_ID_AIFC) && (FormChunk.formType != IFF_ID_AIFF)))
    {
        fprintf(stderr,"%s: %s is not an AIFF-C File\n",gAppName,fname);
        fclose(sourceFile);
        return -1;
    }

    while (!done)
    {
        num = fread(&Header, sizeof(ChunkHeader), 1, sourceFile);
        if (num < 1)
        {
            fclose(sourceFile);
            done = 1;
        }
        
        if(!done)
        {
            skip = (Header.ckSize + 1) & ~1; /* account for pad byte */
        
            switch (Header.ckID)
            {
                case (IFF_ID_COMM):
                    offset = ftell(sourceFile);
                    num = fread (&comm, sizeof(CommonChunk), 1, sourceFile);
                    if (num < 1)
                    {
                        fprintf(stderr,"%s: error reading common chunk\n",gAppName);
                        done = 1;
                    }
                    if (FormChunk.formType != IFF_ID_AIFF)
                        compressionType = comm.compressionTypeH<<16 | comm.compressionTypeL;
                    else
                        compressionType = 'NONE';
                    
                    fseek(sourceFile, offset + skip, SEEK_SET);
                    break;                
                case (IFF_ID_MARK):
                    offset = ftell(sourceFile);
                    fread(&nMarkers, sizeof(short), 1, sourceFile);
                    markers = (Marker *) calloc(nMarkers, sizeof(Marker));
                    for (i=0; i<nMarkers; i++)
                    {
                        fread(&markers[i], sizeof(Marker), 1, sourceFile);
                        ChunkName = ReadPString(sourceFile);        
                        free(ChunkName);            /* Junk the marker name */
                    }
                    fseek(sourceFile, offset + skip, SEEK_SET);
                    break;
                case (IFF_ID_INST):
                    offset = ftell(sourceFile);
                    fread(&inst, sizeof(InstrumentChunk), 1, sourceFile);
                    fseek(sourceFile, offset + skip, SEEK_SET);
                    break;
                case (IFF_ID_SSND):
                    offset = ftell(sourceFile);
                    fread(&ssndData, sizeof (SoundDataChunk), 1, sourceFile);
                    if(ssndData.offset != 0 || ssndData.blockSize !=0)
                    {
                        fprintf(stderr,"%s: AIFC SSND hdr data values != 0",gAppName);
                        fclose(sourceFile);
                        return -1;
                    }
                    readLen = skip - sizeof(SoundDataChunk); /* waveLen must be even */
                    waveLen = (readLen + 7) &~0x00000007;  /* now make it 8 byte aligned */
                
                    if(CheckMemory(0,0,waveLen) != MEMORY_OK)
                    {
                        fprintf(stderr,"%s: Not enough memory for samples\n",gAppName);
                        fprintf(stderr,"%s: needed 0x%x bytes, gSamplesCur = 0x%x, waveLen = 0x%x\n",
                                gAppName,gSamplesCur+waveLen,gSamplesCur,waveLen);
                        fclose(sourceFile);
                        return -1;
                    }
                    smpOffset = gSamplesCur;
                    smpBuf = &gSamples[smpOffset];

                    retVal = fread(smpBuf,readLen,1,sourceFile);
                    
                    wvTblPtr->len = readLen;
                    wvTblPtr->base = (u8*)smpOffset;

                    WriteRamRom(smpOffset + SAMPLES_RAMROM_ADDR,smpBuf,waveLen);
                    gSamplesCur += waveLen;
                    fseek(sourceFile, offset + skip, SEEK_SET);
                    break;
                case (IFF_ID_APPL):
                /* make sure only one of each type of chunk */
                    offset = ftell(sourceFile);
                    fread(&applSig, sizeof(applSig), 1, sourceFile);
                    if (applSig == 'stoc')
                    {
                        ChunkName = ReadPString(sourceFile);
                        if (!strcmp(LOOP_NAME, ChunkName))
                        {
                            fread(&version, sizeof(short), 1, sourceFile);
                            if (version != VERSION)
                            {
                                fprintf(stderr,"%s: Non-identical loop chunk versions\n",gAppName);
                                fclose(sourceFile);
                                return -1;
                            }
                            fread(&nloops, sizeof(short), 1, sourceFile);
                            if (nloops > 1)
                            {
                                fprintf(stderr,"%s: Found %d loops, only 1 supported\n",gAppName);
                                nloops = 1;
                            }

                            mirrorSize = sizeof(ALADPCMloop); 
                            if(mirrorSize & 0x00000007)
                                mirrorSize = (mirrorSize+8) & 0xFFFFFFF8;

                            if(CheckMemory(mirrorSize,0,0)!=MEMORY_OK)
                            {
                                fprintf(stderr,"%s: Not enough memory for a LOOP object\n",gAppName);
                                fclose(sourceFile);
                                return -1;
                            }
                            symObjPtr = &gSymObj[gObjCt];
                            gObjCt++;
                            sprintf(loopName,"_Loop%d",gLoopCt++);
                            symObjPtr->SFObj.objectClass = AL_LOOP_CLASS;
                            symObjPtr->SFObj.bankFileOffset = gMirrorCur;
                            symObjPtr->SFObj.stringOffset = AddName(loopName);
                            symObjPtr->SFObj.refCount = 1;
                            symObjPtr->deleteFlag = 0;

                            AddHashName(loopName,symObjPtr);

                            loopPtr = (ALADPCMloop*)&gMirror[gMirrorCur];

                            fread(loopPtr, sizeof(ALADPCMloop),1, sourceFile);

                            WriteBlock(BANK_RDRAM_ADDR + gMirrorCur,(char*)loopPtr,mirrorSize);

                            wvTblPtr->waveInfo.adpcmWave.loop = (ALADPCMloop*)gMirrorCur;

                            gMirrorCur += mirrorSize;
                        }
                        else if (!strcmp(CODE_NAME, ChunkName))
                        {
                            fread(&version, sizeof(short), 1, sourceFile);
                            if (version != VERSION)
                            {
                                fprintf(stderr, "%s: Bad codebook chunk version\n",gAppName);
                                fclose(sourceFile);
                                return -1;
                            }
                        
                            fread(&order, sizeof(short), 1, sourceFile);
                            fread(&npredictors, sizeof(short), 1, sourceFile);
                        
                            bookSize = npredictors*order*ADPCMVSIZE*sizeof(short);

                            mirrorSize = sizeof(ALADPCMBook) + bookSize; 
                            if(mirrorSize & 0x00000007)
                                mirrorSize = (mirrorSize+8) & 0xFFFFFFF8;

                            if(CheckMemory(mirrorSize,0,0)!=MEMORY_OK)
                            {
                                fprintf(stderr,"%s: Not enough memory for a BOOK object\n",gAppName);
                                fclose(sourceFile);
                                return -1;
                            }
                            symObjPtr = &gSymObj[gObjCt];
                            gObjCt++;
                            sprintf(bookName,"_Book%d",gBookCt++);
                            symObjPtr->SFObj.objectClass = AL_BOOK_CLASS;
                            symObjPtr->SFObj.bankFileOffset = gMirrorCur;
                            symObjPtr->SFObj.stringOffset = AddName(bookName);
                            symObjPtr->SFObj.refCount = 1;
                            symObjPtr->deleteFlag = 0;

                            AddHashName(bookName,symObjPtr);

                            bookPtr = (ALADPCMBook*)&gMirror[gMirrorCur];
                            bookPtr->order = order;
                            bookPtr->npredictors = npredictors;
                        
                            fread(&bookPtr->book[0],sizeof(short), bookSize, sourceFile);

                            WriteBlock(BANK_RDRAM_ADDR + gMirrorCur,(char*)bookPtr,mirrorSize); 

                            wvTblPtr->waveInfo.adpcmWave.book = (ALADPCMBook*)gMirrorCur;
                            gMirrorCur += mirrorSize;
                        }
                        free(ChunkName);
                    }
                    fseek(sourceFile, offset + skip, SEEK_SET); /* probably not needed */
                    break;
                default:      /* skip chunks we don't know about */
                    fseek(sourceFile, skip, SEEK_CUR);                
                    break;
            }         /* end of switch */   
        }  /* end of if(!done)  */            
    }   /* end of while(!done)  */
        
    if (compressionType == 'NONE')
    {
        if(comm.sampleSize == 16)
            wvTblPtr->type = AL_RAW16_WAVE;
        else
        {
            fprintf(stderr,"%s: Raw Sample Size is not 16 bit\n",gAppName);
            return(-1);
        }
        if (inst.sustainLoop.playMode == ForwardLooping)
        {
            if (lookupMarker(&start, inst.sustainLoop.beginLoop, markers, nMarkers))
                fprintf(stderr,"%s: Begin loop marker not found\n",gAppName);
            if (lookupMarker(&end, inst.sustainLoop.endLoop, markers, nMarkers))
                fprintf(stderr,"%s: End loop marker not found\n",gAppName);
            
            /* Make and init a loop object */
            mirrorSize = sizeof(ALADPCMloop); 
            if(mirrorSize & 0x00000007)
                mirrorSize = (mirrorSize+8) & 0xFFFFFFF8;
            
            if(CheckMemory(mirrorSize,0,0)!=MEMORY_OK)
            {
                fprintf(stderr,"%s: Not enough memory for a Raw Loop Object\n",gAppName);
                return(-1);
            }
            
            symObjPtr = &gSymObj[gObjCt];
            gObjCt++;
            sprintf(loopName,"_Loop%d",gLoopCt++);
            symObjPtr->SFObj.objectClass = AL_LOOP_CLASS;
            symObjPtr->SFObj.bankFileOffset = gMirrorCur;
            symObjPtr->SFObj.stringOffset = AddName(loopName);
            symObjPtr->SFObj.refCount = 1;
            symObjPtr->deleteFlag = 0;
            
            AddHashName(loopName,symObjPtr);
            
            rawLoopPtr = (ALRawLoop*)&gMirror[gMirrorCur];
            rawLoopPtr->start = start;
            rawLoopPtr->end = end;
            rawLoopPtr->count = -1;
            
            WriteBlock(BANK_RDRAM_ADDR + gMirrorCur,(char*)rawLoopPtr,mirrorSize);

            wvTblPtr->waveInfo.rawWave.loop = (ALRawLoop*)gMirrorCur;
            
            gMirrorCur += mirrorSize;
        }  /* end of if(forward loop)  */
    }  /* end of if(compressionType == 'NONE')  */

    return(0);
}

char *ReadPString(FILE *ifile)
{
    unsigned char c;
    char        *st;

    fread(&c, 1, 1, ifile);
    st = (char *) malloc(c+1);
    fread(st, c, 1, ifile);
    *(st+c) = 0;
    
    /*
     * Read the pad byte if necessary
     */
    if (!(c & 1))
        fread(&c, 1, 1, ifile);
    return st;
}

int lookupMarker(long *sample, MarkerID loopPoint, Marker *markers, int nmarkers)
{
    int     i;
    
    for (i=0; i<nmarkers; i++)
    {
        if (loopPoint == markers[i].id)
        {
            *sample = (markers[i].positionH<<16) + markers[i].positionL;
            return 0;
        }
    }
    return 1;
}

#endif /* SYSEX_IMPL */