mah17

Untitled

Dec 31st, 2015
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.63 KB | None | 0 0
  1. -- Reworked by Yoshi_E
  2. -- Gui Rework: Yoshi_E
  3.  
  4. -- Noteblock Studio file (.nbs) player for MiscPeripherals (a ComputerCraft mod)
  5. -- You are free to copy, alter, and distribute this under the following conditions:
  6. -- 1) You leave these comments and credits attached
  7. -- 2) You do not make money off of this
  8. -- Program by James0x57 - http://www.youtube.com/watch?v=ro4EK8smULQ
  9. -- Noteblock Studio by _Davve_ - http://www.minecraftforum.net/topic/136749-minecraft-note-block-studio-150000-downloads/
  10. -- MiscPeripherals by RichardG867 - http://www.computercraft.info/forums2/index.php?/topic/4587-cc1481mc146-miscperipherals-23/
  11. -- ComputerCraft by Dan - http://www.computercraft.info/
  12.  
  13. local computer_Dirt=1
  14. local computer_Sand=2
  15. local computer_Stone=3
  16. local computer_Glas=4
  17. local computer_Wood=5
  18.  
  19.  
  20. local args = {...};
  21. local menu = {};
  22. local box = nil;
  23. local song = nil;
  24. local action = 'mainList';
  25. local maxtoshow = 16;
  26. local selected = 1;
  27. local startat = 1;
  28. local background = 1;
  29. local text = 32768;
  30. local playing = false;
  31. local nextone = false;
  32. local start = false;
  33. local g=1
  34. local loop="NL"
  35. local lastsong = {}
  36. rednet.open("top")
  37.  
  38.  
  39. -- FLIB API
  40. function exists(path)
  41. local file = assert(io.open(path, "r"))
  42. if file ~= nil then
  43. file:close()
  44. return true
  45. end
  46.  
  47. return false
  48. end
  49.  
  50. function getTable(path)
  51. if exists(path) then
  52. local file = io.open(path, "r")
  53. local lines = {}
  54. local i = 1
  55. local line = file:read("*l")
  56. while line ~= nil do
  57. lines[i] = line
  58. line = file:read("*l")
  59. i = i + 1
  60. end
  61. file:close()
  62. return lines
  63. end
  64. return {}
  65. end
  66.  
  67. function getLine(path, n)
  68. if exists(path) then
  69. local lines = getTable(path)
  70. return lines[n]
  71. end
  72. return ""
  73. end
  74.  
  75. function getText(path)
  76. if exists(path) then
  77. local file = assert(io.open(path, "r"))
  78. return file:read("*a")
  79. end
  80. return ""
  81. end
  82.  
  83. function fappend(path, text)
  84. local file = assert(io.open(path, "a"))
  85. file:write(text.."\n")
  86. file:close()
  87. end
  88.  
  89. function fwrite(path, text)
  90. local file = assert(io.open(path, "w"))
  91. file:write(text)
  92. file:close()
  93. end
  94.  
  95. function fwriteAtStart(path, text)
  96. local _text = getText(path)
  97. fwrite(path, text.."\n".._text)
  98. end
  99.  
  100. function fwriteFromTable(path, t)
  101. local text = ""
  102. for _, line in pairs(t) do
  103. text = text..line.."\n"
  104. end
  105. fwrite(path, text)
  106. end
  107.  
  108. function fappendFromTable(path, t)
  109. local text = ""
  110. for _, line in pairs(t) do
  111. text = text..line.."\n"
  112. end
  113. fappend(path, text)
  114. end
  115.  
  116. function fwriteAtStartFromTable(path, t)
  117. local text = ""
  118. for _, line in pairs(t) do
  119. text = text..line.."\n"
  120. end
  121. fwriteAtStart(path, text)
  122. end
  123.  
  124. function replaceLine(path, n, text)
  125. local lines = getTable(path)
  126. lines[n] = text
  127. fwriteFromTable(path, lines)
  128. end
  129.  
  130. function getName(path)
  131. if exists(path) then
  132. local lastSlashPos = 1
  133. for i = 1, path:len() do
  134. if path:sub(i, i) == "/" then
  135. lastSlashPos = i
  136. end
  137. end
  138.  
  139. return path:sub(lastSlashPos + 1)
  140. end
  141. return ""
  142. end
  143.  
  144. function getPath(path)
  145. if exists(path) then
  146. local lastSlashPos = 1
  147. for i = 1, path:len() do
  148. if path:sub(i, i) == "/" then
  149. lastSlashPos = i
  150. end
  151. end
  152.  
  153. return path:sub(1, lastSlashPos)
  154. end
  155. return ""
  156. end
  157.  
  158. function fremove(path)
  159. os.remove(path)
  160. end
  161.  
  162. term.setBackgroundColour(1)
  163. function selectedplay()
  164. song = newSong(fs.open(args[1] .. "/" .. menu[selected], "rb"));
  165. readHeader();
  166. --showInfo();
  167. readNotes();
  168. song.fh.close();
  169. --playNotes()
  170. end
  171.  
  172.  
  173. function headline()
  174. lastsong = getTable("lastsong")
  175. --if lastsong[2]~= "NL" then
  176. selected=tonumber(lastsong[1])
  177. term.setTextColour(1)
  178. loop=(lastsong[2])
  179. if lastsong[3]=="play" then
  180. playing=true
  181. else
  182. playing=false
  183. end
  184. --end
  185. term.clear()
  186. while true do
  187. for i=3,19 do
  188. term.setCursorPos(1,i)
  189. print(" ")
  190. end
  191.  
  192. term.setBackgroundColour(background)
  193. term.setTextColour(text)
  194. term.setCursorPos(1,1)
  195. term.clearLine()
  196.  
  197.  
  198. if playing ==false then
  199. print(" << Play >> "..loop)
  200. term.setCursorPos(1,2)
  201. term.setBackgroundColour(256)
  202. term.setTextColour(colours.white)
  203. term.clearLine()
  204. if songname~=nil then
  205. print(" Playing: "..menu[selected])
  206. else
  207. print(" Playing: Unknow")
  208. end
  209.  
  210.  
  211.  
  212. else
  213. start=true
  214. selectedplay()
  215. print(" << Stop >> "..loop)
  216.  
  217. term.setCursorPos(1,2)
  218. term.setBackgroundColour(256)
  219. term.setTextColour(colours.white)
  220. term.clearLine()
  221. if songname~=nil then
  222. print(" Playing: "..menu[selected])
  223. else
  224. print(" Playing: Unknow")
  225. end
  226. end
  227. term.setBackgroundColour(1)
  228. term.setTextColour(text)
  229. --Song List
  230.  
  231. term.setCursorPos(1,3)
  232. for i = g, #menu do
  233. if g + maxtoshow <= i then break end
  234. print(i .. ") " .. menu[i]);
  235. end
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242. sleep(0.01) -- Debug, if 0 it dobble checks: if event ==...
  243. event, param1, param2, param3 = os.pullEvent()
  244. if event == "mouse_click" and param3 == 1 then if param2 == 6 or param2 == 7 or param2 == 8 or param2 == 9 then if playing==false then playing=true else playing=false end end end
  245. if event == "mouse_click" and param3 == 1 then if param2 == 2 or param2 == 3 then if selected~=1 then selected=selected-1 end nextone=true selectedplay() end end
  246. if event == "mouse_click" and param3 == 1 then if param2 == 12 or param2 == 13 then if selected<#menu then selected=selected+1 end nextone=true selectedplay() end end
  247. if event == "mouse_click" and param3 == 1 then if param2 == 16 or param2 == 17 then if loop=="LA" then loop="NL" else if loop=="L1" then loop="LA" else loop="NL" if loop=="NL" then loop="L1" else loop="NL" end end end end end
  248. if event == "mouse_scroll" and param1 == 1 then if g<=#menu-16 then g=g+1 end end
  249. if event == "mouse_scroll" and param1 == -1 then if g>1 then g=g-1 end end
  250. if event == "mouse_click" and param3 == 3 then selected=g end
  251. if event == "mouse_click" and param3 == 4 then selected=g+1 end
  252. if event == "mouse_click" and param3 == 5 then selected=g+2 end
  253. if event == "mouse_click" and param3 == 6 then selected=g+3 end
  254. if event == "mouse_click" and param3 == 7 then selected=g+4 end
  255. if event == "mouse_click" and param3 == 8 then selected=g+5 end
  256. if event == "mouse_click" and param3 == 9 then selected=g+6 end
  257. if event == "mouse_click" and param3 == 10 then selected=g+7 end
  258. if event == "mouse_click" and param3 == 11 then selected=g+8 end
  259. if event == "mouse_click" and param3 == 12 then selected=g+9 end
  260. if event == "mouse_click" and param3 == 13 then selected=g+10 end
  261. if event == "mouse_click" and param3 == 14 then selected=g+11 end
  262. if event == "mouse_click" and param3 == 15 then selected=g+12 end
  263. if event == "mouse_click" and param3 == 16 then selected=g+13 end
  264. if event == "mouse_click" and param3 == 17 then selected=g+14 end
  265. if event == "mouse_click" and param3 == 18 then selected=g+15 end
  266.  
  267. end
  268. end
  269.  
  270. for _,side in ipairs({"left"; "right"; "back"; "bottom"; "top"; "front"}) do
  271. if peripheral.getType(side) == "music" then
  272. box = peripheral.wrap(side);
  273. break;
  274. end
  275. end
  276.  
  277. if turtle ~= nil then
  278. maxtoshow = 7;
  279. end
  280.  
  281. function newSong(x)
  282. return {
  283. fh = x;
  284. length = 0;
  285. height = 0;
  286. name = "";
  287. author = "";
  288. originalAuthor = "";
  289. description = "";
  290. tempo = 10.00; --tks per second
  291. autoSaving = 0;
  292. autoSaveDur = 0;
  293. timeSig = 4;
  294. minSpent = 0;
  295. leftClicks = 0;
  296. rightClicks = 0;
  297. blocksAdded = 0;
  298. blocksRemoved = 0;
  299. midi = "";
  300. music = { wait={}; inst={}; note={}; };
  301. };
  302. end
  303.  
  304. if args[1] == nil or fs.isDir(args[1]) == false then
  305. args[1] = "songs";
  306. if fs.isDir(args[1]) == false then
  307. fs.makeDir("songs");
  308. end
  309. end
  310.  
  311. for _, file in ipairs(fs.list(args[1])) do
  312. if fs.isDir(file) == false and string.find(file, ".nbs", -4, true) ~= nil then -- if file and ends in ".nbs"
  313. menu[#menu+1] = file;
  314. end
  315. end
  316.  
  317.  
  318. function readInt(fh) --little endian, fh is open in rb mode
  319. local ret = 0;
  320. local x = fh.read();
  321. if x == nil then return nil; end
  322. ret = x;
  323. x = fh.read();
  324. if x == nil then return nil; end
  325. ret = (x * 0x100) + ret;
  326. x = fh.read();
  327. if x == nil then return nil; end
  328. ret = (x * 0x10000) + ret;
  329. x = fh.read();
  330. if x == nil then return nil; end
  331. ret = (x * 0x1000000) + ret;
  332. return ret;
  333. end
  334.  
  335. function readShort(fh) --little endian, fh is open in rb mode
  336. local ret = 0;
  337. local x = fh.read();
  338. if x == nil then return nil; end
  339. ret = x;
  340. x = fh.read();
  341. if x == nil then return nil; end
  342. ret = (x * 0x100) + ret;
  343. return ret;
  344. end
  345.  
  346. function readString(fh, len) --fh is open in rb mode
  347. local ret = "";
  348. local x = 0;
  349. for i = 1, len do
  350. x = fh.read();
  351. if x == nil then return nil; end
  352. ret = ret .. string.char(x);
  353. end
  354. return ret;
  355. end
  356.  
  357. function readHeader()
  358. song.length = readShort(song.fh);
  359. song.height = readShort(song.fh);
  360. songname = readString(song.fh, readInt(song.fh));
  361. song.author = readString(song.fh, readInt(song.fh));
  362. song.originalAuthor = readString(song.fh, readInt(song.fh));
  363. song.description = readString(song.fh, readInt(song.fh));
  364. song.tempo = 1.000 / ( readShort(song.fh) / 100.00 );
  365. song.autoSaving = song.fh.read();
  366. song.autoSaveDur = song.fh.read();
  367. song.timeSig = song.fh.read();
  368. song.minSpent = readInt(song.fh);
  369. song.leftClicks = readInt(song.fh);
  370. song.rightClicks = readInt(song.fh);
  371. song.blocksAdded = readInt(song.fh);
  372. song.blocksRemoved = readInt(song.fh);
  373. song.midi = readString(song.fh, readInt(song.fh));
  374. end
  375.  
  376. function readNotes()
  377. local curtk = 1;
  378. local tk = -1;
  379. local layer = -1;
  380. local inst = 0;
  381. local note = 33; -- MC is 33 to 57
  382.  
  383. while true do
  384. tk = readShort(song.fh);
  385. if tk == nil then return false; end
  386. if tk == 0 then break; end
  387. while true do
  388. song.music.wait[curtk] = (tk * song.tempo) * 0.975; -- * 0.975 to speed it up a bit because lua slow
  389. layer = readShort(song.fh); --can't do anything with this info (yet?)
  390. if layer == nil then return false; end
  391. if layer == 0 then break; end
  392. song.music.inst[curtk]=song.fh.read();
  393. if song.music.inst[curtk] == 0 then
  394. song.music.inst[curtk] = 0;
  395. elseif song.music.inst[curtk] == 2 then
  396. song.music.inst[curtk] = 1;
  397. elseif song.music.inst[curtk] == 3 then
  398. song.music.inst[curtk] = 2;
  399. elseif song.music.inst[curtk] == 4 then
  400. song.music.inst[curtk] = 3;
  401. elseif song.music.inst[curtk] == 1 then
  402. song.music.inst[curtk] = 4;
  403. end
  404. song.music.note[curtk]=song.fh.read()-33;
  405. tk = 0;
  406. curtk = curtk + 1;
  407. end
  408. end
  409. return true;
  410. end
  411.  
  412. function playNotes()
  413. while true do
  414. if start==false then
  415. while start==false do
  416. sleep(1)
  417. end
  418. end
  419.  
  420. for i = 1, #song.music.wait - 1 do
  421. if playing==false then
  422. while playing==false do
  423. sleep(1)
  424. end
  425. end
  426. if song.music.wait[i]~=nil then
  427. if song.music.wait[i]>=0 and song.music.wait[i]<=100000 then
  428. if song.music.wait[i] ~= 0 then
  429. sleep(song.music.wait[i]);
  430. end
  431. if song.music.inst[i]==0 then rednet.send(computer_Dirt,song.music.note[i]) end
  432. if song.music.inst[i]==1 then rednet.send(computer_Sand,song.music.note[i]) end
  433. if song.music.inst[i]==2 then rednet.send(computer_Stone,song.music.note[i]) end
  434. if song.music.inst[i]==3 then rednet.send(computer_Glas,song.music.note[i]) end
  435. if song.music.inst[i]==4 then rednet.send(computer_Wood,song.music.note[i]) end
  436. --pcall(box.playNote, song.music.inst[i], song.music.note[i]);
  437. end
  438. end
  439. end
  440. playing=false
  441. if loop=="L1" then
  442. sleep(1)
  443. selectedplay()
  444. playing=true
  445. end
  446. if loop=="LA" then
  447.  
  448. if selected==#menu then
  449. selected=1
  450. sleep(1)
  451. selectedplay()
  452. playing=true
  453. else
  454. selected=selected+1
  455. sleep(1)
  456. selectedplay()
  457. playing=true
  458. end
  459. end
  460. end
  461. end
  462.  
  463.  
  464. --parallel.waitForAll(start, headline)
  465. parallel.waitForAll(playNotes, headline)
  466. --headline()
Add Comment
Please, Sign In to add comment