-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
108 lines (97 loc) · 4.32 KB
/
index.html
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
<html>
<head>
<title>radio controls</title>
</head>
<body>
<center>
<p>Input WAV file</p>
<input type="file" name="file" id="fileinput" accept="audio/wav" required>
<button id="submitButton" type="submit">Upload</button>
<div style="padding: 10px;"></div>
<button hidden id="recordButton">Record Microphone</button>
<button hidden id="uploadButton" disabled>Stop and upload to server</button>
<hr style="height:1px;width:18%;color: #000;background-color:black">
<p>Input WAV file URL</p>
<input type="text" id="url" name="url"/><input id="urlButton" type="submit" value="OK" />
<div id="output" style="margin-bottom: 10px; border: 1px solid;border-color: black;width: 18%;;"></div>
<button id="resetButton" type="submit">Restart Radio</button>
</center>
<script src="https://cdn.rawgit.com/mattdiamond/Recorderjs/08e7abd9/dist/recorder.js"></script>
<script>
let rec = null;
let audioStream = null;
const recordButton = document.getElementById("recordButton");
const uploadButton = document.getElementById("uploadButton");
const submitButton = document.getElementById("submitButton");
const resetButton = document.getElementById("resetButton");
const urlButton = document.getElementById("urlButton");
urlButton.addEventListener("click", uploadUrl)
resetButton.addEventListener("click", resetRadio);
submitButton.addEventListener("click", getSoundData);
recordButton.addEventListener("click", startRecording);
uploadButton.addEventListener("click", exportAudio);
function startRecording() {
let constraints = { audio: true, video:false }
recordButton.disabled = true;
uploadButton.disabled = false;
navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {
const audioContext = new window.AudioContext();
audioStream = stream;
const input = audioContext.createMediaStreamSource(stream);
rec = new Recorder(input, { numChannels: 1 })
rec.record()
document.getElementById("output").innerHTML = "Recording started..."
}).catch(function(err) {
recordButton.disabled = false;
uploadButton.disabled = true;
console.log(err)
});
}
function getSoundData() {
document.getElementById("output").innerHTML = "Uploading..."
uploadSoundData(document.getElementById("fileinput").files[0]);
}
function exportAudio() {
document.getElementById("output").innerHTML = "Uploading..."
uploadButton.disabled = true;
recordButton.disabled = false;
rec.stop();
audioStream.getAudioTracks()[0].stop();
rec.exportWAV(uploadSoundData);
}
function uploadSoundData(blob) {
const filename = "sound-file-" + new Date().getTime() + ".wav";
const formData = new FormData();
formData.append("file", blob);
fetch('/upload', {
method: 'POST',
body: formData
}).then(async result => {
document.getElementById("output").innerHTML = await result.text();
}).catch(error => {
document.getElementById("output").innerHTML = "An error occurred: " + error;
})
}
function resetRadio() {
fetch('/reset', {
method: 'POST',
}).then(async result => {
document.getElementById("output").innerHTML = await result.text();
}).catch(error => {
document.getElementById("output").innerHTML = "An error occurred: " + error;
})
}
function uploadUrl() {
const url = document.getElementById("url").value
console.log(url)
fetch('/uploadurl?url='+url, {
method: 'GET'
}).then(async result => {
document.getElementById("output").innerHTML = await result.text();
}).catch(error => {
document.getElementById("output").innerHTML = "An error occurred: " + error;
})
}
</script>
</body>
</html>