Ok, I've updated the function. The coding is a bit more efficent in places and also should be a little more stable than before (fingers crossed). I think the samples now draw the right way up, not that is critical.
Also I've added an extra draw mode, bar, which draws the sound more complete than lines or dots, well, I think it looks better
function DrawMonoSnd(left,top,right,bottom,block,startpercent#,endpercent#,bckcol,wavcol,drawmode)
` function for drawing standard 8 and 16 bit WAV mono sounds
` left, top, right, bottom = boxed area to draw sound
` block = number of memblock containing sound
` startpercent# = where to draw from in sound data
` endpercent# = where to draw to in sound data
` bckcol = background color
` wavcol = wav color
` drawmode = 0 for dot, 1 for line, 2 for bars
width# = (right - left) + 1 : height# = (bottom - top) + 1
centerline = top + (height#/2) : vscale# = height# / 256.0
` draw background and set wav color
ink bckcol,0 : box left,top,right,bottom
ink wavcol,0
` exit now if there is no memblock
if memblock exist(block) = 0 then exitfunction
` check for 8 or 16 bit sounds
bps = memblock dword(block,0)
frq = memblock dword(block,4)
bs = (bps / frq) / 8
` exit now if sound is neither 8 or 16 bit
if (bs <> 1) and (bs <> 2) then exitfunction
` check percent parameters
if startpercent# = endpercent#
startpercent# = 0.0 : endpercent# = 100.0
else
if startpercent# > endpercent#
t# = startpercent#
startpercent# = endpercent#
endpercent# = t#
endif
endif
if startpercent# < 0.0 then startpercent# = 0.0
if endpercent# > 100.0 then endpercent# = 100.0
` get byte positions from startpercent and endpercent
sndsize = get memblock size(block) - 12
startpos = (sndsize / 100.0) * startpercent#
endpos = (sndsize / 100.0) * endpercent#
drawrange = (endpos - startpos) + 1
` if sound is 16 bit then startpos and endpos must be even
if bs = 2
if (startpos mod 2) then inc startpos,1
if (endpos mod 2) then dec endpos,1
endif
inc startpos, 12 : inc endpos, 12
x# = left
xd# = (width#-2) / (drawrange / bs)
` draw sound
for pos = startpos to (endpos-1) step bs
if bs = 1
v = 128 - memblock byte(block, pos)
else
v = memblock word(block, pos) - 32768
if v > 0 then v = 32768 - v else v = -32768 - v
v = v / 256
endif
sx = int(x#)
sy = centerline + (v * vscale#)
select drawmode
case 2
if pos > startpos
if sy < centerline
box lastsx,sy,sx+1,centerline+1
else
box lastsx,centerline,sx+1,sy+1
endif
endif
lastsx = sx
endcase
case 1
if sx <> lastsx
if pos > startpos then line lastsx,lastsy,sx,sy
lastsx = sx : lastsy = sy
endif
endcase
case default
if sx <> lastsx
box sx,sy,sx+1,sy+1
lastsx = sx
endif
endcase
endselect
x# = x# + xd#
next pos
endfunction
...and a small demo showing the new draw mode.
sync on : sync rate 40
load sound "your_sound.wav",1
make memblock from sound 1,1
ink rgb(255,255,255),0 : text 10,0,"Dot"
DrawMonoSnd(10,16,630,144,1,0,100,rgb(128,128,128),rgb(255,255,0),0)
ink rgb(255,255,255),0 : text 10,148,"Line"
DrawMonoSnd(10,164,630,292,1,0,100,rgb(128,128,128),rgb(255,255,0),1)
ink rgb(255,255,255),0 : text 10,296,"Bar"
DrawMonoSnd(10,312,630,440,1,0,100,rgb(128,128,128),rgb(255,255,0),2)
` This double sync thing for Patch 4 is ugly :(
sync : sync
wait key
delete memblock 1
delete sound 1
end
Programming anything is an art, and you can't rush art.
Unless your name is Bob Ross, then you can do it in thirty minutes.