SNDPLAYCOPY: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
m (Removed protection from "SNDPLAYCOPY")
No edit summary
 
(3 intermediate revisions by 2 users not shown)
Line 4: Line 4:


{{PageSyntax}}
{{PageSyntax}}
: [[_SNDPLAYCOPY]] {{Parameter|handle&}}[, {{Parameter|volume!}}]
: [[_SNDPLAYCOPY]] {{Parameter|handle&}}[, [{{Parameter|volume!}}][, {{Parameter|x!}}][, {{Parameter|y!}}][, {{Parameter|z!}}]]




{{Parameters}}
{{PageParameters}}
* The [[LONG]] {{Parameter|handle&}} value is returned by [[_SNDOPEN]] using a specific sound file.  
* The [[LONG]] {{Parameter|handle&}} value is returned by [[_SNDOPEN]] using a specific sound file.
* The {{Parameter|volume!}} parameter can be any [[SINGLE]] value from 0 (no volume) to 1 (full volume).
* The {{Parameter|volume!}} parameter can be any [[SINGLE]] value from 0 (no volume) to 1 (full volume).
* {{Parameter|x!}} distance values go from left (negative) to right (positive) (beginning with '''v3.3.x''').
* {{Parameter|y!}} distance values go from below (negative) to above (positive) (beginning with '''v3.3.x''').
* {{Parameter|z!}} distance values go from behind (negative) to in front (positive) (beginning with '''v3.3.x''').




Line 16: Line 19:
:#Copies/duplicates the source handle (see [[_SNDCOPY]]).
:#Copies/duplicates the source handle (see [[_SNDCOPY]]).
:#Changes the volume of the copy if volume is passed.
:#Changes the volume of the copy if volume is passed.
:#Applies stereo panning or a 3D position if x, y, z is passed.
:#Plays the copy.
:#Plays the copy.
:#Closes the copy.
:#Closes the copy.
* This statement is a better choice than [[_SNDPLAYFILE]] if the sound will be played often, reducing the burden on the computer.  
* This statement is a better choice than [[_SNDPLAYFILE]] if the sound will be played often, reducing the burden on the computer.
*By setting the x! value to -1 or 1 it plays the sound at full volume from the appropriate speaker.
*Omitted x!, y! or z! values are set to 0.




{{PageExamples}}
{{PageExamples}}
''Example 1:'' Playing a previously opened sound at half volume.
''Example 1:'' Playing a previously opened sound from left and right speakers.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|_SNDPLAYCOPY}} applause&, 0.5 '' ''
{{Cl|DIM}} {{Cl|AS}} {{Cl|LONG}} h, i
 
h = {{Cl|_SNDOPEN}}("explosion.wav")
 
{{Cl|IF}} h > 0 {{Cl|THEN}}
    {{Cl|FOR}} i = 0 {{Cl|TO}} 9
        {{Cl|_LIMIT}} 1
 
        {{Cl|IF}} i {{Cl|MOD}} 2 = 0 {{Cl|THEN}}
            {{Cl|PRINT}} "Playing from right"
            {{Cl|_SNDPLAYCOPY}} h, , 1
        {{Cl|ELSE}}
            {{Cl|PRINT}} "Playing from left"
            {{Cl|_SNDPLAYCOPY}} h, , -1
        {{Cl|END IF}}
    {{Cl|NEXT}}
{{Cl|END IF}}
{{CodeEnd}}
{{CodeEnd}}




''Example 2:'' Playing a song at random volumes.
''Example 2:'' Playing a sound at random volumes.
{{CodeStart}} '' ''
{{CodeStart}}
chomp& = _SNDOPEN("chomp.wav")  
chomp& = {{Cl|_SNDOPEN}}("chomp.wav")
_SNDPLAYCOPY chomp&, 0.5 + RND * 0.49
{{Cl|IF}} chomp& > 0 {{Cl|THEN}} {{Cl|_SNDPLAYCOPY}} chomp&, 0.5 + {{Cl|RND}} * 0.49
{{CodeEnd}}
{{CodeEnd}}





Latest revision as of 01:08, 29 January 2023

The _SNDPLAYCOPY statement copies a sound, plays it, and automatically closes the copy using a handle parameter passed from _SNDOPEN or _SNDCOPY


Syntax

_SNDPLAYCOPY handle&[, [volume!][, x!][, y!][, z!]]


Parameters

  • The LONG handle& value is returned by _SNDOPEN using a specific sound file.
  • The volume! parameter can be any SINGLE value from 0 (no volume) to 1 (full volume).
  • x! distance values go from left (negative) to right (positive) (beginning with v3.3.x).
  • y! distance values go from below (negative) to above (positive) (beginning with v3.3.x).
  • z! distance values go from behind (negative) to in front (positive) (beginning with v3.3.x).


Description

  • Makes coding easier by doing all of the following automatically:
  1. Copies/duplicates the source handle (see _SNDCOPY).
  2. Changes the volume of the copy if volume is passed.
  3. Applies stereo panning or a 3D position if x, y, z is passed.
  4. Plays the copy.
  5. Closes the copy.
  • This statement is a better choice than _SNDPLAYFILE if the sound will be played often, reducing the burden on the computer.
  • By setting the x! value to -1 or 1 it plays the sound at full volume from the appropriate speaker.
  • Omitted x!, y! or z! values are set to 0.


Examples

Example 1: Playing a previously opened sound from left and right speakers.

DIM AS LONG h, i

h = _SNDOPEN("explosion.wav")

IF h > 0 THEN
    FOR i = 0 TO 9
        _LIMIT 1

        IF i MOD 2 = 0 THEN
            PRINT "Playing from right"
            _SNDPLAYCOPY h, , 1
        ELSE
            PRINT "Playing from left"
            _SNDPLAYCOPY h, , -1
        END IF
    NEXT
END IF


Example 2: Playing a sound at random volumes.

chomp& = _SNDOPEN("chomp.wav")
IF chomp& > 0 THEN _SNDPLAYCOPY chomp&, 0.5 + RND * 0.49


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link