Skip to content

Commit

Permalink
new pull request with p5.SoundLoop()
Browse files Browse the repository at this point in the history
  • Loading branch information
jvntf committed Aug 15, 2017
1 parent 9f4690d commit 69a57fd
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ module.exports = function(grunt) {
'distortion': 'src/distortion',
'compressor': 'src/compressor',
'looper': 'src/looper',
'soundloop': 'src/soundLoop',
'soundRecorder': 'src/soundRecorder',
'signal': 'src/signal',
'metro': 'src/metro',
Expand Down
10 changes: 10 additions & 0 deletions examples/sound_loop/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<head>
<script language="javascript" type="text/javascript" src="../../lib/p5.js"></script>

<script language="javascript" type="text/javascript" src="../../lib/addons/p5.dom.js"></script>

<script language="javascript" type="text/javascript" src="../../lib/p5.sound.js"></script>

<script language="javascript" type="text/javascript" src="sketch.js"></script>

</head>
33 changes: 33 additions & 0 deletions examples/sound_loop/sketch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Create a sequence using a Part with callbacks that play back soundfiles.
* The callback includes parameters (the value at that position in the Phrase array)
* as well as time, which should be used to schedule playback with precision.
*
*/

var click, beatbox;
var clickPhrase = [1, 0, 0, 0];
var bboxPhrase = [0, 0, 1, 0, 0, 0, 1, 1];


var looper;
var num;

function preload() {
soundFormats('mp3', 'ogg');
click = loadSound('../files/drum');
beatbox = loadSound('../files/beatbox');

}

function setup() {

// create a part with 8 spaces, where each space represents 1/16th note (default)
looper = new p5.SoundLoop(function(time){

console.log(time);

}, 4);

looper.start();
}
1 change: 1 addition & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ define(function (require) {
require('reverb');
require('metro');
require('looper');
require('soundloop');
require('compressor');
require('soundRecorder');
require('peakdetect');
Expand Down
91 changes: 91 additions & 0 deletions src/soundLoop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
'use strict';

define(function (require) {
var p5sound = require('master');
var Clock = require('Tone/core/Clock');

p5.SoundLoop = function(callback, interval, BPM) {

this.callback = callback;
this._interval = interval;
this._timeSignature = 4;

this.bpm = BPM || 60;


var self = this;
this.clock = new Clock({
'callback' : function(time) {
var timeFromNow = time - p5sound.audiocontext.currentTime;
self.callback(timeFromNow);
},
'frequency' : this.calcFreq(this._interval)
});
};


p5.SoundLoop.prototype.start = function(timeFromNow) {
var t = timeFromNow || 0;
var now = p5sound.audiocontext.currentTime;
this.clock.start(now + t);
};

p5.SoundLoop.prototype.stop = function(timeFromNow) {
var t = timeFromNow || 0;
var now = p5sound.audiocontext.currentTime;
this.clock.stop(now + t);
};

p5.SoundLoop.prototype.pause = function(time) {
this.clock.pause(time);
};

p5.SoundLoop.prototype.setBPM = function (bpm) {
this.bpm = bpm;
this.changeInterval(this._interval);
};

p5.SoundLoop.prototype.setTimeSignature = function(timeSig) {
this._timeSignature = timeSig;
this.changeInterval(this._interval);
};

p5.SoundLoop.prototype.changeInterval = function(newInterval) {
this._interval = newInterval;
this.clock.frequency.value = this.calcFreq(this._interval);
};

p5.SoundLoop.prototype.calcFreq = function(interval) {
if (typeof interval === 'number') {
return this.bpm / 60 / interval * (this._timeSignature / 4);
} else if (typeof interval === 'string') {
return this.bpm / 60 / this._convertNotation(interval) * this._timeSignature / 4;
}
};

p5.SoundLoop.prototype._convertNotation = function(value) {
var type = value.slice(-1);
value = Number(value.slice(0,-1));
switch (type) {
case 'm':
return this._measure(value);
case 'n':
return this._note(value);
default:
//console.warn(something);
}

};

p5.SoundLoop.prototype._measure = function(value) {
return value;
};

p5.SoundLoop.prototype._note = function(value) {
return 1 / value;
};


return p5.SoundLoop;

});

0 comments on commit 69a57fd

Please sign in to comment.