const dgram = require('dgram'); const { clamp, createFrame, fadeFrame, frameToPayload, hexToRgb, samplePalette, toIndex, } = require('./shared-frame-utils'); const DEFAULT_PORT = 4210; const DEFAULT_WIDTH = 16; const DEFAULT_HEIGHT = 16; const DEFAULT_INTERVAL_MS = 70; const PRIMARY_SPEED = 0.15; const SECONDARY_SPEED = 0.32; const WAVE_SCALE = 0.75; const paletteStops = [ { stop: 0.0, color: hexToRgb('100406') }, { stop: 0.25, color: hexToRgb('2e0f1f') }, { stop: 0.5, color: hexToRgb('6a1731') }, { stop: 0.7, color: hexToRgb('b63b32') }, { stop: 0.85, color: hexToRgb('f48b2a') }, { stop: 1.0, color: hexToRgb('ffe9b0') }, ]; const host = process.argv[2]; const port = parseInt(process.argv[3] || String(DEFAULT_PORT), 10); const width = parseInt(process.argv[4] || String(DEFAULT_WIDTH), 10); const height = parseInt(process.argv[5] || String(DEFAULT_HEIGHT), 10); const intervalMs = parseInt(process.argv[6] || String(DEFAULT_INTERVAL_MS), 10); if (!host) { console.error('Usage: node nebula-drift.js [port] [width] [height] [interval-ms]'); process.exit(1); } if (Number.isNaN(port) || Number.isNaN(width) || Number.isNaN(height) || Number.isNaN(intervalMs)) { console.error('Invalid numeric argument. Expected integers for port, width, height, and interval-ms.'); process.exit(1); } if (width <= 0 || height <= 0) { console.error('Matrix dimensions must be positive integers.'); process.exit(1); } const socket = dgram.createSocket('udp4'); const isBroadcast = host === '255.255.255.255' || host.endsWith('.255'); const frame = createFrame(width, height); let timeSeconds = 0; const frameTimeSeconds = intervalMs / 1000; if (isBroadcast) { socket.bind(() => { socket.setBroadcast(true); }); } socket.on('error', (error) => { console.error('Socket error:', error.message); }); function layeredWave(u, v, speed, offset) { return Math.sin((u * 3 + v * 2) * Math.PI * WAVE_SCALE + timeSeconds * speed + offset); } function generateFrame() { timeSeconds += frameTimeSeconds; for (let row = 0; row < height; ++row) { const v = row / Math.max(1, height - 1); for (let col = 0; col < width; ++col) { const u = col / Math.max(1, width - 1); const primary = layeredWave(u, v, PRIMARY_SPEED, 0); const secondary = layeredWave(v, u, SECONDARY_SPEED, Math.PI / 4); const tertiary = Math.sin((u + v) * Math.PI * 1.5 + timeSeconds * 0.18); const combined = 0.45 * primary + 0.35 * secondary + 0.2 * tertiary; const envelope = Math.sin((u * v) * Math.PI * 2 + timeSeconds * 0.1) * 0.25 + 0.75; const value = clamp((combined * 0.5 + 0.5) * envelope, 0, 1); frame[toIndex(col, row, width)] = samplePalette(paletteStops, value); } } return frameToPayload(frame); } function sendFrame() { const payload = generateFrame(); const message = Buffer.from(payload, 'utf8'); socket.send(message, port, host); } setInterval(sendFrame, intervalMs); console.log( `Streaming nebula drift to ${host}:${port} (${width}x${height}, interval=${intervalMs}ms)` );