| <!DOCTYPE html> |
| <html> |
| <body> |
| <script> |
| var context; |
| var oscillator; |
| var scriptProcessor; |
| var isFirst = true; |
| |
| function startPlaying() |
| { |
| if (context) |
| context.close(); |
| |
| isFirst = true; |
| return new Promise(function(resolve, reject) { |
| context = new AudioContext(); |
| oscillator = new OscillatorNode(context); |
| scriptProcessor = context.createScriptProcessor(); |
| oscillator.connect(scriptProcessor); |
| scriptProcessor.connect(context.destination); |
| scriptProcessor.onaudioprocess = (event) => { |
| if (isFirst) { |
| resolve(); |
| isFirst = false; |
| } |
| let inputBuffer = event.inputBuffer; |
| let outputBuffer = event.outputBuffer; |
| for (let channel = 0; channel < outputBuffer.numberOfChannels; channel++) { |
| let inputData = inputBuffer.getChannelData(channel); |
| let outputData = outputBuffer.getChannelData(channel); |
| for (let sample = 0; sample < inputBuffer.length; sample++) |
| outputData[sample] = inputData[sample]; |
| } |
| }; |
| oscillator.start(); |
| }); |
| } |
| |
| function generateAudioInMediaStreamTrack() |
| { |
| if (context) |
| context.close(); |
| |
| context = new AudioContext(); |
| oscillator = new OscillatorNode(context); |
| |
| const mediaStreamDestination = context.createMediaStreamDestination(); |
| oscillator.connect(mediaStreamDestination); |
| oscillator.start(); |
| } |
| |
| function transitionAudioToSpeakers() |
| { |
| if (context) |
| oscillator.connect(context.destination); |
| } |
| |
| </script> |
| </body> |
| </html> |