Register forum user name Search FAQ

PlaySound

Script function

world.PlaySound

Read about scripting

Type

Method

Summary

Plays a sound using DirectSound

Prototype

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

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.


-----------

Playing sounds in the background ...

The default behaviour is for the sounds to be only played if MUSHclient is currently active (the foreground window, that is). If you switch to another application the sound will cut out (and resume if it is still playing when you switch back).

To have the sound play regardless, you need to set a world configuration option, like this:

SetOption ("play_sounds_in_background", 1)

That option only needs to be set once per world file. Once the world file is saved to disk the option will persist.


Available in MUSHclient version 4.28 onwards.



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


To wait until a previous sound has finished playing:


require "wait"

function PlayOneSound (name)
  wait.make ( function ()
    while GetSoundStatus (1) > 0 do
      wait.time (0.1)
    end -- while previous sound still playing
    PlaySound (1, name)
  end )  -- end of wait.make
end -- PlayOneSound 

PlayOneSound ("1.wav")
PlayOneSound ("2.wav")
PlayOneSound ("3.wav")


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)

View list of return code meanings

See Also ...

Topics

Scripting
Utilities

Functions

(AddFont) Adds a custom font for use by MUSHclient
(Base64Decode) Takes a base-64 encoded string and decodes it.
(Base64Encode) Encodes a string using base-64 encoding.
(BlendPixel) Blends a single pixel with another, using a specified blending mode
(ChangeDir) Changes the MUSHclient working directory
(CreateGUID) Creates a GUID - Global Unique Identifier
(EditDistance) Returns the Levenshtein Edit Distance between two words
(ErrorDesc) Converts a MUSHclient script error code into an human-readable description
(ExportXML) Exports a world item in XML format
(FilterPixel) Performs a filtering operation on one pixel
(FixupEscapeSequences) Converts "escape sequences" like \t to their equivalent codes.
(FixupHTML) Fixes up text for writing as HTML
(FlashIcon) Flashes the MUSHclient icon on the Windows taskbar
(GenerateName) Generates a random character name
(GetClipboard) Gets the clipboard contents
(GetScriptTime) Returns the amount of time spent in script routines
(GetSoundStatus) Gets the status of a sound started by PlaySound
(GetUniqueID) Creates a unique ID for general use, or for making Plugin IDs
(GetUniqueNumber) Returns a unique number
(Hash) Produces a hash (checksum) of a specified piece of text
(Help) Shows help for a script function, or a list of functions
(ImportXML) Imports configuration data in XML format
(Menu) Creates a pop-up menu inside the command window
(Metaphone) Returns the metaphone code for the supplied word
(MoveMainWindow) Move and resize the main MUSHclient window
(MoveWorldWindow) Move and resize a world window
(MoveWorldWindowX) Move and resize a specific world window
(MtRand) Returns pseudo-random number using the Mersenne Twister algorithm
(MtSrand) Seed the Mersenne Twister pseudo-random number generator
(ReadNamesFile) Loads in a file for generating character names
(Replace) Replaces one substring with another
(SetBackgroundColour) Sets a background colour for the output window
(SetBackgroundImage) Sets a background image for the output window
(SetClipboard) Sets the clipboard contents
(SetForegroundImage) Sets a foreground image for the output window
(SetMainTitle) Sets the main output window title
(SetSelection) Sets a selection range in the output window
(SetStatus) Sets the status line text
(SetTitle) Sets the world window title
(SetToolBarPosition) Sets the position of the game toolbars on the screen.
(SetUnseenLines) Sets the number of "unseen lines" for this world
(ShiftTabCompleteItem) Adds an item to the list shown for Shift+Tab completion
(Simulate) Simulate input from the MUD, for debugging purposes
(Sound) Plays a sound
(StopSound) Stop playing a sound started by PlaySound
(StripANSI) Strips ANSI colour sequences from a string
(Trace) Trace mode property
(TraceOut) Outputs the supplied message to the world Trace
(TranslateDebug) Sends a debugging message to the localizing translator script
(TranslateGerman) Translate German umluat sequences
(Transparency) Sets the transparency of the main MUSHclient window under Windows XP
(Trim) Trims leading and trailing spaces from a string

(Help topic: function=PlaySound)

Documentation contents page


Search ...

Enter a search string to find matching documentation.

Search for:   

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.