FlatInstrumentTest.c

 
#include <QuickDraw.h>
#include <types.h>
#include <Resources.h>
#include <Memory.h>
#include <math.h>
#include "FlatInstrumentTest.h"
 
 
 
 
static void FillInFlatFieldsMostly(SingleFlatInstrument *f)
/*
 * Fill in fields, but not the snd fields
 */
    {
    short i;
 
    f->z1 = 0;
    f->z2 = 0;
    f->z3 = 0;
    f->z4 = 0;
    for(i = 0; i < 12; i++)
        f->z[i] = 0;
 
    f->tone.synthesizerType = 'ss  ';
    f->tone.synthesizerName[0] = 0;
    f->tone.instrumentName[0] = 0;
    f->tone.instrumentNumber = 0;
    f->tone.gmNumber = 1;
 
    f->quality = 5;
    f->transpose = 1;
    f->volumeAttackRate = 10;
    f->volumeDecayRate = 400;
    f->volumeSustainLevel = 38174;
    f->volumeNaturalDecayRate = 64640;
    f->volumeReleaseRate = 250;
 
    f->k1 = 1;
    f->k127 = 127;
    f->maxInterpolation = 0;
    f->k124 = 124;
    }
 
 
 
 
FlatInstrument *CreateFlatInstrument(void)
    {
    SingleFlatInstrument *f;
    long i;
    #define kSampleLength 40000
 
    f = (SingleFlatInstrument *)
            NewPtrClear(sizeof(SingleFlatInstrument) + kSampleLength);
 
 
    FillInFlatFieldsMostly(f);
 
    /*
     * Fill in the 'snd '
     */
 
    f->k0x00010001 = 0x00010001;
    f->k0x00050000 = 0x00050000;
    f->k0x00A00001 = 0x00A00001;
    f->k0x80510000 = 0x80510000;
    f->k0x00000014 = 0x00000014;
    f->z5 = 0;
 
 
    #define kSampleLength 40000
 
    f->sampleLength = kSampleLength;
    f->sampleRate = 0x56ee8ba3;
    f->loopStart = kSampleLength - 2 - 2044;
    f->loopEnd = kSampleLength - 2;
    f->midiKey = 50;
 
    for(i = 0; i < 256; i++)
        f->sample[i] = Random();
 
    for(i = 256; i < kSampleLength; i++)
        f->sample[i] = (f->sample[i-256] + f->sample[i-255] + 1) >> 1;
 
#if 0
    for(i = 0; i < kSampleLength; i++)
        {
        double a,b,c;
        long k;
 
        a = i;
        a = a/30;
        b = i;
        b = b / 30 + 8 * sin(b/19);
 
        c = i;
        c = c * c / 4000.0  ;
 
        a = sin(a) * sin(b) * 127 + 128;
//      a = sin(a) * sin(b) * sin(c) * 127 + 128;
        k = a;
 
        f->sample[i] = k;
        }
#endif
 
    f->size = ((char *)(&f->sample[i])) - ((char *)(&f->size));
 
    return (FlatInstrument *)f;
    }
 
 
FlatInstrument *CreateFlatInstrumentFromSnd(short id)
    {
    Handle snd;
    SingleFlatInstrument *f;
    long sndSize,flatSize;
 
    f = 0;
    snd = GetResource('snd ',id);
    
    if(!snd)
        goto goHome;
 
    sndSize = GetHandleSize(snd);
    flatSize = sndSize + kFlatInstrumentNoSndSize;
    f = (SingleFlatInstrument *)NewPtrClear(flatSize);
    if(!f)
        goto goHome;
    FillInFlatFieldsMostly(f);
 
    BlockMove(*snd,&f->k0x00010001,sndSize);
    f->size = flatSize - sizeof(ToneDescription);
 
    ReleaseResource(snd);
 
goHome:
    return (FlatInstrument *)f;
    }