-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathpiece.js
137 lines (118 loc) · 3.41 KB
/
piece.js
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import * as Tone from 'tone';
import {
createPrerenderableSampler,
wrapActivate,
transpose,
P1,
M2,
M3,
P4,
P5,
M6,
M7,
getRandomElement,
toss,
} from '@generative-music/utilities';
import { sampleNames } from '../trees.gfm.manifest.json';
import gainAdjustments from '../../../normalize/gain.json';
const MAJOR_SCALE_INTERVALS = [P1, M2, M3, P4, P5, M6, M7];
const OCTAVES = [3, 4, 5, 6];
const getOffsetProgression = () => {
const progression = [];
const startingStep = window.generativeMusic.rng() < 0.5 ? 0 : 1;
const largestStep = window.generativeMusic.rng() * (5 - startingStep) + startingStep;
for (
let step = startingStep;
step <= largestStep;
step += window.generativeMusic.rng() < 0.5 ? 1 : 2
) {
const chord = [];
for (let i = step; i >= 0; i -= 2) {
if (i === 0) {
chord.push(i);
} else {
chord.push(i, -i);
}
}
progression.push(chord);
}
return progression;
};
const makeOffsetProgressionToIndiciesProgression = (
notes,
startingIndex
) => offsetProgression =>
offsetProgression.map(chord =>
chord
.map(offset => startingIndex + offset)
.filter(index => index >= 0 && index < notes.length)
);
const makeIndiciesProgressionToNoteProgression = notes => indiciesProgression =>
indiciesProgression.map(chord => chord.map(index => notes[index]));
const pipe = (...fns) => x => fns.reduce((y, fn) => fn(y), x);
const getProgression = notes =>
pipe(
getOffsetProgression,
makeOffsetProgressionToIndiciesProgression(
notes,
Math.floor(window.generativeMusic.rng() * notes.length)
),
makeIndiciesProgressionToNoteProgression(notes)
)();
const playProgression = (piano, notes) => {
const progression = getProgression(notes);
const perChordDelay = window.generativeMusic.rng() * 3 + 2;
progression.forEach((chord, i) => {
chord.forEach(note =>
piano.triggerAttack(note, `+${i * perChordDelay + window.generativeMusic.rng() / 10}`)
);
});
Tone.Transport.scheduleOnce(() => {
playProgression(piano, notes);
}, `+${window.generativeMusic.rng() * 3 + (progression.length + 1) * perChordDelay}`);
};
const activate = async ({ sampleLibrary, onProgress }) => {
const samples = await sampleLibrary.request(Tone.context, sampleNames);
const getPianoDestination = () =>
Promise.resolve(new Tone.Freeverb({ roomSize: 0.6 }).toDestination());
const [[renderedInstrumentName, sourceInstrumentName]] = sampleNames;
const piano = await createPrerenderableSampler({
samples,
sourceInstrumentName,
renderedInstrumentName,
sampleLibrary,
onProgress,
notes: toss(['C', 'E', 'G'], OCTAVES).concat(['B6']),
additionalRenderLength: 0,
getDestination: getPianoDestination,
});
const schedule = ({ destination }) => {
piano.connect(destination);
const tonic = getRandomElement([
'C',
'C#',
'D',
'D#',
'E',
'F',
'F#',
'G',
'G#',
'A',
'A#',
'B',
]);
const scalePitchClasses = MAJOR_SCALE_INTERVALS.map(transpose(tonic));
const notes = toss(scalePitchClasses, OCTAVES);
playProgression(piano, notes);
return () => {
piano.releaseAll(0);
};
};
const deactivate = () => {
piano.dispose();
};
return [deactivate, schedule];
};
const GAIN_ADJUSTMENT = gainAdjustments['trees'];
export default wrapActivate(activate, { gain: GAIN_ADJUSTMENT });