-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
113 lines (93 loc) · 3.96 KB
/
Copy pathapp.js
File metadata and controls
113 lines (93 loc) · 3.96 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
const URL = 'https://teachablemachine.withgoogle.com/models/f_QTuK-Ln/';
let recognizer;
let indianCount = 0;
let americanCount = 0;
let totalPredictions = 0;
let listening = false;
async function createModel() {
const checkpointURL = URL + 'model.json';
const metadataURL = URL + 'metadata.json';
recognizer = speechCommands.create(
'BROWSER_FFT',
undefined,
checkpointURL,
metadataURL
);
await recognizer.ensureModelLoaded();
}
async function init() {
if (!recognizer) {
await createModel(); // Load the model if not already loaded
}
const classLabels = recognizer.wordLabels();
const labelContainer = document.getElementById('label-container');
for (let i = 0; i < classLabels.length; i++) {
labelContainer.appendChild(document.createElement('div'));
}
listening = true;
recognizer.listen(result => {
if (!listening) return; // Exit if not listening
const scores = result.scores;
for (let i = 0; i < classLabels.length; i++) {
const classPrediction = classLabels[i] + ': ' + result.scores[i].toFixed(2);
labelContainer.childNodes[i].innerHTML = classPrediction;
}
// Assuming Indian accent is in index 0 and American is in index 1 (adjust accordingly)
if (scores[0] > scores[1]) {
indianCount++;
} else {
americanCount++;
}
totalPredictions++;
}, {
includeSpectrogram: true,
probabilityThreshold: 0.75,
overlapFactor: 0.50
});
document.getElementById('result').innerHTML = 'Listening...';
}
function stopRecording() {
if (recognizer) {
recognizer.stopListening();
listening = false;
const indianPercentage = (indianCount / totalPredictions) * 100;
const americanPercentage = (americanCount / totalPredictions) * 100;
const finalAccent = indianPercentage > americanPercentage ? 'Indian' : 'American';
if (indianPercentage > americanPercentage) {
document.getElementById('result').innerHTML = `Your accent is ${indianPercentage.toFixed(2)}% Indian (Foreign)`;
} else {
document.getElementById('result').innerHTML = `Your accent is ${americanPercentage.toFixed(2)}% American`;
}
if (indianPercentage > 30) {
// Display the message
document.getElementById('extraMessage').innerHTML =
`Based on the sample, your accent is ${indianPercentage.toFixed(2)}% foreign. Let's work on that.`;
// Create a button element
const button = document.createElement('button');
button.innerHTML = "Work on Accent";
button.onclick = function() {
window.location.href = 'practice.html'; // Replace with the actual page URL
};
// Add the button to the DOM
document.getElementById('buttonContainer').appendChild(button);
} else {
// Redirect to another page if the accent is not foreign (less than 30%)
window.location.href = 'end.html'; // Replace with the URL for the non-foreign accent page
}
// Reset the counters for the next session
indianCount = 0;
americanCount = 0;
totalPredictions = 0;
}
}
// Example JavaScript to set progress and streak
const progressCircle = document.getElementById('progressCircle');
const progressPercentage = document.getElementById('progressPercentage');
// Example progress value (75% completion)
const progressValue = 75;
progressCircle.style.setProperty('--progress', `${progressValue}%`);
progressPercentage.textContent = `${progressValue}%`;
// Example streak value
const streakCount = 3; // This could be fetched from local storage or server
document.querySelector('.streak-container p').innerHTML =
`<i class="fa-solid fa-calendar"></i> You’re on a <strong>${streakCount}-day streak!</strong> Keep going!`;