-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
135 lines (94 loc) · 3.53 KB
/
script.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
console.log("Det funkkar!");
// for legacy browsers
const AudioContext = window.AudioContext || window.webkitAudioContext;
const audioContext = new AudioContext();
//load media
const file = document.getElementById('fileupload')
file.addEventListener('change', function(){
const files = this.files;
const audio1 = document.getElementById('audio1');
audio1.src = URL.createObjectURL(files[0]);
audio1.load();
})
// get the audio element
const audioElement = document.querySelector('audio');
// pass it into the audio context
const track = audioContext.createMediaElementSource(audioElement);
// Select our play button
const playButton = document.querySelector('button');
playButton.addEventListener('click', () => {
// Check if context is in suspended state (autoplay policy)
if (audioContext.state === 'suspended') {
audioContext.resume();
}
// Play or pause track depending on state
if (playButton.dataset.playing === 'false') {
audioElement.play();
playButton.dataset.playing = 'true';
} else if (playButton.dataset.playing === 'true') {
audioElement.pause();
playButton.dataset.playing = 'false';
}
}, false);
//gain node
const gainNode = audioContext.createGain();
const volumeControl = document.querySelector('#volume');
volumeControl.addEventListener('input',() =>{
gainNode.gain.value = volumeControl.value;
}, false);
//audiovisualizer
//analyser
const analyser = audioContext.createAnalyser();
//canvas element
const canvas = document.getElementById('Visualizer');
// canvas.width=window.innerWidth;
// canvas.height=window.innerHeight;
const ctx = canvas.getContext('2d');
//audioSourve = track analyser = analyser
analyser.fftSize=256;
const bufferLength = analyser.frequencyBinCount;
const dataArray = new Uint8Array(bufferLength);
const barWidth = (canvas.width/2)/bufferLength;
let barHeight;
//Animoi visualisaattori
function animate(){
x=0;
ctx.clearRect(0,0,canvas.width, canvas.height);
analyser. getByteFrequencyData(dataArray);
drawVisualizer(bufferLength, x, barWidth, barHeight, dataArray);
requestAnimationFrame(animate);
}
animate()
//piirrä visualisaattori
//todo Saisiko visualisaattorin numeroarvot printattua?
function drawVisualizer(bufferLength, x, barWidth, barHeight, dataArray){
for (let i=0; i< bufferLength; i++){
barHeight= dataArray[i]*5;
ctx.save();
ctx.translate(canvas.width/2, canvas.height/2);
ctx.rotate(i+Math.PI*2/bufferLength*4);
const hue = i*5
ctx.fillStyle = 'hsl('+ hue+ ',100%,50%'
ctx.fillRect(canvas.width/2 -x, canvas.height-barHeight -30, barWidth, 2);
ctx.fillStyle = 'hsl('+ hue+ ',100%,50%';
ctx.fillRect(canvas.width/2 -x, canvas.height-barHeight, barWidth, barHeight);
x+= barWidth;
ctx.restore();
}
}
//Audion yhdistäminen
track.connect(analyser)
analyser.connect(gainNode)
gainNode.connect(audioContext.destination);
console.log(audioContext);
function Start(){
document.getElementById("playbutton").innerHTML ="play/pause"
}
setInterval(()=>{
document.getElementById("teksti1").innerHTML= dataArray.slice(0,20);
document.getElementById("teksti2").innerHTML= dataArray.slice(20,40);
document.getElementById("teksti3").innerHTML= dataArray.slice(40,60);
document.getElementById("teksti4").innerHTML= dataArray.slice(60,80);
document.getElementById("teksti5").innerHTML= dataArray.slice(80,100);
document.getElementById("teksti6").innerHTML= dataArray.slice(100,120);
},100)