mpd-client

Interface to the Music Player Daemon.

  1. mpd-client
  2. Documentation
    1. Connecting to MPD and obtaining connection/server information
    2. Controlling playback
    3. Managing output devices
    4. Querying and modifying the current playlist
    5. Managing stored playlist
    6. Querying/Updating song database
  3. Examples
  4. About this egg
    1. Author
    2. Version history
    3. License

Documentation

This extension allows convenient access to the tcp interface of MPD.

Connecting to MPD and obtaining connection/server information

[procedure] (connect [HOST] [PORT] [PASSWORD])

Connects to the MPD instance running on HOST (defaults to localhost) at PORT (defaults to 6600). Optionally authenticates using PASSWORD. Returns an MPD connection object.

[procedure] (disconnect CONN)

Closes the connection to MPD. CONN should not be used anymore after calling this function.

[procedure] (mpd-connection? CONN)

Returns #t if CONN is an MPD connection object, #f otherwise.

[procedure] (mpd-host CONN)

Returns the hostname field of the MPD connection object CONN.

[procedure] (mpd-port CONN)

Returns the port field of the MPD connection object CONN.

[procedure] (mpd-password CONN)

Returns the password field of the MPD connection object CONN. This is either a string containing the plain text password or #f if no authentication should be done.

[procedure] (mpd-version CONN)

Returns the version field of the MPD connection object CONN which contains the version number received by the MPD server.

[procedure] (ping CONN)

Sends a ping command to the MPD server, reconnecting if necessary.

[procedure] (clear-error! CONN)

Clears the current error message (which is returned by (get-status)). This is also done by any command that starts playback.

[procedure] (get-stats CONN)

Returns an alist containing various stats:

#;6> (pp (get-stats m))
((artists . 183)
 (albums . 429)
 (songs . 6200)
 (uptime . 130260)
 (playtime . 49958)
 (db_playtime . 1773229)
 (db_update . 1152376960.0))
#;7>
[procedure] (get-status CONN)

Return an alist describing the current status of MPD:

