Skip to content

DarthVanger/guitar-synth-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

78 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Zero-dependency 1kb guitar synth

Js guitar synthesizer based on Karplus–Strong algorithm.

Demo

https://darthvanger.github.io/guitar-synth-js/

Installation

npm i guitar-synth-js

Usage

import { GuitarString } from 'guitar-synth-js';

// Create a new `GuitarString`, passing the sound frequency to the constructor
// (e.g. `440` for concert A note)
const hz = 440;
const string = new GuitarString(hz);

// Create an empty buffer to store the guitar string sound
const sampleRate = 44100;
const soundDurationSec = 5;
const soundBufferSize = sampleRate * soundDurationSec;
const soundBuffer = new Float32Array(soundBufferSize);

// Pluck the string and get values by calling `string.sample()` and `string.tic()` in a loop
string.pluck();
let i = 0;
while (i < soundBufferSize) {
  soundBuffer[i] = string.sample();
  string.tic();
  i++;
}

// Play the generated sound buffer in browser
const AudioContext = window.AudioContext || window.webkitAudioContext;
const audioCtx = new AudioContext({sampleRate});
const audioBuffer = audioCtx.createBuffer(1, soundBuffer.length, sampleRate);

audioBuffer.copyToChannel(soundBuffer, 0);
const source = audioCtx.createBufferSource();
source.connect(audioCtx.destination);
source.buffer = audioBuffer;
source.start();

About

Guitar Synthesizer written in JS

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published