View difference between Paste ID: Z2QQjSUq and MsKHUccS
SHOW: | | - or go back to the newest paste.
1
-- Musicator MAIN
2
-- Created with <3 by FoxWorn3365
3
4
-- Initial values
5
notes = {}
6
clickAssociation = {}
7
lineAssociation = {"skip", "skip", "skip", "do_max", "si", "la", "sol", "fa", "mi", "re", "do_min"}
8
lineAssociationInverse = {["do_max"] = 10, ["si"] = 11, ["la"] = 12, ["sol"] = 13, ["fa"] = 14, ["mi"] = 15, ["re"] = 16, ["do_min"] = 17}
9
rowLineAssociation = {}
10
projectAssociation = {}
11
frequencyManager = {}
12
placedNotes = {}
13
relativeNotes = {}
14
checkForDelete = {["old"] = "main", ["counter"] = 0}
15
page = "home"
16
project = ""
17
current = "main"
18
currentNotes = ""
19
termx, termy = term.getSize()
20
notes["do_min"] = "261.64"
21
notes["do_min_long"] = "261.64"
22
notes["re"] = "293.68"
23
notes["re_long"] = "293.68"
24
notes["mi"] = "329.64"
25
notes["mi_long"] = "329.64"
26
notes["fa"] = "349.24"
27
notes["fa_long"] = "349.24"
28
notes["sol"] = "392.00"
29
notes["sol_long"] = "392.00"
30
notes["la"] = "440.00"
31
notes["la_long"] = "440.00"
32
notes["si"] = "493.92"
33
notes["si_long"] = "493.92"
34
notes["do_max"] = "523.28"
35-
function notesMenu() 
35+
notes["do_max_long"] = "523.28"
36
37
-- Functions
38-
  local used = 8
38+
39-
  paintutils.drawLine(1, 2, termx, 2, colors.lightGray)
39+
40-
  for index, value in ipairs(notes) do
40+
41
  term.clear()
42
  term.setCursorPos(1, 1)
43
end
44
45
function drawHead(text) 
46
  term.setTextColor(colors.white)
47
  paintutils.drawLine(1, 1, termx, 1, colors.lightBlue)
48-
    term.setCursorPos(used, " "..index.." ")
