Published
Edited
Mar 13, 2022
5 stars
Hello, PGLiteINSEE ParquetHello, apcachDruidJS workerHello, OrbitWord Tour: 40k words and their friendsHello, spectral.jsHello, petite-vueHello, @thi.ng/grid-iteratorsHello, thumbhashHello, SwissGLHello, QOI (and glitches)Hello, orbHello, cosmographHello, TabulatorUsing d3.blur with PlotMath.cbrtHello debounceColorcetHello, gliiHello, Open MeteoHello, PyWaveletsHello, typesenseHello, libgifHello, kmeans-engineHappy anniversary, Project Gutenberg!Hello, fflateHello, ArchieML!Hello, d3-bboxCollideHello, jsgeoda!Hello, EDTF!Hello, protovis!Hello, placekeyHello, fuse.jsHello, Reorder.jsHello, shadow DOMjszipHello, procedural-glHello, qhullHello, genetic-jsDruidJSHello, Tippy.jsHello, paintWorkletBig πHello, AutoencoderHello, Shakespearean UUIDs
Hello, ccwt.js
Hello, constrainautorHello, talismanHello, polygon-offsetHello p-queueHello async-poolHello rollup-plugin-asciiHello, algebra.jsHello, pixi.jsHello, d3-renderHello zip/unzipCumulative Wikipedia DonationsHTML <details>regl textureHello, npyjsHello protobufHello, pencil touchHello, LOESSHello html2canvaslegra mapscolor2cssHello, ecsy2D point distributionsHello, delatinThe gpu.js loopDijkstra’s shortest-path treeHello nojacko/Dijkstras-jsHello, tcort/dijkstrajsHello, lambdabaa/dijkstraHello, gpu.js v2Hello jsqrHello qrcodeHello SharedArrayBufferHello GamePad APIHello vtk.jsHello nd4jsHello BiofabricTravelling with a self-organizing mapHello glitchHello UMAP-jsHello pandemoniumHello iocaneHello JSON-editorHello d3-griddingHello mljs/knnWorkerHello lalolibImage to GPU.jsImage to blink.jsTissot's indicatrixVega projectionsHello WebCLGLUsing d3-inertia with observableVideo contouring 3ngraph: pagerank, louvain…Union-FindPerceptron (simple statistics)mljsHello h3-jsEmoji FlagsHello, poisson-disk-sampling
Also listed in…
Algorithms
Insert cell
Insert cell
Insert cell
Insert cell
async function visual(sound) {
const audio_context = new window.AudioContext({ sampleRate: 44100 }),
audio_buffer = await new Promise((resolve) =>
sound.arrayBuffer().then((d) => audio_context.decodeAudioData(d, resolve))
);

const pcmf32_buffer = audio_buffer.getChannelData(0);

const length_in_seconds = pcmf32_buffer.length / audio_context.sampleRate;

const minimum_frequency = 16.34 * length_in_seconds;
const maximum_frequency = 20000.0 * length_in_seconds;
const deviation = 1.0;

const height = 500;

// linear
const frequency_basis_linear = 0;
const frequency_range_linear = maximum_frequency - minimum_frequency;
const frequency_offset_linear = minimum_frequency;

// logarithmic
const frequency_basis_log = 2.0; // each octave double the frequency
const minimum_octave =
Math.log(minimum_frequency) / Math.log(frequency_basis_log);
const maximum_octave =
Math.log(maximum_frequency) / Math.log(frequency_basis_log);

const frequency_range_log = maximum_octave - minimum_octave;
const frequency_offset_log = minimum_octave;

// linear
//CCWT.frequencyBand(frequencies, height, frequency_range_linear, frequency_offset_linear, frequency_basis_linear, deviation)

// logarithmic
const frequencies = CCWT.frequencyBand(
height,
frequency_range_log,
frequency_offset_log,
frequency_basis_log,
deviation
);

// add some padding to avoid start / end oddities (when there is data at one/both end of the signal)
const padding = 1; // suggested by @mootari
const gain_factor = 30;
const fourier_transformed_signal = CCWT.fft1d(
pcmf32_buffer,
padding,
gain_factor
);

const pixels_per_second = 60;
const output_width = Math.floor(length_in_seconds * pixels_per_second);

let percent_complete = 0;

const width = output_width;
const canvas_ctx = DOM.context2d(width, height, 1);
canvas_ctx.fillStyle = "#000000";
canvas_ctx.fillRect(0, 0, width, height);

const row_callback = function (y, row_data, output_padding) {
const spectrogram = canvas_ctx.createImageData(output_width, 1);
const spectro_data = spectrogram.data;

const percent_complete_new = Math.round((y / height) * 100);
if (percent_complete_new != percent_complete) {
console.log(percent_complete_new + "%");
percent_complete = percent_complete_new;
}

let x = 0;
for (x = 0; x < output_width; ++x) {
const r = row_data[output_padding * 2 + x * 2];
const i = row_data[output_padding * 2 + x * 2 + 1];

const amplitude_raw = Math.hypot(r, i);

// logarithmic intensity (sharpen edges / invert result when < 1)
/*
const logarithmic_basis = 1
const log_factor = 1.0 / Math.log(logarithmic_basis);
const value_sign = (0 < amplitude_raw) - (amplitude_raw < 0)
const amplitude = Math.min(Math.max(Math.log(amplitude_raw * value_sign) * log_factor, 0.), 1.) * value_sign
*/

// linear intensity
const value_sign = (0 < amplitude_raw) - (amplitude_raw < 0);
const amplitude = Math.min(amplitude_raw * value_sign, 1.0) * value_sign;

const rgb = d3.rgb(color(amplitude));

const index = x * 4;
spectro_data[index] = rgb.r;
spectro_data[index + 1] = rgb.g;
spectro_data[index + 2] = rgb.b;
spectro_data[index + 3] = 255;
}

canvas_ctx.putImageData(spectrogram, 0, y);
};

CCWT.numericOutput(
fourier_transformed_signal,
padding,
frequencies,
0,
frequencies.length / 2,
output_width,
row_callback
);

return canvas_ctx.canvas;
}
Insert cell
CCWT = {
const fetch = window.fetch;
invalidation.then(() => window.fetch = fetch);
window.fetch = function(url) {
return url === "FFTW.wasm"
? FileAttachment("FFTW.wasm").blob().then(_ => new Response(_))
: fetch.apply(window, arguments);
};
const ccwt = await require("ccwt.js@1.0.4/demo/dist/ccwt.js");
return new Promise(resolve => {
ccwt.onReady = () => {
window.fetch = fetch;
resolve(ccwt);
}
});
}
Insert cell
sound1 = FileAttachment("Nouvel enregistrement.m4a")
Insert cell
sound2 = FileAttachment("female_french_numbers_1_10.wav")
Insert cell
d3 = require("d3-color@3", "d3-scale-chromatic@3")
Insert cell
color = d3.interpolateTurbo
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more