Dmtlant commited on
Commit
4a341f8
1 Parent(s): 7d1863d

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +34 -208
index.html CHANGED
@@ -3,227 +3,53 @@
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>Vocaloid с генетическим алгоритмом</title>
7
- <style>
8
- body {
9
- font-family: Arial, sans-serif;
10
- display: flex;
11
- justify-content: center;
12
- align-items: center;
13
- height: 100vh;
14
- margin: 0;
15
- background: linear-gradient(45deg, #ff9a9e 0%, #fad0c4 99%, #fad0c4 100%);
16
- }
17
- .container {
18
- background-color: white;
19
- padding: 2rem;
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;
27
- padding: 15px 32px;
28
- text-align: center;
29
- text-decoration: none;
30
- display: inline-block;
31
- font-size: 16px;
32
- margin: 4px 2px;
33
- cursor: pointer;
34
- border-radius: 5px;
35
- }
36
- #output {
37
- margin-top: 1rem;
38
- padding: 1rem;
39
- background-color: #f0f0f0;
40
- border-radius: 5px;
41
- }
42
- </style>
43
  </head>
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;
82
- this.mutationRate = mutationRate;
83
- this.population = [];
84
- this.generation = 0;
85
- }
86
 
87
- initialize() {
88
- for (let i = 0; i < this.populationSize; i++) {
89
- this.population.push(this.createRandomIndividual());
90
- }
91
  }
 
92
 
93
- createRandomIndividual() {
94
- return {
95
- verse: this.generateRandomSequence(16),
96
- chorus: this.generateRandomSequence(8),
97
- hook: this.generateRandomSequence(4),
98
- fitness: 0
99
- };
100
- }
101
-
102
- generateRandomSequence(length) {
103
- return Array.from({ length }, () => Math.random() < 0.5 ? 0 : 1);
104
- }
105
-
106
- evolve() {
107
- this.evaluateFitness();
108
- const newPopulation = [];
109
 
110
- while (newPopulation.length < this.populationSize) {
111
- const parent1 = this.selectParent();
112
- const parent2 = this.selectParent();
113
- const child = this.crossover(parent1, parent2);
114
- this.mutate(child);
115
- newPopulation.push(child);
116
- }
117
 
118
- this.population = newPopulation;
119
- this.generation++;
120
- }
121
 
122
- evaluateFitness() {
123
- this.population.forEach(individual => {
124
- individual.fitness = Math.random(); // Заглушка для оценки фитнеса
 
 
 
125
  });
126
- }
127
-
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;
135
- }
136
- }
137
- return this.population[this.population.length - 1];
138
- }
139
-
140
- crossover(parent1, parent2) {
141
- const child = {
142
- verse: [],
143
- chorus: [],
144
- hook: [],
145
- fitness: 0
146
- };
147
 
148
- for (let i = 0; i < parent1.verse.length; i++) {
149
- child.verse.push(Math.random() < 0.5 ? parent1.verse[i] : parent2.verse[i]);
150
- }
151
- for (let i = 0; i < parent1.chorus.length; i++) {
152
- child.chorus.push(Math.random() < 0.5 ? parent1.chorus[i] : parent2.chorus[i]);
153
- }
154
- for (let i = 0; i < parent1.hook.length; i++) {
155
- child.hook.push(Math.random() < 0.5 ? parent1.hook[i] : parent2.hook[i]);
156
- }
157
-
158
- return child;
159
- }
160
-
161
- mutate(individual) {
162
- const mutateSequence = (sequence) => {
163
- return sequence.map(gene => Math.random() < this.mutationRate ? 1 - gene : gene);
164
- };
165
-
166
- individual.verse = mutateSequence(individual.verse);
167
- individual.chorus = mutateSequence(individual.chorus);
168
- individual.hook = mutateSequence(individual.hook);
169
- }
170
-
171
- getBestIndividual() {
172
- return this.population.reduce((best, current) =>
173
- current.fitness > best.fitness ? current : best
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
- // Функция для составления песни из последовательностей
202
- function composeSong(individual) {
203
- const verseToLyrics = seq => seq.map(n => n === 0 ? 'ла' : 'ли').join(' ');
204
- const verse = verseToLyrics(individual.verse);
205
- const chorus = verseToLyrics(individual.chorus);
206
- const hook = verseToLyrics(individual.hook);
207
-
208
- return `Куплет:\n${verse}\n\nПрипев:\n${chorus}\n\nХук:\n${hook}`;
209
- }
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>
 
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Vocaloid</title>
7
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/howler/2.2.3/howler.min.js"></script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  </head>
9
  <body>
10
+ <h1>Vocaloid</h1>
11
+ <input type="text" id="textInput" placeholder="Enter text to synthesize">
12
+ <button id="synthesizeBtn">Synthesize</button>
13
+ <div id="audioContainer"></div>
 
 
 
14
 
15
  <script>
16
+ const textInput = document.getElementById('textInput');
17
+ const synthesizeBtn = document.getElementById('synthesizeBtn');
18
+ const audioContainer = document.getElementById('audioContainer');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ synthesizeBtn.addEventListener('click', () => {
21
+ const text = textInput.value;
22
+ if (text) {
23
+ synthesizeText(text);
24
  }
25
+ });
26
 
27
+ function synthesizeText(text) {
28
+ const utterance = new SpeechSynthesisUtterance(text);
29
+ const voices = window.speechSynthesis.getVoices();
30
+ utterance.voice = voices.find(voice => voice.name === 'Google US English') || voices[0];
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
+ window.speechSynthesis.speak(utterance);
 
 
 
 
 
 
33
 
34
+ utterance.addEventListener('start', () => {
35
+ console.log('Speech synthesis started');
36
+ });
37
 
38
+ utterance.addEventListener('end', () => {
39
+ console.log('Speech synthesis ended');
40
+ const audioBlob = new Blob([utterance.audioBuffer], { type: 'audio/wav' });
41
+ const audioUrl = URL.createObjectURL(audioBlob);
42
+ const audioPlayer = new Howl({
43
+ src: [audioUrl]
44
  });
45
+ audioPlayer.play();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
+ const audioElement = document.createElement('audio');
48
+ audioElement.src = audioUrl;
49
+ audioElement.controls = true;
50
+ audioContainer.appendChild(audioElement);
51
+ });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  }
 
 
 
 
53
  </script>
54
  </body>
55
  </html>