View Single Post
06-11-20, 05:07 PM   #9
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 666
I received a private message asking to customize the volume in my code. I also didn't notice the last post here. Here's an updated code that allows you to replace any UI sound (anything that uses PlaySound) with a custom sound OR adjust the volume of a UI sound.

Lua Code:
  1. local entries={
  2.     [8960]=1 -- ready check
  3.  -- [ID]="path\\to\\sound"
  4.  -- [ID]=volume
  5. }
  6.  
  7.  -- the table above is where you enter the sound ID you want to change,
  8.  -- with the volume you want it at OR the path to a custom sound you want.
  9.  -- for example, [8960]=0.7 will play the ready check sound at 70%.
  10.  -- a custom example of [8960]="chicken.ogg" will play the sound file at
  11.  -- World of Warcraft\_retail_\chicken.ogg instead of the default ready check.
  12.  
  13.  -- custom sounds can be placed alone anywhere in the running client's folder,
  14.  -- they do not need to be used by another addon. Examples:
  15.  -- _retail_\Sound\chicken.ogg
  16.  -- _classic_\Interface\AddOns\chicken.ogg
  17.  -- _classic_\WTF\chicken.ogg
  18.  -- _retail_\ASDFGHJKL\chicken.ogg
  19.  -- the path needed here is everything AFTER the _retail_ or _classic_ folder,
  20.  -- the sound file has to be there when WoW is started to be recognized.
  21.  
  22.  -- this code is unable to control the volume of a custom sound,
  23.  -- you have to adjust the volume of the sound file itself.
  24.  -- PlaySoundFile also does not guarantee no dupes.
  25.  
  26. local pool,f={},CreateFrame('frame') f.handles={} f.lastchan={} f.nodupeids={} f.nodupehandles={}
  27. function f.pool(t,i)
  28.     if i then pool[i]:SetScript('OnUpdate',nil) pool[i].t=nil return end
  29.     for i=1,#pool do
  30.         if pool[i] and not pool[i].t then pool[i].t={i=i,t=t} return i end
  31.     end
  32.     tinsert(pool,CreateFrame('frame')) i=#pool pool[i].t={i=i,t=t} return i
  33. end
  34. f:SetScript('OnEvent',function(_,_,handle)
  35.     if f.nodupehandles[handle] then
  36.         f.nodupeids[f.nodupehandles[handle]]=nil
  37.         f.nodupehandles[handle]=nil
  38.     end
  39.     if f.handles[handle] then
  40.         if f[f.handles[handle]] and f.lastchan[f.handles[handle]]==handle then
  41.             SetCVar(f.handles[handle],f[f.handles[handle]])
  42.             f[f.handles[handle]]=nil
  43.         end
  44.         f.handles[handle]=nil
  45.     end
  46. end)
  47. f:RegisterEvent('SOUNDKIT_FINISHED')
  48. hooksecurefunc('PlaySound',function(id,channel,nodupe,_,_,_,_,own)
  49.     if not own and not f.nodupeids[id] and entries[id] then
  50.         local _,handle=PlaySound(64,'Master',false)
  51.         if handle then
  52.             StopSound(handle) StopSound(handle-1)
  53.             channel=(not channel and 'Master') or (strlower(channel)=='sfx' and 'SFX') or strlower(channel):gsub('^%a',strupper)
  54.             local cvar='Sound_'..channel..'Volume'
  55.             pool[f.pool({id,channel,nodupe,cvar})]:SetScript('OnUpdate',function(self)
  56.                 local handle=nil
  57.                 if type(entries[self.t.t[1]])=="string" then
  58.                     _,handle=PlaySoundFile(entries[self.t.t[1]],self.t.t[2],self.t.t[3])
  59.                 else
  60.                     _,handle=PlaySound(self.t.t[1],self.t.t[2],self.t.t[3],true,nil,nil,nil,true)
  61.                 end
  62.                 if type(entries[self.t.t[1]])=="number" and handle then
  63.                     if self.t.t[3]~=false then
  64.                         f.nodupeids[self.t.t[1]]=handle
  65.                         f.nodupehandles[handle]=self.t.t[1]
  66.                     end
  67.                     f.lastchan[self.t.t[4]]=handle
  68.                     f.handles[handle]=self.t.t[4]
  69.                     if not f[self.t.t[4]] then
  70.                         f[self.t.t[4]]=GetCVar(self.t.t[4])
  71.                         if f[self.t.t[4]] then SetCVar(self.t.t[4],entries[self.t.t[1]]) end
  72.                     end
  73.                 end
  74.                 f.pool('',self.t.i)
  75.             end)
  76.         end
  77.     end
  78. end)
  Reply With Quote