You can edit the below JavaScript code to customize the image tool.
Apply Changes
function processImage(originalImg, durationSec = "5", minFreq = "300", maxFreq = "3400", resolution = "128") {
// Parse and constrain parameters
const duration = Math.max(0.1, Number(durationSec) || 5);
// Default bounds fit a classic telephone bandwidth (300Hz - 3400Hz)
const minF = Math.max(1, Number(minFreq) || 300);
const maxF = Math.min(22050, Number(maxFreq) || 3400);
const H = Math.max(8, Math.min(512, Number(resolution) || 128));
// Calculate relative width to preserve image aspect ratio
let W = Math.floor(originalImg.width * (H / originalImg.height));
if (W > 1000) W = 1000;
if (W < 1) W = 1;
// Scale down the initial image to get visual density
const canvas = document.createElement('canvas');
canvas.width = W;
canvas.height = H;
const ctx = canvas.getContext('2d');
ctx.drawImage(originalImg, 0, 0, W, H);
const imgData = ctx.getImageData(0, 0, W, H).data;
// Pre-calculate visual brightness (amplitude maps for oscillators)
const bright = new Float32Array(W * H);
for (let x = 0; x < W; x++) {
for (let y = 0; y < H; y++) {
const idx = (y * W + x) * 4;
const r = imgData[idx];
const g = imgData[idx+1];
const b = imgData[idx+2];
const a = imgData[idx+3] / 255;
// Calculate perceptive luminance map (normalized 0.0 -> 1.0)
bright[x * H + y] = ((0.299 * r + 0.587 * g + 0.114 * b) / 255) * a;
}
}
// Audio rendering configuration
const sampleRate = 44100;
const samplesTotal = Math.floor(duration * sampleRate);
const samples = new Float32Array(samplesTotal);
const freqs = new Float64Array(H);
const phaseIncs = new Float64Array(H);
const phases = new Float64Array(H);
const twoPi = 2 * Math.PI;
// Assign linear frequencies (y=0 corresponds to top of image mapping to max frequency)
for (let y = 0; y < H; y++) {
freqs[y] = minF + ((H - 1 - y) / (H - 1)) * (maxF - minF);
phaseIncs[y] = (twoPi * freqs[y]) / sampleRate;
}
const samplesPerColumn = samplesTotal / W;
let maxMag = 0.0001;
// Additive oscillator spectral synthesis directly to buffer
for (let i = 0; i < samplesTotal; i++) {
const c_float = i / samplesPerColumn;
const c = Math.floor(c_float);
// Bounds for interpolation
const c_next = Math.min(W - 1, c + 1);
const frac = c_float - c;
let val = 0;
const offset1 = c * H;
const offset2 = c_next * H;
for (let r = 0; r < H; r++) {
// Step phase continuously ensuring no audio cuts or clicks
phases[r] += phaseIncs[r];
if (phases[r] >= twoPi) phases[r] -= twoPi;
// Interpolate amplitude between columns for smooth synthesis
const b0 = bright[offset1 + r];
const b1 = bright[offset2 + r];
const amplitude = b0 + frac * (b1 - b0);
// Skip dead silence for micro-optimization
if (amplitude > 0.005) {
val += amplitude * Math.sin(phases[r]);
}
}
samples[i] = val;
const absVal = Math.abs(val);
if (absVal > maxMag) maxMag = absVal;
}
// Compose classic uncompressed RIFF/WAV Buffer headers
const buffer = new ArrayBuffer(44 + samplesTotal * 2);
const view = new DataView(buffer);
const writeString = (v, off, str) => {
for (let i = 0; i < str.length; i++) {
v.setUint8(off + i, str.charCodeAt(i));
}
};
writeString(view, 0, 'RIFF');
view.setUint32(4, 36 + samplesTotal * 2, true);
writeString(view, 8, 'WAVE');
writeString(view, 12, 'fmt ');
view.setUint32(16, 16, true);
view.setUint16(20, 1, true); // PCM Format
view.setUint16(22, 1, true); // Mono channel
view.setUint32(24, sampleRate, true);
view.setUint32(28, sampleRate * 2, true); // Byte rate
view.setUint16(32, 2, true); // Block align
view.setUint16(34, 16, true); // Bits per sample
writeString(view, 36, 'data');
view.setUint32(40, samplesTotal * 2, true);
// Normalize, prevent signal clipping, & write PCM 16-bit payload
const safeMaxMag = maxMag > 0 ? maxMag : 1;
let byteOffset = 44;
for (let i = 0; i < samplesTotal; i++) {
let s = samples[i] / safeMaxMag;
if (s > 1) s = 1; else if (s < -1) s = -1;
view.setInt16(byteOffset, s < 0 ? s * 0x8000 : s * 0x7FFF, true);
byteOffset += 2;
}
// Audio Blob Output
const blob = new Blob([view], { type: 'audio/wav' });
const url = URL.createObjectURL(blob);
// Construct UI container elements
const container = document.createElement('div');
container.style.fontFamily = 'system-ui, -apple-system, sans-serif';
container.style.maxWidth = '600px';
container.style.margin = '0 auto';
container.style.padding = '25px';
container.style.backgroundColor = '#181820';
container.style.color = '#ffffff';
container.style.borderRadius = '16px';
container.style.textAlign = 'center';
container.style.boxShadow = '0 12px 30px rgba(0,0,0,0.5)';
const title = document.createElement('h3');
title.innerText = '📻 Radiogram Sound Converter';
title.style.margin = '0 0 15px 0';
title.style.color = '#4fd1c5';
title.style.fontWeight = 'bold';
container.appendChild(title);
// Visual radiogram spectrogram approximation
const pCanvas = document.createElement('canvas');
pCanvas.width = W;
pCanvas.height = H;
pCanvas.style.width = '100%';
pCanvas.style.height = 'auto';
pCanvas.style.imageRendering = 'pixelated';
pCanvas.style.borderRadius = '8px';
pCanvas.style.border = '2px solid #2d3748';
pCanvas.style.marginBottom = '20px';
pCanvas.style.backgroundColor = '#000000';
const pCtx = pCanvas.getContext('2d');
const pImgData = pCtx.createImageData(W, H);
for (let x = 0; x < W; x++) {
for (let y = 0; y < H; y++) {
const pIdx = (y * W + x) * 4;
const bLevel = bright[x * H + y];
// Styling it beautifully with classic Radiogram Phosphor Green aesthetic
const intensity = Math.floor(bLevel * 255);
pImgData.data[pIdx] = Math.floor(intensity * 0.1); // R channel
pImgData.data[pIdx+1] = intensity; // G channel
pImgData.data[pIdx+2] = Math.floor(intensity * 0.2); // B channel
pImgData.data[pIdx+3] = 255; // A channel
}
}
pCtx.putImageData(pImgData, 0, 0);
container.appendChild(pCanvas);
const audioElem = document.createElement('audio');
audioElem.style.width = '100%';
audioElem.style.outline = 'none';
audioElem.controls = true;
audioElem.src = url;
container.appendChild(audioElem);
const info = document.createElement('p');
info.style.fontSize = '12px';
info.style.color = '#a0aec0';
info.style.margin = '15px 0 0 0';
info.innerText = `Image visualized as ${durationSec}s spectrogram audio | Telemetric Frequency: ${minFreq}Hz to ${maxFreq}Hz`;
container.appendChild(info);
return container;
}
Apply Changes