-
Notifications
You must be signed in to change notification settings - Fork 60
/
piece.js
161 lines (149 loc) · 4.92 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
import Tone from 'tone';
import { Note, Distance } from 'tonal';
import fetchSpecFile from '@generative-music/samples.generative.fm';
const NOTES = ['C2', 'E2', 'G2', 'C3', 'E3', 'G3', 'C4', 'E4', 'G4'];
const findClosest = (samplesByNote, note) => {
const noteMidi = Note.midi(note);
const maxInterval = 96;
let interval = 0;
while (interval <= maxInterval) {
const higherNote = Note.fromMidi(noteMidi + interval);
if (samplesByNote[higherNote]) {
return higherNote;
}
const lowerNote = Note.fromMidi(noteMidi - interval);
if (samplesByNote[lowerNote]) {
return lowerNote;
}
interval += 1;
}
return note;
};
const getBuffers = samplesByNote =>
new Promise(resolve => {
const buffers = new Tone.Buffers(samplesByNote, {
onload: () => resolve(buffers),
});
});
const makePiece = ({
audioContext,
destination,
preferredFormat,
sampleSource = {},
}) =>
fetchSpecFile(sampleSource.baseUrl, sampleSource.specFilename).then(
({ samples }) => {
if (Tone.context !== audioContext) {
Tone.setContext(audioContext);
}
const masterVol = new Tone.Volume(-5).connect(destination);
const guitarSamples = samples['acoustic-guitar'][preferredFormat];
const hum1Samples = samples['alex-hum-1'][preferredFormat];
const hum2Samples = samples['alex-hum-2'][preferredFormat];
const activeSources = [];
const makePlayNote = (
buffers,
samplesByNote,
bufferDestination,
opts = {}
) => note => {
const closestSampledNote = findClosest(samplesByNote, note);
const difference = Distance.semitones(closestSampledNote, note);
const buffer = buffers.get(closestSampledNote);
const pitchShiftSemitones = Math.random() < 1 ? 0 : 12;
const playbackRate = Tone.intervalToFrequencyRatio(
difference - pitchShiftSemitones
);
const source = new Tone.BufferSource(buffer)
.set(
Object.assign({}, opts, {
playbackRate,
onended: () => {
const i = activeSources.indexOf(source);
if (i >= 0) {
activeSources.splice(i, 1);
}
},
})
)
.connect(bufferDestination);
source.start(
`+1`,
0,
buffer.duration / playbackRate - (opts.fadeOut || 0)
);
};
return Promise.all([
getBuffers(guitarSamples),
getBuffers(hum1Samples),
getBuffers(hum2Samples),
new Tone.Reverb(10).generate(),
]).then(([guitarBuffers, hum1Buffers, hum2Buffers, reverb]) => {
const compressor = new Tone.Compressor().connect(reverb);
const humVolume = new Tone.Volume(-10).connect(compressor);
const playGuitar = makePlayNote(
guitarBuffers,
guitarSamples,
compressor
);
const playHum1 = makePlayNote(hum1Buffers, hum1Samples, humVolume, {
fadeIn: 3,
fadeOut: 3,
curve: 'linear',
});
const playHum2 = makePlayNote(hum2Buffers, hum2Samples, humVolume, {
fadeIn: 3,
fadeOut: 3,
curve: 'linear',
});
const lastHumTime = new Map();
const playHums = note => {
const now = Tone.now();
if (!lastHumTime.has(note) || now - lastHumTime.get(note) > 30) {
[playHum1, playHum2].forEach(playHum => playHum(note));
lastHumTime.set(note, now);
}
};
reverb.connect(masterVol);
reverb.set({ wet: 0.5 });
const firstDelays = NOTES.map(
note => Math.random() * 20 * (Note.pc(note) === 'E' ? 3 : 1)
);
const minFirstDelay = Math.min(...firstDelays);
NOTES.forEach((note, i) => {
const pc = Note.pc(note);
const play = (
time = (Math.random() * 20 + 5) * (pc === 'E' ? 3 : 1)
) => {
Tone.Transport.scheduleOnce(() => {
const octave = Note.oct(note);
if (
(octave === 3 || (octave === 2 && pc === 'G')) &&
Math.random() < 0.1
) {
playHums(note);
} else if (Math.random() < 0.1) {
playHums('E3');
}
playGuitar(note);
play();
}, `+${time}`);
};
play(firstDelays[i] - minFirstDelay);
});
return () => {
[
guitarBuffers,
hum1Buffers,
hum2Buffers,
reverb,
compressor,
humVolume,
masterVol,
...activeSources,
].forEach(node => node.dispose());
};
});
}
);
export default makePiece;