rein0421 commited on
Commit
399c26a
·
verified ·
1 Parent(s): d7254d9

Upload 9 files

Browse files
Files changed (3) hide show
  1. static/menu.js +49 -49
  2. static/process.js +221 -221
  3. static/register_record.js +54 -28
static/menu.js CHANGED
@@ -1,50 +1,50 @@
1
- // Show user registration page
2
- function showUserRegister() {
3
- fetch("/reset");
4
- window.location.href = "userregister";
5
- }
6
-
7
- // Show recorder page
8
- function showRecorder() {
9
- window.location.href = "index";
10
- }
11
-
12
- // Show results page
13
- function showResults() {
14
- window.location.href = "feedback";
15
- }
16
-
17
- // Show talk detail page
18
- function showTalkDetail() {
19
- window.location.href = "talk_detail";
20
- }
21
-
22
- // Reset action page
23
- function resetAction() {
24
- window.location.href = "reset_html";
25
- }
26
-
27
- // Toggle hamburger menu visibility
28
- function toggleMenu(event) {
29
- event.stopPropagation(); // Prevents click event from propagating to the document
30
- const menu = document.getElementById("menu");
31
- menu.classList.toggle("open");
32
- }
33
-
34
- // Close the menu if clicked outside
35
- function closeMenu(event) {
36
- const menu = document.getElementById("menu");
37
- if (
38
- menu.classList.contains("open") &&
39
- !menu.contains(event.target) &&
40
- !event.target.closest("#menuButton")
41
- ) {
42
- menu.classList.remove("open");
43
- }
44
- }
45
-
46
- // Add event listener for closing the menu when clicking outside
47
- document.addEventListener("click", closeMenu);
48
-
49
- // Show recorder page
50
  document.getElementById("add-btn").addEventListener("click", showRecorder);
 
1
+ // Show user registration page
2
+ function showUserRegister() {
3
+ fetch("/reset");
4
+ window.location.href = "userregister";
5
+ }
6
+
7
+ // Show recorder page
8
+ function showRecorder() {
9
+ window.location.href = "index";
10
+ }
11
+
12
+ // Show results page
13
+ function showResults() {
14
+ window.location.href = "feedback";
15
+ }
16
+
17
+ // Show talk detail page
18
+ function showTalkDetail() {
19
+ window.location.href = "talk_detail";
20
+ }
21
+
22
+ // Reset action page
23
+ function resetAction() {
24
+ window.location.href = "reset_html";
25
+ }
26
+
27
+ // Toggle hamburger menu visibility
28
+ function toggleMenu(event) {
29
+ event.stopPropagation(); // Prevents click event from propagating to the document
30
+ const menu = document.getElementById("menu");
31
+ menu.classList.toggle("open");
32
+ }
33
+
34
+ // Close the menu if clicked outside
35
+ function closeMenu(event) {
36
+ const menu = document.getElementById("menu");
37
+ if (
38
+ menu.classList.contains("open") &&
39
+ !menu.contains(event.target) &&
40
+ !event.target.closest("#menuButton")
41
+ ) {
42
+ menu.classList.remove("open");
43
+ }
44
+ }
45
+
46
+ // Add event listener for closing the menu when clicking outside
47
+ document.addEventListener("click", closeMenu);
48
+
49
+ // Show recorder page 名前に気を付けて!
50
  document.getElementById("add-btn").addEventListener("click", showRecorder);
