-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntervalSetBinary.ts
80 lines (69 loc) · 2.04 KB
/
IntervalSetBinary.ts
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
import { musicTheory } from "./../../Data/musicTheory";
import { IntervalSet } from "./IntervalSet";
const divisions = musicTheory.octaveDivisions;
/**
* Static functions to deal with integer numbers as binary representations of
* IntervalSets
*/
export class IntervalSetBinary {
/**
* Convert one interval ordinal to a binary representation of that interval.
*/
static fromOrdinal(ordinal: number): number {
return Math.pow(2, ordinal);
}
/**
* Convert an array of interval ordinals to a binary representation of those
* intervals.
*/
static fromOrdinals(ordinals: number[]): number {
return ordinals
.map(IntervalSetBinary.fromOrdinal)
.reduce((a, b) => a + b, 0);
}
/**
* Convert a binary representation of intervals to an array of interval
* ordinals.
*/
static toOrdinals(binary: number): number[] {
let result: number[] = [];
IntervalSet.chromaticOrdinals.forEach(ordinal => {
if (IntervalSetBinary.containsOrdinal(binary, ordinal)) {
result.push(ordinal);
}
});
return result;
}
/**
* Return true if the given binary interval set contains the given ordinal.
*/
static containsOrdinal(binary: number, ordinal: number): boolean {
return (binary & IntervalSetBinary.fromOrdinal(ordinal)) > 0;
}
/**
* Return the interval set to represent the chromatic scale (all notes).
*/
static get chromatic() {
return Math.pow(2, divisions) - 1
}
/**
* Apply a bit mask to a binary interval representation.
*/
static mask(binary: number, mask: number): number {
return binary & mask;
}
/**
* Return a binary interval set with intervals eliminated that fall outside
* the chromatic set.
*/
static onlyChromatic(binary: number): number {
return IntervalSetBinary.mask(binary, IntervalSetBinary.chromatic);
}
/**
* Return a binary interval set with the tonal center activated even if
* it's inactive in the given set.
*/
static guaranteedToContainTonalCenter(binary: number): number {
return binary | 1;
}
}