Skip to content

Commit

Permalink
Added Karplus-Strong string synthesis
Browse files Browse the repository at this point in the history
  • Loading branch information
wybiral committed Jul 16, 2016
1 parent fca379b commit 2d86612
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ window.onload = function() {
};

function play(ctx, node) {
var freq = 220.0;
// Get current time in seconds
var startTime = ctx.currentTime;
// Set duration in seconds
Expand All @@ -26,12 +27,24 @@ function play(ctx, node) {
var buffer = ctx.createBuffer(1, length, rate);
// Grab channel for inserting samples
var data = buffer.getChannelData(0);
// Get wave period
var period = Math.floor(rate / freq);

// Generate white noise
for (var i = 0; i < data.length; i++) {
// Generate white noise (random excitation)
for (var i = 0; i < period; i++) {
data[i] = Math.random() * 2 - 1;
}

// Decay of feedback loop
var decay = 0.993;
// Length of buffer beyond initial excitation
var len = data.length - period;

// Feedback loop
for (var i = 0; i < len; i++) {
data[i + period] = (data[i] + data[i + 1]) * decay * 0.5;
}

// Create audio source
var source = ctx.createBufferSource();
// Set buffer to our created one
Expand Down

0 comments on commit 2d86612

Please sign in to comment.