const qs = (id) => document.getElementById(id); const navCanvas = qs('navCanvas'); const spectrumCanvas = qs('spectrum'); const waterfallCanvas = qs('waterfall'); const occupancyCanvas = qs('occupancy'); const timelineCanvas = qs('timeline'); const detailSpectrogram = qs('detailSpectrogram'); const wsBadge = qs('wsBadge'); const metaLine = qs('metaLine'); const heroSubtitle = qs('heroSubtitle'); const configStatusEl = qs('configStatus'); const timelineRangeEl = qs('timelineRange'); const metricCenter = qs('metricCenter'); const metricSpan = qs('metricSpan'); const metricRes = qs('metricRes'); const metricSignals = qs('metricSignals'); const metricGpu = qs('metricGpu'); const metricSource = qs('metricSource'); const centerInput = qs('centerInput'); const spanInput = qs('spanInput'); const sampleRateSelect = qs('sampleRateSelect'); const bwSelect = qs('bwSelect'); const fftSelect = qs('fftSelect'); const gainRange = qs('gainRange'); const gainInput = qs('gainInput'); const thresholdRange = qs('thresholdRange'); const thresholdInput = qs('thresholdInput'); const cfarToggle = qs('cfarToggle'); const cfarGuardInput = qs('cfarGuardInput'); const cfarTrainInput = qs('cfarTrainInput'); const cfarRankInput = qs('cfarRankInput'); const cfarScaleInput = qs('cfarScaleInput'); const minDurationInput = qs('minDurationInput'); const holdInput = qs('holdInput'); const emaAlphaInput = qs('emaAlphaInput'); const hysteresisInput = qs('hysteresisInput'); const stableFramesInput = qs('stableFramesInput'); const gapToleranceInput = qs('gapToleranceInput'); const agcToggle = qs('agcToggle'); const dcToggle = qs('dcToggle'); const iqToggle = qs('iqToggle'); const avgSelect = qs('avgSelect'); const maxHoldToggle = qs('maxHoldToggle'); const gpuToggle = qs('gpuToggle'); const recEnableToggle = qs('recEnableToggle'); const recIQToggle = qs('recIQToggle'); const recAudioToggle = qs('recAudioToggle'); const recDemodToggle = qs('recDemodToggle'); const recDecodeToggle = qs('recDecodeToggle'); const recMinSNR = qs('recMinSNR'); const recMaxDisk = qs('recMaxDisk'); const recClassFilter = qs('recClassFilter'); const signalList = qs('signalList'); const eventList = qs('eventList'); const recordingList = qs('recordingList'); const signalCountBadge = qs('signalCountBadge'); const eventCountBadge = qs('eventCountBadge'); const recordingCountBadge = qs('recordingCountBadge'); const healthBuffer = qs('healthBuffer'); const healthDropped = qs('healthDropped'); const healthResets = qs('healthResets'); const healthAge = qs('healthAge'); const healthGpu = qs('healthGpu'); const healthFps = qs('healthFps'); const drawerEl = qs('eventDrawer'); const drawerCloseBtn = qs('drawerClose'); const detailSubtitle = qs('detailSubtitle'); const detailCenterEl = qs('detailCenter'); const detailBwEl = qs('detailBw'); const detailStartEl = qs('detailStart'); const detailEndEl = qs('detailEnd'); const detailSnrEl = qs('detailSnr'); const detailDurEl = qs('detailDur'); const detailClassEl = qs('detailClass'); const jumpToEventBtn = qs('jumpToEventBtn'); const exportEventBtn = qs('exportEventBtn'); const liveListenEventBtn = qs('liveListenEventBtn'); const decodeEventBtn = qs('decodeEventBtn'); const decodeModeSelect = qs('decodeMode'); const recordingMetaEl = qs('recordingMeta'); const decodeResultEl = qs('decodeResult'); const recordingMetaLink = qs('recordingMetaLink'); const recordingIQLink = qs('recordingIQLink'); const recordingAudioLink = qs('recordingAudioLink'); const followBtn = qs('followBtn'); const fitBtn = qs('fitBtn'); const resetMaxBtn = qs('resetMaxBtn'); const timelineFollowBtn = qs('timelineFollowBtn'); const timelineFreezeBtn = qs('timelineFreezeBtn'); const modeButtons = Array.from(document.querySelectorAll('.mode-btn')); const railTabs = Array.from(document.querySelectorAll('.rail-tab')); const tabPanels = Array.from(document.querySelectorAll('.tab-panel')); const presetButtons = Array.from(document.querySelectorAll('.preset-btn')); const liveListenBtn = qs('liveListenBtn'); const listenSecondsInput = qs('listenSeconds'); const listenModeSelect = qs('listenMode'); let latest = null; let currentConfig = null; let liveAudio = null; let stats = { buffer_samples: 0, dropped: 0, resets: 0, last_sample_ago_ms: -1 }; let gpuInfo = { available: false, active: false, error: '' }; let zoom = 1; let pan = 0; let followLive = true; let maxHold = false; let avgAlpha = 0; let avgSpectrum = null; let maxSpectrum = null; let lastFFTSize = null; let processedSpectrum = null; let processedSpectrumSource = null; let processingDirty = true; let pendingConfigUpdate = null; let pendingSettingsUpdate = null; let configTimer = null; let settingsTimer = null; let isSyncingConfig = false; let isDraggingSpectrum = false; let dragStartX = 0; let dragStartPan = 0; let navDrag = false; let timelineFrozen = false; let renderFrames = 0; let renderFps = 0; let lastFpsTs = performance.now(); let wsReconnectTimer = null; let eventsFetchInFlight = false; const events = []; const eventsById = new Map(); let lastEventEndMs = 0; let selectedEventId = null; let timelineRects = []; let liveSignalRects = []; let recordings = []; let recordingsFetchInFlight = false; const GAIN_MAX = 60; const timelineWindowMs = 5 * 60 * 1000; function setConfigStatus(text) { configStatusEl.textContent = text; } function setWsBadge(text, kind = 'neutral') { wsBadge.textContent = text; wsBadge.style.borderColor = kind === 'ok' ? 'rgba(124, 251, 131, 0.35)' : kind === 'bad' ? 'rgba(255, 107, 129, 0.35)' : 'rgba(112, 150, 207, 0.18)'; } function toMHz(hz) { return hz / 1e6; } function fromMHz(mhz) { return mhz * 1e6; } function fmtMHz(hz, digits = 3) { return `${(hz / 1e6).toFixed(digits)} MHz`; } function fmtKHz(hz, digits = 2) { return `${(hz / 1e3).toFixed(digits)} kHz`; } function fmtHz(hz) { if (hz >= 1e6) return `${(hz / 1e6).toFixed(3)} MHz`; if (hz >= 1e3) return `${(hz / 1e3).toFixed(2)} kHz`; return `${hz.toFixed(0)} Hz`; } function fmtMs(ms) { if (ms < 1000) return `${Math.max(0, Math.round(ms))} ms`; return `${(ms / 1000).toFixed(2)} s`; } function colorMap(v) { const x = Math.max(0, Math.min(1, v)); const r = Math.floor(255 * Math.pow(x, 0.55)); const g = Math.floor(255 * Math.pow(x, 1.08)); const b = Math.floor(220 * Math.pow(1 - x, 1.15)); return [r, g, b]; } function snrColor(snr) { const norm = Math.max(0, Math.min(1, (snr + 5) / 35)); const [r, g, b] = colorMap(norm); return `rgb(${r}, ${g}, ${b})`; } function binForFreq(freq, centerHz, sampleRate, n) { return Math.floor((freq - (centerHz - sampleRate / 2)) / (sampleRate / n)); } function maxInBinRange(spectrum, b0, b1) { const n = spectrum.length; let start = Math.max(0, Math.min(n - 1, b0)); let end = Math.max(0, Math.min(n - 1, b1)); if (end < start) [start, end] = [end, start]; let max = -1e9; for (let i = start; i <= end; i++) { if (spectrum[i] > max) max = spectrum[i]; } return max; } function markSpectrumDirty() { processingDirty = true; } function processSpectrum(spectrum) { if (!spectrum) return spectrum; let base = spectrum; if (avgAlpha > 0) { if (!avgSpectrum || avgSpectrum.length !== spectrum.length) { avgSpectrum = spectrum.slice(); } else { for (let i = 0; i < spectrum.length; i++) { avgSpectrum[i] = avgAlpha * spectrum[i] + (1 - avgAlpha) * avgSpectrum[i]; } } base = avgSpectrum; } if (maxHold) { if (!maxSpectrum || maxSpectrum.length !== base.length) { maxSpectrum = base.slice(); } else { for (let i = 0; i < base.length; i++) { if (base[i] > maxSpectrum[i]) maxSpectrum[i] = base[i]; } } base = maxSpectrum; } return base; } function resetProcessingCaches() { avgSpectrum = null; maxSpectrum = null; processedSpectrum = null; processedSpectrumSource = null; processingDirty = true; } function getProcessedSpectrum() { if (!latest?.spectrum_db) return null; if (!processingDirty && processedSpectrumSource === latest.spectrum_db) return processedSpectrum; processedSpectrum = processSpectrum(latest.spectrum_db); processedSpectrumSource = latest.spectrum_db; processingDirty = false; return processedSpectrum; } function resizeCanvas(canvas) { if (!canvas) return; const rect = canvas.getBoundingClientRect(); const dpr = window.devicePixelRatio || 1; const width = Math.max(1, Math.floor(rect.width * dpr)); const height = Math.max(1, Math.floor(rect.height * dpr)); if (canvas.width !== width || canvas.height !== height) { canvas.width = width; canvas.height = height; } } function resizeAll() { [navCanvas, spectrumCanvas, waterfallCanvas, occupancyCanvas, timelineCanvas, detailSpectrogram].forEach(resizeCanvas); } window.addEventListener('resize', resizeAll); resizeAll(); function setSelectValueOrNearest(selectEl, numericValue) { if (!selectEl) return; const options = Array.from(selectEl.options || []); const exact = options.find(o => Number.parseFloat(o.value) === numericValue); if (exact) { selectEl.value = exact.value; return; } let best = options[0]; let bestDist = Infinity; for (const opt of options) { const dist = Math.abs(Number.parseFloat(opt.value) - numericValue); if (dist < bestDist) { best = opt; bestDist = dist; } } if (best) selectEl.value = best.value; } function applyConfigToUI(cfg) { if (!cfg) return; isSyncingConfig = true; centerInput.value = toMHz(cfg.center_hz).toFixed(6); setSelectValueOrNearest(sampleRateSelect, cfg.sample_rate / 1e6); setSelectValueOrNearest(bwSelect, cfg.tuner_bw_khz || 1536); setSelectValueOrNearest(fftSelect, cfg.fft_size); if (lastFFTSize !== cfg.fft_size) { resetProcessingCaches(); lastFFTSize = cfg.fft_size; } const uiGain = Math.max(0, Math.min(GAIN_MAX, GAIN_MAX - cfg.gain_db)); gainRange.value = uiGain; gainInput.value = uiGain; thresholdRange.value = cfg.detector.threshold_db; thresholdInput.value = cfg.detector.threshold_db; if (cfarToggle) cfarToggle.checked = !!cfg.detector.cfar_enabled; if (cfarGuardInput) cfarGuardInput.value = cfg.detector.cfar_guard_cells ?? 2; if (cfarTrainInput) cfarTrainInput.value = cfg.detector.cfar_train_cells ?? 16; if (cfarRankInput) cfarRankInput.value = cfg.detector.cfar_rank ?? 24; if (cfarScaleInput) cfarScaleInput.value = cfg.detector.cfar_scale_db ?? 6; if (minDurationInput) minDurationInput.value = cfg.detector.min_duration_ms; if (holdInput) holdInput.value = cfg.detector.hold_ms; if (emaAlphaInput) emaAlphaInput.value = cfg.detector.ema_alpha ?? 0.2; if (hysteresisInput) hysteresisInput.value = cfg.detector.hysteresis_db ?? 3; if (stableFramesInput) stableFramesInput.value = cfg.detector.min_stable_frames ?? 3; if (gapToleranceInput) gapToleranceInput.value = cfg.detector.gap_tolerance_ms ?? cfg.detector.hold_ms; agcToggle.checked = !!cfg.agc; dcToggle.checked = !!cfg.dc_block; iqToggle.checked = !!cfg.iq_balance; gpuToggle.checked = !!cfg.use_gpu_fft; maxHoldToggle.checked = maxHold; if (cfg.recorder) { if (recEnableToggle) recEnableToggle.checked = !!cfg.recorder.enabled; if (recIQToggle) recIQToggle.checked = !!cfg.recorder.record_iq; if (recAudioToggle) recAudioToggle.checked = !!cfg.recorder.record_audio; if (recDemodToggle) recDemodToggle.checked = !!cfg.recorder.auto_demod; if (recDecodeToggle) recDecodeToggle.checked = !!cfg.recorder.auto_decode; if (recMinSNR) recMinSNR.value = cfg.recorder.min_snr_db ?? 10; if (recMaxDisk) recMaxDisk.value = cfg.recorder.max_disk_mb ?? 0; if (recClassFilter) recClassFilter.value = (cfg.recorder.class_filter || []).join(', '); } spanInput.value = (cfg.sample_rate / zoom / 1e6).toFixed(3); isSyncingConfig = false; } async function loadConfig() { try { const res = await fetch('/api/config'); if (!res.ok) throw new Error('config'); currentConfig = await res.json(); applyConfigToUI(currentConfig); setConfigStatus('Config synced'); } catch { setConfigStatus('Config offline'); } } async function loadSignals() { try { const res = await fetch('/api/signals'); if (!res.ok) return; const sigs = await res.json(); if (Array.isArray(sigs)) { latest = latest || {}; latest.signals = sigs; renderLists(); } } catch {} } async function loadDecoders() { if (!decodeModeSelect) return; try { const res = await fetch('/api/decoders'); if (!res.ok) return; const list = await res.json(); if (!Array.isArray(list)) return; const current = decodeModeSelect.value; decodeModeSelect.innerHTML = ''; list.forEach((mode) => { const opt = document.createElement('option'); opt.value = mode; opt.textContent = mode; decodeModeSelect.appendChild(opt); }); if (current) decodeModeSelect.value = current; } catch {} } async function loadStats() { try { const res = await fetch('/api/stats'); if (!res.ok) return; stats = await res.json(); } catch {} } async function loadGPU() { try { const res = await fetch('/api/gpu'); if (!res.ok) return; gpuInfo = await res.json(); } catch {} } function queueConfigUpdate(partial) { if (isSyncingConfig) return; pendingConfigUpdate = { ...(pendingConfigUpdate || {}), ...partial }; setConfigStatus('Applying…'); clearTimeout(configTimer); configTimer = setTimeout(sendConfigUpdate, 180); } function queueSettingsUpdate(partial) { if (isSyncingConfig) return; pendingSettingsUpdate = { ...(pendingSettingsUpdate || {}), ...partial }; setConfigStatus('Applying…'); clearTimeout(settingsTimer); settingsTimer = setTimeout(sendSettingsUpdate, 120); } async function sendConfigUpdate() { if (!pendingConfigUpdate) return; const payload = pendingConfigUpdate; pendingConfigUpdate = null; try { const res = await fetch('/api/config', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload), }); if (!res.ok) throw new Error('apply'); currentConfig = await res.json(); applyConfigToUI(currentConfig); setConfigStatus('Config applied'); } catch { setConfigStatus('Config apply failed'); } } async function sendSettingsUpdate() { if (!pendingSettingsUpdate) return; const payload = pendingSettingsUpdate; pendingSettingsUpdate = null; try { const res = await fetch('/api/sdr/settings', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload), }); if (!res.ok) throw new Error('apply'); currentConfig = await res.json(); applyConfigToUI(currentConfig); setConfigStatus('Settings applied'); } catch { setConfigStatus('Settings apply failed'); } } function updateHeroMetrics() { if (!latest) return; const span = latest.sample_rate / zoom; const binHz = latest.sample_rate / Math.max(1, latest.spectrum_db?.length || latest.fft_size || 1); metricCenter.textContent = fmtMHz(latest.center_hz, 6); metricSpan.textContent = fmtHz(span); metricRes.textContent = `${binHz.toFixed(1)} Hz/bin`; metricSignals.textContent = String(latest.signals?.length || 0); metricGpu.textContent = gpuInfo.active ? 'ON' : (gpuInfo.available ? 'OFF' : 'N/A'); metricSource.textContent = stats.last_sample_ago_ms >= 0 ? `${stats.last_sample_ago_ms} ms` : 'n/a'; const gpuText = gpuInfo.active ? 'GPU active' : (gpuInfo.available ? 'GPU ready' : 'GPU n/a'); metaLine.textContent = `${fmtMHz(latest.center_hz, 3)} · ${fmtHz(span)} span · ${gpuText}`; heroSubtitle.textContent = `${latest.signals?.length || 0} live signals · ${events.length} recent events tracked`; healthBuffer.textContent = String(stats.buffer_samples ?? '-'); healthDropped.textContent = String(stats.dropped ?? '-'); healthResets.textContent = String(stats.resets ?? '-'); healthAge.textContent = stats.last_sample_ago_ms >= 0 ? `${stats.last_sample_ago_ms} ms` : 'n/a'; healthGpu.textContent = gpuInfo.error ? `${gpuInfo.active ? 'ON' : 'OFF'} · ${gpuInfo.error}` : (gpuInfo.active ? 'ON' : (gpuInfo.available ? 'Ready' : 'N/A')); healthFps.textContent = `${renderFps.toFixed(0)} fps`; } function renderBandNavigator() { if (!latest) return; const ctx = navCanvas.getContext('2d'); const w = navCanvas.width; const h = navCanvas.height; ctx.clearRect(0, 0, w, h); const display = getProcessedSpectrum(); if (!display) return; const minDb = -120; const maxDb = 0; ctx.fillStyle = '#071018'; ctx.fillRect(0, 0, w, h); ctx.strokeStyle = 'rgba(102, 169, 255, 0.25)'; ctx.lineWidth = 1; ctx.beginPath(); for (let x = 0; x < w; x++) { const idx = Math.min(display.length - 1, Math.floor((x / w) * display.length)); const v = display[idx]; const y = h - ((v - minDb) / (maxDb - minDb)) * (h - 10) - 5; if (x === 0) ctx.moveTo(x, y); else ctx.lineTo(x, y); } ctx.stroke(); const span = latest.sample_rate / zoom; const fullStart = latest.center_hz - latest.sample_rate / 2; const viewStart = latest.center_hz - span / 2 + pan * span; const viewEnd = latest.center_hz + span / 2 + pan * span; const x1 = ((viewStart - fullStart) / latest.sample_rate) * w; const x2 = ((viewEnd - fullStart) / latest.sample_rate) * w; ctx.fillStyle = 'rgba(102, 240, 209, 0.10)'; ctx.strokeStyle = 'rgba(102, 240, 209, 0.85)'; ctx.lineWidth = 2; ctx.fillRect(x1, 4, Math.max(2, x2 - x1), h - 8); ctx.strokeRect(x1, 4, Math.max(2, x2 - x1), h - 8); } function drawSpectrumGrid(ctx, w, h, startHz, endHz) { ctx.strokeStyle = 'rgba(86, 109, 148, 0.18)'; ctx.lineWidth = 1; for (let i = 1; i < 6; i++) { const y = (h / 6) * i; ctx.beginPath(); ctx.moveTo(0, y); ctx.lineTo(w, y); ctx.stroke(); } for (let i = 1; i < 8; i++) { const x = (w / 8) * i; ctx.beginPath(); ctx.moveTo(x, 0); ctx.lineTo(x, h); ctx.stroke(); const hz = startHz + (i / 8) * (endHz - startHz); ctx.fillStyle = 'rgba(173, 192, 220, 0.72)'; ctx.font = `${Math.max(11, Math.floor(h / 26))}px Inter, sans-serif`; ctx.fillText((hz / 1e6).toFixed(3), x + 4, h - 8); } } function drawCfarEdgeOverlay(ctx, w, h, startHz, endHz) { if (!latest || !currentConfig?.detector?.cfar_enabled) return; const guard = currentConfig.detector.cfar_guard_cells ?? 0; const train = currentConfig.detector.cfar_train_cells ?? 0; const bins = guard + train; if (bins <= 0) return; const fftSize = latest.fft_size || latest.spectrum_db?.length; if (!fftSize || fftSize <= 0) return; const binHz = latest.sample_rate / fftSize; const edgeHz = bins * binHz; const bandStart = latest.center_hz - latest.sample_rate / 2; const bandEnd = latest.center_hz + latest.sample_rate / 2; const leftEdgeEnd = bandStart + edgeHz; const rightEdgeStart = bandEnd - edgeHz; ctx.fillStyle = 'rgba(255, 204, 102, 0.08)'; ctx.strokeStyle = 'rgba(255, 204, 102, 0.18)'; ctx.lineWidth = 1; const leftStart = Math.max(startHz, bandStart); const leftEnd = Math.min(endHz, leftEdgeEnd); if (leftEnd > leftStart) { const x1 = ((leftStart - startHz) / (endHz - startHz)) * w; const x2 = ((leftEnd - startHz) / (endHz - startHz)) * w; ctx.fillRect(x1, 0, Math.max(2, x2 - x1), h); ctx.strokeRect(x1, 0, Math.max(2, x2 - x1), h); } const rightStart = Math.max(startHz, rightEdgeStart); const rightEnd = Math.min(endHz, bandEnd); if (rightEnd > rightStart) { const x1 = ((rightStart - startHz) / (endHz - startHz)) * w; const x2 = ((rightEnd - startHz) / (endHz - startHz)) * w; ctx.fillRect(x1, 0, Math.max(2, x2 - x1), h); ctx.strokeRect(x1, 0, Math.max(2, x2 - x1), h); } } function renderSpectrum() { if (!latest) return; const ctx = spectrumCanvas.getContext('2d'); const w = spectrumCanvas.width; const h = spectrumCanvas.height; ctx.clearRect(0, 0, w, h); const display = getProcessedSpectrum(); if (!display) return; const n = display.length; const span = latest.sample_rate / zoom; const startHz = latest.center_hz - span / 2 + pan * span; const endHz = latest.center_hz + span / 2 + pan * span; spanInput.value = (span / 1e6).toFixed(3); drawSpectrumGrid(ctx, w, h, startHz, endHz); drawCfarEdgeOverlay(ctx, w, h, startHz, endHz); const minDb = -120; const maxDb = 0; const fill = ctx.createLinearGradient(0, 0, 0, h); fill.addColorStop(0, 'rgba(102, 240, 209, 0.20)'); fill.addColorStop(1, 'rgba(102, 240, 209, 0.02)'); ctx.beginPath(); for (let x = 0; x < w; x++) { const f1 = startHz + (x / w) * (endHz - startHz); const f2 = startHz + ((x + 1) / w) * (endHz - startHz); const b0 = binForFreq(f1, latest.center_hz, latest.sample_rate, n); const b1 = binForFreq(f2, latest.center_hz, latest.sample_rate, n); const v = maxInBinRange(display, b0, b1); const y = h - ((v - minDb) / (maxDb - minDb)) * (h - 18) - 6; if (x === 0) ctx.moveTo(x, y); else ctx.lineTo(x, y); } ctx.lineTo(w, h); ctx.lineTo(0, h); ctx.closePath(); ctx.fillStyle = fill; ctx.fill(); ctx.strokeStyle = '#66f0d1'; ctx.lineWidth = 2; ctx.beginPath(); liveSignalRects = []; for (let x = 0; x < w; x++) { const f1 = startHz + (x / w) * (endHz - startHz); const f2 = startHz + ((x + 1) / w) * (endHz - startHz); const b0 = binForFreq(f1, latest.center_hz, latest.sample_rate, n); const b1 = binForFreq(f2, latest.center_hz, latest.sample_rate, n); const v = maxInBinRange(display, b0, b1); const y = h - ((v - minDb) / (maxDb - minDb)) * (h - 18) - 6; if (x === 0) ctx.moveTo(x, y); else ctx.lineTo(x, y); } ctx.stroke(); if (Array.isArray(latest.signals)) { latest.signals.forEach((s, index) => { const left = s.center_hz - s.bw_hz / 2; const right = s.center_hz + s.bw_hz / 2; if (right < startHz || left > endHz) return; const x1 = ((left - startHz) / (endHz - startHz)) * w; const x2 = ((right - startHz) / (endHz - startHz)) * w; const boxW = Math.max(2, x2 - x1); const color = snrColor(s.snr_db || 0); ctx.fillStyle = color.replace('rgb', 'rgba').replace(')', ', 0.14)'); ctx.strokeStyle = color; ctx.lineWidth = 1.5; ctx.fillRect(x1, 10, boxW, h - 28); ctx.strokeRect(x1, 10, boxW, h - 28); ctx.fillStyle = color; ctx.font = '12px Inter, sans-serif'; const label = `${(s.center_hz / 1e6).toFixed(4)} MHz`; ctx.fillText(label, Math.max(4, x1 + 4), 24 + (index % 3) * 16); liveSignalRects.push({ x: x1, y: 10, w: boxW, h: h - 28, signal: s, }); }); } } function renderWaterfall() { if (!latest) return; const ctx = waterfallCanvas.getContext('2d'); const w = waterfallCanvas.width; const h = waterfallCanvas.height; const prev = ctx.getImageData(0, 0, w, h - 1); ctx.putImageData(prev, 0, 1); const display = getProcessedSpectrum(); if (!display) return; const n = display.length; const span = latest.sample_rate / zoom; const startHz = latest.center_hz - span / 2 + pan * span; const endHz = latest.center_hz + span / 2 + pan * span; const minDb = -120; const maxDb = 0; const row = ctx.createImageData(w, 1); for (let x = 0; x < w; x++) { const f1 = startHz + (x / w) * (endHz - startHz); const f2 = startHz + ((x + 1) / w) * (endHz - startHz); const b0 = binForFreq(f1, latest.center_hz, latest.sample_rate, n); const b1 = binForFreq(f2, latest.center_hz, latest.sample_rate, n); const v = maxInBinRange(display, b0, b1); const norm = Math.max(0, Math.min(1, (v - minDb) / (maxDb - minDb))); const [r, g, b] = colorMap(norm); row.data[x * 4] = r; row.data[x * 4 + 1] = g; row.data[x * 4 + 2] = b; row.data[x * 4 + 3] = 255; } ctx.putImageData(row, 0, 0); } function renderOccupancy() { const ctx = occupancyCanvas.getContext('2d'); const w = occupancyCanvas.width; const h = occupancyCanvas.height; ctx.clearRect(0, 0, w, h); ctx.fillStyle = '#071018'; ctx.fillRect(0, 0, w, h); if (!latest || events.length === 0) return; const bins = new Array(Math.max(32, Math.min(160, Math.floor(w / 8)))).fill(0); const bandStart = latest.center_hz - latest.sample_rate / 2; const bandEnd = latest.center_hz + latest.sample_rate / 2; const now = Date.now(); const windowStart = now - timelineWindowMs; for (const ev of events) { if (ev.end_ms < windowStart || ev.start_ms > now) continue; const left = ev.center_hz - ev.bandwidth_hz / 2; const right = ev.center_hz + ev.bandwidth_hz / 2; const normL = Math.max(0, Math.min(1, (left - bandStart) / (bandEnd - bandStart))); const normR = Math.max(0, Math.min(1, (right - bandStart) / (bandEnd - bandStart))); let b0 = Math.floor(normL * bins.length); let b1 = Math.floor(normR * bins.length); if (b1 < b0) [b0, b1] = [b1, b0]; for (let i = Math.max(0, b0); i <= Math.min(bins.length - 1, b1); i++) { bins[i] += Math.max(0.3, (ev.snr_db || 0) / 12 + 1); } } const maxBin = Math.max(1, ...bins); bins.forEach((v, i) => { const norm = v / maxBin; const [r, g, b] = colorMap(norm); ctx.fillStyle = `rgb(${r}, ${g}, ${b})`; const x = (i / bins.length) * w; const bw = Math.ceil(w / bins.length) + 1; ctx.fillRect(x, 0, bw, h); }); } function renderTimeline() { const ctx = timelineCanvas.getContext('2d'); const w = timelineCanvas.width; const h = timelineCanvas.height; ctx.clearRect(0, 0, w, h); ctx.fillStyle = '#071018'; ctx.fillRect(0, 0, w, h); if (events.length === 0) { timelineRangeEl.textContent = 'No events yet'; return; } const endMs = Date.now(); const startMs = endMs - timelineWindowMs; timelineRangeEl.textContent = `${new Date(startMs).toLocaleTimeString()} - ${new Date(endMs).toLocaleTimeString()}`; let minHz = Infinity; let maxHz = -Infinity; if (latest) { minHz = latest.center_hz - latest.sample_rate / 2; maxHz = latest.center_hz + latest.sample_rate / 2; } else { for (const ev of events) { minHz = Math.min(minHz, ev.center_hz - ev.bandwidth_hz / 2); maxHz = Math.max(maxHz, ev.center_hz + ev.bandwidth_hz / 2); } } if (!isFinite(minHz) || !isFinite(maxHz) || minHz === maxHz) { minHz = 0; maxHz = 1; } ctx.strokeStyle = 'rgba(86, 109, 148, 0.18)'; ctx.lineWidth = 1; for (let i = 1; i < 6; i++) { const y = (h / 6) * i; ctx.beginPath(); ctx.moveTo(0, y); ctx.lineTo(w, y); ctx.stroke(); } for (let i = 1; i < 8; i++) { const x = (w / 8) * i; ctx.beginPath(); ctx.moveTo(x, 0); ctx.lineTo(x, h); ctx.stroke(); } timelineRects = []; for (const ev of events) { if (ev.end_ms < startMs || ev.start_ms > endMs) continue; const x1 = ((Math.max(ev.start_ms, startMs) - startMs) / (endMs - startMs)) * w; const x2 = ((Math.min(ev.end_ms, endMs) - startMs) / (endMs - startMs)) * w; const topHz = ev.center_hz + ev.bandwidth_hz / 2; const bottomHz = ev.center_hz - ev.bandwidth_hz / 2; const y1 = ((maxHz - topHz) / (maxHz - minHz)) * h; const y2 = ((maxHz - bottomHz) / (maxHz - minHz)) * h; const rect = { x: x1, y: y1, w: Math.max(2, x2 - x1), h: Math.max(3, y2 - y1), id: ev.id }; timelineRects.push(rect); ctx.fillStyle = snrColor(ev.snr_db || 0).replace('rgb', 'rgba').replace(')', ', 0.85)'); ctx.fillRect(rect.x, rect.y, rect.w, rect.h); } if (selectedEventId) { const hit = timelineRects.find(r => r.id === selectedEventId); if (hit) { ctx.strokeStyle = '#ffffff'; ctx.lineWidth = 2; ctx.strokeRect(hit.x - 1, hit.y - 1, hit.w + 2, hit.h + 2); } } } function renderDetailSpectrogram() { const ev = eventsById.get(selectedEventId); const ctx = detailSpectrogram.getContext('2d'); const w = detailSpectrogram.width; const h = detailSpectrogram.height; ctx.clearRect(0, 0, w, h); ctx.fillStyle = '#071018'; ctx.fillRect(0, 0, w, h); if (!latest || !ev) return; const display = getProcessedSpectrum(); if (!display) return; const n = display.length; const localSpan = Math.min(latest.sample_rate, Math.max(ev.bandwidth_hz * 4, latest.sample_rate / 10)); const startHz = ev.center_hz - localSpan / 2; const endHz = ev.center_hz + localSpan / 2; const minDb = -120; const maxDb = 0; const row = ctx.createImageData(w, 1); for (let x = 0; x < w; x++) { const f1 = startHz + (x / w) * (endHz - startHz); const f2 = startHz + ((x + 1) / w) * (endHz - startHz); const b0 = binForFreq(f1, latest.center_hz, latest.sample_rate, n); const b1 = binForFreq(f2, latest.center_hz, latest.sample_rate, n); const v = maxInBinRange(display, b0, b1); const norm = Math.max(0, Math.min(1, (v - minDb) / (maxDb - minDb))); const [r, g, b] = colorMap(norm); row.data[x * 4] = r; row.data[x * 4 + 1] = g; row.data[x * 4 + 2] = b; row.data[x * 4 + 3] = 255; } for (let y = 0; y < h; y++) ctx.putImageData(row, 0, y); const centerX = w / 2; ctx.strokeStyle = 'rgba(255,255,255,0.65)'; ctx.lineWidth = 1; ctx.beginPath(); ctx.moveTo(centerX, 0); ctx.lineTo(centerX, h); ctx.stroke(); } function renderLists() { const signals = Array.isArray(latest?.signals) ? [...latest.signals] : []; signals.sort((a, b) => (b.snr_db || 0) - (a.snr_db || 0)); signalCountBadge.textContent = `${signals.length} live`; metricSignals.textContent = String(signals.length); if (signals.length === 0) { signalList.innerHTML = '