As of 2016-02-26, there will be no more posts for this blog. s/blog/pba/
Showing posts with label MPD. Show all posts

Just finished some changes for showing song title and artist of music which played by (S)MPlayer in my dzen.

The modified lf-submit.sh (lf-mplayer-wrapper.sh) doesnt extract time information because it will have to parse MPlayers output, though its not hard, but I just didnt want to do it. Beside, the script couldnt decide the song length, because I might set A-B playing mode. Basically, only song title and artist are available to display and thats what my dzen used to show me the songs played by MPD.

Except the time and players status, my dzen shows me same information as if showing MPD status, including Last.fm playcount and loved status. lf-submit.sh helps save the current song information to /tmp/lf-submit.sh.currentsong and my status.c pick up and show them, then lf-playcount-image.sh helps retrieve data from Last.fm.

I dont feel I really need song to be shown in dzen when I play a music video, you can just watch pictures, why do you want to read text? Anyway, it just got me something to do.

If you have watched the screencast above, you may notice that there are some glitches when rolling the text after 050. I knew someday I would have to fix that, problems with characters other than ASCII. Now, I really have to, but later.

Last few days, I have been watching German music video. I found there are some awesome videos and some funny ones. I will post later hopefully.

Oh, sorry for the last 1 minute and 30 second, seeing me fixing that exclamation mark thing!

I have a Bash script which connects to MPD server and display current song and also accepts some keys to control the player. I have stripped some functions from it a few times when I needed to write a Bash script to do something with MPD, e.g. showing Last.fm playcount in Conky. A few times, I wanted to know what metadata of MPD command currentsong provides, I quick hacked the script and made it printed out the returned data.

I think its time to write a simple client to accept command and print out the response. Stripping that Bash script was my first thought, then I decided to write a Python script, instead. Its not because I prefer Python or something, just I hadnt written a single line Python code for a long time.

Before I showed you that Python code, there is one more thing I just found out. A really really simple client for MPD, which is telnet tool.

$ telnet 0 6600
Trying 0.0.0.0...
Connected to 0.
Escape character is '^]'.
OK MPD 0.15.0
currentsong
file: A/Alexandria Maillot/Just Another Girl/Just Another Girl.mp3
Time: 224
Artist: Alexandria Maillot
AlbumArtist: Alexandria Maillot
Title: Just Another Girl
Album: Just Another Girl
Track: 1/4
Genre: Pop
Composer: Alexandria Maillot and Joby Baker
Pos: 1
Id: 159
OK
stats
artists: 56
albums: 56
songs: 392
uptime: 124
playtime: 54
db_playtime: 84859
db_update: 1291807454
OK
pasus
ACK [5@0] {} unknown command "pasus"
pause
OK
play
OK
close
Connection closed by foreign host.

You dont really need mpc if you are a script nut with nc (netcat) or expect. If you dont need to process the response, you can always give commands to MPD, e.g.

echo "next" | telnet 0 6600

This will make MPD to play next song.

Now the Python code:

#!/usr/bin/env python

try:
  import readline
except ImportError:
  pass
import socket
import sys


HOST = 'localhost'
PORT = 6600
RECV_SIZE = 2**20 # One megabyte
try:
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  print 'Connecting to %s:%d...' % (HOST, PORT)
  s.connect((HOST, PORT))
except socket.error:
  print 'Unable to connect.'
  sys.exit(1)

print s.recv(RECV_SIZE)
print '*** <Control+C> to exit ***\n'

while True:
  try:
    cmd = raw_input(">>> ")
    if cmd:
      s.send(cmd + '\n')
      if 'close' in cmd:
        break
      print s.recv(RECV_SIZE)
  except KeyboardInterrupt:
    break

print
s.close()

A sample interaction:

% ./smpdc.py
Connecting to localhost:6600...
OK MPD 0.15.0

*** <Control+C> to exit ***

>>> currentsong
file: A/Alexandria Maillot/Just Another Girl/Just Another Girl.mp3
Time: 224
Artist: Alexandria Maillot
AlbumArtist: Alexandria Maillot
Title: Just Another Girl
Album: Just Another Girl
Track: 1/4
Genre: Pop
Composer: Alexandria Maillot and Joby Baker
Pos: 1
Id: 159
OK

>>> playlist
0:A/A Lion Named Roar/Shoot It Straight.mp3
1:A/Alexandria Maillot/Just Another Girl/Just Another Girl.mp3
2:A/Ana Free/Keep On Walking.mp3
3:A/Ana Free/Questions In My Mind.mp3
4:S/Stars/Stars_Dead_Hearts.mp3
OK

>>> next
OK

>>> currentsong
file: A/Ana Free/Questions In My Mind.mp3
Time: 231
Artist: Ana Free
AlbumArtist: Ana Free
Title: Questions In My Mind
Album: Radian
Track: 1/5
Date: 2010
Genre: Pop Rock
Composer: Ana Gomes Ferreira and Blake Harris Brandes
Pos: 3
Id: 161
OK

>>> close

Just finished this hours-project. I happened to read this thread and know the FIGlet and TOIlet, hours later, I am showing you MPDisp. I used FIGlet not TOIlet because TOIlet doesn't support center aligning. Here is a screenshot:

MPDisp

You can read and download the code. The only dependency should be FIGlet (do I really need to say you need MPD?). It doesn't rely on (nc)mpc(pp), it contacts MPD directly. All it does is show the song information and give you a little bit of control, see the key list:

  • p - Pause/Resume
  • Enter - Play
  • s - Stop
  • n - Next song
  • p - Previous song
  • r - Repeat mode on/off
  • S - Single mode on/off
  • R - Random mode on/off
  • q - Quit
  • Q - Quit with MPD
I was thinking to use TOIlet, it has a feature called filter, which can render with colors. But it can't align text center. I might try to see what I can do to next.

The only problem I have seen is if you have too small window, text will be messed up, and I am not intended to solve it right now.