aUCBLogo Demos and Tests / makewav6
to makewav6
starttime=timefine
rate=44100
size=rate*22
wavHeaderType=(list
[ChunkID Word RIFF]
[wavfilesize Int]
[RIFFtype Word WAVE] ;the last item is the string length
[formatChunkID Word fmt\ ]
[formatChunkSize Int 16]
[compressionCode Int16 1]
[NumberOfChannels Int16 1]
(list "SampleRate "Int rate)
(list "BytesPerSecond "Int rate*2)
[BlockAlign Int16 2]
[BitsPerSample Int16 16]
[DataChunkID word data]
[DataChunkSize Int]
)
wavHeader=struct wavHeaderType
wavHeaderSize=SizeOf wavHeader
wavHeaderSizeHalf=int wavHeaderSize/2
wavsize=wavHeaderSize+size*2
wav=Int16Array int wavsize/2
; setReader wav
; wh=readStructBin wavHeaderType
; pr wh
; setReader []
start0=wavHeaderSizeHalf
maxsize=0
length=int rate/6
gentones [2 2 5 2 7 4 7 4 7 4 7 2 5 2 7 2 9 1 7 4 999 4
2 2 5 2 7 4 5 4 2 4 2 2 0 1 2 6 999 6
2 2 5 2 7 4 5 4 0 4 0 2 -2 2 0 2 2 1 0 4 999 4
2 2 5 2 7 4 5 4 0 4 0 2 -2 1 0 6 999 6]
start=start0
moll [-26 2 -27 2 -29 4 -29 4 -29 4 -29 2 -22 2 -29 2 -22 1 -29 4 999 4
-29 2 -26 2 -22 4 -22 4 -22 4 -22 2 -24 1 -22 6 999 6]
GC
dur [-26 2 -27 2 -31 8 -31 8 -31 2 -31 1 -31 4 999 4
-19 2 -22 2 -24 4 -24 4 -24 4 -24 2 -19 1 -24 6 999 6]
wavHeader'wavfilesize=maxsize*2
wavHeader'DataChunkSize=maxsize*2-wavHeaderSize
; pr wavHeader
setWriter wav
typeBin wavHeader
setWriter []
GC
(pr timefine-starttime [seconds])
playWav wav 1
(pr [playWav wav 1])
(pr [playWav wav 1+8])
(pr [playWave [] 0])
playWave [] 0
end
to gentones tlist
start=start0
while [and not empty? tlist not Key?]
[ t=first tlist
tlist=bf tlist
l=first tlist
tlist=bf tlist
if t == 999 [start=start+l*length continueLoop]
f=440*2^(t/12)
phi=rSeqFloatArray 360*1/rate 360*l*length/rate l*length
setItems start+1 wav
(items start+1 start+l*length wav)
+Int16array ( (sin phi*f)-(sin phi*(f+6/l)) )*(Int16Max/4)
start=start+l*length
]
maxsize=max maxsize start
end
to moll tlist
while [and not empty? tlist not Key?]
[ t=first tlist
tlist=bf tlist
l=first tlist
tlist=bf tlist
if t == 999 [start=start+l*length continueLoop]
f=440*2^(t/12)
f2=440*2^((t+7)/12)
f3=440*2^((t+12)/12)
f4=440*2^((t+15)/12)
phi=rSeqFloatArray 360*1/rate 360*l*length/rate l*length
setItems start+1 wav
(items start+1 start+l*length wav)
+Int16array ( (sin phi*f )-(sin phi*(f +6/l))
+(sin phi*f2)-(sin phi*(f2+6/l))
+(sin phi*f3)-(sin phi*(f3+6/l))
+(sin phi*f4)-(sin phi*(f4+6/l)) )*(Int16Max/32)
start=start+l*length
]
maxsize=max maxsize start
end
to dur tlist
while [and not empty? tlist not Key?]
[ t=first tlist
tlist=bf tlist
l=first tlist
tlist=bf tlist
if t == 999 [start=start+l*length continueLoop]
f=440*2^(t/12)
f2=440*2^((t+7)/12)
f3=440*2^((t+12)/12)
f4=440*2^((t+16)/12)
phi=rSeqFloatArray 360*1/rate 360*l*length/rate l*length
setItems start+1 wav
(items start+1 start+l*length wav)
+Int16array ( (sin phi*f )-(sin phi*(f +6/l))
+(sin phi*f2)-(sin phi*(f2+6/l))
+(sin phi*f3)-(sin phi*(f3+6/l))
+(sin phi*f4)-(sin phi*(f4+6/l)) )*(Int16Max/32)
start=start+l*length
]
maxsize=max maxsize start
end
to playWav wav flags
noRefresh
playWave wav flags
i=1
while [and (or (i==1) ((BitAnd flags 8) == 8)) not Key?]
[ t0=timefine
ht
WindowMode
setUpdateGraph false
setFC RGBA 1 1 1 0.3
ds=rate/50
s=start0
k=1
while [and s+ds < maxsize not Key?]
[ ;clean
pu home
fillRect [-402 -300][402 300]
s=int (timefine-t0)*rate
; while [and wav.s > 0 s+ds < maxsize][s=s+1]
; while [and wav.s < 0 s+ds < maxsize][s=s+1]
; while [and wav.s >= wav.(s+1) s+ds < maxsize][s=s+1]
; while [and wav.s < wav.(s+1) s+ds < maxsize][s=s+1]
maxa=-Int16Max
for [x 0 ds]
[ if wav.(s+x) > maxa
[ snew=s+x
maxa=wav.snew
]
]
s=snew
pu setXY -400 wav.s*300/Int16Max
pd
for [x 0 ds]
[ setXY x*800/ds-400 wav.(s+x)*300/Int16Max
]
updateGraph
k=k+10
]
i=i+1
]
if Key? [playWave [] 0]
refresh
end