Reference | Audio
These are audio functions.
- PS.AudioLoad ( file, path )
- PS.AudioPlay ( file, volume, func, path, data )
- PS.AudioPause ( channel )
- PS.AudioStop ( channel )
- PS.AudioPath ( path )
- PS.Piano ( note )
- PS.Harpsichord ( note )
- PS.Xylophone ( note )
- Audio Library
PS.AudioLoad ( file, path )
PS.AudioLoad() preloads an audio file without playing it. If you know you're going to use a sound in your game, loading it in advance can help to reduce network lag.
Parameters:
- file : string
- (optional) path : string
Returns: object
Default: null
The file parameter must be a string matching the filename of the audio to be played. If the default Perlenspiel library path is used, it must be the name of one of the sounds in the Audio Library. Be sure the filename is spelled correctly! Note that filenames are all lower-case, and that no file extension should be included.
The optional path parameter must be a string containing the complete, case-sensitive pathname of the directory containing the sound file specified in the id parameter. Sound files should be in 16-bit .wav format, 22.050 or 44.100kHz sampling rate, mono or stereo.
If no path parameter is passed, or the value undefined or PS.CURRENT is passed, the current audio path is used. If the value PS.DEFAULT is specified for path, the default Perlenspiel directory pathname is used.
The return value is true if no errors occur. If the load fails, or a parameter error occurs, the value PS.ERROR is returned.
Example:
PS.Init = function ( options )
{
// set up a 10 x 10 grid
PS.GridSize( 10, 10 );
// preload a sound for later use
PS.AudioLoad( "fx_click" );
};
PS.AudioPlay ( file, volume, func, path, data )
PS.AudioPlay() plays an library audio file (loading it first if necessary), and returns the channel number associated with that file.
Parameters:
- file : string
- (optional) volume : float
- (optional) func : function
- (optional) path : string
- (optional) data : anything
Returns: integer
Default: 0
The file parameter must be a string matching the filename of the audio to be played. If the default Perlenspiel library path is used, it must be the name of one of the sounds in the Audio Library. Be sure the filename is spelled correctly! Note that filenames are all lower-case, and that no file extension should be included.
The optional volume parameter controls the level at which the sound will be played. It must be a number between 0.0 (total silence) and 1.0 (full volume). Any value outside this range is clamped. If the value PS.DEFAULT is specified, or no volume parameter is provided, the default value (0.5, 50% volume) is used.
The optional func and data parameters let you specify a function that will be called when the playback of the sound is paused or concluded. The func parameter must be a valid Javascript function. The data parameter can be any Javascript data type. When the sound ends, the function specified in func is called with data as its sole parameter.
If PS.DEFAULT is specified for func, the default behavior (no on-end function) is applied. If a func parameter is supplied without a data parameter, the parameter passed to func will be the channel number associated with the sound file.
NOTE: The stop function is not called when a sound is paused with a call to PS.AudioPause(), or stopped with a call to PS.AudioStop(). However, it is called at the conclusion of a sound that was paused during playback one or more times with PS.AudioPause().
The optional path parameter must be a string containing the complete, case-sensitive pathname of the directory containing the sound file specified in the id parameter. Sound files should be in 16-bit .wav format, 22.050 or 44.100kHz sampling rate, mono or stereo.
If no path parameter is passed, or the value undefined or PS.CURRENT is passed, the current audio path is used. If the value PS.DEFAULT is specified for path, the default Perlenspiel directory pathname is used.
The return value is a number representing the channel associated with the sound file. This value can be used as a parameter to PS.AudioPause() and PS.AudioStop() to pause or stop a previously started sound. If you don't need to explicitly pause or stop a sound, this value can be ignored.
If a file loading or parameter error occurs, the value PS.ERROR is returned.
Example:
PS.Click = function ( x, y, data, options )
{
// play a click when any bead is touched
PS.AudioPlay( "fx_click" );
};
PS.AudioPause ( channel )
PS.AudioPause() lets you pause and/or resume an audio channel.
Parameters:
- channel : integer
Returns: integer
Default: 0
The channel parameter should be a number previously returned by a call to PS.AudioPlay().
If the specified channel is still playing, it will pause immediately.
If the specified channel is already paused as a result of a previous call from PS.AudioPause(), it will resume playing at the point from which it was paused.
If the specified channel is not playing, paused or stopped, or if the channel is invalid, nothing happens.
The return value is the channel number, or PS.ERROR if an error occurs.
PS.AudioStop ( channel )
PS.AudioStop() lets you stop an audio channel.
Parameters:
- channel : integer
Returns: integer
Default: 0
The channel parameter should be a number previously returned by a call to PS.AudioPlay().
If the specified channel is still playing, it will stop immediately.
If the specified channel is currently paused by a previous call from PS.AudioPause(), its status will change from “paused” to “stopped,” and subsequent calls from PS.AudioPause() will have no effect.
If the specified channel is not playing or paused, or if the channel is invalid, nothing happens.
The return value is the channel number, or PS.ERROR if an error occurs.
PS.AudioPath ( path )
PS.AudioPath() lets you change the current path used to load audio files.
Parameters:
- path : string
- PS.CURRENT
- PS.DEFAULT
Returns: string
The optional path parameter must be a string containing the complete, case-sensitive path of a directory containing the required sound files. This path will be used for all subsequent calls to PS.AudioLoad() and PS.AudioPlay() that do not include an explicit path parameter.
If no parameter is passed, or the value undefined or PS.CURRENT is passed, the current audio path is maintained.
If the value PS.DEFAULT is passed, the current audio path reverts to the Perlenspiel system library.
The return value is the new current audio path, or PS.ERROR if an error occurs.
PS.Piano ( note, flag )
PS.Piano() returns the filename of an indexed piano note in the Audio Library.
Parameters:
- note : integer
- (optional) flag : boolean
Returns: string
The 88 notes on Perlenspiel's piano range from A0 to C8.
PS.Piano() takes a note parameter between 1 and 88 inclusive, and returns a string representing the filename of the associated piano sound. Values outside this range are clamped.
There are two versions of each piano note, short (about 1 second in duration) and long (about 4 seconds). The optional flag parameter determines which version's filename is returned. If flag is false or not supplied, the short version is returned. If flag is true, the long version is returned.
If a parameter error occurs, the value PS.ERROR is returned.
Examples:
PS.Click = function ( x, y, data, options )
{
// play SHORT version of piano note 44
// when any bead is clicked
PS.AudioPlay( PS.Piano(44) );
};
PS.KeyDown = function ( key, shift, ctrl, options )
{
// play LONG version of piano note 32
// when any key is pressed
PS.AudioPlay( PS.Piano( 32, true ) );
};
PS.Harpsichord ( note, flag )
PS.Harpsichord() returns the filename of an indexed harpsichord note in the Audio Library.
Parameters:
- note : integer
- (optional) flag : boolean
Returns: string
The 57 notes on Perlenspiel's harpsichord range from A2 to F7.
PS.Harpsichord() takes an index value between 1 and 57 inclusive, and returns a string representing the filename of the associated harpsichord sound. Values outside this range are clamped.
There are two versions of each harpsichord note, short (about 1 second in duration) and long (about 4 seconds). The optional flag parameter determines which version's filename is returned. If flag is false or not supplied, the short version is returned. If flag is true, the long version is returned.
If a parameter error occurs, the value PS.ERROR is returned.
PS.Xylophone ( note )
PS.Xylophone() returns the filename of an indexed xylophone note in the Audio Library.
Parameters:
- note : integer
Returns: string
The 39 notes on Perlenspiel's xylophone range from A4 to B7.
PS.Xylophone() takes an index value between 1 and 39 inclusive, and returns a string representing the filename of the associated xylophone sound. Values outside this range are clamped.
If a parameter error occurs, null is returned.
Audio Library
The Perlenspiel audio library contains nearly 400 sounds that you can use in your games, including a full range of piano, harpsichord and xylophone notes, dozens of percussion instruments, and sound effects ranging from simple to silly.
To use these sounds, pass the filenames below as a string to the PS.AudioPlay() command. Be sure the filename is spelled correctly! Note that filenames are all lower-case.
Example:
PS.Click = function ( x, y, data, options )
{
// play a click when a bead is touched
PS.AudioPlay( "fx_click" );
};
If you prefer a local copy of the audio files for development, right-click here and choose Save Link As to download a .zip archive containing all of the sounds. Unzip the archived /audio/ directory to a convenient location and use PS.AudioPath() to point Perlenspiel's default audio path to your local directory.
Click on any filename to hear what it sounds like.
Sound effects
All sound effect filenames begin with an “fx_” prefix.
- fx_click: Your basic click
- fx_tick: Metallic tick
- fx_bang: Small bang
- fx_blip: Electronic blip
- fx_bloop: Electronic bloop
- fx_boop: Electronic boop
- fx_beep: Double electronic beep
- fx_pop: Pop
- fx_chirp1: High electronic chirp
- fx_chirp2: Low electronic chirp
- fx_ding: Ding
- fx_swoosh: Swoosh
- fx_bucket: A kicked bucket
- fx_drip1: Liquid drip
- fx_drip2: Liquid drip with pop
- fx_squawk: Duck squawk
- fx_hoot: Windy hoot
- fx_whistle: Police whistle
- fx_uhoh: High-speed “Uh-oh,” for errors
- fx_bomb1: Bomb explosion 1
- fx_bomb2: Bomb explosion 2
- fx_bomb3: Bomb explosion 3
- fx_gun: Your basic gun
- fx_shotgun: Your basic shotgun
- fx_magnum: Your basic .357 Magnum
- fx_silencer: Gunshot with silencer
- fx_tada: Tacky “ta-daaa!” fanfare
- fx_wilhelm: The classic Wilhelm scream
Percussion instruments
All percussion filenames begin with a “perc_” prefix.
- perc_drum_snare: Snare drum hit
- perc_drum_bass: Bass drum kick
- perc_drum_tom1: Tom-tom 1 (high)
- perc_drum_tom2: Tom-tom 2 (medium)
- perc_drum_tom3: Tom-tom 3 (medium-low)
- perc_drum_tom4: Tom-tom 4 (low)
- perc_cymbal_crash1: Crash cymbal 1
- perc_cymbal_crash2: Crash cymbal 2
- perc_cymbal_crash3: Crash cymbal 3
- perc_cymbal_crash4: Crash cymbal 4
- perc_cymbal_ride: Ride cymbal
- perc_hihat_closed: Closed hi-hat
- perc_hihat_open: Open hi-hat
- perc_hihat_pedal: Pedaled hi-hat
- perc_bongo_low: Low bongo
- perc_bongo_high: High bongo
- perc_conga_low: Low conga
- perc_conga_high: High conga
- perc_block_low: Low woodblock
- perc_block_high: High woodblock
- perc_block_low: Low cowbell
- perc_block_high: High cowbell
- perc_shaker: Shaker
- perc_tambourine: Tambourine
- perc_triangle: Triangle
Xylophone notes
Taps on a 39-note xylophone (notes A4 to B7) are available.
You can translate a xylophone note number (1-39) to its associated filename with the PS.Xylophone() function.
- xylo_a4 xylo_bb4 xylo_b4
- xylo_c5 xylo_db5 xylo_d5 xylo_eb5 xylo_e5 xylo_f5 xylo_gb5 xylo_g5 xylo_ab5 xylo_a5 xylo_bb5 xylo_b5
- xylo_c6 xylo_db6 xylo_d6 xylo_eb6 xylo_e6 xylo_f6 xylo_gb6 xylo_g6 xylo_ab6 xylo_a6 xylo_bb6 xylo_b6
- xylo_c7 xylo_db7 xylo_d7 xylo_eb7 xylo_e7 xylo_f7 xylo_gb7 xylo_g7 xylo_ab7 xylo_a7 xylo_bb7 xylo_b7
Piano notes
All 88 keys on a standard concert piano (notes A0 to C8) are available in two versions, short (about 1 second) and long (about 4 seconds).
You can translate a note number (1-88) to its associated filename with the PS.Piano() function.
Short version filenames begin with a “piano_” prefix.
- piano_a0 piano_bb0 piano_b0
- piano_c1 piano_db1 piano_d1 piano_eb1 piano_e1 piano_f1 piano_gb1 piano_g1 piano_ab1 piano_a1 piano_bb1 piano_b1
- piano_c2 piano_db2 piano_d2 piano_eb2 piano_e2 piano_f2 piano_gb2 piano_g2 piano_ab2 piano_a2 piano_bb2 piano_b2
- piano_c3 piano_db3 piano_d3 piano_eb3 piano_e3 piano_f3 piano_gb3 piano_g3 piano_ab3 piano_a3 piano_bb3 piano_b3
- piano_c4 piano_db4 piano_d4 piano_eb4 piano_e4 piano_f4 piano_gb4 piano_g4 piano_ab4 piano_a4 piano_bb4 piano_b4
- piano_c5 piano_db5 piano_d5 piano_eb5 piano_e5 piano_f5 piano_gb5 piano_g5 piano_ab5 piano_a5 piano_bb5 piano_b5
- piano_c6 piano_db6 piano_d6 piano_eb6 piano_e6 piano_f6 piano_gb6 piano_g6 piano_ab6 piano_a6 piano_bb6 piano_b6
- piano_c7 piano_db7 piano_d7 piano_eb7 piano_e7 piano_f7 piano_gb7 piano_g7 piano_ab7 piano_a7 piano_bb7 piano_b7
- piano_c8
Long version filenames begin with a “l_piano_” prefix.
- l_piano_a0 l_piano_bb0 l_piano_b0
- l_piano_c1 l_piano_db1 l_piano_d1 l_piano_eb1 l_piano_e1 l_piano_f1 l_piano_gb1 l_piano_g1 l_piano_ab1 l_piano_a1 l_piano_bb1 l_piano_b1
- l_piano_c2 l_piano_db2 l_piano_d2 l_piano_eb2 l_piano_e2 l_piano_f2 l_piano_gb2 l_piano_g2 l_piano_ab2 l_piano_a2 l_piano_bb2 l_piano_b2
- l_piano_c3 l_piano_db3 l_piano_d3 l_piano_eb3 l_piano_e3 l_piano_f3 l_piano_gb3 l_piano_g3 l_piano_ab3 l_piano_a3 l_piano_bb3 l_piano_b3
- l_piano_c4 l_piano_db4 l_piano_d4 l_piano_eb4 l_piano_e4 l_piano_f4 l_piano_gb4 l_piano_g4 l_piano_ab4 l_piano_a4 l_piano_bb4 l_piano_b4
- l_piano_c5 l_piano_db5 l_piano_d5 l_piano_eb5 l_piano_e5 l_piano_f5 l_piano_gb5 l_piano_g5 l_piano_ab5 l_piano_a5 l_piano_bb5 l_piano_b5
- l_piano_c6 l_piano_db6 l_piano_d6 l_piano_eb6 l_piano_e6 l_piano_f6 l_piano_gb6 l_piano_g6 l_piano_ab6 l_piano_a6 l_piano_bb6 l_piano_b6
- l_piano_c7 l_piano_db7 l_piano_d7 l_piano_eb7 l_piano_e7 l_piano_f7 l_piano_gb7 l_piano_g7 l_piano_ab7 l_piano_a7 l_piano_bb7 l_piano_b7
- l_piano_c8
Harpshicord notes
A 57-key harpsichord manual (notes A2 to F7) is available in two versions, short (about 1 second) and long (about 4 seconds).
You can translate a harpsichord note number (1-57) to its associated filename with the PS.Harpsichord() function.
Short version filenames begin with a “hchord_” prefix.
- hchord_a2 hchord_bb2 hchord_b2
- hchord_c3 hchord_db3 hchord_d3 hchord_eb3 hchord_e3 hchord_f3 hchord_gb3 hchord_g3 hchord_ab3 hchord_a3 hchord_bb3 hchord_b3
- hchord_c4 hchord_db4 hchord_d4 hchord_eb4 hchord_e4 hchord_f4 hchord_gb4 hchord_g4 hchord_ab4 hchord_a4 hchord_bb4 hchord_b4
- hchord_c5 hchord_db5 hchord_d5 hchord_eb5 hchord_e5 hchord_f5 hchord_gb5 hchord_g5 hchord_ab5 hchord_a5 hchord_bb5 hchord_b5
- hchord_c6 hchord_db6 hchord_d6 hchord_eb6 hchord_e6 hchord_f6 hchord_gb6 hchord_g6 hchord_ab6 hchord_a6 hchord_bb6 hchord_b6
- hchord_c7 hchord_db7 hchord_d7 hchord_eb7 hchord_e7 hchord_f7
Long version filenames begin with a “l_hchord_” prefix.
- l_hchord_a2 l_hchord_bb2 l_hchord_b2
- l_hchord_c3 l_hchord_db3 l_hchord_d3 l_hchord_eb3 l_hchord_e3 l_hchord_f3 l_hchord_gb3 l_hchord_g3 l_hchord_ab3 l_hchord_a3 l_hchord_bb3 l_hchord_b3
- l_hchord_c4 l_hchord_db4 l_hchord_d4 l_hchord_eb4 l_hchord_e4 l_hchord_f4 l_hchord_gb4 l_hchord_g4 l_hchord_ab4 l_hchord_a4 l_hchord_bb4 l_hchord_b4
- l_hchord_c5 l_hchord_db5 l_hchord_d5 l_hchord_eb5 l_hchord_e5 l_hchord_f5 l_hchord_gb5 l_hchord_g5 l_hchord_ab5 l_hchord_a5 l_hchord_bb5 l_hchord_b5
- l_hchord_c6 l_hchord_db6 l_hchord_d6 l_hchord_eb6 l_hchord_e6 l_hchord_f6 l_hchord_gb6 l_hchord_g6 l_hchord_ab6 l_hchord_a6 l_hchord_bb6 l_hchord_b6
- l_hchord_c7 l_hchord_db7 l_hchord_d7 l_hchord_eb7 l_hchord_e7 l_hchord_f7