Code: Select all
local wave = require "wave"
local sData = love.sound.newSoundData "Example.ogg"
wave.save{ filename = "Example.wav", sound = sData, overwrite = true, callback = function() love.graphics.setColor(255, 0, 0) end}
There is also a more complex way to use it:
Code: Select all
local t = {}
t.bitDepth = 16
t.channels = 1
t.sampleRate = 48000
local BIRATEPI = t.sampleRate/math.pi/2
local sine = function(s, v, a) return a*math.sin(s*v/BIRATEPI) end
for i = 1, t.sampleRate*15 do
t[i] = sine(i, 220, 2)
end
local sin = wave.save{ sound = t, filename = "Sine.wav", callback = function() love.system.openURL(love.filesystem.getSaveDirectory()) end, overwrite = true, exceptionMode = "normalize" }
love.update = function(dt)
wave.update(dt)
end
love.draw = function()
love.graphics.print("This is not a fancy demo. If everything is red, the .ogg file was saved as a wav file.\nWhen the following's done, a folder will automagically be opened.", 0, 0)
local p = sin:getPercent()
if p then
love.graphics.print(p .. "%", 0, 32)
end
local s = sin:getStatus()
if s then
love.graphics.print(s, 0, 48)
end
if sin:getError() then
love.graphics.print(sin:getError(), 0, 64)
end
end
wave.save can receive normal arguments (filename, soundData or table, callback, overwrite, exceptionMode) or a table {filename = string, sound = soundData or table, callback = func/false, overwrite = true, exceptionMode = string}.
The filename is, of course, the name/path the file will end up into in the save directory.
Sound is either a SoundData or the aforementioned table.
Callback is a function that will be called if the file is succesfully saved or if the thread encountered an error. It gets one argument, a boolean which is true if the thread's done and false if it errored. If the callback itself is false (not nil!), wave.save will behave synchronously and block everything till it's done. The function itself will return true if the file got saved correctly or nil, error if the thread encountered an error.
Overwrite is a boolean that tells Wave wether to overwrite a file, if it already exists.
ExceptionMode is the one single reason the API got changed a few hours after first release. It only makes sense if you're saving a table and it can have three values:
- "error": if any single sample is greater than 1 or lesser than -1, the thread will halt and Wave won't even try to save. Default if unspecified.
- "clip": if any sample is greater than 1 or lesser than -1, it becomes 1 or -1 respectively. Standard clipping.
- "normalize": normalizes the whole table, dividing every sample by the absolute value of the biggest one (thus making it 1 or -1 and every other one smaller)
EDIT: A point has been brought up on IRC. Poll!
EDIT2: Poll's over. 0 results, but a good talk on IRC brought a solution. Thanks, z0rg and DesertRock!