1 | /**
|
---|
2 | * @license -------------------------------------------------------------------
|
---|
3 | * module: WebAudioShim - Fix naming issues for WebAudioAPI supports
|
---|
4 | * src: https://github.com/Dinahmoe/webaudioshim
|
---|
5 | * author: Dinahmoe AB
|
---|
6 | * -------------------------------------------------------------------
|
---|
7 | * Copyright (c) 2012 DinahMoe AB
|
---|
8 | *
|
---|
9 | * Permission is hereby granted, free of charge, to any person
|
---|
10 | * obtaining a copy of this software and associated documentation
|
---|
11 | * files (the "Software"), to deal in the Software without
|
---|
12 | * restriction, including without limitation the rights to use,
|
---|
13 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
14 | * copies of the Software, and to permit persons to whom the
|
---|
15 | * Software is furnished to do so, subject to the following
|
---|
16 | * conditions:
|
---|
17 | *
|
---|
18 | * The above copyright notice and this permission notice shall be
|
---|
19 | * included in all copies or substantial portions of the Software.
|
---|
20 | *
|
---|
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
28 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
29 | */
|
---|
30 |
|
---|
31 | window.AudioContext = window.AudioContext || window.webkitAudioContext || null;
|
---|
32 | window.OfflineAudioContext = window.OfflineAudioContext || window.webkitOfflineAudioContext || null;
|
---|
33 |
|
---|
34 | (function (Context) {
|
---|
35 | var isFunction = function (f) {
|
---|
36 | return Object.prototype.toString.call(f) === "[object Function]" ||
|
---|
37 | Object.prototype.toString.call(f) === "[object AudioContextConstructor]";
|
---|
38 | };
|
---|
39 | var contextMethods = [
|
---|
40 | ["createGainNode", "createGain"],
|
---|
41 | ["createDelayNode", "createDelay"],
|
---|
42 | ["createJavaScriptNode", "createScriptProcessor"]
|
---|
43 | ];
|
---|
44 | ///
|
---|
45 | var proto;
|
---|
46 | var instance;
|
---|
47 | var sourceProto;
|
---|
48 | ///
|
---|
49 | if (!isFunction(Context)) {
|
---|
50 | return;
|
---|
51 | }
|
---|
52 | instance = new Context();
|
---|
53 | if (!instance.destination || !instance.sampleRate) {
|
---|
54 | return;
|
---|
55 | }
|
---|
56 | proto = Context.prototype;
|
---|
57 | sourceProto = Object.getPrototypeOf(instance.createBufferSource());
|
---|
58 |
|
---|
59 | if (!isFunction(sourceProto.start)) {
|
---|
60 | if (isFunction(sourceProto.noteOn)) {
|
---|
61 | sourceProto.start = function (when, offset, duration) {
|
---|
62 | switch (arguments.length) {
|
---|
63 | case 0:
|
---|
64 | throw new Error("Not enough arguments.");
|
---|
65 | case 1:
|
---|
66 | this.noteOn(when);
|
---|
67 | break;
|
---|
68 | case 2:
|
---|
69 | if (this.buffer) {
|
---|
70 | this.noteGrainOn(when, offset, this.buffer.duration - offset);
|
---|
71 | } else {
|
---|
72 | throw new Error("Missing AudioBuffer");
|
---|
73 | }
|
---|
74 | break;
|
---|
75 | case 3:
|
---|
76 | this.noteGrainOn(when, offset, duration);
|
---|
77 | }
|
---|
78 | };
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | if (!isFunction(sourceProto.noteOn)) {
|
---|
83 | sourceProto.noteOn = sourceProto.start;
|
---|
84 | }
|
---|
85 |
|
---|
86 | if (!isFunction(sourceProto.noteGrainOn)) {
|
---|
87 | sourceProto.noteGrainOn = sourceProto.start;
|
---|
88 | }
|
---|
89 |
|
---|
90 | if (!isFunction(sourceProto.stop)) {
|
---|
91 | sourceProto.stop = sourceProto.noteOff;
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (!isFunction(sourceProto.noteOff)) {
|
---|
95 | sourceProto.noteOff = sourceProto.stop;
|
---|
96 | }
|
---|
97 |
|
---|
98 | contextMethods.forEach(function (names) {
|
---|
99 | var name1;
|
---|
100 | var name2;
|
---|
101 | while (names.length) {
|
---|
102 | name1 = names.pop();
|
---|
103 | if (isFunction(this[name1])) {
|
---|
104 | this[names.pop()] = this[name1];
|
---|
105 | } else {
|
---|
106 | name2 = names.pop();
|
---|
107 | this[name1] = this[name2];
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }, proto);
|
---|
111 | })(window.AudioContext); |
---|