Guest User

delightfulSoundTest.lua v1.0.2

a guest
Jun 4th, 2025
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.86 KB | None | 0 0
  1. -- Delightful Enhanced sound test code by rixithechao
  2. -- v1.0.2
  3. -- June 4, 2025
  4.  
  5. -- This code comes with zero guarantee that it works perfectly.  If it has any issues, feel free to fix it up or modify it to suit your needs
  6.  
  7. -- If you use this library, please either include me in your episode credits (in a "Libraries Used" or "Additional Code By" section or whatever equivalent you have) or add the line "Delightful Enhanced sound test code used with permission from its programmer"
  8.  
  9.  
  10. --[[ USAGE INSTRUCTIONS:
  11.     First, load this library and configure it with the following lines of code at the top of your level's luna.lua file:
  12.    
  13.         local soundTest = require("delightfulSoundTest")
  14.  
  15.         soundTest.section = #
  16.         soundTest.font = valid textplus font
  17.  
  18.         soundTest.eventNames = {
  19.             toggle = "vanilla event name to toggle the music player",
  20.             previous = "same but for moving to the next track",
  21.             next = "for moving to the previous track",
  22.             showPrevious = "for showing the previous track block",
  23.             hidePrevious = "for hiding the previous track block",
  24.             showNext = "for showing the next track block",
  25.             hideNext = "for hiding the next track block",
  26.         }
  27.  
  28.     By default, it uses section 0, textplus font 3, and the following event names.
  29.    
  30.         "SoundTest Toggle"
  31.         "SoundTest Previous"
  32.         "SoundTest Next"
  33.         "SoundTest ShowPrevious"
  34.         "SoundTest HidePrevious"
  35.         "SoundTest ShowNext"
  36.         "SoundTest HideNext"
  37.  
  38.  
  39.     Once you have the library configured, you'll need to load your music.  You can manually list them like so:
  40.  
  41.         soundTest.tracks = {
  42.             "a path as you'd specify it in a section's custom music field in the editor",
  43.             "another path in the same format",
  44.             "and so on",
  45.         }
  46.  
  47.  
  48.     Or you can call the load function, which will fill out the track list automatically from the specified folder (or "music" if you don't give an argument.)  Note that this loads them in alphabetical order based on their filenames, so you may want to specify track numbers in the filenames if you want them included in a specific order (i.e. '01-title.ogg', `02-intro.spc`, etc.)
  49.    
  50.         soundTest.load() -- loads from 'episodeName/music/'
  51.         soundTest.load("allTheMusic") -- loads from 'episodeName/allTheMusic/'
  52.    
  53.  
  54.     Of course, the track list is really just an orderered lua table, so if you want you can load most of the tracks from one folder automatically and then append a few from elsewhere in the project.
  55.  
  56.         soundTest.load()
  57.         soundTest.tracks[#soundTest.tracks+1] = "another path somewhere else in the project"
  58.    
  59.    
  60.     Altogether, your code will look something like this:
  61.  
  62.         local soundTest = require("delightfulSoundTest")
  63.  
  64.         soundTest.section = 19
  65.         soundTest.eventNames = {
  66.             toggle = "Music Player Toggle",
  67.             previous = "Music Player Prev",
  68.             next = "Music Player Next",
  69.             showPrevious = "Music Player Show Prev",
  70.             hidePrevious = "Music Player Hide Prev",
  71.             showNext = "Music Player Show Next",
  72.             hideNext = "Music Player Hide Next",
  73.         }
  74.  
  75.         soundTest.load()
  76. ]]
  77.  
  78. local textplus = require("textplus")
  79.  
  80. local soundTest = {
  81.     section = 0,
  82.     font = textplus.loadFont("textplus/font/3.ini"),
  83.     eventNames = {
  84.         toggle = "SoundTest Toggle",
  85.         previous = "SoundTest Previous",
  86.         next = "SoundTest Next",
  87.  
  88.         showPrevious = "SoundTest ShowPrevious",
  89.         hidePrevious = "SoundTest HidePrevious",
  90.         showNext = "SoundTest ShowNext",
  91.         hideNext = "SoundTest HideNext",
  92.     },
  93.     tracks = {},
  94.    
  95.     noMusicText = "NO MUSIC LOADED",
  96.  
  97.     current = 1,
  98.     playing = false,
  99.     playTime = 0
  100. }
  101.  
  102.  
  103. function onInitAPI()
  104.     registerEvent(soundTest, "onEvent")
  105.     registerEvent(soundTest, "onDraw")
  106. end
  107.  
  108.  
  109. function soundTest.load(path)
  110.     soundTest.tracks = Misc.listLocalFiles(Misc.resolveDirectory(path  or  "music"))
  111. end
  112.  
  113. function soundTest.onEvent(eventName)
  114.  
  115.     -- Sound test stuff
  116.     if  eventName == soundTest.eventNames.toggle  then
  117.         soundTest.playing = not soundTest.playing
  118.  
  119.         if  soundTest.playing  then
  120.             Audio.MusicChange(soundTest.section, soundTest.musicFolderPath .. soundTest.tracks[soundTest.current])
  121.         else
  122.             Audio.MusicChange(soundTest.section, 0)
  123.         end
  124.     end
  125.  
  126.     if  eventName == soundTest.eventNames.previous  then
  127.         soundTest.current = math.max(1, soundTest.current-1)
  128.         if  soundTest.current == #soundTest.tracks-1  then
  129.             triggerEvent(soundTest.eventNames.showNext)
  130.         end
  131.         if  soundTest.current == 1  then
  132.             triggerEvent(soundTest.eventNames.hidePrevious)
  133.         end
  134.  
  135.         if  soundTest.playing  then
  136.             Audio.MusicChange(soundTest.section, soundTest.musicFolderPath .. soundTest.tracks[soundTest.current])
  137.         end
  138.     end
  139.  
  140.     if  eventName == soundTest.eventNames.next  then
  141.         soundTest.current = math.min(#soundTest.tracks, soundTest.current+1)
  142.         if  soundTest.current == 2  then
  143.             triggerEvent(soundTest.eventNames.showPrevious)
  144.         end
  145.         if  soundTest.current == #soundTest.tracks  then
  146.             triggerEvent(soundTest.eventNames.hideNext)
  147.         end
  148.  
  149.         if  soundTest.playing  then
  150.             Audio.MusicChange(soundTest.section, soundTest.musicFolderPath .. soundTest.tracks[soundTest.current])
  151.         end
  152.     end
  153. end
  154.  
  155. function soundTest.onDraw()
  156.     if  player.section == soundTest.section  then
  157.  
  158.         local musicNameText = soundTest.noMusicText
  159.         if  #soundTest.tracks > 0  then
  160.             musicNameText = "<align center>" .. tostring(soundTest.current) .. "/" .. tostring(#soundTest.tracks) .. "<br><br>" .. soundTest.tracks[soundTest.current] .. "</align>"
  161.         end
  162.  
  163.         textplus.print{
  164.             text=musicNameText,
  165.             font=soundTest.font,
  166.             xscale=2,yscale=2,
  167.             x=-119600,y=-120496,
  168.             maxWidth=600,
  169.             priority=5,
  170.             pivot=vector(0.5,0),
  171.             sceneCoords=true,
  172.             color=Color.white
  173.         }
  174.  
  175.         if  soundTest.playing  then
  176.             soundTest.playTime = Audio.MusicClock()
  177.         else
  178.             soundTest.playTime = 0
  179.         end
  180.  
  181.         textplus.print{
  182.             text="<align center>" .. os.date("%M:%S", soundTest.playTime) .. ":" .. string.format("%02d",math.floor(100*soundTest.playTime)%100) .. "</align>",
  183.             font=soundTest.font,
  184.             xscale=2,yscale=2,
  185.             x=-119600,y=-120404,
  186.             maxWidth=600,
  187.             priority=5,
  188.             pivot=vector(0.5,0),
  189.             sceneCoords=true,
  190.             color=Color.white
  191.         }
  192.     end
  193. end
  194.  
  195.  
  196. return soundTest
Advertisement
Add Comment
Please, Sign In to add comment