Give me advice about game sounds

Hi!

I need your advice about game sounds.
I have about 100 short sounds and about 5 long music tracks. I combine them in .fla file and compile .swf file.
In my AIR game I dynamically load .swf and use it such way:

var appDomain:ApplicationDomain = null;
for each(appDomain in _applicationDomainArray)
{
	if(appDomain.hasDefinition(name))
	{
		var temp:Class = appDomain.getDefinition(name) as Class;
		return new temp() as Sound;
	}
}

What can your recommend in OpenFL/Starling version of game?
Is it a good idea to embed about 100 .ogg files in project.xml?

How do your use sounds in your projects?

Thx!

For sound effects, itā€™s probably fine.

For music tracks, it depends on how long the tracks are and when you choose to load them. Iā€™ve found that OpenFL takes a while to load .ogg music files and that can cause hiccups in the CPU. We were loading music tracks in our OpenFL game (targeting a Windows .exe) as things were animating and there was always about a half second stutter.

If you can stomach using more disk space, .wav files will load very quickly. Otherwise, you might have to try loading the .ogg files on a separate thread.

What mechanism do you use to load .ogg/.wav files?

embed=true in project.xml

Starling AssetManager

Sound.loadFromFile

or anything else?

I use Starlingā€™s AssetManager with .ogg files

assetManager.enqueueWithName(Config.PATH_MUSIC + fileName + fileExtension, musicID);
assetManager.loadQueue(callbackFunction);

private function callbackFunction():Void
{
   trace("music files loaded");
}
2 Likes

What about formats? Safari doesā€™t support .ogg
How to configure OpenFL/Starling project to use different formats for one game sound?

I use conditionnal compiling or whatever itā€™s called :slight_smile:

like

var fileExtension:String;
#if flash
fileExtension = ".mp3";
#elseif sys
fileExtension = ".ogg";
#end

I donā€™t have much web experience with openFL, if there is a format that will work with all browsers (mp3 maybe ?) then you can do

#if html5
fileExtension = ".mp3";
#end

Otherwise you will probably have to mix conditionnal compiling + safari detection

1 Like

I think there is a mechanism inside Lime or OpenFL to automate that.

Like using html5 ā€˜audioā€™ tag:

<audio controls>
  <source src="myAudio.mp3" type="audio/mpeg">
  <source src="myAudio.ogg" type="audio/ogg">  
</audio>

Somebody knows??)

There might be an even better way, but I know of a couple of options (Note: my examples will use OpenFLā€™s asset system, rather than Starlingā€™s).

  1. In project.xml, give each sound asset an id, but use a different file extension for different targets:
<assets path="sounds/my_sound.mp3" id="sounds/my_sound" if="html5"/>
<assets path="sounds/my_sound.ogg" id="sounds/my_sound" if="cpp"/>

Since they both have the same id, the Haxe code shouldnā€™t need any conditional compilation:

var sound:Sound = Assets.getSound("sounds/my_sound");
  1. If you have a lot of sounds, it might be somewhat tedious to list every individual sound file in your project.xml, so you could include all sounds with a particular file extension like this:
<assets path="sounds" include="*.mp3" if="html5"/>
<assets path="sounds" include="*.ogg" if="cpp"/>

Then, you need only minor conditional compilation in your Haxe code to differentiate between extensions:

private static final SOUND_FILE_EXTENSION:String = #if html5 ".mp3" #else ".ogg" #end;

Which youā€™d use like this:

var sound:Sound = Assets.getSound("sounds/my_sound" + SOUND_FILE_EXTENSION);
4 Likes

Thx!

But how about one html5 target with different extensions?
.ogg for all browsers except Safari and .mp3 for Safari?

That kind of detection would need to be done at run-time rather than compile-time. I donā€™t think OpenFL has an API for that.

However, hereā€™s one way to do it manually from Haxe by calling native JS APIs:

var htmlWindow = cast(js.Lib.global, js.html.Window);
var audio = cast(htmlWindow.document.createElement("audio"), js.html.Audio);
var canPlayOgg = audio.canPlayType("audio/ogg; codecs=vorbis") != "";
trace("can play ogg? " + canPlayOgg);

If you plan to support native targets too, youā€™ll obviously need to wrap that section in #if html5 and #end.

2 Likes

Hi all!

  1. I have an ā€œUnknown errorā€ in Starling asset manager while adding .wav files. Is it supported?
    No problem with .ogg.

  2. I have another problem. How to start background game music without a user gesture on the page?
    Chrome prevent it - Autoplay policy in Chrome - Chrome Developers

Thx!

Hi

On which target ? Pretty sure I used .wav on windows with no issue. If itā€™s a Starling issue Iā€™ll look into it

My experience with web is limited/old but I think all browsers do this and thereā€™s no way around it ? Kinda like fullscreen, I think you have to make the user click or tap something in your app first (I could be wrong)

html5.

Example:

_assets = new AssetManager(1);
        _assets.enqueue([
            Assets.getPath("assets/sounds/village_loop.ogg"), // no errors
            Assets.getPath("assets/sounds/ambar.wav") // error
        ]);

 _assets.loadQueue(onComplete, onError);

private function onError(str:String):Void
{
    // str = "Unknown error" for ambar.wav
}

File ambar.wav (if you need)

I only did a quick test on getting the path but it works for me : could it be something with your project.xml asset settings ?

if you look into your html5/bin/assets/sounds folder, is the ambar.wav file there ?

Yes.
I have
<assets path="assets/sounds" embed="false" />
in project.xml

I converted .wav to .mp3 and now it works.

About my second question. I added temporal global listener to first TouchEvent from user to start music. I donā€™t know is it a good solution, but it works.

P.S. If it is interested - my current game working progress - Mushroomers Demo

My bad, I thought you were having an error when trying to get the path

Iā€™ll do some further checking on this.

The game looks good already, I was lucky to find the button to change the language :slight_smile: I love how russian looks but I canā€™t read anything ^^

I got some mushrooms but didnā€™t really understand what I was supposed to do :slight_smile:

By the way, you might be interested in knowing that there is a port of as3 feathers for openFL-starling in progress. I donā€™t know how much time it will take though.

It is only part of game) It will be ā€œcity builderā€ with mushrooms collecting as economic part of game.

ā€œfeathers for openFL-starlingā€ sounds interesting! As for me I need reach textField stuff form it.