static/process.js CHANGED
@@ -1,222 +1,222 @@
1
-
2
- let isRecording = false;
3
- let mediaRecorder;
4
- let audioChunks = [];
5
- let recordingInterval;
6
- let count_voice = 0;
7
- let before_rate = [];
8
- const RECORDING_INTERVAL_MS = 5000; // 5秒
9
- // メンバーとチャートの初期化
10
- let members = [];
11
- let voiceData = [];
12
- let baseMemberColors = ["#4caf50", "#007bff", "#ffc107", "#dc3545", "#28a745", "#9c27b0", "#ff9800"];
13
- // Chart.js の初期化
14
- const ctx = document.getElementById("speechChart").getContext("2d");
15
- const speechChart = new Chart(ctx, {
16
- type: "doughnut",
17
- data: {
18
- labels: members,
19
- datasets: [
20
- {
21
- data: voiceData,
22
- backgroundColor: getMemberColors(members.length),
23
- },
24
- ],
25
- },
26
- options: {
27
- responsive: true,
28
- plugins: {
29
- legend: {
30
- display: true,
31
- position: "bottom",
32
- labels: { color: "white" },
33
- },
34
- },
35
- },
36
- });
37
- // サーバーからメンバー情報を取得してチャートを更新する関数
38
- async function updateChartFrom() {
39
- try {
40
- const response = await fetch("/confirm");
41
- if (!response.ok) {
42
- throw new Error(`HTTP error! status: ${response.status}`);
43
- }
44
- const data = await response.json();
45
- if (!data || !data.members || !Array.isArray(data.members)) {
46
- console.error("Invalid member data received:", data);
47
- members = ["member1"];
48
- voiceData = [50, 50];
49
- updateChart();
50
- return;
51
- }
52
- members = data.members;
53
- voiceData = [];
54
- for (let i = 0; i < members.length; i++) {
55
- voiceData.push(100 / members.length);
56
- }
57
- updateChart();
58
- } catch (error) {
59
- console.error("Failed to fetch member data:", error);
60
- members = ["member1"];
61
- voiceData = [50, 50];
62
- updateChart();
63
- }
64
- }
65
- function updateChart() {
66
- // 一人モードの場合は、ユーザーとグレー(無音)の比率をチャートに表示
67
- if (members.length === 1) {
68
- const userName = members[0];
69
- speechChart.data.labels = [userName, "無音"];
70
- speechChart.data.datasets[0].backgroundColor = ["#4caf50", "#757575"];
71
- } else {
72
- // 複数メンバーの場合は通常通りの処理
73
- speechChart.data.labels = members;
74
- speechChart.data.datasets[0].backgroundColor = getMemberColors(members.length);
75
- }
76
- speechChart.data.datasets[0].data = voiceData;
77
- speechChart.update();
78
- }
79
-
80
- // ページ読み込み時にチャート情報を更新
81
- updateChartFrom();
82
- // 録音ボタンの録音開始/停止処理
83
- async function toggleRecording() {
84
- const recordButton = document.getElementById("recordButton");
85
- if (!isRecording) {
86
- // 録音開始
87
- isRecording = true;
88
- recordButton.classList.add("recording");
89
- try {
90
- const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
91
- mediaRecorder = new MediaRecorder(stream);
92
- audioChunks = [];
93
- mediaRecorder.ondataavailable = (event) => {
94
- if (event.data.size > 0) {
95
- audioChunks.push(event.data);
96
- }
97
- };
98
- mediaRecorder.onstop = () => {
99
- sendAudioChunks([...audioChunks]);
100
- audioChunks = [];
101
- };
102
- mediaRecorder.start();
103
- // 5秒ごとに録音を停止して送信するインターバルを設定
104
- recordingInterval = setInterval(() => {
105
- if (mediaRecorder && mediaRecorder.state === "recording") {
106
- mediaRecorder.stop();
107
- mediaRecorder.start();
108
- }
109
- }, RECORDING_INTERVAL_MS);
110
- } catch (error) {
111
- console.error("マイクへのアクセスに失敗しました:", error);
112
- isRecording = false;
113
- recordButton.classList.remove("recording");
114
- }
115
- } else {
116
- // 録音停止
117
- isRecording = false;
118
- recordButton.classList.remove("recording");
119
- if (mediaRecorder && mediaRecorder.state === "recording") {
120
- mediaRecorder.stop();
121
- }
122
- clearInterval(recordingInterval);
123
- count_voice = 0;
124
- //before_rate = [];
125
- }
126
- }
127
- function sendAudioChunks(chunks) {
128
- const audioBlob = new Blob(chunks, { type: "audio/wav" });
129
- const reader = new FileReader();
130
- reader.onloadend = () => {
131
- const base64String = reader.result.split(",")[1];
132
- const form = document.getElementById("recordForm");
133
- const nameInput = form.querySelector('input[name="name"]');
134
- const name = nameInput ? nameInput.value : "unknown";
135
- fetch("/upload_audio", {
136
- method: "POST",
137
- headers: { "Content-Type": "application/json" },
138
- body: JSON.stringify({ audio_data: base64String, name: name }),
139
- })
140
- .then((response) => response.json())
141
- .then((data) => {
142
- if (data.error) {
143
- alert("エラー: " + data.error);
144
- console.error(data.details);
145
- } else if (data.rate !== undefined) {
146
- updateChartData(data.rate);
147
- } else if (data.rates !== undefined) {
148
- updateChartData(data.rates);
149
- }
150
- })
151
- .catch((error) => {
152
- console.error("エラー:", error);
153
- });
154
- };
155
- reader.readAsDataURL(audioBlob);
156
- }
157
- function getMemberColors(memberCount) {
158
- // 一人モードの場合は特別な処理をしない(updateChartで処理するため)
159
- if (memberCount <= 1) {
160
- return ["#4caf50", "#757575"];
161
- } else {
162
- let colors = [];
163
- for (let i = 0; i < memberCount; i++) {
164
- colors.push(baseMemberColors[i % baseMemberColors.length]);
165
- }
166
- return colors;
167
- }
168
- }
169
- function updateChartData(newRate) {
170
- // 一人モードの時の処理
171
- if (members.length === 1) {
172
- if (count_voice === 0) {
173
- speechChart.data.datasets[0].data = [newRate, 100 - newRate];
174
- before_rate = [newRate];
175
- } else {
176
- // 一人モードでは、過去のデータと現在のデータを加重平均する
177
- let tmp_rate = (newRate + before_rate[0] * count_voice) / (count_voice + 1);
178
- speechChart.data.datasets[0].data = [tmp_rate, 100 - tmp_rate];
179
- before_rate = [tmp_rate];
180
- }
181
- count_voice++;
182
- // 一人モードでは常に緑色とグレーの組み合わせを使用
183
- speechChart.data.labels = [members[0], "無音"];
184
- speechChart.data.datasets[0].backgroundColor = ["#4caf50", "#757575"];
185
- } else {
186
- console.log(before_rate)
187
- // 複数人モードの処理
188
- if (!Array.isArray(newRate)) {
189
- console.error("newRate is not an array:", newRate);
190
- return;
191
- }
192
- if (newRate.length !== members.length) {
193
- console.error(
194
- "newRate length does not match members length:",
195
- newRate,
196
- members
197
- );
198
- return;
199
- }
200
- let averagedRates = new Array(newRate.length);
201
- for (let i = 0; i < newRate.length; i++) {
202
- let tmp_rate;
203
- if (count_voice === 0) {
204
- // 初回はそのまま
205
- tmp_rate = newRate[i];
206
- } else {
207
- // 2回目以降は、過去の平均値と現在の値を加重平均する
208
- tmp_rate = (newRate[i] + before_rate[i] * count_voice) / (count_voice + 1);
209
- }
210
- averagedRates[i] = tmp_rate;
211
- }
212
- // before_rateを更新
213
- before_rate = averagedRates;
214
- //グラフに反映
215
- speechChart.data.datasets[0].data = averagedRates;
216
- count_voice++;
217
- speechChart.data.datasets[0].backgroundColor = getMemberColors(
218
- members.length
219
- );
220
- }
221
- speechChart.update();
222
  }
 
