Thursday, June 4, 2009

A bit about music sorting.

As man, who likes to listen music as background of my working, I encountered a problem with getting/sorting new music. Old good music is still good, but it still old....

I can't just listen every track before adding it to my playlist as this can really take forever. (:

As background of this story, imagine i have server with a lot of music of different styles/groups etc, but i don't know which group will be good for me or which music is totally crap from my POV.

Some time ago i encountered information about "union" filesystems, particulary funionfs, and this gave the direction for my subconsciousness to work on problem of sorting music. And this is solution i finally came to.

As i'm using the mocp player, there is no problem to obtain currently playing track information from script. This was the base.

Setup step 1, creating music sorting playground:

Using funionfs establishing "out" directory (entry from /etc/fstab):

funionfs#/home/elk/.music_sort/tmp   \
/home/elk/.music_sort/out \
fuse \
rw,allow_other,dirs=/home/elk/music=ro 0 0


So, after this, we will have "out" in /home/elk/.music_sort/out, where player should search for music and play music from. /home/elk/music is directory, where music is actually located (and where is symlink to server music).

So, now, if someone will delete some file in /home/elk/.music_sort/out, file will not be actually deleted, but will be marked as deleted in /home/elk/.music_sort/tmp.

But with deleting not all so easy, as i encountered some problems with "deleting" files on server. Server is cifs share mounted and in /home/elk/music is symbolic link to place, where this music is located, and any unification fs i have tried expirienced different troubles with
deleting in such circumstances.

So, instead of deleting files via funionfs, script creates deleted-markers in /home/elk/.music_sort/tmp.

Setup step 2, script to do "deleting":


#!/bin/bash

# this part obtains music file information
FILENAME=`mocp -i | grep -o "File:.*" | sed 's/File: //'`
TRACKNAME=`mocp -i | grep -o "SongTitle:.*" | sed 's/SongTitle: //'`
AUTHOR=`mocp -i | grep -o "Artist:.*" | sed 's/Artist: //'`

SORTDIR='/home/elk/.music_sort/out'

# this part produces dialog
POSITION="-G +950+28"

DIRNAME=`dirname "$FILENAME"`

# if file not in music directory, we should not be able to delete it, as
# this can lead to some unexpected behaviour

if [ "$SORTDIR" != "${FILENAME:0:25}" ] ; then
export MAIN_DIALOG="
<vbox>
<frame Track>
<text>
<label>\"$AUTHOR - $TRACKNAME\"</label>
</text>
</frame>
<frame File>
<text>
<label>\"$FILENAME\"</label>
</text>
</frame>
<frame Directory>
<text>
<label>\"$DIRNAME\"</label>
</text>
</frame>

<frame Can not be deleted>
<text>
<label>Not in music sorting dir, so can not be deleted.</label>
</text>
</frame>
<button ok></button>
</vbox>"


gtkdialog $POSITION --program MAIN_DIALOG
exit 1
fi

export MAIN_DIALOG="
<vbox>
<frame Track>
<text>
<label>\"$AUTHOR - $TRACKNAME\"</label>
</text>
</frame>
<frame File>
<text>
<label>\"$FILENAME\"</label>
</text>
</frame>
<frame Directory>
<text>
<label>\"$DIRNAME\"</label>
</text>
</frame>

<frame Actions:>
<hbox>
<button>
<label> Delete track </label>
<action type=\"exit\">track</action>
</button>
<button>
<label> Delete dir </label>
<action type=\"exit\">dir</action>
</button>
<button cancel></button>
</hbox>
</frame>
</vbox>
"


eval `gtkdialog $POSITION --program MAIN_DIALOG`
echo exit: $EXIT

# as format for deleting marker for funionfs is filename_DELETED~
# we should create this file instead of dir or file

if [ "$EXIT" == "track" ] ; then
DELETED_FN='/home/elk/.music_sort/tmp'/"${FILENAME:26}"_DELETED~
mkdir -p "`dirname "$DELETED_FN"`"
touch "$DELETED_FN"
mocp -f
elif [ "$EXIT" == "dir" ] ; then
DELETED_FN=`dirname '/home/elk/.music_sort/tmp'/"${FILENAME:26}"`_DELETED~
mkdir -p "`dirname "$DELETED_FN"`"
touch "$DELETED_FN"
mocp -f
fi


There is some comments in script, so there should be no problems to understand it.

So, finally, i added a button on toolbar, which calls this script.

So, looks like this:



So now every time as I want to never hear again track, which is played by now, i press button on toolbar, this dialog appears and i click to "delete dir" or "delete track".

Music is not actually deleted, but my player will not see it anymore.

No comments: