Allow empty attribute syntax to mean "same command as ID".
[featherfall2.git] / src / yuu / audio.js
1 /* Copyright 2014 Yukkuri Games
2 Licensed under the terms of the GNU GPL v2 or later
3 @license http://www.gnu.org/licenses/gpl-2.0.html
4 @source: http://yukkurigames.com/yuu/
5 */
6
7 (function (yuu) {
8 "use strict";
9
10 var yT = this.yT || require("./yT");
11 var yf = this.yf || require("./yf");
12
13 yuu.Audio = yT({
14 /** Audio context/source/buffer accessor
15
16 You probably don't need to make this yourself; one is made
17 named yuu.audio during initialization.
18
19 You can set the master volume with yuu.audio.masterVolume.
20 */
21 constructor: function () {
22 this._ctx = new window.AudioContext();
23 this._compressor = this._ctx.createDynamicsCompressor();
24 this._masterVolume = this._ctx.createGain();
25 this._masterVolume.connect(this._compressor);
26 this._compressor.connect(this._ctx.destination);
27 this._musicVolume = this._ctx.createGain();
28 this._musicVolume.connect(this._masterVolume);
29 this._masterVolume.gain.value = 0.5;
30 this._musicVolume.gain.value = 0.5;
31
32 this._bufferCache = {};
33 this._mute = false;
34 this._storage = null;
35 this._volume = this._masterVolume.gain.value;
36 },
37
38 destination: { alias: "_masterVolume", readonly: true },
39 music: { alias: "_musicVolume", readonly: true },
40
41 _readStorage: function () {
42 if (!this._storage)
43 return;
44 yf.each.call(this, function (prop) {
45 this[prop] = this._storage.getObject(prop, this[prop]);
46 }, ["volume", "musicVolume", "mute"]);
47 },
48
49 _writeStorage: yf.debounce(function () {
50 if (!this._storage)
51 return;
52 yf.each.call(this, function (prop) {
53 this._storage.setObject(prop, this[prop]);
54 }, ["volume", "musicVolume", "mute"]);
55 }),
56
57 storage: {
58 get: function () { return this._storage; },
59 set: function (v) {
60 this._storage = v;
61 this._readStorage();
62 }
63 },
64
65 mute: {
66 get: function () { return this._mute; },
67 set: function (v) {
68 this._mute = !!v;
69 this.volume = this.volume;
70 }
71 },
72
73 volume: {
74 get: function () { return this._volume; },
75 set: function (v) {
76 this._volume = v;
77 v = this._mute ? 0 : v;
78 this._masterVolume.gain.value = v;
79 this._writeStorage();
80 }
81 },
82
83 musicVolume: {
84 get: function () { return this._musicVolume.gain.value; },
85 set: function (v) {
86 this._musicVolume.gain.value = v;
87 this._writeStorage();
88 }
89 },
90
91 currentTime: { alias: "_ctx.currentTime" },
92
93 decodeAudioData: function (data) {
94 var ctx = this._ctx;
95 try {
96 return ctx.decodeAudioData(data);
97 } catch (exc) {
98 return new Promise(function (resolve, reject) {
99 ctx.decodeAudioData(data, function (buffer) {
100 resolve(buffer);
101 }, function () {
102 reject(new Error("Error decoding audio buffer"));
103 });
104 });
105 }
106 },
107
108 createBufferSource: function (path) {
109 var source = this._ctx.createBufferSource();
110 var sample = new yuu.AudioSample(path, this);
111 if ((source.buffer = sample.buffer) === null) {
112 sample.ready.then(function () {
113 source.buffer = sample.buffer;
114 });
115 }
116 return source;
117 },
118
119 sampleRate: { alias: "_ctx.sampleRate" },
120 createGain: { proxy: "_ctx.createGain" },
121 createMediaElementSource: { proxy: "_ctx.createMediaElementSource" },
122 createOscillator: { proxy: "_ctx.createOscillator" },
123 });
124
125 // FIXME: This parsing is garbagey, would be better to parse when
126 // first handed a dfn and turn everything into a function.
127 function applyMod (s, v) {
128 if (yf.isFunction(s))
129 return s(v);
130 else if (s === +s)
131 return s;
132 else if (s[0] === "-" || s[0] === "+")
133 return v + (+s);
134 else if (s[0] === "x" || s[0] === "*")
135 return v * +s.substring(1);
136 else if (s[s.length - 1] === "%")
137 return v * (parseFloat(s) / 100);
138 else
139 return +s;
140 }
141
142 var Envelope = yuu.Envelope = yT({
143 constructor: yf.argcd(
144 function (pairs) {
145 Envelope.call(this, pairs, 1);
146 },
147 function (pairs, scale) {
148 pairs = pairs || { "0": 1, "100%": 1 };
149 this.ts = Object.keys(pairs);
150 this.vs = yf.map.call(pairs, yf.getter, this.ts);
151 this.scale = scale;
152 var a = 0, b = 0;
153 var unlimited = false;
154 yf.each(function (t) {
155 if (+t) {
156 a = Math.max(+t, a);
157 b = Math.min(+t, b);
158 }
159 unlimited = unlimited || (t[t.length - 1] === "%");
160 }, this.ts);
161 this.minDuration = a - b;
162 this.maxDuration = (unlimited || a === b)
163 ? Infinity
164 : this.minDuration;
165 var vMin = Math.min.apply(Math, this.vs);
166 var vMax = Math.max.apply(Math, this.vs);
167 this.constant = vMin === vMax && this.vs[0] * this.scale;
168 }
169 ),
170
171 schedule: function (param, t0, scale, duration) {
172 if (this.constant !== false) {
173 param.setValueAtTime(scale * this.constant, t0);
174 } else {
175 yf.each.call(this, function (s, v) {
176 v = v * scale * this.scale;
177 var t = t0 + applyMod(s, duration);
178 if (t === t0)
179 param.setValueAtTime(v, t);
180 else
181 param.linearRampToValueAtTime(v, t);
182 }, this.ts, this.vs);
183 }
184 }
185 });
186
187 yuu.AudioSample = yuu.Caching(yT({
188 constructor: function (path, ctx) {
189 ctx = ctx || yuu.audio;
190 var url = yuu.resourcePath(path, "sound", "wav");
191 this.data = null;
192 this.ready = yuu.GET(url, { responseType: "arraybuffer" })
193 .then(ctx.decodeAudioData.bind(ctx))
194 .then(yf.setter.bind(this, "buffer"))
195 .then(yf.K(this));
196 }
197 }), function (args) { return args.length <= 2 ? args[0] : null; });
198
199 yuu.Modulator = yT({
200 constructor: function (dfn) {
201 this.envelope = new yuu.Envelope(dfn.envelope);
202 this.frequency = dfn.frequency;
203 this.index = dfn.index || 1.0;
204 },
205
206 createModulator: function (ctx, t0, fundamental, duration) {
207 var modulator = ctx.createOscillator();
208 modulator.frequency.value = applyMod(
209 this.frequency, fundamental);
210 modulator.start(t0);
211 modulator.stop(t0 + duration);
212 var modulatorG = ctx.createGain();
213 modulator.connect(modulatorG);
214 this.envelope.schedule(
215 modulatorG.gain, t0, this.index * fundamental, duration);
216 return modulatorG;
217 }
218 });
219
220 yuu.Instrument = yT({
221 constructor: function (dfn) {
222 if (yf.isString(dfn)) {
223 var sampleName = dfn;
224 dfn = { sample: {} };
225 dfn.sample[sampleName] = {};
226 }
227 this.envelope = new yuu.Envelope(dfn.envelope);
228 this.frequency = dfn.frequency || (dfn.sample ? {} : { "x1": 1.0 });
229 this.modulator = yf.map(
230 yf.new_(yuu.Modulator), yf.arrayify(dfn.modulator || []));
231 this.sample = dfn.sample || {};
232 this.ready = yuu.ready(
233 yf.map(yf.new_(yuu.AudioSample), Object.keys(this.sample)),
234 this);
235 },
236
237 createSound: function (ctx, t0, fundamental, amplitude, duration) {
238 // TODO: In the case of exactly one sample with a constant
239 // envelope, optimize out the extra gain node.
240 duration = yf.clamp(duration || 0,
241 this.envelope.minDuration,
242 this.envelope.maxDuration);
243 var ret = ctx.createGain();
244 var dst = ret;
245
246 yf.ipairs(function (name, params) {
247 var buffer = new yuu.AudioSample(name).buffer;
248 if (buffer && !params.loop)
249 duration = Math.max(buffer.duration, duration);
250 }, this.sample);
251
252 var modulators = yf.map(function (modulator) {
253 return modulator.createModulator(
254 ctx, t0, fundamental, duration);
255 }, this.modulator);
256
257 yf.ipairs.call(this, function (name, params) {
258 var src = ctx.createBufferSource(name);
259 src.loop = params.loop || false;
260 src.playbackRate.value = applyMod(
261 params.playbackRate || 1, fundamental || ctx.sampleRate);
262 yf.each(function (mod) { mod.connect(src.playbackRate); },
263 modulators);
264 if (params.duration)
265 src.start(t0, params.offset || 0, params.duration);
266 else
267 src.start(t0, params.offset || 0);
268 src.stop(t0 + duration);
269 src.connect(dst);
270 }, this.sample);
271
272 yf.ipairs.call(this, function (mfreq, mamp) {
273 var osc = ctx.createOscillator();
274 osc.frequency.value = applyMod(mfreq, fundamental);
275 osc.start(t0);
276 osc.stop(t0 + duration);
277 yf.each(function (mod) { mod.connect(osc.frequency); },
278 modulators);
279 if (mamp !== 1) {
280 var gain = ctx.createGain();
281 gain.gain.value = mamp;
282 osc.connect(gain);
283 gain.connect(dst);
284 } else {
285 osc.connect(dst);
286 }
287 }, this.frequency);
288
289 ret.gain.value = 0;
290 this.envelope.schedule(ret.gain, t0, amplitude, duration);
291 return ret;
292 },
293
294 play: yf.argcd(
295 function () {
296 return this.play(null, 0, 0, 1, 1);
297 },
298 function (ctx, t, freq, amp, duration) {
299 ctx = ctx || yuu.audio;
300 t = t || ctx.currentTime;
301 var g = this.createSound(ctx, t, freq, amp, duration);
302 g.connect(ctx.destination);
303 return g;
304 }
305 )
306 });
307
308 yuu.Instruments = yf.mapValues(yf.new_(yuu.Instrument), {
309 SINE: {
310 envelope: { "0": 0, "0.016": 1, "-0.016": 1, "100%": 0 },
311 },
312
313 ORGAN: {
314 envelope: { "0": 0, "0.016": 1, "-0.016": 1, "100%": 0 },
315 frequency: { "x1": 0.83, "x1.5": 0.17 }
316 },
317
318 SIREN: {
319 envelope: { "0": 1 },
320 modulator: {
321 envelope: { "0": 1 },
322 frequency: "1",
323 index: 0.2
324 }
325 },
326
327 BELL: {
328 envelope: { "0": 1, "2.5": 0.2, "5": 0 },
329 modulator: {
330 envelope: { "0": 1, "2.5": 0.2, "5": 0 },
331 frequency: "x1.5",
332 }
333 },
334
335 BRASS: {
336 envelope: { "0": 0, "0.2": 1, "0.4": 0.6, "-0.1": 0.5, "100%": 0 },
337 modulator: {
338 envelope: { "0": 0, "0.2": 1, "0.4": 0.6,
339 "-0.1": 0.5, "100%": 0 },
340 frequency: "x1",
341 index: 5.0
342 }
343 },
344 });
345
346 // Tune to A440 by default, although every interface should provide
347 // some ways to work around this. This gives C4 = ~261.63 Hz.
348 // https://en.wikipedia.org/wiki/Scientific_pitch_notation
349 yuu.C4_HZ = 440 * Math.pow(2, -9/12);
350
351 yuu.Scale = yT({
352 constructor: function (intervals) {
353 this.intervals = intervals;
354 this.length = this.intervals.length;
355 this.span = yf.foldl(function (a, b) { return a + b; }, intervals);
356 },
357
358 hz: function (tonic, degree, accidental) {
359 accidental = accidental || 0;
360 var s = this.span * ((degree / this.intervals.length) | 0)
361 + accidental;
362 degree %= this.intervals.length;
363 if (degree < 0) {
364 degree += this.intervals.length;
365 s -= this.span;
366 }
367 var i = 0;
368 while (degree >= 1) {
369 degree -= 1;
370 s += this.intervals[i];
371 i++;
372 }
373 if (degree > 0)
374 s += this.intervals[i] * degree;
375 return tonic * Math.pow(2, s / 1200.0);
376 }
377 });
378
379 yuu.Scales = yf.mapValues(yf.new_(yuu.Scale), {
380 CHROMATIC: yf.repeat(100, 12),
381 MINOR: [200, 100, 200, 200, 100, 200, 200],
382 MAJOR: [200, 200, 100, 200, 200, 200, 100],
383 WHOLE_TONE: [200, 200, 200, 200, 200, 200],
384 AUGMENTED: [300, 100, 300, 100, 300, 100],
385 _17ET: yf.repeat(1200 / 17, 17),
386 DOUBLE_HARMONIC: [100, 300, 100, 200, 100, 300, 100],
387 });
388
389 var DURATION = { T: 1/8, S: 1/4, I: 1/2, Q: 1, H: 2, W: 4,
390 ".": 1.5, "/": 1/3, "<": -1 };
391 var ACCIDENTAL = { b: -1, "#": 1, t: 0.5, d: -0.5 };
392
393 var NOTE = /([TSIQHW][<.\/]*)?(?:([XZ]|(?:[A-G][b#dt]*[0-9]+))|([+-]?[0-9.]+)([b#dt]*))|([-+<>{]|(?:[TSIQHW][.\/]?))/g;
394
395 var LETTERS = { Z: null, X: null };
396
397 yuu.parseNote = function (note, scale, C4) {
398 return (C4 || yuu.C4_HZ) * Math.pow(2, LETTERS[note] / 12);
399 };
400
401 yuu.parseScore = function (score, scale, tonic, C4) {
402 // Note language:
403 //
404 // To play a scientific pitch note and advance the time, just
405 // use its name: G4, Cb2, A#0
406 //
407 // To adjust the length of the note, use T, S, I, Q (default),
408 // H, W for 32nd through whole. Append . to do
409 // time-and-a-half. Append / to cut into a third. Append < to
410 // go back in time.
411 //
412 // To play a note on the provided scale, use a 0-based number
413 // (which can be negative). To move the current scale up or
414 // down, use + or -. For example, in C major, 0 and C4 produce
415 // the same note; after a -, 0 and C3 produce the same note.
416 //
417 // To rest, use Z or X.
418 //
419 // To play multiple notes at the same time, enclose them all with
420 // < ... >. The time will advance in accordance with the shortest
421 // one.
422 //
423 // To reset the time, scale offset, and duration, use a {.
424 // This can be more convenient when writing pieces with
425 // multiple parts than grouping, e.g.
426 // H < 1 8 > < 2 7 > < 3 6 > < 4 5 >
427 // is easier to understand when split into multiple lines:
428 // H 1 2 3 4
429 // { H 8 7 6 5
430
431 scale = scale || yuu.Scales.MAJOR;
432 C4 = C4 || yuu.C4_HZ;
433 tonic = tonic || scale.tonic || C4;
434 if (yf.isString(tonic))
435 tonic = yuu.parseNote(tonic, C4);
436
437 var t = 0;
438 var notes = [];
439 var degree = 0;
440 var groupLength = 0;
441 var defaultDuration = "Q";
442 var match;
443
444 function calcDuration (d, m) { return d * DURATION[m]; }
445 function calcAccidental (d, m) { return d * ACCIDENTAL[m]; }
446
447 while ((match = NOTE.exec(score))) {
448 switch (match[5]) {
449 case "<":
450 groupLength = Infinity;
451 break;
452 case ">":
453 t += groupLength === Infinity ? 0 : groupLength;
454 groupLength = 0;
455 break;
456 case "+":
457 degree += scale.length;
458 break;
459 case "-":
460 degree -= scale.length;
461 break;
462 case "{":
463 t = 0;
464 degree = 0;
465 groupLength = 0;
466 defaultDuration = "Q";
467 break;
468 default:
469 if (match[5]) {
470 defaultDuration = match[5];
471 continue;
472 }
473 var letter = match[2];
474 var duration = yf.foldl(
475 calcDuration, match[1] || defaultDuration, 1);
476 if (LETTERS[letter] !== null) {
477 var offset = match[3];
478 var accidental = yf.foldl(
479 calcAccidental, match[4] || "", 0) * 100;
480 notes.push({
481 time: t,
482 duration: duration,
483 hz: letter
484 ? C4 * Math.pow(2, LETTERS[letter]/12.0)
485 : scale.hz(tonic, degree + (+offset || 0), accidental)
486 });
487 }
488 if (groupLength && duration > 0)
489 groupLength = Math.min(groupLength, duration);
490 else
491 t += duration;
492 }
493 }
494
495 notes.sort(function (a, b) { return a.time - b.time; });
496 return notes;
497 };
498
499 yf.irange.call(LETTERS, function (i) {
500 yf.ipairs.call(this, function (l, o) {
501 var b = o + 12 * (i - 4);
502 this[l + i] = b;
503 yf.ipairs.call(this, function (s, m) {
504 this[l + s + i] = b + m;
505 }, ACCIDENTAL);
506 }, { C: 0, D: 2, E: 4, F: 5, G: 7, A: 9, B: 11 });
507 }, 11);
508
509 yuu.registerInitHook(function () {
510 if (!window.AudioContext)
511 throw new Error("Web Audio isn't supported.");
512 yuu.audio = new yuu.Audio();
513 yuu.defaultCommands.volume = yuu.propcmd(
514 yuu.audio, "volume",
515 "get/set the current master audio volume", "0...1");
516 yuu.defaultCommands.musicVolume = yuu.propcmd(
517 yuu.audio, "musicVolume",
518 "get/set the current music volume", "0...1");
519 yuu.defaultCommands.mute = yuu.propcmd(
520 yuu.audio, "mute", "mute or unmute audio");
521 });
522
523 }).call(typeof exports === "undefined" ? this : exports,
524 typeof exports === "undefined"
525 ? this.yuu : (module.exports = require('./core')));