-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
1612 lines (1418 loc) · 50.8 KB
/
script.js
File metadata and controls
1612 lines (1418 loc) · 50.8 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ======== TRADUÇÕES ========
const translations = {
pt: {
"header.title": "Voice Atelier",
"header.subtitle":
"Projeto OBL 4101 – Modificações de voz (tempo, pitch e efeitos especiais)",
"header.language": "Idioma:",
"pill.title": "🔊 Interface Web para o Vocodeur.m",
"step1.title": "1. Carregar áudio",
"step1.desc":
"Use os áudios do projeto (Extrait.wav, Diner.wav, Halleluia.wav) ou grave a sua própria voz.",
"step1.labelFile": "Arquivo de áudio (.wav recomendado)",
"step1.placeholderFile": "Clique para selecionar um arquivo",
"step1.recordTitle": "ou grave sua voz",
"step2.title": "2. Escolher efeito",
"step2.desc":
"Os efeitos abaixo correspondem às funções que você implementou no MATLAB.",
"step2.effectLabel": "Efeito de modificação:",
"step3.title": "3. Resultado do processamento",
"step3.desc":
"Depois de aplicar o efeito, o áudio processado aparece aqui.",
"effect.none": "Nenhum (som original)",
"effect.tempo": "Mudança de tempo (PVoc)",
"effect.pitch": "Mudança de pitch (PVoc + resample)",
"effect.robot": "Voz robótica",
"effect.harmonic": "Duo / Trio harmônico",
"effect.ghost": "Fantasma / Whisper",
"effect.telephone": "Telefone / Rádio",
"effect.harmonic-duo-trio": "Duo / Trio harmônico",
"effect.ghost-whisper": "Fantasma / Whisper",
"effect.telephone-radio": "Telefone / Rádio",
"effects.none.note":
"Nenhum efeito especial – o áudio original será reproduzido.",
"effects.tempo.note":
"Modifica a velocidade da fala sem alterar o pitch (usa PVoc).",
"effects.tempo.factorLabel": "Fator de tempo (rapp)",
"effects.tempo.help":
"Valores > 1 deixam o som mais lento; valores < 1 deixam mais rápido.",
"effects.pitch.note":
"Modifica o pitch sem alterar a velocidade (PVoc + resample).",
"effects.pitch.semitonesLabel": "Deslocamento de pitch (semitons)",
"effects.pitch.help":
"Valores positivos deixam a voz mais aguda, negativos mais grave.",
"effects.robot.note":
"Robotização usando modulação em frequência (função Rob.m).",
"effects.robot.fcLabel": "Frequência de modulação (fc em Hz)",
"effects.robot.help":
"Experimente fc ≈ 200–800 Hz para fala; valores muito altos soam metálicos.",
"effects.harmonic.note":
"Gera vozes adicionais (duo ou trio) a partir da voz original (harmonização).",
"effects.harmonic.voicesLabel": "Número de vozes",
"effects.harmonic.interval1Label":
"Intervalo voz 1 (semitons – ex: 7 = quinta)",
"effects.harmonic.interval2Label":
"Intervalo voz 2 (semitons – ex: 12 = oitava)",
"effects.harmonic.mixLabel": "Mistura seco/molhado",
"effects.harmonic.help":
"Você pode criar combinações como quinta + oitava para trio harmônico.",
"effects.ghost.note":
"Efeito de sussurro/fantasma, baseado em remoção de pitch e reforço de ruído.",
"effects.ghost.intensityLabel": "Intensidade",
"effects.ghost.help":
"Aumente a intensidade para mais efeito de sussurro; ajuste a reverb para profundidade de eco.",
"effects.ghost.reverbLabel": "Quantidade de reverberação",
"effects.telephone.note":
"Simula o som de telefone/rádio: banda limitada e possível ruído.",
"effects.telephone.lowCutLabel": "Corte de graves (Hz)",
"effects.telephone.highCutLabel": "Corte de agudos (Hz)",
"effects.telephone.noiseLabel": "Nível de ruído (0–1)",
"effects.telephone.noiseHelp":
"Use um valor pequeno (ex: 0.05–0.15) para simular chiado de linha.",
"status.selectEffectHint":
"Selecione um efeito para ver os parâmetros.",
"status.selectEffect": "Selecione um efeito...",
"status.noneApplied": "Nenhum efeito aplicado ainda.",
"status.processing": "Processando áudio... (simulação no front-end)",
"status.processingDone":
"Simulação concluída.",
"status.noFile": "Selecione primeiro um arquivo de áudio.",
"status.selectAndApply": 'Escolha um efeito e clique em "Aplicar".',
"buttons.apply": "▶ Aplicar efeito",
"buttons.reset": "Limpar seleção",
"buttons.record": "🎤 Gravar",
"buttons.stop": "⏹ Parar",
"buttons.clearRecord": "✕ Limpar",
"recording.title": "Gravação de voz",
"recording.label": "ou grave a sua própria voz",
"status.label": "Status:",
"info.title": "Sobre os efeitos",
"info.desc":
"Esta interface foi pensada para se conectar com o código MATLAB do seu projeto.",
"info.items.tempo": "Tempo – PVoc com fatores <1 ou >1.",
"info.items.pitch": "Pitch – PVoc + resample.",
"info.items.robot": "Robot – modulação (Rob.m).",
"info.items.harmonic": "Duo/Trio – transposição harmônica.",
"info.items.ghost": "Fantasma – Whisper / Ghost effect.",
"info.items.telephone":
"Telefone/Rádio – filtragem em banda e possível ruído.",
"tags.tfct": "TFCT",
"tags.pv": "Phase Vocoder",
"tags.rm": "Ring Modulation",
"tags.bp": "Bandpass Filter"
},
en: {
"header.title": "Voice Atelier",
"header.subtitle":
"OBL 4101 Project – Voice modifications (time, pitch and special effects)",
"header.language": "Language:",
"pill.title": "🔊 Web interface for Vocodeur.m",
"step1.title": "1. Load audio",
"step1.desc":
"Use the project audios (Extrait.wav, Diner.wav, Halleluia.wav) or record your own voice.",
"step1.labelFile": "Audio file (.wav recommended)",
"step1.placeholderFile": "Click to choose a file",
"step1.recordTitle": "or record your voice",
"step2.title": "2. Choose effect",
"step2.desc":
"The effects below correspond to the functions you implemented in MATLAB.",
"step2.effectLabel": "Effect modification:",
"step3.title": "3. Processing result",
"step3.desc":
"After applying the effect, the processed audio appears here.",
"effect.none": "None (original sound)",
"effect.tempo": "Time-stretch (PVoc)",
"effect.pitch": "Pitch-shift (PVoc + resample)",
"effect.robot": "Robot voice",
"effect.harmonic": "Harmonic duo / trio",
"effect.ghost": "Ghost / Whisper",
"effect.telephone": "Telephone / Radio",
"effect.harmonic-duo-trio": "Harmonic duo / trio",
"effect.ghost-whisper": "Ghost / Whisper",
"effect.telephone-radio": "Telephone / Radio",
"effects.none.note":
"No special effect – the original audio will be played.",
"effects.tempo.note":
"Changes the speech rate without changing the pitch (PVoc).",
"effects.tempo.factorLabel": "Time factor (rapp)",
"effects.tempo.help":
"Values > 1 make it slower; values < 1 make it faster.",
"effects.pitch.note":
"Changes pitch without changing duration (PVoc + resample).",
"effects.pitch.semitonesLabel": "Pitch shift (semitones)",
"effects.pitch.help":
"Positive values make the voice higher, negative lower.",
"effects.robot.note":
"Robotisation using frequency modulation (Rob.m function).",
"effects.robot.fcLabel": "Modulation frequency (fc in Hz)",
"effects.robot.help":
"Try fc ≈ 200–800 Hz for speech; very high values sound metallic.",
"effects.harmonic.note":
"Creates extra voices (duo or trio) from the original voice (harmonisation).",
"effects.harmonic.voicesLabel": "Number of voices",
"effects.harmonic.interval1Label":
"Voice 1 interval (semitones – e.g. 7 = fifth)",
"effects.harmonic.interval2Label":
"Voice 2 interval (semitones – e.g. 12 = octave)",
"effects.harmonic.mixLabel": "Dry/wet mix",
"effects.harmonic.help":
"You can make combinations like fifth + octave for a trio effect.",
"effects.ghost.note":
"Whisper/ghost effect, based on pitch removal and noise enhancement.",
"effects.ghost.intensityLabel": "Intensity",
"effects.ghost.help":
"Increase intensity for more whisper effect; adjust reverb for echo depth.",
"effects.ghost.reverbLabel": "Reverb amount",
"effects.telephone.note":
"Simulates telephone/radio sound: band-limited with optional noise.",
"effects.telephone.lowCutLabel": "Low cut (Hz)",
"effects.telephone.highCutLabel": "High cut (Hz)",
"effects.telephone.noiseLabel": "Noise level (0–1)",
"effects.telephone.noiseHelp":
"Use a small value (e.g. 0.05–0.15) to simulate line hiss.",
"status.selectEffectHint": "Select an effect to see its parameters.",
"status.selectEffect": "Select an effect...",
"status.noneApplied": "No effect applied yet.",
"status.processing": "Processing audio... (front-end simulation)",
"status.processingDone":
"Simulation finished.",
"status.noFile": "Please select an audio file first.",
"status.selectAndApply": 'Select an effect and click "Apply".',
"buttons.apply": "▶ Apply effect",
"buttons.reset": "Clear selection",
"buttons.record": "🎤 Record",
"buttons.stop": "⏹ Stop",
"buttons.clearRecord": "✕ Clear",
"recording.title": "Voice Recording",
"recording.label": "or record your own voice",
"status.label": "Status:",
"info.title": "About the effects",
"info.desc": "This interface is designed to connect to your MATLAB code.",
"info.items.tempo": "Time – PVoc with factors <1 or >1.",
"info.items.pitch": "Pitch – PVoc + resample.",
"info.items.robot": "Robot – modulation (Rob.m).",
"info.items.harmonic": "Duo/Trio – harmonic transposition.",
"info.items.ghost": "Ghost – Whisper / Ghost effect.",
"info.items.telephone":
"Telephone/Radio – narrow-band filtering and possible noise.",
"tags.tfct": "STFT",
"tags.pv": "Phase Vocoder",
"tags.rm": "Ring Modulation",
"tags.bp": "Bandpass Filter"
},
fr: {
"header.title": "Voice Atelier",
"header.subtitle":
"Projet OBL 4101 – Modifications de voix (temps, hauteur et effets spéciaux)",
"header.language": "Langue :",
"pill.title": "🔊 Interface Web pour Vocodeur.m",
"step1.title": "1. Charger l’audio",
"step1.desc":
"Utilisez les audios du projet (Extrait.wav, Diner.wav, Halleluia.wav) ou enregistrez votre propre voix.",
"step1.labelFile": "Fichier audio (.wav recommandé)",
"step1.placeholderFile": "Cliquez pour choisir un fichier",
"step1.recordTitle": "ou enregistrez votre voix",
"step2.title": "2. Choisir l’effet",
"step2.desc":
"Les effets ci-dessous correspondent aux fonctions implémentées dans MATLAB.",
"step2.effectLabel": "Effet de modification :",
"step3.title": "3. Résultat du traitement",
"step3.desc":
"Après l’application de l’effet, l’audio traité apparaît ici.",
"effect.none": "Aucun (son original)",
"effect.tempo": "Changement de temps (PVoc)",
"effect.pitch": "Changement de hauteur (PVoc + resample)",
"effect.robot": "Voix robotisée",
"effect.harmonic": "Duo / Trio harmonique",
"effect.ghost": "Fantôme / Whisper",
"effect.telephone": "Téléphone / Radio",
"effect.harmonic-duo-trio": "Duo / Trio harmonique",
"effect.ghost-whisper": "Fantôme / Whisper",
"effect.telephone-radio": "Téléphone / Radio",
"effects.none.note":
"Aucun effet spécial – le son original est reproduit.",
"effects.tempo.note":
"Modifie la vitesse sans changer la hauteur (PVoc).",
"effects.tempo.factorLabel": "Facteur de temps (rapp)",
"effects.tempo.help":
"Valeurs > 1 : plus lent ; valeurs < 1 : plus rapide.",
"effects.pitch.note":
"Modifie la hauteur sans changer la durée (PVoc + resample).",
"effects.pitch.semitonesLabel": "Déplacement (demi-tons)",
"effects.pitch.help":
"Valeurs positives : plus aigu ; négatives : plus grave.",
"effects.robot.note":
"Robotisation par modulation de fréquence (fonction Rob.m).",
"effects.robot.fcLabel": "Fréquence de modulation (fc en Hz)",
"effects.robot.help":
"Essayez fc ≈ 200–800 Hz pour la parole ; très élevé donne un son métallique.",
"effects.harmonic.note":
"Crée des voix supplémentaires (duo ou trio) à partir de la voix originale (harmonisation).",
"effects.harmonic.voicesLabel": "Nombre de voix",
"effects.harmonic.interval1Label":
"Intervalle voix 1 (demi-tons – ex : 7 = quinte)",
"effects.harmonic.interval2Label":
"Intervalle voix 2 (demi-tons – ex : 12 = octave)",
"effects.harmonic.mixLabel": "Mix dry/wet",
"effects.harmonic.help":
"Vous pouvez combiner quinte + octave pour un effet trio.",
"effects.ghost.note":
"Effet chuchoté/fantôme, basé sur la suppression de la hauteur et l’ajout de bruit.",
"effects.ghost.typeLabel": "Type d’effet",
"effects.ghost.intensityLabel": "Intensité",
"effects.ghost.help":
"Augmentez l'intensité pour plus d'effet de chuchotement; ajustez la réverbération pour la profondeur d'écho.",
"effects.ghost.reverbLabel": "Quantité de réverbération",
"effects.telephone.note":
"Simule un son de téléphone/radio : bande limitée et bruit optionnel.",
"effects.telephone.lowCutLabel": "Coupure basse (Hz)",
"effects.telephone.highCutLabel": "Coupure haute (Hz)",
"effects.telephone.noiseLabel": "Niveau de bruit (0–1)",
"effects.telephone.noiseHelp":
"Utilisez une petite valeur (0.05–0.15) pour simuler le souffle de ligne.",
"status.selectEffectHint":
"Choisissez un effet pour afficher ses paramètres.",
"status.noneApplied": "Aucun effet appliqué pour l’instant.",
"status.processing":
"Traitement de l’audio... (simulation côté front-end)",
"status.processingDone":
"Simulation terminée.",
"status.noFile": "Sélectionnez d’abord un fichier audio.",
"status.selectAndApply":
'Choisissez un effet et cliquez sur « Appliquer ».',
"buttons.apply": "▶ Appliquer l’effet",
"buttons.reset": "Effacer la sélection",
"buttons.record": "🎤 Enregistrer",
"buttons.stop": "⏹ Arrêter",
"buttons.clearRecord": "✕ Effacer",
"recording.title": "Enregistrement vocal",
"recording.label": "ou enregistrez votre propre voix",
"status.label": "Statut:",
"info.title": "À propos des effets",
"info.desc":
"Cette interface est conçue pour se connecter à votre code MATLAB.",
"info.items.tempo": "Temps – PVoc, facteurs <1 ou >1.",
"info.items.pitch": "Hauteur – PVoc + resample.",
"info.items.robot": "Robot – modulation (Rob.m).",
"info.items.harmonic": "Duo/Trio – transposition harmonique.",
"info.items.ghost": "Fantôme – Whisper / Ghost effect.",
"info.items.telephone":
"Téléphone/Radio – filtrage en bande et bruit éventuel.",
"tags.tfct": "TFCT",
"tags.pv": "Phase Vocoder",
"tags.rm": "Ring Modulation",
"tags.bp": "Bandpass Filter",
"effects.none.note":
"Aucun effet spécial – le son original est reproduit."
}
};
let currentLang = "en";
function t(key) {
const dict = translations[currentLang] || translations.pt;
let result = dict[key];
// Valores padrão para chaves que podem não estar no dicionário
if (!result) {
const defaults = {
"status.selectEffect": {
pt: "Selecione um efeito...",
en: "Select an effect...",
fr: "Sélectionnez um efeito..."
}
};
if (defaults[key]) {
result = defaults[key][currentLang];
}
}
return result || "";
}
function applyTranslations() {
document.querySelectorAll("[data-i18n]").forEach((el) => {
const key = el.getAttribute("data-i18n");
const text = t(key);
if (!text) return;
if (el.tagName === "INPUT" || el.tagName === "TEXTAREA") {
el.placeholder = text;
} else if (el.tagName === "OPTION") {
el.textContent = text;
} else {
el.textContent = text;
}
});
document.querySelectorAll("[data-i18n-placeholder]").forEach((el) => {
const key = el.getAttribute("data-i18n-placeholder");
const text = t(key);
if (!text) return;
el.placeholder = text;
});
// Traduzir options do select
document.querySelectorAll("select option").forEach((option) => {
const key = option.getAttribute("data-i18n");
if (key) {
const text = t(key);
if (text && text !== key) {
option.textContent = text;
}
}
});
// Traduzir primeira opção especialmente
const firstOption = document.querySelector("select option[value='']");
if (firstOption) {
const text = t("status.selectEffect");
if (text && text !== "status.selectEffect") {
firstOption.textContent = text;
}
}
}
// ======== SELETORES ========
const fileInput = document.getElementById("audio-input");
const fileNameSpan = document.getElementById("file-name");
const originalAudio = document.getElementById("original-audio");
const processedAudio = document.getElementById("processed-audio");
const effectSelect = document.getElementById("effect-select");
const parametersContainer = document.getElementById("parameters-container");
const effectNote = document.getElementById("effect-note") || { textContent: "" };
const applyBtn = document.getElementById("apply-effect");
const resetBtn = document.getElementById("reset-effect");
const statusText = document.getElementById("status-text");
const resultLabel = document.getElementById("result-label");
const langSelect = document.getElementById("lang-select");
const langToggle = document.querySelector(".lang-toggle");
const langMenu = document.querySelector(".lang-menu");
const langOptions = document.querySelectorAll(".lang-option");
const currentLangFlag = document.getElementById("current-lang-flag");
// elementos extras usados no upload
const fileInputText = document.getElementById("file-input-text");
const fileWrapper = document.querySelector(".file-input-wrapper") || document.querySelector(".file-input-label");
// Elementos de gravação
const recordBtn = document.getElementById("record-btn");
const stopBtn = document.getElementById("stop-btn");
const clearRecordBtn = document.getElementById("clear-record-btn");
const recordingTimer = document.getElementById("recording-timer");
const timerDisplay = document.getElementById("timer-display");
// Variáveis de gravação
let mediaRecorder;
let recordedChunks = [];
let recordingStartTime;
let recordingTimerInterval;
const recordingConstraints = { audio: true };
// ===== FUNÇÕES DE GRAVAÇÃO DE ÁUDIO =====
async function startRecording() {
try {
const stream = await navigator.mediaDevices.getUserMedia(recordingConstraints);
mediaRecorder = new MediaRecorder(stream);
recordedChunks = [];
mediaRecorder.ondataavailable = (event) => {
if (event.data.size > 0) {
recordedChunks.push(event.data);
}
};
mediaRecorder.onstop = () => {
const blob = new Blob(recordedChunks, { type: "audio/webm" });
const url = URL.createObjectURL(blob);
// Decodificar o áudio gravado
blob.arrayBuffer().then((arrayBuffer) => {
audioCtx.decodeAudioData(arrayBuffer, (buffer) => {
originalSamples = buffer.getChannelData(0).slice();
originalSampleRate = buffer.sampleRate;
decodedBuffer = buffer;
originalAudio.src = url;
originalFile = { name: t("recording.title") };
fileNameSpan.textContent = t("recording.title");
statusText.textContent = t("status.selectAndApply");
updateApplyButtonState();
}, (error) => {
console.error("Erro ao decodificar áudio:", error);
statusText.textContent = t("status.recordingError");
});
});
// Parar todas as tracks
stream.getTracks().forEach(track => track.stop());
};
mediaRecorder.start();
recordBtn.disabled = true;
stopBtn.disabled = false;
clearRecordBtn.disabled = true;
recordingStartTime = Date.now();
recordingTimer.style.display = "block";
recordingTimerInterval = setInterval(updateRecordingTimer, 100);
} catch (err) {
console.error("Erro ao acessar microfone:", err);
statusText.textContent = t("status.microphoneError");
}
}
function stopRecording() {
if (mediaRecorder && mediaRecorder.state !== "inactive") {
mediaRecorder.stop();
recordBtn.disabled = false;
stopBtn.disabled = true;
clearRecordBtn.disabled = false;
clearInterval(recordingTimerInterval);
recordingTimer.style.display = "none";
}
}
function clearRecording() {
recordedChunks = [];
originalSamples = null;
decodedBuffer = null;
originalFile = null;
fileNameSpan.textContent = t("step1.placeholderFile");
originalAudio.src = "";
processedAudio.src = "";
statusText.textContent = t("status.noFile");
recordBtn.disabled = false;
stopBtn.disabled = true;
clearRecordBtn.disabled = true;
updateApplyButtonState();
}
function updateRecordingTimer() {
const elapsed = Date.now() - recordingStartTime;
const seconds = Math.floor(elapsed / 1000);
const minutes = Math.floor(seconds / 60);
const secs = seconds % 60;
timerDisplay.textContent = `${minutes}:${secs.toString().padStart(2, '0')}`;
}
// Event listeners para gravação
if (recordBtn) recordBtn.addEventListener("click", startRecording);
if (stopBtn) stopBtn.addEventListener("click", stopRecording);
if (clearRecordBtn) clearRecordBtn.addEventListener("click", clearRecording);
// ===== CARROSSEL DO TOPO =====
const carouselElement = document.getElementById("carousel-example");
if (carouselElement) {
const carouselItems = carouselElement.querySelectorAll(".carousel-item");
const indicators = carouselElement.querySelectorAll(
".carousel-indicators li"
);
const prevBtn = carouselElement.querySelector(".carousel-control-prev");
const nextBtn = carouselElement.querySelector(".carousel-control-next");
const progressDots = carouselElement.querySelectorAll(".carousel-progress-dot");
let currentSlideIndex = 0;
const totalSlides = carouselItems.length;
let autoplayInterval;
const autoplayDelay = 12000; // 12 segundos
function updateProgressDots() {
progressDots.forEach((dot, i) => {
if (i === currentSlideIndex) {
dot.classList.add("active");
} else {
dot.classList.remove("active");
}
});
}
function showSlide(index) {
if (!totalSlides) return;
currentSlideIndex = (index + totalSlides) % totalSlides;
carouselItems.forEach((item, i) => {
if (i === currentSlideIndex) {
item.classList.add("active");
} else {
item.classList.remove("active");
}
});
indicators.forEach((dot, i) => {
if (i === currentSlideIndex) {
dot.classList.add("active");
} else {
dot.classList.remove("active");
}
});
updateProgressDots();
// Reiniciar autoplay
clearInterval(autoplayInterval);
startAutoplay();
}
function startAutoplay() {
autoplayInterval = setInterval(() => {
showSlide(currentSlideIndex + 1);
}, autoplayDelay);
}
// clique nas bolinhas
indicators.forEach((dot, i) => {
dot.addEventListener("click", (event) => {
event.preventDefault();
showSlide(i);
});
});
// clique nos dots de progresso
progressDots.forEach((dot, i) => {
dot.addEventListener("click", (event) => {
event.preventDefault();
showSlide(i);
});
});
// setas de navegação
if (prevBtn) {
prevBtn.addEventListener("click", (event) => {
event.preventDefault();
showSlide(currentSlideIndex - 1);
});
}
if (nextBtn) {
nextBtn.addEventListener("click", (event) => {
event.preventDefault();
showSlide(currentSlideIndex + 1);
});
}
// garante que o primeiro slide esteja ativado
showSlide(0);
}
let originalFile = null;
// ===== BACKEND NO NAVEGADOR =====
const AudioContextClass =
window.AudioContext || window.webkitAudioContext || null;
const audioCtx = AudioContextClass ? new AudioContextClass() : null;
let decodedBuffer = null;
let originalSamples = null;
let originalSampleRate = 44100;
// Decodifica o arquivo selecionado em amostras
function decodeFileToSamples(file) {
if (!audioCtx || !file) return;
file
.arrayBuffer()
.then((buf) => audioCtx.decodeAudioData(buf))
.then((audioBuffer) => {
decodedBuffer = audioBuffer;
originalSampleRate = audioBuffer.sampleRate;
const ch0 =
audioBuffer.numberOfChannels > 0
? audioBuffer.getChannelData(0)
: new Float32Array(0);
originalSamples = new Float32Array(ch0.length);
originalSamples.set(ch0);
console.log(
"Áudio decodificado:",
originalSamples.length,
"amostras @",
originalSampleRate,
"Hz"
);
})
.catch((err) => {
console.error("Erro ao decodificar áudio:", err);
});
}
function cloneSamples(samples) {
if (!samples) return null;
const out = new Float32Array(samples.length);
out.set(samples);
return out;
}
function normalizeSamples(samples) {
if (!samples) return null;
let max = 0;
for (let i = 0; i < samples.length; i++) {
const v = Math.abs(samples[i]);
if (v > max) max = v;
}
if (max < 1e-9) return samples;
const out = new Float32Array(samples.length);
const g = 0.99 / max;
for (let i = 0; i < samples.length; i++) {
out[i] = samples[i] * g;
}
return out;
}
// --- Tempo (re-amostragem simples) ---
function resampleTempo(samples, factor) {
if (!samples || factor <= 0) return cloneSamples(samples);
if (factor === 1) return cloneSamples(samples);
const inLen = samples.length;
const outLen = Math.max(1, Math.floor(inLen * factor));
const out = new Float32Array(outLen);
for (let i = 0; i < outLen; i++) {
const pos = i / factor;
const i0 = Math.floor(pos);
const i1 = Math.min(inLen - 1, i0 + 1);
const frac = pos - i0;
out[i] = samples[i0] * (1 - frac) + samples[i1] * frac;
}
return normalizeSamples(out);
}
// --- Pitch-shift simples (mantém o mesmo tamanho) ---
function pitchShiftSimple(samples, semitones) {
if (!samples || !isFinite(semitones) || semitones === 0) {
return cloneSamples(samples);
}
const factor = Math.pow(2, semitones / 12);
const inLen = samples.length;
const outLen = inLen;
const out = new Float32Array(outLen);
for (let i = 0; i < outLen; i++) {
const pos = i / factor;
if (pos >= inLen - 1) {
out[i] = 0;
} else {
const i0 = Math.floor(pos);
const i1 = i0 + 1;
const frac = pos - i0;
out[i] = samples[i0] * (1 - frac) + samples[i1] * frac;
}
}
return normalizeSamples(out);
}
// ===== FUNÇÕES DE PROCESSAMENTO AVANÇADO =====
// Implementação simplificada do Phase Vocoder
function phaseVocoder(samples, rapp, fftSize) {
if (!samples || rapp <= 0) return cloneSamples(samples);
if (Math.abs(rapp - 1.0) < 0.001) return cloneSamples(samples);
const hop = Math.floor(fftSize / 4);
const numFrames = Math.floor((samples.length - fftSize) / hop) + 1;
const outLen = Math.max(1, Math.floor(samples.length / rapp));
const out = new Float32Array(outLen);
// Hann window
const window = new Float32Array(fftSize);
for (let i = 0; i < fftSize; i++) {
window[i] = 0.5 * (1 - Math.cos((2 * Math.PI * i) / (fftSize - 1)));
}
let outIdx = 0;
const phase = new Float32Array(fftSize);
const phaseAdvance = (2 * Math.PI * hop) / fftSize;
for (let frameIdx = 0; frameIdx < numFrames && outIdx < outLen; frameIdx++) {
const offset = frameIdx * hop;
if (offset + fftSize > samples.length) break;
// Extrair frame e aplicar janela
const frame = new Float32Array(fftSize);
for (let i = 0; i < fftSize; i++) {
frame[i] = (samples[offset + i] || 0) * window[i];
}
// FFT
const spectrum = fft(frame);
// Sintetizar com nova taxa
const synthFrame = new Float32Array(fftSize);
for (let i = 0; i < fftSize; i++) {
let val = 0;
for (let k = 1; k < fftSize / 2; k++) {
const re = spectrum[2 * k];
const im = spectrum[2 * k + 1];
const mag = Math.sqrt(re * re + im * im);
phase[k] += phaseAdvance * k * rapp;
// Unwrap phase
while (phase[k] > Math.PI) phase[k] -= 2 * Math.PI;
while (phase[k] < -Math.PI) phase[k] += 2 * Math.PI;
val += 2 * mag * Math.cos((2 * Math.PI * k * i) / fftSize + phase[k]);
}
synthFrame[i] = (val / fftSize) * window[i];
}
// Sobreposição e adição com taxa reduzida
const synthHop = Math.floor(hop / rapp);
for (let i = 0; i < fftSize && outIdx < outLen; i++) {
if (i % Math.ceil(1 / rapp) === 0) {
out[outIdx++] = synthFrame[i];
}
}
}
// Pad com zeros se necessário
while (outIdx < outLen && outIdx < out.length) {
out[outIdx++] = 0;
}
return normalizeSamples(out);
}
// FFT simplificada (Cooley-Tukey)
function fft(input) {
const N = input.length;
// Pad para potência de 2
let size = 1;
while (size < N) size *= 2;
const padded = new Float32Array(size);
for (let i = 0; i < N; i++) {
padded[i] = input[i];
}
return fftCore(padded);
}
function fftCore(input) {
const N = input.length;
if (N <= 1) {
const output = new Float32Array(N * 2);
if (N === 1) {
output[0] = input[0];
output[1] = 0;
}
return output;
}
if (N === 2) {
const output = new Float32Array(4);
output[0] = input[0] + input[1];
output[1] = 0;
output[2] = input[0] - input[1];
output[3] = 0;
return output;
}
// Bit reversal
const bitReverse = (x, bits) => {
let result = 0;
for (let i = 0; i < bits; i++) {
result = (result << 1) | (x & 1);
x >>= 1;
}
return result;
};
const bits = Math.log2(N);
const reversed = new Float32Array(N);
for (let i = 0; i < N; i++) {
reversed[bitReverse(i, bits)] = input[i];
}
// Butterfly operations
const output = new Float32Array(N * 2);
for (let i = 0; i < N; i++) {
output[2 * i] = reversed[i];
output[2 * i + 1] = 0;
}
for (let s = 1; s <= bits; s++) {
const m = 1 << s;
const mby2 = m >> 1;
for (let k = 0; k < N; k += m) {
for (let j = 0; j < mby2; j++) {
const t = (2 * Math.PI * j) / m;
const wr = Math.cos(t);
const wi = -Math.sin(t);
const t0 = k + j;
const t1 = k + j + mby2;
const ar = output[2 * t0];
const ai = output[2 * t0 + 1];
const br = output[2 * t1];
const bi = output[2 * t1 + 1];
const cr = wr * br - wi * bi;
const ci = wr * bi + wi * br;
output[2 * t0] = ar + cr;
output[2 * t0 + 1] = ai + ci;
output[2 * t1] = ar - cr;
output[2 * t1 + 1] = ai - ci;
}
}
}
return output;
}
// Resample simples usando interpolação linear
function simplexResample(samples, upFactor, downFactor) {
if (!samples || upFactor <= 0 || downFactor <= 0) return cloneSamples(samples);
const ratio = upFactor / downFactor;
const outLen = Math.max(1, Math.floor(samples.length * ratio));
const out = new Float32Array(outLen);
for (let i = 0; i < outLen; i++) {
const pos = i / ratio;
const idx = Math.floor(pos);
const frac = pos - idx;
if (idx >= samples.length - 1) {
out[i] = samples[samples.length - 1] || 0;
} else {
out[i] = samples[idx] * (1 - frac) + samples[idx + 1] * frac;
}
}
return out;
}
// ---- EFEITOS ----
function effectTempo(samples, sr, params) {
const rapp =
parseFloat(params.rapp != null ? params.rapp : params.factor) || 1.0;
return resampleTempo(samples, rapp);
}
function effectPitch(samples, sr, params) {
const semitones = parseFloat(params.semitones || 0) || 0;
return pitchShiftSimple(samples, semitones);
}
function effectRobot(samples, sr, params) {
if (!samples) return null;
const fc = parseFloat(params.fc || 400) || 400;
const out = new Float32Array(samples.length);
const twoPiFcOverFs = (2 * Math.PI * fc) / sr;
for (let n = 0; n < samples.length; n++) {
out[n] = samples[n] * Math.cos(twoPiFcOverFs * n);
}
return normalizeSamples(out);
}
function effectHarmonic(samples, sr, params) {
if (!samples) return null;
// Criar 3 vozes com pitch-shift simples
const v1 = cloneSamples(samples); // voz original
const v2 = pitchShiftSimple(samples, 7); // 5ª (7 semitons)
const v3 = pitchShiftSimple(samples, 12); // oitava (12 semitons)
const len = samples.length;
// Normalizar cada voz
const normalize = (arr) => {
let max = 0;
for (let i = 0; i < len; i++) {
max = Math.max(max, Math.abs(arr[i]));
}
if (max === 0) return arr;
const result = new Float32Array(len);
for (let i = 0; i < len; i++) {
result[i] = arr[i] / max;
}
return result;
};
const v1norm = normalize(v1);
const v2norm = normalize(v2);
const v3norm = normalize(v3);
// Somar as 3 vozes
const result = new Float32Array(len);
const gain = 1 / 3;
for (let i = 0; i < len; i++) {
result[i] = (v1norm[i] + v2norm[i] + v3norm[i]) * gain;
}
return normalizeSamples(result);
}
function effectGhostWhisper(samples, sr, params) {
if (!samples) return null;
const intensity = parseFloat(params.intensity || 0.8) || 0.8;
const reverb = parseFloat(params.reverb || 0.3) || 0.3;
const len = samples.length;
const out = new Float32Array(len);
// Criar envoltório de amplitude com média móvel
const windowSize = Math.max(10, Math.floor(sr * 0.005)); // 5ms
const envelope = new Float32Array(len);
for (let i = 0; i < len; i++) {
let sum = 0;
const start = Math.max(0, i - windowSize / 2);
const end = Math.min(len, i + windowSize / 2);
for (let j = start; j < end; j++) {
sum += Math.abs(samples[j]);
}
envelope[i] = sum / (end - start);