#;9> (pp (get-status m))
((volume . 78)
 (repeat . #f)
 (random . #f)
 (playlist . 78)
 (playlistlength . 77)
 (xfade . 0)
 (state . play)
 (song . 12)
 (songid . 12)
 (time 31 623)
 (bitrate . 128)
 (audio 44100 16 2))
#;10>
[procedure] (shutdown-server! CONN)

Kill MPD.

[procedure] (get-commands CONN)

Returns a list of commands the current user has access to.

Controlling playback

[procedure] (play-song! CONN #!optional SONG TIME)

Start playing at song SONG (or the first in playlist) at TIME (in seconds).

[procedure] (next-song! CONN)

Play the next song in the playlist.

[procedure] (previous-song! CONN)

Play the previous song in the playlist.

[procedure] (stop! CONN)

Stop playback.

[procedure] (pause! CONN PAUSE?)

Pause if PAUSE? is true, resume playing otherwise.

[procedure] (set-options! CONN . OPTS)

Set playback options. Options are: #:crossfade, #:random, #:repeat and #:volume and they can be mapped to arguments.

Managing output devices

[procedure] (get-output-devices CONN)

Returns a list of alists describing the available output devices.

[procedure] (enable-output-device! CONN ID)

Enables the output device with id ID.

[procedure] (disable-output-device! CONN ID)

Disables the output device with id ID.

Querying and modifying the current playlist

[procedure] (get-playlist CONN [ID])

Return a list of alists describing the songs in the current playlist. (Optionally only the song with id ID):

#;10>  (pp (get-playlist m 12))
(((Id . 12)
  (Pos . 12)
  (Time . 623)
  (Title . "the leper affinity")
  (Track . 1)
  (Album . "blackwater park")
  (Artist . "opeth")
  (file . "metal/opeth/blackwater park/01 the leper affinity.mp3")))
#;11>
[procedure] (get-current-song CONN)

Returns an alist with information about the current song (the same information that (get-playlist) returns).

[procedure] (get-playlist-changes CONN VERSION)

Return a list of alists describing new songs since playlist version VERSION. Note: to detect songs that were deleted at the end of the playlist, use the playlistlength returned by (get-status).

[procedure] (add-song CONN PATH)

Adds PATH (a string naming a file or directory) to the end of the current playlist. Directories are added recursively. Increments playlist version by 1 for each added song.

[procedure] (move-song! CONN FROM TO)

Move song at position FROM to TO. The playlist version is incremented by 1.

[procedure] (swap-songs! CONN SONG1 SONG2)

Swap position of the two songs.

[procedure] (remove-song! CONN SONG)

Remove the song SONG (position or ID) from playlist. The playlist version is incremented by 1.

[procedure] (shuffle-playlist! CONN)

Shuffles the current playlist and increments the playlist version by 1.

[procedure] (clear-playlist! CONN)

Clears the current playlist, incrementing playlist version by 1.

Managing stored playlist

[procedure] (load-playlist! CONN PLAYLIST)

Loads the playlist named "PLAYLIST.m3u" from the playlist directory. The playlist version is incremented by the number of songs added.

[procedure] (remove-playlist! CONN PLAYLIST)

Removes the playlist named "PLAYLIST.m3u" from the playlist directory.

[procedure] (save-playlist! CONN PLAYLIST)

Saves the current playlist to "PLAYLIST.m3u" in the playlist directory.

Querying/Updating song database

[procedure] (find-songs CONN TYPE STRING)

Searches for songs and returns a list of alists. TYPE is e.g. 'title, 'album or 'artist. STRING is the search string (which must match exactly).

[procedure] (search-songs CONN TYPE SEARCHSTRING)

Returns a list of alists describing the matching songs. TYPE is e.g. 'title, 'album or 'artist. SEARCHSTRING is the string which is searched for (not case sensitive, doesn't need to be an exact match).

[procedure] (list-metadata CONN TYPE [LIMIT STRING])

Return a list of values of metadata TYPE (e.g. 'title, 'album or 'artist), optionally limited by LIMIT and STRING. E.g. (list-metadata m 'album 'artist "nevermore") to get a list of all albums by Nevermore.

[procedure] (list-directory/r CONN [PATH] [FULL #t])

Returns a list of all songs (below PATH, if given). If FULL is #t (default), list all metadata. If FULL is #f, return only filename metadata.

[procedure] (list-directory CONN [PATH])

Like (list-directory/r), with FULL as #t. The data is returned as a list of alists.

[procedure] (update-song-database! CONN [PATH])

Updates MPD's database, removing old songs and adding new songs. Optionally, the update process can be limited to PATH (a string naming a file or directory).

Examples

#!/usr/local/bin/csi -script

(require-extension mpd-client srfi-18 utils)

(define mpd (connect))

(define (get-mpd-status)
  (let ((status (get-status mpd)))
    (list (alist-ref 'songid status)
          (alist-ref 'state status))))

(define (get-songname id)
  (let ((info (car (get-playlist mpd id))))
    (or (alist-ref 'Title info)
        (pathname-strip-directory
         (alist-ref 'file info)))))

(let loop ((status (get-mpd-status)) (old-status #f))
  (unless (equal? status old-status)
    (let ((song (car status))
          (state (cadr status)))
      (cond
       ((eq? state 'play)
        (printf "Now playing: ~a\n"
                (if song
                    (get-songname song)
                    "(unknown song)")))

       ((eq? state 'stop)
        (printf "STOP\n")))))
  (thread-sleep! 1)
  (loop (get-mpd-status) status))

About this egg

Author

Hans Bulfone

Version history

1.12
Added regex as a dependency; converted eggdoc documentation to wiki format [Ivan Raikov]
1.1
Added new commands supported by recent versions of MPD
1.02
Fixed a bug that caused playlists to be returned incorrectly by mpd:ls/info et al.
1.01
Fixed typo in documentation
1.0
Initial release

License

Copyright (c) 2006-2007, Hans Bulfone
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the author nor the names of his contributors may
      be used to endorse or promote products derived from this software
      without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.