-
Notifications
You must be signed in to change notification settings - Fork 0
/
SourceWrapper.js
85 lines (77 loc) · 2.82 KB
/
SourceWrapper.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
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
const NodeWrapper = require('./NodeWrapper')
module.exports = class SourceWrapper extends NodeWrapper {
constructor (context, type, value) {
super(context, type)
this.outputs = new Set()
this._value = value
this.isPlaying = false
this._pausedAt = 0
if (this.type === 'buffer') {
this.instance = this.context.createBufferSource()
this.instance.buffer = this._value
} else if (this.type === 'constant') {
this.instance = this.context.createConstantSource()
} else if (this.type === 'oscillator') {
this.instance = this.context.createOscillator()
} else if (this.type === 'mediaElement') {
this.instance = this.context.createMediaElementSource(this._value)
} else if (this.type === 'mediaStream') {
this.instance = this.context.createMediaStreamSource(this._value)
}
}
play (time) {
if (!this.isPlaying) {
if (!this.instance) {
if (this.type === 'buffer') {
this.instance = this.context.createBufferSource()
this.instance.buffer = this._value
} else if (this.type === 'constant') {
this.instance = this.context.createConstantSource()
} else if (this.type === 'oscillator') {
this.instance = this.context.createOscillator()
} else if (this.type === 'mediaElement') {
this.instance = this.context.createMediaElementSource(this._value)
} else if (this.type === 'mediaStream') {
this.instance = this.context.createMediaStreamSource(this._value)
}
}
// restore connections
this.outputs.forEach(output => {
if (output.instance) this.instance.connect(output.instance)
})
this.instance.start(this._pausedAt || time)
this.isPlaying = true
}
}
stop () {
if (this.isPlaying) {
// stop and delete source instance, since we can't call start more than
// once
this.instance.stop()
delete this.instance
this.isPlaying = false
this._pausedAt = 0
}
}
pause () {
if (this.isPlaying) {
this.instance.stop()
this.isPlaying = false
this._pausedAt = this.context.currentTime
}
}
update (config) {
if (this.type === 'buffer') {
if (config.buffer) this.instance.buffer = config.buffer
if (config.loop) this.instance.loop = config.loop
if (config.detune) this.instance.detune.value = config.detune
if (config.playbackRate) this.instance.playbackRate.value = config.playbackRate
} else if (this.type === 'constant') {
if (config.offset) this.instance.offset.value = config.offset
} else if (this.type === 'oscillator') {
if (config.type) this.instance.type = config.type
if (config.detune) this.instance.detune.value = config.detune
if (config.frequency) this.instance.frequency.value = config.frequency
}
}
}