-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsynthesizer.js
More file actions
99 lines (95 loc) · 3.5 KB
/
Copy pathsynthesizer.js
File metadata and controls
99 lines (95 loc) · 3.5 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
95
96
97
98
99
//based on https://github.com/uber5001/CanvasKeyboard
import adsr from './adsr.js'
import Tunings from './tunings.js'
export default class Synthesizer {
static instance
//singleton factory
static getInstance() {
if (!Synthesizer.instance) {
Synthesizer.instance = new Synthesizer()
}
return Synthesizer.instance
}
constructor() {
this.oscillators = []
this.context = new (window.AudioContext ||
window.webkitAudioContext ||
window.mozAudioContext ||
window.oAudioContext ||
window.msAudioContext)()
this.context.resume()
this.tunings = new Tunings()
this.tuningEqual = this.tunings.tuningEqual
this.tuningCircleOfFifth = this.tunings.tuningCircleOfFifth
}
noteOn(note, channel) {
if (channel > 1) {
console.log('There is not a tunning on this channel!')
return null
}
if (!this.oscillators[note]) {
this.oscillators[note] = {}
this.oscillators[note][channel] = false
}
if (!this.oscillators[note][channel]) {
//sine, square, sawtooth, triangle, custom
this.oscillators[note][channel] = this.context.createOscillator()
//this.oscillators[note][channel].type = 'sine'
this.oscillators[note][channel].type = 'square'
this.oscillators[note][channel] = new adsr(
this.context,
0,
2,
0.3,
0.3
)
if (channel == 0) {
this.oscillators[note][
channel
].frequency.value = this.tuningEqual[note]
this.gainL = this.context.createGain()
this.gainL.gain.value = 0.1
this.pannerL = this.context.createStereoPanner()
this.pannerL.pan.value = -0.5
this.oscillators[note][channel].connect(this.gainL)
this.gainL.connect(this.pannerL)
this.pannerL.connect(this.context.destination)
console.log(
'equal',
note,
channel,
this.oscillators[note][channel].frequency.value + ' Hz'
)
}
if (channel == 1) {
this.oscillators[note][
channel
].frequency.value = this.tuningCircleOfFifth[note]
this.gainR = this.context.createGain()
this.gainR.gain.value = 0.1
this.pannerR = this.context.createStereoPanner()
this.pannerR.pan.value = 0.5
this.oscillators[note][channel].connect(this.gainR)
this.gainR.connect(this.pannerR)
this.pannerR.connect(this.context.destination)
console.log(
'fifth',
note,
channel,
this.oscillators[note][channel].frequency.value + ' Hz'
)
}
this.oscillators[note][channel].start(this.context.currentTime)
}
}
noteOff(note, channel) {
if (channel > 1) {
console.log('There is not a tunning on this channel!')
return null
}
if (this.oscillators[note][channel]) {
this.oscillators[note][channel].stop(this.context.currentTime)
this.oscillators[note][channel] = false
}
}
}