[Home] [Downloads] [Search] [Help/forum]

MUSHclient scripting

Description of MUSHclient world function: world.PlaySound


Name PlaySound
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.


Note: Available in 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
Returns 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

Introduced in version 4.28

See also ...

Function Description
GetSoundStatus Gets the status of a sound started by PlaySound
Sound Plays a sound
StopSound Stop playing a sound started by PlaySound

Search for script function

Enter a word or phrase in the box below to narrow the list down to those that match.

The function name, prototype, summary, and description are searched.

Search for:   

Leave blank to show all functions.


Return codes

Many functions return a "code" which indicates the success or otherwise of the function.

You can view a list of the return codes


Function prototypes

The "prototype" part of each function description lists exactly how the function is called (what arguments, if any, to pass to it).

You can view a list of the data types used in function prototypes


View all functions

[Back]

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]