-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebcodecs-opus-recorder-mse-wav-player.html
154 lines (151 loc) · 4.89 KB
/
webcodecs-opus-recorder-mse-wav-player.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
<!DOCTYPE html>
<html>
<head>
<title>
WebCodecs Opus Recorder/Media Source Extensions Opus EncodedAudioChunk,
WAV Player
</title>
<style>
fieldset {
width: 300px;
}
fieldset input,
label,
select,
audio {
position: relative;
display: block;
}
#file, #clear {
display:inline-block;
}
#file {
width: 200px;
}
</style>
</head>
<body>
<p>WebCodecs Opus Recorder and Player</p>
<button id="record">Record</button>
<button id="stop">Stop</button>
<br />
<label for="playback-type">Playback type:</label>
<select id="playback-type">
<option selected value="mediaSource">MediaSource</option>
<option value="wav">WAV</option>
</select>
<label for="media-session-metadata">
<code>MediaSession</code>
metadata:
</label>
<input
type="checkbox"
name="media-session-metadata"
id="media-session-metadata"
/>
<fieldset id="meta" disabled>
<label for="album">Album:</label>
<input type="text" id="album" name="album" />
<label for="artist">Artist:</label>
<input type="text" id="artist" name="artist" />
<label for="title">Title:</label>
<input type="text" id="title" name="title" />
<label for="artwork">Artwork:</label>
<input type="file" id="artwork" name="artwork" accept="image/*" />
</fieldset>
<label for="upload">
<code>.opus.webcodecs</code>
file upload:
</label>
<input type="file" accept=".opus.webcodecs" id="file" name="upload" />
<input type="button" id="clear" value="Clear file selection" />
<script type="module">
import {
WebCodecsOpusRecorder,
WebCodecsOpusPlayer,
WavAudioEncoder,
} from './WebCodecsOpusRecorder.js';
let webcodecsOpusRecorder, mediaRecorder, stream, track, audio;
document.getElementById('media-session-metadata').onchange = (e) => {
document.getElementById('meta').disabled = !e.target.checked;
};
document.getElementById('clear').onclick = (e) => {
document.getElementById('file').value = '';
};
document.getElementById('record').onclick = async () => {
stream = await navigator.mediaDevices.getUserMedia({
audio: {
echoCancellation: false,
noiseSupression: false,
autoGainControl: false,
channelCount: 2,
},
});
[track] = stream.getAudioTracks();
if (stream.getVideoTracks().length) {
stream.getVideoTracks()[0].stop();
stream.removeTrack(stream.getVideoTracks()[0]);
}
webcodecsOpusRecorder = new WebCodecsOpusRecorder(track);
webcodecsOpusRecorder.start();
console.log(
track.label,
await track.getConstraints(),
track.getSettings()
);
};
document.getElementById('stop').onclick = async () => {
if (!document.getElementById('meta').disabled) {
const metadata = {};
document
.querySelectorAll(
'input[name=artist], input[name=album], input[name=title]'
)
.forEach((el) => {
metadata[el.name] = el.value;
});
const ctx = document.createElement('canvas').getContext('2d');
const [artworkFile] = document.querySelector('#artwork').files;
const setArtwork = async (blob, context) => {
return [{
src: `data:${blob.type};base64,${btoa(
new Uint8Array(await blob.arrayBuffer()).reduce((acc, i) =>
acc += String.fromCharCode.apply(null, [i]), '')
)}`,
type: blob.type,
sizes: `${context.width}x${context.height}`,
}]
}
if (artworkFile) {
metadata.artwork = await setArtwork(artworkFile, await createImageBitmap(artworkFile));
} else {
ctx.fillStyle = 'goldenrod';
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
const blob = await new Promise((resolve) =>
ctx.canvas.toBlob(resolve)
);
metadata.artwork = await setArtwork(blob, ctx.canvas);
}
console.log(metadata, webcodecsOpusRecorder);
webcodecsOpusRecorder.metadata.mediaSessionMetadata = metadata;
}
webcodecsOpusRecorder.stop();
};
document.getElementById('file').onchange = async ({
target: {
files: [file],
},
}) => {
if (file) {
const webcodecsOpusPlayer = new WebCodecsOpusPlayer(
await file.arrayBuffer(),
{
type: document.getElementById('playback-type').value,
}
);
await webcodecsOpusPlayer.play();
}
};
</script>
</body>
</html>