Man, why won't they ever fix the sound memblocks??
Converting a sound
to a memblock works fine with most files I've tried, but if you're to convert them back, it must be 8bit mono, or the program will crash.
Anyways, I think I'll have to write a plugin for this; the image memblock approach wasn't very quick either.
Here's the code (uncommented though; I hope it's not too unclear)
function makeWaveformImage(sound as dword, img as dword, width as word, height as word, col as dword)
local mem as dword : mem = findFreeMemblock(0)
local memImg as dword : memImg = findFreeMemblock(mem)
local amp as float
local size as dword
local offset as dword
local channels as byte
local samplesPerSec as dword
local bytesPerSample as dword
local oldX as dword
local oldAmp as float
make memblock from sound mem, sound
make memblock memImg, 12 + (4 * width * height)
write memblock dword memImg, 0, width
write memblock dword memImg, 4, height
write memblock dword memImg, 8, 32
size = get memblock size(mem)
offset = 28 + memblock dword(mem, 24)
channels = memblock dword(mem, 4)
samplesPerSec = memblock dword(mem, 8)
bytesPerSample = memblock dword(mem, 20) / 8
for x = 0 to (size - offset - bytesPerSample) step bytesPerSample
amp = readMemblockBytes(mem, x + offset, bytesPerSample) / (256.0 ^ bytesPerSample)
if int(((x * 1.0) / (size - bytesPerSample)) * width) <> oldX or amp <> oldAmp
if amp > 0.50
memblockPixel(memImg, ((x * 1.0) / (size - bytesPerSample)) * width , -(height / 2.0) + (amp * height), col)
else
memblockPixel(memImg, ((x * 1.0) / (size - bytesPerSample)) * width , (height / 2.0) + (amp * height), col)
endif
endif
oldX = int(((x * 1.0) / (size - bytesPerSample)) * w)
oldAmp = amp
next x
make image from memblock img, memImg
delete memblock mem
delete memblock memImg
endfunction
function findFreeMemblock(start as byte)
for i = (start + 1) to 255
if not memblock exist(i) then exitfunction i
next i
endfunction 0
function memblockPixel(mem as byte, x as dword, y as dword, col as dword)
local w, h as dword
w = memblock dword(mem, 0)
h = memblock dword(mem, 4)
local location as dword
location = (x * 4) + (y * w * 4)
write memblock dword mem, 12 + location, col
endfunction
function readMemblockBytes(mem as byte, pos as dword, bytes as byte)
select bytes
case 1
value = memblock byte(mem, pos)
endcase
case 2
value = (memblock byte(mem, pos + 1) * 256) + memblock byte(mem, pos)
endcase
case 3
value = (memblock byte(mem, pos + 2) * 256 * 256) + (memblock byte(mem, pos + 1) * 256) + memblock byte(mem, pos)
endcase
case 4
value = (memblock byte(mem, pos + 3) * 256 * 256 * 256) + (memblock byte(mem, pos + 2) * 256 * 256) + (memblock byte(mem, pos + 1) * 256) + memblock byte(mem, pos)
endcase
endselect
endfunction value
"I kören hörs de brummande busarna Björnligan och Gondolen"