WebAudioAPI.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. window.AudioContext = window.AudioContext || window.webkitAudioContext || null;
  31. window.OfflineAudioContext = window.OfflineAudioContext || window.webkitOfflineAudioContext || null;
  32. (function (Context) {
  33. var isFunction = function (f) {
  34. return Object.prototype.toString.call(f) === "[object Function]" ||
  35. Object.prototype.toString.call(f) === "[object AudioContextConstructor]";
  36. };
  37. var contextMethods = [
  38. ["createGainNode", "createGain"],
  39. ["createDelayNode", "createDelay"],
  40. ["createJavaScriptNode", "createScriptProcessor"]
  41. ];
  42. ///
  43. var proto;
  44. var instance;
  45. var sourceProto;
  46. ///
  47. if (!isFunction(Context)) {
  48. return;
  49. }
  50. instance = new Context();
  51. if (!instance.destination || !instance.sampleRate) {
  52. return;
  53. }
  54. proto = Context.prototype;
  55. sourceProto = Object.getPrototypeOf(instance.createBufferSource());
  56. if (!isFunction(sourceProto.start)) {
  57. if (isFunction(sourceProto.noteOn)) {
  58. sourceProto.start = function (when, offset, duration) {
  59. switch (arguments.length) {
  60. case 0:
  61. throw new Error("Not enough arguments.");
  62. case 1:
  63. this.noteOn(when);
  64. break;
  65. case 2:
  66. if (this.buffer) {
  67. this.noteGrainOn(when, offset, this.buffer.duration - offset);
  68. } else {
  69. throw new Error("Missing AudioBuffer");
  70. }
  71. break;
  72. case 3:
  73. this.noteGrainOn(when, offset, duration);
  74. }
  75. };
  76. }
  77. }
  78. if (!isFunction(sourceProto.noteOn)) {
  79. sourceProto.noteOn = sourceProto.start;
  80. }
  81. if (!isFunction(sourceProto.noteGrainOn)) {
  82. sourceProto.noteGrainOn = sourceProto.start;
  83. }
  84. if (!isFunction(sourceProto.stop)) {
  85. sourceProto.stop = sourceProto.noteOff;
  86. }
  87. if (!isFunction(sourceProto.noteOff)) {
  88. sourceProto.noteOff = sourceProto.stop;
  89. }
  90. contextMethods.forEach(function (names) {
  91. var name1;
  92. var name2;
  93. while (names.length) {
  94. name1 = names.pop();
  95. if (isFunction(this[name1])) {
  96. this[names.pop()] = this[name1];
  97. } else {
  98. name2 = names.pop();
  99. this[name1] = this[name2];
  100. }
  101. }
  102. }, proto);
  103. })(window.AudioContext);