Banner of  Configure mpd, mpc, and ncmpcpp for Effortless Music Playback

The best configuration for mpd music player with ncmpcpp and mpc (2020)

Category: Linux

Date: September 2020
Views: 1.29K

mpd stands for music player daemon, it is a music player and as its name suggests it is a daemon, ie a process that runs in the background and you cannot interact with it directly.
the music player daemon needs a way to control it. comes ncmpcpp is a command line graphical interface built for mpd and highly configurable. with beautiful colors and features. you can control mpd with mpc - music player control. is a command line tool to control the music player from the command line via sockets.

In this article I give you the enitre configuration for mpd, mpc and ncmpcpp and my personal bash script to add more simplicity to using mpc to control the music player daemon via simple and short commands.

Installing mpd, mpc and ncmpcpp

it is extremely easy to install these 3 packages :

# debian based distributions:
$ sudo apt install mpd mpc ncmpcpp

# Arch based distributions:
$ sudo pacman -S mpd mpc ncmpcpp

Configuring mpd:

we create a ".mpd" directory anywhere we want, preferably in our home directly. then we create mpd.conf file and we put the following lines in it :

music_directory         "/home/myusername/path/to/My Music"
playlist_directory      "/home/myusername/.mpd/playlists"
db_file                 "/home/myusername/.mpd/mpd.db"
log_file                "/tmp/mpd.log"
pid_file                "/home/myusername/.mpd/"
state_file              "/home/myusername/.mpd/mpdstate"
restore_paused          "yes"
auto_update             "yes"
audio_output {
    type "pulse"
    name "pulse audio"
audio_output {
    type                 "fifo"
    name                 "my_fifo"
    path                 "/tmp/mpd.fifo"
    format               "44100:16:2"

bind_to_address          ""
port                     "6601"

the above config file will set the path to our music directory, and an optional yet very important path to our custom playlists directory. the other files will be created dynamically as the mpd runs.
While running in the background, mpd acts like a server running in our localhost and can be controlled from port "6601", we can choose any port we want as long as it is not used by some other application.
we execute mpd only once, onboot or when we want to play some music or in anyway we want. by issuing this command:

$ mpd /home/myusername/.mpd/mpd.conf

Configuring ncmpcpp:

Configuring ncmpcpp is just as easy as Configuring mpd, however it would take a lot of trial and error to get the perfect colors and layout of the user interface.
We begin by creating a ".ncmpcpp" directory anywhere we want, we may even name it something else entirely. the name is only to remind us that it is the directory for ncmpcpp . then we create a config file in it and put the following in it:

##### connection settings #####
mpd_host = "localhost"
mpd_port = "6601"
mpd_music_dir = "/home/myusername/Music/My Music"
lyrics_directory = "/home/myusername/Music/My Music/nmcpplyrics"
mpd_connection_timeout = "5"
mpd_crossfade_time = "1"

ncmpcpp_directory = "/home/myusername/.ncmpcpp"

##### delays #####
playlist_disable_highlight_delay = "1"
message_delay_time = "4"

##### window interface #####
user_interface = classic
header_visibility = yes
titles_visibility = no
cyclic_scrolling = yes
header_text_scrolling = yes
playlist_display_mode = "columns"
browser_playlist_prefix = "$2playlist$9 "
alternative_header_first_line_format = "{%t}"
alternative_header_second_line_format = "$7{%a} - $5{%b} $8{(%y)}"
song_window_title_format = "{%a - }{%t}|{%f}"
##### song list format #####
song_list_format = "{%a - %t}|{$8%f$9}{$3(%l)$9}"
song_library_format = "{%n - }{%t}|{%f}"
selected_item_prefix = "$6"
selected_item_suffix = "$9"
now_playing_prefix = "$b$r"
now_playing_suffix = "$/b$/r"
song_status_format = "{(%l) }{%a - }{%t}|{%f}"
playlist_shorten_total_times = yes
song_columns_list_format = "(30)[52]{a} (30)[4]{t} (7f)[88]{l}"

###### various settings #####
#incremental_seeking = yes
#seek_time = "1"
#autocenter_mode = yes
#centered_cursor = yes
#default_place_to_search_in = "database"
#default_find_mode = "wrapped"
#default_tag_editor_pattern = "%n - %t"
#header_visibility = yes
#header_text_scrolling = yes
#cyclic_scrolling = no
#statusbar_visibility = yes
#follow_now_playing_lyrics = yes
#clock_display_seconds = yes
#enable_window_title = yes
#user_interface = "alternative"
#progressbar_look = "=>"
progressbar_look = "─╼-"
seek_time = 10
#external_editor = "vim"
#use_console_editor = yes
#locked_screen_width_part = "60"

##### colors definitions #####
colors_enabled = yes
empty_tag_color = "52"
header_window_color = "52"
volume_color = "11"
state_line_color = "yellow"
state_flags_color = "11"
main_window_color = "52"
color1 = "white"
color2 = "green"
progressbar_color = "11"
progressbar_elapsed_color = "yellow"
statusbar_color = "52"
visualizer_color = "11"
window_border_color = "green"
active_window_border = "11"
alternative_ui_separator_color = "black"

#### visualizer ##############
visualizer_in_stereo = yes
visualizer_fifo_path = "/tmp/mpd.fifo"
visualizer_output_name = "my_fifo"
visualizer_sync_interval = "30"
visualizer_type = "spectrum"
visualizer_look = "◆▋"

######## more ######
external_editor = vim
use_console_editor = yes

The important part of that config file is the connection settings. Obviously they need to match mpd's config file: the host, the port and the music directory ... need to be the same. all the remaining configs are just to tweak the interface. the above config will produce the following interface:


mpc does not require any configuration. all we need is to provide the host and port in which mpd is running when we try to control it, the following command will toggle the music player play/pause from the terminal :

$ mpc --host= --port=6601  toggle

one can simply alias the above command to a more convenient one, or make a shell script to control mpd using mpc, and that's what I did in the following script:

function printhelp () {
  printf '\033[1;33m  %-15s\t\033[1;37m%s\n' "$1" "$2"
mympc=' mpc --host= --port=6601 '
case "$1" in
	"" )  $mympc -q  next  ;;
	"c")  ;;
	"s") if [ -z "$2" ]
		then $mympc -q  stop
		     for i do
			if (($ii!=0)) ; then query+="$i|" ; fi
		     "$0" ll |grep --color=always -i -E "$query"| sort -n | uniq
	     exit ;;
       "pp"|"m") if ! `$mympc |grep -F "playing">/dev/null`
		then $mympc -q play
		else $mympc -q pause
	"p") $mympc -q  prev ;;
	"n") $mympc -q  next ;;
	"v") $mympc   volume +2|awk 'NR==3{print $2}' ;;
	"vv") $mympc   volume -2|awk 'NR==3{print $2}' ;;
		printf "\t \033[1;32m my script to control mpd \033[1m\n"
		printhelp  "h,H,help" "print this help"
		printhelp  "c" "currently playing song"
		printhelp  "" "play next"
		printhelp  "pp,m" "play/pause"
		printhelp  "p" "previous"
		printhelp  "n" "next"
		printhelp  "v" "volume +2"
		printhelp  "vv" "volume -2"
		printhelp  "s" "stop"
		printhelp  "s   [pattern]" "search index of song(s) with pattern(s)"
		printhelp  "[number]" "play nth song"
		printhelp  "a   [patterns]" "add songs w/ patter(n) to curr playlist"
		printhelp  "ll" "print current playlists"
		printhelp  "d" "remove current song from playlist"
		printhelp  "ss  [patterns]" "print result of search pattern(s)"
		printhelp  "sl  [patterns]" "play songs with pattern(s)"
		printhelp  "ssl [patterns]" "print then play songs with pattern(s)"
		printhelp  "save [name]" "save current playlist"
		printhelp  "l   [playlist]" "load playlist"
		printhelp  "*" "other commands accepted by mpc"
	"a") $mympc search any "$2" | $mympc add  ;;
       "ll") index=$($mympc status -f "%position%" |head -1)
	     $mympc playlist|awk '{$0=substr($0,0,60) ; print $0 }'\
	       |nl| less +"$index"
	     ss=$( $mympc search  any "")
	     for i do
		if (($ii!=0)) ; then
		ss+=$( $mympc search  any "$i")
	     if [[ "$1" == "ss" || "$1" == "ssl" ]] ; then
		     printf "$ss" |grep --color=always -i -E "$query"| sort -n | uniq
	     if [[ "$1" == "sl" || "$1" == "ssl" ]] ; then
		     printf "$ss" |grep -i -E "$query" | sort -n | uniq >| "$HOME/.mpd/playlist/tmp.m3u"
		     "$0" l tmp|grep -v ^loading
		     rm "$HOME/.mpd/playlist/tmp.m3u"
	"save") $mympc rm "$2" 2>/dev/null; $mympc save "$2" ;;
	"l") $mympc -q clear
	     $mympc -q load $2
	     $mympc -q play ;;
	index=$($mympc status -f "%position%" |head -1)
	CURRENT="$( $mympc -f "%file%" playlist | sha512sum )"
	while read -r line
	    i="$( $mympc -f "%file%" playlist $line | sha512sum )"
	    if [ "$i" = "$CURRENT" ]; then
	done <<< "$( $mympc lsplaylist )"
     $mympc del 0
     $mympc rm "$line"
     $mympc save "$line"
     $mympc -q clear
     echo "removed from $line"
     $mympc -q load "$line"
     $mympc -q play $index
     if [[ $1 =~ $number ]]
     then $mympc -q play "$1"
     else $mympc  "$@"
     title=$($mympc status -f "%title%" |head -1)
     artist=$($mympc status -f "%artist%" |head -1)
     file=$($mympc status -f "%file%" |head -1)
     index=$($mympc status -f "%position%" |head -1)
     size=$($mympc status |awk 'NR==2{print $2}'|cut -d'/' -f2)
     stats=$( $mympc status |awk 'NR==2{printf("%s - %s",$2,$3) }' )
     if ! [[ -z "$artist" && -z "$title" ]]
     then str="$artist - ${title##*/}"
	  else str="${file##*/}"
	echo "$stats # $str"

the above script looks like :


We can search single or multiple words queries:



Next Article

0 Comments, latest

No comments.