CrossBrowdy - Basic tutorial

Audio

Sound FX

With the CB_Speaker static class you can manage sound effects easily. This is done thanks to the jsfx library.

Here is an example managing sound effects:


	//Sound effects example:
	var jsfxObject = CB_Speaker.getJsfxObject(); //Gets the 'jsfx' object.
	if (jsfxObject !== null)
	{
		//Defines the sound effects:
		var library =
		{
			"select":
			{
				"Volume":
				{
					"Sustain": 0.1,
					"Decay": 0.15,
					"Punch": 0.55
				}
			},
			"jump":
			{
				"Frequency":
				{
					"Start": 632.5719976385375,
					"Slide": 0.23934902059283936
				},
				"Generator":
				{
					"Func": "square",
					"A": 0.4414022634702427
				},
				"Filter":
				{
					"HP":0.08955229309222913
				},
				"Volume":
				{
					"Sustain": 0.3053579728670927,
					"Decay": 0.23659526483594398
				}
			},
			"dynamic":
				function()
				{
					return { "Frequency": { "Start": Math.random() * 440 + 220 } };
				},
			"coin":
				jsfx.Preset.Coin
		};
		
		//Loads the sound effects:
		var sfx = CB_AudioDetector.isAPISupported("WAAPI") ? jsfxObject.Live(library) : jsfxObject.Sounds(library); //Uses AudioContext (Web Audio API) if available.
		
		//Plays the sound effects:
		//Note: at least the first time, it is recommended to do it through a user-driven event (as "onClick", "onTouchStart", etc.) in order to maximize compatibility (as some clients could block sounds otherwise).
		try
		{
			sfx.select();
			sfx.jump();
			sfx.dynamic();
			sfx.coin();
		}
		catch(E) { CB_console("Error playing sound: " + E); }
	}

When playing sound effects, at least the first time (you can do it silently as a trick), it is recommended to do it through a user-driven event (as "onClick", "onTouchStart", etc.) in order to maximize compatibility (as some clients could block sounds otherwise).

You can read more about sound effects management in the jsfx library web site.

Check the API documentation to read more about the CB_Speaker static class.

Go back to Guides & Tutorials













Share