PlaySound
Script function

world.PlaySound

DOC_scripting Read about scripting

Type

Method

Summary

Plays a sound using DirectSound

Prototype

long PlaySound(short Buffer, BSTR FileName, BOOL Loop, double Volume, double Pan);

DOC_data_types View list of data type meanings


Description

Plays a sound, or adjusts the parameters of a sound that is currently playing.

Also see below in the Lua notes for PlaySoundMemory which plays a sound loaded into memory.

You can choose to use 1 of 10 "sound buffers" - sounds placed in different buffers will play simultaneously.

If a sound is currently playing in the selected buffer it is automatically stopped and its memory freed. Thus you do not need to stop sounds first before replacing them by something else.

See: http://mushclient.com/forum/?id=8721 for a more detailed description.

You can use StopSound to stop the sound playing at any time, and GetSoundStatus to see if a sound is playing or not in a particular buffer.

------------
Buffer: Which buffer to use (1 to 10) or 0 to take the first free buffer when playing a new sound. If there are no free buffers, buffer 1 is chosen.

------------
FileName: The sound file to play. Can be the empty string in which case you are modifying the behaviour of a currently-playing sound in that buffer.

If the file name is a fully-qualified path name (ie. it starts with a slash, or a drive letter, colon, slash like "C:/") then it is used "as is".

Otherwise, MUSHclient prepends the MUSHclient execution directory + "sounds" subdirectory to the name

For example, the file name "swordhit.wav" would be looked for at: C:/Program Files/MUSHclient/sounds/swordhit.wav

You can use forward slashes in the file name (for convenience in typing strings in scripts), they are converted to backslashes.

The only file type supported is a .wav file, as follows:

* 16-bit
* 22.05 KHz
* PCM (ie. uncompressed)
* Mono or Stereo

Note that the entire file is read into memory, uncompressed. We do not attempt to "stream" the file. Thus large sound files will take a lot of memory. The sound functions are intended for sound effects and short loops of music or ambient sounds. They are not intended to play Beethoven's Fifth Symphony while you play.

You can convert sound files which are in other formats (eg. MP3) into WAV files by using various programs. One very good free program is Audacity, which is available for Mac or Windows:

http://audacity.sourceforge.net/

In Audacity, open your file, and then choose: File Menu -> Export As WAV...

------------
Loop: true or false (1 or 0). If true the sound loops, and thus repeats indefinitely until cancelled. A sound can be cancelled by calling the StopSound function for that buffer number, or simply playing a different sound in that buffer. Obviously it is preferable to not use buffer 0 if you are planning to loop, because you won't know which buffer actually got allocated.

If omitted in Lua scripting this defaults to false (not looping).

------------
Volume: -100 to 0. This is the number of db (decibels) to attenuate (reduce) the sound. Decibels are a logarithmic scale, every 3 db corresponds to half the perceived sound volume. Thus a volume of zero is "full volume". A volume of -3 is half-volume, -6 is quarter-volume, and so on. If you want to fade a sound away rather than just cut if off, you get quite a good effect by readjusting the sound every half second or so in a loop, reducing the volume by -2 each time, from 0 to -40. A sound at volume -40 is pretty quiet, after which you can just stop playing the sound. If you are going to write a "reduce the volume" loop you would need to use Lua coroutines, or some other timer mechanism, to turn the volume in that channel down periodically.

If omitted in Lua scripting this defaults to 0 (full volume).

If the volume is out of the range -100 to 0 then 0 (full volume) is used.

------------
Pan: -100 to +100. This is the number of db (decibels) to attenuate the sound in one speaker, to create a stereo affect (that is, more from one speaker than the other).

Pan of -100 makes the sound come completely from the left speaker (the right speaker is cut off).
Pan of 0 makes the sound come from both speakers, and thus it sounds centered.
Pan of +100 makes the sound come completely from the right speaker (the left speaker is cut off).

If omitted in Lua scripting this defaults to 0 (both speakers, or centered).

If the pan value is out of the range -100 to +100 then 0 is used.



VBscript example

' play sound once only, full volume, centered, in buffer 1
PlaySound 1, "boing.wav", vbFalse, 0, 0

' play sound once only, quarter volume, from the left speaker, in buffer 2
PlaySound 2, "ding.wav", vbFalse, -6, -100

' play sound looping, eighth volume, from the right speaker, in buffer 3
PlaySound 3, "forest.wav", vbTrue, -9, 100 

' play sound once only, full volume, slightly to the left, in the first available buffer
PlaySound 0, "swordhit.wav", vbFalse, 0, -3  


'  To adjust an existing sound, like the forest sound above:

PlaySound 3, "", vbFalse, -9, 100  ' cancel looping for buffer 3, still play at -9 db
PlaySound 3, "", vbFalse, -12, 100 ' cancel looping for buffer 3 and make softer



Lua example

-- play sound once only, full volume, centered, in buffer 1
PlaySound (1, "boing.wav", false, 0, 0)  

-- play sound once only, quarter volume, from the left speaker, in buffer 2
PlaySound (2, "ding.wav", false, -6, -100)  

-- play sound looping, eighth volume, from the right speaker, in buffer 3
PlaySound (3, "forest.wav", true, -9, 100) 

-- play sound once only, full volume, slightly to the left, in the first available buffer
PlaySound (0, "swordhit.wav", false, 0, -3)  


To adjust an existing sound, like the forest sound above:

PlaySound (3, "", false, -9, 100)  --> cancel looping for buffer 3, still play at -9 db
PlaySound (3, "", false, -12, 100)  --> cancel looping for buffer 3 and make softer



