Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added FlxSoundGroup for managing volume of music/sfx/speech etc separately, closes #362 #1316

Closed
wants to merge 5 commits into from

Conversation

JoeCreates
Copy link
Member

I snuck another small modification in here, too. Just making a for loop more efficient.

FlxSound now has a group. You can set a sound's group either by adding the sound to the group, setting the group of the sound, or by passing the name of a group to play(), load() or playMusic().

Presently groups must be added to the FlxSoundFrontend by calling addGroup.

Example use:

// Create a group named "sfx"
FlxG.sound.addGroup("sfx");
// Play a sound effect
FlxG.sound.play("mysound", 1, false, "sfx");
// Set sfx volume
FlxG.sound.getGroup("sfx").volume = 0.5;

@Gama11
Copy link
Member

Gama11 commented Oct 4, 2014

By default, sounds don't belong to a group it seems... What do you think about flixel setting up a "sfx" (default value in play()) and a "music" (default value in playMusic()) group? That way you'd be able to set sfx and music volume separately out of the box, which is arguably the most common use case.

Overall, this seems like a very flexible yet lightweight feature. Not sure about the string-identifiers, though...

@JoeCreates
Copy link
Member Author

I'm not sure about the string identifiers, either. It seems that something like it might be necessary in order to avoid users having to keep global references to their groups, though.

For defaults, I'm wondering about having a defaultGroup and defaultMusicGroup that can be changed? I'm not super keen on the existing FlxG.sound.music, mind. It doesn't seem to be useful for anything but the most basic case where music is always mutually exclusive. In my current project, music transitions, and certain events trigger a short musical piece over the top of the background music, and these would certainly be classed as music when determining their volume.

I'm wondering if maybe the single FlxG.sound.music could be replaced with a music group if FlxSoundGroup is improved a bit more?

@sruloart
Copy link
Contributor

sruloart commented Oct 4, 2014

"Music group" sounds weird. But Music/Sounds groups are useful for:

  • Preloading sounds on mobile (without it you'll get a noticeable lag), and you can also use it for
  • playing several sounds per event, or
  • dividing your music track into loops by hand and then play them with a timer/event instead of playing the entire song: this can save memory and will also increase your precision with how, when and where you choose to play the music.

You can do all that without the groups, but it will make life a bit more simple.

for (sprite in _sprites)
{
if (sprite != null && sprite.exists && sprite.visible)
if (sprite != null && sprite.exists && sprite.visible && sprite.isOnScreen(Camera))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you changing this logic?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the old way was inefficient. My way produces the exact same result, except it breaks out of the loop as soon as it is clear the result will be true. In the old way, even after the result can be nothing but true, it still loops through every other sprite in the sprite group.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Besides, it's more reabable. :)

@gamedevsam
Copy link
Contributor

I don't see any logic in the sound group to handle pre-loading of sound assets. This seems like one of the main reasons for using sound groups.

@gamedevsam
Copy link
Contributor

Should we wait for pre-load sound functionality or merge this in and add it later @Gama11?

@sruloart
Copy link
Contributor

sruloart commented Oct 6, 2014

I have an old snippet from the forum by @Hasufel, it's pretty straight forward:

//preloading
var soundBank:Array<Sound> = new Array<Sound>();
var soundList = ["sound1.ogg", "sound2.ogg"];
for (i in 0...soundList.length) soundBank.push(Assets.getSound(soundList[i]));
//while in game
soundBank[n].play(); //n being the sound u need

@gamedevsam
Copy link
Contributor

This pull request adds a new class FlxSoundGroup, so pre-loading sounds through that class should be even easier.

@JoeCreates
Copy link
Member Author

Added ability to set default groups.

@Gama11
Copy link
Member

Gama11 commented Oct 6, 2014

@gamedevsam @sruloart I'm not sure how sound groups would be useful for preloading sounds. There already are cache() and cacheAll(). To preload a sound, the asset string key is used, but for a sound to be contained in a group you need a FlxSound instance already which doesn't exist at that point.

@@ -490,10 +495,12 @@ class FlxSound extends FlxBasic
/**
* Call after adjusting the volume to update the sound channel's settings.
*/
private function updateTransform():Void
public function updateTransform():Void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use @:allow() here instead of making the function public.

@sruloart
Copy link
Contributor

sruloart commented Oct 6, 2014

@Gama11 I'm not saying this right here is the complete answer, I was just describing what music/sound groups are good for :)
Anyways,

  • Cache doesn't cover music (so chacheAll doesn't in fact cache all), and also,

To preload a sound, the asset string key is used

  • Say I don't want to cache one sound or cacheAll? what if I have 50 sfx, but only 17 of them are relevant to a scene, and I want to better handle my RAM space by adding and removing many sfx during gameplay? this way I have groups of FlxSounds, storing all the relevant data (including their keys) for me to use when I want to cache or remove only the ones I've planned into/from the RAM.

Does that make sense?

@gamedevsam
Copy link
Contributor

I see what gama is saying... loading (caching) and playing sounds is mostly handled through one static location, the SoundFrontend, like so:

// loads a sound asset and creates an FlxSound object to manage it:
var sound:FlxSound = FlxG.sound.load("path_to_sound");

// loads and plays a sound immediately:
FlxG.sound.play("path_to_sound");

// loads a sound asset, but does not create an FlxSound object:
FlxG.sound.cache("path_to_sound");

You can't really use FlxSoundGroups to manage caching of sounds, since you need an FlxSound to add to the group, and calling FlxG.sound.load("path_to_sound") will load (cache) the sound right then and there.

FlxSoundGroup isn't an asset management solution, it's just a solution to play / stop / change volume on multiple sounds. It's not really useful for anything else.

*/
public function getGroup(name:String):FlxSoundGroup
{
return groups.get(name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The more I think about this change the more I want to automate this functionality. Something like:

public function getGroup(name:String):FlxSoundGroup
{
    if(name != "")
    {
        if(!groups.exists(name))
            groups.set(name, new FlxSoundGroup());
        return groups.get(name);
    }

That would eliminate the need for users to spawn sound groups manually and would simplify their usage.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, was wondering about this. Probably would be a good addition. :)

@sruloart
Copy link
Contributor

sruloart commented Oct 6, 2014

We can discuss the all sounds/assets management deal later, I for one welcome the new FlxSoundGroup in open arms :)

@gamedevsam
Copy link
Contributor

@Gama11: you can merge this if you want, I'm not a big fan of how it's implemented, but I don't have a better suggestion atm.

@Gama11
Copy link
Member

Gama11 commented Nov 19, 2014

I'm unsure as well.

@JoeCreates
Copy link
Member Author

The only alternative I can think of would require a complete overhaul of the front ends stuff to make it easier to extend. I'm not particularly keen to do that myself at the moment. If you main reservation is the use of the string identifiers, then perhaps consider that it is pretty much the same as what we have for sprite animations . . .

@Gama11 Gama11 modified the milestone: 4.0.0 May 30, 2015
@gamedevsam
Copy link
Contributor

Any opposition to this change? It's been lingering for a while, seems like we should merge it and improve it as necessary.

Gama11 added a commit that referenced this pull request Jan 9, 2016
@Gama11 Gama11 closed this in 656549d Jan 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants