{{DISPLAYTITLE:_SNDOPEN}}
The '''_SNDOPEN''' function loads a sound file into memory and returns a [[LONG]] handle value above 0.


{{PageSyntax}}
::: sound_handle&amp; = '''_SNDOPEN ('''''filename$''[, &quot;[VOL][,][SYNC][,][LEN][,][PAUSE][,][SETPOS]&quot;]''')'''


{{PageDescription}}
* Returns a [[LONG]] ''sound handle'' value to the sound file in memory. '''A zero value means the sound could NOT be loaded.'''
* The literal or variable [[STRING]] sound ''file name'' can be WAV, OGG, AIFF, RIFF, VOC, MP3, MOD or MIDI file types.
*Capabilities of VOL, LEN, SYNC, SETPOS and PAUSE is a string of parameters separated by commas. It is NOT case sensitive.
* '''ALWAYS check the handle value returned is greater than zero before attempting to play the sound!'''
* The handle can be used by most of the SND sound playing Functions and Subs in QB64 except [[_SNDPLAYFILE]] which plays a sound file name directly and does not use a handle value.
* Handles can be closed with [[_SNDCLOSE]] when the sound is no longer necessary. 
*An '''ILLEGAL FUNCTION CALL''' error message means the capabilities$ string was invalid or two NON-SYNC sounds are using the same channel!
* If a WAV sound file won't play, try it using the Windows [[Windows_Libraries#Play_WAV_Sounds|Play WAV sounds library]] to check it or convert the sound file to OGG!


&lt;center&gt;'''Sound File Capabilities'''&lt;/center&gt;
* Each capability can only be specified once and must be valid for that file or it won't play. Capabilities can be listed in any order. 
{{TextStart}}    QB64 supports the following sound file formats ('''Bold is a guaranteed capability'''):

             WAV = &quot;'''VOL,SYNC,LEN''',PAUSE&quot;      [http://www.rarewares.org/ogg-oggdropxpd.php Free WAV to OGG GUI converter]
             OGG = &quot;VOL,SYNC,LEN,PAUSE&quot;      [http://www.rarewares.org/ogg-oggenc.php Free WAV to OGG converter]
             AIF = &quot;VOL,SYNC,LEN,PAUSE&quot;
             RIF = &quot;VOL,SYNC,LEN,PAUSE&quot;
             VOC = &quot;VOL,SYNC,LEN,PAUSE&quot;
             MID = &quot;'''VOL'''&quot;
             MOD = &quot;VOL,PAUSE&quot;
             MP3 = &quot;'''VOL''',PAUSE,SETPOS&quot;        [http://www.freemp3wmaconverter.com/index.html Free WMA, MP3 and OGG converter]

        ''Note:'' {{Cb|_SNDBAL}} only affects MP3 volume. Sound will reside in main channel.
{{TextEnd}}

* The required sound file capabilities can make a difference as to whether a sound file can be played or not and how it can be used.

:'''Only one sound can exist on the primary channel, and it must be closed before playing another non-SYNC sound. 
&lt;center&gt;See {{KW|_SNDCOPY}} and {{KW|_SNDPLAYCOPY}}&lt;/center&gt; 
{{TextStart}}                                  '''Capability Descriptions'''
 
   &quot;'''VOL'''&quot; can change the volume or balance of the sound using {{Cb|_SNDVOL}} and {{Cb|_SNDBAL}}.
   &quot;'''LEN'''&quot; can get the length of the sound using the {{Cb|_SNDLEN}} function.
   &quot;'''PAUSE'''&quot; can pause the sound using {{Cb|_SNDPAUSE}} and the {{Cb|_SNDPAUSED}} function can check it.
   &quot;'''SETPOS'''&quot; can change the position the sound is (or will be) playing from. See {{Cb|_SNDSETPOS}}.
   &quot;'''SYNC'''&quot; can load the sound onto a unique channel, to be played simultaneously with others. 

        '''When SYNC is not specified, the sound is loaded onto the primary channel.'''
{{TextEnd}}


''Snippet 1:'' Loading a sound file to use in the program later. Only load it ONCE and use the handle any time you want!
{{CodeStart}}
h&amp; = {{Cl|_SNDOPEN}}(&quot;dog.wav&quot;,&quot;sync,vol&quot;)       'only use the capabilities of that file type
IF h&amp; = 0 THEN BEEP ELSE {{Cl|_SNDPLAY}} h&amp;      'check for valid handle before using!
{{CodeEnd}}


''Snippet 2:'' Playing a sound from 2 different speakers based on program results. Must use SYNC!
{{CodeStart}} '' ''
Laff&amp; = {{Cl|_SNDOPEN}}(&quot;KONGlaff.ogg&quot;, &quot;SYNC,VOL&quot;) 'load sound file and get LONG handle value 
{{Cl|IF}} LaffX! &lt; -1 {{Cl|THEN}} LaffX! = -1   'set full volume to left speaker
{{Cl|IF}} LaffX! &gt; 1 {{Cl|THEN}} LaffX! = 1     'set full volume to right speaker

{{Cl|_SNDBAL}} Laff&amp;, LaffX!             'balance sound to left or right speaker
{{Cl|_SNDPLAY}} Laff&amp;                    'play sound '' ''
{{CodeEnd}}



{{PageSeeAlso}}
* [[_SNDCLOSE]], [[_SNDPLAY]], [[_SNDSTOP]]
* [[_SNDPAUSE]], [[_SNDLOOP]], [[_SNDLIMIT]]
* [[_SNDSETPOS]], [[_SNDGETPOS]]
* [[_SNDPLAYING]], [[_SNDPAUSED]]
* [[_SNDCOPY]], [[_SNDPLAYCOPY]]
* [[_SNDBAL]], [[_SNDLEN]], [[_SNDVOL]]
* [[_SNDPLAYFILE]] {{text|(plays a named sound file directly and closes)}}
* [[_SNDRAW]], [[_SNDRATE]], [[_SNDRAWLEN]] {{text|{raw sounds without files)}}


{{PageNavigation}}