* Sends the element's audio to an output. * * The parameter, `audioNode`, can be an `AudioNode` or an object from the * `p5.sound` library. * * If no element is provided, as in `myElement.connect()`, the element * connects to the main output. All connections are removed by the *
(obj)
| 916 | * or an object from the p5.sound library |
| 917 | */ |
| 918 | connect(obj) { |
| 919 | let audioContext, mainOutput; |
| 920 | |
| 921 | // if p5.sound exists, same audio context |
| 922 | if (this._getAudioContext() && this._getSoundOut()) { |
| 923 | audioContext = this._getAudioContext(); |
| 924 | mainOutput = this._getSoundOut().input; |
| 925 | } else { |
| 926 | try { |
| 927 | audioContext = obj.context; |
| 928 | mainOutput = audioContext.destination; |
| 929 | } catch (e) { |
| 930 | throw 'connect() is meant to be used with Web Audio API or p5.sound.js'; |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | // create a Web Audio MediaElementAudioSourceNode if none already exists |
| 935 | if (!this.audioSourceNode) { |
| 936 | this.audioSourceNode = audioContext.createMediaElementSource(this.elt); |
| 937 | |
| 938 | // connect to main output when this method is first called |
| 939 | this.audioSourceNode.connect(mainOutput); |
| 940 | } |
| 941 | |
| 942 | // connect to object if provided |
| 943 | if (obj) { |
| 944 | if (obj.input) { |
| 945 | this.audioSourceNode.connect(obj.input); |
| 946 | } else { |
| 947 | this.audioSourceNode.connect(obj); |
| 948 | } |
| 949 | } else { |
| 950 | // otherwise connect to main output of p5.sound / AudioContext |
| 951 | this.audioSourceNode.connect(mainOutput); |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | /** |
| 956 | * Disconnect all Web Audio routing, including to the main output. |