Skip to content

Commit 69a57fd

Browse files
committed
new pull request with p5.SoundLoop()
1 parent 9f4690d commit 69a57fd

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed

Gruntfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ module.exports = function(grunt) {
8787
'distortion': 'src/distortion',
8888
'compressor': 'src/compressor',
8989
'looper': 'src/looper',
90+
'soundloop': 'src/soundLoop',
9091
'soundRecorder': 'src/soundRecorder',
9192
'signal': 'src/signal',
9293
'metro': 'src/metro',

examples/sound_loop/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<head>
2+
<script language="javascript" type="text/javascript" src="../../lib/p5.js"></script>
3+
4+
<script language="javascript" type="text/javascript" src="../../lib/addons/p5.dom.js"></script>
5+
6+
<script language="javascript" type="text/javascript" src="../../lib/p5.sound.js"></script>
7+
8+
<script language="javascript" type="text/javascript" src="sketch.js"></script>
9+
10+
</head>

examples/sound_loop/sketch.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Create a sequence using a Part with callbacks that play back soundfiles.
3+
* The callback includes parameters (the value at that position in the Phrase array)
4+
* as well as time, which should be used to schedule playback with precision.
5+
*
6+
*/
7+
8+
var click, beatbox;
9+
var clickPhrase = [1, 0, 0, 0];
10+
var bboxPhrase = [0, 0, 1, 0, 0, 0, 1, 1];
11+
12+
13+
var looper;
14+
var num;
15+
16+
function preload() {
17+
soundFormats('mp3', 'ogg');
18+
click = loadSound('../files/drum');
19+
beatbox = loadSound('../files/beatbox');
20+
21+
}
22+
23+
function setup() {
24+
25+
// create a part with 8 spaces, where each space represents 1/16th note (default)
26+
looper = new p5.SoundLoop(function(time){
27+
28+
console.log(time);
29+
30+
}, 4);
31+
32+
looper.start();
33+
}

src/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ define(function (require) {
2424
require('reverb');
2525
require('metro');
2626
require('looper');
27+
require('soundloop');
2728
require('compressor');
2829
require('soundRecorder');
2930
require('peakdetect');

src/soundLoop.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
'use strict';
2+
3+
define(function (require) {
4+
var p5sound = require('master');
5+
var Clock = require('Tone/core/Clock');
6+
7+
p5.SoundLoop = function(callback, interval, BPM) {
8+
9+
this.callback = callback;
10+
this._interval = interval;
11+
this._timeSignature = 4;
12+
13+
this.bpm = BPM || 60;
14+
15+
16+
var self = this;
17+
this.clock = new Clock({
18+
'callback' : function(time) {
19+
var timeFromNow = time - p5sound.audiocontext.currentTime;
20+
self.callback(timeFromNow);
21+
},
22+
'frequency' : this.calcFreq(this._interval)
23+
});
24+
};
25+
26+
27+
p5.SoundLoop.prototype.start = function(timeFromNow) {
28+
var t = timeFromNow || 0;
29+
var now = p5sound.audiocontext.currentTime;
30+
this.clock.start(now + t);
31+
};
32+
33+
p5.SoundLoop.prototype.stop = function(timeFromNow) {
34+
var t = timeFromNow || 0;
35+
var now = p5sound.audiocontext.currentTime;
36+
this.clock.stop(now + t);
37+
};
38+
39+
p5.SoundLoop.prototype.pause = function(time) {
40+
this.clock.pause(time);
41+
};
42+
43+
p5.SoundLoop.prototype.setBPM = function (bpm) {
44+
this.bpm = bpm;
45+
this.changeInterval(this._interval);
46+
};
47+
48+
p5.SoundLoop.prototype.setTimeSignature = function(timeSig) {
49+
this._timeSignature = timeSig;
50+
this.changeInterval(this._interval);
51+
};
52+
53+
p5.SoundLoop.prototype.changeInterval = function(newInterval) {
54+
this._interval = newInterval;
55+
this.clock.frequency.value = this.calcFreq(this._interval);
56+
};
57+
58+
p5.SoundLoop.prototype.calcFreq = function(interval) {
59+
if (typeof interval === 'number') {
60+
return this.bpm / 60 / interval * (this._timeSignature / 4);
61+
} else if (typeof interval === 'string') {
62+
return this.bpm / 60 / this._convertNotation(interval) * this._timeSignature / 4;
63+
}
64+
};
65+
66+
p5.SoundLoop.prototype._convertNotation = function(value) {
67+
var type = value.slice(-1);
68+
value = Number(value.slice(0,-1));
69+
switch (type) {
70+
case 'm':
71+
return this._measure(value);
72+
case 'n':
73+
return this._note(value);
74+
default:
75+
//console.warn(something);
76+
}
77+
78+
};
79+
80+
p5.SoundLoop.prototype._measure = function(value) {
81+
return value;
82+
};
83+
84+
p5.SoundLoop.prototype._note = function(value) {
85+
return 1 / value;
86+
};
87+
88+
89+
return p5.SoundLoop;
90+
91+
});

0 commit comments

Comments
 (0)