1
+
2
+ let isRecording = false;
3
+ let mediaRecorder;
4
+ let audioChunks = [];
5
+ let recordingInterval;
6
+ let count_voice = 0;
7
+ let before_rate = [];
8
+ const RECORDING_INTERVAL_MS = 5000; // 5秒
9
+ // メンバーとチャートの初期化
10
+ let members = [];
11
+ let voiceData = [];
12
+ let baseMemberColors = ["#4caf50", "#007bff", "#ffc107", "#dc3545", "#28a745", "#9c27b0", "#ff9800"];
13
+ // Chart.js の初期化
14
+ const ctx = document.getElementById("speechChart").getContext("2d");
15
+ const speechChart = new Chart(ctx, {
16
+ type: "doughnut",
17
+ data: {
18
+ labels: members,
19
+ datasets: [
20
+ {
21
+ data: voiceData,
22
+ backgroundColor: getMemberColors(members.length),
23
+ },
24
+ ],
25
+ },
26
+ options: {
27
+ responsive: true,
28
+ plugins: {
29
+ legend: {
30
+ display: true,
31
+ position: "bottom",
32
+ labels: { color: "white" },
33
+ },
34
+ },
35
+ },
36
+ });
37
+ // サーバーからメンバー情報を取得してチャートを更新する関数
38
+ async function updateChartFrom() {
39
+ try {
40
+ const response = await fetch("/confirm");
41
+ if (!response.ok) {
42
+ throw new Error(`HTTP error! status: ${response.status}`);
43
+ }
44
+ const data = await response.json();
45
+ if (!data || !data.members || !Array.isArray(data.members)) {
46
+ console.error("Invalid member data received:", data);
47
+ members = ["member1"];
48
+ voiceData = [50, 50];
49
+ updateChart();
50
+ return;
51
+ }
52
+ members = data.members;
53
+ voiceData = [];
54
+ for (let i = 0; i < members.length; i++) {
55
+ voiceData.push(100 / members.length);
56
+ }
57
+ updateChart();
58
+ } catch (error) {
59
+ console.error("Failed to fetch member data:", error);
60
+ members = ["member1"];
61
+ voiceData = [50, 50];
62
+ updateChart();
63
+ }
64
+ }
65
+ function updateChart() {
66
+ // 一人モードの場合は、ユーザーとグレー(無音)の比率をチャートに表示
67
+ if (members.length === 1) {
68
+ const userName = members[0];
69
+ speechChart.data.labels = [userName, "無音"];
70
+ speechChart.data.datasets[0].backgroundColor = ["#4caf50", "#757575"];
71
+ } else {
72
+ // 複数メンバーの場合は通常通りの処理
73
+ speechChart.data.labels = members;
74
+ speechChart.data.datasets[0].backgroundColor = getMemberColors(members.length);
75
+ }
76
+ speechChart.data.datasets[0].data = voiceData;
77
+ speechChart.update();
78
+ }
79
+
80
+ // ページ読み込み時にチャート情報を更新
81
+ updateChartFrom();
82
+ // 録音ボタンの録音開始/停止処理
83
+ async function toggleRecording() {
84
+ const recordButton = document.getElementById("recordButton");
85
+ if (!isRecording) {
86
+ // 録音開始
87
+ isRecording = true;
88
+ recordButton.classList.add("recording");
89
+ try {
90
+ const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
91
+ mediaRecorder = new MediaRecorder(stream);
92
+ audioChunks = [];
93
+ mediaRecorder.ondataavailable = (event) => {
94
+ if (event.data.size > 0) {
95
+ audioChunks.push(event.data);
96
+ }
97
+ };
98
+ mediaRecorder.onstop = () => {
99
+ sendAudioChunks([...audioChunks]);
100
+ audioChunks = [];
101
+ };
102
+ mediaRecorder.start();
103
+ // 5秒ごとに録音を停止して送信するインターバルを設定
104
+ recordingInterval = setInterval(() => {
105
+ if (mediaRecorder && mediaRecorder.state === "recording") {
106
+ mediaRecorder.stop();
107
+ mediaRecorder.start();
108
+ }
109
+ }, RECORDING_INTERVAL_MS);
110
+ } catch (error) {
111
+ console.error("マイクへのアクセスに失敗しました:", error);
112
+ isRecording = false;
113
+ recordButton.classList.remove("recording");
114
+ }
115
+ } else {
116
+ // 録音停止
117
+ isRecording = false;
118
+ recordButton.classList.remove("recording");
119
+ if (mediaRecorder && mediaRecorder.state === "recording") {
120
+ mediaRecorder.stop();
121
+ }
122
+ clearInterval(recordingInterval);
123
+ count_voice = 0;
124
+ //before_rate = [];
125
+ }
126
+ }
127
+ function sendAudioChunks(chunks) {
128
+ const audioBlob = new Blob(chunks, { type: "audio/wav" });
129
+ const reader = new FileReader();
130
+ reader.onloadend = () => {
131
+ const base64String = reader.result.split(",")[1];
132
+ const form = document.getElementById("recordForm");
133
+ const nameInput = form.querySelector('input[name="name"]');
134
+ const name = nameInput ? nameInput.value : "unknown";
135
+ fetch("/upload_audio", {
136
+ method: "POST",
137
+ headers: { "Content-Type": "application/json" },
138
+ body: JSON.stringify({ audio_data: base64String, name: name }),
139
+ })
140
+ .then((response) => response.json())
141
+ .then((data) => {
142
+ if (data.error) {
143
+ alert("エラー: " + data.error);
144
+ console.error(data.details);
145
+ } else if (data.rate !== undefined) {
146
+ updateChartData(data.rate);
147
+ } else if (data.rates !== undefined) {
148
+ updateChartData(data.rates);
149
+ }
150
+ })
151
+ .catch((error) => {
152
+ console.error("エラー:", error);
153
+ });
154
+ };
155
+ reader.readAsDataURL(audioBlob);
156
+ }
157
+ function getMemberColors(memberCount) {
158
+ // 一人モードの場合は特別な処理をしない(updateChartで処理するため)
159
+ if (memberCount <= 1) {
160
+ return ["#4caf50", "#757575"];
161
+ } else {
162
+ let colors = [];
163
+ for (let i = 0; i < memberCount; i++) {
164
+ colors.push(baseMemberColors[i % baseMemberColors.length]);
165
+ }
166
+ return colors;
167
+ }
168
+ }
169
+ function updateChartData(newRate) {
170
+ // 一人モードの時の処理
171
+ if (members.length === 1) {
172
+ if (count_voice === 0) {
173
+ speechChart.data.datasets[0].data = [newRate, 100 - newRate];
174
+ before_rate = [newRate];
175
+ } else {
176
+ // 一人モードでは、過去のデータと現在のデータを加重平均する
177
+ let tmp_rate = (newRate + before_rate[0] * count_voice) / (count_voice + 1);
178
+ speechChart.data.datasets[0].data = [tmp_rate, 100 - tmp_rate];
179
+ before_rate = [tmp_rate];
180
+ }
181
+ count_voice++;
182
+ // 一人モードでは常に緑色とグレーの組み合わせを使用
183
+ speechChart.data.labels = [members[0], "無音"];
184
+ speechChart.data.datasets[0].backgroundColor = ["#4caf50", "#757575"];
185
+ } else {
186
+ console.log(before_rate)
187
+ // 複数人モードの処理
188
+ if (!Array.isArray(newRate)) {
189
+ console.error("newRate is not an array:", newRate);
190
+ return;
191
+ }
192
+ if (newRate.length !== members.length) {
193
+ console.error(
194
+ "newRate length does not match members length:",
195
+ newRate,
196
+ members
197
+ );
198
+ return;
199
+ }
200
+ let averagedRates = new Array(newRate.length);
201
+ for (let i = 0; i < newRate.length; i++) {
202
+ let tmp_rate;
203
+ if (count_voice === 0) {
204
+ // 初回はそのまま
205
+ tmp_rate = newRate[i];
206
+ } else {
207
+ // 2回目以降は、過去の平均値と現在の値を加重平均する
208
+ tmp_rate = (newRate[i] + before_rate[i] * count_voice) / (count_voice + 1);
209
+ }
210
+ averagedRates[i] = tmp_rate;
211
+ }
212
+ // before_rateを更新
213
+ before_rate = averagedRates;
214
+ //グラフに反映
215
+ speechChart.data.datasets[0].data = averagedRates;
216
+ count_voice++;
217
+ speechChart.data.datasets[0].backgroundColor = getMemberColors(
218
+ members.length
219
+ );
220
+ }
221
+ speechChart.update();
222
  }
