require("opengl") require "audio.gen" require("muro") Synth, Param = audio.gen.Synth, audio.gen.Param local gl = gl local GL = gl local Texture = muro.Texture local Shader = muro.Shader local Slab = muro.Slab local sketch = muro.Sketch local ctx = "Hello! LOL" local win = Window(ctx, 0, 0, 512, 512) local numLines = 0 local rotAmt = 10 local rootNote = 100 local noteIndex = 1 local offsetY = 0.5 local fx1 = Slab{ ctx = ctx, file = LuaAV.findfile("cf.gaussian.2p.shl"), } fx1:texture().type = GL.FLOAT local fx2 = Slab{ ctx = ctx, file = LuaAV.findfile("cf.gaussian.2p.shl"), } fx2:texture().type = GL.FLOAT local accum = Slab{ ctx = ctx, file = LuaAV.findfile("co.accum.shl"), } accum:texture().type = GL.FLOAT local scene = Texture{ ctx = ctx, dim = {1024, 1024}, } ----------- GLV STUFF require("glv") local vals = { param1 = 0.1, param2 = 0.2, param3 = 0.3, param4 = 0.5, param5 = 0.6, } local min = math.min local max = math.max local Rect = glv.Rect local View = glv.View local GLV = glv.GLV local Panel = require("widgets.panel") local Inspector = require("widgets.panel.generic") local insp = Inspector{ name = "Params", params = vals } local panel = Panel{ rect = {5, 5, 150, 200}, name = "panel", } panel:add_inspector(insp) local gui = GLV{ dim = win.dim, disable = glv.Controllable, -- filename = script.path .. "/gui.serial.lua", panel, } function gui:resize(dim) self.dim = dim for name, v in pairs(self.children) do local x, y = v.x, v.y local w, h = v.w, v.h v.x = min(v.x, dim[1]-v.w) v.y = min(v.y, dim[2]-v.h) end end local mouse_event = { down = glv.MouseDown, drag = glv.MouseDrag, up = glv.MouseUp, } local mouse_btn = { left = 0, right = 1, } function gui:mouse(event, btn, x, y) local glv_event = mouse_event[event] if(glv_event) then local glv_btn = mouse_btn[btn] return self:setMouseEvent(glv_event, glv_btn, x, y) end end ------------ local beep = Synth{ name = "beep", params = { Param("freq", 440), Param("freq2", 880), Param("pan", 0.5), Param("fb", 0.5), Param("mod", 5), }, expr = function() local i = index / samplerate() local p = i/dur local ip = 1 - p local e = sin(math.pi * ip*ip*ip) local e2 = sin(math.pi * p*ip*ip) local v = sin(math.pi * 2 * i * freq + (fb * mod * ip)) local v2 = sin(math.pi * 2 * i * freq2) v = v - 0.2*fb v = v * e * amp v2 = v2 * e2 * amp fb = v return stereo(v + v2, pan) end, } local random = math.random local color = {1, 0, 1, 1} local angle = 20 local axis = {1, 1, 1} local ll = 1. local noteTable = { 1, 9/8, 5/4, 4/3, 3/2, 5/3, 15/8, 2/1 } local rhythmCount = 0 local seedCount = 0 local checkThis = { 0.95, 0.2, 0.5, 0.2, 0.95, 0.2, 0.5, 0.2 } local numSeeds = 64 local seedIndex = 1 local seeds = {} for i = 1, numSeeds do seeds[i] = math.random() * 100000000 end math.randomseed(1) local play = false function more(p) --print("more", now()) -- beep(p) local rep = 2^math.random(3) -- wait(math.random(8)/8) -- if math.random() < 0.4 then rootNote = vals.param1 * 300 + 100 local len = math.random(5) + 1 p.dur = math.random()*10 + 1 p.amp = math.random()*0.4 fx1:param("width", {p.dur*1.5, 0}) fx2:param("width", {0, p.amp * 10}) local choice = math.random(3) if (choice == 1) then p.freq = p.freq * math.random(2) elseif (choice == 2) then p.freq = p.freq / math.random(2) end rotAmt = 100 * p.freq / 500 offsetY = noteIndex / 8 --print(rotAmt) beep(p) numLines = len + 1 for i = 1, len do p.amp = 0.4*(1 - i/len) p.dur = math.random()*0.1 + 0.1 p.pan = math.random() beep(p) --[[ gl.PushMatrix() gl.Rotate((p.amp+now())*angle, axis[1], axis[2], axis[3]) gl.Color(color) gl.LineWidth(ll) gl.Begin(GL.LINES) gl.Vertex(-1, 0, 0) gl.Vertex(1, 0, 0) gl.End() gl.PopMatrix() --]] --wait(rep/8) wait(0.3) end -- end end function note() noteIndex = math.random(#noteTable) return noteTable[noteIndex] end function rhythm() local index = (rhythmCount % #checkThis) + 1 rhythmCount = rhythmCount + 1 -- print("index: ", index) if (index == 1) then seedCount = seedCount + 1 -- print("seedCount: ", seedCount) end if ((seedCount % 3) == 0) then seedCount = seedCount + 1 math.randomseed(seeds[seedIndex]) -- print("seed!!!!") end local prob = checkThis[index] if (math.random() < prob) then return true else return false end end function song() while true do if play then local p = { amp = 1, dur = 4 * math.random(), freq = rootNote * note() , freq2 = rootNote * note() * 2 } local p2 = { amp = 1, dur = 4 * math.random(), freq = rootNote * note() * 2, freq2 = rootNote * note() * 2 } -- print("before", now()) --go(1, more, p) if (rhythm()) then go(more, p) color[1] = random() color[3] = 1-color[1] angle = random(40)-20 --axis[1] = random() --axis[2] = random() ll = random(10) end if (rhythm()) then go(0.05, more, p2) end end wait(2) end end go(song) function win:key(event, key) if(event == "down" and key == 27) then self.cursor = self.fullscreen self.fullscreen = not self.fullscreen end if(event == "down" and key == 32) then play = not play end end --LuaAV.findfileinpath(script.path, "filename", true) (path, filename, recursive) function win:mouse(event, button, x, y) if(event == "down" or event == "up") then --print("x: ", x, " y: ", y) local x_fraction = x / self.dim[1] local y_fraction = y / self.dim[2] local width = math.sqrt(numSeeds) local index_i = math.floor(x_fraction* width) local index_j = math.floor(y_fraction* width) --print("index_i: ", index_i, " index_j: ", index_j) seedIndex = index_j*width + index_i + 1 --print("seedIndex: ", seedIndex) math.randomseed(seeds[seedIndex]) end gui:mouse(event, button, x, y) end function win:resize() gui:resize(self.dim) end function drawthelines() end function win:draw() local rot = now()*angle scene:startcapture() gl.Rotate(rot, axis[1], axis[2], axis[3]) gl.Color(color) gl.Translate(0, 2*offsetY - 1, 0) -- self.dim[2] => height of window for i = 1, numLines do gl.Translate(offsetY*0.3, 0, 0) gl.LineWidth(ll * i / numLines) gl.Color(color) gl.Rotate(rot + rotAmt*i, axis[1], axis[2], axis[3]) gl.Begin(GL.LINES) gl.Vertex(-1, 0, 0) gl.Vertex(1, 0, 0) gl.End() end scene:endcapture() sketch.enter_ortho() gl.Color(1, 1, 1, 1) --fx1:param("width", {(now()*0.1)%10, 0}) --fx2:param("width", {0, (now()*0.1)%10}) accum:draw(scene, fx2) fx1:draw(accum) fx2:draw(fx1) local tex = fx2:texture() gl.Color(1, 1, 1, 1) tex:bind(0) gl.Begin(GL.QUADS) sketch.quad() gl.End() tex:unbind(0) sketch.leave_ortho() --gui:draw() end