-
Notifications
You must be signed in to change notification settings - Fork 0
/
nice4.js
249 lines (209 loc) · 5.22 KB
/
nice4.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
const Speaker = require('speaker');
const Readable = require('stream').Readable;
const util = require('util');
// Create a range array.
Array.range = (length) => {
let result = new Array(length);
for (let i = 0; i < length; i += 1) {
result[i] = i;
}
return result;
};
module.exports.makeAudioOutput = () => new Speaker({
channels: 1,
bitDepth: 32,
sampleRate: 44100,
signed: true,
float: true,
});
let out = module.exports.makeAudioOutput()
const Pitch = (str) => {
str = str.toLowerCase();
let i = 0;
// Read in the note name. Use C as the default if it cannot be read.
let note = i < str.length ? str[i] : null;
if (['a', 'b', 'c', 'd', 'e', 'f', 'g'].includes(note)) {
i += 1;
} else {
note = 'c';
}
// Read in the accidental. Accidentals are optional so only advance the index
// if we found a valid one.
let accidental = i < str.length ? str[i] : null;
if (accidental == '#' || accidental == 'b') {
i += 1;
} else {
accidental = null;
}
// Read in the octave, and parse it if present. If it's missing, use a
// default octave of 4.
let octave = i < str.length ? parseInt(str[i]) : null;
if (!octave) {
octave = 4;
}
// Calculate the frequency based on A 440.
const semitoneOffsets = {
'b#': -9, 'c': -9,
'c#': -8, 'db': -8,
'd': -7,
'd#': -6, 'eb': -6,
'e': -5, 'fb': -5,
'e#': -4, 'f': -4,
'f#': -3, 'gb': -3,
'g': -2,
'g#': -1, 'ab': -1,
'a': 0,
'a#': 1, 'bb': 1,
'b': 2, 'cb': 2
}
let noteName = note + (accidental || '');
let frequency = 440 * Math.pow(2, semitoneOffsets[noteName] / 12);
// Adjust the frequency for the octave.
frequency = frequency * Math.pow(2, octave - 4);
return {
frequency: frequency
};
};
const Audio = function (func) {
Readable.call(this);
this.buffer = Buffer.alloc(4);
this.counter = 0;
this.func = func;
};
util.inherits(Audio, Readable);
Audio.prototype._read = function () {
this.buffer.writeFloatLE(this.func(this.counter / 44100), 0);
this.push(this.buffer);
this.counter += 1;
};
// Wave generators.
const sine = (freq) => (time) => {
return Math.sin(freq * 2 * Math.PI * time);
};
const square = (freq) => (time) => {
return (freq * time % 1) < 0.5 ? 1 : 0;
};
const triangle = (freq) => (time) => {
let x = freq * time % 1;
return x < 0.5 ? 4 * x - 1 : 4 * (0.5 - x) + 1;
};
const harmonic = (freq) => {
let components =
Array.range(10)
.map(i => i + 1)
.map(i => freq * i)
.map(f => sine(f));
return (time) =>
components.reduce(
(result, component, i) => result + component(time) / (i + 1),
0);
};
const add = (...signals) => (t) => {
return signals.map(signal => signal(t)).reduce((a, b) => a + b, 0);
};
const sub = (base, ...signals) => (t) => {
return signals.map(signal => signal(t)).reduce((a, b) => a - b, base(t));
};
const mul = (sig, fac) => (t) => {
return sig(t) * fac;
};
let chordSources = [
['Ab', 'C', 'Eb'],
['F', 'Ab', 'C'],
['C', 'Eb', 'G'],
['Bb', 'D', 'F'],
['G', 'Bb', 'D'],
['Ab', 'C', 'Eb'],
['C', 'Eb', 'G'],
['Eb', 'G', 'Bb'],
['Ab', 'C', 'Eb'],
['G', 'Bb', 'D'],
['F', 'Ab', 'C'],
['C', 'Eb', 'G'],
['Bb', 'D', 'F'],
['Ab', 'C', 'Eb'],
['Bb', 'D', 'F'],
['Eb', 'G', 'Bb'],
];
let chordWaves = chordSources.map((sources) => {
let pitches = sources.map(Pitch);
let frequencies = pitches.map(p => p.frequency);
let waves = frequencies.map(square);
return t => waves.reduce((sum, wave) => sum + wave(t), 0);
});
let harms = [];
for (let i = 0; i < 20; i += 1) {
harms.push(harmonic(220 * (i + 1)));
}
let summies = [
// harmonic(220),
harmonic(275),
harmonic(330),
harmonic(440),
// harmonic(550)
];
const perfectFifth = (f) => f * 3 / 2;
const perfectFourth = (f) => f * 4 / 3;
const majorThird = (f) => f * 5 / 4;
const minorThird = (f) => f * 6 / 5;
const weird = (f) => f * 7 / 6;
const majorSecond = (f) => f * 9 / 8;
const minorSecond = (f) => f * 17 / 16;
const oct = (f) => f * 2;
const cents = (c) => (
(f) => f * Math.pow(2, c / 1200)
);
const rat = (r) => (
(f) => f * r
);
const stack = (start, ...intervals) => {
const res = [start];
intervals.forEach(i => {
start = i(start);
res.push(start);
});
return res;
}
const jm3 = rat(6 / 5);
const j3 = rat(5 / 4);
const j4 = rat(4 / 3);
const j5 = rat(3 / 2);
const j6 = rat(5 / 3);
const js7 = rat(7 / 6 * 3 / 2);
const octave = cents(1200);
const semitone = cents(100);
const et2 = cents(200);
const etm3 = cents(300);
const et3 = cents(400);
const et4 = cents(500);
const et5 = cents(700);
const et6 = cents(900);
const etm7 = cents(1000);
const et7 = cents(1100);
const majorTriad = (f) => (
stack(f, j3, jm3)
);
const minorTriad = (f) => (
stack(f, jm3, j3)
);
const invert = (chord) => {
let inverted = chord.slice(1);
inverted.push(chord[0] * 2);
return inverted;
}
let fsm = [220, 440, 660, 880].map(sine);
fsm = mul(add(...fsm), 0.2);
// let thing = add(sine(440), sine(perfectFifth(440)));
let thing = add(
sine(27.5),
// triangle(80),
// triangle(200),
// sine(et3(220)),
);
let audio = new Audio(fsm);
audio.pipe(out);
// let tuning = equalTemperament(440);
// let a = sine(tuning("Ab"));
// let b = sine(tuning("Bb"));
// let result = mix(a, b)
// new Audio