Sound

Pi.js sound capabilities allow developers to create immersive audio experiences for their games and applications. Please note that as per browser restrictions sound functions will only work if they are in response to a user input. That is why all the following examples start with a onkey event.

One of the key functions in Pi.js is the play function, which allows developers to play music based on notes. This function takes a string of notes and their respective durations and plays them back in sequence. The notes can be specified using either their names (e.g., "C4" for middle C) or their MIDI values (e.g., 60 for middle C). This makes it easy to create custom soundtracks for games and other interactive applications.

$.screen( "300x200" );
$.print( "Press any key to play sound." );
$.onkey( "any", "down", keyPress, true );
// Key press function
function keyPress() {
	$.play("T250 O3 C4 A4 D4 A4 E4 A4 F4 A4 G4 A4 A4 A4 B4 A4");
}

Another important function in Pi.js is createAudioPool, which allows developers to create a pool of sounds that can be played simultaneously. This is useful for creating sound effects that need to be played in quick succession, such as gunshots or explosions. The function takes an array of sound files and preloads them so that they can be played quickly when needed. Developers can then use the playAudioPool function to play the sounds in the pool.

var bombPool = $.createAudioPool( "/sounds/bomb.wav", 1 );
function keyPress() {
	$.playAudioPool( bombPool );
}
$.ready( function () {
    $.screen( "300x200" );
    $.print( "Press any key to play sound." );
    $.onkey( "any", "down", keyPress );
} );

The sound function in Pi.js allows developers to play a sound for a certain duration and frequency. This is useful for creating simple sound effects, such as beeps or clicks. The function takes a frequency value in hertz and a duration value in milliseconds, and generates a sound wave that is played back through the computer's speakers.

var data = [
  392, 8, "My ", 659, 8, "Bon-", 587, 8, "nie ", 523, 8, "lies ", 587, 8,
  "o-", 523, 8, "ver ", 440, 8, "the ", 392, 8, "o-", 330, 32, "cean\n",
  392, 8, "My ", 659, 8, "Bon-", 587, 8, "nie ", 523, 8, "lies ", 523, 8,
  "o-", 494, 8, "ver ", 523, 8, "the ", 587, 40, "sea\n", 392, 8, "My ",
  659, 8, "Bon-", 587, 8, "nie", 523, 8, " lies ", 587, 8, "o-", 523, 8,
  "ver ", 440, 8, "the ", 392, 8, "o-", 330, 32, "cean\n", 392, 8, "Oh ",
  440, 8, "bring ", 587, 8, "back ", 523, 8, "my ", 494, 8, "Bon-", 440,
  8, "nie ", 494, 8, "to ", 523, 32, "me"
];
var i;
var totalDuration = 0;
var volume = 0.15;
var attackRate = 0.01;
var decayRate = 0.1;
var sustainRate = 0.89;
var printTimeouts = [];
$.screen( "300x200" );
$.print( "Press any key to play sound." );
$.onkey( "any", "down", keyPress, true );
function keyPress() {
	for( i = 0; i < data.length; i += 3 ) {
	var freq = data[ i ];
	var duration = data[ i + 1 ] / 18.2;
	var sustain = duration * sustainRate;
	var attack = duration * attackRate;
	var decay = duration * decayRate;
	var word = data[ i + 2 ];
	$.sound(
		freq, sustain, volume, "sawtooth", totalDuration, attack, decay
	);
	printWord( word, totalDuration );
	totalDuration += duration;
	}
}
function printWord( word, delay ) {
	printTimeouts.push( setTimeout( function () {
		$.print( word, word.indexOf( "\n" ) === -1 );
	}, delay * 1000 ) );
}

Overall, Pi.js provides a robust set of sound capabilities that can be used to create immersive audio experiences for games and other interactive applications. The library's sound functions make it easy to create custom soundtracks, play sound effects, and manage multiple sounds simultaneously. Whether you're a beginner or an experienced developer, Pi.js provides a powerful set of tools for creating engaging audio experiences on the web.

Please checkout the Pi.js Reference page to see a full listing of sound functions.