-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynth.js
53 lines (44 loc) · 1.06 KB
/
synth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Stephen Bly
// Game Engine Programming
// Symple Synth
// based off code from: https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API
var oscillator;
var playing = false;
function setup()
{
var contex = new (window.AudioContext || window.webkitAudioContext)();
oscillator = contex.createOscillator();
oscillator.type = 'sine'; // wave type is: sine, square, sawtooth, triangle, custom
oscillator.frequency.value = 3000; // frequency of wave
// warm distortion?
var real = new Float32Array(2);
var imag = new Float32Array(2);
real[0] = 0;
real[1] = 1;
imag[0] = 0;
imag[1] = 0;
var wave = contex.createPeriodicWave(real, imag);
oscillator.setPeriodicWave(wave);
oscillator.connect(contex.destination);
}
function play()
{
if (playing)
{
oscillator.stop();
playing = false;
}
else
{
oscillator.start();
playing = true;
}
}
function setType(type)
{
oscillator.type = type;
}
function setFrequency(frequency)
{
oscillator.frequency.vaue = frequency;
}