-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
354 lines (291 loc) · 12.2 KB
/
Copy pathapp.js
File metadata and controls
354 lines (291 loc) · 12.2 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
const stimuliConsonants = ["B", "D", "G", "K", "N", "S", "SH", "T", "V", "Z"];
const displayConsonants = ["B", "D", "G", "K", "N", "S", "SH", "T", "V", "Z", "???"];
const practiceFiles = [
"VCV_asa_1_60SNR.wav", "VCV_asa_2_60SNR.wav", "VCV_aba_1_60SNR.wav", "VCV_asha_1_60SNR.wav",
"VCV_aba_2_60SNR.wav", "VCV_asha_2_60SNR.wav", "VCV_ada_1_60SNR.wav", "VCV_ata_1_60SNR.wav",
"VCV_ada_2_60SNR.wav", "VCV_ata_2_60SNR.wav", "VCV_aga_1_60SNR.wav", "VCV_ava_1_60SNR.wav",
"VCV_aga_2_60SNR.wav", "VCV_ava_2_60SNR.wav", "VCV_aka_1_60SNR.wav", "VCV_aza_1_60SNR.wav",
"VCV_aka_2_60SNR.wav", "VCV_aza_2_60SNR.wav", "VCV_ana_1_60SNR.wav", "VCV_ana_2_60SNR.wav"
];
const testFilesBase = [
"VCV_aba_2_18SNR.wav", "VCV_aga_1_3SNR.wav", "VCV_aka_2_0SNR.wav", "VCV_asa_1_0SNR.wav",
"VCV_asha_2_0SNR.wav", "VCV_ava_1_18SNR.wav", "VCV_aza_2_0SNR.wav", "VCV_ada_1_4SNR.wav",
"VCV_aga_2_3SNR.wav", "VCV_ana_1_15SNR.wav", "VCV_asa_2_0SNR.wav", "VCV_ata_1_0SNR.wav",
"VCV_ava_2_18SNR.wav", "VCV_aba_1_18SNR.wav", "VCV_ada_2_4SNR.wav", "VCV_aka_1_0SNR.wav",
"VCV_ana_2_15SNR.wav", "VCV_asha_1_0SNR.wav", "VCV_ata_2_0SNR.wav", "VCV_aza_1_0SNR.wav"
];
let state = "IDLE";
let currentQueue = [];
let currentStimulus = null;
let trialResults = [];
let confusionMatrix = {};
let isWaitingForAudio = false;
// Progress tracking
let totalPhaseTrials = 0;
let currentTrialNumber = 0;
// Web Audio API context (Tone)
let audioCtx;
let calibOsc;
let calibGain;
// HTML Audio context (Speech Stream - v3 style)
let calibSpeechAudio = new Audio("Stimuli/practice/VCV_aba_1_60SNR.wav");
calibSpeechAudio.loop = true;
let isCalibrating = false;
// Elements
const grid = document.getElementById('consonant-grid');
const startBtn = document.getElementById('start-btn');
const interfaceDiv = document.getElementById('interface');
const resultsArea = document.getElementById('results-area');
const statusMsg = document.getElementById('status-msg');
const phaseTitle = document.getElementById('phase-title');
const downloadBtn = document.getElementById('download-btn');
const setupArea = document.getElementById('setup-area');
const calibBtn = document.getElementById('calib-btn');
const progressBar = document.getElementById('test-progress');
const currentTrialSpan = document.getElementById('trial-current');
const totalTrialSpan = document.getElementById('trial-total');
const calibRadios = document.getElementsByName('calib-source');
// Initialize Data Structure
stimuliConsonants.forEach(s => {
confusionMatrix[s] = {};
displayConsonants.forEach(r => {
confusionMatrix[s][r] = 0;
});
});
// Initialize Grid
displayConsonants.forEach(c => {
if (c === "???") {
const spacer = document.createElement('div');
spacer.className = "grid-spacer";
grid.appendChild(spacer);
}
const btn = document.createElement('button');
btn.innerText = c;
btn.className = "consonant-btn";
btn.dataset.consonant = c;
btn.onclick = () => handleResponse(c);
grid.appendChild(btn);
});
// Event Listeners
startBtn.addEventListener('click', startPhase);
downloadBtn.addEventListener('click', downloadCSV);
// Stop calibration automatically if they switch options while it's playing
calibRadios.forEach(radio => {
radio.addEventListener('change', () => {
if (isCalibrating) {
stopCalibration();
}
});
});
calibBtn.addEventListener('click', () => {
if (!isCalibrating) {
startCalibration();
} else {
stopCalibration();
}
});
function startCalibration() {
const selectedSource = document.querySelector('input[name="calib-source"]:checked').value;
if (selectedSource === 'tone') {
if (!audioCtx) {
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
}
calibOsc = audioCtx.createOscillator();
calibGain = audioCtx.createGain();
calibOsc.type = 'sine';
calibOsc.frequency.value = 1000; // 1 kHz Tone
const targetAmplitude = 0.01 * Math.SQRT2; // 0.01 RMS
// 50 ms onset ramp
calibGain.gain.setValueAtTime(0, audioCtx.currentTime);
calibGain.gain.linearRampToValueAtTime(targetAmplitude, audioCtx.currentTime + 0.05);
calibOsc.connect(calibGain);
calibGain.connect(audioCtx.destination);
calibOsc.start();
} else if (selectedSource === 'speech') {
calibSpeechAudio.play().catch(e => {
console.error("Audio playback blocked", e);
});
}
calibBtn.innerText = "Stop Calibration Audio";
calibBtn.classList.add('active');
isCalibrating = true;
}
function stopCalibration() {
if (!isCalibrating) return;
// Handle Tone offset
if (audioCtx && calibGain && calibOsc) {
calibGain.gain.setValueAtTime(calibGain.gain.value, audioCtx.currentTime);
calibGain.gain.linearRampToValueAtTime(0, audioCtx.currentTime + 0.05);
calibOsc.stop(audioCtx.currentTime + 0.05);
}
// Handle Speech offset (v3 style)
calibSpeechAudio.pause();
calibSpeechAudio.currentTime = 0;
calibBtn.innerText = "Play Calibration Audio";
calibBtn.classList.remove('active');
isCalibrating = false;
}
function startPhase() {
// Standard audio unlock for browsers (prevents Autoplay blocks if user clicks start too fast)
const unlockAudio = new Audio();
unlockAudio.play().catch(e => {});
// Revert to single column layout for the actual testing grid
document.getElementById('app-container').classList.remove('two-column-mode');
stopCalibration();
startBtn.classList.add('hidden');
setupArea.classList.add('hidden');
interfaceDiv.classList.remove('hidden');
// Reset button styles for later phases
startBtn.style.cssText = "background-color: #fff; border: 1px solid #ccc; padding: 10px 20px; font-weight: bold; font-size: 18px; text-align: center; color: #555; text-decoration: none;";
if (state === "IDLE") {
state = "PRACTICE";
phaseTitle.innerText = "Practice Phase";
statusMsg.innerHTML = "Listen to the stimulus and select the consonant you heard.<br>If you’re unsure, take your best guess.<br>Select <strong>???</strong> only if you think you heard a consonant that is not one of the choices.";
let practiceSelection = practiceFiles.filter(f => f.includes('_1_'));
currentQueue = shuffle([...practiceSelection]);
totalPhaseTrials = currentQueue.length;
currentTrialNumber = 0;
updateProgressUI();
setupNextTrial();
} else if (state === "PRACTICE_DONE") {
state = "GRADED";
phaseTitle.innerText = "Graded Test Phase";
statusMsg.innerHTML = "Test in progress. Noise has been added. No feedback will be provided.<br><br>Listen to the stimulus and select the consonant you heard.<br>If you’re unsure, take your best guess.<br>Select <strong>???</strong> only if you think you heard a consonant that is not one of the choices.";
let fullTestArray = [];
for (let i = 0; i < 5; i++) {
let shuffledBlock = shuffle([...testFilesBase]);
fullTestArray = fullTestArray.concat(shuffledBlock);
}
currentQueue = fullTestArray;
totalPhaseTrials = currentQueue.length;
currentTrialNumber = 0;
updateProgressUI();
setupNextTrial();
}
}
function updateProgressUI() {
progressBar.max = totalPhaseTrials;
progressBar.value = currentTrialNumber;
currentTrialSpan.innerText = currentTrialNumber === 0 ? 1 : currentTrialNumber;
totalTrialSpan.innerText = totalPhaseTrials;
}
function setupNextTrial() {
resetButtons();
isWaitingForAudio = true;
if (currentQueue.length === 0) {
endPhase();
return;
}
currentStimulus = currentQueue.shift();
currentTrialNumber++;
updateProgressUI();
setTimeout(() => {
const folder = state === "PRACTICE" ? "Stimuli/practice/" : "Stimuli/test/";
const audio = new Audio(folder + currentStimulus);
audio.onended = () => { isWaitingForAudio = false; };
audio.onerror = () => {
console.warn("Audio missing: " + currentStimulus);
isWaitingForAudio = false;
};
audio.play().catch(e => {
console.error("Audio failed", e);
isWaitingForAudio = false;
});
}, 500);
}
function handleResponse(selected) {
if (isWaitingForAudio) return;
const actual = parseConsonant(currentStimulus);
isWaitingForAudio = true;
if (state === "PRACTICE") {
const buttons = document.querySelectorAll('.consonant-btn');
buttons.forEach(btn => {
if (btn.dataset.consonant === actual) {
btn.classList.add('correct-highlight');
}
});
setTimeout(setupNextTrial, 1500);
} else if (state === "GRADED") {
trialResults.push({ stimulus: currentStimulus, target: actual, response: selected });
confusionMatrix[actual][selected]++;
setTimeout(setupNextTrial, 500);
}
}
function endPhase() {
interfaceDiv.classList.add('hidden');
if (state === "PRACTICE") {
state = "PRACTICE_DONE";
phaseTitle.innerText = "Practice Complete";
statusMsg.innerText = "You have completed the practice phase. The graded test will now begin. There will be 100 trials with noise added, and no feedback will be provided.";
startBtn.innerText = "Step 3: Begin Graded Test";
startBtn.classList.remove('hidden');
} else if (state === "GRADED") {
state = "FINISHED";
phaseTitle.innerText = "Test Complete";
statusMsg.innerText = "Thank you. The test is now complete.";
resultsArea.classList.remove('hidden');
renderHeatmap();
}
}
function resetButtons() {
const buttons = document.querySelectorAll('.consonant-btn');
buttons.forEach(btn => {
btn.classList.remove('correct-highlight');
});
}
function parseConsonant(filename) {
const part = filename.split('_')[1];
return part.substring(1, part.length - 1).toUpperCase();
}
function shuffle(array) {
let currentIndex = array.length, randomIndex;
while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
[array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]];
}
return array;
}
function renderHeatmap() {
const container = document.getElementById('heatmap-container');
let html = '<table><tr><th>Actual \ Resp</th>';
displayConsonants.forEach(c => html += `<th>${c}</th>`);
html += '</tr>';
stimuliConsonants.forEach(actual => {
html += `<tr><td><strong>${actual}</strong></td>`;
displayConsonants.forEach(response => {
const count = confusionMatrix[actual][response];
const intensity = count / 10;
const color = `rgba(255, 100, 0, ${intensity})`;
html += `<td style="background-color: ${color}">${count}</td>`;
});
html += '</tr>';
});
html += '</table>';
container.innerHTML = html;
}
function downloadCSV() {
let csvContent = "data:text/csv;charset=utf-8,Trial,Phase,Stimulus,Target,Response\n";
trialResults.forEach((r, index) => {
csvContent += `${index + 1},Graded,${r.stimulus},${r.target},${r.response}\n`;
});
// Dynamic Filename Logic: qVCV_NH001_L_20260511.csv
let pId = document.getElementById('participant-id').value.trim();
if (!pId) {
pId = "UNKNOWN"; // Fallback if left blank
}
const ear = document.getElementById('test-ear').value.charAt(0); // L, R, or B
const date = new Date();
const yyyy = date.getFullYear();
const mm = String(date.getMonth() + 1).padStart(2, '0');
const dd = String(date.getDate()).padStart(2, '0');
const dateString = `${yyyy}${mm}${dd}`;
const filename = `qVCV_${pId}_${ear}_${dateString}.csv`;
const encodedUri = encodeURI(csvContent);
const link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", filename);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}