forked from Okazari/Rythm.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.js
More file actions
95 lines (83 loc) · 2.76 KB
/
Player.js
File metadata and controls
95 lines (83 loc) · 2.76 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
import Analyser from './Analyser.js'
class Player {
constructor(forceAudioContext) {
this.browserAudioCtx = window.AudioContext || window.webkitAudioContext
this.audioCtx = forceAudioContext || new this.browserAudioCtx()
this.connectedSources = []
Analyser.initialise(this.audioCtx.createAnalyser())
this.gain = this.audioCtx.createGain()
this.source = {}
this.audio = {}
this.hzHistory = []
this.inputTypeList = {
"TRACK" : 0,
"STREAM" : 1,
"EXTERNAL" : 2,
}
}
createSourceFromAudioElement = (audioElement) => {
audioElement.setAttribute('rythm-connected', this.connectedSources.length)
const source = this.audioCtx.createMediaElementSource(audioElement)
this.connectedSources.push(source)
return source
}
connectExternalAudioElement = (audioElement) => {
this.audio = audioElement
this.currentInputType = this.inputTypeList['EXTERNAL']
const connectedIndex = audioElement.getAttribute('rythm-connected')
if (!connectedIndex) {
this.source = this.createSourceFromAudioElement(this.audio)
} else {
this.source = this.connectedSources[connectedIndex]
}
this.connectSource(this.source)
}
connectSource = (source) => {
source.connect(this.gain)
this.gain.connect(Analyser.analyser)
if(this.currentInputType !== this.inputTypeList['STREAM']){
Analyser.analyser.connect(this.audioCtx.destination)
this.audio.addEventListener("ended", this.stop)
}
}
setMusic = (trackUrl) => {
this.audio = new Audio(trackUrl)
this.currentInputType = this.inputTypeList['TRACK']
this.source = this.createSourceFromAudioElement(this.audio)
this.connectSource(this.source)
}
setGain = (value) => {
this.gain.gain.value = value
}
plugMicrophone = () => {
return this.getMicrophoneStream().then(stream => {
this.audio = stream
this.currentInputType = this.inputTypeList['STREAM']
this.source = this.audioCtx.createMediaStreamSource(stream)
this.connectSource(this.source)
})
}
getMicrophoneStream = () => {
navigator.getUserMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia)
return new Promise((resolve, reject) => {
navigator.getUserMedia(
{ audio:true },
medias => resolve(medias),
error => reject(error)
)
})
}
start = () => {
if(this.currentInputType === this.inputTypeList['TRACK']){
this.audio.play()
}
}
stop = () => {
if (this.currentInputType === this.inputTypeList['TRACK']) {
this.audio.pause()
} else if (this.currentInputType === this.inputTypeList['STREAM']) {
this.audio.getAudioTracks()[0].enabled = false
}
}
}
export default Player