spell-cards.love/0031-save_wav

20 lines
637 B
Plaintext

save_wav = function(filename, sounddata)
-- credit to Intas and zorg on the LÖVE forum <3
-- https://love2d.org/forums/viewtopic.php?t=86173
local samples = {}
for i = 0, sounddata:getSampleCount() - 1 do
local sample = sounddata:getSample(i)
local n
local to16bit = sample * 32767
if (to16bit > 0) then
n = math.floor(math.min(to16bit, 32767))
else
n = math.floor(math.max(to16bit, -32768))
end
table.insert(samples, n)
end
local w = wav.create_context(filename, "w")
w.init(sounddata:getChannelCount(), sounddata:getSampleRate(), sounddata:getBitDepth())
w.write_samples_interlaced(samples)
w.finish()
end