Advertisement
minimic2002

Roblox Sound Script (Playlists And Sound Data)

May 24th, 2020 (edited)
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. -- Minis Sound Script
  2. -- https://developer.roblox.com/en-us/api-reference/class/Sound
  3.  
  4.  
  5. -- AddSound
  6. function AddSound(ID,Name,WhereToAdd)
  7. local NewSound = Instance.new("Sound",WhereToAdd)
  8. NewSound.SoundId = ID
  9. NewSound.Name = Name
  10. end
  11.  
  12. -- Play
  13. function Play(Sound)
  14. Sound:Play()
  15. end
  16.  
  17. -- Stop
  18. function Stop(Sound)
  19. Sound:Stop()
  20. end
  21.  
  22. -- Resume
  23. function Resume(Sound)
  24. Sound:Resume()
  25. end
  26.  
  27. -- Pause
  28. function Pause(Sound)
  29. Sound:Pause()
  30. end
  31.  
  32. -- GetNumbSoundId
  33. function GetNumbSoundId(SoundId)
  34. local Split = string.split(SoundId,"/")
  35. local NumbId = tonumber(Split[3])
  36. return NumbId
  37. end
  38.  
  39. -- GetSoundName
  40. function GetSoundName(SoundId)
  41. local MS = game:GetService("MarketplaceService")
  42. local NumbId = GetNumbSoundId(SoundId)
  43. local Asset = MS:GetProductInfo(NumbId)
  44. return Asset.Name
  45. end
  46.  
  47. -- GetSoundStats (Loudness Is Based Off Scripts Position)
  48. function GetSoundStats(Sound)
  49. -- Log Properties That Will Be Changed
  50. local BPlaying = Sound.Playing
  51. local BTimePos = Sound.TimePosition
  52. local BMaxD = Sound.MaxDistance
  53. local BLooped = Sound.Looped
  54. local BEmitterSize = Sound.EmitterSize
  55.  
  56.  
  57. -- PrepForQuickPlay
  58. Sound.Playing = false
  59. Sound.TimePosition = 0
  60. Sound.Volume = 0
  61. Sound.PlaybackSpeed = 1
  62. Sound.EmitterSize = 99999
  63. Sound.Looped = false
  64.  
  65.  
  66. -- Running Test
  67. Sound.Playing = true
  68. local LoundessLog = {}
  69. local SampleCount = 0
  70. while Sound.Playing == true do
  71. SampleCount = SampleCount + 1
  72. table.insert(LoundessLog,1,Sound.PlaybackLoudness)
  73. wait(1/200)
  74. end
  75.  
  76. -- GetAverageLoudness and HighestLoudness
  77. local AcumulatedLoudness = 0
  78. local HighestLoudness = 0
  79.  
  80. for i = 1,#LoundessLog do
  81. AcumulatedLoudness = AcumulatedLoudness + LoundessLog[i]
  82. if LoundessLog[i] > HighestLoudness then
  83. HighestLoudness = LoundessLog[i]
  84. end
  85. end
  86.  
  87. -- Data
  88. local SoundId = Sound.SoundId
  89. local Name = GetSoundName(Sound.SoundId) --
  90. local HighestLoudness = HighestLoudness --
  91. local AverageLoudness = AcumulatedLoudness / SampleCount --
  92. local Duration = Sound.TimeLength --
  93.  
  94. local Data = {SoundId,Name,Duration,AverageLoudness,HighestLoudness}
  95. end
  96.  
  97. -- SaveSoundData
  98. function SaveSoundData(Data)
  99. local DataStoreService = game:GetService("DataStoreService")
  100. local NewStore = DataStoreService:GetDataStore("Minimic2002SoundData")
  101.  
  102. -- Makes It Into 1 String As You Cant Save Arrays
  103. local NewData = ""
  104. for i = 1,#Data do
  105. NewData = NewData .. ";" .. tostring(Data[i])
  106. end
  107. NewStore:SetAsync("Sound " ..Data[1],NewData) -- Sets All the Data To SoundId
  108. end
  109.  
  110. -- GetSoundData
  111. function GetSoundData(SoundId)
  112. local DataStoreService = game:GetService("DataStoreService")
  113. local NewStore = DataStoreService:GetDataStore("Minimic2002SoundData")
  114. local Result = NewStore:GetAsync("Sound " ..SoundId)
  115.  
  116. -- Turn Data Back Into An Array
  117. local Split = string.split(Result,";")
  118. local Data = {}
  119. for i = 1,#Split do
  120. local ToN = tonumber(Split[i])
  121. if ToN == nil then
  122. table.insert(Data,Split[i])
  123. else
  124. table.insert(Data,ToN)
  125. end
  126.  
  127. end
  128.  
  129. return Data
  130. end
  131.  
  132. -- SoundData
  133. function SoundData(Sound)
  134. local SoundId = Sound.SoundId
  135.  
  136. -- AttemptToGetData
  137. local SoundData = GetSoundData(SoundId)
  138. if SoundData == nil then
  139. -- Log Sound As There Is No Data On It
  140. local Clone = Sound:Clone() -- Clone It So Original Uneffected
  141. Clone.Parent = Sound.Parent -- Play From Same Location
  142. local SoundStats = GetSoundStats(Sound)
  143. SaveSoundData(SoundStats) -- Saves Data From Stats
  144.  
  145. else
  146. return SoundData -- Found Data And Is Returning It
  147. end
  148. end
  149.  
  150.  
  151. -- PlaylistFunctions
  152.  
  153. -- AddToPlaylist()
  154. function AddToPlaylist(Playlist,SoundId)
  155. local DataStoreService = game:GetService("DataStoreService")
  156.  
  157. local NewStore = DataStoreService:GetDataStore("Minimic2002PlaylistData")
  158. local Result = NewStore:GetAsync("Playlist " ..Playlist)
  159. if Result == nil then -- Nothing There Yet
  160. -- Starts A New Store For That Playlist
  161. NewStore:SetAsync("Playlist " ..Playlist,SoundId)
  162. else
  163. -- Add Onto String
  164. local PlaylistData = Result .. ";" .. SoundId
  165. NewStore:SetAsync("Playlist " ..Playlist,PlaylistData)
  166. end
  167. end
  168.  
  169. -- GetPlaylistData
  170. function GetPlaylistData(Playlist)
  171. local DataStoreService = game:GetService("DataStoreService")
  172. local NewStore = DataStoreService:GetDataStore("Minimic2002PlaylistData")
  173. local Result = NewStore:GetAsync("Playlist " ..Playlist)
  174.  
  175. local Split = string.split(Result,";")
  176. local NewPlaylistData = {}
  177. for i = 1,#Split do
  178. table.insert(NewPlaylistData,Split[i])
  179. end
  180.  
  181. return NewPlaylistData
  182. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement