Marlingaming

Text Format based Music Player

Sep 14th, 2021 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. --Music Player
  2. --this program takes data from a selected file and plays the notes through a connected speaker
  3. --this program is for Minecraft 1.16.5, with the mod CC Tweaked
  4. local tArg = {...}
  5. Music_SelectedSong = Music_SelectedSong
  6. Music_Bit = Music_Bit
  7. Music_PlayState = Music_PlayState
  8. Music_SongLength = Music_SongLength
  9. Music_ReferenceLoc = nil
  10. Music_SettingsEnd = 5
  11. local NoteOutput = ["block.note_block.bass","block.note_block.basedrum","block.note_block.bell","block.note_block.chime","block.note_block.guitar","block.note_block.bit","block.note_block.pling","block.note_block.harp","block.note_block.flute"]
  12.  
  13. local speaker = peripheral.find("speaker")
  14.  
  15. start()
  16.  
  17. local function start()
  18. if fs.exists("/rom/music/"..Music_SelectedSong..".mtf") == true or Music_PlayState == "n" then
  19. ResetScript()
  20. if Music_PlayState == "n" then
  21. Music_SelectedSong = tArg[4]
  22. Music_PlayState = "ready"
  23. local file = fs.open("/rom/music/"..Music_SelectedSong..".mtf","r")
  24. file.close()
  25. else
  26. if tArg[1] == "Play" and Music_PlayState == "ready" or Music_PlayState == "paused" then
  27. Music_PlayState = "playing"
  28. elseif tArg[1] == "Pause" and Music_PlayState == "playing" then
  29. Music_PlayState = "paused"
  30. elseif tArg[1] == "Stop" and Music_PlayState ~= "n" then
  31. Music_PlayState = "n"
  32. ResetScript()
  33. elseif tArg[1] == "Seek" and Music_PlayState ~= "n" then
  34. Music_Bit = Music_ReferenceEnd+tArg[2]
  35. if Music_Bit >= Music_SongLength then
  36. ResetScript()
  37. else
  38. end
  39. end
  40. SongRead()
  41. end
  42.  
  43. local function ResetScript()
  44. Music_PlayState = "n"
  45. Music_Bit = 0
  46. Music_SongLength = 10
  47. Music_SelectedSong = nil
  48. end
  49.  
  50. local function SongRead()
  51. local Song = fs.open("/rom/music/"..Music_SelectedSong..".mtf","r")
  52. local i = 1
  53. while true do
  54. if Song.substring(i,i) == "|" then
  55. break
  56. else
  57. i = i + 1
  58. end
  59. end
  60. Music_SettingsEnd = i
  61. SettingsRead()
  62. if Music_Bit == 0 then
  63. Music_Bit = Music_SettingsEnd + 1
  64. end
  65. local i = Music_ReferenceLoc
  66. local I = 1
  67. while true do
  68. if Song.readLine(i) == "end" then
  69. break
  70. else
  71. NoteOutput[I] = Song.readLine(i)
  72. I = I + 1
  73. i = i + 1
  74. end
  75. end
  76. while true do
  77. sleep(1)
  78. if Music_Bit >= Music_SongLength then
  79. Song.close()
  80. ResetScript()
  81. else
  82. speaker.playNote( NoteOutput[ Song.substring( Music_Bit, Music_Bit) ], Song.read( Music_Bit),Song.read( Music_Bit))
  83. Music_Bit = Music Bit + 4
  84. end
  85. end
  86. Song.close()
  87. end
  88.  
  89. local function SettingsRead()
  90. local I = 1
  91. for i = 1, Music_SettingsEnd/2 do
  92. local Item = Song.substring(i,i+2)
  93. if I == 1 --Song End
  94. Music_SongLength = Item
  95. elseif I == 2 --Reference Variables Location
  96. Music_ReferenceLoc = Item
  97. elseif I == 1
  98. elseif I == 1
  99. end
  100. I = I + 1
  101. i = i + 3
  102. end
  103. end
  104.  
  105. --//Tutorial//
  106. --to use this script you need to know the variables and terms and what they mean
  107. --Note: this is what the program runs every time a bit goes by. a Note is made up of 4 characters, the OutputID(01-99), Pitch, and Volume. for the script to work properly, you must also have a reference file so the script can find what Sounds a Output ID stands for.
  108. --file. the file has to be set up like this "(Song Name).mtf". the mtf at the end stands for Music Text Format labeling the file as an item that only scripts that can read it are ones who read files based on the mtf format, and also the label helps with an organization with so many other scripts out there.
  109. --a Bit usually lasts for 0.4-0.8 seconds.
Add Comment
Please, Sign In to add comment