'===========================================================================
' Subject: Math Wave Generator                Date: 06-13-03 (  :  )       
'  Author: Thomas Watson                      Code: LIBERTY                
'  Origin: alycewatson@charter.net          Packet: SOUND.ABC
'===========================================================================
'Wave file generator
'by Thomas Watson, May 2003
'
'some neat waveforms to try (first parameter is frequency,
'second parameter is amplitude (cannot exceed 127 in total)
'insert one in the waveform generation loop below
'
'num = soundbyte(500,127,i)
'num = soundbyte(600+sin(i/1800)*15, 127, i)
'num = soundbyte(700,64,i)+soundbyte(705,63,i)      'waves can be superimposed, multiple tones possible
'num = soundbyte(800+mod(i,20),127,i)


if val(Version$)<4 then end
CLS
print "Generating wave..."

global length: length = 2                           'length of wave in seconds
global samplerate: samplerate = 11025               'sound quality, typically 11025, 22050, or 44100
global channels: channels = 1                       '1 for mono, 2 for stereo
global bitspersample: bitspersample = 8             'either 8 or 16
global filename$:filename$=DefaultDir$+"\test.wav"  'destination file name

OPEN filename$ FOR OUTPUT AS #1     'clear the file
CLOSE #1

OPEN filename$ FOR BINARY AS #1
'write file header based of length, sample rate, channels, and bitspersample
dat$ = "RIFF" + littleendian$(ascii$(length * samplerate * channels * bitspersample / 8 + 36, 4))
dat$ = dat$ + "WAVEfmt " + littleendian$(ascii$(16, 4)) + littleendian$(ascii$(1, 2)) + littleendian$(ascii$(channels, 2))
dat$ = dat$ + littleendian$(ascii$(samplerate, 4)) + littleendian$(ascii$(samplerate * bitspersample / 8, 4))
dat$ = dat$ + littleendian$(ascii$(channels * bitspersample / 8, 2)) + littleendian$(ascii$(bitspersample, 2))
dat$ = dat$ + "data" + littleendian$(ascii$(length * samplerate * channels * bitspersample / 8, 4))
print #1, dat$

'write data to file
FOR i = 1 TO length * samplerate * bitspersample * channels / 8
num = soundbyte(600, 127, i)                                         '<----- insert waveform equation here
dat$ = CHR$(num + 128)
print #1, dat$
NEXT i

CLOSE #1
print "Done!"
playwave filename$
END

FUNCTION ascii$(num, leng)  'function to return ascii equivalent of a number
remain = num
ascii$ = ""
b$ = ""
pow=0

while pow<>1
    a = remain
    pow = 1
    WHILE a > 255
        a = INT(remain / (256 ^ pow))
        pow = pow + 1
    WEND
    b$ = b$ + CHR$(a)
    remain = remain - (a * (256 ^ (pow - 1)))
wend

while LEN(b$) <> leng
b$ = CHR$(0) + b$
wend

ascii$ = b$
END FUNCTION

FUNCTION littleendian$(st$)     'function to reverse  byte order
res$ = ""
FOR i = LEN(st$) TO 1 STEP -1
res$ = res$ + MID$(st$, i, 1)
NEXT i
littleendian$ = res$
END FUNCTION

FUNCTION soundbyte(frequency, amplitude, bitnum)     'function to calculate the appropriate byte based on frequency and amplitude
soundbyte = SIN(6.283185 * bitnum / (samplerate * bitspersample * channels / 8 / frequency)) * amplitude
END FUNCTION

function mod(number, divisor)       'modulus for use in waveform generation
    mod=(number)-(int(number/divisor)*divisor)
    end function
