-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathskytunes.cpp
656 lines (635 loc) · 18.4 KB
/
skytunes.cpp
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
#include <bits/stdc++.h>
#include <windows.h>
#include <ctime>
#include <iostream>
#include <filesystem>
#include <fstream>
namespace fil = std::filesystem;
time_t t1, t2, previous_pause_time = 0;
using namespace std;
const int mxm = 1e9 + 7;
const int mx = 1e5 + 1;
string current;
string current_name;
bool check_validity(string s)
{
ifstream f(s.c_str());
return f.good();
}
string clean_path(string s)
{
int t = s.size();
string ans;
for (int i = t - 1; i >= 0; i--)
{
if (s[i] == '\\')
{
break;
}
else
{
ans.push_back(s[i]);
}
}
reverse(ans.begin(), ans.end());
for (int i = 0; i < 4; i++)
{
ans.pop_back();
}
return ans;
}
bool playing = 0;
class Node
{
private:
string mp3_path;
string mp3_name;
Node *ne;
Node *pr;
friend class DoublyLinkedList;
};
map<string, Node *> collections;
void play_music()
{
string s = "open \"" + current;
s += "\" type mpegvideo alias mp3";
mciSendString(s.c_str(), NULL, 0, NULL);
mciSendString("play mp3", NULL, 0, NULL);
}
class DoublyLinkedList
{
public:
DoublyLinkedList()
{
h = new Node();
t = new Node();
h->ne = t;
t->pr = h;
h->mp3_path = "a";
t->ne = NULL;
h->pr = NULL;
c = h;
}
void add_back(string s)
{
Node *w = new Node();
w->mp3_path = s;
Node *i = t->pr;
i->ne = w;
w->ne = t;
w->pr = i;
t->pr = w;
w->mp3_name = clean_path(s);
collections[w->mp3_name] = w;
}
void moveanywhere(string s1, string s2)
{
Node *t1 = collections[s1];
Node *t2 = collections[s2];
Node *t1_prev = t1->pr;
Node *t1_next = t1->ne;
Node *t2_prev = t2->pr;
Node *t2_next = t2->ne;
cout << "press 1 to add after " << s2 << endl;
cout << "press 0 to add before " << s2 << endl;
string key;
label6:
cin >> key;
if (key == "1")
{
if(t2->ne==t1){
return;
}
PATH7:
t1_prev->ne = t1_next;
t1_next->pr = t1_prev;
t1->ne = t2_next;
t1->pr = t2;
t2->ne = t1;
t2_next->pr = t1;
}
else if (key == "0")
{
PATH8:
if(t2->pr==t1){
return;
}
t1_prev->ne = t1_next;
t1_next->pr = t1_prev;
t1->ne = t2;
t1->pr = t2_prev;
t2_prev->ne = t1;
t2->pr = t1;
}
else
{
cout << "Wrong Enter again\n";
goto label6;
}
}
void delete_song(string s)
{
if (s == c->mp3_name)
{
next_song();
play_current();
mciSendString("close mp3", NULL, 0, NULL);
play_music();
}
Node *temp = collections[s];
Node *t_next = temp->ne;
Node *t_prev = temp->pr;
t_prev->ne = t_next;
t_next->pr = t_prev;
delete (temp);
cout << " Your song has been deleted" << endl;
collections.erase(s);
}
void next_song()
{
if (c == t->pr)
{
c = h;
}
c = c->ne;
}
void previous_song()
{
if (c->pr != h)
{
c = c->pr;
}
else
{
c = t->pr;
}
}
void play_current()
{
if (c->mp3_path == "a")
{
c = c->ne;
}
current = c->mp3_path;
current_name = c->mp3_name;
cout << current_name << "\n";
}
void save_all()
{
std::ofstream ofs;
ofs.open("save.txt", std::ofstream::out | std::ofstream::trunc);
ofs.close();
std::filesystem::path cwd = std::filesystem::current_path();
string path = cwd.string();
path += "\\save.txt";
ofstream fw(path.c_str(), std::ofstream::out);
Node *cu = h->ne;
while (cu->ne != NULL)
{
fw << cu->mp3_path << "\n";
cu = cu->ne;
}
fw.close();
}
void display_list()
{
int cnt = 1;
Node *cu = h->ne;
while (cu->ne != NULL)
{
cout << cnt << ". " << cu->mp3_name << "\n";
cnt++;
cu = cu->ne;
}
}
void insert_anywhere(string name)
{
Node *r = collections[name];
cout << "press 1 to add after this song\npress 0 to add before this song.";
string key;
label1:
cin >> key;
if (key == "1")
{
cin.ignore();
PATH:
cout << "Enter path of song:";
string path1;
getline(cin, path1);
if (!check_validity(path1))
{
cout << "Please enter a valid path! " << endl;
goto PATH;
}
if (path1[path1.size() - 1] != '3')
{
cout << "\nPlease enter a valid song: ";
goto PATH;
}
Node *u = new Node();
u->mp3_path = path1;
u->mp3_name = clean_path(path1);
(r->ne)->pr = u;
u->ne = r->ne;
r->ne = u;
u->pr = r;
collections[u->mp3_name] = u;
}
else if (key == "0")
{
cin.ignore();
PATH2:
cout << "Enter path of song:";
string path1;
getline(cin, path1);
if (!check_validity(path1))
{
cout << "Please enter a valid path! " << endl;
goto PATH2;
}
if (path1[path1.size() - 1] != '3')
{
cout << "\nPlease enter a valid song: ";
goto PATH2;
}
Node *u = new Node();
u->mp3_path = path1;
u->mp3_name = clean_path(path1);
Node *t = r->pr;
t->ne = u;
u->ne = r;
u->pr = t;
r->pr = u;
collections[u->mp3_name] = u;
}
else
{
cout << "Wrong Enter again\n";
goto label1;
}
}
void goto_song()
{
cout << endl;
cout << "Enter Name of the song:";
string si;
cin.ignore();
sip:
getline(cin, si);
if(collections.find(si)==collections.end()){
cout<<"Please Enter a valid Name!\n";
goto sip;
}
c = collections[si];
playing = 1;
mciSendString("close mp3", NULL, 0, NULL);
t1 = time(0);
t2 = time(0);
play_current();
play_music();
}
void insert_next()
{
cin.ignore();
PATH3:
string path1;
getline(cin, path1);
if (!check_validity(path1))
{
cout << "Please enter a valid path! " << endl;
goto PATH3;
}
if (path1[path1.size() - 1] != '3')
{
cout << "\nPlease Enter a MP3 file!";
goto PATH3;
}
Node *u = new Node();
u->mp3_path = path1;
u->mp3_name = clean_path(path1);
(c->ne)->pr = u;
u->ne = c->ne;
c->ne = u;
u->pr = c;
collections[u->mp3_name] = u;
}
private:
Node *h;
Node *t;
Node *c;
};
DoublyLinkedList *dl = new DoublyLinkedList();
void save_playlist()
{
dl->save_all();
}
void read_folder()
{
std::filesystem::path cwd = std::filesystem::current_path();
string path = cwd.string();
path += "\\music";
for (const auto &entry : fil::directory_iterator(path))
{
string e = entry.path().string();
dl->add_back(e);
}
}
void make_list()
{
string paths;
ifstream Fileread("save.txt");
while (getline(Fileread, paths))
{
dl->add_back(paths);
}
}
void display_all()
{
dl->display_list();
}
int main()
{
int n = 0;
char ch;
FILE *f = fopen("save.txt", "r");
if (fscanf(f, "%c", &ch) == EOF)
{
read_folder();
}
else
{
make_list();
}
fclose(f);
bool sta = false; // this is to check if this is the first file that player will play;
// escape-quit
// space pause play
// arrow for changing songs
string key;
while (true)
{
if (!sta)
{
cout << "Press 1 to start skytunes and press 4 to quit\n";
goto label;
}
label:
key = -9;
cin >> key;
system("CLS");
cout<<" _______ ___ _ __ __ _______ __ __ __ _ _______ _______\n";
cout<<" | || | | || | | || || | | || | | || || |\n";
cout<<" | _____|| |_| || |_| ||_ _|| | | || |_| || ___|| _____|\n";
cout<<" | |_____ | _|| | | | | |_| || || |___ | |_____ \n";
cout<<" |_____ || |_ |_ _| | | | || _ || ___||_____ |\n";
cout<<" _____| || _ | | | | | | || | | || |___ _____| |\n";
cout<<" |_______||___| |_| |___| |___| |_______||_| |__||_______||_______|\n";
cout << "Press 1 to pause/play the file \npress 2 to play the next song\npress 3 to play the previous song\npress 4 to exit the file.\npress 5 to display the playlist\npress 6 to add song at next position\npress 7 to add song anywhere" << endl;
cout << "Press 8 to play any song" << endl;
cout << "Press 9 to delete a song" << endl;
cout << "Press 10 to move song anywhere" << endl;
sta = true;
if (key == "1")
{
if (!playing)
{
t1 = time(0);
dl->play_current();
playing = 1;
cout << " Audio file playing...\n\n";
play_music();
}
else
{
mciSendString("pause mp3", NULL, 0, NULL);
t2 = time(0);
playing = 0;
cout << "Audio file paused after " << t2 - t1 + previous_pause_time << " seconds.\n\n";
previous_pause_time += t2 - t1;
}
}
else if (key == "2")
{
mciSendString("close mp3", NULL, 0, NULL);
t1 = time(0);
previous_pause_time = 0;
dl->next_song();
dl->play_current();
playing = 1;
cout << " Audio file playing...\n\n";
play_music();
}
else if (key == "3")
{
mciSendString("close mp3", NULL, 0, NULL);
t1 = time(0);
dl->previous_song();
previous_pause_time = 0;
dl->play_current();
playing = 1;
cout << " Audio file playing...\n\n";
play_music();
}
else if (key == "4")
{
save_playlist();
exit(0);
}
else if (key == "5")
{
display_all();
}
else if (key == "6")
{
cout << "add path of the song:";
dl->insert_next();
}
else if (key == "7")
{
cout << "Enter name of song with respect you want to insert:";
string si;
cin.ignore();
PATH4:
getline(cin, si);
if (collections.find(si) == collections.end())
{
cout << "Enter a valid song name!" << endl;
goto PATH4;
}
dl->insert_anywhere(si);
}
else if (key == "8")
{
dl->goto_song();
}
else if (key == "9")
{
cout << "Enter the song name you want to delete: ";
string s;
cin.ignore();
PATH5:
getline(cin, s);
if(collections.find(s)==collections.end()){
cout<<"Please enter a valid name!\n";
goto PATH5;
}
dl->delete_song(s);
}
else if (key == "10")
{
cout << "Enter the name of the song you want move:\n";
string s1;
cin.ignore();
label11:
getline(cin, s1);
if (collections.count(s1) == 0)
{
cout << "Please enter a valid song!" << endl;
goto label11;
}
label12:
cout << "Enter the song before or after you want to insert:\n";
string s2;
getline(cin, s2);
if (collections.count(s2) == 0)
{
cout << "Please enter a valid song!"
<< " " << s2 << endl;
goto label12;
}
dl->moveanywhere(s1, s2);
}
else
{
cout << "Please enter a valid key: " << endl;
goto label;
}
label1:
cin >> key;
system("CLS");
cout<<" _______ ___ _ __ __ _______ __ __ __ _ _______ _______\n";
cout<<" | || | | || | | || || | | || | | || || |\n";
cout<<" | _____|| |_| || |_| ||_ _|| | | || |_| || ___|| _____|\n";
cout<<" | |_____ | _|| | | | | |_| || || |___ | |_____ \n";
cout<<" |_____ || |_ |_ _| | | | || _ || ___||_____ |\n";
cout<<" _____| || _ | | | | | | || | | || |___ _____| |\n";
cout<<" |_______||___| |_| |___| |___| |_______||_| |__||_______||_______|\n"; cout << "Press 1 to pause/play the file \npress 2 to play the next song\npress 3 to play the previous song\npress 4 to exit the file.\npress 5 to display the playlist\npress 6 to add song at next position\npress 7 to add song anywhere" << endl;
cout << "Press 8 to play any song" << endl;
cout << "Press 9 to delete a song" << endl;
cout << "Press 10 to move song anywhere in the list" << endl;
if (key == "1")
{
if (!playing)
{
t1 = time(0);
dl->play_current();
playing = 1;
cout << " Audio file playing...\n\n";
play_music();
}
else
{
mciSendString("pause mp3", NULL, 0, NULL);
t2 = time(0);
playing = 0;
cout << "Audio file paused after " << t2 - t1 + previous_pause_time << " seconds.\n\n";
previous_pause_time += t2 - t1;
}
}
else if (key == "2")
{
mciSendString("close mp3", NULL, 0, NULL);
dl->next_song();
previous_pause_time = 0;
t1 = time(0);
playing = 1;
dl->play_current();
cout << " Audio file playing...\n\n";
play_music();
}
else if (key == "3")
{
mciSendString("close mp3", NULL, 0, NULL);
dl->previous_song();
t1 = time(0);
previous_pause_time = 0;
dl->play_current();
playing = 1;
cout << " Audio file playing...\n\n";
play_music();
}
else if (key == "4")
{
save_playlist();
exit(0);
}
else if (key == "5")
{
display_all();
}
else if (key == "6")
{
cout << "add path of the song:";
dl->insert_next();
}
else if (key == "7")
{
string si;
cin.ignore();
PATH8:
cout << "Enter name of song with respect you want to insert:";
getline(cin, si);
if (collections.find(si) == collections.end())
{
cout << "Enter a valid song name!" << endl;
goto PATH8;
}
dl->insert_anywhere(si);
}
else if (key == "8")
{
dl->goto_song();
}
else if (key == "9")
{
cout << "Enter the song name you want to delete: ";
string s;
cin.ignore();
PATH55:
getline(cin, s);
if(collections.find(s)==collections.end()){
cout<<"Please enter a valid name!\n";
goto PATH55;
}
dl->delete_song(s);
}
else if (key == "10")
{
cout << "Enter the name of the song you want move:\n";
string st1;
label111:
getline(cin, st1);
if (collections.count(st1) == 0)
{
cout << "Please enter a valid song!" << endl;
goto label111;
}
label121:
cout << "Enter the song before or after you want to insert:\n";
string st2;
getline(cin, st2);
if (collections.count(st2) == 0)
{
cout << "Please enter a valid song!"
<< " " << st2 << endl;
goto label121;
}
dl->moveanywhere(st1, st2);
}
else
{
cout << "Please enter a valid key: " << endl;
goto label1;
}
}
}