-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic_player.c
249 lines (197 loc) · 5.42 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
#include "queue.h"
#include "dll.h"
#include "music_player.h"
#include<stdio.h>
#include<stdlib.h>
playlist_t *create_playlist() // return a newly created doubly linked list
{
// DO NOT MODIFY!!!
playlist_t *playlist = (playlist_t *)malloc(sizeof(playlist_t));
playlist->list = create_list();
playlist->num_songs = 0;
playlist->last_song = NULL;
return playlist;
}
void delete_playlist(playlist_t *playlist) // delete the playlist
{
// DO NOT MODIFY!!!
delete_list(playlist->list);
free(playlist);
}
music_queue_t *create_music_queue() // return a newly created queue
{
// DO NOT MODIFY!!!
return create_queue();
}
void clear_music_queue(music_queue_t *q) // clear the queue q
{
// DO NOT MODIFY!!!
delete_queue(q);
}
void add_song(playlist_t *playlist, int song_id, int where) // TODO: add a song id to the end of the playlist
{
if (where == -2){
insert_back(playlist->list, song_id);
playlist->num_songs++;
}
else if (where == -1){
insert_front(playlist->list, song_id);
playlist->num_songs++;
}
else if (where >= 0){
insert_after(playlist->list, song_id, where);
playlist->num_songs++;
}
}
void delete_song(playlist_t *playlist, int song_id) // TODO: remove song id from the playlist
{
delete_node(playlist->list, song_id);
playlist->num_songs--;
}
song_t *search_song(playlist_t *playlist, int song_id) // TODO: return a pointer to the node where the song id is present in the playlist
{
return (search(playlist->list, song_id));
}
void search_and_play(playlist_t *playlist, int song_id) // TODO: play the song with given song_id from the list(no need to bother about the queue. Call to this function should always play the given song and further calls to play_next and play_previous)
{
song_t *temp = search_song(playlist, song_id);
if (temp == NULL)
{
playlist->last_song = NULL;
return;
}
play_song(temp->data);
playlist->last_song = temp;
}
void play_next(playlist_t *playlist, music_queue_t *q) // TODO: play the next song in the linked list if the queue is empty. If the queue if not empty, play from the queue
{
if (empty(q))
{
if (playlist->last_song == NULL || playlist->last_song->next == NULL)
{
play_song(playlist->list->head->data);
playlist->last_song = playlist->list->head;
}
else
{
play_song(playlist->last_song->next->data);
playlist->last_song = playlist->last_song->next;
}
}
else
{
play_song(dequeue(q));
}
}
void play_previous(playlist_t *playlist) // TODO: play the previous song from the linked list
{
if (playlist->num_songs == 0)
return;
if (playlist->last_song == NULL || playlist->last_song->prev == NULL)
{
play_song(playlist->list->tail->data);
playlist->last_song = playlist->list->tail;
return;
}
play_song(playlist->last_song->prev->data);
playlist->last_song = playlist->last_song->prev;
}
void play_from_queue(music_queue_t *q) // TODO: play a song from the queue
{
play_song(q->front->data);
}
void insert_to_queue(music_queue_t *q, int song_id) // TODO: insert a song id to the queue
{
enqueue(q, song_id);
}
void reverse(playlist_t *playlist) // TODO: reverse the order of the songs in the given playlist. (Swap the nodes, not data)
{
song_t *temp;
song_t *curr = NULL;
song_t *first = playlist->list->head;
while (first != NULL)
{
temp = first->next;
first->prev = temp;
first->next = curr;
curr = first;
first = temp;
}
playlist->list->tail = playlist->list->head;
playlist->list->head = curr;
}
void k_swap(playlist_t *playlist, int k) // TODO: swap the node at position i with node at position i+k upto the point where i+k is less than the size of the linked list
{
int n = playlist->num_songs;
int i = 0;
song_t *temp = playlist->list->head;
song_t *cur = playlist->list->head;
for (i = 0; i + k < n; i++)
{
cur = temp;
for (int y = 0; y < k; y++)
{
cur = cur->next;
}
song_t *curn = cur->next;
song_t *curp = cur->prev;
song_t *tempn = temp->next;
song_t *tempp = temp->prev;
if (k == 1)
{
curp = cur;
tempn = temp;
}
cur->next = tempn;
cur->prev = tempp;
temp->next = curn;
temp->prev = curp;
if (tempn != NULL)
tempn->prev = cur;
if (curn != NULL)
curn->prev = temp;
if (curp != NULL)
curp->next = temp;
if (temp == playlist->list->head)
{
playlist->list->head = cur;
}
else
{
tempp->next = cur;
}
temp = tempn;
}
song_t *last = playlist->list->head;
song_t *slast = NULL;
while (last != NULL)
{
slast = last;
last = last->next;
}
playlist->list->tail = slast;
}
void shuffle(playlist_t *playlist, int k) // TODO: perform k_swap and reverse
{
reverse(playlist);
}
song_t *debug(playlist_t *playlist) // TODO: if the given linked list has a cycle, return the start of the cycle, else return null. Check cycles only in forward direction i.e with the next pointer. No need to check for cycles in the backward pointer.
{
song_t *i = playlist->list->head;
for (song_t *i = playlist->list->head; i->next != NULL; i = i->next)
{
if (i->next->prev != i)
return (i->next);
}
return (NULL);
}
void display_playlist(playlist_t *p) // Displays the playlist
{
// DO NOT MODIFY!!!
display_list(p->list);
}
void play_song(int song_id)
{
// DO NOT MODIFY!!!
printf("Playing: %d\n", song_id);
}