-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
288 lines (240 loc) · 7.39 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
local layouts = require "layouts"
local UI, ui = require "ui"
require "run"
local config = {}
local lastRandomKey
local instruments = require "instruments"
local settings = require "settings"
local startRecording = false
local recording = false
local stopRecording = false
-- Push a preset onto the preset channel to the synth thread
local pushPreset = function(preset, name)
print("Pushing", name)
for i, v in ipairs(preset) do
v.effects = v.effects or {}
presets:push{
amplitude = v.amplitude;
keyshift = v.keyshift;
waveform = v.waveform;
effects = #v.effects;
}
for i, e in ipairs(v.effects) do
presets:push(e)
end
end
channel:push({action = "preset", name = name, voices = #preset})
end
function love.load(arrrgs)
for i, v in pairs(arg) do print(i, v) end
print "arr"
for i, v in pairs(arrrgs) do print(i, v) end
for i, v in ipairs(arrrgs) do
if v:match("^%-%-") then --option
config[v:match("^%-%-(.-)$")] = true
else --par
local o = arg[i-1]:match("^%-%-(.-)$")
config[o] = v
end
end
if config.seq or (arg[1] and arg[1]:match("^seq.*")) then
require "seq"
return
end
ui = UI.new("ui.png")
SR = tonumber(config.samplerate) or 44100
SL = tonumber(config.buffersize) or 512
SQ = love.audio.newQueueableSource(SR,16,1, 4) -- "Queue type"...
SD = love.sound.newSoundData(SL,SR,16,1) -- Buffer
thread = love.thread.newThread("thread.lua")
channel = love.thread.newChannel()
presets = love.thread.newChannel()
channel:push(SR)
channel:push(SL)
channel:push(SQ)
channel:push(SD)
thread:start(channel, presets)
for i, v in pairs(instruments) do
pushPreset(v, i)
end
instrument = "sine"
love.graphics.present()
notes = {number = 0}
--[[ TO DO:
- ADSR (attack (time to peak), decay (time to sustain), sustain (level to sustain), release (time to zero)
- send single notes events (start-stop), not table of frequencies
- ???
- profit
--]]
t = {}
tn = 0
config.attack = tonumber(config.attack) or 0.02
config.decay = tonumber(config.decay) or 0.05
config.sustain = tonumber(config.sustain) or 0.8
config.release = tonumber(config.release) or 0.05
config.duration = tonumber(config.duration)
config.chorus = tonumber(config.chorus) or 0
settings.attack = config.attack
settings.decay = config.decay
settings.sustain = config.sustain
settings.release = config.release
settings.duration = config.duration
settings.chorus = config.chorus
settings.defaults.attack = config.attack
settings.defaults.decay = config.decay
settings.defaults.sustain = config.sustain
settings.defaults.release = config.release
settings.defaults.duration = config.duration
settings.defaults.chorus = config.chorus
instruments = {
{name = "sine", display = "Sine"},
{name = "organ", display = "Organ", attack = 0.1, chorus = 0.17},
{name = "flute", display = "Voice (?)", attack = 0.1},
{name = "saw", display = "Saw wave"},
{name = "square", display = "Square wave"},
{name = "minkQM", display = "Strings...?", attack = 0.1},
{name = "minkQM1", display = "Pizz. strings...???", attack = 0, decay = 0.15, sustain = 0.2, duration = 0},
{name = "vibraphone", display = "Vibraphone - like thing???", attack = 0, decay = .3, sustain = 0.1, duration = 0},
{name = "test", display = "I don't even know", attack = 0, release = .2},
}
if not config.layout then
keys = layouts.openmpt
elseif layouts[config.layout] then
keys = layouts[config.layout]
else
local p, t = pcall(love.filesystem.load, config.layout .. ".lua")
if p then
keys = t() --I'm assuming you're not an idiot with that property.
else
keys = layouts.openmpt
end
end
if tonumber(config.shift) then
for i, v in pairs(keys) do
keys[i] = v + config.shift
end
end
love.keyboard.setKeyRepeat(true)
end
function love.update(dt)
end
function love.draw()
ui:draw()
love.graphics.setColor(1, 1, 1)
love.graphics.print(settings:format(), 0, 40)
for i, v in ipairs(instruments) do
if instrument == v.name then
love.graphics.setColor(1,1,1)
else
love.graphics.setColor(.5,.5,.5)
end
local x = math.floor(800 / #instruments * (i - 1))
love.graphics.printf("F" .. i .. "\n" .. v.display, x, 0, 800/#instruments, "center")
end
end
function love.keypressed(kk,k, isRepeat)
local ctrl = love.keyboard.isDown("lctrl", "rctrl")
local shift = love.keyboard.isDown("lshift", "rshift")
local alt = love.keyboard.isDown("lalt")
if settings.keys[k] then
settings:keypressed(k)
return
end
if isRepeat then
return
end
if k == "space" then
local note = love.math.random(64, 64+23)
for i, v in pairs(keys) do
if v == note then
k = i
lastRandomKey = k
end
end
end
if keys[k] then
if startRecording then
channel:push{action = "record"}
startRecording = false
recording = true
end
local effects = {}
if shift then
effects[1] = {type = "vibrato", 6, 1/4}
end
if settings.chorus > 0 then
effects[#effects+1] = {type = "chorus", settings.chorus}
end
channel:push{
action = "start",
id = notes.number,
instrument = instrument,
attack = settings.attack,
decay = settings.decay,
sustain = settings.sustain,
release = settings.release,
duration = settings.duration,
frequency = keys[k],
amplitude = 1,
effects = effects,
}
ui:noteDown(keys[k])
notes[k] = notes.number
notes.number = notes.number + 1
elseif k:match("f[0-9]+") then
local n = tonumber(k:match("f([0-9]+)"))
if shift then
if instruments[n] then
instruments[n].attack = settings.attack or settings.defaults.attack or config.attack
instruments[n].decay = settings.decay or settings.defaults.decay or config.decay
instruments[n].sustain = settings.sustain or settings.defaults.sustain or config.sustain
instruments[n].release = settings.release or settings.defaults.release or config.release
instruments[n].duration = settings.duration or settings.defaults.duration or config.duration
instruments[n].chorus = settings.chorus or settings.defaults.chorus or config.chorus
end
else
if instruments[n] then
instrument = instruments[n].name
settings.attack = instruments[n].attack or config.attack
settings.decay = instruments[n].decay or config.decay
settings.sustain = instruments[n].sustain or config.sustain
settings.release = instruments[n].release or config.release
settings.duration = instruments[n].duration or config.duration
settings.chorus = instruments[n].chorus or config.chorus
end
end
end
if UI.mods[k] then
ui:modDown(k)
end
if k == "insert" then
if not recording then
startRecording = not startRecording
else
print("Recording stopped")
recording = false
channel:push{action="stop"}
end
end
end
function love.keyreleased(kk,k)
if k == "space" then
k = lastRandomKey
end
if keys[k] and notes[k] then
--t[k] = nil
--tn = tn - 1
--local s = (tn == 0) and stop or t
--channel:push(s)
channel:push{
action = "release",
id = notes[k]
}
ui:noteUp(keys[k])
notes[k] = nil
end
if UI.mods[k] then
ui:modUp(k)
end
end
love.threaderror = print