CrossBrowdy - Basic tutorial

Audio

Music composition

With the CB_Speaker static class you can manage music composition easily. This is done thanks to the Band.js library.

Here is an example managing music composition:


	//Music composition example (taken from https://github.com/meenie/band.js):
	var bandJSObject = CB_Speaker.getBandJSObject(); //Gets a new 'BandJS' object.
	if (bandJSObject !== null)
	{
		//Sets a time signature:
		bandJSObject.setTimeSignature(4, 4);
		
		//Sets the tempo:
		bandJSObject.setTempo(80);

		//Creates an instrument and adds notes:
		var piano = bandJSObject.createInstrument();
		piano.note("quarter", "C4");
		piano.note("quarter", "D4");
		piano.note("quarter", "E4");
		piano.note("quarter", "F4");

		//Finishes and plays it:
		//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).
		var player = bandJSObject.finish();
		player.play()
	}

When playing music composed, 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 it in the Band.js library web site.

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

Go back to Guides & Tutorials

« PrevReturnNext »
CrossBrowdy by Joan Alba Maldonado