-
Notifications
You must be signed in to change notification settings - Fork 60
/
piece.js
153 lines (141 loc) · 4.13 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
import Tone from 'tone';
import { Note } from 'tonal';
import * as Range from 'tonal-range';
import fetchSpecFile from '@generative-music/samples.generative.fm/browser-client';
const MAX_STEP_DISTANCE = 3;
const MAX_PHRASE_LENGTH = 3;
const PHRASE_P_BASE = 0.5;
const cMajorRange = Range.scale(['C', 'D', 'E', 'F', 'G', 'A', 'B']);
const getSampledInstrument = samplesByNote =>
new Promise(resolve => {
const instrument = new Tone.Sampler(samplesByNote, {
onload: () => resolve(instrument),
});
});
const getNextNotesForNote = (notes, 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 = (
notes,
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(notes, lastNote);
return generatePhrase(
notes,
phrase.concat([
possibleNextNotes[Math.floor(Math.random() * possibleNextNotes.length)],
])
);
}
return phrase;
};
const getPossibleNotesForInstrument = (
samplesByNote,
octaves = [0, 1, 2, 3, 4, 5, 6, 7, 8]
) => {
const sampledNotes = Object.keys(samplesByNote);
const lowestNote = sampledNotes.reduce(
(currentLowest, note) =>
Note.freq(note) < Note.freq(currentLowest) ? note : currentLowest,
Infinity
);
const highestNote = sampledNotes.reduce(
(currentHighest, note) =>
Note.freq(note) > Note.freq(currentHighest) ? note : currentHighest,
-Infinity
);
return cMajorRange([lowestNote, highestNote]).filter(note =>
octaves.includes(Note.oct(note))
);
};
const instrumentConfigs = {
'vsco2-piano-mf': {
isSingleNote: false,
secondsBetweenNotes: 2,
octaves: [2, 3, 4, 5, 6],
},
'vsco2-contrabass-susvib': {
isSingleNote: true,
},
'vsco2-violin-arcvib': {
isSingleNote: false,
secondsBetweenNotes: 8,
},
};
const makeInstrumentComponent = (
samplesByNote,
instrumentName,
connectToNode
) => {
const start = instrument => {
instrument.connect(connectToNode);
const { isSingleNote, secondsBetweenNotes, notes } = instrumentConfigs[
instrumentName
];
const playPhrase = () => {
const phrase = generatePhrase(notes);
if (isSingleNote) {
instrument.triggerAttack(phrase[0], `+1`);
} else {
phrase.forEach((note, i) => {
instrument.triggerAttack(note, `+${i * secondsBetweenNotes + 1}`);
});
}
};
Tone.Transport.scheduleRepeat(() => {
playPhrase();
}, Math.random() * 10 + 10);
};
return getSampledInstrument(samplesByNote).then(instrument => {
start(instrument);
return instrument;
});
};
const makePiece = ({
audioContext,
destination,
preferredFormat,
sampleSource = {},
}) =>
fetchSpecFile(sampleSource.baseUrl, sampleSource.specFilename).then(
({ samples }) => {
if (Tone.context !== audioContext) {
Tone.setContext(audioContext);
}
const delay = new Tone.FeedbackDelay({
feedback: 0.3 + Math.random() / 30,
wet: 0.5,
delayTime: 10 + Math.random() * 2,
}).connect(destination);
const reverb = new Tone.Freeverb({ roomSize: 0.6 }).connect(delay);
return Promise.all(
Reflect.ownKeys(instrumentConfigs).map(instrumentName => {
const instrumentSamplesByNote =
samples[instrumentName][preferredFormat];
instrumentConfigs[
instrumentName
].notes = getPossibleNotesForInstrument(
instrumentSamplesByNote,
instrumentConfigs[instrumentName].octaves
);
return makeInstrumentComponent(
instrumentSamplesByNote,
instrumentName,
reverb
);
})
).then(instruments => () => {
instruments.concat(delay, reverb).forEach(node => node.dispose());
});
}
);
export default makePiece;