forked from cifkao/tonnetz-viz
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaudio.js
More file actions
94 lines (69 loc) · 1.89 KB
/
audio.js
File metadata and controls
94 lines (69 loc) · 1.89 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
var audio = (function() {
"use strict";
var noctaves = 4;
var shepard = function(freq) {
var x = Math.log2(freq / 360) / (noctaves / 3);
return Math.exp(-x * x);
};
var createWave = function(freq) {
var n = 1 + 1 << (2 * noctaves + 1);
var re = new Float32Array(n);
var im = new Float32Array(n);
for (let i = 1; i < n; i *= 2)
re[i] = shepard(i * freq);
return ctx.createPeriodicWave(re, im);
};
var Note = function(pitch) {
var base = pitch - 12 * noctaves;
var freq = Math.pow(2, (base - 9) / 12) * 440;
var wave = createWave(freq);
this.oscillator = ctx.createOscillator();
this.oscillator.frequency.value = freq;
this.oscillator.setPeriodicWave(wave);
this.output = ctx.createGain();
this.gain = this.output.gain;
this.gain.value = 0;
this.oscillator.connect(this.output);
this.output.connect(ctx.destination);
this.oscillator.start();
};
var maxVol = 1 / 12;
var sustVol = maxVol / 4;
var attack = 0.008;
var release = 0.24;
Note.prototype.start = function() {
var now = ctx.currentTime;
this.gain.cancelScheduledValues(now);
this.gain.setTargetAtTime(maxVol, now, attack);
this.gain.setTargetAtTime(sustVol, now + attack, release);
};
Note.prototype.stop = function() {
var now = ctx.currentTime;
this.gain.cancelScheduledValues(now);
this.gain.setTargetAtTime(0, now, release);
};
var module = {};
var ctx;
var notes = [];
module.init = function() {
var AudioContext = window.AudioContext;
if (AudioContext) {
ctx = new AudioContext();
for (let t = 0; t < 12; t++)
notes[t] = new Note(t);
module.noteOn = function(pitch) {
notes[pitch].start();
};
module.noteOff = function(pitch) {
notes[pitch].stop();
};
}
};
module.noteOn = function() {};
module.noteOff = function() {};
module.allNotesOff = function() {
for (let t = 0; t < 12; t++)
module.noteOff(t);
};
return module;
})();