-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
175 lines (156 loc) · 5.62 KB
/
index.html
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Recording canvas+audio example</title>
</head>
<body>
<h1>Web Media Recording</h1>
<h4>1. Recording data from canvas tag</h4>
<h4>2. Recording data from audio tag</h4>
<h4>3. Merging streams from 1 and 2 to generate video with sound</h4>
<hr />
<canvas id="canvas" width="600" height="400"></canvas>
<audio src="./data/viper.mp3" id="audio" controls></audio>
<hr />
<br />
<button id="startButton">Start Recording</button>
<button id="stopButton">Stop Recording</button>
<button id="pauseButton">Pause Recording</button>
<button id="resumeButton">Resume Recording</button>
<button id="changeaudioButton">Change Audio</button>
<br />
<br />
<button id="previewButton">Preview Video</button>
<button id="closePreviewButton">Close Preview</button>
<button id="downloadButton">Download Video</button>
<script type="module" src="./dist/ac-recorder.js"></script>
<script type="module">
import { ACRecorder } from "./dist/ac-recorder.js";
let ACR = new ACRecorder(document.querySelector("#audio"), "#canvas");
ACR.setListeners({
start: () => {
console.warn("# recorder - start");
},
fail: () => {
console.warn("# recorder - fail");
},
pause: () => {
console.warn("# recorder - pause");
},
resume: () => {
console.warn("# recorder - resume");
},
stop: () => {
console.warn("# recorder - stop");
},
});
let audios = [
"./data/viper.mp3",
];
let times = 0;
let animateRAF = null;
// Get page elements
const startButton = document.getElementById("startButton");
const stopButton = document.getElementById("stopButton");
const pauseButton = document.getElementById("pauseButton");
const resumeButton = document.getElementById("resumeButton");
const previewButton = document.getElementById("previewButton");
const closePreviewButton = document.getElementById("closePreviewButton");
const downloadButton = document.getElementById("downloadButton");
const changeaudioButton = document.getElementById("changeaudioButton");
// Add event listeners
startButton.addEventListener("click", () => {
document
.querySelector("#audio")
.play()
.then((res) => {
ACR.start()
.then((res) => {
drawAudioFrequency(res);
})
.catch(console.error);
});
});
pauseButton.addEventListener("click", () => {
document.querySelector("#audio").pause();
cancelAnimationFrame(animateRAF)
ACR.pause();
});
resumeButton.addEventListener("click", () => {
document.querySelector("#audio").play();
ACR.resume();
});
stopButton.addEventListener("click", () => {
document.querySelector("#audio").pause();
cancelAnimationFrame(animateRAF)
ACR.stop();
});
previewButton.addEventListener("click", () => {
ACR.preview();
});
closePreviewButton.addEventListener("click", () => {
ACR.closePreview();
});
downloadButton.addEventListener("click", () => {
ACR.download();
});
changeaudioButton.addEventListener("click", () => {
let url = audios[times % audios.length];
console.log(times, url);
ACR.changeAudio(url);
times++;
});
function drawAudioFrequency(context = {}) {
const { canvas, audioContext, audioSourceNode } = context;
// Create analyser node
const analyserNode = audioContext.createAnalyser();
analyserNode.fftSize = 1024; // [32,32768]
// Connect the media source node and analyzer node
audioSourceNode.connect(analyserNode);
audioSourceNode.connect(audioContext.destination);
// Create a drawing environment
const canvasCtx = canvas.getContext("2d");
const cvsHeight = canvas.height;
const cvsHalfHeight = cvsHeight / 2;
const cvsWidth = canvas.width;
// Coordinate system transformation
canvasCtx.translate(0, cvsHeight / 2);
canvasCtx.scale(1, -1);
function animate() {
// Get analysis data
const dataArray = new Uint8Array(analyserNode.frequencyBinCount);
// analyserNode.getByteTimeDomainData(dataArray);
analyserNode.getByteFrequencyData(dataArray);
// Clear the canvas
canvasCtx.clearRect(0, -cvsHalfHeight, cvsWidth, cvsHeight);
// Set the brush style
canvasCtx.lineWidth = 2;
canvasCtx.strokeStyle = "#84019a";
// Start drawing path
canvasCtx.beginPath();
// Calculate the position of each data point
const sliceWidth = (cvsWidth * 1.0) / dataArray.length;
let x = 0;
for (let i = 0; i < dataArray.length; i++) {
const v = dataArray[i] / 128;
const y = (v * cvsHalfHeight) / 2;
if (i === 0) {
canvasCtx.moveTo(x, y);
} else {
canvasCtx.lineTo(x, y);
}
x += sliceWidth + 1;
}
// Stop drawing path
canvasCtx.stroke();
// Loop draw
animateRAF = requestAnimationFrame(animate);
}
animate();
}
</script>
</body>
</html>