File size: 12,167 Bytes
ddd03df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>YouTube Chord Analyzer</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://cdn.jsdelivr.net/npm/tonal@4.6.2/dist/tonal.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@magenta/music@1.23.1/es6/core.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        .piano-key {
            position: relative;
            border: 1px solid #333;
            display: flex;
            align-items: flex-end;
            justify-content: center;
            cursor: pointer;
            user-select: none;
        }
        
        .white-key {
            width: 40px;
            height: 160px;
            background-color: white;
            z-index: 1;
            border-radius: 0 0 5px 5px;
            box-shadow: 0 5px 5px rgba(0,0,0,0.2);
        }
        
        .black-key {
            width: 24px;
            height: 100px;
            background-color: black;
            margin-left: -12px;
            margin-right: -12px;
            z-index: 2;
            border-radius: 0 0 3px 3px;
        }
        
        .active-key {
            background-color: #3b82f6;
        }
        
        .active-black-key {
            background-color: #1d4ed8;
        }
        
        .chord-progression {
            background-color: rgba(59, 130, 246, 0.2);
            border-left: 3px solid #3b82f6;
        }
        
        .visualizer-container {
            position: relative;
            height: 200px;
            background-color: #1e293b;
            border-radius: 8px;
            overflow: hidden;
        }
        
        .audio-wave {
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 100%;
            display: flex;
            align-items: flex-end;
        }
        
        .audio-bar {
            flex-grow: 1;
            background-color: rgba(59, 130, 246, 0.6);
            margin-right: 2px;
            transition: height 0.05s ease;
        }
        
        .audio-bar:last-child {
            margin-right: 0;
        }
        
        @keyframes pulse {
            0% { transform: scale(1); }
            50% { transform: scale(1.05); }
            100% { transform: scale(1); }
        }
        
        .analyzing {
            animation: pulse 1.5s infinite;
        }
    </style>
