-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathInfiniteBeats.js
More file actions
90 lines (75 loc) · 2.38 KB
/
InfiniteBeats.js
File metadata and controls
90 lines (75 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// This file contains code derived from EternalJukebox (https://github.com/UnderMybrella/EternalJukebox/).
// Copyright 2021 UnderMybrella
// See the LICENSE file for the full MIT license terms.
import remixTrack from './remixTrack.js';
import calculateNearestNeighbors from './calculateNearestNeighbors.js';
// configs for chances to branch
const randomBranchChanceDelta = .018;
const maxRandomBranchChance = .5
const minRandomBranchChance = .18
function defaultRandom() {
return Math.random();
}
export default class InfiniteBeats {
constructor(spotifyAnalysis, random=defaultRandom) {
let track = {
analysis: {
sections: spotifyAnalysis.sections,
bars: spotifyAnalysis.bars,
beats: spotifyAnalysis.beats,
tatums: spotifyAnalysis.tatums,
segments: spotifyAnalysis.segments,
},
};
// Deep clone track since we're going to modify it.
track = JSON.parse(JSON.stringify(track));
this._curRandomBranchChance = minRandomBranchChance
this._random = random;
this._beats = track.analysis.beats;
remixTrack(track);
const { lastBranchPoint } = calculateNearestNeighbors(track);
this._lastBranchPoint = lastBranchPoint;
}
getNextBeat(curBeat) {
// console.log('next');
if (!curBeat) {
return this._beats[0];
} else {
const nextIndex = curBeat.which + 1;
if (nextIndex < 0) {
return this._beats[0];
} else if (nextIndex >= this._beats.length) {
return undefined;
} else {
return this._selectRandomNextBeat(this._beats[nextIndex]);
}
}
}
_selectRandomNextBeat(seed) {
if (seed.neighbors.length === 0) {
return seed;
} else if (this._shouldRandomBranch(seed)) {
var next = seed.neighbors.shift();
seed.neighbors.push(next);
var beat = next.dest;
return beat;
} else {
return seed;
}
}
_shouldRandomBranch(q) {
if (q.which == this._lastBranchPoint) {
return true;
}
// return true; // TEST, remove
this._curRandomBranchChance += randomBranchChanceDelta;
if (this._curRandomBranchChance > maxRandomBranchChance) {
this._curRandomBranchChance = maxRandomBranchChance;
}
var shouldBranch = this._random() < this._curRandomBranchChance;
if (shouldBranch) {
this._curRandomBranchChance = minRandomBranchChance;
}
return shouldBranch;
}
}