Skip to content
A JavaScript library for manipulating WebAudio Contexts, specifically for time-stretching a pitch change
Branch: master
Clone or download
Stephen Blades
Stephen Blades Security updates
Latest commit 536cb82 Jun 17, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist
src
.babelrc Update to resolve security issue with a dependency's dependency, as w… Mar 5, 2019
.eslintrc.json Code style fixes. Added 'play' CustomEvent Sep 22, 2018
.gitignore
.prettierrc.json Code style fixes. Added 'play' CustomEvent Sep 22, 2018
CHANGELOG.md Update CHANGELOG. Mar 5, 2019
LICENSE first commit Aug 20, 2018
README.md Code style fixes. Added 'play' CustomEvent Sep 22, 2018
bensound-actionable.mp3 first commit Aug 20, 2018
example.js Update to resolve security issue with a dependency's dependency, as w… Mar 5, 2019
index.html first commit Aug 20, 2018
package-lock.json Security updates Jun 17, 2019
package.json Security updates Jun 17, 2019
rollup.config.js Update to resolve security issue with a dependency's dependency, as w… Mar 5, 2019
server.js first commit Aug 20, 2018

README.md

SoundTouchJS

SoundTouchJS is a JavaScript audio time-stretching and pitch-shifting library, based on the C++ implementation of Soundtouch. The earliest implementation in JavaScript was written by Ryan Berdeen and later expanded by Jakub Faila. I have further expanded this library into a distributable package, refactored for es2015 development.

This package includes the getWebAudioNode utility written by Adrian Holovaty, as well as the user-friendly PitchShifter wrapper from Jakub Faila.

Installation

You can easily install SoundTouchJS for use in your project:

npm install soundtouchjs

General Usage

You can use whatever method you prefer to get your audio file, but once you have the data you must decode it into an AudioBuffer. Once you've decoded the data you can then create a new PitchShifter.

import {PitchShifter} from 'soundtouchjs';

const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
const gainNode = audioCtx.createGain();
let shifter;

// here you retrieved your file with 'fetch' or a new instance of the 'FileReader', and from the data...
    if (shifter) {
        shifter.off(); // remove any current listeners
    }
    audioCtx.decodeAudioData(buffer, audioBuffer => {
        shifter = new PitchShifter(audioCtx, audioBuffer, 1024);
        shifter.on('play', (detail) => {
            // do something with detail.timePlayed;
            // do something with detail.formattedTimePlayed;
            // do something with detail.percentagePlayed
        });
        shifter.tempo = 1;
        shifter.pitch = 1;
    });

To begin playback you connect the PitchShifter to the WebAudio destination (or another node), and disconnect it to pause. It's important to note that the PitchShifter is a pseudo-node, and cannot be connected to.

const play = function () {
    shifter.connect(gainNode); // connect it to a GainNode to control the volume
    gainNode.connect(audioCtx.destination); // attach the GainNode to the 'destination' to begin playback
};

Running The Example

An example has been included with the package to see some basic functionality. It has been written in pure javascript, but could easily be integrated with your favorite framework. To run the example:

npm run example

then open your browser to http://localhost:8080. A royalty free music file is included under Creative Commons License with this repository.

Music: "Actionable" from Bensound.com.

This is a limited use license, and we do not grant any permissions beyond theirs. Please refer to their licensing for further information.

All core components of the package are available as separate entities for more advanced audio manipulations. See the source code for greater understanding.

You can’t perform that action at this time.