PlayTone: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
Created page with "PlayTone(''frequency'', ''duration''[, ''volume'']) == Description == PlayTone generates a tone with the specified ''frequency'', ''duration'' and ''volume''. Playback is im..."
 
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
PlayTone(''frequency'', ''duration''[, ''volume''])
PlayTone(''frequency'', ''duration''[, ''volume'']) in vibrations per second. 440 corresponds to the normal frequency for A.


== Description ==
== Description ==
Line 5: Line 5:
PlayTone generates a tone with the specified ''frequency'', ''duration'' and ''volume''. Playback is immediate, with no on screen controls, making it excellent for user interactions and gaming. The tone is played asynchronously, so if you do PlayTone before the previous tone is complete, two tones will play at the same time, making chords possible.
PlayTone generates a tone with the specified ''frequency'', ''duration'' and ''volume''. Playback is immediate, with no on screen controls, making it excellent for user interactions and gaming. The tone is played asynchronously, so if you do PlayTone before the previous tone is complete, two tones will play at the same time, making chords possible.


''frequency''
''frequency'' defines the pitch of the tone, in vibrations per second. 440 corresponds to A in the most music.


''duration''
''duration'' is the length of the tone, in milliseconds.


''volume''
''volume'' is optional. 1 is the loudest volume.


PlayTone returns a reference to the sound. You can use this reference to control the sound.
PlayTone returns a reference to the sound. You can use this reference to control the sound.


== Properties and Methods ==


== Properties and Methods ==
The following methods and properties apply to the object which is returned by tone = PlayTone(f,d):


{| class="wikitable"
{| class="wikitable"
|-
|-
| soundRef.start(0) || Start playing the sound. PlaySound auto plays, so this is not normally needed.
| tone.oscillator.onended || Name of function to call when tone completes.
|-
|-
| soundRef.stop(0) || Stop playing the sound.
| tone.oscillator.stop() || Stop playing the tone.
|-
|-
| soundRef.loop || Make the sound loop endlessly
| tone.oscillator.type || Wave form to use. Sine is default. Can be sine, square, sawtooth, triangle or custom.
|}
|}


== Example (Basic) ==
For more information, see
<pre>
 
  PlaySound "BellToll.wav"
https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/createOscillator
</pre>
 
From there, dig further into the docs. There are many more possibilities!)
 
== Example ==
 
<tabber>
JavaScript=
<syntaxhighlight lang="JavaScript">
PlayTone(440, 1000);


Make sound loop
// Play a chord:
<pre>
var C = 261.63;
  bell = PlaySound("BellToll.wav")
var E = 329.63;
  bell.loop=True
var G = 392.00;
</pre>
PlayTone(C, 1000);
PlayTone(E, 1000);
PlayTone(G, 1000);


Stop playing sound
// Stop playing tone:
<pre>
tone = PlayTone(440,1000);
  bell.stop()
tone.oscillator.stop();
</pre>
</syntaxhighlight>
|-|
BASIC=
<syntaxhighlight lang="vb.net">
PlayTone(440, 1000);


== Example (JavaScript) ==
' Play a chord:
<pre>
Dim C = 261.63
NSB.PlaySound("BellToll.wav");
Dim E = 329.63
</pre>
Dim G = 392.00
Make sound loop
PlayTone(C, 1000)
<pre>
PlayTone(E, 1000)
  bell = NSB.PlaySound("BellToll.wav");
PlayTone(G, 1000)
  bell.loop=true;
</pre>


Stop playing sound
' Stop playing tone
<pre>
tone = PlayTone(440,1000)
  bell.stop()
tone.oscillator.stop()
</pre>
</syntaxhighlight>
</tabber>


== Output ==
== Output ==


(sound of a bell tolling)
(single tone)
 
(chord)
 
(nothing - it is stopped before you can hear it start)


[[Category:Language Reference]]
[[Category:Language Reference]]


[[Category:Messages]]
[[Category:Messages]]

Latest revision as of 22:59, 24 July 2019

PlayTone(frequency, duration[, volume]) in vibrations per second. 440 corresponds to the normal frequency for A.

Description

PlayTone generates a tone with the specified frequency, duration and volume. Playback is immediate, with no on screen controls, making it excellent for user interactions and gaming. The tone is played asynchronously, so if you do PlayTone before the previous tone is complete, two tones will play at the same time, making chords possible.

frequency defines the pitch of the tone, in vibrations per second. 440 corresponds to A in the most music.

duration is the length of the tone, in milliseconds.

volume is optional. 1 is the loudest volume.

PlayTone returns a reference to the sound. You can use this reference to control the sound.

Properties and Methods

The following methods and properties apply to the object which is returned by tone = PlayTone(f,d):

tone.oscillator.onended Name of function to call when tone completes.
tone.oscillator.stop() Stop playing the tone.
tone.oscillator.type Wave form to use. Sine is default. Can be sine, square, sawtooth, triangle or custom.

For more information, see

https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/createOscillator

From there, dig further into the docs. There are many more possibilities!)

Example

PlayTone(440, 1000);

// Play a chord:
var C = 261.63;
var E = 329.63;
var G = 392.00;
PlayTone(C, 1000);
PlayTone(E, 1000);
PlayTone(G, 1000);

// Stop playing tone:
tone = PlayTone(440,1000);
tone.oscillator.stop();

PlayTone(440, 1000);

' Play a chord:
Dim C = 261.63
Dim E = 329.63
Dim G = 392.00
PlayTone(C, 1000)
PlayTone(E, 1000)
PlayTone(G, 1000)

' Stop playing tone
tone = PlayTone(440,1000)
tone.oscillator.stop()

Output

(single tone)

(chord)

(nothing - it is stopped before you can hear it start)