Decoder: streaming APT.Decoder (coherent PLL or envelope demod, sync A+B tracking loop with coasting and timing-jump re-lock), telemetry analysis, Q-gated impulse (FM click) repair. GUI with fast / realtime decode, Listen, live input, telemetry table, markers. Encoder: APT.Encoder with test impairments (noise, clicks, clock offset, fading, line-timing jump); encoder dialog that feeds the decoder. Tools: offline decode/encode, quality survey, stub-DOM GUI smoke test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
272 lines
8.0 KiB
JavaScript
272 lines
8.0 KiB
JavaScript
/*
|
|
* encoder-ui.js - encoder dialog: build APT lines from a test pattern, two
|
|
* channel images or a decoded full-line APT image, preview them, add
|
|
* impairments, then download a WAV or hand the signal to the decoder
|
|
* (window.AptApp from app.js).
|
|
*/
|
|
(function () {
|
|
'use strict';
|
|
|
|
const $ = (id) => document.getElementById(id);
|
|
const W = APT.LINE_WORDS, IW = 909;
|
|
|
|
const enc = {
|
|
lines: [], /* Uint8Array(2080) per line */
|
|
imgA: null, /* { w: 909, h, data: Uint8Array } */
|
|
imgB: null,
|
|
frame: null, /* { h, data: Uint8Array(2080 * h) } */
|
|
busy: false,
|
|
};
|
|
|
|
for (const id of ['encChA', 'encChB'])
|
|
for (const c of APT.CHANNEL_NAMES) {
|
|
const o = document.createElement('option');
|
|
o.value = c;
|
|
o.textContent = `${c} (${APT.CHANNEL_DESC[c]})`;
|
|
$(id).appendChild(o);
|
|
}
|
|
$('encChA').value = '2';
|
|
$('encChB').value = '4';
|
|
|
|
const srcKind = () => document.querySelector('input[name=encSrc]:checked').value;
|
|
|
|
/* image file -> grayscale, scaled to the given width (height keeps aspect) */
|
|
async function LoadGray(file, width)
|
|
{
|
|
const bmp = await createImageBitmap(file);
|
|
const h = Math.max(1, Math.round(bmp.height * width / bmp.width));
|
|
const c = document.createElement('canvas');
|
|
c.width = width;
|
|
c.height = h;
|
|
const ctx = c.getContext('2d', { willReadFrequently: true });
|
|
ctx.imageSmoothingQuality = 'high';
|
|
ctx.drawImage(bmp, 0, 0, width, h);
|
|
const px = ctx.getImageData(0, 0, width, h).data;
|
|
const g = new Uint8Array(width * h);
|
|
for (let i = 0; i < g.length; i++)
|
|
g[i] = Math.round(0.299 * px[4 * i] + 0.587 * px[4 * i + 1] + 0.114 * px[4 * i + 2]);
|
|
return { w: width, h, data: g };
|
|
}
|
|
|
|
/* the same test pattern as tools/encode.js */
|
|
function Pattern(i, x, ch)
|
|
{
|
|
if (ch === 0)
|
|
return ((x >> 5) + (i >> 5)) & 1 ? Math.round(x / 908 * 255) : 255 - Math.round(x / 908 * 255);
|
|
return Math.round(127.5 + 127.5 * Math.sin(x / 40 + i / 25) * Math.cos(i / 60));
|
|
}
|
|
|
|
function BuildLines()
|
|
{
|
|
const opts = { chA: $('encChA').value, chB: $('encChB').value };
|
|
const out = [];
|
|
const kind = srcKind();
|
|
if (kind === 'pattern') {
|
|
const n = Math.max(8, Math.min(8000, parseInt($('encLines').value, 10) || 600));
|
|
const a = new Uint8Array(IW), b = new Uint8Array(IW);
|
|
for (let i = 0; i < n; i++) {
|
|
for (let x = 0; x < IW; x++) {
|
|
a[x] = Pattern(i, x, 0);
|
|
b[x] = Pattern(i, x, 1);
|
|
}
|
|
out.push(APT.BuildLine(i, a, b, opts));
|
|
}
|
|
} else if (kind === 'images') {
|
|
const A = enc.imgA, B = enc.imgB || enc.imgA;
|
|
if (!A)
|
|
return out;
|
|
const n = Math.max(A.h, B.h);
|
|
const inv = $('encInvB').checked;
|
|
const a = new Uint8Array(IW), b = new Uint8Array(IW);
|
|
for (let i = 0; i < n; i++) {
|
|
const ra = Math.min(i, A.h - 1), rb = Math.min(i, B.h - 1);
|
|
for (let x = 0; x < IW; x++) {
|
|
a[x] = i < A.h ? A.data[ra * IW + x] : 0;
|
|
const v = i < B.h ? B.data[rb * IW + x] : 0;
|
|
b[x] = inv ? 255 - v : v;
|
|
}
|
|
out.push(APT.BuildLine(i, a, b, opts));
|
|
}
|
|
} else if (enc.frame) {
|
|
const F = enc.frame, keep = $('encKeepTel').checked;
|
|
for (let i = 0; i < F.h; i++) {
|
|
const row = F.data.subarray(i * W, (i + 1) * W);
|
|
if (keep) {
|
|
out.push(new Uint8Array(row));
|
|
} else {
|
|
/* keep the two images, regenerate sync, space and telemetry */
|
|
out.push(APT.BuildLine(i, row.subarray(86, 995), row.subarray(1126, 2035), opts));
|
|
}
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function Preview()
|
|
{
|
|
enc.lines = BuildLines();
|
|
const n = enc.lines.length, c = $('encCanvas');
|
|
const rate = parseInt($('encRate').value, 10);
|
|
if (!n) {
|
|
c.height = 1;
|
|
$('encInfo').textContent = srcKind() === 'pattern' ? '' : 'choose an image';
|
|
return;
|
|
}
|
|
c.width = W;
|
|
c.height = n;
|
|
const ctx = c.getContext('2d');
|
|
const id = ctx.createImageData(W, n);
|
|
const d = id.data;
|
|
for (let i = 0; i < n; i++) {
|
|
const l = enc.lines[i];
|
|
for (let x = 0; x < W; x++) {
|
|
const p = (i * W + x) * 4;
|
|
d[p] = d[p + 1] = d[p + 2] = l[x];
|
|
d[p + 3] = 255;
|
|
}
|
|
}
|
|
ctx.putImageData(id, 0, 0);
|
|
const sec = n / 2;
|
|
$('encInfo').textContent = `${n} lines, ${Math.floor(sec / 60)}:${String(Math.round(sec % 60)).padStart(2, '0')} at 2 lines/s, ` +
|
|
`${(sec * rate * 2 / 1048576).toFixed(1)} MB as 16-bit WAV at ${rate} Hz`;
|
|
}
|
|
|
|
function EncOpts()
|
|
{
|
|
const num = (id, d) => {
|
|
const v = parseFloat($(id).value);
|
|
return isFinite(v) ? v : d;
|
|
};
|
|
const o = { ppm: num('encPpm', 0), fade: Math.max(0, Math.min(1, num('encFade', 0))), seed: (Math.random() * 1e9) | 0 };
|
|
if ($('encNoiseOn').checked)
|
|
o.snr = num('encSnr', 12);
|
|
if ($('encClicksOn').checked)
|
|
o.clicks = Math.max(0, num('encClicks', 0));
|
|
if ($('encJumpOn').checked)
|
|
o.jumpAt = Math.max(1, Math.round(num('encJump', 300)));
|
|
return o;
|
|
}
|
|
|
|
/* modulate in slices so the page stays responsive */
|
|
function Encode()
|
|
{
|
|
return new Promise((resolve) => {
|
|
const lines = enc.lines, n = lines.length;
|
|
const rate = parseInt($('encRate').value, 10);
|
|
const e = new APT.Encoder(rate, (i) => (i < n ? lines[i] : null), EncOpts());
|
|
const total = Math.ceil((n / 2 + 0.1) * rate);
|
|
const chunk = Math.round(rate / 4);
|
|
let out = new Float32Array(total), len = 0;
|
|
const step = () => {
|
|
const t = performance.now();
|
|
while (performance.now() - t < 40) {
|
|
const p = e.render(chunk);
|
|
if (len + p.length > out.length) {
|
|
const nb = new Float32Array(Math.max(out.length * 2, len + p.length));
|
|
nb.set(out.subarray(0, len));
|
|
out = nb;
|
|
}
|
|
out.set(p, len);
|
|
len += p.length;
|
|
if (p.length < chunk) {
|
|
$('encBarFill').style.width = '100%';
|
|
resolve({ rate, data: out.slice(0, len) });
|
|
return;
|
|
}
|
|
}
|
|
$('encBarFill').style.width = (100 * len / total).toFixed(1) + '%';
|
|
setTimeout(step, 0);
|
|
};
|
|
step();
|
|
});
|
|
}
|
|
|
|
function BaseName()
|
|
{
|
|
const k = srcKind();
|
|
const o = EncOpts();
|
|
let s = 'apt-' + (k === 'pattern' ? 'pattern' : k === 'images' ? 'images' : 'frame');
|
|
if (o.snr !== undefined)
|
|
s += `-snr${o.snr}`;
|
|
if (o.clicks)
|
|
s += `-clk${o.clicks}`;
|
|
if (o.jumpAt)
|
|
s += `-jump${o.jumpAt}`;
|
|
return s;
|
|
}
|
|
|
|
async function Run(action)
|
|
{
|
|
if (enc.busy)
|
|
return;
|
|
Preview();
|
|
if (!enc.lines.length)
|
|
return;
|
|
enc.busy = true;
|
|
for (const id of ['encDecode', 'encRealtime', 'encSave'])
|
|
$(id).disabled = true;
|
|
try {
|
|
const sig = await Encode();
|
|
const name = BaseName() + '.wav';
|
|
if (action === 'save') {
|
|
const blob = new Blob([Wav.Build(sig.data, sig.rate)], { type: 'audio/wav' });
|
|
const a = document.createElement('a');
|
|
a.href = URL.createObjectURL(blob);
|
|
a.download = name;
|
|
a.click();
|
|
setTimeout(() => URL.revokeObjectURL(a.href), 10000);
|
|
} else {
|
|
$('encDialog').close();
|
|
window.AptApp.LoadSource({ name: name + ' (encoded)', rate: sig.rate, data: sig.data });
|
|
if (action === 'realtime')
|
|
window.AptApp.StartRealtime();
|
|
else
|
|
window.AptApp.StartFast();
|
|
}
|
|
} finally {
|
|
enc.busy = false;
|
|
for (const id of ['encDecode', 'encRealtime', 'encSave'])
|
|
$(id).disabled = false;
|
|
$('encBarFill').style.width = '0';
|
|
}
|
|
}
|
|
|
|
function Pick(kind)
|
|
{
|
|
document.querySelector(`input[name=encSrc][value=${kind}]`).checked = true;
|
|
}
|
|
|
|
$('btnEncoder').onclick = () => {
|
|
$('encDialog').showModal();
|
|
Preview();
|
|
};
|
|
$('encImgA').onchange = async (e) => {
|
|
enc.imgA = e.target.files[0] ? await LoadGray(e.target.files[0], IW) : null;
|
|
Pick('images');
|
|
Preview();
|
|
};
|
|
$('encImgB').onchange = async (e) => {
|
|
enc.imgB = e.target.files[0] ? await LoadGray(e.target.files[0], IW) : null;
|
|
Pick('images');
|
|
Preview();
|
|
};
|
|
$('encFrame').onchange = async (e) => {
|
|
const f = e.target.files[0];
|
|
if (f) {
|
|
const g = await LoadGray(f, W);
|
|
enc.frame = { h: g.h, data: g.data };
|
|
} else {
|
|
enc.frame = null;
|
|
}
|
|
Pick('frame');
|
|
Preview();
|
|
};
|
|
for (const id of ['encLines', 'encChA', 'encChB', 'encRate', 'encInvB', 'encKeepTel'])
|
|
$(id).addEventListener('change', Preview);
|
|
for (const el of document.querySelectorAll('input[name=encSrc]'))
|
|
el.addEventListener('change', Preview);
|
|
$('encDecode').onclick = () => Run('decode');
|
|
$('encRealtime').onclick = () => Run('realtime');
|
|
$('encSave').onclick = () => Run('save');
|
|
})();
|