All files / src/fx fx.js

38.89% Statements 7/18
42.86% Branches 6/14
66.67% Functions 2/3
38.89% Lines 7/18
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                          31x                                                                                                                                     159x         159x         159x                   159x 159x             31x              
/**
 * Imports the config module
 * @module config
 */
import config from "../config.js";
 
/**
 * AmplitudeJS FX Module. Helps with configuring and setting up visualizations
 * and any other features of the Web Audio API that Amplitude takes advantage
 * of.
 *
 * @module fx/FX
 */
let Fx = (function() {
  /**
   * Configures the Web Audio API to work with AmplitudeJS
   */
  function configureWebAudioAPI() {
    /*
			Gets the context for the browser. If this is null, the Web Audio
			API is unavailable.
		*/
    let browserContext =
      window.AudioContext ||
      window.webkitAudioContext ||
      window.mozAudioContext ||
      window.oAudioContext ||
      window.msAudioContext;
 
    /*
			If we have a context, the Web Audio API is available and we can continue,
			otherwise, we alert the user if they have debug turned on.
		*/
    if (browserContext) {
      /*
				Web Audio API is available, set the context in our config.
			*/
      config.context = new browserContext();
 
      /*
				Create an analyzer that we will use in the context.
			*/
      config.analyser = config.context.createAnalyser();
 
      /*
				Bind the source to the Javascript Audio Element.
			*/
      config.source = config.context.createMediaElementSource(config.audio);
 
      /*
				Connect the analyser to the source
			*/
      config.source.connect(config.analyser);
 
      /*
				Connect the context destination to the analyser.
			*/
      config.analyser.connect(config.context.destination);
 
      /*
				Set cross origin to anonymous so we have a better chance of being able
				to use the power of the Web Audio API.
			*/
      config.audio.crossOrigin = "anonymous";
    } else {
      AmplitudeHelpers.writeDebugMessage(
        "Web Audio API is unavailable! We will set any of your visualizations with your back up definition!"
      );
    }
  }
 
  /**
   * Determines if the web audio API is available or not.
   */
  function webAudioAPIAvailable() {
    /*
			Gets the context for the browser. If this is null, the Web Audio
			API is unavailable.
		*/
    let browserContext =
      window.AudioContext ||
      window.webkitAudioContext ||
      window.mozAudioContext ||
      window.oAudioContext ||
      window.msAudioContext;
    config.web_audio_api_available = false;
 
    /*
			Determines if the Web Audio API is available or not.
		*/
    Iif (browserContext) {
      /*
				Set the flag in the config that the Web Audio API is available
			*/
      config.web_audio_api_available = true;
      return true;
    } else {
      /*
				Set the flag in the config that the Web Audio API is not available
			*/
      config.web_audio_api_available = false;
      return false;
    }
  }
 
  /*
		Returns the publicly accessible methods
	*/
  return {
    configureWebAudioAPI: configureWebAudioAPI,
    webAudioAPIAvailable: webAudioAPIAvailable
  };
})();
 
export default Fx;