Spaces:
Running
Running
Update index.html
Browse files- index.html +56 -18
index.html
CHANGED
@@ -20,7 +20,7 @@
|
|
20 |
border-radius: 10px;
|
21 |
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
22 |
}
|
23 |
-
button {
|
24 |
background-color: #4CAF50;
|
25 |
border: none;
|
26 |
color: white;
|
@@ -44,15 +44,38 @@
|
|
44 |
<body>
|
45 |
<div class="container">
|
46 |
<h1>Vocaloid с генетическим алгоритмом</h1>
|
47 |
-
<button onclick="
|
|
|
|
|
48 |
<div id="output"></div>
|
49 |
</div>
|
50 |
|
51 |
<script>
|
52 |
const synth = window.speechSynthesis;
|
53 |
const outputDiv = document.getElementById('output');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
-
// Генетический алгоритм
|
56 |
class GeneticAlgorithm {
|
57 |
constructor(populationSize = 50, mutationRate = 0.01) {
|
58 |
this.populationSize = populationSize;
|
@@ -105,7 +128,7 @@
|
|
105 |
selectParent() {
|
106 |
const totalFitness = this.population.reduce((sum, individual) => sum + individual.fitness, 0);
|
107 |
let randomValue = Math.random() * totalFitness;
|
108 |
-
|
109 |
randomValue -= individual.fitness;
|
110 |
if (randomValue <= 0) {
|
111 |
return individual;
|
@@ -151,22 +174,28 @@
|
|
151 |
);
|
152 |
}
|
153 |
}
|
|
|
154 |
|
155 |
-
// Функция для
|
156 |
-
function
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
// Эволюция на протяжении нескольких поколений
|
161 |
-
for (let i = 0; i < 10; i++) {
|
162 |
-
ga.evolve();
|
163 |
}
|
164 |
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
}
|
171 |
|
172 |
// Функция для составления песни из последовательностей
|
@@ -181,11 +210,20 @@
|
|
181 |
|
182 |
// Функция для воспроизведения текста
|
183 |
function speak(text) {
|
|
|
184 |
const utterance = new SpeechSynthesisUtterance(text);
|
185 |
-
|
|
|
|
|
|
|
|
|
186 |
utterance.rate = 0.8;
|
187 |
synth.speak(utterance);
|
188 |
}
|
|
|
|
|
|
|
|
|
189 |
</script>
|
190 |
</body>
|
191 |
</html>
|
|
|
20 |
border-radius: 10px;
|
21 |
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
22 |
}
|
23 |
+
button, select {
|
24 |
background-color: #4CAF50;
|
25 |
border: none;
|
26 |
color: white;
|
|
|
44 |
<body>
|
45 |
<div class="container">
|
46 |
<h1>Vocaloid с генетическим алгоритмом</h1>
|
47 |
+
<button onclick="startLearning()">Начать обучение</button>
|
48 |
+
<button onclick="stopLearning()">Остановить обучение</button>
|
49 |
+
<select id="voiceSelect"></select>
|
50 |
<div id="output"></div>
|
51 |
</div>
|
52 |
|
53 |
<script>
|
54 |
const synth = window.speechSynthesis;
|
55 |
const outputDiv = document.getElementById('output');
|
56 |
+
const voiceSelect = document.getElementById('voiceSelect');
|
57 |
+
let learningInterval;
|
58 |
+
let ga;
|
59 |
+
|
60 |
+
// Заполнение списка голосов
|
61 |
+
function populateVoiceList() {
|
62 |
+
const voices = synth.getVoices();
|
63 |
+
voiceSelect.innerHTML = '';
|
64 |
+
voices.forEach((voice, i) => {
|
65 |
+
const option = document.createElement('option');
|
66 |
+
option.textContent = `${voice.name} (${voice.lang})`;
|
67 |
+
option.setAttribute('data-lang', voice.lang);
|
68 |
+
option.setAttribute('data-name', voice.name);
|
69 |
+
voiceSelect.appendChild(option);
|
70 |
+
});
|
71 |
+
}
|
72 |
+
|
73 |
+
populateVoiceList();
|
74 |
+
if (speechSynthesis.onvoiceschanged !== undefined) {
|
75 |
+
speechSynthesis.onvoiceschanged = populateVoiceList;
|
76 |
+
}
|
77 |
|
78 |
+
// Генетический алгоритм (оставляем без изменений)
|
79 |
class GeneticAlgorithm {
|
80 |
constructor(populationSize = 50, mutationRate = 0.01) {
|
81 |
this.populationSize = populationSize;
|
|
|
128 |
selectParent() {
|
129 |
const totalFitness = this.population.reduce((sum, individual) => sum + individual.fitness, 0);
|
130 |
let randomValue = Math.random() * totalFitness;
|
131 |
+
for (const individual of this.population) {
|
132 |
randomValue -= individual.fitness;
|
133 |
if (randomValue <= 0) {
|
134 |
return individual;
|
|
|
174 |
);
|
175 |
}
|
176 |
}
|
177 |
+
}
|
178 |
|
179 |
+
// Функция для начала обучения
|
180 |
+
function startLearning() {
|
181 |
+
if (!ga) {
|
182 |
+
ga = new GeneticAlgorithm();
|
183 |
+
ga.initialize();
|
|
|
|
|
|
|
184 |
}
|
185 |
|
186 |
+
learningInterval = setInterval(() => {
|
187 |
+
ga.evolve();
|
188 |
+
const bestIndividual = ga.getBestIndividual();
|
189 |
+
const song = composeSong(bestIndividual);
|
190 |
+
|
191 |
+
outputDiv.innerHTML = `<p>Поколение: ${ga.generation}</p><pre>${song}</pre>`;
|
192 |
+
speak(song);
|
193 |
+
}, 5000); // Генерировать новую песню каждые 5 секун
|
194 |
+
}
|
195 |
+
|
196 |
+
// Функция для остановки обучения
|
197 |
+
function stopLearning() {
|
198 |
+
clearInterval(learningInterval);
|
199 |
}
|
200 |
|
201 |
// Функция для составления песни из последовательностей
|
|
|
210 |
|
211 |
// Функция для воспроизведения текста
|
212 |
function speak(text) {
|
213 |
+
synth.cancel(); // Останавливаем предыдущее воспроизведение
|
214 |
const utterance = new SpeechSynthesisUtterance(text);
|
215 |
+
const selectedOption = voiceSelect.selectedOptions[0];
|
216 |
+
const selectedVoice = synth.getVoices().find(voice => voice.name === selectedOption.getAttribute('data-name'));
|
217 |
+
|
218 |
+
utterance.voice = selectedVoice;
|
219 |
+
utterance.lang = selectedOption.getAttribute('data-lang');
|
220 |
utterance.rate = 0.8;
|
221 |
synth.speak(utterance);
|
222 |
}
|
223 |
+
|
224 |
+
// Инициализация генетического алгоритма
|
225 |
+
ga = new GeneticAlgorithm();
|
226 |
+
ga.initialize();
|
227 |
</script>
|
228 |
</body>
|
229 |
</html>
|