48+
49-
    local endused = used + (#index + 2)
49+
50-
    for i=used, used+(#index + 2) do
50+
51-
      clickAssociation[i] = index
51+
52
end
53
54
function notesMenu(ll) 
55
  term.setTextColor(colors.white)
56
  local isColored = false
57
  local used = 5
58
  paintutils.drawLine(1, ll, termx, ll, colors.lightGray)
59
  for i,v in pairs(notes) do
60
    if isColored == false then
61
      term.setBackgroundColor(colors.lightGray)
62
      isColored = true
63-
function
63+
64
      term.setBackgroundColor(colors.gray)
65
      isColored = false
66
    end
67
    term.setCursorPos(used, ll)
68-
    notesMenu()
68+
    term.write(" "..i.." ")
69-
    os.pullEvent("mouse_click")
69+
    local endused = used + (#i + 2)
70
    for i=used, used+(#i + 2) do
71
      clickAssociation[i] = i
72
    end
73
    used = endused
74
  end
75
end
76
77
function normalize() 
78
  term.setBackgroundColor(colors.white)
79
  term.setTextColor(colors.black)
80
end
81
82
function makeLines() 
83
  normalize()
84
  local line = 7
85
  local colorized = true
86
  for i=0, 11 do
87
    local noteNumberArray = line-6
88
    if colorized == false then
89
      colorized = true
90
      rowLineAssociation[line] = lineAssociation[noteNumberArray]
91
    else
92
      paintutils.drawLine(0, line, termx, line, colors.lightGray)
93
      rowLineAssociation[line] = lineAssociation[noteNumberArray]
94
      colorized = false
95
    end
96
    line = line+1
97
  end
98
  paintutils.drawPixel(1, 13, colors.red)
99
  normalize()
100
end
101
102
function loadPlayer(project)
103
  for i,file in pairs(fs.list(project)) do
104
    loadNotes(project, file)
105
    frequencyManager[file] = i
106
    relativeNotes[file] = placedNotes
107
  end
108
  return fs.list(project)
109
end
110
    
111
112
function drawProjectMenu(ll, project)
113
  paintutils.drawLine(1, ll, termx, ll, colors.orange)
114
  term.setTextColor(colors.white)
115
  term.setCursorPos(2, ll)
116
  term.write("Project: "..project..".music")
117
  term.setCursorPos(46, ll)
118
  term.setBackgroundColor(colors.yellow)
119
  term.setTextColor(colors.white)
120
  term.write(" PLAY ")
121
  normalize()
122
end
123
124
function createProjectPart(project, part)
125
  local file = fs.open(project.."/"..part, "w")
126
  file.write("{}")
127
  file.flush()
128
  file.close()
129
end
130
131
function drawSheduleMenuFromProject(ll, project, current)
132
  local start = 1
133
  paintutils.drawLine(1, ll, termx, ll, colors.blue)
134
  if fs.isDir(project) == false then
135
    shell.run("mkdir "..project)
136
    local file = fs.open(project.."/main", "w")
137
    file.write("{}")
138
    file.flush()
139
    file.close()
140
    term.setBackgroundColor(colors.yellow)
141
    term.setTextColor(colors.white)
142
    term.setCursorPos(start, ll)
143
    term.write(" main ")
144
  else
145
    local used = 1
146
    for index,value in pairs(fs.list(project)) do
147
      if value == current then
148
        term.setBackgroundColor(colors.lightBlue)
149
      else 
150
        term.setBackgroundColor(colors.blue)
151
      end
152
      term.setTextColor(colors.white)
153
      term.setCursorPos(used, ll)
154
      term.write(" "..value.." ")
155
      local newused = used + (#value+2) 
156
      for i=used, newused-1 do
157
        projectAssociation[i] = value
158
      end
159
      used = used + (#value+2)
160
    end
161
  end
162
  local interactionx, interactiony = term.getCursorPos()
163
  projectAssociation[interactionx] = "new"
164
  projectAssociation[interactionx+1] = "new"
165
  term.setTextColor(colors.black)
166
  term.setBackgroundColor(colors.blue)
167
  print(" +")
168
  term.setCursorPos(46, ll)
169
  term.setBackgroundColor(colors.lightBlue)
170
  term.setTextColor(colors.white)
171
  term.write(" SAVE ")
172
  normalize()
173
end
174
175
function loadNotes(project, current) 
176
  currentNotes = current
177
  local file = fs.open(project.."/"..current, "r")
178
  local lsn = file.readAll()
179
  file.close()
180
  placedNotes = textutils.unserialize(lsn)
181
  if placedNotes == nil or placedNotes == " " then
182
    placedNotes = {}
183
  end
184
end
185
186
function drawNotes(project, current)
187
  if currentNotes ~= current then
188
    currentNotes = current
189
    loadNotes(project, current)
190
  end
191
  for row,note in pairs(placedNotes) do
192
    local clearnote = string.gsub(note, "_long", "")
193
    if lineAssociationInverse[clearnote] ~= nil then
194
      if string.find(note, "long") then
195
        paintutils.drawPixel(row, lineAssociationInverse[clearnote], colors.green)
196
      else
197
        paintutils.drawPixel(row, lineAssociationInverse[clearnote], colors.black)
198
      end
199
    end
200
  end
201
end
202
203
-- Visualizer
204
while true do
205
  if page == "home" then
206
    reset()
207
    drawHead("Musicator v1.0 - Make your Music!")
208
    print("\n\nBenvenuto su Musicator, per iniziare inserisci il nome di un progetto gia' esistente oppure di uno nuovo!") 
209
    project = read()
210
    page = "editor"
211
  elseif page == "editor" then
212
    reset()
213
    drawHead("Musicator v1.0 - Make your Music!")
214
    drawProjectMenu(2, project)
215
    drawSheduleMenuFromProject(3, project, current)
216
    notesMenu(4)
217
    makeLines()
218
    drawNotes(project, current)
219
    normalize()
220
    local event, button, x, y = os.pullEvent("mouse_click")
221
    if x > 0 then
222
      if y == 3 then
223
        if x > 42 then
224
          if button ~= 3 then
225
            local file = fs.open(project.."/"..current, "w")
226
            file.write(textutils.serialize(placedNotes))
227
            file.flush()
228
            file.close()
229
          else
230
            local file = fs.open(project.."/"..current, "w")
231
            file.write(" ")
232
            file.flush()
233
            file.close()
234
          end
235
        elseif projectAssociation[x] ~= nil then
236
          if projectAssociation[x] == "new" then
237
            paintutils.drawLine(1, 3, termx, 3, colors.lightBlue)
238
            term.setCursorPos(2, 3)
239
            term.write("New project part: ")
240
            term.setCursorPos(20, 3)
241
            local part = read()
242
            createProjectPart(project, part)
243
            current = part
244
            sleep(0.35)
245
          else
246
            if checkForDelete["old"] == projectAssociation[x] and checkForDelete["count"] < 3 then
247
              checkForDelete["count"] = checkForDelete["count"] + 1
248
              current = projectAssociation[x]
249
            elseif checkForDelete["old"] == projectAssociation[x] and checkForDelete["count"] >= 3 and  projectAssociation[x] ~= "main" then
250
              shell.run("rm "..project.."/"..projectAssociation[x])
251
            elseif checkForDelete["old"] ~= projectAssociation[x] then
252
              checkForDelete["old"] = projectAssociation[x]
253
              checkForDelete["count"] = 1
254
              current = projectAssociation[x]
255
            else
256
              exit()
257
            end
258
          end
259
        end
260
      elseif y == 2 and x > 45 then
261
        page = "player"
262
      elseif y > 6 then
263
        if placedNotes[x] == nil then
264
          if button == 1 then
265
            placedNotes[x] = rowLineAssociation[y]
266
            paintutils.drawPixel(x, y, colors.black)
267
          elseif button == 2 then
268
            placedNotes[x] = rowLineAssociation[y].."_long"
269
            paintutils.drawPixel(x, y, colors.green)
270
          end
271
        else 
272
          placedNotes[x] = nil
273
          paintutils.drawPixel(x, y, colors.white)
274
        end
275
      end
276
    end
277
  elseif page == "player" then
278
    reset()
279
    drawHead("Musicator v1.0 - Player")
280
    speaker = peripheral.wrap("right")
281
    print("\n\nPer avviare il player premi un tasto qualsiasi\n")
282
    os.pullEvent()
283
    tracks = loadPlayer(project)
284
    term.setCursorPos(7, 7)
285
    for i=1, 51, 1 do
286
      ntp = i 
287
      term.setCursorPos(10, 10)
288
      local longNote = false
289
      local nextEmpty = false
290
      print("Playing note number "..i)
291
      for fi,file in pairs(tracks) do
292
        if relativeNotes[file][ntp] ~= nil then
293
          term.setCursorPos(15, 13)
294
          if relativeNotes[file][ntp+1] == nil then
295
            nextEmpty = true
296
          end
297
          local note = notes[relativeNotes[file][ntp]]
298
          if string.find(relativeNotes[file][ntp], "long") then
299
            longNote = true
300
          end
301
          print("Playing note: "..note)
302
          speaker.start(tonumber(frequencyManager[file]), tonumber(note))
303
        end
304
      end
305
      if longNote then
306
        sleep(0.85)
307
      else
308
        sleep(0.4)
309
      end
310
      if nextEmpty then
311
        speaker.shutdown()
312
      end
313
      sleep(0.01)
314
    end
315
  else
316
    page = "home"
317
  end
318
end