mirror of
https://github.com/nucular/sfxrlua.git
synced 2024-12-24 18:44:20 +00:00
Added optional frequency and bits arguments to stuff
This commit is contained in:
parent
db899ddb3a
commit
47299dfa0c
59
sfxr.lua
59
sfxr.lua
@ -30,6 +30,12 @@ sfxr.SAWTOOTH = 1
|
|||||||
sfxr.SINE = 2
|
sfxr.SINE = 2
|
||||||
sfxr.NOISE = 3
|
sfxr.NOISE = 3
|
||||||
|
|
||||||
|
sfxr.FREQ_44100 = 44100
|
||||||
|
sfxr.FREQ_22050 = 22050
|
||||||
|
sfxr.BITS_FLOAT = 0
|
||||||
|
sfxr.BITS_16 = 16
|
||||||
|
sfxr.BITS_8 = 8
|
||||||
|
|
||||||
-- Utilities
|
-- Utilities
|
||||||
|
|
||||||
local function trunc(n)
|
local function trunc(n)
|
||||||
@ -139,9 +145,14 @@ function sfxr.Sound:resetBuffers()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function sfxr.Sound:generate()
|
function sfxr.Sound:generate(freq, bits)
|
||||||
-- Basically the main synthesizing function, yields the sample data
|
-- Basically the main synthesizing function, yields the sample data
|
||||||
|
|
||||||
|
freq = freq or sfxr.FREQ_44100
|
||||||
|
bits = bits or sfxr.BITS_FLOAT
|
||||||
|
assert(freq == sfxr.FREQ_44100 or freq == sfxr.FREQ_22050, "Invalid freq argument")
|
||||||
|
assert(bits == sfxr.BITS_FLOAT or bits == sfxr.BITS_16 or bits == sfxr.BITS_8, "Invalid bits argument")
|
||||||
|
|
||||||
-- Initialize ALL the locals!
|
-- Initialize ALL the locals!
|
||||||
local fperiod, maxperiod
|
local fperiod, maxperiod
|
||||||
local slide, dslide
|
local slide, dslide
|
||||||
@ -176,7 +187,7 @@ function sfxr.Sound:generate()
|
|||||||
end
|
end
|
||||||
reset()
|
reset()
|
||||||
|
|
||||||
local phase = 0
|
local second_sample = false
|
||||||
|
|
||||||
local env_vol = 0
|
local env_vol = 0
|
||||||
local env_stage = 1
|
local env_stage = 1
|
||||||
@ -215,7 +226,7 @@ function sfxr.Sound:generate()
|
|||||||
|
|
||||||
-- Yay, the main closure
|
-- Yay, the main closure
|
||||||
|
|
||||||
return function()
|
local function iter()
|
||||||
-- Repeat maybe
|
-- Repeat maybe
|
||||||
rep_time = rep_time + 1
|
rep_time = rep_time + 1
|
||||||
if rep_limit ~= 0 and rep_time >= rep_limit then
|
if rep_limit ~= 0 and rep_time >= rep_limit then
|
||||||
@ -364,29 +375,51 @@ function sfxr.Sound:generate()
|
|||||||
-- Hard limit
|
-- Hard limit
|
||||||
ssample = clamp(ssample, -1, 1)
|
ssample = clamp(ssample, -1, 1)
|
||||||
|
|
||||||
-- Aaaand finally
|
-- Frequency conversion
|
||||||
|
second_sample = not second_sample
|
||||||
|
if freq == sfxr.FREQ_22050 and second_sample then
|
||||||
|
-- hah!
|
||||||
|
return iter()
|
||||||
|
end
|
||||||
|
|
||||||
|
-- bit conversions
|
||||||
|
if bits == sfxr.BITS_FLOAT then
|
||||||
return ssample
|
return ssample
|
||||||
|
elseif bits == sfxr.BITS_16 then
|
||||||
|
return trunc(ssample * 32000) % (256*256)
|
||||||
|
else
|
||||||
|
return ssample * 127 + 128
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function sfxr.Sound:getEnvelopeLimit()
|
return iter
|
||||||
|
end
|
||||||
|
|
||||||
|
function sfxr.Sound:getEnvelopeLimit(freq)
|
||||||
local env_length = {self.envelope.attack^2 * 100000,
|
local env_length = {self.envelope.attack^2 * 100000,
|
||||||
self.envelope.sustain^2 * 100000,
|
self.envelope.sustain^2 * 100000,
|
||||||
self.envelope.decay^2 * 100000}
|
self.envelope.decay^2 * 100000}
|
||||||
|
local limit = trunc(env_length[1] + env_length[2] + env_length[3] + 2)
|
||||||
|
|
||||||
return trunc(env_length[1] + env_length[2] + env_length[3] + 2)
|
if freq == nil or freq == sfxr.FREQ_44100 then
|
||||||
|
return limit
|
||||||
|
elseif freq == sfxr.FREQ_22050 then
|
||||||
|
return math.ceil(limit / 2)
|
||||||
|
else
|
||||||
|
error("Invalid freq argument")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function sfxr.Sound:getLimit()
|
function sfxr.Sound:getLimit(freq)
|
||||||
return self:getEnvelopeLimit()
|
return self:getEnvelopeLimit(freq)
|
||||||
end
|
end
|
||||||
|
|
||||||
function sfxr.Sound:generateTable()
|
function sfxr.Sound:generateTable(freq, bits)
|
||||||
local t = {}
|
local t = {}
|
||||||
t[self:getLimit()] = 0
|
t[self:getLimit(freq)] = 0
|
||||||
|
|
||||||
local i = 1
|
local i = 1
|
||||||
for v in self:generate() do
|
for v in self:generate(freq, bits) do
|
||||||
if not v then
|
if not v then
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
@ -398,8 +431,8 @@ function sfxr.Sound:generateTable()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function sfxr.Sound:generateSoundData(freq, bits)
|
function sfxr.Sound:generateSoundData(freq, bits)
|
||||||
local tab = self:generateTable()
|
freq = freq or sfxr.FREQ_44100
|
||||||
|
local tab = self:generateTable(freq, sfxr.BITS_FLOAT)
|
||||||
local data = love.sound.newSoundData(#tab, freq, bits, 1)
|
local data = love.sound.newSoundData(#tab, freq, bits, 1)
|
||||||
|
|
||||||
for i = 0, #tab - 1 do
|
for i = 0, #tab - 1 do
|
||||||
|
Loading…
Reference in New Issue
Block a user