-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMusicPlayer.cpp
212 lines (161 loc) · 4.79 KB
/
MusicPlayer.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
//
// Created by Christopher Scherban on 11/3/17.
//
#include<iostream>
#include <algorithm> // std::sort
#include <boost/algorithm/string.hpp> //to split some strings
#include "MusicPlayer.h"
#include "InvalidSearchException.h"
using namespace boost::filesystem;
#include <boost/algorithm/string.hpp>
/**
*
* @param p is the path of the current directory
*/
void MusicPlayer::iterate_through_directory(path &p){
path _temp;
for(directory_iterator dir(p); dir!=directory_iterator(); dir++){
_temp = path(*dir);
if(is_directory(_temp)){
iterate_through_directory(_temp);
}
else if(extension(_temp) == ".mp3"){ //checks for a specific extension
struct Song s;
s.name = _temp.stem().string();
s.path = _temp.string();
songs.push_back(s);
}
}
}
/**
* Constructor for MusicPlayer
* @param Dir
*
*
* WHEN YOU GET HOME IMPLEMENT THE THINGY WHERE YOU USE SPLIT TO GET ALBUM NAME AND ARTIST NAME FROM SONG;
*/
MusicPlayer::MusicPlayer(std::string Dir){
path p(Dir.c_str());
iterate_through_directory(p);
std::sort(songs.begin(),songs.end(),[](Song a,Song b){ return a.path.compare(b.path); }); //soring lambda
int i = 0;
std::string next_album, next_artist;
while(i<songs.size()){
struct Artist artist;
artist.name = extract_artist(songs[i].path);
next_artist = artist.name;
while(i<songs.size() && artist.name == next_artist ){
struct Album album;
album.name = extract_album(songs[i].path);
next_album = album.name;
while(i < songs.size() && (album.name == next_album && artist.name == next_artist)){
album.songs.push_back(songs[i]);
if(++i < songs.size()){
next_album = extract_album(songs[i].path);
next_artist = extract_artist(songs[i].path);
}
}
artist.albums.push_back(album);
}
artists.push_back(artist);
}
/**
If Stuff isnt working, try looking at the output.
Make sure the file structure is Artist/Album/Song for all members of the dirrectory
This will only look at the innermost two subdirrectories contained in the dirrectory
And treat them as Artist and Album.
for(Artist a: artists){
for(Album b: a.albums){
for(Song s : b.songs){
std::cout<<a.name<<"\t "<<b.name<<"\t"<<s.name<<std::endl;
}
}
}
*/
}
/**
* does exacly as advertised
* Stops all the playing of music.
*/
void MusicPlayer::stop(){
system("killall afplay");
}
std::string MusicPlayer::extract_artist(std::string path){
//std::cout<<"Hello: "<<path<<std::endl;
std::vector<std::string> result;
boost::split(result, path,boost::is_any_of("/") );
return result[result.size() - 3];
//return " ";
}
std::string MusicPlayer::extract_album(std::string path){
std::vector<std::string> result;
boost::split(result, path,boost::is_any_of("/") );
return result[result.size() - 2];
}
/**
* Plays all of an artist
* @param a
*/
void MusicPlayer::playArtist(Artist a){
std::cout<<std::endl<<"Hey";
for(int i = 0; i < a.albums.size(); i++){
playAlbum(a.albums[i]);
}
}
/**
*
* @param Album a, which is to be played
*/
void MusicPlayer::playAlbum(Album a){
for(int i = 0; i < a.songs.size(); i++){
playSong(a.songs[i]);
}
}
/**
* Plays a song
* this will only work on a Mac
*
* @param s pretty obvious isn't it?
*/
void MusicPlayer::playSong(Song s){
system(("afplay " + s.path).c_str());
}
/**
*
* @param querry : The name of the song you are looking for
* @return The song desired or null if nothign is found
*/
MusicPlayer::Song MusicPlayer::findSong(std::string querry){
for(Song s: songs){
if(querry == s.name){
return s;
}
}
throw InvalidSearchException("The Song you searched for does not exist", querry.c_str());
}
/**
*
* @param querry , the name of the album you are searching for
* @return the album you wanted, or null if nothign is found
*/
MusicPlayer::Album MusicPlayer::findAlbum(std::string querry){
for(Album a: albums){
if(querry == a.name){
return a;
}
}
throw InvalidSearchException("The Album you searched for does not exist", querry.c_str());
}
/**
*
* @param querry the name of the artist you are wishing to find
* @return the Artist that you want to find, or null if nothign is found
*/
MusicPlayer::Artist MusicPlayer::findArtist(std::string querry){
for(Artist a: artists){
if(querry == a.name){
return a;
}
}
throw InvalidSearchException("The Artist you searched for does not exist", querry.c_str());
}