|
| 1 | +const expect = chai.expect; |
| 2 | + |
| 3 | +describe('p5.Looper', function () { |
| 4 | + it('setBPM sets the BPM for all parts', function () { |
| 5 | + let part1 = new p5.Part(); |
| 6 | + let part2 = new p5.Part(); |
| 7 | + let part3 = new p5.Part(); |
| 8 | + expect(part1.getBPM()).to.equal(120); |
| 9 | + expect(part2.getBPM()).to.equal(120); |
| 10 | + p5.prototype.setBPM(200); |
| 11 | + expect(part1.getBPM()).to.equal(200); |
| 12 | + expect(part2.getBPM()).to.equal(200); |
| 13 | + expect(part3.getBPM()).to.equal(200); |
| 14 | + }); |
| 15 | + describe('Phrase', function () { |
| 16 | + it('can be initialized', function () { |
| 17 | + let phrase = new p5.Phrase('bbox', () => {}, [1, 1, 1, 0, 2, 1, 0]); |
| 18 | + |
| 19 | + expect(phrase.phraseStep).to.equal(0); |
| 20 | + expect(phrase.name).to.equal('bbox'); |
| 21 | + expect(phrase.sequence).to.deep.equal([1, 1, 1, 0, 2, 1, 0]); |
| 22 | + }); |
| 23 | + }); |
| 24 | + describe('Part', function () { |
| 25 | + it('can be initialized', function () { |
| 26 | + let part = new p5.Part(); |
| 27 | + expect(part.length).to.equal(0); |
| 28 | + expect(part.partStep).to.equal(0); |
| 29 | + expect(part.phrases).to.be.an('array').that.is.empty; |
| 30 | + expect(part.tatums).to.equal(0.0625); |
| 31 | + part.metro.setBPM(120); |
| 32 | + expect(part.metro).to.have.property('bpm').to.equal(120); |
| 33 | + expect(part.metro).to.have.property('clock'); |
| 34 | + expect(part.metro).to.have.property('syncedParts').to.be.an('array'); |
| 35 | + }); |
| 36 | + it('can be initialized with steps and beat length', function () { |
| 37 | + let part = new p5.Part(16); |
| 38 | + expect(part.length).to.equal(16); |
| 39 | + part = new p5.Part(8, 1 / 16); |
| 40 | + expect(part.length).to.equal(8); |
| 41 | + expect(part.tatums).to.equal(1 / 16); |
| 42 | + }); |
| 43 | + describe('methods', function () { |
| 44 | + it('can set BPM to metro', function () { |
| 45 | + let part = new p5.Part(); |
| 46 | + part.setBPM(300); |
| 47 | + expect(part.metro).to.have.property('bpm').to.equal(300); |
| 48 | + part.setBPM(150, 0.1); |
| 49 | + expect(part.metro).to.have.property('bpm').to.equal(150); |
| 50 | + }); |
| 51 | + it('can get BPM of metro', function () { |
| 52 | + let part = new p5.Part(); |
| 53 | + part.setBPM(600); |
| 54 | + expect(part.getBPM()).to.equal(600); |
| 55 | + }); |
| 56 | + it('can be started and stopped', function (done) { |
| 57 | + let ticks; |
| 58 | + let part = new p5.Part(); |
| 59 | + part.setBPM(600); |
| 60 | + part.start(); |
| 61 | + setTimeout(() => { |
| 62 | + ticks = part.metro.metroTicks; |
| 63 | + part.stop(); |
| 64 | + expect(part.partStep).to.equal(0); |
| 65 | + expect(ticks).to.not.equal(0); |
| 66 | + setTimeout(() => { |
| 67 | + expect(part.metro.metroTicks).to.equal(ticks); |
| 68 | + done(); |
| 69 | + }, 100); |
| 70 | + }, 1000); |
| 71 | + }); |
| 72 | + it('can be started and stopped with a delay', function (done) { |
| 73 | + let ticks; |
| 74 | + let part = new p5.Part(); |
| 75 | + part.setBPM(600); |
| 76 | + part.start(0.1); |
| 77 | + expect(part.metro.metroTicks).to.be.zero; |
| 78 | + setTimeout(() => { |
| 79 | + ticks = part.metro.metroTicks; |
| 80 | + part.stop(0.15); |
| 81 | + expect(ticks).to.not.equal(0); |
| 82 | + setTimeout(() => { |
| 83 | + expect(part.metro.metroTicks).to.not.equal(ticks); |
| 84 | + }, 140); |
| 85 | + setTimeout(() => { |
| 86 | + ticks = part.metro.metroTicks; |
| 87 | + setTimeout(() => { |
| 88 | + expect(part.metro.metroTicks).to.equal(ticks); |
| 89 | + done(); |
| 90 | + }, 100); |
| 91 | + }, 200); |
| 92 | + }, 1000); |
| 93 | + }); |
| 94 | + it('can be started and paused', function (done) { |
| 95 | + let ticks; |
| 96 | + let part = new p5.Part(); |
| 97 | + part.setBPM(600); |
| 98 | + expect(part.metro.syncedParts.length).to.equal(0); |
| 99 | + part.start(); |
| 100 | + expect(part.isPlaying).to.be.true; |
| 101 | + expect(part.metro.syncedParts.length).to.equal(1); |
| 102 | + setTimeout(() => { |
| 103 | + ticks = part.metro.metroTicks; |
| 104 | + part.pause(); |
| 105 | + expect(ticks).to.not.equal(0); |
| 106 | + setTimeout(() => { |
| 107 | + expect(part.metro.metroTicks).to.equal(ticks); |
| 108 | + done(); |
| 109 | + }, 100); |
| 110 | + }, 1000); |
| 111 | + }); |
| 112 | + it('can start and stop looping', function () { |
| 113 | + let part = new p5.Part(); |
| 114 | + expect(part.looping).to.be.false; |
| 115 | + part.loop(); |
| 116 | + expect(part.looping).to.be.true; |
| 117 | + expect(part.isPlaying).to.be.true; |
| 118 | + part.noLoop(); |
| 119 | + expect(part.looping).to.be.false; |
| 120 | + }); |
| 121 | + it('can add a phrase with 1 or 3 arguments', function () { |
| 122 | + let part = new p5.Part(1); |
| 123 | + expect(part.length).to.equal(1); |
| 124 | + part.addPhrase('kick', () => {}, [0, 1, 0, 0]); |
| 125 | + expect(part.phrases.length).to.equal(1); |
| 126 | + expect(part.phrases[0].sequence).to.deep.equal([0, 1, 0, 0]); |
| 127 | + expect(part.length).to.equal(4); |
| 128 | + part.addPhrase(new p5.Phrase('snare', () => {}, [0, 1, 0, 1, 1])); |
| 129 | + expect(part.phrases.length).to.equal(2); |
| 130 | + expect(part.phrases[1].sequence).to.deep.equal([0, 1, 0, 1, 1]); |
| 131 | + expect(part.length).to.equal(5); |
| 132 | + }); |
| 133 | + it('can remove a phrase', function () { |
| 134 | + let part = new p5.Part(); |
| 135 | + part.addPhrase('kick', () => {}, [1, 0, 0]); |
| 136 | + part.addPhrase('bbox', () => {}, [0, 0, 0, 1]); |
| 137 | + part.addPhrase('snare', () => {}, [0, 1, 1, 1]); |
| 138 | + part.addPhrase('kick', () => {}, [0, 1]); |
| 139 | + expect(part.phrases.length).to.equal(4); |
| 140 | + part.removePhrase('kick'); |
| 141 | + expect(part.phrases.length).to.equal(2); |
| 142 | + let count = 0; |
| 143 | + for (var i in part.phrases) { |
| 144 | + if (part.phrases[i].name === 'kick') count++; |
| 145 | + } |
| 146 | + expect(count).to.be.zero; |
| 147 | + }); |
| 148 | + it('can get a phrase', function () { |
| 149 | + let part = new p5.Part(); |
| 150 | + part.addPhrase('bbox', () => {}, [1, 0]); |
| 151 | + part.addPhrase('bass', () => {}, [0, 1, 0, 1, 1]); |
| 152 | + part.addPhrase('kick', () => {}, [0, 0, 1]); |
| 153 | + part.addPhrase('drum', () => {}, [0, 1, 1]); |
| 154 | + part.addPhrase('bass', () => {}, [1, 1]); |
| 155 | + let phrase = part.getPhrase('bass'); |
| 156 | + expect(phrase.name).to.equal('bass'); |
| 157 | + expect(phrase.sequence).to.deep.equal([0, 1, 0, 1, 1]); |
| 158 | + }); |
| 159 | + it('can replace the sequence of a given phrase name', function () { |
| 160 | + let part = new p5.Part(); |
| 161 | + part.addPhrase('drum', () => {}, [0, 1]); |
| 162 | + part.addPhrase('kick', () => {}, [0, 0, 0, 0, 1]); |
| 163 | + part.addPhrase('bass', () => {}, [0, 0, 1]); |
| 164 | + part.addPhrase('bbox', () => {}, [1, 0, 0, 0, 1]); |
| 165 | + let phrase = part.getPhrase('drum'); |
| 166 | + expect(phrase.sequence).to.deep.equal([0, 1]); |
| 167 | + part.replaceSequence('drum', [1, 0, 1, 0, 1]); |
| 168 | + phrase = part.getPhrase('drum'); |
| 169 | + expect(phrase.sequence).to.deep.equal([1, 0, 1, 0, 1]); |
| 170 | + }); |
| 171 | + it('can increment step', function (done) { |
| 172 | + let part = new p5.Part(3); |
| 173 | + let count = 0; |
| 174 | + part.onStep((a) => (count += a)); |
| 175 | + part.incrementStep(2); |
| 176 | + expect(count).to.equal(2); |
| 177 | + expect(part.partStep).to.equal(1); |
| 178 | + part.incrementStep(3); |
| 179 | + expect(count).to.equal(5); |
| 180 | + expect(part.partStep).to.equal(2); |
| 181 | + part.incrementStep(5); |
| 182 | + expect(count).to.equal(5); |
| 183 | + done(); |
| 184 | + }); |
| 185 | + it('onended is called on the last incrementStep call', function (done) { |
| 186 | + let part = new p5.Part(2); |
| 187 | + part.onStep(() => {}); |
| 188 | + part.incrementStep(); |
| 189 | + part.loop(); |
| 190 | + part.looping = false; |
| 191 | + part.incrementStep(); |
| 192 | + expect(part.partStep).to.equal(0); |
| 193 | + part = new p5.Part(2); |
| 194 | + part.onStep(() => {}); |
| 195 | + part.incrementStep(); |
| 196 | + part.loop(); |
| 197 | + part.noLoop(); |
| 198 | + part.incrementStep(); |
| 199 | + expect(part.partStep).to.equal(0); |
| 200 | + expect(part.isPlaying).to.be.false; |
| 201 | + done(); |
| 202 | + }); |
| 203 | + }); |
| 204 | + }); |
| 205 | + describe('Score', function () { |
| 206 | + it('can be created with no arguments', function () { |
| 207 | + let score = new p5.Score(); |
| 208 | + expect(score.looping).to.be.false; |
| 209 | + expect(score.parts).to.be.a('array').that.is.empty; |
| 210 | + expect(score.currentPart).to.be.zero; |
| 211 | + }); |
| 212 | + it('can be created with parts as the arguments', function () { |
| 213 | + let sequences = [ |
| 214 | + [1, 0, 1], |
| 215 | + [1, 0, 0, 1], |
| 216 | + [1, 0, 0, 0, 1], |
| 217 | + ]; |
| 218 | + let part1 = new p5.Part(); |
| 219 | + let part2 = new p5.Part(); |
| 220 | + let part3 = new p5.Part(); |
| 221 | + part1.addPhrase('drum', () => {}, sequences[0]); |
| 222 | + part2.addPhrase('kick', () => {}, sequences[1]); |
| 223 | + part3.addPhrase('bbox', () => {}, sequences[2]); |
| 224 | + let score = new p5.Score(part1, part2, part3); |
| 225 | + expect(score.parts[0].nextPart.phrases[0].sequence).deep.equal( |
| 226 | + sequences[1] |
| 227 | + ); |
| 228 | + expect(score.parts[1].nextPart.phrases[0].sequence).deep.equal( |
| 229 | + sequences[2] |
| 230 | + ); |
| 231 | + }); |
| 232 | + describe('methods', function () { |
| 233 | + it('can be started and stopped', function (done) { |
| 234 | + let part1 = new p5.Part(); |
| 235 | + let part2 = new p5.Part(); |
| 236 | + let score = new p5.Score(part1, part2); |
| 237 | + let ticks; |
| 238 | + score.start(); |
| 239 | + expect(score.parts[0].isPlaying).to.be.true; |
| 240 | + setTimeout(() => { |
| 241 | + ticks = score.parts[0].metro.metroTicks; |
| 242 | + score.stop(); |
| 243 | + expect(score.parts[0].partStep).to.equal(0); |
| 244 | + expect(score.currentPart).to.be.zero; |
| 245 | + expect(score.scoreStep).to.be.zero; |
| 246 | + expect(ticks).to.not.equal(0); |
| 247 | + setTimeout(() => { |
| 248 | + expect(score.parts[0].metro.metroTicks).to.equal(ticks); |
| 249 | + done(); |
| 250 | + }, 100); |
| 251 | + }, 1000); |
| 252 | + }); |
| 253 | + it('can start and stop looping', function (done) { |
| 254 | + let part1 = new p5.Part(); |
| 255 | + let part2 = new p5.Part(); |
| 256 | + let score = new p5.Score(part1, part2); |
| 257 | + score.loop(); |
| 258 | + expect(score.parts[0].isPlaying).to.be.true; |
| 259 | + expect(score.looping).to.be.true; |
| 260 | + score.noLoop(); |
| 261 | + expect(score.looping).to.be.false; |
| 262 | + done(); |
| 263 | + }); |
| 264 | + it('can play next part when ended', function () { |
| 265 | + let part1 = new p5.Part(1); |
| 266 | + let part2 = new p5.Part(1); |
| 267 | + let score = new p5.Score(part1, part2); |
| 268 | + score.start(); |
| 269 | + expect(score.currentPart).to.be.zero; |
| 270 | + part1.incrementStep(); |
| 271 | + expect(score.scoreStep).to.be.zero; |
| 272 | + expect(score.parts[0].isPlaying).to.be.false; |
| 273 | + expect(score.parts[1].isPlaying).to.be.true; |
| 274 | + expect(score.currentPart).to.equal(1); |
| 275 | + part2.incrementStep(); |
| 276 | + expect(score.parts[0].isPlaying).to.be.false; |
| 277 | + }); |
| 278 | + it('can be paused', function () { |
| 279 | + let part1 = new p5.Part(1); |
| 280 | + let part2 = new p5.Part(1); |
| 281 | + let score = new p5.Score(part1, part2); |
| 282 | + score.start(); |
| 283 | + part1.incrementStep(); |
| 284 | + expect(score.parts[0].isPlaying).to.be.false; |
| 285 | + expect(score.parts[1].isPlaying).to.be.true; |
| 286 | + score.pause(); |
| 287 | + expect(score.currentPart).to.equal(1); |
| 288 | + }); |
| 289 | + it('can reset a part', function () { |
| 290 | + let sequences = [ |
| 291 | + [1, 1], |
| 292 | + [1, 0, 1], |
| 293 | + ]; |
| 294 | + let part1 = new p5.Part(2); |
| 295 | + part1.addPhrase('drum', () => {}, sequences[0]); |
| 296 | + part1.addPhrase('kick', () => {}, sequences[1]); |
| 297 | + let score = new p5.Score(part1); |
| 298 | + score.start(); |
| 299 | + part1.incrementStep(); |
| 300 | + part1.phrases[0].phraseStep = 1; |
| 301 | + part1.phrases[1].phraseStep = 2; |
| 302 | + score.resetPart(0); |
| 303 | + expect(part1.partStep).to.be.zero; |
| 304 | + expect(part1.phrases[0].phraseStep).to.be.zero; |
| 305 | + expect(part1.phrases[1].phraseStep).to.be.zero; |
| 306 | + }); |
| 307 | + it('can reset all parts', function () { |
| 308 | + let sequences = [ |
| 309 | + [1, 1], |
| 310 | + [1, 0, 1], |
| 311 | + [1, 0, 0, 1], |
| 312 | + ]; |
| 313 | + let part1 = new p5.Part(2); |
| 314 | + let part2 = new p5.Part(1); |
| 315 | + part1.addPhrase('drum', () => {}, sequences[0]); |
| 316 | + part1.addPhrase('kick', () => {}, sequences[1]); |
| 317 | + part2.addPhrase('bbox', () => {}, sequences[2]); |
| 318 | + let score = new p5.Score(part1, part2); |
| 319 | + score.start(); |
| 320 | + part1.incrementStep(); |
| 321 | + part1.incrementStep(); |
| 322 | + part2.incrementStep(); |
| 323 | + part1.phrases[0].phraseStep = 1; |
| 324 | + part1.phrases[1].phraseStep = 2; |
| 325 | + score.resetParts(); |
| 326 | + expect(part1.partStep).to.be.zero; |
| 327 | + expect(part1.phrases[0].phraseStep).to.be.zero; |
| 328 | + expect(part1.phrases[1].phraseStep).to.be.zero; |
| 329 | + expect(part2.partStep).to.be.zero; |
| 330 | + expect(part2.phrases[0].phraseStep).to.be.zero; |
| 331 | + }); |
| 332 | + it('can setBPM for all parts', function () { |
| 333 | + let part1 = new p5.Part(); |
| 334 | + let part2 = new p5.Part(); |
| 335 | + let part3 = new p5.Part(); |
| 336 | + let score = new p5.Score(part1, part2, part3); |
| 337 | + part1.setBPM(300); |
| 338 | + score.setBPM(150, 0.2); |
| 339 | + expect(part1.metro.bpm).to.equal(150); |
| 340 | + expect(part2.metro.bpm).to.equal(150); |
| 341 | + expect(part3.metro.bpm).to.equal(150); |
| 342 | + }); |
| 343 | + }); |
| 344 | + }); |
| 345 | +}); |
0 commit comments