File size: 11,371 Bytes
b46a7cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be3690e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b46a7cb
 
be3690e
b46a7cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
class VoiceTrack extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: 'open' });
    this.recording = false;
    this.mediaRecorder = null;
    this.audioChunks = [];
    this.analysisResult = null;
  }

  connectedCallback() {
    this.shadowRoot.innerHTML = `
      <style>
        :host {
          display: block;
          margin: 2rem 0;
        }
        .container {
          background: rgba(15, 23, 42, 0.7);
          backdrop-filter: blur(10px);
          border: 1px solid rgba(255, 255, 255, 0.1);
          border-radius: 1rem;
          padding: 2rem;
        }
        .header {
          display: flex;
          align-items: center;
          margin-bottom: 1.5rem;
        }
        .icon {
          width: 3rem;
          height: 3rem;
          background: rgba(124, 58, 237, 0.2);
          border-radius: 50%;
          display: flex;
          align-items: center;
          justify-content: center;
          margin-right: 1rem;
        }
        h2 {
          font-size: 1.5rem;
          font-weight: 600;
          margin: 0;
          background: linear-gradient(90deg, #7c3aed 0%, #2563eb 100%);
          -webkit-background-clip: text;
          background-clip: text;
          color: transparent;
        }
        .controls {
          display: flex;
          gap: 1rem;
          margin-bottom: 1.5rem;
        }
        button {
          flex: 1;
          padding: 0.75rem 1.5rem;
          border-radius: 0.5rem;
          font-weight: 500;
          display: flex;
          align-items: center;
          justify-content: center;
          gap: 0.5rem;
          cursor: pointer;
          transition: all 0.2s;
          border: none;
        }
        .record-btn {
          background: #7c3aed;
          color: white;
        }
        .record-btn:hover {
          background: #6d28d9;
        }
        .record-btn.recording {
          background: #dc2626;
          animation: pulse 1.5s infinite;
        }
        .analyze-btn {
          background: #2563eb;
          color: white;
        }
        .analyze-btn:hover {
          background: #1d4ed8;
        }
        .analyze-btn:disabled {
          opacity: 0.5;
          cursor: not-allowed;
        }
        .timer {
          font-size: 1.25rem;
          font-weight: 600;
          color: #7c3aed;
          text-align: center;
          margin: 1rem 0;
        }
        .results {
          display: none;
          margin-top: 1.5rem;
        }
        .metric {
          display: flex;
          justify-content: space-between;
          margin-bottom: 0.75rem;
          padding-bottom: 0.75rem;
          border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }
        .metric-label {
          font-weight: 500;
        }
        .metric-value {
          font-weight: 600;
          color: #7c3aed;
        }
        .progress-bar {
          height: 8px;
          border-radius: 4px;
          background: rgba(124, 58, 237, 0.2);
          margin-top: 0.5rem;
        }
        .progress-fill {
          height: 100%;
          border-radius: 4px;
          background: linear-gradient(90deg, #7c3aed 0%, #2563eb 100%);
          width: 0%;
          transition: width 0.3s ease;
        }
        @keyframes pulse {
          0% { opacity: 1; }
          50% { opacity: 0.7; }
          100% { opacity: 1; }
        }
      </style>
      <div class="container">
        <div class="header">
          <div class="icon">
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
              <path d="M12 1a3 3 0 0 0-3 3v8a3 3 0 0 0 6 0V4a3 3 0 0 0-3-3z"></path>
              <path d="M19 10v2a7 7 0 0 1-14 0v-2"></path>
              <line x1="12" y1="19" x2="12" y2="23"></line>
              <line x1="8" y1="23" x2="16" y2="23"></line>
            </svg>
          </div>
          <h2>VoiceTrack Analysis</h2>
        </div>

        <p>Record your voice to analyze pronunciation and confidence levels.</p>

        <div class="controls">
          <button class="record-btn" id="recordBtn">
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
              <circle cx="12" cy="12" r="10"></circle>
              <circle cx="12" cy="12" r="3"></circle>
            </svg>
            Record
          </button>
          <button class="analyze-btn" id="analyzeBtn" disabled>
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
              <path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"></path>
            </svg>
            Analyze
          </button>
        </div>

        <div class="timer" id="timer">00:00</div>

        <div class="results" id="results">
          <h3>Analysis Results</h3>
          <div class="metric">
            <span class="metric-label">Pronunciation Accuracy</span>
            <span class="metric-value" id="pronunciationScore">0%</span>
          </div>
          <div class="progress-bar">
            <div class="progress-fill" id="pronunciationBar"></div>
          </div>

          <div class="metric">
            <span class="metric-label">Confidence Level</span>
            <span class="metric-value" id="confidenceScore">0%</span>
          </div>
          <div class="progress-bar">
            <div class="progress-fill" id="confidenceBar"></div>
          </div>

          <div class="metric">
            <span class="metric-label">Fluency</span>
            <span class="metric-value" id="fluencyScore">0%</span>
          </div>
          <div class="progress-bar">
            <div class="progress-fill" id="fluencyBar"></div>
          </div>

          <div class="metric">
            <span class="metric-label">Clarity</span>
            <span class="metric-value" id="clarityScore">0%</span>
          </div>
          <div class="progress-bar">
            <div class="progress-fill" id="clarityBar"></div>
          </div>
        </div>
      </div>
    `;

    this.recordBtn = this.shadowRoot.getElementById('recordBtn');
    this.analyzeBtn = this.shadowRoot.getElementById('analyzeBtn');
    this.timer = this.shadowRoot.getElementById('timer');
    this.results = this.shadowRoot.getElementById('results');

    this.setupEventListeners();
  }

  setupEventListeners() {
    this.recordBtn.addEventListener('click', () => {
      if (this.recording) {
        this.stopRecording();
      } else {
        this.startRecording();
      }
    });

    this.analyzeBtn.addEventListener('click', () => {
      this.analyzeRecording();
    });
  }

  async startRecording() {
    try {
      const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
      this.mediaRecorder = new MediaRecorder(stream);
      this.audioChunks = [];

      this.mediaRecorder.ondataavailable = event => {
        this.audioChunks.push(event.data);
      };

      this.mediaRecorder.onstop = () => {
        this.recording = false;
        this.recordBtn.classList.remove('recording');
        this.analyzeBtn.disabled = false;
        clearInterval(this.timerInterval);
      };

      this.mediaRecorder.start();
      this.recording = true;
      this.recordBtn.classList.add('recording');
      this.analyzeBtn.disabled = true;
      this.results.style.display = 'none';

      // Start timer
      let seconds = 0;
      this.timerInterval = setInterval(() => {
        seconds++;
        const minutes = Math.floor(seconds / 60);
        const remainingSeconds = seconds % 60;
        this.timer.textContent = `${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
      }, 1000);
    } catch (error) {
      console.error('Error accessing microphone:', error);
      alert('Could not access microphone. Please check permissions.');
    }
  }

  stopRecording() {
    if (this.mediaRecorder && this.recording) {
      this.mediaRecorder.stop();
      this.recording = false;
      this.mediaRecorder.stream.getTracks().forEach(track => track.stop());
    }
  }
  async analyzeRecording() {
    if (this.audioChunks.length === 0) return;

    this.analyzeBtn.disabled = true;
    this.analyzeBtn.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"></path></svg> Analyzing...';

    const audioBlob = new Blob(this.audioChunks, { type: 'audio/wav' });
    
    try {
      // Simulate API response for demo
      await new Promise(resolve => setTimeout(resolve, 1500));
      
      // Check if we're in a game context
      const isGamePage = window.location.pathname.includes('gamequest');
      
      if (isGamePage) {
        // Game-specific analysis with higher scores
        this.analysisResult = {
          pronunciation: Math.floor(Math.random() * 20) + 80,
          confidence: Math.floor(Math.random() * 20) + 80,
          fluency: Math.floor(Math.random() * 20) + 80,
          clarity: Math.floor(Math.random() * 20) + 80
        };
        
        // Update game score if on game page
        if (typeof updateScore === 'function') {
          const points = Math.floor(this.analysisResult.confidence / 10);
          updateScore(points);
        }
      } else {
        // Regular analysis
        this.analysisResult = {
          pronunciation: Math.floor(Math.random() * 30) + 70,
          confidence: Math.floor(Math.random() * 30) + 70,
          fluency: Math.floor(Math.random() * 30) + 70,
          clarity: Math.floor(Math.random() * 30) + 70
        };
      }

      this.displayResults();
} catch (error) {
      console.error('Error analyzing recording:', error);
      alert('Error analyzing recording. Please try again.');
    } finally {
      this.analyzeBtn.disabled = false;
      this.analyzeBtn.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"></path></svg> Analyze';
    }
  }

  displayResults() {
    this.results.style.display = 'block';
    
    this.shadowRoot.getElementById('pronunciationScore').textContent = `${this.analysisResult.pronunciation}%`;
    this.shadowRoot.getElementById('pronunciationBar').style.width = `${this.analysisResult.pronunciation}%`;
    
    this.shadowRoot.getElementById('confidenceScore').textContent = `${this.analysisResult.confidence}%`;
    this.shadowRoot.getElementById('confidenceBar').style.width = `${this.analysisResult.confidence}%`;
    
    this.shadowRoot.getElementById('fluencyScore').textContent = `${this.analysisResult.fluency}%`;
    this.shadowRoot.getElementById('fluencyBar').style.width = `${this.analysisResult.fluency}%`;
    
    this.shadowRoot.getElementById('clarityScore').textContent = `${this.analysisResult.clarity}%`;
    this.shadowRoot.getElementById('clarityBar').style.width = `${this.analysisResult.clarity}%`;
  }
}

customElements.define('voice-track', VoiceTrack);