-
Notifications
You must be signed in to change notification settings - Fork 59
/
piece.js
178 lines (161 loc) · 4.79 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { Scale, Note } from 'tonal';
import Tone from 'tone';
import fetchSpecFile from '@generative-music/samples.generative.fm';
const OCTAVES = [3, 4, 5, 6];
const MAX_STEP_DISTANCE = 2;
const MAX_PHRASE_LENGTH = 3;
const PHRASE_P_BASE = 0.5;
const pitchClasses = Scale.notes('C', 'major');
const notes = OCTAVES.reduce(
(allNotes, octave) =>
allNotes.concat(pitchClasses.map(pc => `${pc}${octave}`)),
[]
);
const getNextNotesForNote = note => {
const index = notes.findIndex(n => n === note);
const lowestIndex = Math.max(0, index - MAX_STEP_DISTANCE);
return notes
.slice(lowestIndex, index)
.concat(notes.slice(index + 1, index + MAX_STEP_DISTANCE + 1));
};
const generatePhrase = (
phrase = [notes[Math.floor(Math.random() * notes.length)]]
) => {
if (
phrase.length < MAX_PHRASE_LENGTH &&
Math.random() < PHRASE_P_BASE ** phrase.length
) {
const lastNote = phrase[phrase.length - 1];
const possibleNextNotes = getNextNotesForNote(lastNote);
return generatePhrase(
phrase.concat([
possibleNextNotes[Math.floor(Math.random() * possibleNextNotes.length)],
])
);
}
return phrase;
};
const getSampledInstrument = samplesByNote =>
new Promise(resolve => {
const instrument = new Tone.Sampler(samplesByNote, {
onload: () => resolve(instrument),
});
});
const makePiece = ({
audioContext,
destination,
preferredFormat,
sampleSource = {},
}) =>
fetchSpecFile(sampleSource.baseUrl, sampleSource.specFilename)
.then(({ samples }) => {
if (Tone.context !== audioContext) {
Tone.setContext(audioContext);
}
return getSampledInstrument(samples['vsco2-piano-mf'][preferredFormat]);
})
.then(piano => {
const reverb = new Tone.Freeverb({ roomSize: 0.7 });
const delayFudge = Math.random() * 3;
const delay = new Tone.FeedbackDelay({
wet: 0.5,
delayTime: 5 + delayFudge,
feedback: 0.8 - delayFudge / 100,
});
const chorusLfo = new Tone.LFO({
frequency: Math.random() / 100,
phase: 90,
});
chorusLfo.start();
const chorus = new Tone.Chorus({ wet: 0 });
chorusLfo.connect(chorus.wet);
const autoFilter = new Tone.AutoFilter({
frequency: Math.random() / 100,
baseFrequency: 250,
octaves: 5,
type: 'sine',
});
autoFilter.start();
const pitchLfo = new Tone.LFO({
frequency: Math.random() / 100,
phase: 90,
});
pitchLfo.start();
const pitchShift = new Tone.PitchShift({ pitch: 7 });
pitchLfo.connect(pitchShift.wet);
const tremoloFrequencyLfo = new Tone.LFO({
frequency: Math.random() / 100,
phase: 90,
min: 0.1,
max: 10,
});
const tremoloLfo = new Tone.LFO({
frequency: Math.random() / 100,
phase: 90,
});
tremoloFrequencyLfo.start();
tremoloLfo.start();
const tremolo = new Tone.Tremolo();
tremoloFrequencyLfo.connect(tremolo.frequency);
tremoloLfo.connect(tremolo.wet);
tremolo.start();
const compressor = new Tone.Compressor();
piano.chain(
pitchShift,
delay,
reverb,
chorus,
autoFilter,
tremolo,
compressor,
destination
);
const synth = new Tone.MonoSynth({
oscillator: { type: 'sine' },
volume: -45,
envelope: { release: 3, attack: 0.5 },
}).chain(delay);
const playPhrase = (iterationsSinceLastSub = 6) => {
const phrase = generatePhrase();
const generateSub = iterationsSinceLastSub > 5 && Math.random() < 0.02;
if (generateSub) {
const lowestNoteMidi = phrase
.map(note => Note.midi(note))
.reduce(
(lowestFound, noteMidi) => Math.min(lowestFound, noteMidi),
Infinity
);
const lowestNote = Note.fromMidi(lowestNoteMidi);
const lowestNotePitchClass = Note.pc(lowestNote);
synth.triggerAttackRelease(`${lowestNotePitchClass}1`, 3);
}
phrase.forEach((note, i) => {
if (Tone.context.state !== 'running') {
Tone.context.resume();
}
piano.triggerAttack(note, `+${i * 1.5}`);
});
Tone.Transport.scheduleOnce(() => {
playPhrase(generateSub ? 0 : iterationsSinceLastSub + 1);
}, `+${Math.random() * 10 + 10}`);
};
playPhrase();
return () => {
[
reverb,
delay,
chorusLfo,
chorus,
autoFilter,
pitchLfo,
pitchShift,
tremoloFrequencyLfo,
tremoloLfo,
tremolo,
compressor,
piano,
synth,
].forEach(node => node.dispose());
};
});
export default makePiece;