-
Notifications
You must be signed in to change notification settings - Fork 0
/
music_player.c
600 lines (503 loc) · 13.9 KB
/
music_player.c
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
#include<stdio.h>
#include<windows.h>//don't break this approch
#include<mmsystem.h>//use -lwinmm linker with gcc
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<conio.h>
#define N "\x1b[0m"
#define X "\x1b[5m"
#define R "\x1b[31m"
#define G "\x1b[32m"
#define B "\x1b[34m"
#define BY "\x1b[43m"
void sub();
void print(char*);
void ex(char*);
void plyStart(char*);
void plyStop();
int check();
void storeToFile();
void loadFromFile();
void registerUser();
void displayUsers();
void loginUser();
void insert();
void deleteElement();
void show();
void nextPathly();
void prePly();
void firstPathly();
void lastPathly();
void specifictitle();
void backupPlaylist(char*);
void restorePlaylist(char*);
void sub2(char*);
struct User{
char uName[25];
char pass[25];
struct User* next;
};
struct Ply{
char title[50];
char path[70];
struct Ply* next;
struct Ply* pre;
};
struct User* head = NULL;
char tTitle[50],tPath[70];
struct Ply* head2 = NULL;
struct Ply* c=NULL;
void print(char *s)
{
int c_delay=50,s_delay=100,n_delay=150;
for (int i = 0; s[i] != '\0'; i++)
{
printf("%c", s[i]);
fflush(stdout);
if (s[i] == ' ')
{
usleep(s_delay * 1000);
}
else if (s[i] == '\n')
{
usleep(n_delay * 1000);
}
else
{
usleep(c_delay * 1000);
}
}
}
void plyStart(char* filename) {
char command[100];
sprintf(command, "open %s type mpegvideo alias mp3", filename);
mciSendString(command, NULL, 0, NULL);
mciSendString("play mp3", NULL, 0, NULL);
}
void plyStop() {
mciSendString("stop mp3", NULL, 0, NULL);
mciSendString("close mp3", NULL, 0, NULL);
}
int check() {
char status[256];
mciSendString("status mp3 mode", status, sizeof(status), NULL);
if (strstr(status, "playing") != NULL) {
return 1;
} else {
return 0;
}
}
void storeToFile() {
FILE* file = fopen("catch.dat", "w");
if (file != NULL) {
struct User* current = head;
while (current != NULL) {
fprintf(file, "%s %s\n", current->uName, current->pass);
current = current->next;
}
fclose(file);
}
}
void loadFromFile() {
FILE* file = fopen("catch.dat", "r");
if (file != NULL) {
char uName[25], pass[25];
while (fscanf(file, "%s %s", uName, pass) == 2) {
struct User* newUser = (struct User*)malloc(sizeof(struct User));
if (newUser == NULL) {
printf("Memory allocation failed.\n");
fclose(file);
return;
}
strcpy(newUser->uName, uName);
strcpy(newUser->pass, pass);
newUser->next = NULL;
newUser->next = head;
head = newUser;
}
fclose(file);
}
}
void registerUser() {
char uName[25], pass[25], cPass[25];
print("Enter username ? ");
scanf("%s", uName);
struct User* current = head;
while (current != NULL) {
if (strcmp(current->uName, uName) == 0) {
printf(X R BY"Username already exists. Please choose a different username."N B BY);
getch();
return;
}
current = current->next;
}
print("Enter password ? ");
scanf("%s", pass);
print("Confirm password ? ");
scanf("%s", cPass);
if (strcmp(pass, cPass) != 0) {
printf(X R BY"Password and confirm password do not match. Registration failed."N B BY);
system(X R BY"pause"N B BY);
return;
}
struct User* newUser = (struct User*)malloc(sizeof(struct User));
if (newUser == NULL) {
printf(X R BY"Memory allocation failed.\n"N B BY);
getch();
return;
}
strcpy(newUser->uName, uName);
strcpy(newUser->pass, pass);
newUser->next = NULL;
newUser->next = head;
head = newUser;
FILE* file = fopen(uName, "w");
fclose(file);
print(X G"Registration successfully."N B BY);
getch();
}
void displayUsers() {
struct User* current = head;
print("\nRegistered Users:\n");
while (current != NULL) {
printf("Username: %s\n", current->uName);
current = current->next;
}
printf("\n");
print(X G"Press enter to continue..."N B BY);
getch();
}
void loginUser() {
char uName[25], pass[25];
print("Enter username ? ");
scanf("%s", uName);
print("Enter password ? ");
scanf("%s", pass);
struct User* current = head;
while (current != NULL) {
if (strcmp(current->uName, uName) == 0 && strcmp(current->pass, pass) == 0) {
printf(X G"Login successful."N B BY);
getch();
sub2(uName);
}
current = current->next;
}
printf(X R BY"Invalid username or password. Login failed."N B BY);
getch();
}
void insert() {
print("Enter Music Name ? ");
while ((getchar()) != '\n');
scanf("%[^\n]%*c", tTitle);
struct Ply *check_ptr = head2;
while (check_ptr != NULL) {
if (strcmp(check_ptr->title, tTitle) == 0) {
printf(X R BY"Music with the same title already exists. Please choose a different title."N B BY);
getch();
return;
}
check_ptr = check_ptr->next;
}
struct Ply *new_Ply = (struct Ply *)malloc(sizeof(struct Ply));
strcpy(new_Ply->title, tTitle);
print('Enter specific (full) .mp3 path in Double-quotas [exmaple : "C:\windows32\Yash\Desktop\New folder\beep.mp3" ] ? ');
scanf("%[^\n]%*c", new_Ply->path);
if (head2 == NULL) {
new_Ply->next = new_Ply->pre = new_Ply;
head2 = c = new_Ply;
} else {
struct Ply *last = head2->pre;
new_Ply->pre = last;
last->next = new_Ply;
new_Ply->next = head2;
head2->pre = new_Ply;
}
print(X G"Song added..."N B BY);
getch();
}
void deleteElement() {
if (head2 == NULL) {
printf(X R BY"No Music is there to delete!"N B BY);
getch();
return;
}
print("Enter Music Name to delete ? \n");
while ((getchar()) != '\n')
;
scanf("%[^\n]%*c", tTitle);
struct Ply *ptr = head2;
do {
if (ptr->next == ptr && strcmp(ptr->title, tTitle) == 0) {
printf(X G"One file deleted! Playlist is Empty Now!"N B BY);
head2 = NULL;
free(ptr);
getch();
return;
} else if (strcmp(ptr->title, tTitle) == 0) {
struct Ply *pre = ptr->pre;
struct Ply *next = ptr->next;
pre->next = next;
next->pre = pre;
if (ptr == c) {
c = next;
}
if (ptr == head2) {
head2 = next;
}
free(ptr);
printf(X G"Music deleted!"N B BY);
getch();
return;
}
ptr = ptr->next;
} while (ptr != head2);
printf("No Music file is there!\n");
}
void show() {
if (head2 == NULL) {
printf(X R BY"Playlist is Empty!"N B BY);
getch();
return;
}
struct Ply *show_ptr = head2;
int i = 1;
printf("\nDisplaying Playlist:\n");
do {
printf("Song %d : %s\n", i, show_ptr->title);
i++;
show_ptr = show_ptr->next;
} while (show_ptr != head2);
getch();
}
void nextPathly() {
if (c == NULL) {
printf(X R BY"No songs in Playlist!"N B BY);
getch();
return;
}
c = c->next;
if (c != NULL) {
printf(X G"Playing Next Song: %s"N BY B, c->title);
plyStop();
plyStart(c->path);
}
getch();
}
void prePly() {
if (c == NULL) {
printf(X R BY"No songs in Playlist!"N B BY);
getch();
return;
}
c = c->pre;
if (c != NULL) {
printf(X G"Playing preious Song: %s"N BY B, c->title);
plyStop();
plyStart(c->path);
}
getch();
}
void firstPathly() {
if (head2 == NULL) {
printf(X R BY"Playlist is Empty!"N B BY);
getch();
return;
}
printf(X G"Playing First Music: %s"N BY B, head2->title);
if (check() == 0) {
plyStop();
plyStart(head2->path);
}
getch();
}
void lastPathly() {
if (head2 == NULL) {
printf(X R BY"Playlist is Empty!"N B BY);
getch();
return;
}
printf("Playing Last Music: %s\n", head2->pre->title);
if (check() == 0) {
plyStop();
plyStart(head2->pre->path);
}
getch();
}
void specifictitle() {
if (head2 == NULL) {
printf(X R BY"No music is there to be searched!"N B BY);
getch();
return;
}
printf("Enter Music Name to search:\n");
while ((getchar()) != '\n')
;
scanf("%[^\n]%*c", tTitle);
struct Ply *ptr = head2;
do {
if (strcmp(ptr->title, tTitle) == 0) {
print("Music Found!\n");
printf(X G"Playing Music: %s"N B BY, ptr->title);
if (check() == 0) {
plyStop();
plyStart(ptr->path);
}
getch();
return;
}
ptr = ptr->next;
} while (ptr != head2);
printf("\n"X R BY"There is no Music file with this name!"N B BY);
getch();
}
void backupPlaylist(char* uName) {
FILE *file = fopen(uName, "w");
struct Ply *ptr = head2;
if (head2 != NULL) {
do {
fprintf(file, "%s|%s\n", ptr->title, ptr->path);
ptr = ptr->next;
} while (ptr != head2);
}
fclose(file);
}
void restorePlaylist(char* uName) {
FILE *file = fopen(uName, "r");
char line[100];
while (fgets(line, sizeof(line), file) != NULL) {
struct Ply *new_Ply = (struct Ply *)malloc(sizeof(struct Ply));
sscanf(line, "%[^|]|%[^\n]", new_Ply->title, new_Ply->path);
if (head2 == NULL) {
new_Ply->next = new_Ply->pre = new_Ply;
head2 = c = new_Ply;
} else {
struct Ply *last = head2->pre;
new_Ply->pre = last;
last->next = new_Ply;
new_Ply->next = head2;
head2->pre = new_Ply;
}
}
fclose(file);
}
void sub2(char* uName){
int choice;
storeToFile();
restorePlaylist(uName);
do {
system("cls");
printf("-------------------------------------------\n");
printf(" Welcome,%s\n",uName);
printf("-------------------------------------------\n");
printf("\n###### User panel ######\n");
printf("1. Add Music\n");
printf("2. Remove Music\n");
printf("3. Show Playlist\n");
printf("4. Play next file\n");
printf("5. Play previous file\n");
printf("6. Play first file\n");
printf("7. Play Last file\n");
printf("8. Play specific file\n");
printf("9. Exit\n");
printf("0. Pause any music\n\n");
print("Enter your choice ? ");
scanf("%d", &choice);
switch (choice) {
case 1:
insert();
backupPlaylist(uName);
break;
case 2:
deleteElement();
backupPlaylist(uName);
break;
case 3:
show();
break;
case 4:
nextPathly();
break;
case 5:
prePly();
break;
case 6:
firstPathly();
break;
case 7:
lastPathly();
break;
case 8:
specifictitle();
break;
case 9:
ex(uName);
break;
case 0:
plyStop();
break;
default:
printf(X R BY"Invalid choice. Please try again."N B BY);
getch();
}
} while (1);
}
void sub(){
int c;
do{
system("cls");
printf("\n##############################################");
printf("\n############ Welcome ################");
printf("\n############ *to music plyer ################");
printf("\n##############################################");
printf("\n1.Register\n2.Login\n3.Show users\n4.Exit\n\n");
print("Enter your choice ? ");
scanf("%d",&c);
switch (c){
case 1:registerUser();
break;
case 2:loginUser();
break;
case 3:displayUsers();
system(X G"pause"N B BY);
break;
case 4:
ex(NULL);
break;
default:
printf(X R BY"Invalid choice. Please try again.\n"N B BY);
getch();
}
}while(1);
}
void ex(char* uName) {
char c;
do {
system("cls");
printf(R BY"Dear %s, do you really want to exit (y / n) ? "N B BY, uName);
scanf(" %c", &c); // Note the space before %c to consume any leading whitespace
switch (c) {
case 'n':
case 'N':
return;
break;
case 'y':
case 'Y':
break;
default:
printf(X R BY"Invalid choice. Please try again.\n"N B BY);
getch();
}
} while (c != 'y' && c != 'Y');
print(" Thnk Uhh! for using our software... ");
storeToFile();
backupPlaylist(uName);
getch();
exit(0);
}
int main(){
printf(B BY);
loadFromFile();
sub();
return 0;
}