Skip to content

Commit 329ad99

Browse files
(Gsoc'21)➕Added tests for metro.js and fixed a bug
1 parent eef4ce5 commit 329ad99

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

src/metro.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ class Metro {
6060
}
6161

6262
getBPM() {
63-
return (this.clock.getRate() / this.tatums) * 60;
63+
return this.bpm;
64+
// return (this.clock.getRate() / this.tatums) * 60;
6465
}
6566

6667
_init() {
@@ -78,6 +79,7 @@ class Metro {
7879
this.syncedParts.push(part);
7980
}
8081

82+
//timeFromNow is in seconds
8183
start(timeFromNow) {
8284
var t = timeFromNow || 0;
8385
var now = p5sound.audiocontext.currentTime;

test/tests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import('./tests/main.js');
22
import('./tests/p5.Helpers.js');
33
import('./tests/p5.Distortion.js');
44
import('./tests/p5.AudioContext.js');
5+
import('./tests/p5.Metro.js')
56
import('./tests/p5.Effect.js');
67
import('./tests/p5.Filter.js');
78
import('./tests/p5.Gain.js');

test/tests/p5.Metro.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
const expect = chai.expect;
2+
let metro;
3+
4+
describe('p5.Metro', function () {
5+
beforeEach(function () {
6+
metro = new p5.Metro();
7+
});
8+
9+
it('can be created', function () {
10+
expect(metro).to.have.property('bpm').to.equal(120);
11+
expect(metro).to.have.property('clock');
12+
expect(metro).to.have.property('syncedParts').to.be.an('array');
13+
});
14+
15+
it('can be initialised with a beatlength, bpm', function () {
16+
metro.beatLength(0.0625);
17+
metro.setBPM(60);
18+
expect(metro.tatums).to.equal(4);
19+
expect(metro.tatumTime).to.equal(0.25);
20+
expect(metro.getBPM()).to.equal(60);
21+
});
22+
23+
it('can be started and stopped', function (done) {
24+
this.timeout = 2000;
25+
let ticks;
26+
metro.setBPM(600);
27+
metro.start();
28+
setTimeout(() => {
29+
ticks = metro.metroTicks;
30+
metro.stop();
31+
expect(ticks).to.not.equal(0);
32+
setTimeout(() => {
33+
expect(metro.metroTicks).to.equal(ticks);
34+
done();
35+
}, 100);
36+
}, 1000);
37+
});
38+
39+
it('can be started and stopped with dealy', function (done) {
40+
let ticks;
41+
metro.setBPM(600);
42+
metro.start(0.1);
43+
setTimeout(() => {
44+
ticks = metro.metroTicks;
45+
metro.stop(0.2);
46+
expect(ticks).to.not.equal(0);
47+
setTimeout(() => {
48+
expect(metro.metroTicks).to.not.equal(ticks);
49+
}, 100);
50+
setTimeout(() => {
51+
ticks = metro.metroTicks;
52+
setTimeout(() => {
53+
expect(metro.metroTicks).to.equal(ticks);
54+
done();
55+
}, 100);
56+
}, 200);
57+
}, 1000);
58+
});
59+
60+
it('can sync parts', function () {
61+
let part = new p5.Part();
62+
part.addPhrase('snare', () => {}, [0, 0, 1, 0]);
63+
part.setBPM(60);
64+
part.noLoop();
65+
part.start();
66+
expect(metro.syncedParts.length).to.equal(0);
67+
metro.resetSync(part);
68+
expect(metro.syncedParts.length).to.equal(1);
69+
});
70+
71+
it('parts can be pushed into syncedParts', function () {
72+
let phraseAttack = new p5.Phrase('testerAttack', () => {}, [1, 0, 0, 0]);
73+
let part = new p5.Part();
74+
part.addPhrase(phraseAttack);
75+
part.setBPM(60);
76+
part.start();
77+
expect(metro.syncedParts.length).to.equal(0);
78+
metro.pushSync(part);
79+
expect(metro.syncedParts.length).to.equal(1);
80+
metro.pushSync(part);
81+
expect(metro.syncedParts.length).to.equal(2);
82+
metro.resetSync(part);
83+
expect(metro.syncedParts.length).to.equal(1);
84+
});
85+
});

0 commit comments

Comments
 (0)