Add measured room EQ for Mini 2
This commit is contained in:
@@ -5,6 +5,9 @@ import { spotifyStatus, startSpotify, finishSpotify } from './spotify.mjs';
|
||||
|
||||
const assets = new Map([
|
||||
['/', ['index.html', 'text/html; charset=utf-8']],
|
||||
['/room-mic', ['room-mic.html', 'text/html; charset=utf-8']],
|
||||
['/room-mic.js', ['room-mic.js', 'text/javascript; charset=utf-8']],
|
||||
['/room-mic-worklet.js', ['room-mic-worklet.js', 'text/javascript; charset=utf-8']],
|
||||
['/app.js', ['app.js', 'text/javascript; charset=utf-8']],
|
||||
['/style.css', ['style.css', 'text/css; charset=utf-8']],
|
||||
].map(([route, [file, type]]) => [route, [readFileSync(new URL('./public/' + file, import.meta.url)), type]]));
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
class RoomMic extends AudioWorkletProcessor {
|
||||
constructor() {
|
||||
super();
|
||||
this.remaining = sampleRate * 15;
|
||||
this.block = new Float32Array(4096);
|
||||
this.offset = 0;
|
||||
}
|
||||
|
||||
flush() {
|
||||
const block = this.block.slice(0, this.offset);
|
||||
this.port.postMessage(block, [block.buffer]);
|
||||
this.offset = 0;
|
||||
}
|
||||
|
||||
process(inputs) {
|
||||
const mono = inputs[0]?.[0];
|
||||
if (!mono || !this.remaining) return true;
|
||||
const count = Math.min(mono.length, this.remaining);
|
||||
for (let i = 0; i < count; i++) {
|
||||
this.block[this.offset++] = mono[i];
|
||||
if (this.offset === this.block.length) this.flush();
|
||||
}
|
||||
this.remaining -= count;
|
||||
if (this.remaining === 0) {
|
||||
if (this.offset) this.flush();
|
||||
this.port.postMessage('done');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
registerProcessor('room-mic', RoomMic);
|
||||
@@ -0,0 +1,19 @@
|
||||
<!doctype html>
|
||||
<html lang="ru">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<title>Замер Mini 2</title>
|
||||
<link rel="stylesheet" href="/style.css">
|
||||
<script type="module" src="/room-mic.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
<h1>Замер Mini 2</h1>
|
||||
<p>Поставь микрофон на высоте ушей в точке прослушивания. После начала записи запусти тестовый сигнал на колонке.</p>
|
||||
<button type="button" id="record" class="primary">Начать запись на 15 секунд</button>
|
||||
<p id="status" role="status"></p>
|
||||
<a id="download" hidden>Скачать WAV</a>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,83 @@
|
||||
const button = document.querySelector('#record');
|
||||
const status = document.querySelector('#status');
|
||||
const download = document.querySelector('#download');
|
||||
let objectUrl;
|
||||
|
||||
function wav(blocks, rate) {
|
||||
const n = blocks.reduce((count, block) => count + block.length, 0);
|
||||
const data = new DataView(new ArrayBuffer(44 + n * 2));
|
||||
const ascii = (offset, value) => [...value].forEach((letter, i) => data.setUint8(offset + i, letter.charCodeAt(0)));
|
||||
ascii(0, 'RIFF');
|
||||
data.setUint32(4, 36 + n * 2, true);
|
||||
ascii(8, 'WAVEfmt ');
|
||||
data.setUint32(16, 16, true);
|
||||
data.setUint16(20, 1, true);
|
||||
data.setUint16(22, 1, true);
|
||||
data.setUint32(24, rate, true);
|
||||
data.setUint32(28, rate * 2, true);
|
||||
data.setUint16(32, 2, true);
|
||||
data.setUint16(34, 16, true);
|
||||
ascii(36, 'data');
|
||||
data.setUint32(40, n * 2, true);
|
||||
let offset = 44;
|
||||
for (const block of blocks) {
|
||||
for (const value of block) {
|
||||
data.setInt16(offset, Math.round(Math.max(-1, Math.min(1, value)) * 32767), true);
|
||||
offset += 2;
|
||||
}
|
||||
}
|
||||
return new Blob([data], { type: 'audio/wav' });
|
||||
}
|
||||
|
||||
button.onclick = async () => {
|
||||
button.disabled = true;
|
||||
let audio, stream, deadline, node;
|
||||
const close = () => {
|
||||
clearTimeout(deadline);
|
||||
node?.disconnect();
|
||||
stream?.getTracks().forEach(track => track.stop());
|
||||
if (audio && audio.state !== 'closed') void audio.close();
|
||||
button.disabled = false;
|
||||
};
|
||||
try {
|
||||
audio = new AudioContext();
|
||||
await audio.resume();
|
||||
stream = await navigator.mediaDevices.getUserMedia({
|
||||
video: false,
|
||||
audio: { channelCount: 1, echoCancellation: false, noiseSuppression: false, autoGainControl: false },
|
||||
});
|
||||
const settings = stream.getAudioTracks()[0].getSettings();
|
||||
if ([settings.echoCancellation, settings.noiseSuppression, settings.autoGainControl].includes(true)) {
|
||||
throw new Error('Браузер оставил включённой обработку микрофона. Для точного замера используй другой браузер или Mac.');
|
||||
}
|
||||
await audio.audioWorklet.addModule('/room-mic-worklet.js');
|
||||
node = new AudioWorkletNode(audio, 'room-mic');
|
||||
const blocks = [];
|
||||
node.port.onmessage = ({ data }) => {
|
||||
if (data !== 'done') {
|
||||
blocks.push(data);
|
||||
return;
|
||||
}
|
||||
const blob = wav(blocks, audio.sampleRate);
|
||||
if (objectUrl) URL.revokeObjectURL(objectUrl);
|
||||
objectUrl = URL.createObjectURL(blob);
|
||||
download.href = objectUrl;
|
||||
download.download = `room-${new Date().toISOString().replaceAll(':', '-')}.wav`;
|
||||
download.hidden = false;
|
||||
status.textContent = 'Запись готова. Файл сохранится на этом устройстве.';
|
||||
close();
|
||||
};
|
||||
audio.createMediaStreamSource(stream).connect(node);
|
||||
node.connect(audio.destination);
|
||||
const unreported = [settings.echoCancellation, settings.noiseSuppression, settings.autoGainControl].some(value => value === undefined);
|
||||
status.textContent = 'Запись идёт. Запусти тестовый сигнал.' +
|
||||
(unreported ? ' Браузер не сообщил все настройки обработки.' : '');
|
||||
deadline = setTimeout(() => {
|
||||
close();
|
||||
status.textContent = 'Запись прервалась. Оставь вкладку открытой и попробуй снова.';
|
||||
}, 19000);
|
||||
} catch (error) {
|
||||
close();
|
||||
status.textContent = error.message;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user