Smaller fixes

This commit is contained in:
nucular 2014-05-07 21:40:53 +02:00
parent 2562f39571
commit db899ddb3a

View File

@ -56,6 +56,14 @@ local function cpypol(a, b)
end end
end end
-- Constructor
function sfxr.newSound(...)
local instance = setmetatable({}, sfxr.Sound)
instance:__init(...)
return instance
end
-- The main Sound class -- The main Sound class
sfxr.Sound = {} sfxr.Sound = {}
@ -107,7 +115,7 @@ function sfxr.Sound:resetParameters()
self.change.amount = 0.0 self.change.amount = 0.0
self.change.speed = 0.0 self.change.speed = 0.0
self.duty.ratio = 0.5 self.duty.ratio = 0.0
self.duty.sweep = 0.0 self.duty.sweep = 0.0
self.phaser.offset = 0.0 self.phaser.offset = 0.0
@ -185,15 +193,15 @@ function sfxr.Sound:generate()
local iphase = math.abs(trunc(phase)) local iphase = math.abs(trunc(phase))
local ltp = 0 local fltp = 0
local ltdp = 0 local fltdp = 0
local ltw = self.lowpass.cutoff^3 * 0.1 local fltw = self.lowpass.cutoff^3 * 0.1
local ltw_d = 1 + self.lowpass.sweep * 0.0001 local fltw_d = 1 + self.lowpass.sweep * 0.0001
local ltdmp = 5 / (1 + self.lowpass.resonance^2 * 20) * (0.01 + ltw) local fltdmp = 5 / (1 + self.lowpass.resonance^2 * 20) * (0.01 + fltw)
ltdmp = clamp(ltdmp, nil, 0.8) fltdmp = clamp(fltdmp, nil, 0.8)
local ltphp = 0 local fltphp = 0
local lthp = self.highpass.cutoff^2 * 0.1 local flthp = self.highpass.cutoff^2 * 0.1
local lthp_d = 1 + self.highpass.sweep * 0.0003 local flthp_d = 1 + self.highpass.sweep * 0.0003
local vib_phase = 0 local vib_phase = 0
local vib_speed = self.vibrato.speed^2 * 0.01 local vib_speed = self.vibrato.speed^2 * 0.01
@ -211,8 +219,8 @@ function sfxr.Sound:generate()
-- 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
reset()
rep_time = 0 rep_time = 0
reset()
end end
-- Update the change time and apply it if needed -- Update the change time and apply it if needed
@ -271,13 +279,12 @@ function sfxr.Sound:generate()
-- Phaser -- Phaser
phase = phase + dphase phase = phase + dphase
iphase = math.abs(trunc(phase)) iphase = clamp(math.abs(trunc(phase)), nil, 1023)
if iphase > 1024 then iphase = 1024 end
-- Lowpass stuff -- Filter stuff
if lthp_d ~= 0 then if flthp_d ~= 0 then
lthp = clamp(lthp * lthp_d, 0.00001, 0.1) flthp = clamp(flthp * flthp_d, 0.00001, 0.1)
end end
-- And finally the actual tone generation and supersampling -- And finally the actual tone generation and supersampling
@ -293,7 +300,7 @@ function sfxr.Sound:generate()
--phase = 0 --phase = 0
phase = phase % period phase = phase % period
if self.waveType == sfxr.NOISE then if self.waveType == sfxr.NOISE then
for i = 1, 32 do for i = 1, 33 do
self.noiseBuffer[i] = random(-1, 1) self.noiseBuffer[i] = random(-1, 1)
end end
end end
@ -301,6 +308,7 @@ function sfxr.Sound:generate()
-- Tone oscillators ahead!!! -- Tone oscillators ahead!!!
-- update the base waveform
local fp = phase / period local fp = phase / period
if self.waveType == sfxr.SQUARE then if self.waveType == sfxr.SQUARE then
@ -317,27 +325,27 @@ function sfxr.Sound:generate()
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[math.floor(phase * 32 / period) + 2] sample = self.noiseBuffer[trunc(phase * 32 / period) % 32 + 1]
end end
-- Apply the lowpass filter to the sample -- Apply the lowpass filter to the sample
local pp = ltp local pp = fltp
ltw = clamp(ltw * ltw_d, 0, 0.1) fltw = clamp(fltw * fltw_d, 0, 0.1)
if self.lowpass.cutoff ~= 1 then if self.lowpass.cutoff ~= 1 then
ltdp = ltdp + (sample - ltp) * ltw fltdp = fltdp + (sample - fltp) * fltw
ltdp = ltdp - ltdp * ltdmp fltdp = fltdp - fltdp * fltdmp
else else
ltp = sample fltp = sample
ltdp = 0 fltdp = 0
end end
ltp = ltp + ltdp fltp = fltp + fltdp
-- Apply the highpass filter to the sample -- Apply the highpass filter to the sample
ltphp = ltphp + ltp - pp fltphp = fltphp + fltp - pp
ltphp = ltphp - ltphp * lthp fltphp = fltphp - fltphp * flthp
sample = ltphp sample = fltphp
-- Apply the phaser to the sample -- Apply the phaser to the sample
@ -667,12 +675,4 @@ function sfxr.Sound:randomBlip()
self.highpass.cutoff = 0.1 self.highpass.cutoff = 0.1
end end
-- Constructor
function sfxr.newSound(...)
local instance = setmetatable({}, sfxr.Sound)
instance:__init(...)
return instance
end
return sfxr return sfxr