Advertisement
gustav9797

Untitled

Jan 27th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. local args = {...};
  2. local menu = {};
  3. local box = nil;
  4. local song = nil;
  5. local action = 'mainList';
  6. local maxtoshow = 12;
  7. local selected = tonumber(args[1]);
  8. local startat = 1;
  9.  
  10. for _,side in ipairs({"left"; "right"; "back"; "bottom"; "top"; "front"}) do
  11. if peripheral.getType(side) == "note" then
  12. box = peripheral.wrap(side);
  13. break;
  14. end
  15. end
  16.  
  17. if turtle ~= nil then
  18. maxtoshow = 7;
  19. end
  20.  
  21. function newSong(x)
  22. return {
  23. fh = x;
  24. length = 0;
  25. height = 0;
  26. name = "";
  27. author = "";
  28. originalAuthor = "";
  29. description = "";
  30. tempo = 10.00; --tks per second
  31. autoSaving = 0;
  32. autoSaveDur = 0;
  33. timeSig = 4;
  34. minSpent = 0;
  35. leftClicks = 0;
  36. rightClicks = 0;
  37. blocksAdded = 0;
  38. blocksRemoved = 0;
  39. midi = "";
  40. music = { wait={}; inst={}; note={}; };
  41. };
  42. end
  43.  
  44. for _, file in ipairs(fs.list("songs")) do
  45. if fs.isDir(file) == false and string.find(file, ".nbs", -4, true) ~= nil then -- if file and ends in ".nbs"
  46. menu[#menu+1] = file;
  47. end
  48. end
  49.  
  50. function showInfo()
  51. term.clear();
  52. print("Now Playing: \n\n\n\n\n\n " .. song.name);
  53. print("\n\n\n\n\n\nAuthor: " .. song.author);
  54. print("Original Author: " .. song.originalAuthor);
  55. print("Description: ");
  56. print(song.description);
  57. end
  58.  
  59. function readInt(fh) --little endian, fh is open in rb mode
  60. local ret = 0;
  61. local x = fh.read();
  62. if x == nil then return nil; end
  63. ret = x;
  64. x = fh.read();
  65. if x == nil then return nil; end
  66. ret = (x * 0x100) + ret;
  67. x = fh.read();
  68. if x == nil then return nil; end
  69. ret = (x * 0x10000) + ret;
  70. x = fh.read();
  71. if x == nil then return nil; end
  72. ret = (x * 0x1000000) + ret;
  73. return ret;
  74. end
  75.  
  76. function readShort(fh) --little endian, fh is open in rb mode
  77. local ret = 0;
  78. local x = fh.read();
  79. if x == nil then return nil; end
  80. ret = x;
  81. x = fh.read();
  82. if x == nil then return nil; end
  83. ret = (x * 0x100) + ret;
  84. return ret;
  85. end
  86.  
  87. function readString(fh, len) --fh is open in rb mode
  88. local ret = "";
  89. local x = 0;
  90. for i = 1, len do
  91. x = fh.read();
  92. if x == nil then return nil; end
  93. ret = ret .. string.char(x);
  94. end
  95. return ret;
  96. end
  97.  
  98. function readHeader()
  99. song.length = readShort(song.fh);
  100. song.height = readShort(song.fh);
  101. song.name = readString(song.fh, readInt(song.fh));
  102. song.author = readString(song.fh, readInt(song.fh));
  103. song.originalAuthor = readString(song.fh, readInt(song.fh));
  104. song.description = readString(song.fh, readInt(song.fh));
  105. song.tempo = 1.000 / ( readShort(song.fh) / 100.00 );
  106. song.autoSaving = song.fh.read();
  107. song.autoSaveDur = song.fh.read();
  108. song.timeSig = song.fh.read();
  109. song.minSpent = readInt(song.fh);
  110. song.leftClicks = readInt(song.fh);
  111. song.rightClicks = readInt(song.fh);
  112. song.blocksAdded = readInt(song.fh);
  113. song.blocksRemoved = readInt(song.fh);
  114. song.midi = readString(song.fh, readInt(song.fh));
  115. end
  116.  
  117. function readNotes()
  118. local curtk = 1;
  119. local tk = -1;
  120. local layer = -1;
  121. local inst = 0;
  122. local note = 33; -- MC is 33 to 57
  123.  
  124. while true do
  125. tk = readShort(song.fh);
  126. if tk == nil then return false; end
  127. if tk == 0 then break; end
  128. while true do
  129. song.music.wait[curtk] = (tk * song.tempo) * 0.975; -- * 0.975 to speed it up a bit because lua slow
  130. layer = readShort(song.fh); --can't do anything with this info (yet?)
  131. if layer == nil then return false; end
  132. if layer == 0 then break; end
  133. song.music.inst[curtk]=song.fh.read();
  134. if song.music.inst[curtk] == 0 then
  135. song.music.inst[curtk] = 0;
  136. elseif song.music.inst[curtk] == 2 then
  137. song.music.inst[curtk] = 1;
  138. elseif song.music.inst[curtk] == 3 then
  139. song.music.inst[curtk] = 2;
  140. elseif song.music.inst[curtk] == 4 then
  141. song.music.inst[curtk] = 3;
  142. elseif song.music.inst[curtk] == 1 then
  143. song.music.inst[curtk] = 4;
  144. end
  145. song.music.note[curtk]=song.fh.read()-33;
  146. tk = 0;
  147. curtk = curtk + 1;
  148. end
  149. end
  150. return true;
  151. end
  152.  
  153. function playNotes()
  154. for i = 1, #song.music.wait - 1 do
  155. if song.music.wait[i] ~= 0 then
  156. os.sleep(song.music.wait[i]);
  157. end
  158. --box.playNote(song.music.inst[i], song.music.note[i]);
  159. pcall(box.playNote, song.music.inst[i], song.music.note[i]);
  160. end
  161. end
  162.  
  163. function printSongs(x)
  164. action = 'mainList';
  165. term.clear();
  166. action = 'playSong';
  167. return selected, x;
  168. end
  169.  
  170. if box == nil then
  171. print("No Iron Noteblock Detected");
  172. else
  173. selected, startat = printSongs(1);
  174. while true do
  175. if action == 'playSong' then
  176. song = newSong(fs.open("songs/" .. menu[tonumber(tostring(selected))], "rb"));
  177. readHeader();
  178. showInfo();
  179. readNotes();
  180. song.fh.close();
  181. playNotes();
  182. --loops if don't change action
  183. action = 'mainList';
  184. else
  185. if action == 'backNextMenu' then
  186. if selected == 1 then
  187. startat = startat - maxtoshow;
  188. if startat < 1 then startat = 1; end
  189. selected, startat = printSongs(startat);
  190. elseif selected == 2 then
  191. startat = startat + maxtoshow
  192. if startat > #menu then startat = 1; end
  193. selected, startat = printSongs(startat);
  194. else --if selected == 0 then
  195. selected, startat = printSongs(startat);
  196. end
  197. elseif action =='mainList' then
  198. selected, startat = printSongs(startat);
  199. end
  200. end
  201. end
  202. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement