-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.lua
354 lines (306 loc) · 7.6 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
--[[
___
______ ___ ___ ___ / \ ___
_/ \_/ \_/ _ \_/ \_-- /- \
/ / / / / / /__/ /__/ __/ / /
/___/__/__/\__/\_/___/ \____/ \____/
(c) 2020 – 2022 marc2o \______/
https://marc2o.github.io
]]
VERSION = "0.6.0"
--[[
0.6.0
- basic error handling when dropping malformed mml files
0.5.0
- simple gui added
- noise synth bug fix
0.4.0
- loops implemented
- bug fixes
0.3.1
- simple lowpass filter
0.3.0
- basic lfo support implemented
- @vib macro implemented
0.2.2
- corrected song length so that it now loops cleanly
0.2.1
- audio volume of channel C (triangle) fine-tuned
- some changes to the player code
0.2.0 (09.10.2022)
- complete rewrite of synthesizer and parser
- refactored aiff exporter
- new app screen
]]
require("Modules.NamedColorPalette")
require("Modules.Music")
require("Modules.WriteAiff")
require("Modules.Gui")
Font = nil
Colors = nil
local visualizer = ""
local t_ui = {
song_title = {
color = "text_title",
text = "SONG INFO",
x = 16,
y = 16
},
title_label = {
color = "text_info",
text = "title:",
x = 16,
y = 40
},
title_text = {
color = "text_default",
text = "",
x = 144,
y = 40
},
composer_label = {
color = "text_info",
text = "composer:",
x = 16,
y = 64
},
composer_text = {
color = "text_default",
text = "",
x = 144,
y = 64
},
programmer_label = {
color = "text_info",
text = "programmer:",
x = 16,
y = 88
},
programmer_text = {
color = "text_default",
text = "",
x = 144,
y = 88
},
copyright_label = {
color = "text_info",
text = "copyright:",
x = 16,
y = 112
},
copyright_text = {
color = "text_default",
text = "",
x = 144,
y = 112
},
voices_label = {
color = "text_info",
text = "voices:",
x = 16,
y = 136
},
voice_A_text = {
color = "text_empty",
text = "A",
x = 144,
y = 136
},
voice_B_text = {
color = "text_empty",
text = "B",
x = 160,
y = 136
},
voice_C_text = {
color = "text_empty",
text = "C",
x = 176,
y = 136
},
voice_D_text = {
color = "text_empty",
text = "D",
x = 192,
y = 136
},
voice_E_text = {
color = "text_empty",
text = "E",
x = 208,
y = 136
},
}
--[[
function export_button.action()
write_aiff()
end
]]
function write_aiff()
local content = ""
local file = aiff:createFile(Music.meta.title .. " by " .. Music.meta.composer)
aiff:writeFile(file, {
soundData = Music.audio.sound_data,
title = Music.meta.title,
composer = Music.meta.composer
})
aiff:closeFile(file)
local path = {
macOS = "~/Library/Application Support/LOVE/Music/",
Windows = "%appdata%\\LOVE\\Music\\",
Linux = "~/.local/share/love/"
}
local os = love.system.getOS()
if os == "OS X" then os = "macOS" end
local success = love.window.showMessageBox("AIFF-Export", "Saved to " .. path[os], "info", true)
end
----------------------------------
-- LÖVE BASE FUNCTIONS
----------------------------------
function love.update(dt)
if dt < 1/60 then
love.timer.sleep(1/60 - dt)
end
NEXT_t = NEXT_t + MIN_dt
---
Gui.buttons["play_pause"].is_active = Music:is_ready()
Gui.buttons["export"].is_active = Music:is_ready()
if Music:is_playing() then
local value = Music:get_current_sample()
visualizer = visualizer .. string.char(math.floor(math.abs(value * 100)))
if visualizer:len() > 80 then
visualizer = visualizer:sub(2, 81)
end
end
end
function love.draw()
love.graphics.clear(Colors:get_color("background"))
if 1 == 1 then
for i = visualizer:len() - 1, 0, -1 do
local height = visualizer:sub(i + 1, i + 1):byte()
local width = (love.graphics.getWidth() - 32) / visualizer:len()
if height == nil or height == 0 then
height = 2
end
if i == 0 then
love.graphics.setColor(Colors:get_color("cursor"))
else
love.graphics.setColor(1, 1, 1, 0.1)
end
love.graphics.rectangle("fill", 16 + i * width, 250 - height, 5, height * 2)
end
end
for _, element in pairs(t_ui) do
love.graphics.setColor(Colors:get_color(element.color))
love.graphics.print(element.text, element.x, element.y)
end
Gui:draw_buttons()
---
local current_time = love.timer.getTime()
if NEXT_t <= current_time then
NEXT_t = current_time
return
end
love.timer.sleep(NEXT_t - current_time)
end
function love.load()
--print("v"..VERSION)
Font = love.graphics.newFont("Assets/FiraCode-Medium.ttf", 16)
love.graphics.setFont(Font)
-- see Assets/FiraCode-LICENSE.txt for more info
-- https://github.com/tonsky/FiraCode
love.window.setMode(
800,
400,
{
fullscreen = false,
vsync = true,
resizable = false,
centered = true
}
)
love.window.setTitle("Music v" .. VERSION)
Colors = NamedColorPalette:new()
Colors:create(require("Assets.colors"))
Gui:init()
Gui:add_button(
"play_pause",
"Play/Pause",
Font:getHeight(),
love.graphics.getHeight() - 3 * Font:getHeight(),
true
)
Gui:add_button_toggle_actions("play_pause", function () Music:play() end, function () Music:pause() end)
Gui:add_hotkey("play_pause","tab")
Gui:add_button(
"export",
"Export AIFF",
Gui.buttons["play_pause"].rect.x + Gui.buttons["play_pause"].rect.w + Font:getHeight(),
love.graphics.getHeight() - 3 * Font:getHeight(),
false
)
Gui:add_button_action("export", function () write_aiff() end)
Gui:add_button(
"quit",
"Quit",
Gui.buttons["export"].rect.x + Gui.buttons["export"].rect.w + 2 * Font:getHeight(),
love.graphics.getHeight() - 3 * Font:getHeight(),
false
)
Gui:add_button_action("quit", function () love.event.quit() end)
Gui:add_hotkey("quit", "escape")
---
MIN_dt = 1/60
NEXT_t = love.timer.getTime()
end
function love.quit()
end
function love.filedropped(file)
Music:pause()
Gui.buttons["play_pause"].is_active = false
Gui.buttons["play_pause"].toggled = false
Gui.buttons["export"].is_active = false
t_ui.title_text.text = ""
t_ui.composer_text.text = ""
t_ui.programmer_text.text = ""
t_ui.copyright_text.text = ""
t_ui["voice_A_text"].color = "text_empty"
t_ui["voice_B_text"].color = "text_empty"
t_ui["voice_C_text"].color = "text_empty"
t_ui["voice_D_text"].color = "text_empty"
t_ui["voice_E_text"].color = "text_empty"
if file then
file:open("r")
--file_content = file:read()
local content = {}
for line in file:lines() do
if string.len(line) > 0 then table.insert(content, line) end
end
file:close()
Music:init()
local success = false
if pcall(function () Music:parse_mml(content) end) then
success = true
else
-- error
local error = love.window.showMessageBox("Error", "Malformed mml", "info", true)
end
if success then
Music:render_audio()
t_ui.title_text.text = Music.meta.title
t_ui.composer_text.text = Music.meta.composer
t_ui.programmer_text.text = Music.meta.programmer
t_ui.copyright_text.text = Music.meta.copyright
local used_voices = Music:get_used_voices()
for voice, used in pairs(used_voices) do
if used then
t_ui["voice_" .. voice .. "_text"].color = "text_value"
else
t_ui["voice_" .. voice .. "_text"].color = "text_empty"
end
end
end
else
local error = love.window.showMessageBox("Error", "Unable to open file", "info", true)
end
end