File size: 6,326 Bytes
3bb8cb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Сравнение аудиофайлов</title>
    <script src="https://cdn.jsdelivr.net/npm/wavesurfer.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <style>

        body { font-family: Arial, sans-serif; text-align: center; }

        .waveform-container { display: flex; flex-direction: column; align-items: center; gap: 20px; }

        .waveform { width: 80%; }

        .waveform div { height: 150px; cursor: pointer; }

        .chart-container { width: 80%; margin: auto; }

        .audio-controls { margin-top: 20px; }

    </style>
</head>
<body>
    <h2>Сравнение двух аудиофайлов</h2>
    <p>Нажмите <strong>1</strong> для воспроизведения первого трека, <strong>2</strong> для второго.</p>
    <input type="file" id="file1" accept="audio/*" onchange="updateLabel(1)">
    <input type="file" id="file2" accept="audio/*" onchange="updateLabel(2)">
    <button onclick="analyzeAudio()">Анализировать</button>
    
    <div class="waveform-container">
        <div class="waveform">
            <h3 id="track1-label">Файл 1</h3>
            <div id="waveform1"></div>
            <p id="time1">Время: 0:00</p>
            <p id="rms1">RMS: -</p>
            <p id="lufs1">LUFS: -</p>
            <button onclick="playAudio(1)" style="font-size: 2em;">▶️/⏸</button>
        </div>
        <div class="waveform">
            <h3 id="track2-label">Файл 2</h3>
            <div id="waveform2"></div>
            <p id="time2">Время: 0:00</p>
            <p id="rms2">RMS: -</p>
            <p id="lufs2">LUFS: -</p>
            <button onclick="playAudio(2)" style="font-size: 2em;">▶️/⏸</button>
        </div>
    </div>
    
    <canvas id="histogramChart"></canvas>
    <canvas id="spectrumChart"></canvas>
    
    <script>

        let wavesurfer1, wavesurfer2;

        

        document.addEventListener('keydown', function(event) {

            if (event.key === '1') {

                playAudio(1);

            } else if (event.key === '2') {

                playAudio(2);

            }

        });

        

        function updateLabel(track) {

            const fileInput = document.getElementById(`file${track}`);

            const label = document.getElementById(`track${track}-label`);

            label.textContent = fileInput.files[0] ? fileInput.files[0].name : `Файл ${track}`;

        }

        

        function analyzeAudio() {

            const file1 = document.getElementById('file1').files[0];

            const file2 = document.getElementById('file2').files[0];

            if (!file1 || !file2) {

                alert('Выберите два аудиофайла!');

                return;

            }

            

            wavesurfer1 = WaveSurfer.create({ container: '#waveform1', waveColor: 'blue', progressColor: 'darkblue', height: 150 });

            wavesurfer2 = WaveSurfer.create({ container: '#waveform2', waveColor: 'red', progressColor: 'darkred', height: 150 });

            

            const url1 = URL.createObjectURL(file1);

            const url2 = URL.createObjectURL(file2);

            

            wavesurfer1.load(url1);

            wavesurfer2.load(url2);

            

            wavesurfer1.on('seek', syncSeek);

            wavesurfer2.on('seek', syncSeek);

            wavesurfer1.on('audioprocess', () => updateTime(1));

            wavesurfer2.on('audioprocess', () => updateTime(2));

            

            calculateLoudness(file1, 'rms1', 'lufs1');

            calculateLoudness(file2, 'rms2', 'lufs2');

        }

        

        function playAudio(track) {

            let otherWavesurfer = track === 1 ? wavesurfer2 : wavesurfer1;

            let currentWavesurfer = track === 1 ? wavesurfer1 : wavesurfer2;

            

            let currentTime = otherWavesurfer.getCurrentTime();

            otherWavesurfer.pause();

            currentWavesurfer.seekTo(currentTime / currentWavesurfer.getDuration());

            currentWavesurfer.playPause();

        }

        

        function syncSeek() {

            let time1 = wavesurfer1.getCurrentTime();

            let time2 = wavesurfer2.getCurrentTime();

            let avgTime = (time1 + time2) / 2;

            

            wavesurfer1.seekTo(avgTime / wavesurfer1.getDuration());

            wavesurfer2.seekTo(avgTime / wavesurfer2.getDuration());

        }

        

        function updateTime(track) {

            let wavesurfer = track === 1 ? wavesurfer1 : wavesurfer2;

            let timeElement = document.getElementById(`time${track}`);

            let currentTime = wavesurfer.getCurrentTime();

            timeElement.textContent = `Время: ${formatTime(currentTime)}`;

        }

        

        function formatTime(seconds) {

            const minutes = Math.floor(seconds / 60);

            const secs = Math.floor(seconds % 60);

            return `${minutes}:${secs < 10 ? '0' : ''}${secs}`;

        }

        

        function calculateLoudness(file, rmsElementId, lufsElementId) {

            const reader = new FileReader();

            reader.readAsArrayBuffer(file);

            reader.onload = function(event) {

                const audioContext = new (window.AudioContext || window.webkitAudioContext)();

                audioContext.decodeAudioData(event.target.result, function(buffer) {

                    let data = buffer.getChannelData(0);

                    let sum = 0;

                    for (let i = 0; i < data.length; i++) {

                        sum += data[i] * data[i];

                    }

                    let rms = Math.sqrt(sum / data.length);

                    document.getElementById(rmsElementId).textContent = `RMS: ${rms.toFixed(6)}`;

                    

                    let lufs = 20 * Math.log10(rms);

                    document.getElementById(lufsElementId).textContent = `LUFS: ${lufs.toFixed(2)}`;

                });

            };

        }

    </script>
</body>
</html>