npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

crunker

v2.4.0

Published

Simple way to merge or concatenate audio files with the Web Audio API.

Downloads

11,212

Readme

Crunker

Simple way to merge, concatenate, play, export and download audio files with the Web Audio API.

  • No dependencies
  • Tiny 2kB gzipped
  • Written in Typescript

View online demos

Installation

yarn add crunker
npm install crunker

Example

let crunker = new Crunker();

crunker
  .fetchAudio('/song.mp3', '/another-song.mp3')
  .then((buffers) => {
    // => [AudioBuffer, AudioBuffer]
    return crunker.mergeAudio(buffers);
  })
  .then((merged) => {
    // => AudioBuffer
    return crunker.export(merged, 'audio/mp3');
  })
  .then((output) => {
    // => {blob, element, url}
    crunker.download(output.blob);
    document.body.append(output.element);
    console.log(output.url);
  })
  .catch((error) => {
    // => Error Message
  });

crunker.notSupported(() => {
  // Handle no browser support
});

Condensed Example

let crunker = new Crunker();

crunker
  .fetchAudio('/voice.mp3', '/background.mp3')
  .then((buffers) => crunker.mergeAudio(buffers))
  .then((merged) => crunker.export(merged, 'audio/mp3'))
  .then((output) => crunker.download(output.blob))
  .catch((error) => {
    throw new Error(error);
  });

Input file Example

let crunker = new Crunker();

const onFileInputChange = async (target) => {
  const buffers = await crunker.fetchAudio(...target.files, '/voice.mp3', '/background.mp3');
};

<input onChange={onFileInputChange(this)} type="file" accept="audio/*" />;

Other Examples

Graphic Representation of Methods

Merge

merge

Concat

concat

Methods

For more detailed API documentation, view the Typescript typings.

new Crunker()

Create a new instance of Crunker. You may optionally provide an object with a sampleRate key, but it will default to the same sample rate as the internal audio context, which is appropriate for your device.

crunker.fetchAudio(songURL, anotherSongURL)

Fetch one or more audio files.
Returns: an array of audio buffers in the order they were fetched.

crunker.mergeAudio(arrayOfBuffers);

Merge two or more audio buffers.
Returns: a single AudioBuffer object.

crunker.concatAudio(arrayOfBuffers);

Concatenate two or more audio buffers in the order specified.
Returns: a single AudioBuffer object.

crunker.padAudio(buffer, padStart, seconds);

Pad the audio with silence, at the beginning, the end, or any specified points through the audio.
Returns: a single AudioBuffer object.

crunker.sliceAudio(buffer, start, end, fadeIn, fadeOut);

Slice the audio to the specified range, removing any content outside the range. Optionally add a fade-in at the start and a fade-out at the end to avoid audible clicks.

  • buffer: The audio buffer to be trimmed.
  • start: The starting second from where the audio should begin.
  • end: The ending second where the audio should be trimmed.
  • fadeIn: (Optional) Number of seconds for the fade-in effect at the beginning. Default is 0.
  • fadeOut: (Optional) Number of seconds for the fade-out effect at the end. Default is 0.

Returns: a single AudioBuffer object.

crunker.export(buffer, type);

Export an audio buffers with MIME type option.
Type: e.g. 'audio/mp3', 'audio/wav', 'audio/ogg'. IMPORTANT: the MIME type does not change the actual file format. It will always be a WAVE file under the hood.
Returns: an object containing the blob object, url, and an audio element object.

crunker.download(blob, filename);

Automatically download an exported audio blob with optional filename.
Filename: String not containing the .mp3, .wav, or .ogg file extension.
Returns: the HTMLAnchorElement element used to simulate the automatic download.

crunker.play(buffer);

Starts playing the exported audio buffer in the background.
Returns: the HTMLAudioElement.

crunker.notSupported(callback);

Execute custom code if Web Audio API is not supported by the users browser.
Returns: The callback function.

Properties

For more detailed API documentation, view the Typescript typings.

crunker.context

Access the AudioContext used internally by a given Crunker.
Returns: AudioContext.

License

MIT