SNDOPEN: Difference between revisions
Jump to navigation
Jump to search
Code by Johny B
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
(Created page with "{{DISPLAYTITLE:_SNDOPEN}} The _SNDOPEN function loads a sound file into memory and returns a LONG handle value above 0. {{PageSyntax}} : {{Parameter|soundHandle&}} = _SNDOPEN({{Parameter|fileName$}}) {{PageDescription}} * Returns a LONG {{Parameter|soundHandle&}} value to the sound file in memory. '''A zero value means the sound could not be loaded.''' * The literal or variable STRING sound {{Parameter|fileName$}} can be '''WAV, OGG or MP3''' file...") |
(Added new format information. Updated notes on how to deal with invalid handles. Improved example #2) |
||
Line 8: | Line 8: | ||
{{PageDescription}} | {{PageDescription}} | ||
* Returns a [[LONG]] {{Parameter|soundHandle&}} value to the sound file in memory. '''A | * Returns a [[LONG]] {{Parameter|soundHandle&}} value to the sound file in memory. '''A value less than one means the sound could not be loaded.''' | ||
* The literal or variable [[STRING]] sound {{Parameter|fileName$}} can be '''WAV, OGG or | * The literal or variable [[STRING]] sound {{Parameter|fileName$}} can be '''WAV, FLAC, OGG, MP3, MID, IT, XM, S3M, MOD or RAD (v2 only)''' file types. | ||
* '''Always check the handle value returned is greater than zero before attempting to play the sound.''' | * '''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 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 [[Windows_Libraries#Play_WAV_Sounds|Play WAV sounds library]] to check it or convert the sound file to OGG. | * 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 FLAC, OGG or MP3. | ||
Line 20: | Line 20: | ||
{{CodeStart}} | {{CodeStart}} | ||
h& = {{Cl|_SNDOPEN}}("dog.wav") | h& = {{Cl|_SNDOPEN}}("dog.wav") | ||
IF h& = 0 THEN BEEP ELSE {{Cl|_SNDPLAY}} h& 'check for valid handle before using! | IF h& <= 0 THEN BEEP ELSE {{Cl|_SNDPLAY}} h& 'check for valid handle before using! | ||
{{CodeEnd}} | {{CodeEnd}} | ||
''Snippet 2:'' Playing a sound from 2 different speakers based on program results. | ''Snippet 2:'' Playing a sound from 2 different speakers based on program results. | ||
{{CodeStart}} ' | {{CodeStart}} | ||
Laff& = {{Cl|_SNDOPEN}}("KONGlaff.ogg") 'load sound file and get LONG handle value | ' This examples load, plays and then bounces the sound between the left and right channels | ||
{{Cl|IF}} | Laff& = {{Cl|_SNDOPEN}}("KONGlaff.ogg") 'load sound file and get LONG handle value | ||
{{Cl|IF}} | {{Cl|IF}} Laff& > 0 {{Cl|THEN}} | ||
{{Cl|_SNDPLAY}} Laff& 'play sound | |||
{{Cl|ELSE}} | |||
{{Cl|PRINT}} "Failed to load sound file." | |||
{{Cl|END}} | |||
{{Cl|END}} {{Cl|IF}} | |||
{{Cl|PRINT}} "Press ESC to stop." | |||
dir = 0.01 | |||
{{Cl|DO}} | |||
{{Cl|IF}} laffx! <= -1 {{Cl|THEN}} dir = 0.01 | |||
{{Cl|IF}} laffx! >= 1 {{Cl|THEN}} dir = -0.01 | |||
laffx! = laffx! + dir | |||
{{Cl|LOCATE}} , 1: {{Cl|PRINT USING}} "Balance = ##.##"; laffx!; | |||
{{Cl|_SNDBAL}} Laff&, laffx! 'balance sound to left or right speaker | |||
{{Cl| | {{Cl|_LIMIT}} 60 | ||
{{Cl| | {{Cl|LOOP}} {{Cl|WHILE}} {{Cl|_SNDPLAYING}}(Laff&) {{Cl|AND}} {{Cl|_KEYHIT}} <> 27 | ||
{{CodeEnd}} | {{CodeEnd}} | ||
Revision as of 07:35, 27 August 2022
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 value less than one means the sound could not be loaded.
- The literal or variable STRING sound fileName$ can be WAV, FLAC, OGG, MP3, MID, IT, XM, S3M, MOD or RAD (v2 only) file types.
- 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 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 FLAC, OGG or MP3.
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.
' This examples load, plays and then bounces the sound between the left and right channels Laff& = _SNDOPEN("KONGlaff.ogg") 'load sound file and get LONG handle value IF Laff& > 0 THEN _SNDPLAY Laff& 'play sound ELSE PRINT "Failed to load sound file." END END IF PRINT "Press ESC to stop." dir = 0.01 DO IF laffx! <= -1 THEN dir = 0.01 IF laffx! >= 1 THEN dir = -0.01 laffx! = laffx! + dir LOCATE , 1: PRINT USING "Balance = ##.##"; laffx!; _SNDBAL Laff&, laffx! 'balance sound to left or right speaker _LIMIT 60 LOOP WHILE _SNDPLAYING(Laff&) AND _KEYHIT <> 27 |
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 (plays a named sound file directly and closes)
- _SNDRAW, _SNDRATE, _SNDRAWLEN (raw sounds without files)