-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ren Yuan
committed
May 31, 2021
1 parent
a1f4e94
commit 4d84825
Showing
9 changed files
with
8,554 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
//Created by Ren Yuan | ||
|
||
|
||
let audioContext; | ||
if (typeof window !== 'undefined') { | ||
window.AudioContext = window.AudioContext || (window as any).webkitAudioContext; | ||
audioContext = new AudioContext(); | ||
} | ||
|
||
|
||
export class AudioIn{ | ||
context:AudioContext; | ||
node:MediaStreamAudioSourceNode; | ||
|
||
constructor(callback:Function){ | ||
this.context = audioContext; | ||
let constraints = {audio: true, video: false}; | ||
window.navigator.mediaDevices.getUserMedia(constraints).then((stream) => { | ||
this.node = audioContext.createMediaStreamSource(stream); | ||
callback(); | ||
}).catch((error) => {}); | ||
} | ||
|
||
connect(node:AudioNode){ | ||
this.node.connect(node); | ||
} | ||
|
||
disconnect(){ | ||
this.node.disconnect(); | ||
} | ||
} | ||
|
||
|
||
export class Sound{ | ||
context:AudioContext; | ||
audio:HTMLMediaElement; | ||
node:MediaElementAudioSourceNode; | ||
|
||
constructor(url:string, callback:Function){ | ||
this.context = audioContext; | ||
this.audio = new Audio(url); | ||
this.audio.addEventListener('loadeddata', () => { | ||
this.node = audioContext.createMediaElementSource(this.audio); | ||
callback(); | ||
}); | ||
} | ||
|
||
duration():number{ | ||
return this.audio.duration; | ||
} | ||
|
||
currentTime():number{ | ||
return this.audio.currentTime; | ||
} | ||
|
||
volume():number{ | ||
return this.audio.volume; | ||
} | ||
|
||
paused():boolean{ | ||
return this.audio.paused; | ||
} | ||
|
||
loop(loop:boolean = true){ | ||
this.audio.loop = loop; | ||
} | ||
|
||
autoplay(auto:boolean = true){ | ||
this.audio.autoplay = auto; | ||
} | ||
|
||
play(){ | ||
this.audio.play(); | ||
} | ||
|
||
pause(){ | ||
this.audio.pause(); | ||
} | ||
|
||
connect(node:AudioNode){ | ||
this.node.connect(node); | ||
} | ||
|
||
disconnect(){ | ||
this.node.disconnect(); | ||
} | ||
} | ||
|
||
|
||
export class Analyser{ | ||
context:AudioContext; | ||
node:AnalyserNode; | ||
|
||
constructor(){ | ||
this.context = audioContext; | ||
this.node = audioContext.createAnalyser(); | ||
} | ||
|
||
analyze(audio:AudioIn|Sound){ | ||
audio.node.connect(this.node); | ||
} | ||
|
||
fftSize(fftSize:number){ | ||
this.node.fftSize = fftSize; | ||
} | ||
|
||
binCount():number{ | ||
return this.node.frequencyBinCount; | ||
} | ||
|
||
smooth(smoothing:number){ | ||
this.node.smoothingTimeConstant = smoothing; | ||
} | ||
|
||
level():number { | ||
let data = this.timeDomain(); | ||
let sum = 0; | ||
for (let i=0; i<data.length; i++) { | ||
sum += data[i]*data[i]; | ||
} | ||
sum /= data.length; | ||
return Math.sqrt(sum); | ||
} | ||
|
||
timeDomain():number[]{ | ||
let timeDomain = new Uint8Array(this.node.frequencyBinCount); | ||
this.node.getByteTimeDomainData(timeDomain); | ||
|
||
let data = new Array(timeDomain.length); | ||
for (let i=0; i<timeDomain.length; i++) { | ||
data[i] = (timeDomain[i]-128)/128; | ||
} | ||
return data; | ||
} | ||
|
||
freqDomain():number[]{ | ||
let freqDomain = new Uint8Array(this.node.frequencyBinCount); | ||
this.node.getByteFrequencyData(freqDomain); | ||
|
||
let data = new Array(freqDomain.length); | ||
for (let i=0; i<freqDomain.length; i++) { | ||
data[i] = freqDomain[i]/255; | ||
} | ||
return data; | ||
} | ||
|
||
connect(node:AudioNode){ | ||
this.node.connect(node); | ||
} | ||
|
||
disconnect(){ | ||
this.node.disconnect(); | ||
} | ||
|
||
output(){ | ||
this.node.connect(audioContext.destination); | ||
} | ||
} |
Oops, something went wrong.