Skip to content

Commit 2270c06

Browse files
committed
Add genres field to songs
1 parent f120aae commit 2270c06

3 files changed

Lines changed: 53 additions & 0 deletions

File tree

README.org

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ The library is implemented around a set of entities.
4545
| song | structure | name, album, file, … | |
4646
| album | structure | name, artist | |
4747
| artist | structure | name | |
48+
| genre | structure | name | |
4849
| directory | structure | name, path | |
4950
| stored-playlist | structure | name | A named user-specified sequence of songs |
5051
| search-criteria | structure | type, what | Read the [[https://www.musicpd.org/doc/protocol/database.html][protocol documentation]] |
5152
| filter | structure | text | Read the [[https://www.musicpd.org/doc/html/protocol.html#filters][protocol documentation]] |
5253
|------------------+-----------+----------------------+---------------------------------------------------------|
5354
| artists | symbol | /none/ | Represent the set of all artists |
5455
| albums | symbol | /none/ | Represent the set of all albums |
56+
| genres | symbol | /none/ | Represent the set of all genres |
5557
| directories | symbol | /none/ | Represent all directories in ~libmpdel-music-directory~ |
5658
| current-playlist | symbol | /none/ | Represent the currently played sequence of songs |
5759
| stored-playlists | symbol | /none/ | Represent the set of all stored playlists |

libmpdel.el

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ message from the server.")
180180
(track nil :read-only t)
181181
(file nil :read-only t)
182182
(album nil :read-only t)
183+
(genres nil :read-only t)
183184
(disc nil :read-only t)
184185
(date nil :read-only t)
185186
(id nil :read-only t)
@@ -190,6 +191,11 @@ message from the server.")
190191
(:conc-name libmpdel--stored-playlist-))
191192
(name nil :read-only t))
192193

194+
(cl-defstruct (libmpdel-genre
195+
(:constructor libmpdel--genre-create)
196+
(:conc-name libmpdel--genre-))
197+
(name nil :read-only t))
198+
193199
(cl-defstruct (libmpdel-search-criteria
194200
(:constructor libmpdel-search-criteria-create)
195201
(:conc-name libmpdel--search-criteria-))
@@ -235,6 +241,14 @@ message from the server.")
235241
"Return SONG's album."
236242
(libmpdel--song-album song))
237243

244+
(cl-defmethod libmpdel-genres ((song libmpdel-song))
245+
"Return SONG's genres."
246+
(libmpdel--song-genres song))
247+
248+
(cl-defmethod libmpdel-genres ((genre libmpdel-genre))
249+
"Return singleton list GENRE."
250+
(list genre))
251+
238252
(cl-defgeneric libmpdel-entity-name (entity)
239253
"Return the name of ENTITY.")
240254

@@ -246,6 +260,10 @@ message from the server.")
246260
"Return ALBUM's name."
247261
(libmpdel--album-name album))
248262

263+
(cl-defmethod libmpdel-entity-name ((genre libmpdel-genre))
264+
"Return GENRE's name."
265+
(libmpdel--genre-name genre))
266+
249267
(cl-defmethod libmpdel-entity-name ((song libmpdel-song))
250268
"Return SONG's name.
251269
@@ -265,6 +283,10 @@ If the SONG's name is nil, return the filename instead."
265283
"Return a string describing the `albums' entity."
266284
"All albums")
267285

286+
(cl-defmethod libmpdel-entity-name ((_entity (eql genres)))
287+
"Return a string describing the `genres' entity."
288+
"All genres")
289+
268290
(cl-defmethod libmpdel-entity-name ((_entity (eql current-playlist)))
269291
"Return a string describing the `current-playlist' entity."
270292
"Current playlist")
@@ -295,6 +317,10 @@ If the SONG's name is nil, return the filename instead."
295317
"Return ALBUM's artist."
296318
(libmpdel-artist album))
297319

320+
(cl-defmethod libmpdel-entity-parent ((_genre libmpdel-genre))
321+
"Return the `genres' entity."
322+
'genres)
323+
298324
(cl-defmethod libmpdel-entity-parent ((_artist libmpdel-artist))
299325
"Return the `artists' entity."
300326
'artists)
@@ -339,12 +365,19 @@ If the SONG's name is nil, return the filename instead."
339365
(when (and (stringp pos) (not (string= pos "")))
340366
(string-to-number pos))))
341367

368+
(defun libmpdel--genres-create (genre-names)
369+
"Return a list of genres whose names are GENRE-NAMES."
370+
(mapcar (lambda (name)
371+
(libmpdel--genre-create :name name))
372+
genre-names))
373+
342374
(defun libmpdel--create-song-from-data (song-data)
343375
"Return a song from SONG-DATA, a server's response."
344376
(libmpdel--song-create
345377
:name (cdr (assq 'Title song-data))
346378
:track (cdr (assq 'Track song-data))
347379
:file (cdr (assq 'file song-data))
380+
:genres (libmpdel--genres-create (libmpdel-entries song-data 'Genre))
348381
:album (libmpdel--album-create
349382
:name (cdr (assq 'Album song-data))
350383
:artist (libmpdel--artist-create :name (cdr (assq 'Artist song-data))))
@@ -923,6 +956,11 @@ If HANDLER is nil, ignore response."
923956
(libmpdel-entity-to-criteria (libmpdel-artist album))
924957
(libmpdel-entity-name album)))
925958

959+
(cl-defmethod libmpdel-entity-to-criteria ((genre libmpdel-genre))
960+
"Return search query matching all songs from GENRE."
961+
(format "genre %S"
962+
(libmpdel-entity-name genre)))
963+
926964
(cl-defmethod libmpdel-entity-to-criteria ((song libmpdel-song))
927965
"Return search query matching SONG."
928966
(format "%s title %S"
@@ -959,6 +997,17 @@ If HANDLER is nil, ignore response."
959997
:artist libmpdel--unknown-artist))
960998
(libmpdel-sorted-entries data 'Album))))))
961999

1000+
(cl-defmethod libmpdel-list ((_entity (eql genres)) function)
1001+
"Call FUNCTION with all genres as parameter."
1002+
(libmpdel-send-command
1003+
"list genre"
1004+
(lambda (data)
1005+
(funcall function
1006+
(mapcar
1007+
(lambda (genre-name)
1008+
(libmpdel--genre-create :name genre-name))
1009+
(libmpdel-sorted-entries data 'Genre))))))
1010+
9621011
(cl-defmethod libmpdel-list ((_entity (eql stored-playlists)) function)
9631012
"Call FUNCTION with all stored playlists as parameter."
9641013
(libmpdel-send-command

test/libmpdel-test.el

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,11 @@
117117
'((Title . "The song")
118118
(file . "foo/song.ogg")
119119
(Album . "The Album")
120+
(Genre . "The Genre")
120121
(Artist . "The Artist")))))
121122
(should (equal "The song" (libmpdel-entity-name song)))
122123
(should (equal "foo/song.ogg" (libmpdel-song-file song)))
124+
(should (equal (list "The Genre") (mapcar #'libmpdel-entity-name (libmpdel-genres song))))
123125
(should (equal "The Album" (libmpdel-entity-name (libmpdel-album song))))
124126
(should (equal "The Artist" (libmpdel-entity-name (libmpdel-artist (libmpdel-album song)))))))
125127

0 commit comments

Comments
 (0)