new-clip.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <section class="new-clip">
  2. <style>
  3. #record-voice + label + div, #upload-file + label + div {
  4. margin-left: 1em;
  5. padding: .5em;
  6. }
  7. #script-select {
  8. max-width: calc(100% - 6ch);
  9. }
  10. #title-input {
  11. max-width: calc(100% - 16ch);
  12. }
  13. section.new-clip .new-clip {
  14. margin-bottom: 1em;
  15. }
  16. .new-clip .hbox {
  17. display: flex;
  18. align-items: center;
  19. }
  20. .new-clip .hbox select, .hbox input[type=text] {
  21. width: 100%;
  22. flex-shrink: 1;
  23. }
  24. .new-clip .hbox label {
  25. margin-left: 1em;
  26. margin-right: .5em;
  27. }
  28. .new-clip .hbox label:first-child {
  29. margin-left: 0px;
  30. }
  31. .new-clip h2 {
  32. margin-top: 0px;
  33. }
  34. .new-clip #submit {
  35. float: right;
  36. }
  37. #upload-file + label + div, #record-voice + label + div {
  38. }
  39. /* Show upload/recording elements only when that option is selected. */
  40. #record-voice + label + div { display: none; }
  41. #record-voice:checked + label + div { display: block; }
  42. #upload-file + label + div { display: none;}
  43. #upload-file:checked + label + div { display: block;}
  44. </style>
  45. <h2>Add a Clip</h2>
  46. <div class="hbox">
  47. <label for="title-input">Title</label>
  48. <input type="text" id="title-input" value="Untitled"/>
  49. <label for="color-select">Color</label>
  50. <input type="color" id="color-select" value="#000000"/>
  51. </div>
  52. <br>
  53. <!-- Script selection -->
  54. <div class="hbox">
  55. <label for="script-select">Script </label>
  56. <select class="script" id="script-select">
  57. {% include 'speech-scripts.html' %}
  58. </select>
  59. </div>
  60. <textarea rows="6" id="transcript"></textarea>
  61. <script>
  62. globalState.render(['transcript'], current => {
  63. $('#transcript').value = current.transcript;
  64. });
  65. function formatTranscript(value) {
  66. return value.replace(/\s\s+/g, '\n').trim()
  67. }
  68. globalState.set('transcript', formatTranscript($('#script-select').value));
  69. $('#script-select').addEventListener('change', evt => {
  70. let value = $('#script-select').value;
  71. globalState.set('transcript', value == 'custom' ? '' : formatTranscript(value));
  72. });
  73. $('#transcript').addEventListener('change', evt => {
  74. if (globalState.get('transcript') != $('#transcript').value) {
  75. globalState.set('transcript', $('#transcript').value);
  76. $('#script-select').value = 'custom';
  77. }
  78. });
  79. </script>
  80. <!-- Upload Options -->
  81. <input type="radio" name="add-recording" id="upload-file" checked>
  82. <label for="upload-file">Upload a file</label>
  83. <div>
  84. <input type='file' id='audio-file-input' name='recording'
  85. capture="user" accept="audio/*,video/*,audio/mpeg,audio/mp4,audio/x-aiff,audio/ogg,audio/vorbis,audio/opus,audio/vnd.wav,.wav,.mp3,.ogg,.opus,.m4a,.webm"/>
  86. </div>
  87. <br>
  88. <input type="radio" name="add-recording" id="record-voice">
  89. <label for="record-voice">Record your voice</label>
  90. <div>
  91. <button id="start-recording">⏺︎ Start</button>
  92. <button id="stop-recording">⏹︎ Stop</button>
  93. Recorded <span id="recording-count">0</span> seconds
  94. <br>
  95. <br>
  96. <audio controls id="recording-preview"></audio>
  97. </div>
  98. <!-- Submit -->
  99. <button id="submit" class='attention' onclick="submitAudio()">Add Clip</button>
  100. <script>
  101. try {
  102. // record from their microphone in-browser,
  103. let mediaRecorder = null;
  104. let audioChunks = [];
  105. let recordedAudio = null;
  106. function recordAudio() {
  107. let stopButton = document.querySelector("#stop-recording");
  108. let startButton = document.querySelector("#start-recording");
  109. const startRecording = () => {
  110. $('#recording-count').innerHTML = '0';
  111. let recordingInterval = setInterval(() => {
  112. $('#recording-count').innerHTML = Number($('#recording-count').innerHTML) + 1;
  113. }, 1000);
  114. mediaRecorder.addEventListener("stop", () => {
  115. clearInterval(recordingInterval);
  116. });
  117. stopButton.disabled = false;
  118. startButton.disabled = true;
  119. audioChunks = [];
  120. mediaRecorder.start();
  121. mediaRecorder.addEventListener("dataavailable", event => {
  122. audioChunks.push(event.data);
  123. });
  124. }
  125. function alertRecordingFailure(exception) {
  126. alert(
  127. String(window.location).slice(0, 5) == 'https'
  128. ?
  129. "Sorry, something went wrong. "
  130. + "Please ensure that your microphone is connected "
  131. + "and that your browser allows us to record from it. "
  132. + "Alternately, you can try uploading a file recorded from a native app instead."
  133. :
  134. "Recording in-browser is only available "
  135. + "when accessing the site over a secure connection. "
  136. + "To record, reload the page ensuring "
  137. + "that the URL starts with 'https', "
  138. + "including the 's' at the end."
  139. );
  140. console.error(exception);
  141. }
  142. if (mediaRecorder) { startRecording(); } else {
  143. try {
  144. navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
  145. mediaRecorder = new MediaRecorder(stream);
  146. mediaRecorder.addEventListener("stop", () => {
  147. stopButton.disabled = true;
  148. startButton.disabled = false;
  149. const audioBlob = new Blob(audioChunks);
  150. recordedAudio = audioBlob;
  151. let reader = new FileReader(recordedAudio);
  152. reader.readAsDataURL(recordedAudio);
  153. reader.addEventListener("load", () => {
  154. $('#recording-preview').src = reader.result.replace(
  155. /data:.*;base64/, "data:audio/wav;base64"
  156. );
  157. $('#recording-preview').load();
  158. });
  159. });
  160. stopButton.addEventListener("click", () => {mediaRecorder.stop();}, false);
  161. startRecording();
  162. }).catch(exception => alertRecordingFailure(exception));
  163. } catch(exception) { alertRecordingFailure(exception); }
  164. }
  165. }
  166. $('#start-recording').addEventListener('click', evt => {
  167. recordAudio();
  168. });
  169. function submitAudio() {
  170. let clip = new Clip();
  171. let formData = new FormData();
  172. let transcriptText = $('#transcript').value;
  173. formData.append('transcript', transcriptText);
  174. formData.append('referrer', document.referrer);
  175. formData.append('screen-width', window.screen.width);
  176. formData.append('screen-height', window.screen.height);
  177. formData.append('lang', navigator.language || navigator.userLanguage);
  178. clip.transcript = transcriptText;
  179. clip.title = $('#title-input').value;
  180. clip.color = $('#color-select').value;
  181. if ($('#upload-file').checked) {
  182. fileInput = $('#audio-file-input')
  183. if (fileInput.files.length < 1) return
  184. let recording = fileInput.files[0];
  185. formData.append('recording', recording);
  186. clip.loadAudioFile(recording);
  187. } else if ($('#record-voice').checked) {
  188. formData.append('recording', recordedAudio);
  189. clip.loadAudioFile(recordedAudio);
  190. } else {
  191. alert('Please upload a file or make a recording.');
  192. return;
  193. }
  194. $('button.new-clip').classList.add('hidden');
  195. $('button.new-clip + .spinner').classList.remove('hidden');
  196. hideModal();
  197. fetch('./backend.cgi', { method: 'POST', body: formData})
  198. .then(response => {
  199. if (!response.ok) throw new Error(response.status);
  200. if (response.headers.get('Content-Length') < 2) return {};
  201. return response.json();
  202. }).then(data => {
  203. $('button.new-clip').classList.remove('hidden');
  204. $('button.new-clip + .spinner').classList.add('hidden');
  205. console.assert(data.hasOwnProperty('phones') && data.phones.length < 1);
  206. clip.loadResponse(data);
  207. globalState.mutate('clips', clips => clips.push(clip));
  208. globalState.set('playing', false);
  209. globalState.set('playingClip', clip);
  210. globalState.set('previewClip', clip);
  211. }).catch(() => {
  212. alert(
  213. "Sorry, we weren't able to process your recording. " +
  214. "Make sure to speak clearly and ensure " +
  215. "that your transcript matches the audio. " +
  216. "For increased chance of success," +
  217. "try using short clips with a 1-second period of silence."
  218. );
  219. $('button.new-clip').classList.remove('hidden');
  220. $('button.new-clip + .spinner').classList.add('hidden');
  221. });
  222. }
  223. } catch (exception) {
  224. alert("An unknown error occured.");
  225. console.error(exception);
  226. }
  227. </script>
  228. </section>