Difference between revisions of "Global Command: startSound"

From The Official Visionaire Studio: Adventure Game Engine Wiki
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{| class="toccolours mw-collapsible mw-collapsed ts"
+
Play an audio file; optionally pass property values.
|-
+
 
! Function History
+
{| class="ts"
 
|-
 
|-
| Available since v4.0
+
| style="width:15%" | Related functions
 +
| [[Global Command: getSoundId|getSoundId]] | [[Global Command: getSoundProperty|getSoundProperty]] | [[Global Command: setSoundProperty|setSoundProperty]] | [[Global Command: stopSound|stopSound]] | [[Global Command: toggleSoundPause|toggleSoundPause]]
 
|}
 
|}
  
  
Allows you to play sound files; with optional property values.
+
== Syntax ==
{|class="toccolours mw-collapsible mw-collapsed ts"
+
<syntaxhighlight lang="lua">
 +
startSound(soundfile [, flags])
 +
</syntaxhighlight>
 +
 
 +
 
 +
== Parameters ==
 +
 
 +
{| class="ts"
 
|-
 
|-
! Additional Info
+
! style="width:15%" | Parameter
 +
! style="width:15%" colspan="2" | Type/Structure
 +
! Description
 
|-
 
|-
| Properties currently available...
+
| soundfile
 +
| colspan="2" | string
 +
| Relative path to the audio file you want to play. Add the "vispath:" prefix.  
 
|-
 
|-
| volume, balance, offset & loop
+
| rowspan="5" | flags
 +
| rowspan="5" | table
 +
| flags = 1
 +
| indicates flags table
 
|-
 
|-
| &nbsp;
+
| balance (int)
 +
| Set left/right audio balance value in the range of -100 (100% left channel) to 100 (100% right channel).
 
|-
 
|-
| ''Please note that only mono sounds can be panned using the balance property; all stereo channel sounds will remain centered''.
+
| loop (bool)
 +
| Set to true to loop the sound.
 +
|-
 +
| offset (int)
 +
| Set the playtime position in milliseconds.
 +
|-
 +
| volume (int)
 +
| Set the volume in the range of 0 (mute) to 100 (full volume).
 
|}
 
|}
  
  
Syntax:
+
== Return values ==
<syntaxhighlight>
 
startSound("filename", {flags=1, properties})
 
</syntaxhighlight>
 
  
 +
{| class="ts"
 +
|-
 +
! style="width:15%" | Type
 +
! Description
 +
|-
 +
| integer
 +
| Unique id referencing the started sound, or -1 if the sound could not be started.
 +
|}
  
Example 1: basic play audio file method
 
<syntaxhighlight>
 
startSound("vispath:sounds/example.ogg")
 
</syntaxhighlight>
 
 
Example 2: again with properties
 
<syntaxhighlight>
 
-- play example.ogg at 100% volume level, audio centered, from 1 second mark with loop mode on
 
startSound("vispath:sounds/example.ogg", {flags=1, volume=100, balance=0, offset=1000, loop=true})
 
  
--[[
+
== Examples ==
let's say you have an audio file with a duration of 00:03:24 & you want to start the audio file at: 2 minutes & 44 seconds;
 
so what we need to do is convert minutes/seconds into ms like so: (2*60)*1000 + (44*1000) = 164000ms
 
  
math explanation:
+
'''Example 1:''' Play an audio file and store the sound id for later use. Update the left/right balance of the sound while it is playing.
2m*60s = 120 seconds * 1000ms = 120,000ms | 44s*1000ms = 44,000ms
+
<syntaxhighlight lang="lua">
120,000 + 44,000 = 164,000ms
+
local mySoundId = startSound('vispath:sounds/example.ogg')
--]]
+
setSoundProperty(mySoundId, {flags=1, balance=40})
 
</syntaxhighlight>
 
</syntaxhighlight>
  
  
 
+
'''Example 2:''' Play an audio file and set balance, offset and volume.
<span class="bold underline">Arguments</span>
+
<syntaxhighlight lang="lua">
 
+
startSound('vispath:sounds/example.ogg', {flags=1, balance=-10, offset=1000, volume=70})
<span class="bold">filename</span>: path <br/>
+
</syntaxhighlight>
The path to the audio file to be played; '''vispath:''' must be included at the beginning of the path.
 
<syntaxhighlight>"vispath:path/filename.type"</syntaxhighlight>
 
 
 
 
 
<span class="bold underline">Flags</span>
 
 
 
<span class="bold">properties</span>
 
* volume: allows you to set volume level via an integer value
 
* balance: allows you to set the left/right audio balance via an integer value (-100 to 100; 0 is centered)
 
* offset: allows you to start audio file from x time via an integer value (in ms)
 
* loop: allows you to declare if audio file should loop via a boolean value (true/false)
 
 
 
 
 
<span class="bold underline">Return</span>
 
 
 
none
 
 
 
{| class="ts"
 
|-
 
| '''Relevant Pages''': [[GetSoundId_(CMS)|getSoundId]] - [[GetSoundProperty_(CMS)|getSoundProperty]] - [[SetSoundProperty_(CMS)|setSoundProperty]] - [[StopSound_(CMS)|stopSound]] - [[ToggleSoundPause_(CMS)|toggleSoundPause]]
 
|}
 
 
{{toc}}
 
{{toc}}

Latest revision as of 20:46, 4 May 2023

Play an audio file; optionally pass property values.

Related functions getSoundId | getSoundProperty | setSoundProperty | stopSound | toggleSoundPause


Syntax

startSound(soundfile [, flags])


Parameters

Parameter Type/Structure Description
soundfile string Relative path to the audio file you want to play. Add the "vispath:" prefix.
flags table flags = 1 indicates flags table
balance (int) Set left/right audio balance value in the range of -100 (100% left channel) to 100 (100% right channel).
loop (bool) Set to true to loop the sound.
offset (int) Set the playtime position in milliseconds.
volume (int) Set the volume in the range of 0 (mute) to 100 (full volume).


Return values

Type Description
integer Unique id referencing the started sound, or -1 if the sound could not be started.


Examples

Example 1: Play an audio file and store the sound id for later use. Update the left/right balance of the sound while it is playing.

local mySoundId = startSound('vispath:sounds/example.ogg')
setSoundProperty(mySoundId, {flags=1, balance=40})


Example 2: Play an audio file and set balance, offset and volume.

startSound('vispath:sounds/example.ogg', {flags=1, balance=-10, offset=1000, volume=70})