The noise and phaser buffers are not longer exposed as instance members but rather created every generation

This commit is contained in:
nucular 2014-06-10 14:23:24 +02:00
parent 0540c95b95
commit c217c68ea3
2 changed files with 21 additions and 27 deletions

View File

@ -115,8 +115,6 @@ function playSound()
source:stop() source:stop()
end end
sound:resetBuffers()
local t = love.timer.getTime() local t = love.timer.getTime()
local tab = sound:generateTable(sfxr.FREQ_44100, sfxr.BITS_FLOAT) local tab = sound:generateTable(sfxr.FREQ_44100, sfxr.BITS_FLOAT)
t = love.timer.getTime() - t t = love.timer.getTime() - t

View File

@ -25,7 +25,7 @@ local sfxr = {}
-- Constants -- Constants
sfxr.VERSION = "0.0" sfxr.VERSION = "0.0.1"
sfxr.SQUARE = 0 sfxr.SQUARE = 0
sfxr.SAWTOOTH = 1 sfxr.SAWTOOTH = 1
@ -123,17 +123,12 @@ function sfxr.Sound:__init()
self.lowpass = {} self.lowpass = {}
self.highpass = {} self.highpass = {}
-- Phaser and noise buffers
self.phaserbuffer = {}
self.noisebuffer = {}
-- These won't be affected by Sound.resetParameters() -- These won't be affected by Sound.resetParameters()
self.supersamples = 8 self.supersamples = 8
self.volume.master = 0.5 self.volume.master = 0.5
self.volume.sound = 0.5 self.volume.sound = 0.5
self:resetParameters() self:resetParameters()
self:resetBuffers()
end end
function sfxr.Sound:resetParameters() function sfxr.Sound:resetParameters()
@ -171,17 +166,6 @@ function sfxr.Sound:resetParameters()
self.highpass.sweep = 0.0 self.highpass.sweep = 0.0
end end
function sfxr.Sound:resetBuffers()
-- Reset the sample buffers
for i=1, 1025 do
self.phaserbuffer[i] = 0
end
for i=1, 33 do
self.noisebuffer[i] = random(-1, 1)
end
end
function sfxr.Sound:generate(freq, bits) function sfxr.Sound:generate(freq, bits)
-- Basically the main synthesizing function, yields the sample data -- Basically the main synthesizing function, yields the sample data
@ -196,6 +180,18 @@ function sfxr.Sound:generate(freq, bits)
local square_duty, square_slide local square_duty, square_slide
local chg_mod, chg_time, chg_limit local chg_mod, chg_time, chg_limit
local phaserbuffer = {}
local noisebuffer = {}
-- Reset the sample buffers
for i=1, 1025 do
phaserbuffer[i] = 0
end
for i=1, 33 do
noisebuffer[i] = random(-1, 1)
end
local function reset() local function reset()
-- subtract 0.017 to match the pitch of the original -- subtract 0.017 to match the pitch of the original
fperiod = 100 / ((self.frequency.start - 0.017)^2 + 0.001) fperiod = 100 / ((self.frequency.start - 0.017)^2 + 0.001)
@ -263,7 +259,7 @@ function sfxr.Sound:generate(freq, bits)
-- Yay, the main closure -- Yay, the main closure
local function iter() local function next()
-- 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
@ -350,7 +346,7 @@ function sfxr.Sound:generate(freq, bits)
phase = phase % period phase = phase % period
if self.wavetype == sfxr.NOISE then if self.wavetype == sfxr.NOISE then
for i = 1, 33 do for i = 1, 33 do
self.noisebuffer[i] = random(-1, 1) noisebuffer[i] = random(-1, 1)
end end
end end
end end
@ -374,7 +370,7 @@ function sfxr.Sound:generate(freq, bits)
sample = math.sin(fp * 2 * math.pi) sample = math.sin(fp * 2 * math.pi)
elseif self.wavetype == sfxr.NOISE then elseif self.wavetype == sfxr.NOISE then
sample = self.noisebuffer[trunc(phase * 32 / period) % 32 + 1] sample = noisebuffer[trunc(phase * 32 / period) % 32 + 1]
end end
-- Apply the lowpass filter to the sample -- Apply the lowpass filter to the sample
@ -398,8 +394,8 @@ function sfxr.Sound:generate(freq, bits)
-- Apply the phaser to the sample -- Apply the phaser to the sample
self.phaserbuffer[bit.band(ipp, 1023) + 1] = sample phaserbuffer[bit.band(ipp, 1023) + 1] = sample
sample = sample + self.phaserbuffer[bit.band(ipp - iphase + 1024, 1023) + 1] sample = sample + phaserbuffer[bit.band(ipp - iphase + 1024, 1023) + 1]
ipp = bit.band(ipp + 1, 1023) ipp = bit.band(ipp + 1, 1023)
-- Accumulation and envelope application -- Accumulation and envelope application
@ -417,7 +413,7 @@ function sfxr.Sound:generate(freq, bits)
second_sample = not second_sample second_sample = not second_sample
if freq == sfxr.FREQ_22050 and second_sample then if freq == sfxr.FREQ_22050 and second_sample then
-- hah! -- hah!
local nsample = iter() local nsample = next()
if nsample then if nsample then
return (ssample + nsample) / 2 return (ssample + nsample) / 2
else else
@ -435,7 +431,7 @@ function sfxr.Sound:generate(freq, bits)
end end
end end
return iter return next
end end
function sfxr.Sound:getEnvelopeLimit(freq) function sfxr.Sound:getEnvelopeLimit(freq)
@ -919,7 +915,7 @@ function sfxr.Sound:save(f, compressed)
code = code .. string.format("%s=%s;", name, obj) code = code .. string.format("%s=%s;", name, obj)
end end
elseif type(obj) == "table" and name ~= "phaserbuffer" and name ~= "noisebuffer" then elseif type(obj) == "table" then
local spacing = compressed and "" or "\n" .. string.rep(" ", #keys - 1) local spacing = compressed and "" or "\n" .. string.rep(" ", #keys - 1)
code = code .. spacing .. string.format("%s={", name) code = code .. spacing .. string.format("%s={", name)