1 | // The controller for asr.xsl
|
---|
2 | // @ts-nocheck
|
---|
3 |
|
---|
4 | /**
|
---|
5 | * The name of the file input that audio files can be uploaded to.
|
---|
6 | */
|
---|
7 | const FILE_UPLOAD_INPUT_NAME = "#fileUpload";
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * The name of the container that holds the progress display for the audio upload.
|
---|
11 | */
|
---|
12 | const FILE_UPLOAD_PROGRESS_CONTAINER_NAME = "#prgFileUploadContainer";
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * The name of the button that triggers the audio upload
|
---|
16 | */
|
---|
17 | const FILE_UPLOAD_BUTTON_NAME = "#btnFileUpload";
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * @callback loadScriptCallback
|
---|
21 | * @param {Event} ev The event
|
---|
22 | * @returns {void}
|
---|
23 | */
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Loads a remote script.
|
---|
27 | * Found at https://stackoverflow.com/questions/950087/how-do-i-include-a-javascript-file-in-another-javascript-file
|
---|
28 | *
|
---|
29 | * @param {string} url The URL from which to load the script.
|
---|
30 | * @param {loadScriptCallback} callback Callback performed when the script has been loaded.
|
---|
31 | */
|
---|
32 | function loadScript(url, callback = () => {})
|
---|
33 | {
|
---|
34 | // Append the script to the body element
|
---|
35 | var body = document.body;
|
---|
36 | var script = document.createElement('script');
|
---|
37 | script.type = 'text/javascript';
|
---|
38 | script.src = url;
|
---|
39 |
|
---|
40 | script.onload = callback;
|
---|
41 |
|
---|
42 | body.appendChild(script);
|
---|
43 | }
|
---|
44 |
|
---|
45 | loadScript("./interfaces/atea/js/asr/TranscribeService.js");
|
---|
46 |
|
---|
47 | async function doAudioUpload()
|
---|
48 | {
|
---|
49 | const files = $(FILE_UPLOAD_INPUT_NAME)[0].files;
|
---|
50 | const transcribeService = new TranscribeService();
|
---|
51 |
|
---|
52 | $(FILE_UPLOAD_PROGRESS_CONTAINER_NAME).removeClass("asr-hidden");
|
---|
53 |
|
---|
54 | // Remove old transcriptions
|
---|
55 | const transcriptionsList = document.querySelector("#transcriptionsList");
|
---|
56 | if (transcriptionsList == null)
|
---|
57 | {
|
---|
58 | console.error("Cannot find transcriptions list");
|
---|
59 | return;
|
---|
60 | }
|
---|
61 |
|
---|
62 | while (transcriptionsList.lastChild) {
|
---|
63 | transcriptionsList.removeChild(transcriptionsList.lastChild);
|
---|
64 | }
|
---|
65 |
|
---|
66 | try
|
---|
67 | {
|
---|
68 | for await (const transcriptionBundle of transcribeService.chunkTranscribeFiles(files))
|
---|
69 | {
|
---|
70 | for (const t of transcriptionBundle)
|
---|
71 | {
|
---|
72 | if (!t.success) {
|
---|
73 | insertError(t.file_name, t.log);
|
---|
74 | }
|
---|
75 | else {
|
---|
76 | insertTranscription(t);
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 | catch (e)
|
---|
82 | {
|
---|
83 | console.error("Failed to transcribe files: " + e);
|
---|
84 | insertError("all", e.statusMessage);
|
---|
85 | }
|
---|
86 |
|
---|
87 | $(FILE_UPLOAD_PROGRESS_CONTAINER_NAME).addClass("asr-hidden");
|
---|
88 | }
|
---|
89 |
|
---|
90 | // Adapted from https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template
|
---|
91 | const transcriptionTemplate = document.querySelector('#transcriptionTemplate');
|
---|
92 | const metadataTemplate = document.querySelector("#metadataTemplate");
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Inserts a transcription object into the DOM.
|
---|
96 | *
|
---|
97 | * @param {TranscriptionModel} transcription The transcription to insert.
|
---|
98 | */
|
---|
99 | function insertTranscription(transcription)
|
---|
100 | {
|
---|
101 | // Get the transcription list and template
|
---|
102 | let list = document.querySelector("#transcriptionsList");
|
---|
103 |
|
---|
104 | // Insert a new transcription row
|
---|
105 | let clone = transcriptionTemplate.content.firstElementChild.cloneNode(true);
|
---|
106 | let spans = clone.querySelectorAll("span");
|
---|
107 | spans[0].textContent = transcription.file_name;
|
---|
108 | spans[1].textContent = transcription.transcription;
|
---|
109 |
|
---|
110 | // Get the metadata list and template
|
---|
111 | let metadataList = clone.querySelector("#metadataList");
|
---|
112 |
|
---|
113 | if (metadataList == null || metadataTemplate == null)
|
---|
114 | {
|
---|
115 | console.error("Could not find metadata list and/or template.");
|
---|
116 | return;
|
---|
117 | }
|
---|
118 |
|
---|
119 | // Insert metadata rows
|
---|
120 | for (let metadata of transcription.metadata)
|
---|
121 | {
|
---|
122 | let metadataClone = metadataTemplate.content.firstElementChild.cloneNode(true);
|
---|
123 | let metadataSpans = metadataClone.querySelectorAll("td");
|
---|
124 |
|
---|
125 | metadataSpans[0].textContent = metadata.char;
|
---|
126 | metadataSpans[1].textContent = metadata.start_time;
|
---|
127 |
|
---|
128 | metadataList.appendChild(metadataClone);
|
---|
129 | }
|
---|
130 |
|
---|
131 | list.appendChild(clone);
|
---|
132 | }
|
---|
133 |
|
---|
134 | const errorTemplate = document.querySelector("#errorTemplate");
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Inserts a transcription error into the DOM.
|
---|
138 | *
|
---|
139 | * @param {String} fileName
|
---|
140 | * @param {String | undefined} statusMessage
|
---|
141 | */
|
---|
142 | function insertError(fileName, statusMessage)
|
---|
143 | {
|
---|
144 | // Get the transcription list and template
|
---|
145 | let list = document.querySelector("#transcriptionsList");
|
---|
146 |
|
---|
147 | if (list == null || errorTemplate == null)
|
---|
148 | {
|
---|
149 | console.error("Could not find transcription list and/or error template");
|
---|
150 | return;
|
---|
151 | }
|
---|
152 |
|
---|
153 | // Insert a new transcription row
|
---|
154 | let clone = errorTemplate.content.firstElementChild.cloneNode(true);
|
---|
155 | let spans = clone.querySelectorAll("span");
|
---|
156 | spans[0].textContent = statusMessage;
|
---|
157 | spans[1].textContent = fileName;
|
---|
158 |
|
---|
159 | list.appendChild(clone);
|
---|
160 | }
|
---|
161 |
|
---|
162 | $(FILE_UPLOAD_INPUT_NAME).on("input", toggleUploadButtonState);
|
---|
163 |
|
---|
164 | function toggleUploadButtonState(e)
|
---|
165 | {
|
---|
166 | if (e.target.files.length <= 0) {
|
---|
167 | $(FILE_UPLOAD_BUTTON_NAME).prop("disabled", true);
|
---|
168 | }
|
---|
169 | else {
|
---|
170 | $(FILE_UPLOAD_BUTTON_NAME).prop("disabled", false);
|
---|
171 | }
|
---|
172 | }
|
---|