Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Ren Yuan committed May 31, 2021
1 parent a1f4e94 commit 4d84825
Show file tree
Hide file tree
Showing 9 changed files with 8,554 additions and 0 deletions.
3,983 changes: 3,983 additions & 0 deletions dist/c2.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/c2.min.js

Large diffs are not rendered by default.

158 changes: 158 additions & 0 deletions src/audio.ts
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);
}
}
Loading

0 comments on commit 4d84825

Please sign in to comment.