dechatter 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +241 -241
- package/THIRD_PARTY_NOTICES.md +24 -24
- package/package.json +39 -39
- package/web/dechatter.zip +0 -0
- package/web/engine.js +92 -92
- package/web/index.html +81 -81
- package/web/mic-worklet.js +34 -34
- package/web/mic.js +106 -106
- package/web/pyodide/pyodide.js +4 -4
- package/web/widget.css +85 -85
- package/web/widget.js +806 -806
- package/web/wizard.js +190 -190
package/web/engine.js
CHANGED
|
@@ -1,92 +1,92 @@
|
|
|
1
|
-
// Web worker: dechatter's Python running in Pyodide. Sessions live in a filesystem that is
|
|
2
|
-
// persisted to IndexedDB, so they survive reloads and browser restarts.
|
|
3
|
-
|
|
4
|
-
import { loadPyodide } from './pyodide/pyodide.js';
|
|
5
|
-
|
|
6
|
-
const ROOT = '/sessions';
|
|
7
|
-
// Calls that change files; the filesystem is flushed to IndexedDB after each of them.
|
|
8
|
-
const WRITES = new Set(['create', 'save_setup', 'plan', 'record_stop', 'analyze', 'set_verdict', 'import_zip']);
|
|
9
|
-
|
|
10
|
-
let pyodide;
|
|
11
|
-
let call;
|
|
12
|
-
|
|
13
|
-
// cncjs serves .wasm as application/octet-stream, which streaming compilation refuses.
|
|
14
|
-
// Relabel those responses; the bytes still stream straight through.
|
|
15
|
-
const plainFetch = self.fetch.bind(self);
|
|
16
|
-
self.fetch = async (input, init) => {
|
|
17
|
-
const response = await plainFetch(input, init);
|
|
18
|
-
const url = typeof input === 'string' ? input : (input.url ?? String(input));
|
|
19
|
-
if (!response.ok || !url.split('?')[0].endsWith('.wasm')) {
|
|
20
|
-
return response;
|
|
21
|
-
}
|
|
22
|
-
return new Response(response.body, { status: response.status, headers: { 'Content-Type': 'application/wasm' } });
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
function sync(populate) {
|
|
26
|
-
return new Promise((resolve, reject) => {
|
|
27
|
-
pyodide.FS.syncfs(populate, (err) => (err ? reject(err) : resolve()));
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
async function boot() {
|
|
32
|
-
postMessage({ boot: 'Loading Python' });
|
|
33
|
-
pyodide = await loadPyodide({ indexURL: new URL('./pyodide/', import.meta.url).href });
|
|
34
|
-
postMessage({ boot: 'Loading numpy and scipy' });
|
|
35
|
-
await pyodide.loadPackage(['numpy', 'scipy']);
|
|
36
|
-
const source = await fetch(new URL('./dechatter.zip', import.meta.url));
|
|
37
|
-
if (!source.ok) {
|
|
38
|
-
throw new Error(`cannot load dechatter.zip: ${source.status}`);
|
|
39
|
-
}
|
|
40
|
-
pyodide.unpackArchive(await source.arrayBuffer(), 'zip');
|
|
41
|
-
pyodide.FS.mkdirTree(ROOT);
|
|
42
|
-
pyodide.FS.mount(pyodide.FS.filesystems.IDBFS, {}, ROOT);
|
|
43
|
-
await sync(true);
|
|
44
|
-
call = pyodide.runPython(`
|
|
45
|
-
import logging
|
|
46
|
-
from pathlib import Path
|
|
47
|
-
from dechatter import browser
|
|
48
|
-
|
|
49
|
-
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(name)s: %(message)s")
|
|
50
|
-
_sessions = browser.Sessions(Path("${ROOT}"))
|
|
51
|
-
|
|
52
|
-
def _call(method, *args):
|
|
53
|
-
return browser.call(_sessions, method, *args)
|
|
54
|
-
|
|
55
|
-
_call
|
|
56
|
-
`);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
async function handle({ method, args }) {
|
|
60
|
-
const converted = args.map((arg) => (ArrayBuffer.isView(arg) ? pyodide.toPy(arg) : arg));
|
|
61
|
-
const result = call(method, ...converted);
|
|
62
|
-
for (const arg of converted) {
|
|
63
|
-
arg?.destroy?.();
|
|
64
|
-
}
|
|
65
|
-
if (typeof result !== 'string') {
|
|
66
|
-
const bytes = result.toJs();
|
|
67
|
-
result.destroy();
|
|
68
|
-
return { ok: bytes };
|
|
69
|
-
}
|
|
70
|
-
const reply = JSON.parse(result);
|
|
71
|
-
if ('ok' in reply && WRITES.has(method)) {
|
|
72
|
-
await sync(false);
|
|
73
|
-
}
|
|
74
|
-
return reply;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const ready = boot().then(
|
|
78
|
-
() => postMessage({ boot: null }),
|
|
79
|
-
(err) => {
|
|
80
|
-
postMessage({ boot: null, fatal: `dechatter could not start: ${err.message ?? err}` });
|
|
81
|
-
throw err;
|
|
82
|
-
},
|
|
83
|
-
);
|
|
84
|
-
|
|
85
|
-
onmessage = async ({ data }) => {
|
|
86
|
-
try {
|
|
87
|
-
await ready;
|
|
88
|
-
postMessage({ id: data.id, ...(await handle(data)) });
|
|
89
|
-
} catch (err) {
|
|
90
|
-
postMessage({ id: data.id, error: `${err.message ?? err}` });
|
|
91
|
-
}
|
|
92
|
-
};
|
|
1
|
+
// Web worker: dechatter's Python running in Pyodide. Sessions live in a filesystem that is
|
|
2
|
+
// persisted to IndexedDB, so they survive reloads and browser restarts.
|
|
3
|
+
|
|
4
|
+
import { loadPyodide } from './pyodide/pyodide.js';
|
|
5
|
+
|
|
6
|
+
const ROOT = '/sessions';
|
|
7
|
+
// Calls that change files; the filesystem is flushed to IndexedDB after each of them.
|
|
8
|
+
const WRITES = new Set(['create', 'save_setup', 'plan', 'record_stop', 'analyze', 'set_verdict', 'import_zip']);
|
|
9
|
+
|
|
10
|
+
let pyodide;
|
|
11
|
+
let call;
|
|
12
|
+
|
|
13
|
+
// cncjs serves .wasm as application/octet-stream, which streaming compilation refuses.
|
|
14
|
+
// Relabel those responses; the bytes still stream straight through.
|
|
15
|
+
const plainFetch = self.fetch.bind(self);
|
|
16
|
+
self.fetch = async (input, init) => {
|
|
17
|
+
const response = await plainFetch(input, init);
|
|
18
|
+
const url = typeof input === 'string' ? input : (input.url ?? String(input));
|
|
19
|
+
if (!response.ok || !url.split('?')[0].endsWith('.wasm')) {
|
|
20
|
+
return response;
|
|
21
|
+
}
|
|
22
|
+
return new Response(response.body, { status: response.status, headers: { 'Content-Type': 'application/wasm' } });
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
function sync(populate) {
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
pyodide.FS.syncfs(populate, (err) => (err ? reject(err) : resolve()));
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function boot() {
|
|
32
|
+
postMessage({ boot: 'Loading Python' });
|
|
33
|
+
pyodide = await loadPyodide({ indexURL: new URL('./pyodide/', import.meta.url).href });
|
|
34
|
+
postMessage({ boot: 'Loading numpy and scipy' });
|
|
35
|
+
await pyodide.loadPackage(['numpy', 'scipy']);
|
|
36
|
+
const source = await fetch(new URL('./dechatter.zip', import.meta.url));
|
|
37
|
+
if (!source.ok) {
|
|
38
|
+
throw new Error(`cannot load dechatter.zip: ${source.status}`);
|
|
39
|
+
}
|
|
40
|
+
pyodide.unpackArchive(await source.arrayBuffer(), 'zip');
|
|
41
|
+
pyodide.FS.mkdirTree(ROOT);
|
|
42
|
+
pyodide.FS.mount(pyodide.FS.filesystems.IDBFS, {}, ROOT);
|
|
43
|
+
await sync(true);
|
|
44
|
+
call = pyodide.runPython(`
|
|
45
|
+
import logging
|
|
46
|
+
from pathlib import Path
|
|
47
|
+
from dechatter import browser
|
|
48
|
+
|
|
49
|
+
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(name)s: %(message)s")
|
|
50
|
+
_sessions = browser.Sessions(Path("${ROOT}"))
|
|
51
|
+
|
|
52
|
+
def _call(method, *args):
|
|
53
|
+
return browser.call(_sessions, method, *args)
|
|
54
|
+
|
|
55
|
+
_call
|
|
56
|
+
`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function handle({ method, args }) {
|
|
60
|
+
const converted = args.map((arg) => (ArrayBuffer.isView(arg) ? pyodide.toPy(arg) : arg));
|
|
61
|
+
const result = call(method, ...converted);
|
|
62
|
+
for (const arg of converted) {
|
|
63
|
+
arg?.destroy?.();
|
|
64
|
+
}
|
|
65
|
+
if (typeof result !== 'string') {
|
|
66
|
+
const bytes = result.toJs();
|
|
67
|
+
result.destroy();
|
|
68
|
+
return { ok: bytes };
|
|
69
|
+
}
|
|
70
|
+
const reply = JSON.parse(result);
|
|
71
|
+
if ('ok' in reply && WRITES.has(method)) {
|
|
72
|
+
await sync(false);
|
|
73
|
+
}
|
|
74
|
+
return reply;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const ready = boot().then(
|
|
78
|
+
() => postMessage({ boot: null }),
|
|
79
|
+
(err) => {
|
|
80
|
+
postMessage({ boot: null, fatal: `dechatter could not start: ${err.message ?? err}` });
|
|
81
|
+
throw err;
|
|
82
|
+
},
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
onmessage = async ({ data }) => {
|
|
86
|
+
try {
|
|
87
|
+
await ready;
|
|
88
|
+
postMessage({ id: data.id, ...(await handle(data)) });
|
|
89
|
+
} catch (err) {
|
|
90
|
+
postMessage({ id: data.id, error: `${err.message ?? err}` });
|
|
91
|
+
}
|
|
92
|
+
};
|
package/web/index.html
CHANGED
|
@@ -1,81 +1,81 @@
|
|
|
1
|
-
<!doctype html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
<head>
|
|
4
|
-
<meta charset="utf-8">
|
|
5
|
-
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
-
<title>dechatter</title>
|
|
7
|
-
<link rel="stylesheet" href="widget.css">
|
|
8
|
-
</head>
|
|
9
|
-
<body>
|
|
10
|
-
<p id="error" class="error" hidden></p>
|
|
11
|
-
<p id="boot" class="muted">Starting.</p>
|
|
12
|
-
|
|
13
|
-
<div id="main" hidden>
|
|
14
|
-
<header>
|
|
15
|
-
<select id="sessions" aria-label="Session"></select>
|
|
16
|
-
<button id="new-session">New</button>
|
|
17
|
-
<button id="edit-setup" disabled>Setup</button>
|
|
18
|
-
<button id="export" disabled>Export</button>
|
|
19
|
-
<button id="import">Import</button>
|
|
20
|
-
<input id="import-file" type="file" accept=".zip" hidden>
|
|
21
|
-
<div id="summary" class="muted"></div>
|
|
22
|
-
</header>
|
|
23
|
-
|
|
24
|
-
<dl id="links">
|
|
25
|
-
<dt>cncjs</dt>
|
|
26
|
-
<dd id="cncjs">connecting</dd>
|
|
27
|
-
<dt>Microphone</dt>
|
|
28
|
-
<dd>
|
|
29
|
-
<button id="enable-mic" hidden>Enable microphone</button>
|
|
30
|
-
<select id="device" aria-label="Microphone" hidden></select>
|
|
31
|
-
<meter id="level" min="-100" max="0" low="-70" high="-6" optimum="-30" value="-100" hidden></meter>
|
|
32
|
-
<span id="level-db" class="muted"></span>
|
|
33
|
-
</dd>
|
|
34
|
-
</dl>
|
|
35
|
-
<p id="mic-warning" class="error" hidden></p>
|
|
36
|
-
|
|
37
|
-
<section id="setup" hidden>
|
|
38
|
-
<h2 id="setup-title"></h2>
|
|
39
|
-
<p id="setup-hint" class="muted"></p>
|
|
40
|
-
<form id="setup-form"></form>
|
|
41
|
-
<p class="wizard-buttons">
|
|
42
|
-
<button id="setup-back" type="button">Back</button>
|
|
43
|
-
<button id="setup-next" class="primary" type="submit" form="setup-form">Next</button>
|
|
44
|
-
<button id="cancel-setup" type="button">Cancel</button>
|
|
45
|
-
<span id="units" class="muted"></span>
|
|
46
|
-
<button id="follow-machine" type="button" hidden>Use mm instead</button>
|
|
47
|
-
</p>
|
|
48
|
-
</section>
|
|
49
|
-
|
|
50
|
-
<section id="step" hidden></section>
|
|
51
|
-
|
|
52
|
-
<section id="round" hidden>
|
|
53
|
-
<h2 id="round-title"></h2>
|
|
54
|
-
<table>
|
|
55
|
-
<thead>
|
|
56
|
-
<tr><th>#</th><th>rpm</th><th>width</th><th>feed</th><th>P</th><th>result</th></tr>
|
|
57
|
-
</thead>
|
|
58
|
-
<tbody id="passes"></tbody>
|
|
59
|
-
</table>
|
|
60
|
-
</section>
|
|
61
|
-
|
|
62
|
-
<section id="map-section" hidden>
|
|
63
|
-
<h2 id="map-title">Stability map</h2>
|
|
64
|
-
<canvas id="map" width="320" height="180"></canvas>
|
|
65
|
-
<p class="muted legend">
|
|
66
|
-
Green is likely stable, red likely chatter. Dots are stable cuts, crosses chatter.
|
|
67
|
-
</p>
|
|
68
|
-
</section>
|
|
69
|
-
|
|
70
|
-
<section id="best-section" hidden>
|
|
71
|
-
<h2>Best stable cuts</h2>
|
|
72
|
-
<table>
|
|
73
|
-
<thead><tr><th>rpm</th><th>width</th><th>depth</th><th>feed</th><th>cm³/min</th></tr></thead>
|
|
74
|
-
<tbody id="best"></tbody>
|
|
75
|
-
</table>
|
|
76
|
-
</section>
|
|
77
|
-
</div>
|
|
78
|
-
|
|
79
|
-
<script type="module" src="widget.js"></script>
|
|
80
|
-
</body>
|
|
81
|
-
</html>
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<title>dechatter</title>
|
|
7
|
+
<link rel="stylesheet" href="widget.css">
|
|
8
|
+
</head>
|
|
9
|
+
<body>
|
|
10
|
+
<p id="error" class="error" hidden></p>
|
|
11
|
+
<p id="boot" class="muted">Starting.</p>
|
|
12
|
+
|
|
13
|
+
<div id="main" hidden>
|
|
14
|
+
<header>
|
|
15
|
+
<select id="sessions" aria-label="Session"></select>
|
|
16
|
+
<button id="new-session">New</button>
|
|
17
|
+
<button id="edit-setup" disabled>Setup</button>
|
|
18
|
+
<button id="export" disabled>Export</button>
|
|
19
|
+
<button id="import">Import</button>
|
|
20
|
+
<input id="import-file" type="file" accept=".zip" hidden>
|
|
21
|
+
<div id="summary" class="muted"></div>
|
|
22
|
+
</header>
|
|
23
|
+
|
|
24
|
+
<dl id="links">
|
|
25
|
+
<dt>cncjs</dt>
|
|
26
|
+
<dd id="cncjs">connecting</dd>
|
|
27
|
+
<dt>Microphone</dt>
|
|
28
|
+
<dd>
|
|
29
|
+
<button id="enable-mic" hidden>Enable microphone</button>
|
|
30
|
+
<select id="device" aria-label="Microphone" hidden></select>
|
|
31
|
+
<meter id="level" min="-100" max="0" low="-70" high="-6" optimum="-30" value="-100" hidden></meter>
|
|
32
|
+
<span id="level-db" class="muted"></span>
|
|
33
|
+
</dd>
|
|
34
|
+
</dl>
|
|
35
|
+
<p id="mic-warning" class="error" hidden></p>
|
|
36
|
+
|
|
37
|
+
<section id="setup" hidden>
|
|
38
|
+
<h2 id="setup-title"></h2>
|
|
39
|
+
<p id="setup-hint" class="muted"></p>
|
|
40
|
+
<form id="setup-form"></form>
|
|
41
|
+
<p class="wizard-buttons">
|
|
42
|
+
<button id="setup-back" type="button">Back</button>
|
|
43
|
+
<button id="setup-next" class="primary" type="submit" form="setup-form">Next</button>
|
|
44
|
+
<button id="cancel-setup" type="button">Cancel</button>
|
|
45
|
+
<span id="units" class="muted"></span>
|
|
46
|
+
<button id="follow-machine" type="button" hidden>Use mm instead</button>
|
|
47
|
+
</p>
|
|
48
|
+
</section>
|
|
49
|
+
|
|
50
|
+
<section id="step" hidden></section>
|
|
51
|
+
|
|
52
|
+
<section id="round" hidden>
|
|
53
|
+
<h2 id="round-title"></h2>
|
|
54
|
+
<table>
|
|
55
|
+
<thead>
|
|
56
|
+
<tr><th>#</th><th>rpm</th><th>width</th><th>feed</th><th>P</th><th>result</th></tr>
|
|
57
|
+
</thead>
|
|
58
|
+
<tbody id="passes"></tbody>
|
|
59
|
+
</table>
|
|
60
|
+
</section>
|
|
61
|
+
|
|
62
|
+
<section id="map-section" hidden>
|
|
63
|
+
<h2 id="map-title">Stability map</h2>
|
|
64
|
+
<canvas id="map" width="320" height="180"></canvas>
|
|
65
|
+
<p class="muted legend">
|
|
66
|
+
Green is likely stable, red likely chatter. Dots are stable cuts, crosses chatter.
|
|
67
|
+
</p>
|
|
68
|
+
</section>
|
|
69
|
+
|
|
70
|
+
<section id="best-section" hidden>
|
|
71
|
+
<h2>Best stable cuts</h2>
|
|
72
|
+
<table>
|
|
73
|
+
<thead><tr><th>rpm</th><th>width</th><th>depth</th><th>feed</th><th>cm³/min</th></tr></thead>
|
|
74
|
+
<tbody id="best"></tbody>
|
|
75
|
+
</table>
|
|
76
|
+
</section>
|
|
77
|
+
</div>
|
|
78
|
+
|
|
79
|
+
<script type="module" src="widget.js"></script>
|
|
80
|
+
</body>
|
|
81
|
+
</html>
|
package/web/mic-worklet.js
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
// Audio thread: hand the microphone's samples to the page in blocks, mixed down to mono.
|
|
2
|
-
|
|
3
|
-
const BLOCK = 4096;
|
|
4
|
-
|
|
5
|
-
class Capture extends AudioWorkletProcessor {
|
|
6
|
-
constructor() {
|
|
7
|
-
super();
|
|
8
|
-
this.block = new Float32Array(BLOCK);
|
|
9
|
-
this.filled = 0;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
process(inputs) {
|
|
13
|
-
const channels = inputs[0];
|
|
14
|
-
if (!channels.length) {
|
|
15
|
-
return true;
|
|
16
|
-
}
|
|
17
|
-
const frames = channels[0].length;
|
|
18
|
-
for (let i = 0; i < frames; i += 1) {
|
|
19
|
-
let sum = 0;
|
|
20
|
-
for (const channel of channels) {
|
|
21
|
-
sum += channel[i];
|
|
22
|
-
}
|
|
23
|
-
this.block[this.filled] = sum / channels.length;
|
|
24
|
-
this.filled += 1;
|
|
25
|
-
if (this.filled === BLOCK) {
|
|
26
|
-
this.port.postMessage(this.block.slice());
|
|
27
|
-
this.filled = 0;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
return true;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
registerProcessor('capture', Capture);
|
|
1
|
+
// Audio thread: hand the microphone's samples to the page in blocks, mixed down to mono.
|
|
2
|
+
|
|
3
|
+
const BLOCK = 4096;
|
|
4
|
+
|
|
5
|
+
class Capture extends AudioWorkletProcessor {
|
|
6
|
+
constructor() {
|
|
7
|
+
super();
|
|
8
|
+
this.block = new Float32Array(BLOCK);
|
|
9
|
+
this.filled = 0;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
process(inputs) {
|
|
13
|
+
const channels = inputs[0];
|
|
14
|
+
if (!channels.length) {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
const frames = channels[0].length;
|
|
18
|
+
for (let i = 0; i < frames; i += 1) {
|
|
19
|
+
let sum = 0;
|
|
20
|
+
for (const channel of channels) {
|
|
21
|
+
sum += channel[i];
|
|
22
|
+
}
|
|
23
|
+
this.block[this.filled] = sum / channels.length;
|
|
24
|
+
this.filled += 1;
|
|
25
|
+
if (this.filled === BLOCK) {
|
|
26
|
+
this.port.postMessage(this.block.slice());
|
|
27
|
+
this.filled = 0;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
registerProcessor('capture', Capture);
|
package/web/mic.js
CHANGED
|
@@ -1,106 +1,106 @@
|
|
|
1
|
-
// The browser's microphone, raw: the voice-call processing browsers apply by default
|
|
2
|
-
// (echo cancellation, noise suppression, automatic gain) would reshape the very spectrum the
|
|
3
|
-
// chatter detector reads, so it is switched off and checked.
|
|
4
|
-
|
|
5
|
-
const PROCESSING = ['echoCancellation', 'noiseSuppression', 'autoGainControl'];
|
|
6
|
-
|
|
7
|
-
export class Mic {
|
|
8
|
-
constructor() {
|
|
9
|
-
this.context = null;
|
|
10
|
-
this.stream = null;
|
|
11
|
-
this.label = '';
|
|
12
|
-
this.rate = 0;
|
|
13
|
-
this.levelDb = null;
|
|
14
|
-
this.processing = []; // processing the browser kept on despite being asked not to
|
|
15
|
-
this.chunks = null; // Float32Array blocks while recording, else null
|
|
16
|
-
this.frames = 0;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
get open() {
|
|
20
|
-
return this.stream !== null;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
get recording() {
|
|
24
|
-
return this.chunks !== null;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
get seconds() {
|
|
28
|
-
return this.rate ? this.frames / this.rate : 0;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
async devices() {
|
|
32
|
-
const all = await navigator.mediaDevices.enumerateDevices();
|
|
33
|
-
return all.filter((d) => d.kind === 'audioinput' && d.deviceId).map((d) => ({ id: d.deviceId, label: d.label }));
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
async start(deviceId) {
|
|
37
|
-
await this.close();
|
|
38
|
-
const audio = { channelCount: 1 };
|
|
39
|
-
for (const name of PROCESSING) {
|
|
40
|
-
audio[name] = false;
|
|
41
|
-
}
|
|
42
|
-
if (deviceId) {
|
|
43
|
-
audio.deviceId = { exact: deviceId };
|
|
44
|
-
}
|
|
45
|
-
const stream = await navigator.mediaDevices.getUserMedia({ audio });
|
|
46
|
-
const track = stream.getAudioTracks()[0];
|
|
47
|
-
const settings = track.getSettings();
|
|
48
|
-
const context = new AudioContext();
|
|
49
|
-
await context.audioWorklet.addModule(new URL('./mic-worklet.js', import.meta.url));
|
|
50
|
-
const capture = new AudioWorkletNode(context, 'capture');
|
|
51
|
-
capture.port.onmessage = ({ data }) => this.onBlock(data);
|
|
52
|
-
context.createMediaStreamSource(stream).connect(capture);
|
|
53
|
-
|
|
54
|
-
this.context = context;
|
|
55
|
-
this.stream = stream;
|
|
56
|
-
this.label = track.label;
|
|
57
|
-
this.rate = context.sampleRate;
|
|
58
|
-
this.processing = PROCESSING.filter((name) => settings[name] === true);
|
|
59
|
-
track.onended = () => this.close();
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
onBlock(block) {
|
|
63
|
-
let power = 0;
|
|
64
|
-
for (const sample of block) {
|
|
65
|
-
power += sample * sample;
|
|
66
|
-
}
|
|
67
|
-
this.levelDb = 10 * Math.log10(power / block.length + 1e-12);
|
|
68
|
-
if (this.chunks) {
|
|
69
|
-
this.chunks.push(block);
|
|
70
|
-
this.frames += block.length;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
record() {
|
|
75
|
-
this.chunks = [];
|
|
76
|
-
this.frames = 0;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// Everything heard since record(), as one Float32Array.
|
|
80
|
-
finish() {
|
|
81
|
-
const samples = new Float32Array(this.frames);
|
|
82
|
-
let offset = 0;
|
|
83
|
-
for (const chunk of this.chunks ?? []) {
|
|
84
|
-
samples.set(chunk, offset);
|
|
85
|
-
offset += chunk.length;
|
|
86
|
-
}
|
|
87
|
-
this.chunks = null;
|
|
88
|
-
this.frames = 0;
|
|
89
|
-
return samples;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// Browsers may keep an audio context suspended until the user interacts with the page.
|
|
93
|
-
async resume() {
|
|
94
|
-
if (this.context?.state === 'suspended') {
|
|
95
|
-
await this.context.resume();
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
async close() {
|
|
100
|
-
this.stream?.getTracks().forEach((track) => track.stop());
|
|
101
|
-
await this.context?.close();
|
|
102
|
-
this.stream = null;
|
|
103
|
-
this.context = null;
|
|
104
|
-
this.levelDb = null;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
1
|
+
// The browser's microphone, raw: the voice-call processing browsers apply by default
|
|
2
|
+
// (echo cancellation, noise suppression, automatic gain) would reshape the very spectrum the
|
|
3
|
+
// chatter detector reads, so it is switched off and checked.
|
|
4
|
+
|
|
5
|
+
const PROCESSING = ['echoCancellation', 'noiseSuppression', 'autoGainControl'];
|
|
6
|
+
|
|
7
|
+
export class Mic {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.context = null;
|
|
10
|
+
this.stream = null;
|
|
11
|
+
this.label = '';
|
|
12
|
+
this.rate = 0;
|
|
13
|
+
this.levelDb = null;
|
|
14
|
+
this.processing = []; // processing the browser kept on despite being asked not to
|
|
15
|
+
this.chunks = null; // Float32Array blocks while recording, else null
|
|
16
|
+
this.frames = 0;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
get open() {
|
|
20
|
+
return this.stream !== null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
get recording() {
|
|
24
|
+
return this.chunks !== null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
get seconds() {
|
|
28
|
+
return this.rate ? this.frames / this.rate : 0;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async devices() {
|
|
32
|
+
const all = await navigator.mediaDevices.enumerateDevices();
|
|
33
|
+
return all.filter((d) => d.kind === 'audioinput' && d.deviceId).map((d) => ({ id: d.deviceId, label: d.label }));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async start(deviceId) {
|
|
37
|
+
await this.close();
|
|
38
|
+
const audio = { channelCount: 1 };
|
|
39
|
+
for (const name of PROCESSING) {
|
|
40
|
+
audio[name] = false;
|
|
41
|
+
}
|
|
42
|
+
if (deviceId) {
|
|
43
|
+
audio.deviceId = { exact: deviceId };
|
|
44
|
+
}
|
|
45
|
+
const stream = await navigator.mediaDevices.getUserMedia({ audio });
|
|
46
|
+
const track = stream.getAudioTracks()[0];
|
|
47
|
+
const settings = track.getSettings();
|
|
48
|
+
const context = new AudioContext();
|
|
49
|
+
await context.audioWorklet.addModule(new URL('./mic-worklet.js', import.meta.url));
|
|
50
|
+
const capture = new AudioWorkletNode(context, 'capture');
|
|
51
|
+
capture.port.onmessage = ({ data }) => this.onBlock(data);
|
|
52
|
+
context.createMediaStreamSource(stream).connect(capture);
|
|
53
|
+
|
|
54
|
+
this.context = context;
|
|
55
|
+
this.stream = stream;
|
|
56
|
+
this.label = track.label;
|
|
57
|
+
this.rate = context.sampleRate;
|
|
58
|
+
this.processing = PROCESSING.filter((name) => settings[name] === true);
|
|
59
|
+
track.onended = () => this.close();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
onBlock(block) {
|
|
63
|
+
let power = 0;
|
|
64
|
+
for (const sample of block) {
|
|
65
|
+
power += sample * sample;
|
|
66
|
+
}
|
|
67
|
+
this.levelDb = 10 * Math.log10(power / block.length + 1e-12);
|
|
68
|
+
if (this.chunks) {
|
|
69
|
+
this.chunks.push(block);
|
|
70
|
+
this.frames += block.length;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
record() {
|
|
75
|
+
this.chunks = [];
|
|
76
|
+
this.frames = 0;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Everything heard since record(), as one Float32Array.
|
|
80
|
+
finish() {
|
|
81
|
+
const samples = new Float32Array(this.frames);
|
|
82
|
+
let offset = 0;
|
|
83
|
+
for (const chunk of this.chunks ?? []) {
|
|
84
|
+
samples.set(chunk, offset);
|
|
85
|
+
offset += chunk.length;
|
|
86
|
+
}
|
|
87
|
+
this.chunks = null;
|
|
88
|
+
this.frames = 0;
|
|
89
|
+
return samples;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Browsers may keep an audio context suspended until the user interacts with the page.
|
|
93
|
+
async resume() {
|
|
94
|
+
if (this.context?.state === 'suspended') {
|
|
95
|
+
await this.context.resume();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async close() {
|
|
100
|
+
this.stream?.getTracks().forEach((track) => track.stop());
|
|
101
|
+
await this.context?.close();
|
|
102
|
+
this.stream = null;
|
|
103
|
+
this.context = null;
|
|
104
|
+
this.levelDb = null;
|
|
105
|
+
}
|
|
106
|
+
}
|