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();
No edit summary |
No edit summary |
||
(5 intermediate revisions by the same user not shown) | |||
Line 27: | Line 27: | ||
For more information, see | For more information, see | ||
https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/createOscillator | https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/createOscillator | ||
== Example | From there, dig further into the docs. There are many more possibilities!) | ||
== Example == | |||
<tabber> | |||
< | JavaScript= | ||
<syntaxhighlight lang="JavaScript"> | |||
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(); | |||
</syntaxhighlight> | |||
|-| | |||
BASIC= | |||
<syntaxhighlight lang="vb.net"> | |||
PlayTone(440, 1000); | |||
Play a chord: | ' 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 | ' Stop playing tone | ||
tone = PlayTone(440,1000) | |||
tone.oscillator.stop() | |||
</syntaxhighlight> | |||
</ | </tabber> | ||
== Output == | == Output == | ||
(single tone) | (single tone) | ||
(chord) | (chord) | ||
(nothing - it is stopped before you can hear it start) | (nothing - it is stopped before you can hear it start) | ||
PlayTone(frequency, duration[, volume]) in vibrations per second. 440 corresponds to the normal frequency for A.
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.
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!)
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()
(single tone)
(chord)
(nothing - it is stopped before you can hear it start)