static/register_record.js CHANGED
@@ -63,8 +63,9 @@ function sendAudioChunks(chunks, button) {
63
  const reader = new FileReader();
64
  reader.onloadend = () => {
65
  const base64String = reader.result.split(",")[1]; // Base64エンコードされた音声データ
66
- const form = button.closest("form");
67
- const nameInput = form.querySelector('input[name="name"]');
 
68
  const name = nameInput ? nameInput.value : "unknown"; // 名前がない
69
  fetch("/upload_base_audio", {
70
  method: "POST",
@@ -96,29 +97,54 @@ function sendAudioChunks(chunks, button) {
96
  reader.readAsDataURL(audioBlob);
97
  }
98
 
99
- document.getElementById("add-btn").addEventListener("click", () => {
100
- const newItem = document.createElement("div");
101
- newItem.className = "flex items-center gap-3 flex-wrap";
102
- newItem.innerHTML = `
103
- <form
104
- action="/submit"
105
- method="POST"
106
- class="flex items-center space-x-2 w-full sm:w-auto"
107
- onsubmit="event.preventDefault();"
108
- >
109
- <input
110
- type="text"
111
- name="name"
112
- placeholder="名前を入力"
113
- class="flex-1 px-4 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 bg-gray-700 text-white"
114
- />
115
-
116
- <button type="button" class="record-button" aria-label="音声録音開始">
117
- <div class="record-icon"></div>
118
- </button>
119
- </form>
120
- `;
121
- newItem.addEventListener("click", handleRecording);
122
- document.getElementById("people-list").appendChild(newItem);
123
- userCount++; // 新しいメンバーを追加するたびにカウントを増やす
124
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  const reader = new FileReader();
64
  reader.onloadend = () => {
65
  const base64String = reader.result.split(",")[1]; // Base64エンコードされた音声データ
66
+ // フォームの取得方法を変更
67
+ const form = button.closest(".user-item")?.querySelector("form")
68
+ const nameInput = form?.querySelector('input[name="name"]');
69
  const name = nameInput ? nameInput.value : "unknown"; // 名前がない
70
  fetch("/upload_base_audio", {
71
  method: "POST",
 
97
  reader.readAsDataURL(audioBlob);
98
  }
99
 
100
+ // 前の画面に戻る
101
+ function goBack() {
102
+ window.history.back();
103
+ }
104
+
105
+ // Add user function
106
+ function addUser() {
107
+ const userName = prompt("ユーザー名を入力してください");
108
+ if (userName) {
109
+ const userList = document.getElementById("people-list");
110
+ const userDiv = document.createElement("div");
111
+ userDiv.classList.add(
112
+ "user-item", // 追加
113
+ "bg-gray-700",
114
+ "p-4",
115
+ "rounded-lg",
116
+ "text-white",
117
+ "flex",
118
+ "justify-between",
119
+ "items-center",
120
+ "flex-wrap", // 追加
121
+ "gap-3" // 追加
122
+ );
123
+ userDiv.innerHTML = `
124
+ <form
125
+ action="/submit"
126
+ method="POST"
127
+ class="flex items-center space-x-2 w-full sm:w-auto"
128
+ onsubmit="event.preventDefault();"
129
+ >
130
+ <input
131
+ type="text"
132
+ name="name"
133
+ placeholder="名前を入力"
134
+ value="${userName}"
135
+ class="flex-1 px-4 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 bg-gray-700 text-white"
136
+ />
137
+ <button type="button" class="record-button" aria-label="音声録音開始">
138
+ <div class="record-icon"></div>
139
+ </button>
140
+ </form>
141
+ `;
142
+ userDiv
143
+ .querySelector(".record-button")
144
+ .addEventListener("click", handleRecording);
145
+ userList.appendChild(userDiv);
146
+ userCount++;
147
+ }
148
+ }
149
+
150
+ document.getElementById("add-btn").addEventListener("click", addUser);