Skip to content

Commit

Permalink
Merge pull request #6 from catfact/save-patterns-add-pan
Browse files Browse the repository at this point in the history
Save patterns add pan
  • Loading branch information
tehn committed Nov 10, 2019
2 parents c05546d + b0a9e90 commit d3761c4
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 35 deletions.
110 changes: 81 additions & 29 deletions awake.lua
Expand Up @@ -32,7 +32,7 @@

engine.name = 'PolyPerc'

hs = include('lib/halfsecond')
local hs = include('lib/halfsecond')

local MusicUtil = require "musicutil"

Expand All @@ -45,35 +45,78 @@ local g = grid.connect()

local alt = false

mode = 1
mode_names = {"STEP","LOOP","SOUND","OPTION"}
local mode = 1
local mode_names = {"STEP","LOOP","SOUND","OPTION"}

one = {
local one = {
pos = 0,
length = 8,
start = 1,
data = {1,0,3,5,6,7,8,7,0,0,0,0,0,0,0,0}
}
two = {

local two = {
pos = 0,
length = 7,
start = 1,
data = {5,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
}

function add_pattern_params()
params:add_separator()

params:add{type = "number", id = "one_length", name = "<one> length]", min=1, max=8,
default = one.length,
action=function(x) one.length = x end }

params:add{type = "number", id = "one_start", name = "<one> start]", min=1, max=16,
default=one.start,
action=function(x) one.start = x end }

for i=1,16 do
params:add{type = "number", id= ("one_data_"..i), name = ("<one> data "..i), min=1, max=8,
default = one.data[i],
action=function(x)one.data[i] = x end }
end

params:add_separator()

params:add{type = "number", id = "two_length", name = "<two> length]", min=1, max=8,
default = two.length,
action=function(x)two.length = x end}

params:add{type = "number", id = "two_start", name = "<two> start]", min=1, max=16,
default = two.start,
action=function(x)two.start = x end }

for i=1,16 do
params:add{type = "number", id= "two_data_"..i, name = "<two> data "..i, min=1, max=8,
default = two.data[i],
action=function(x) two.data[i] = x end }
end

params:add_separator()
end

local set_loop_data = function(which, step, val)
params:set(which.."_data_"..step, val)
end


local midi_out_device
local midi_out_channel

local scale_names = {}
notes = {}
local notes = {}
local active_notes = {}

local edit_ch = 1
local edit_pos = 1

snd_sel = 1
snd_names = {"cut","gain","pw","rel","fb","rate"}
snd_params = {"cutoff","gain","pw","release","delay_feedback","delay_rate"}
local snd_names = {"cut","gain","pw","rel","fb","rate", "pan", "delay_pan"}
local snd_params = {"cutoff","gain","pw","release", "delay_feedback","delay_rate", "pan", "delay_pan"}
local NUM_SND_PARAMS = #snd_params

local BeatClock = require 'beatclock'
local clk = BeatClock.new()
Expand Down Expand Up @@ -101,17 +144,17 @@ local function all_notes_off()
active_notes = {}
end

local function morph(loop)
local function morph(loop, which)
for i=1,loop.length do
if loop.data[i] > 0 then
loop.data[i] = util.clamp(loop.data[i]+math.floor(math.random()*3)-1,1,8)
set_loop_data(which, i, util.clamp(loop.data[i]+math.floor(math.random()*3)-1,1,8))
end
end
end

local function random()
for i=1,one.length do one.data[i] = math.floor(math.random()*9) end
for i=1,two.length do two.data[i] = math.floor(math.random()*9) end
for i=1,one.length do set_loop_data(one, i, math.floor(math.random()*9)) end
for i=1,two.length do set_loop_data(two, i, math.floor(math.random()*9)) end
end

local function step()
Expand Down Expand Up @@ -202,6 +245,7 @@ function init()
end}
params:add{type = "number", id = "midi_out_device", name = "midi out device",
min = 1, max = 4, default = 1,

action = function(value) midi_out_device = midi.connect(value) end}
params:add{type = "number", id = "midi_out_channel", name = "midi out channel",
min = 1, max = 16, default = 1,
Expand Down Expand Up @@ -250,38 +294,46 @@ function init()
cs_GAIN = controlspec.new(0,4,'lin',0,1,'')
params:add{type="control",id="gain",controlspec=cs_GAIN,
action=function(x) engine.gain(x) end}

cs_PAN = controlspec.new(-1,1, 'lin',0,0,'')
params:add{type="control",id="pan",controlspec=cs_PAN,
action=function(x) engine.pan(x) end}


params:default()

crow.input[1].mode("change", 1, 0.05, "rising")
crow.input[1].change = function(s)
morph(one)
morph(two)
morph(one, "one")
morph(two, "two")
end
crow.input[2].mode("change", 1, 0.05, "rising")
crow.input[2].change = random

clk:start()

hs.init()

add_pattern_params()
params:default()

end

function g.key(x, y, z)
local grid_h = g.rows
if z > 0 then
if (grid_h == 8 and edit_ch == 1) or (grid_h == 16 and y <= 8) then
if one.data[x] == 9-y then
one.data[x] = 0
set_loop_data("one", x, 0)
else
one.data[x] = 9-y
set_loop_data("one", x, 9-y)
end
end
if (grid_h == 8 and edit_ch == 2) or (grid_h == 16 and y > 8) then
if grid_h == 16 then y = y - 8 end
if two.data[x] == 9-y then
two.data[x] = 0
set_loop_data("two", x, 0)
else
two.data[x] = 9-y
set_loop_data("two", x, 9-y)
end
end
gridredraw()
Expand Down Expand Up @@ -330,16 +382,16 @@ function enc(n, delta)
end
elseif n==3 then
if edit_ch == 1 then
one.data[edit_pos] = util.clamp(one.data[edit_pos]+delta,0,8)
params:delta("one_data_"..edit_pos, delta)
else
two.data[edit_pos] = util.clamp(two.data[edit_pos]+delta,0,8)
params:delta("two_data_"..edit_pos, delta)
end
end
elseif mode == 2 then --loop
if n==2 then
one.length = util.clamp(one.length+delta,1,16)
params:delta("one_length", delta)
elseif n==3 then
two.length = util.clamp(two.length+delta,1,16)
params:delta("two_length", delta)
end
elseif mode == 3 then --sound
if n==2 then
Expand Down Expand Up @@ -382,13 +434,14 @@ function key(n,z)
end
else
-- clear
for i=1,one.length do one.data[i] = 0 end
for i=1,two.length do two.data[i] = 0 end
for i=1,one.length do params:set("one_data_"..i, 0) end
for i=1,two.length do params:set("two_data_"..i, 0) end

end
elseif n==3 and z==1 then
if not alt==true then
-- morph
if edit_ch == 1 then morph(one) else morph(two) end
if edit_ch == 1 then morph(one, "one") else morph(two, "two") end
else
-- random
random()
Expand All @@ -406,9 +459,9 @@ function key(n,z)
end
elseif mode == 3 then --sound
if n==2 and z==1 then
snd_sel = util.clamp(snd_sel - 2,1,5)
snd_sel = util.clamp(snd_sel - 2,1,NUM_SND_PARAMS-1)
elseif n==3 and z==1 then
snd_sel = util.clamp(snd_sel + 2,1,5)
snd_sel = util.clamp(snd_sel + 2,1,NUM_SND_PARAMS-1)
end
elseif mode == 4 then --option
if n==2 then
Expand All @@ -428,7 +481,6 @@ function key(n,z)
end
end
]]--

redraw()
Expand Down
20 changes: 14 additions & 6 deletions lib/Engine_PolyPerc.sc
@@ -1,31 +1,31 @@
// CroneEngine_PolyPerc
// sine with perc envelopes, triggered on freq
// pulse wave with perc envelopes, triggered on freq
Engine_PolyPerc : CroneEngine {
var pg;
var amp=0.3;
var release=0.5;
var pw=0.5;
var cutoff=1000;
var gain=2;
var pan = 0;

*new { arg context, doneCallback;
^super.new(context, doneCallback);
}

alloc {
pg = ParGroup.tail(context.xg);
SynthDef("PolyPerc", {
arg out, freq = 440, pw=pw, amp=amp, cutoff=cutoff, gain=gain, release=release;
SynthDef("PolyPerc", {
arg out, freq = 440, pw=pw, amp=amp, cutoff=cutoff, gain=gain, release=release, pan=pan;
var snd = Pulse.ar(freq, pw);
var filt = MoogFF.ar(snd,cutoff,gain);
var env = Env.perc(level: amp, releaseTime: release).kr(2);
// out.poll;
Out.ar(out, (filt*env).dup);
Out.ar(out, Pan2.ar((filt*env), pan));
}).add;

this.addCommand("hz", "f", { arg msg;
var val = msg[1];
Synth("PolyPerc", [\out, context.out_b, \freq,val,\pw,pw,\amp,amp,\cutoff,cutoff,\gain,gain,\release,release], target:pg);
Synth("PolyPerc", [\out, context.out_b, \freq,val,\pw,pw,\amp,amp,\cutoff,cutoff,\gain,gain,\release,release,\pan,pan], target:pg);
});

this.addCommand("amp", "f", { arg msg;
Expand All @@ -35,14 +35,22 @@ Engine_PolyPerc : CroneEngine {
this.addCommand("pw", "f", { arg msg;
pw = msg[1];
});

this.addCommand("release", "f", { arg msg;
release = msg[1];
});

this.addCommand("cutoff", "f", { arg msg;
cutoff = msg[1];
});

this.addCommand("gain", "f", { arg msg;
gain = msg[1];
});

this.addCommand("pan", "f", { arg msg;
postln("pan: " ++ msg[1]);
pan = msg[1];
});
}
}
3 changes: 3 additions & 0 deletions lib/halfsecond.lua
Expand Up @@ -42,6 +42,9 @@ function sc.init()
params:add{id="delay_feedback", name="delay feedback", type="control",
controlspec=controlspec.new(0,1.0,'lin',0,0.75,""),
action=function(x) softcut.pre_level(1,x) end}
params:add{id="delay_pan", name="delay pan", type="control",
controlspec=controlspec.new(-1,1.0,'lin',0,0,""),
action=function(x) softcut.pan(1,x) end}
end

return sc

0 comments on commit d3761c4

Please sign in to comment.