filloax

[Computronics]Iron note block .nbs redstone player -edited

Nov 4th, 2014
831
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.35 KB | None | 0 0
  1. local defaultSong = "chobb" --default song, .nbs is automatically appended
  2. local defaultSide = "top" --side of the note block
  3.  
  4. -- Noteblock Studio file (.nbs) player for MiscPeripherals (a ComputerCraft mod)
  5. -- version 2
  6. -- You are free to copy, alter, and distribute this under the following conditions:
  7. -- 1) You leave these comments and credits attached
  8. -- 2) You do not make money off of this
  9. -- Program by James0x57 - http://www.youtube.com/watch?v=ro4EK8smULQ
  10. -- Noteblock Studio by _Davve_ - http://www.minecraftforum.net/topic/136749-minecraft-note-block-studio-150000-downloads/
  11. -- MiscPeripherals by RichardG867 - http://www.computercraft.info/forums2/index.php?/topic/4587-cc1481mc146-miscperipherals-23/
  12. -- ComputerCraft by Dan - http://www.computercraft.info/
  13. -- If you use this in anything you create, please show credit where due, and let me know because I'd love to see it used!! =)
  14.  
  15. -- Edited by thefofthef for use with redstone
  16.  
  17. --Sintax: "<program name>" for redstone control (uses the default song) or "<program name> song (.nbs is automatically appended) noteBlockSide"
  18.  
  19. local Mdebug = true
  20. local rSide = "front"  --side of the redstone toggle
  21.  
  22.  
  23. --if Mdebug then print("") end
  24.  
  25. local args = {...}
  26. local auto = false; --toggled true if there aren't arguments for redstone control
  27. local box --note block
  28.  
  29. if args[1] == nil then
  30.     auto=true;
  31.     box=peripheral.wrap(defaultSide)
  32. elseif type(args[1])=="string" and args[2]==nil then
  33.     auto=false;
  34.     box=peripheral.wrap(defaultSide)
  35. elseif type(args[1])~="string" and type(args[2])~="string" then
  36.     error("syntax is <play song side> or <play>!")
  37. elseif type(args[1])=="string" and type(args[2])=="string" then
  38.     auto=false;
  39.     box=peripheral.wrap(args[2])
  40. end
  41.  
  42. function newSong(x)
  43.         if Mdebug then print("loading song") end
  44.         return {
  45.                 fh = x;
  46.                 length = 0;
  47.                 height = 0;
  48.                 name = "";
  49.                 author = "";
  50.                 originalAuthor = "";
  51.                 description = "";
  52.                 tempo = 10.00; --tks per second
  53.                 autoSaving = 0;
  54.                 autoSaveDur = 0;
  55.                 timeSig = 4;
  56.                 minSpent = 0;
  57.                 leftClicks = 0;
  58.                 rightClicks = 0;
  59.                 blocksAdded = 0;
  60.                 blocksRemoved = 0;
  61.                 midi = "";
  62.                 music = { wait={}; inst={}; note={}; };
  63.         };
  64. end
  65.  
  66. function readShort(fh) --little endian, fh is open in rb mode
  67.         if Mdebug then print("reading short...") end
  68.         local ret = 0;
  69.         local x = fh.read();
  70.         if x == nil then return nil; end
  71.         ret = x;
  72.         x = fh.read();
  73.         if x == nil then return nil; end
  74.         ret = (x * 0x100) + ret;
  75.         if Mdebug then print("read short") end
  76.         return ret;
  77. end
  78.  
  79. function readInt(fh) --little endian, fh is open in rb mode
  80.         local ret = 0;
  81.         if Mdebug then print("reading int...") end
  82.         local x = fh.read();
  83.         if x == nil then return nil; end
  84.         ret = x;
  85.         x = fh.read();
  86.         if x == nil then return nil; end
  87.         ret = (x * 0x100) + ret;
  88.         x = fh.read();
  89.         if x == nil then return nil; end
  90.         ret = (x * 0x10000) + ret;
  91.         x = fh.read();
  92.         if x == nil then return nil; end
  93.         ret = (x * 0x1000000) + ret;
  94.         if Mdebug then print("read int") end
  95.         return ret;
  96. end
  97.  
  98. function readString(fh, len) --fh is open in rb mode
  99.         if Mdebug then print("reading string...") end
  100.         local ret = "";
  101.         local x = 0;
  102.         for i = 1, len do
  103.                 x = fh.read();
  104.                 if x == nil then return nil; end
  105.                 ret = ret .. string.char(x);
  106.         end
  107.         if Mdebug then print("read string") end
  108.         return ret;
  109. end
  110.  
  111. function readHeader()
  112.         if Mdebug then print("reading header...") end
  113.         song.length             = readShort(song.fh);
  114.         song.height             = readShort(song.fh);
  115.         song.name                       = readString(song.fh, readInt(song.fh));
  116.         song.author             = readString(song.fh, readInt(song.fh));
  117.         song.originalAuthor = readString(song.fh, readInt(song.fh));
  118.         song.description        = readString(song.fh, readInt(song.fh));
  119.         song.tempo                      = 1.000 / ( readShort(song.fh) / 100.00 );
  120.         song.autoSaving         = song.fh.read();
  121.         song.autoSaveDur        = song.fh.read();
  122.         song.timeSig            = song.fh.read();
  123.         song.minSpent           = readInt(song.fh);
  124.         song.leftClicks         = readInt(song.fh);
  125.         song.rightClicks        = readInt(song.fh);
  126.         song.blocksAdded        = readInt(song.fh);
  127.         song.blocksRemoved      = readInt(song.fh);
  128.         song.midi                       = readString(song.fh, readInt(song.fh));
  129.         if Mdebug then print("read header") end
  130. end
  131.  
  132. function readNotes()
  133.         local curtk = 1;
  134.         local tk = -1;
  135.         local layer = -1;
  136.         local inst = 0;
  137.         local note = 33; -- MC is 33 to 57
  138.         if Mdebug then print("reading notes...") end
  139.         while true do
  140.                 tk = readShort(song.fh);
  141.                 if tk == nil then return false; end
  142.                 if tk == 0 then break; end
  143.                 while true do
  144.                         song.music.wait[curtk] = (tk * song.tempo) * 0.965; -- * 0.965 to speed it up a bit because lua slow
  145.                         layer = readShort(song.fh); --can't do anything with this info (yet?)
  146.                         if layer == nil then return false; end
  147.                         if layer == 0 then break; end
  148.                         song.music.inst[curtk]=song.fh.read();
  149.                         if song.music.inst[curtk] == 0 then
  150.                                 song.music.inst[curtk] = 0;
  151.                         elseif song.music.inst[curtk] == 2 then
  152.                                 song.music.inst[curtk] = 1;
  153.                         elseif song.music.inst[curtk] == 3 then
  154.                                 song.music.inst[curtk] = 2;
  155.                         elseif song.music.inst[curtk] == 4 then
  156.                                 song.music.inst[curtk] = 3;
  157.                         elseif song.music.inst[curtk] == 1 then
  158.                                 song.music.inst[curtk] = 4;
  159.                         end
  160.                         song.music.note[curtk]=song.fh.read()-33;
  161.                         tk = 0;
  162.                         curtk = curtk + 1;
  163.                 end
  164.                
  165.         end
  166.         if Mdebug then print("read notes") end
  167.         return true;
  168. end
  169.        
  170. function loadSong(fSong) -- loads a song
  171.         song = newSong(fs.open(fSong..".nbs", "rb"));
  172.         readHeader();
  173.         readNotes();
  174.         song.fh.close();
  175.         action = 'songReady';
  176.         if Mdebug then print("loaded song") end    
  177. end
  178.  
  179. function playNotes()
  180.         isPlaying = true;
  181.         for i = 1, #song.music.wait - 1 do
  182.             if song.music.wait[i] ~= 0 then
  183.                     os.sleep(song.music.wait[i]);
  184.             end
  185.             if redstone.getInput(rSide) == false and auto == true then break; end
  186.             pcall(box.playNote, song.music.inst[i], song.music.note[i]);
  187.         end
  188.         isPlaying = false;
  189. end
  190.  
  191. if auto==false then
  192.     loadSong(args[1])
  193.     print("Playing "..args[1]..".nbs")
  194.     playNotes()
  195. else
  196.     loadSong(defaultSong)
  197.     while true do
  198.         if redstone.getInput(rSide) == true then
  199.             playNotes()
  200.             os.sleep(4)
  201.         end
  202.         os.sleep(1)
  203.     end
  204. end
Advertisement
Add Comment
Please, Sign In to add comment