Skip to content

Instantly share code, notes, and snippets.

@interstar
Created September 24, 2021 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save interstar/5ddd8ee582c3573db5165e06718b78df to your computer and use it in GitHub Desktop.
Save interstar/5ddd8ee582c3573db5165e06718b78df to your computer and use it in GitHub Desktop.
A noisy square-wave drone synth
--[[
name: square demo
description: > generate a squarewave with pulse width control
author: synaesmedia
See https://www.youtube.com/watch?v=8kXs8XIyq1U
--]]
require "include/protoplug"
function freq_to_delta(f)
local sr = plugin.isSampleRateKnown() and plugin.getSampleRate() or 44100
return 2*math.pi*f/sr
end
function make_osc(init_freq, init_amp,init_pw)
o = {
freq=init_freq,
amp=init_amp,
delta=0.06,
phase=0,
pulse_width=init_pw,
next=function(self)
local v = 1
local s = math.sin(self.phase)
if s > self.pulse_width then
v = 1
else
v = -1
end
self.phase=self.phase+self.delta
return v*self.amp
end,
set_freq=function(self,f)
self.freq = f
self.delta = freq_to_delta(f)
end,
set_amp=function(self,a)
self.amp=a
end,
set_pw=function(self,pw)
self.pulse_width=pw
end
}
o:set_freq(init_freq) -- so that delta is initialized properly
return o
end
local osc1 = make_osc(440,0.5,0.5)
local osc2 = make_osc(441,0.5,0.5)
local lfo1 = make_osc(4,1,0.5)
local lfo_amp = 0
function plugin.processBlock(samples, smax)
for i=0,smax do
vlfo = lfo1:next()
v1 = osc1:next()
v2 = osc2:next()
v = (v1+v2)/2
vf = ((v*lfo_amp*vlfo)+(v*(1-lfo_amp)))/2
samples[0][i] = vf
samples[1][i] = vf
end
end
plugin.manageParams {
{
name = "OSC1 Amplitude";
changed = function(val)
osc1:set_amp(val)
end;
};
{
name = "OSC1 Freq";
changed = function(val)
osc1:set_freq(val*3000)
end;
};
{
name = "OSC1 Pulse Width";
max = 2;
changed = function(val)
osc1:set_pw(val-1)
end;
};
{
name = "OSC2 Amplitude";
changed = function(val)
osc2:set_amp(val)
end;
};
{
name = "OSC2 Freq";
changed = function(val)
osc2:set_freq(val*3000)
end;
};
{
name = "OSC2 Pulse Width";
max = 2;
changed = function(val)
osc2:set_pw(val-1)
end;
};
{
name = "LFO1 Amplitude";
changed = function(val)
lfo_amp = val
end;
};
{
name = "LFO1 Freq";
changed = function(val)
lfo1:set_freq(val*val*1000)
end;
};
{
name = "LFO1 Pulse Width";
max = 2;
changed = function(val)
lfo1:set_pw(val-1)
end;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment