Skip to content

Instantly share code, notes, and snippets.

@remarkablemark
Created March 18, 2026 01:15
Show Gist options
  • Select an option

  • Save remarkablemark/5558a60814ca728bcb3cd80345967024 to your computer and use it in GitHub Desktop.

Select an option

Save remarkablemark/5558a60814ca728bcb3cd80345967024 to your computer and use it in GitHub Desktop.
click.wav
const fs = require('fs');
// Generate a simple click sound (short high-pitched tone with decay)
const sampleRate = 44100;
const duration = 0.05; // 50ms
const numSamples = Math.floor(sampleRate * duration);
const frequency = 800; // Hz
// Create WAV header
const headerSize = 44;
const dataSize = numSamples * 2; // 16-bit mono
const fileSize = headerSize + dataSize - 8;
const header = Buffer.alloc(headerSize);
// RIFF chunk descriptor
header.write('RIFF', 0);
header.writeUInt32LE(fileSize, 4);
header.write('WAVE', 8);
// fmt sub-chunk
header.write('fmt ', 12);
header.writeUInt32LE(16, 16); // Subchunk1Size (16 for PCM)
header.writeUInt16LE(1, 20); // AudioFormat (1 for PCM)
header.writeUInt16LE(1, 22); // NumChannels (1 for mono)
header.writeUInt32LE(sampleRate, 24); // SampleRate
header.writeUInt32LE(sampleRate * 2, 28); // ByteRate
header.writeUInt16LE(2, 32); // BlockAlign
header.writeUInt16LE(16, 34); // BitsPerSample
// data sub-chunk
header.write('data', 36);
header.writeUInt32LE(dataSize, 40);
// Generate audio data
const data = Buffer.alloc(dataSize);
for (let i = 0; i < numSamples; i++) {
const t = i / sampleRate;
// Sine wave with exponential decay
const amplitude = Math.exp(-t * 60);
const sample = Math.sin(2 * Math.PI * frequency * t) * amplitude * 32767;
data.writeInt16LE(Math.floor(sample), i * 2);
}
// Write WAV file
const wavBuffer = Buffer.concat([header, data]);
fs.writeFileSync('click.wav', wavBuffer);
console.log('Created click.wav');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment