functions for generating .wav file data 🔉
Go to file
jmath 87d0d3c438 chore(package.json): bump version 2022-11-18 18:47:26 -07:00
source feature(README): update with some generic use cases 2022-11-18 18:28:18 -07:00
.gitignore feature(getWavData): piece together wav file data from sample rate and channels data 2022-10-17 12:17:44 -06:00
.npmignore feature(getWavData): piece together wav file data from sample rate and channels data 2022-10-17 12:17:44 -06:00
LICENSE feature(getWavData): piece together wav file data from sample rate and channels data 2022-10-17 12:17:44 -06:00
README.md feature(README): update with some generic use cases 2022-11-18 18:28:18 -07:00
package.json chore(package.json): bump version 2022-11-18 18:47:26 -07:00
tsconfig.json feature(getWavData): piece together wav file data from sample rate and channels data 2022-10-17 12:17:44 -06:00
yarn.lock feature(getWavData): piece together wav file data from sample rate and channels data 2022-10-17 12:17:44 -06:00

README.md

clumsy-wav

functions for generating .wav file data 🔉

tl;dr

generate a three-second wav file of a 440hz sine wave at 48000 samples per second

import { getWavBuffer } from "clumsy-wav";

const lengthOfAudioInSeconds = 3;
const sampleRate: SampleRate = 48000;
const sampleCount = lengthOfAudioInSeconds * sampleRate;
const channelsData: MonoChannelsData = [
  new Array(sampleCount).fill(undefined).map((_, sampleIndex) => {
    const waveFrequency = 440;
    const frequencySampleRateScalar = sampleCount / sampleRate;
    const angleStep = (2 * Math.PI) / sampleRate;
    const sampleAngle = sampleIndex * angleStep;
    return Math.sin(waveFrequency * frequencySampleRateScalar * sampleAngle);
  }),
];
const wavBuffer = getWavBuffer(sampleRate, channelsData);

play wav file

// within some async onClick handler
const audioContext = new AudioContext({
  sampleRate,
});
const audioSourceNode = new AudioBufferSourceNode(currentAudioContext);
audioSourceNode.buffer = await audioContext.decodeAudioData(wavBuffer.slice(0));
audioSourceNode.connect(audioContext.destination);
audioSourceNode.start();

download wav file

// within some onClick handler
const wavFile = new Blob([wavBuffer], {
  type: "audio/wav",
});
const wavUrl = URL.createObjectURL(wavFile);
const tempAnchor = document.createElement("a");
tempAnchor.href = wavUrl;
tempAnchor.download = "sine440.wav";
tempAnchor.click();
URL.revokeObjectURL(wavUrl);

installation

yarn add clumsy-wav

resources