File size: 5,755 Bytes
b412841
9daa331
b412841
9daa331
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b412841
 
9daa331
 
 
 
 
 
722255b
9daa331
5751736
9daa331
 
 
 
5751736
722255b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9daa331
 
 
b412841
9daa331
 
 
 
 
ae2e16c
 
9daa331
 
ae2e16c
9daa331
 
 
ae2e16c
9daa331
ae2e16c
 
 
 
722255b
 
9daa331
 
 
 
 
 
 
 
 
 
722255b
 
 
 
 
 
 
 
 
 
9daa331
 
 
ae2e16c
 
 
 
9daa331
ae2e16c
9daa331
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
722255b
 
 
9daa331
 
b412841
9daa331
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
<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Voice Recorder Interface</title>
  <style>
    body {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      height: 100vh;
      margin: 0;
      background-color: #121212;
      color: white;
    }
    .chart {
      width: 300px;
      height: 300px;
      margin-bottom: 20px;
    }
    .record-button {
      position: fixed;
      bottom: 30px;
      width: 80px;
      height: 80px;
      background-color: transparent;
      border-radius: 50%;
      border: 4px solid white;
      display: flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
      box-shadow: 0 4px 6px rgba(0, 0, 0, 0.4);
      transition: all 0.2s ease;
    }
    .record-icon {
      width: 60px;
      height: 60px;
      background-color: #d32f2f;
      border-radius: 50%;
      transition: all 0.2s ease;
    }
    .recording .record-icon {
      width: 40px;
      height: 40px;
      border-radius: 10%;
    }
    .result-button {
      margin-top: 20px;
      padding: 10px 20px;
      background-color: #4caf50;
      border: none;
      border-radius: 5px;
      color: white;
      cursor: pointer;
      box-shadow: 0 4px 6px rgba(0, 0, 0, 0.4);
    }
    .result-button:hover {
      background-color: #388e3c;
    }
  </style>
  <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
  <div class="chart">
    <canvas id="speechChart"></canvas>
  </div>
  <button class="record-button" id="recordButton" onclick="toggleRecording()">
    <div class="record-icon" id="recordIcon"></div>
  </button>
  <!-- 結果表示用のボタンは、必要に応じて別画面に遷移させるなどの実装に変更可能です -->
  <button class="result-button" id="resultButton" onclick="showResults()">結果を表示</button>

  <script>
    let isRecording = false;
    let mediaRecorder;
    let audioChunks = [];

    // Chart.jsの初期化
    const ctx = document.getElementById('speechChart').getContext('2d');
    const speechChart = new Chart(ctx, {
      type: 'doughnut',
      data: {
        labels: ['自分', '他の人'],
        datasets: [{
          // 初期値は仮のデータとして [30, 70] を設定
          data: [30, 70],
          backgroundColor: ['#4caf50', '#757575'],
        }],
      },
      options: {
        responsive: true,
        plugins: {
          legend: {
            display: true,
            position: 'bottom',
            labels: {
              color: 'white'
            }
          }
        }
      }
    });

    async function toggleRecording() {
      const recordButton = document.getElementById('recordButton');
      const recordIcon = document.getElementById('recordIcon');

      if (!isRecording) {
        // 録音開始
        isRecording = true;
        recordButton.classList.add('recording');
        try {
          const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
          mediaRecorder = new MediaRecorder(stream);
          audioChunks = [];

          mediaRecorder.ondataavailable = event => {
            if (event.data.size > 0) {
              audioChunks.push(event.data);
            }
          };

          mediaRecorder.onstop = () => {
            const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
            const reader = new FileReader();
            reader.onloadend = () => {
              // Base64エンコードされた文字列を取得
              const base64String = reader.result.split(',')[1];
              // サーバーへ音声データを送信
              fetch('/upload_audio', {
                method: 'POST',
                headers: {
                  'Content-Type': 'application/json',
                },
                body: JSON.stringify({ audio_data: base64String }),
              })
              .then(response => response.json())
              .then(data => {
                if(data.error) {
                  alert('エラー: ' + data.error);
                  console.error(data.details);
                  return;
                }
                // data.rate はパーセンテージ(例: 40なら自分の音声が40%)\n
                // 円グラフのデータを更新:自分の音声をdata.rate、他の音声を(100 - data.rate)として設定
                speechChart.data.datasets[0].data = [data.rate, 100 - data.rate];
                speechChart.update();
                alert('音声の解析が完了しました。自分の音声: ' + data.rate.toFixed(2) + '%, 他の人: ' + (100 - data.rate).toFixed(2) + '%');
              })
              .catch(error => {
                console.error('エラー:', error);
              });
            };
            reader.readAsDataURL(audioBlob);
          };

          mediaRecorder.start();
        } catch (error) {
          console.error('マイクへのアクセスに失敗しました:', error);
          isRecording = false;
          recordButton.classList.remove('recording');
        }
      } else {
        // 録音停止
        isRecording = false;
        recordButton.classList.remove('recording');
        if (mediaRecorder && mediaRecorder.state !== 'inactive') {
          mediaRecorder.stop();
        }
      }
    }

    function showResults() {
      // 結果表示の別画面へ遷移する場合の処理例(必要に応じて実装してください):
      // window.location.href = 'feedback.html';
      alert('結果は円グラフ上に反映されています。');
    }
  </script>
</body>
</html>