SNDOPEN-v0.960: Difference between revisions
Jump to navigation
Jump to search
Attention!! - This page is outdated and provided for reference and/or education only.
(Return to historic Table of Contents)
Code by Johny B
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
No edit summary |
No edit summary |
||
(7 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
{{DISPLAYTITLE:_SNDOPEN-v0.960}} | {{DISPLAYTITLE:_SNDOPEN-v0.960}} | ||
'''{{Text|Attention!! - This page is outdated and provided for reference only.|red}}''' | ---- | ||
---- | |||
<center>'''{{Text|Attention!! - This page is outdated and provided for reference and/or education only.|red}}'''</center> | |||
<center>([[Historic Pages|Return to historic Table of Contents]])</center> | |||
---- | |||
----<br> | |||
The [[_SNDOPEN]] function loads a sound file into memory and returns a [[LONG]] handle value above 0. | The [[_SNDOPEN]] function loads a sound file into memory and returns a [[LONG]] handle value above 0. | ||
Line 15: | Line 21: | ||
* The handle can be used by most of the _SND sound playing functions and statements in QB64 except [[_SNDPLAYFILE]] which plays a sound file directly from the disk and does not use a handle value. | * The handle can be used by most of the _SND sound playing functions and statements in QB64 except [[_SNDPLAYFILE]] which plays a sound file directly from the disk and does not use a handle value. | ||
* Handles can be closed with [[_SNDCLOSE]] when the sound is no longer necessary. | * Handles can be closed with [[_SNDCLOSE]] when the sound is no longer necessary. | ||
* If a WAV sound file won't play, try it using the Windows [[ | * 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. | ||
* The raw audio data can be accessed with [[_MEMSOUND]]. | * The raw audio data can be accessed with [[_MEMSOUND]]. | ||
Line 34: | Line 40: | ||
{{Cl|_SNDBAL}} Laff&, LaffX! 'balance sound to left or right speaker | {{Cl|_SNDBAL}} Laff&, LaffX! 'balance sound to left or right speaker | ||
{{Cl|_SNDPLAY}} Laff& 'play sound | {{Cl|_SNDPLAY}} Laff& 'play sound | ||
{{CodeEnd}} | {{CodeEnd}} | ||
Line 96: | Line 102: | ||
{{Cl|PRINT}} xleft, xright, volume, {{Cl|_SNDGETPOS}}(s&); " " | {{Cl|PRINT}} xleft, xright, volume, {{Cl|_SNDGETPOS}}(s&); " " | ||
LOOP | LOOP | ||
{{CodeEnd}}{{ | {{CodeEnd}} | ||
{{Small|Code by Johny B}} | |||
Line 106: | Line 113: | ||
* [[_SNDCOPY]], [[_SNDPLAYCOPY]] | * [[_SNDCOPY]], [[_SNDPLAYCOPY]] | ||
* [[_SNDBAL]], [[_SNDLEN]], [[_SNDVOL]] | * [[_SNDBAL]], [[_SNDLEN]], [[_SNDVOL]] | ||
* [[_SNDPLAYFILE]] | * [[_SNDPLAYFILE]] | ||
* [[_SNDRAW]], [[_SNDRATE]], [[_SNDRAWLEN]] | * [[_SNDRAW]], [[_SNDRATE]], [[_SNDRAWLEN]] | ||
* [[_MEMSOUND]] | * [[_MEMSOUND]] | ||
{{PageNavigation}} | {{PageNavigation}} | ||
Latest revision as of 10:15, 24 September 2023
The _SNDOPEN function loads a sound file into memory and returns a LONG handle value above 0.
Syntax
- soundHandle& = _SNDOPEN(fileName$)
Description
- Returns a LONG soundHandle& value to the sound file in memory. A zero value means the sound could not be loaded.
- The literal or variable STRING sound fileName$ can be WAV, OGG or MP3 file types.
- Always check the handle value returned is greater than zero before attempting to play the sound.
- Make sure the variable is set to 0 before using _SNDOPEN.
- The handle can be used by most of the _SND sound playing functions and statements in QB64 except _SNDPLAYFILE which plays a sound file directly from the disk and does not use a handle value.
- Handles can be closed with _SNDCLOSE when the sound is no longer necessary.
- If a WAV sound file won't play, try it using the Windows Play WAV sounds library to check it or convert the sound file to OGG.
- The raw audio data can be accessed with _MEMSOUND.
Examples
Snippet 1: Loading a sound file to use in the program later. Only load it once and use the handle any time you want.
h& = _SNDOPEN("dog.wav") IF h& = 0 THEN BEEP ELSE _SNDPLAY h& 'check for valid handle before using! |
Snippet 2: Playing a sound from 2 different speakers based on program results.
Laff& = _SNDOPEN("KONGlaff.ogg") 'load sound file and get LONG handle value IF LaffX! < -1 THEN LaffX! = -1 'set full volume to left speaker IF LaffX! > 1 THEN LaffX! = 1 'set full volume to right speaker _SNDBAL Laff&, LaffX! 'balance sound to left or right speaker _SNDPLAY Laff& 'play sound |
Example: Playing a file and controlling playback:
s& = _SNDOPEN("song.ogg") PRINT "READY"; s& _SNDPLAY s& _SNDLOOP s& xleft = -1 xright = 1 DO k$ = INKEY$ SELECT CASE k$ CASE "f" xleft = xleft - 0.1 _SNDBAL s&, xleft, , , 1 CASE "g" xleft = xleft + 0.1 _SNDBAL s&, xleft, , , 1 CASE "h" xright = xright - 0.1 _SNDBAL s&, xright, , , 2 CASE "j" xright = xright + 0.1 _SNDBAL s&, xright, , , 2 CASE "n" volume = volume - 0.1 _SNDVOL s&, volume CASE "m" volume = volume + 0.1 _SNDVOL s&, volume CASE "p" _SNDPAUSE s& CASE " " _SNDPLAY s& CASE "i" PRINT _SNDPLAYING(s&) PRINT _SNDPAUSED(s&) SLEEP CASE "b" _SNDSETPOS s&, 110 CASE "l" _SNDLIMIT s&, 10 PRINT "LIM" SLEEP CASE "k" _SNDSTOP s& CASE "c" _SNDCLOSE s& SLEEP s2& = _SNDOPEN("song.ogg") CASE "d" s2& = _SNDCOPY(s&) _SNDPLAY s2& END SELECT LOCATE 1, 1 PRINT xleft, xright, volume, _SNDGETPOS(s&); " " LOOP |
See also
- _SNDCLOSE, _SNDPLAY, _SNDSTOP
- _SNDPAUSE, _SNDLOOP, _SNDLIMIT
- _SNDSETPOS, _SNDGETPOS
- _SNDPLAYING, _SNDPAUSED
- _SNDCOPY, _SNDPLAYCOPY
- _SNDBAL, _SNDLEN, _SNDVOL
- _SNDPLAYFILE
- _SNDRAW, _SNDRATE, _SNDRAWLEN
- _MEMSOUND