Lua notes

All arguments except the buffer number are optional.

The file name defaults to the empty string, looping defaults to false, volume defaults to full volume, and panning defaults to centered.

From version 4.42 onwards, Lua also supports PlaySoundMemory which plays a sound file loaded into a memory buffer.

In this case the second argument is the memory buffer, not the file name.

local f = assert (io.open ("some_sound.wav", "rb"))
local sound_data = f:read ("*a")  -- read all of it
f:close ()  -- close it

PlaySoundMemory (1, sound_data, false, 0, 0)  -- play sound loaded into sound_data



Return value

eCannotPlaySound:

* DirectSound not initialized
* An attempt made to change the volume, looping, or pan of a sound that is not playing
* Sound file is not a .wav file
* Cannot parse sound file
* Sound file is not a PCM format file (wrong sound file format)
* Cannot find sound data in sound file
* Cannot create internal sound buffer (maybe all in use)
* Cannot read sound data into memory
* DirectSound refuses to play the sound

eBadParameter:

* An attempt made to change the volume, looping, or pan of a sound that is not in use
* No sound file name given, and buffer 0 given
* Buffer number outside range 0 to 10
* File name > 127 characters long

eFileNotFound:

* Sound file not found

eOK:

* Sound played OK (or volume/looping/pan adjusted OK)


DOC_errors View list of return code meanings


See Also ...

Topics

DOC_scripting Scripting
DOC_utils Utilities

Functions

FNC_AddFont AddFont (Adds a custom font for use by MUSHclient)
FNC_Base64Decode Base64Decode (Takes a base-64 encoded string and decodes it.)
FNC_Base64Encode Base64Encode (Encodes a string using base-64 encoding.)
FNC_BlendPixel BlendPixel (Blends a single pixel with another, using a specified blending mode)
FNC_ChangeDir ChangeDir (Changes the MUSHclient working directory)
FNC_CreateGUID CreateGUID (Creates a GUID - Global Unique Identifier)
FNC_EditDistance EditDistance (Returns the Levenshtein Edit Distance between two words)
FNC_ErrorDesc ErrorDesc (Converts a MUSHclient script error code into an human-readable description)
FNC_ExportXML ExportXML (Exports a world item in XML format)
FNC_FilterPixel FilterPixel (Performs a filtering operation on one pixel)
FNC_FixupEscapeSequences FixupEscapeSequences (Converts "escape sequences" like \t to their equivalent codes.)
FNC_FixupHTML FixupHTML (Fixes up text for writing as HTML)
FNC_FlashIcon FlashIcon (Flashes the MUSHclient icon on the Windows taskbar)
FNC_GenerateName GenerateName (Generates a random character name)
FNC_GetClipboard GetClipboard (Gets the clipboard contents)
FNC_GetScriptTime GetScriptTime (Returns the amount of time spent in script routines)
FNC_GetSoundStatus GetSoundStatus (Gets the status of a sound started by PlaySound)
FNC_GetUniqueID GetUniqueID (Creates a unique ID for general use, or for making Plugin IDs)
FNC_GetUniqueNumber GetUniqueNumber (Returns a unique number)
FNC_Hash Hash (Produces a hash (checksum) of a specified piece of text)
FNC_Help Help (Shows help for a script function, or a list of functions)
FNC_ImportXML ImportXML (Imports configuration data in XML format)
FNC_Metaphone Metaphone (Returns the metaphone code for the supplied word)
FNC_MoveMainWindow MoveMainWindow (Move and resize the main MUSHclient window)
FNC_MoveWorldWindow MoveWorldWindow (Move and resize a world window)
FNC_MoveWorldWindowX MoveWorldWindowX (Move and resize a specific world window)
FNC_MtRand MtRand (Returns pseudo-random number using the Mersenne Twister algorithm)
FNC_MtSrand MtSrand (Seed the Mersenne Twister pseudo-random number generator)
FNC_ReadNamesFile ReadNamesFile (Loads in a file for generating character names)
FNC_Replace Replace (Replaces one substring with another)
FNC_SetBackgroundColour SetBackgroundColour (Sets a background colour for the output window)
FNC_SetBackgroundImage SetBackgroundImage (Sets a background image for the output window)
FNC_SetClipboard SetClipboard (Sets the clipboard contents)
FNC_SetForegroundImage SetForegroundImage (Sets a foreground image for the output window)
FNC_SetStatus SetStatus (Sets the status line text)
FNC_SetToolBarPosition SetToolBarPosition (Sets the position of the game toolbars on the screen.)
FNC_ShiftTabCompleteItem ShiftTabCompleteItem (Adds an item to the list shown for Shift+Tab completion)
FNC_Simulate Simulate (Simulate input from the MUD, for debugging purposes)
FNC_Sound Sound (Plays a sound)
FNC_StopSound StopSound (Stop playing a sound started by PlaySound)
FNC_StripANSI StripANSI (Strips ANSI colour sequences from a string)
FNC_Trace Trace (Trace mode property)
FNC_TraceOut TraceOut (Outputs the supplied message to the world Trace)
FNC_TranslateDebug TranslateDebug (Sends a debugging message to the localizing translator script)
FNC_TranslateGerman TranslateGerman (Translate German umluat sequences)
FNC_Transparency Transparency (Sets the transparency of the main MUSHclient window under Windows XP)
FNC_Trim Trim (Trims leading and trailing spaces from a string)

(Help topic: function=PlaySound)

DOC_contents Documentation contents page