Source/WaveTableSynthPlay.c

/*
    WaveTableSynthPlay  12:32:47 PM  10/13/92 ¥ Brigham Stevens
    
    Shows how to use the waveTableSynth
    
    InitDialogs is called only to install crash handler in case
    no Debugger exists.
 
    BuildProgram WaveTableSynthPlay
*/
 
#include <Dialogs.h>
#include <Sound.h>
#include <Memory.h>
 
#define WAVE_SIZE (512*50)
 
void WaveTableSynthPlay();
 
void WaveTableSynthPlay()
{
    SndChannelPtr   chan;
    SndCommand      mycmd;
    unsigned char   *wave;
    unsigned char   *waveSurfer;
    short           soundByte;
    short           change = 0;
    short           err;
    long            tickTime;
 
    /* Nab some memory for a waveTable to build our sound */
    wave = (unsigned char *) NewPtr (WAVE_SIZE);
    if(!wave) {
        DebugStr("\pNewPtr failed to make wave...");
        return;
    }
 
    /* create a channel linked with the waveTableSynth */       
    chan = nil;
    err = SndNewChannel (&chan, waveTableSynth, 0x07, nil);
    if (err) {
        DebugStr("\p error SndNewChannel [1]");
        goto bail;
    }
    
    /* generate a cool wave */
    waveSurfer = wave;
    for (soundByte = 0; soundByte <= WAVE_SIZE; ++soundByte) {
        *waveSurfer++ = change++;
        if(change > WAVE_SIZE / 4) {
            change += 44;
        }
    }
    
    /* install the wave */
    mycmd.cmd = waveTableCmd;
    mycmd.param1 = WAVE_SIZE;
    mycmd.param2 = (long)wave;
    
    err = SndDoImmediate (chan, &mycmd);
    if (err) {
        DebugStr("\p error SndDoImmediate [1]");
        goto bail;
    }
 
    /* play the wave */
    mycmd.cmd = freqCmd;
    mycmd.param1 = 0;
    mycmd.param2 = 20;
    
    err = SndDoImmediate (chan, &mycmd);
    if (err) {
        DebugStr("\p error SndDoImmediate [2]");
        goto bail;
    }
 
    Delay(180,&tickTime);
    
    /* Now dump the channel */
    err = SndDisposeChannel (chan,false);
    if (err) {
        DebugStr("\p error SndDisposeChannel [1]");
        goto bail;
    }
 
bail:
    DisposePtr((char *)wave);
}
 
 
 
void main()
{
    WaveTableSynthPlay();
}