Source: main/webapp/modules/AudioContextFactory.js

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var Guacamole = Guacamole || {};
  20. /**
  21. * Maintains a singleton instance of the Web Audio API AudioContext class,
  22. * instantiating the AudioContext only in response to the first call to
  23. * getAudioContext(), and only if no existing AudioContext instance has been
  24. * provided via the singleton property. Subsequent calls to getAudioContext()
  25. * will return the same instance.
  26. *
  27. * @namespace
  28. */
  29. Guacamole.AudioContextFactory = {
  30. /**
  31. * A singleton instance of a Web Audio API AudioContext object, or null if
  32. * no instance has yes been created. This property may be manually set if
  33. * you wish to supply your own AudioContext instance, but care must be
  34. * taken to do so as early as possible. Assignments to this property will
  35. * not retroactively affect the value returned by previous calls to
  36. * getAudioContext().
  37. *
  38. * @type {AudioContext}
  39. */
  40. 'singleton' : null,
  41. /**
  42. * Returns a singleton instance of a Web Audio API AudioContext object.
  43. *
  44. * @return {AudioContext}
  45. * A singleton instance of a Web Audio API AudioContext object, or null
  46. * if the Web Audio API is not supported.
  47. */
  48. 'getAudioContext' : function getAudioContext() {
  49. // Fallback to Webkit-specific AudioContext implementation
  50. var AudioContext = window.AudioContext || window.webkitAudioContext;
  51. // Get new AudioContext instance if Web Audio API is supported
  52. if (AudioContext) {
  53. try {
  54. // Create new instance if none yet exists
  55. if (!Guacamole.AudioContextFactory.singleton)
  56. Guacamole.AudioContextFactory.singleton = new AudioContext();
  57. // Return singleton instance
  58. return Guacamole.AudioContextFactory.singleton;
  59. }
  60. catch (e) {
  61. // Do not use Web Audio API if not allowed by browser
  62. }
  63. }
  64. // Web Audio API not supported
  65. return null;
  66. }
  67. };