Repository navigation
Expand file tree
/
Copy pathjava.js
More file actions
674 lines (582 loc) · 29.8 KB
/
Copy pathjava.js
File metadata and controls
674 lines (582 loc) · 29.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
// Backend Simulation - Database and API Layer
class SnoringSymphonyBackend {
constructor() {
this.data = {
gallery: [],
leaderboard: [],
stats: {
totalSnores: 247,
totalShares: 1423,
totalLaughs: 8956
},
users: new Map()
};
this.initializeDefaultData();
}
initializeDefaultData() {
// Initialize with some sample data
this.data.gallery = [
{
id: '1',
name: 'Midnight Jazz Snore',
genre: 'jazz',
creator: 'SleepyJazzLover',
plays: 542,
likes: 89,
shares: 34,
audioUrl: null,
timestamp: new Date('2024-12-01')
},
{
id: '2',
name: 'Horror Snore Attack',
genre: 'horror',
creator: 'NightmareKing',
plays: 721,
likes: 156,
shares: 67,
audioUrl: null,
timestamp: new Date('2024-12-02')
},
{
id: '3',
name: 'Rap Battle Snore',
genre: 'rap',
creator: 'SnoreFlow',
plays: 389,
likes: 92,
shares: 28,
audioUrl: null,
timestamp: new Date('2024-12-03')
}
];
this.data.leaderboard = [
{ position: 1, name: 'SnoreKing2025', track: 'The Thunderous Classical', genre: 'classical', score: 2847, meme: '👑' },
{ position: 2, name: 'SleepyMcSnoreface', track: 'Jazz Fusion Madness', genre: 'jazz', score: 2134, meme: '🥈' },
{ position: 3, name: 'NightNoiseNinja', track: 'Western Showdown', genre: 'western', score: 1823, meme: '🥉' }
];
}
// API Methods
async uploadSnore(audioBlob, metadata) {
return new Promise((resolve) => {
setTimeout(() => {
const id = Date.now().toString();
const audioUrl = URL.createObjectURL(audioBlob);
resolve({ success: true, audioUrl, id });
}, 1000);
});
}
async transformAudio(audioUrl, genre, remixGenres = []) {
return new Promise((resolve) => {
const processingTime = remixGenres.length > 0 ? 4000 : 3000;
setTimeout(() => {
const trackName = this.generateTrackName(genre, remixGenres);
const transformedUrl = audioUrl; // In real app, this would be processed audio
const newTrack = {
id: Date.now().toString(),
name: trackName,
genre: remixGenres.length > 0 ? 'remix' : genre,
genres: remixGenres.length > 0 ? remixGenres : [genre],
creator: 'You',
plays: 0,
likes: 0,
shares: 0,
audioUrl: transformedUrl,
timestamp: new Date()
};
this.data.gallery.unshift(newTrack);
this.updateStats();
resolve({
success: true,
track: newTrack,
message: `🎉 SUCCESS! Created: "${trackName}" - Ready to share! 🚀`
});
}, processingTime);
});
}
generateTrackName(genre, remixGenres = []) {
if (remixGenres.length > 0) {
return `Ultimate ${remixGenres.join('-').toUpperCase()} Chaos Symphony`;
}
const names = {
jazz: ["Smooth Snore Serenade", "Midnight Snore Blues", "Bebop Bedroom Beat", "Cool Cat Snore", "Jazzy Sleepy Time"],
rap: ["Snore Flow Supreme", "Sleep Rap Battle", "Bedtime Bars & Beats", "MC Sleepy's Drop", "Snore Cypher"],
horror: ["Nightmare Snore Symphony", "Terror in the Bedroom", "Haunted Sleep Sounds", "Demon Snore", "Scary Sleep"],
classical: ["Snore Symphony No. 9", "The Sleeping Beauty Overture", "Nocturne for Nostrils", "Beethoven's Dream", "Mozart's Sleep"],
western: ["Cowboy's Lullaby", "Wild West Sleep Sounds", "Snore on the Range", "Sheriff's Slumber", "Desert Dream"]
};
return names[genre][Math.floor(Math.random() * names[genre].length)];
}
async shareToSocial(trackId, platform) {
return new Promise((resolve) => {
setTimeout(() => {
const track = this.data.gallery.find(t => t.id === trackId);
if (track) {
track.shares++;
this.updateStats();
}
const messages = {
whatsapp: `🎵 Check out my epic snore symphony "${track?.name}"! Made with Snoring Symphony! 😴🎭 #SnoreArt`,
instagram: `Just turned my snore into a ${track?.genre?.toUpperCase()} masterpiece! 🎵😂 #SnoringSymphony #EpicSnores #SleepMusic`,
youtube: `My LEGENDARY ${track?.genre?.toUpperCase()} snore transformation: "${track?.name}"! 🎵👑 Created with Snoring Symphony!`
};
resolve({
success: true,
platform,
message: messages[platform],
shareUrl: `https://snoringsymphony.com/track/${trackId}`
});
}, 500);
});
}
async updateLeaderboard(trackName, genre, score = null) {
const newScore = score || Math.floor(Math.random() * 1000) + 500;
const newEntry = {
position: this.data.leaderboard.length + 1,
name: 'You',
track: trackName,
genre: genre,
score: newScore,
meme: this.getRandomMemeEmoji()
};
this.data.leaderboard.push(newEntry);
this.data.leaderboard.sort((a, b) => b.score - a.score);
// Update positions
this.data.leaderboard.forEach((entry, index) => {
entry.position = index + 1;
});
return newEntry;
}
getRandomMemeEmoji() {
const memes = ['🤪', '😂', '🎭', '🎪', '🦄', '🚀', '⭐', '💫', '🎯', '🔥'];
return memes[Math.floor(Math.random() * memes.length)];
}
updateStats() {
this.data.stats.totalSnores++;
this.data.stats.totalLaughs += Math.floor(Math.random() * 50) + 10;
}
getGallery() {
return this.data.gallery.sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp));
}
getLeaderboard() {
return this.data.leaderboard;
}
getStats() {
return this.data.stats;
}
async deleteTrack(trackId) {
return new Promise((resolve) => {
setTimeout(() => {
this.data.gallery = this.data.gallery.filter(track => track.id !== trackId);
resolve({ success: true });
}, 500);
});
}
async likeTrack(trackId) {
return new Promise((resolve) => {
setTimeout(() => {
const track = this.data.gallery.find(t => t.id === trackId);
if (track) {
track.likes++;
this.updateStats();
}
resolve({ success: true, likes: track?.likes || 0 });
}, 300);
});
}
}
// Frontend Application
class SnoringSymphonyApp {
constructor() {
this.backend = new SnoringSymphonyBackend();
this.isRecording = false;
this.mediaRecorder = null;
this.audioChunks = [];
this.selectedGenre = null;
this.remixGenres = [];
this.recordedAudio = null;
this.uploadedAudio = null;
this.memes = [
"...NASA thought it was a rocket launch! 🚀",
"...the earthquake department called to check! 🌍",
"...your neighbors moved to another country! 🏃♂️",
"...even hibernating bears woke up! 🐻",
"...it registered on the Richter scale! 📊",
"...aliens thought it was a communication signal! 👽",
"...thunder got jealous! ⚡",
"...the ocean waves got scared! 🌊",
"...even zombies couldn't sleep! 🧟♂️",
"...it became a new alarm clock standard! ⏰",
"...seismologists reported a new kind of tremor! 🌋",
"...your pet started wearing earplugs! 🐕",
"...the Wi-Fi signal got disrupted! 📶",
"...even construction workers took a break! 👷♂️",
"...birds started migrating in December! 🦅"
];
this.init();
}
init() {
this.setupEventListeners();
this.loadInitialData();
this.updateStatsDisplay();
this.startRandomMemeGeneration();
}
setupEventListeners() {
// Record button
document.getElementById('recordBtn').addEventListener('click', () => this.toggleRecording());
// Upload area
const uploadArea = document.getElementById('uploadArea');
const fileInput = document.getElementById('fileInput');
uploadArea.addEventListener('click', () => fileInput.click());
uploadArea.addEventListener('dragover', this.handleDragOver.bind(this));
uploadArea.addEventListener('dragleave', this.handleDragLeave.bind(this));
uploadArea.addEventListener('drop', this.handleFileDrop.bind(this));
fileInput.addEventListener('change', this.handleFileSelect.bind(this));
}
async loadInitialData() {
this.renderGallery();
this.renderLeaderboard();
}
updateStatsDisplay() {
const stats = this.backend.getStats();
document.getElementById('totalSnores').textContent = stats.totalSnores.toLocaleString();
document.getElementById('totalShares').textContent = stats.totalShares.toLocaleString();
document.getElementById('totalLaughs').textContent = stats.totalLaughs.toLocaleString();
}
startRandomMemeGeneration() {
setInterval(() => {
if (Math.random() < 0.3) { // 30% chance every interval
this.generateMeme();
}
}, 15000); // Every 15 seconds
}
async toggleRecording() {
const btn = document.getElementById('recordBtn');
const status = document.getElementById('status');
const visualizer = document.getElementById('visualizer');
const audioControls = document.getElementById('recordedAudioControls');
if (!this.isRecording) {
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
this.mediaRecorder = new MediaRecorder(stream);
this.audioChunks = [];
this.mediaRecorder.ondataavailable = event => {
this.audioChunks.push(event.data);
};
this.mediaRecorder.onstop = async () => {
const audioBlob = new Blob(this.audioChunks, { type: 'audio/wav' });
const result = await this.backend.uploadSnore(audioBlob, { source: 'recording' });
if (result.success) {
this.recordedAudio = result.audioUrl;
const audioElement = document.getElementById('recordedAudio');
audioElement.src = this.recordedAudio;
audioControls.style.display = 'block';
status.textContent = "Epic snore recorded! Choose a genre to transform it! 🎵";
}
visualizer.style.display = 'none';
};
this.mediaRecorder.start();
this.isRecording = true;
btn.classList.add('recording');
btn.innerHTML = '<div>⏹️<br>STOP</div>';
status.textContent = "Recording your legendary snore... 🎙️";
visualizer.style.display = 'flex';
} catch (err) {
console.error('Error accessing microphone:', err);
status.textContent = "Oops! Couldn't access microphone 🎤❌";
}
} else {
this.mediaRecorder.stop();
this.isRecording = false;
btn.classList.remove('recording');
btn.innerHTML = '<div>🎙️<br>START</div>';
this.mediaRecorder.stream.getTracks().forEach(track => track.stop());
}
}
handleDragOver(e) {
e.preventDefault();
document.getElementById('uploadArea').style.background = 'rgba(255,255,255,0.2)';
}
handleDragLeave(e) {
e.preventDefault();
document.getElementById('uploadArea').style.background = 'transparent';
}
handleFileDrop(e) {
e.preventDefault();
document.getElementById('uploadArea').style.background = 'transparent';
const files = e.dataTransfer.files;
if (files.length > 0) {
this.processFile(files[0]);
}
}
handleFileSelect(e) {
if (e.target.files.length > 0) {
this.processFile(e.target.files[0]);
}
}
async processFile(file) {
const uploadStatus = document.getElementById('uploadStatus');
const audioControls = document.getElementById('uploadedAudioControls');
if (file.type.startsWith('audio/')) {
const result = await this.backend.uploadSnore(file, { source: 'upload', filename: file.name });
if (result.success) {
this.uploadedAudio = result.audioUrl;
const audioElement = document.getElementById('uploadedAudio');
audioElement.src = this.uploadedAudio;
audioControls.style.display = 'block';
uploadStatus.textContent = `✅ ${file.name} loaded! Ready to transform! 🎵`;
}
} else {
uploadStatus.textContent = "❌ Please upload an audio file!";
}
}
selectGenre(genre) {
// Remove previous selection
document.querySelectorAll('.genre-btn').forEach(btn => btn.classList.remove('selected'));
// Add selection to current genre
document.querySelector(`.genre-btn.${genre}`).classList.add('selected');
this.selectedGenre = genre;
const status = document.getElementById('genreStatus');
const currentAudio = this.recordedAudio || this.uploadedAudio;
if (currentAudio) {
this.transformAudio(genre);
} else {
status.textContent = `${genre.toUpperCase()} selected! Record or upload a snore first! 🎵`;
}
}
async transformAudio(genre) {
const status = document.getElementById('genreStatus');
const overlay = document.getElementById('processingOverlay');
const processingText = document.getElementById('processingText');
const currentAudio = this.recordedAudio || this.uploadedAudio;
if (!currentAudio) {
status.textContent = "Please record or upload a snore first! 🎙️";
return;
}
// Show processing overlay
overlay.style.display = 'flex';
processingText.textContent = `🎵 Transforming your snore into ${genre.toUpperCase()} magic... ✨`;
try {
const result = await this.backend.transformAudio(currentAudio, genre);
if (result.success) {
status.textContent = result.message;
await this.backend.updateLeaderboard(result.track.name, genre);
this.renderGallery();
this.renderLeaderboard();
this.updateStatsDisplay();
// Show success animation
this.showSuccessAnimation(result.track.name);
}
} catch (error) {
status.textContent = "❌ Something went wrong during transformation!";
} finally {
overlay.style.display = 'none';
}
}
addToRemix(genre) {
if (!this.remixGenres.includes(genre)) {
this.remixGenres.push(genre);
document.getElementById('remixStatus').textContent =
`Remix ingredients: ${this.remixGenres.map(g => g.toUpperCase()).join(', ')} 🎭`;
}
}
async createRemix() {
const remixStatus = document.getElementById('remixStatus');
if (this.remixGenres.length < 2) {
remixStatus.textContent = "Add at least 2 genres for ultimate chaos! 🎪";
return;
}
const currentAudio = this.recordedAudio || this.uploadedAudio;
if (!currentAudio) {
remixStatus.textContent = "Record or upload a snore first! 🎙️";
return;
}
const overlay = document.getElementById('processingOverlay');
const processingText = document.getElementById('processingText');
overlay.style.display = 'flex';
processingText.textContent = "🎭 Creating the most chaotic snore remix ever... Hold on! 🌪️";
try {
const result = await this.backend.transformAudio(currentAudio, 'remix', this.remixGenres);
if (result.success) {
remixStatus.textContent = `🎉 REMIX COMPLETE: "${result.track.name}" - This is LEGENDARY! 👑`;
await this.backend.updateLeaderboard(result.track.name, 'remix');
this.renderGallery();
this.renderLeaderboard();
this.updateStatsDisplay();
this.remixGenres = [];
this.showSuccessAnimation(result.track.name);
}
} catch (error) {
remixStatus.textContent = "❌ Remix failed! Try again!";
} finally {
overlay.style.display = 'none';
}
}
showSuccessAnimation(trackName) {
// Create floating success message
const successMsg = document.createElement('div');
successMsg.style.cssText = `
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: linear-gradient(45deg, #00d4aa, #00b894);
color: white;
padding: 20px;
border-radius: 15px;
font-size: 1.2rem;
font-weight: bold;
z-index: 1001;
animation: successPop 3s ease-in-out forwards;
box-shadow: 0 10px 30px rgba(0,212,170,0.5);
`;
successMsg.textContent = `🎉 "${trackName}" created successfully! 🚀`;
// Add animation keyframes
if (!document.getElementById('successAnimation')) {
const style = document.createElement('style');
style.id = 'successAnimation';
style.textContent = `
@keyframes successPop {
0% { transform: translate(-50%, -50%) scale(0); opacity: 0; }
20% { transform: translate(-50%, -50%) scale(1.2); opacity: 1; }
80% { transform: translate(-50%, -50%) scale(1); opacity: 1; }
100% { transform: translate(-50%, -50%) scale(0); opacity: 0; }
}
`;
document.head.appendChild(style);
}
document.body.appendChild(successMsg);
setTimeout(() => {
document.body.removeChild(successMsg);
}, 3000);
}
renderGallery() {
const gallery = this.backend.getGallery();
const galleryGrid = document.getElementById('galleryGrid');
galleryGrid.innerHTML = gallery.map(track => {
const genreEmojis = {
jazz: "🎷", rap: "🎤", horror: "👻",
classical: "🎻", western: "🤠", remix: "🎭"
};
return `
<div class="gallery-item" data-track-id="${track.id}">
<h3>${genreEmojis[track.genre] || '🎵'} ${track.name}</h3>
<p>By: ${track.creator}</p>
<p>🎧 ${track.plays} plays • ❤️ ${track.likes} likes • 📤 ${track.shares} shares</p>
${track.audioUrl ? `
<audio class="audio-player" controls>
<source src="${track.audioUrl}" type="audio/wav">
</audio>
` : ''}
<div class="audio-controls">
<button class="play-btn" onclick="app.likeTrack('${track.id}')">❤️ Like</button>
${track.creator === 'You' ? `<button class="delete-btn" onclick="app.deleteTrack('${track.id}')">🗑️ Delete</button>` : ''}
</div>
<div class="social-share">
<button class="social-btn whatsapp" onclick="app.shareToSocial('${track.id}', 'whatsapp')">WhatsApp</button>
<button class="social-btn instagram" onclick="app.shareToSocial('${track.id}', 'instagram')">Instagram</button>
<button class="social-btn youtube" onclick="app.shareToSocial('${track.id}', 'youtube')">YouTube</button>
</div>
</div>
`;
}).join('');
}
renderLeaderboard() {
const leaderboard = this.backend.getLeaderboard();
const leaderboardList = document.getElementById('leaderboardList');
leaderboardList.innerHTML = leaderboard.map((entry, index) => {
const backgrounds = [
'linear-gradient(45deg, #ffd700, #ffed4e)',
'linear-gradient(45deg, #c0c0c0, #e5e5e5)',
'linear-gradient(45deg, #cd7f32, #daa520)',
'rgba(255,255,255,0.1)'
];
const textColors = ['color: #333;', 'color: #333;', 'color: white;', 'color: white;'];
return `
<div class="leaderboard-item" style="background: ${backgrounds[index] || backgrounds[3]}; ${textColors[index] || textColors[3]}">
<div class="winner-meme">${entry.meme}</div>
<div>
<h3>${entry.position === 1 ? '🥇' : entry.position === 2 ? '🥈' : entry.position === 3 ? '🥉' : '🏅'} ${entry.name} - "${entry.track}"</h3>
<p>${entry.genre.toUpperCase()} • Score: ${entry.score.toLocaleString()} points</p>
</div>
</div>
`;
}).join('');
}
async shareToSocial(trackId, platform) {
try {
const result = await this.backend.shareToSocial(trackId, platform);
if (result.success) {
// Show share dialog
const shareText = `Sharing to ${platform.toUpperCase()}!\n\n"${result.message}"\n\n🚀 Link: ${result.shareUrl}`;
alert(shareText);
// Update gallery display
this.renderGallery();
this.updateStatsDisplay();
// Increment global shares
this.backend.data.stats.totalShares++;
}
} catch (error) {
alert(`Failed to share to ${platform}. Please try again!`);
}
}
async deleteTrack(trackId) {
if (confirm('Are you sure you want to delete this masterpiece?')) {
try {
const result = await this.backend.deleteTrack(trackId);
if (result.success) {
this.renderGallery();
alert('Track deleted successfully! 🗑️');
}
} catch (error) {
alert('Failed to delete track. Please try again!');
}
}
}
async likeTrack(trackId) {
try {
const result = await this.backend.likeTrack(trackId);
if (result.success) {
this.renderGallery();
this.updateStatsDisplay();
}
} catch (error) {
console.error('Failed to like track:', error);
}
}
generateMeme() {
const memeText = document.getElementById('memeText');
const randomMeme = this.memes[Math.floor(Math.random() * this.memes.length)];
memeText.textContent = randomMeme;
// Add a little animation
memeText.style.transform = 'scale(1.1)';
memeText.style.transition = 'transform 0.3s ease';
setTimeout(() => {
memeText.style.transform = 'scale(1)';
}, 300);
}
}
// Initialize the application
let app;
document.addEventListener('DOMContentLoaded', () => {
app = new SnoringSymphonyApp();
});
// Global functions for onclick handlers
function selectGenre(genre) {
app.selectGenre(genre);
}
function addToRemix(genre) {
app.addToRemix(genre);
}
function createRemix() {
app.createRemix();
}
function generateMeme() {
app.generateMeme();
}
// Auto-update stats periodically
setInterval(() => {
if (app) {
app.updateStatsDisplay();
}
}, 30000); // Every 30 seconds