HeroEngine Forums
Welcome, Guest. Please login or Register for HeroCloud Account.

Author Topic: [solved] FX audio volume  (Read 1114 times)

RogerD

  • World Owners
  • ****
  • Posts: 10
    • View Profile
[solved] FX audio volume
« on: Jul 18, 12, 02:15:42 PM »

I've added some sound effects to our game using the FX system.  They work fine.  But now I'd like to make the audio volume user-settable.  I can't for the life of me figure out how to get to the volume for the fx from within HeroScript.  Can anyone offer any advice?
« Last Edit: Jul 20, 12, 01:07:54 PM by RogerD »
Logged

FI-ScottZ

  • General Accounts
  • *
  • Posts: 1407
    • View Profile
    • Forever Interactive, Inc.
Re: FX audio volume
« Reply #1 on: Jul 18, 12, 07:07:23 PM »

This forum topic details some of my investigation of fmod sounds in HeroEngine.

What I have found is that while the old DirectMusic segments could be played either as 3D sound nodes, or directly as stereo sounds with no node created (which is what those Sound System methods allow), unfortunately there is no way to play an fmod sound right now except by creating an hbnode.  If you look at the code that the fx fmod task performs, you can see how they create the node.  My link above shows how you can specify in code the event path for a fmod sound from fx.  If you handled the ONFXSTART event, you could get access to the created node.

Once you have the node ref, you can set its volume via the volume property as such:
fmodNode["volume"] = 1.0

The server class _AudioFMODEventWritableInstanceProperties details the properties that can be set, which is much less than there were for DM sounds.

You can create fmod nodes yourself in a client script via a prop bucket in a similar fashion to how the method Play3D in SoundClassMethods creates a DM node.
Logged
Scott Zarnke
Lead Programmer, Visions of Zosimos
CTO, Forever Interactive, Inc.

RogerD

  • World Owners
  • ****
  • Posts: 10
    • View Profile
Re: FX audio volume
« Reply #2 on: Jul 18, 12, 07:26:32 PM »

access to the created node.

Once you have the node ref, you can set its volume via the volume property as such:
fmodNode["volume"] = 1.0

The server class _AudioFMODEventWritableInstanceProperties details the properties that can be set, which is much less than there were for DM sounds.

Excellent, thanks Scott.  This is exactly the info I was looking for!

I had eventually found a hacky way to set the property by instantiating the fx via _InstantiateFxFromSpec and picking out the _fxVolume parameter from it, but I did not really understand what the code I cobbled together was doing.  What you posted here is a lot better.

The other link was great reading too.  I was dreading creating lots and lots of FX, both because that GUI is awkward to use and because it would get all cluttered up with lots of tiny, almost identical FX.  I'm going to try to do the same thing.  Thanks for sharing your discoveries.
Logged

RogerD

  • World Owners
  • ****
  • Posts: 10
    • View Profile
Re: [solved] FX audio volume
« Reply #3 on: Jul 20, 12, 01:20:01 PM »

Just as an update, I got all of my audio options working the way I wanted.  I cribbed your idea of playing a 2D sound, and I additionally saved and tracked the sound and saved it in a list of currently playing sounds:

Code: [Select]
shared function _OnFxGroupPreLoad(theFxGroup as NodeRef of Class _FxGroup)
  //----------------------------------------------------------------------
  // Called just before resources are loaded into prop buckets
  // This call allows you to dynamically change the resources to be loaded
  //----------------------------------------------------------------------
  if theFXGroup._fxName == "Audio"
    theFXGroup._fxFireRate = theFXGroup._fxDynData["duration"]   
    theFXGroup._fxTimeOut = theFXGroup._fxFireRate + 0:00:01.000
   
    task as NodeRef of Class _Fx3DSoundFMOD = theFXGroup.FindFxTaskByName("Sound")
    if task != None
      task._fxResourceName = theFXGroup._fxDynData["soundpath"]
      task.EventPath = theFXGroup._fxDynData["eventpath"]
      task._fxVolume = theFXGroup._fxDynData["volume"]
      AudioManagerClassMethods:TrackSound(task, theFXGroup._fxDynData["category"], true)
    .
  .
.

As you can see, I had to adjust the timeouts to be customized for each sound I sent, or I'd end up with songs that cut off to early or having effects hanging out in the background forever.

Then, I handled the ONFXSTOP event.  In that event, I stop tracking a sound when it is completed.
Code: [Select]
shared function _FxEventOccurance(event as String, fxName as String, theFxGroup as NodeRef of Class _FxGroup)
  var assets = QueryAssociation(theFxGroup, "ActiveFxAsset", None)
  if theFXGroup._fxName == "Audio"
    task as NodeRef of Class _Fx3DSoundFMOD = theFXGroup.FindFxTaskByName("Sound")
    if event = "ONFXSTOP"
      AudioManagerClassMethods:UntrackSound(task)
    .
  . 
.

I pass in the correct volume (based on user settings) when I create the effect.  If the volume changes later while an effect is already playing (like for background music), I look through a list of tracked sounds and set the volume parameter on each one:

Code: [Select]
method ResetSoundLevels(sl references LookupList indexed by ID of ID, newVolume as Float)
  foreach task in sl
    var assets = QueryAssociation(task, "ActiveFxAsset", None)
    if assets.length > 0
      instance as NodeRef of Class HBNode = assets[1].target
      if instance != None
        instance["Volume"] = newVolume
      .
    .
  .
.

That changes the volume even of an already-playing sound.

Thanks again for your help in getting me started.  I'm happy to say that my audio configuration works exactly the way I want it to now.
Logged

FI-ScottZ

  • General Accounts
  • *
  • Posts: 1407
    • View Profile
    • Forever Interactive, Inc.
Re: [solved] FX audio volume
« Reply #4 on: Jul 20, 12, 02:49:38 PM »

Great to hear.  As an additional challenge, you could use the spec system to define an audio spec, use its spec editor as a tool to define sound properties visually, and then play sounds by their spec key. 8)
Logged
Scott Zarnke
Lead Programmer, Visions of Zosimos
CTO, Forever Interactive, Inc.