home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 2.0 Developer Kit / QuickTime 2.0 Developer Kit.iso / mac / MAC / Programming Stuff / Sample Code / Music Architecture / Embedding Instruments / FlatInstrumentTest.c next >
Encoding:
C/C++ Source or Header  |  1994-07-08  |  2.4 KB  |  147 lines  |  [TEXT/KAHL]

  1.  
  2. #include <QuickDraw.h>
  3. #include <types.h>
  4. #include <Resources.h>
  5. #include <Memory.h>
  6. #include <math.h>
  7. #include "FlatInstrumentTest.h"
  8.  
  9.  
  10.  
  11.  
  12. static void FillInFlatFieldsMostly(SingleFlatInstrument *f)
  13. /*
  14.  * Fill in fields, but not the snd fields
  15.  */
  16.     {
  17.     short i;
  18.  
  19.     f->z1 = 0;
  20.     f->z2 = 0;
  21.     f->z3 = 0;
  22.     f->z4 = 0;
  23.     for(i = 0; i < 12; i++)
  24.         f->z[i] = 0;
  25.  
  26.     f->tone.synthesizerType = 'ss  ';
  27.     f->tone.synthesizerName[0] = 0;
  28.     f->tone.instrumentName[0] = 0;
  29.     f->tone.instrumentNumber = 0;
  30.     f->tone.gmNumber = 1;
  31.  
  32.     f->quality = 5;
  33.     f->transpose = 1;
  34.     f->volumeAttackRate = 10;
  35.     f->volumeDecayRate = 400;
  36.     f->volumeSustainLevel = 38174;
  37.     f->volumeNaturalDecayRate = 64640;
  38.     f->volumeReleaseRate = 250;
  39.  
  40.     f->k1 = 1;
  41.     f->k127 = 127;
  42.     f->maxInterpolation = 0;
  43.     f->k124 = 124;
  44.     }
  45.  
  46.  
  47.  
  48.  
  49. FlatInstrument *CreateFlatInstrument(void)
  50.     {
  51.     SingleFlatInstrument *f;
  52.     long i;
  53.     #define kSampleLength 40000
  54.  
  55.     f = (SingleFlatInstrument *)
  56.             NewPtrClear(sizeof(SingleFlatInstrument) + kSampleLength);
  57.  
  58.  
  59.     FillInFlatFieldsMostly(f);
  60.  
  61.     /*
  62.      * Fill in the 'snd '
  63.      */
  64.  
  65.     f->k0x00010001 = 0x00010001;
  66.     f->k0x00050000 = 0x00050000;
  67.     f->k0x00A00001 = 0x00A00001;
  68.     f->k0x80510000 = 0x80510000;
  69.     f->k0x00000014 = 0x00000014;
  70.     f->z5 = 0;
  71.  
  72.  
  73.     #define kSampleLength 40000
  74.  
  75.     f->sampleLength = kSampleLength;
  76.     f->sampleRate = 0x56ee8ba3;
  77.     f->loopStart = kSampleLength - 2 - 2044;
  78.     f->loopEnd = kSampleLength - 2;
  79.     f->midiKey = 50;
  80.  
  81.     for(i = 0; i < 256; i++)
  82.         f->sample[i] = Random();
  83.  
  84.     for(i = 256; i < kSampleLength; i++)
  85.         f->sample[i] = (f->sample[i-256] + f->sample[i-255] + 1) >> 1;
  86.  
  87. #if 0
  88.     for(i = 0; i < kSampleLength; i++)
  89.         {
  90.         double a,b,c;
  91.         long k;
  92.  
  93.         a = i;
  94.         a = a/30;
  95.         b = i;
  96.         b = b / 30 + 8 * sin(b/19);
  97.  
  98.         c = i;
  99.         c = c * c / 4000.0  ;
  100.  
  101.         a = sin(a) * sin(b) * 127 + 128;
  102. //        a = sin(a) * sin(b) * sin(c) * 127 + 128;
  103.         k = a;
  104.  
  105.         f->sample[i] = k;
  106.         }
  107. #endif
  108.  
  109.     f->size = ((char *)(&f->sample[i])) - ((char *)(&f->size));
  110.  
  111.     return (FlatInstrument *)f;
  112.     }
  113.  
  114.  
  115. FlatInstrument *CreateFlatInstrumentFromSnd(short id)
  116.     {
  117.     Handle snd;
  118.     SingleFlatInstrument *f;
  119.     long sndSize,flatSize;
  120.  
  121.     f = 0;
  122.     snd = GetResource('snd ',id);
  123.     
  124.     if(!snd)
  125.         goto goHome;
  126.  
  127.     sndSize = GetHandleSize(snd);
  128.     flatSize = sndSize + kFlatInstrumentNoSndSize;
  129.     f = (SingleFlatInstrument *)NewPtrClear(flatSize);
  130.     if(!f)
  131.         goto goHome;
  132.     FillInFlatFieldsMostly(f);
  133.  
  134.     BlockMove(*snd,&f->k0x00010001,sndSize);
  135.     f->size = flatSize - sizeof(ToneDescription);
  136.  
  137.     ReleaseResource(snd);
  138.  
  139. goHome:
  140.     return (FlatInstrument *)f;
  141.     }
  142.  
  143.  
  144.  
  145.  
  146.  
  147.