forked from processing/p5.js-sound
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new pull request with p5.SoundLoop()
- Loading branch information
Showing
5 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
}); |