Skip to content

Commit 0db339a

Browse files
committed
added getters and setters for interval, bpm, timesig
1 parent 69a57fd commit 0db339a

File tree

2 files changed

+97
-30
lines changed

2 files changed

+97
-30
lines changed

examples/sound_loop/sketch.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55
*
66
*/
77

8-
var click, beatbox;
8+
var click, beatbox ;
99
var clickPhrase = [1, 0, 0, 0];
1010
var bboxPhrase = [0, 0, 1, 0, 0, 0, 1, 1];
1111

1212

13-
var looper;
13+
var looper1, looper2;
1414
var num;
1515

16+
var c,d;
17+
var count1, count2;
18+
19+
20+
1621
function preload() {
1722
soundFormats('mp3', 'ogg');
1823
click = loadSound('../files/drum');
@@ -21,13 +26,29 @@ function preload() {
2126
}
2227

2328
function setup() {
24-
29+
createCanvas(500,500)
30+
count1 = 0;
31+
count2 = 0;
32+
c = color(50);
33+
d = color(50);
2534
// create a part with 8 spaces, where each space represents 1/16th note (default)
26-
looper = new p5.SoundLoop(function(time){
35+
looper1 = new p5.SoundLoop(function(time){
36+
click.play();
37+
}, "4n");
38+
39+
looper2 = new p5.SoundLoop(function(time){
2740

28-
console.log(time);
41+
beatbox.play();
42+
}, 4/3);
2943

30-
}, 4);
44+
looper1.start();
45+
looper2.start();
3146

32-
looper.start();
47+
48+
looper1.bpm = 120;
49+
looper2.bpm = 120;
50+
3351
}
52+
53+
54+

src/soundLoop.js

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,28 @@ define(function (require) {
77
p5.SoundLoop = function(callback, interval, BPM) {
88

99
this.callback = callback;
10+
11+
//set private variables
1012
this._interval = interval;
11-
this._timeSignature = 4;
1213

13-
this.bpm = BPM || 60;
14+
//musicalTimeMode is true if time is specified as "4n", "8n" ...etc
15+
this.musicalTimeMode = typeof this._interval === 'number' ? false : true;
1416

17+
//These variables should only be modified if using musicalTimeMode
18+
//If these variables are modified when interval is measured in seconds,
19+
//the interval will become inaccurate.
20+
//ex. 8 second interval at 60BPM in 4/4 time will be 8 seconds long
21+
//8 second interval at 120BPM in 5/4 time will be 3.2 seconds long
22+
this._timeSignature = 4;
23+
this._bpm = BPM || 60;
1524

1625
var self = this;
1726
this.clock = new Clock({
1827
'callback' : function(time) {
1928
var timeFromNow = time - p5sound.audiocontext.currentTime;
2029
self.callback(timeFromNow);
2130
},
22-
'frequency' : this.calcFreq(this._interval)
31+
'frequency' : this.calcFreq()
2332
});
2433
};
2534

@@ -40,29 +49,20 @@ define(function (require) {
4049
this.clock.pause(time);
4150
};
4251

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);
52+
p5.SoundLoop.prototype._update = function() {
53+
this.clock.frequency.value = this.calcFreq();
5654
};
5755

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;
56+
p5.SoundLoop.prototype.calcFreq = function() {
57+
if (typeof this._interval === 'number') {
58+
console.log(this._interval);
59+
return this._bpm / 60 / this._interval * (this._timeSignature / 4);
60+
} else if (typeof this._interval === 'string') {
61+
return this._bpm / 60 / this._convertNotation(this._interval) * (this._timeSignature / 4);
6362
}
6463
};
6564

65+
//TIME NOTATION FUNCS
6666
p5.SoundLoop.prototype._convertNotation = function(value) {
6767
var type = value.slice(-1);
6868
value = Number(value.slice(0,-1));
@@ -77,14 +77,60 @@ define(function (require) {
7777

7878
};
7979

80+
8081
p5.SoundLoop.prototype._measure = function(value) {
81-
return value;
82+
return value * this._timeSignature;
8283
};
8384

8485
p5.SoundLoop.prototype._note = function(value) {
85-
return 1 / value;
86+
return this._timeSignature / value ;
8687
};
8788

89+
// PUBLIC VARIABLES
90+
Object.defineProperty(p5.SoundLoop.prototype, 'bpm', {
91+
get : function() {
92+
return this._bpm;
93+
},
94+
set : function(bpm) {
95+
if (!this.musicalTimeMode) {
96+
console.warn('Changing the BPM in "seconds" mode is not advised. '+
97+
'This will make the specified time interval inaccurate. '+
98+
'8 second interval at 60BPM in 4/4 time will be 8 seconds long ' +
99+
'8 second interval at 120BPM in 5/4 time will be 3.2 seconds long. '+
100+
'Use musical timing notation ("2n", "4n", "1m"...etc');
101+
}
102+
this._bpm = bpm;
103+
this._update();
104+
}
105+
});
106+
107+
Object.defineProperty(p5.SoundLoop.prototype, 'timeSignature', {
108+
get : function() {
109+
return this._timeSignature;
110+
},
111+
set : function(timeSig) {
112+
if (!this.musicalTimeMode) {
113+
console.warn('Changing the time signature in "seconds" mode is not advised. '+
114+
'This will make the specified time interval inaccurate. '+
115+
'8 second interval at 60BPM in 4/4 time will be 8 seconds long ' +
116+
'8 second interval at 120BPM in 5/4 time will be 3.2 seconds long. '+
117+
'Use musical timing notation ("2n", "4n", "1m"...etc');
118+
}
119+
this._timeSignature = timeSig;
120+
this._update();
121+
}
122+
});
123+
124+
Object.defineProperty(p5.SoundLoop.prototype, 'interval', {
125+
get : function() {
126+
return this._interval;
127+
},
128+
set : function(interval) {
129+
this._interval = interval;
130+
this._update();
131+
}
132+
});
133+
88134

89135
return p5.SoundLoop;
90136

0 commit comments

Comments
 (0)