</head>
<body class="bg-gray-100 min-h-screen">
    <div class="container mx-auto px-4 py-8">
        <header class="mb-8 text-center">
            <h1 class="text-4xl font-bold text-blue-600 mb-2">YouTube Chord Analyzer</h1>
            <p class="text-gray-600 text-lg">Detect chords and keys from any YouTube video in real-time</p>
        </header>
        
        <div class="bg-white rounded-xl shadow-lg p-6 mb-8">
            <div class="flex flex-col md:flex-row gap-6">
                <div class="flex-1">
                    <div class="mb-4">
                        <label for="youtube-url" class="block text-sm font-medium text-gray-700 mb-1">YouTube Video URL</label>
                        <div class="flex gap-2">
                            <input type="text" id="youtube-url" placeholder="https://www.youtube.com/watch?v=..." 
                                   class="flex-1 px-4 py-2 border border-gray-300 rounded-lg focus:ring-blue-500 focus:border-blue-500">
                            <button id="analyze-btn" class="bg-blue-600 hover:bg-blue-700 text-white px-6 py-2 rounded-lg font-medium transition-colors">
                                <i class="fas fa-play mr-2"></i> Analyze
                            </button>
                        </div>
                    </div>
                    
                    <div id="video-container" class="aspect-w-16 aspect-h-9 bg-gray-200 rounded-lg overflow-hidden mb-4">
                        <div class="flex items-center justify-center h-full text-gray-500">
                            <i class="fas fa-music text-4xl"></i>
                        </div>
                    </div>
                    
                    <div class="flex items-center justify-between mb-4">
                        <div class="flex items-center gap-2">
                            <button id="play-btn" class="bg-blue-600 hover:bg-blue-700 text-white p-2 rounded-full disabled:opacity-50" disabled>
                                <i class="fas fa-play"></i>
                            </button>
                            <button id="pause-btn" class="bg-gray-200 hover:bg-gray-300 text-gray-800 p-2 rounded-full disabled:opacity-50" disabled>
                                <i class="fas fa-pause"></i>
                            </button>
                            <span id="time-display" class="text-sm text-gray-600">00:00 / 00:00</span>
                        </div>
                        <div id="status-indicator" class="flex items-center gap-2">
                            <span class="text-sm font-medium text-gray-500">Ready</span>
                            <div class="w-3 h-3 rounded-full bg-gray-400"></div>
                        </div>
                    </div>
                    
                    <div class="visualizer-container mb-4">
                        <div id="audio-wave" class="audio-wave"></div>
                    </div>
                </div>
                
                <div class="flex-1">
                    <div class="bg-gray-50 rounded-lg p-4 mb-4">
                        <h3 class="font-medium text-lg text-gray-800 mb-3">Current Analysis</h3>
                        
                        <div class="grid grid-cols-2 gap-4 mb-4">
                            <div class="bg-white p-3 rounded-lg shadow-sm">
                                <p class="text-sm text-gray-500 mb-1">Detected Key</p>
                                <p id="current-key" class="text-2xl font-bold text-blue-600">-</p>
                            </div>
                            <div class="bg-white p-3 rounded-lg shadow-sm">
                                <p class="text-sm text-gray-500 mb-1">Current Chord</p>
                                <p id="current-chord" class="text-2xl font-bold text-blue-600">-</p>
                            </div>
                        </div>
                        
                        <div class="mb-4">
                            <p class="text-sm text-gray-500 mb-2">Chord Notes</p>
                            <div id="piano" class="flex relative h-40 mb-2"></div>
                        </div>
                        
                        <div>
                            <p class="text-sm text-gray-500 mb-2">Chord Progression</p>
                            <div id="chord-progression" class="flex gap-2 overflow-x-auto py-2">
                                <div class="text-center">
                                    <div class="w-12 h-12 bg-gray-200 rounded-full flex items-center justify-center mx-auto mb-1">
                                        <span class="text-gray-600">?</span>
                                    </div>
                                    <span class="text-xs text-gray-500">0:00</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="bg-white rounded-xl shadow-lg p-6">
            <h2 class="text-xl font-bold text-gray-800 mb-4">Chord Analysis Results</h2>
            
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Time</th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Bar</th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Measure</th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Chord</th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Notes</th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Duration</th>
                        </tr>
                    </thead>
                    <tbody id="results-table" class="bg-white divide-y divide-gray-200">
                        <tr>
                            <td colspan="6" class="px-6 py-4 text-center text-gray-500">No analysis data yet. Enter a YouTube URL and click "Analyze"</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <script>
        // Initialize variables
        let player;
        let audioContext;
        let analyser;
        let dataArray;
        let animationId;
        let currentChord = '';
        let detectedKey = '';
        let chordHistory = [];
        let barCount = 1;
        let measureCount = 1;
        let lastChordChangeTime = 0;
        
        // DOM elements
        const youtubeUrlInput = document.getElementById('youtube-url');
        const analyzeBtn = document.getElementById('analyze-btn');
        const videoContainer = document.getElementById('video-container');
        const playBtn = document.getElementById('play-btn');
        const pauseBtn = document.getElementById('pause-btn');
        const timeDisplay = document.getElementById('time-display');
        const statusIndicator = document.getElementById('status-indicator');
        const statusText = statusIndicator.querySelector('span');
        const statusDot = statusIndicator.querySelector('div');
        const audioWave = document.getElementById('audio-wave');
        const currentKeyDisplay = document.getElementById('current-key');
        const currentChordDisplay = document.getElementById('current-chord');
        const piano = document.getElementById('piano');
        const chordProgression = document.getElementById('chord-progression');
        const resultsTable = document.getElementById('results-table');
        
        // Initialize piano keyboard
        function initPiano() {
            piano.innerHTML = '';
            
            const whiteKeys = ['C', 'D', 'E', 'F', 'G', 'A', 'B'];
            const blackKeys = ['C#', 'D#', '', 'F#', 'G#', 'A#', ''];
            
            whiteKeys.forEach((note, i) => {
                const key = document.createElement('div');
                key.className = 'piano-key white-key';
                key.dataset.note = note;
                key.innerHTML = `<span class="text-xs text-gray-500 mb-1">${note}</span>`;
                piano.appendChild(key);
                
                // Add black keys between appropriate white keys
                if (i < 6 && blackKeys[i] !== '') {
                    const blackKey = document.createElement('div');
                    blackKey.className = 'piano-key black-key';
                    blackKey.dataset.note = blackKeys[i];
                    piano.appendChild(blackKey);
                }
            });
        }
        
        // Highlight piano keys for current chord
        function highlightChord(chord) {
            // Reset all keys
            document.querySelectorAll('.piano-key').forEach(key => {
                if (key.classList.contains('white-key')) {
                    key.classList.remove('active-key');
                } else {
                    key.classList.remove('active-black-key');
                }
            });
            
            if (!chord
</html>