Thread Tools Display Modes
Prev Previous Post   Next Post Next
10-31-10, 04:26 AM   #1
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 793
Playing sounds at unique volume levels (theory)

Two API: PlayMusic() StopMusic()

Using the "music" track for addon sounds? i.e. set all to 25% and this to 100% to distinct certain events.

Prototype:
lua Code:
  1. local frames = {}
  2. local function Create()
  3.     local f
  4.     for i=1, 10 do -- create 10 frames
  5.         f = CreateFrame("Frame")
  6.         f:Hide()
  7.         f:SetScript("OnShow", function(self)
  8.             if not self.sound or not self.len then return end
  9.             SetCVar("musicVolume", self.volume or 1)
  10.             StopMusic()
  11.             PlayMusic(self.sound)
  12.         end)
  13.         f:SetScript("OnUpdate", function(self, elapsed)
  14.             if not self.sound or not self.len then return end
  15.             self.elapsed = (self.elapsed or 0) + elapsed
  16.             if self.elapsed >= self.len then
  17.                 StopMusic()
  18.                 self.sound = nil
  19.                 self.len = nil
  20.                 self.volume = nil
  21.                 self.elapsed = nil
  22.                 self:Hide()
  23.             end
  24.         end)
  25.         table.insert(frames, f)
  26.     end
  27. end
  28. local function GetAvailable()
  29.     for _,f in pairs(frames) do
  30.         if not f.sound and not f.len then
  31.             return f
  32.         end
  33.     end
  34. end
  35. local function GetSoundFileLength(sound)
  36.     local len = 0
  37.     -- NYI
  38.     return len
  39. end
  40. local function PlaySoundFile2(sound, volume)
  41.     local f = GetAvailable()
  42.     f.sound = sound
  43.     f.len = GetSoundFileLength(sound)
  44.     f.volume = volume
  45.     f:Show()
  46. end
  47. local function SetVolumeSettings()
  48.     -- Init. volume settings so the music channel trick works and that normal music is not playing
  49. end

Using PlaySoundFile2("1.mp3", 0.5) will store the sound, len and volume int othe frame and Show it. The onShow will from there set the music volume cvar, stop the last sound and play the current one. The OnUpdate will handle stopping the music when the sound expires, clear the temp variables and hide the frame to avoid OnUpdate performance instability.

The only problem is the GetSoundFileLength part as the game has no way of knowing how long a sound is, but for things like announcements one could run the game where sound effects and such are on 25% while the music is at 100% (and turn off the in-game music so it does not bother you). Now when a sound is played on the music channel it's louder than any other sound. Only instability is that it loops if not stopped (it is in this addon) and that it can not play multiple music files at once so it's pointless for anything but announcements that do not occur too often -perhaps something Bossmods should incorporate.
  Reply With Quote
 

WoWInterface » Developer Discussions » General Authoring Discussion » Playing sounds at unique volume levels (theory)


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off