Brother John’s Projects: Foobar2000 Scripting
Three things are the subject of this chapter and all of them concern displaying information about the currently playing song. First we build a script for the title bar of the Foobar2000 window, then we fill the status bar with some technical info and yet another playback indicator and finally we configure the display for the tooltip triggered by the Foobar2000 system tray icon.
When playback is stopped the title bar contains Foobar2000’s name and version number. This behaviour is fixed, but as soon as a song starts playing we can use Tagz to script the title bar. However this includes only single line text display, no colour scripts. Depending on your Windows Visual Style the final result will look similar to this:
The screenshot shows the maximum amount of info displayed: artist name, track title and album name. To type in the script go the Display / Title formatting page of the preferences and activate the Main window title tab.
[%artist%' - ']%title%[' ['%album%']']
Do I really have to explain the script...?
The status bar code is definitely more interesting as you can see from the screenshot below.
First go to the Display / Columns UI config page, choose the Status bar/Systray tab and change the Status bar font to Franklin Gothic Medium, 9pt or whatever you chose earlier for the playlist. Then go back to Display / Title formatting, activate the Status bar tab and delete the present script. Our own script displays information in five sections: codec, stream info, playback time, progress indicator, pause indicator. Let’s look at them one after the other.
$if($strcmp(%codec%,'ATSC A/52'),'Dolby Digital AC3', $if($info(lame_version),'Lame MP3', $if($strcmp(%codec%,'FLAC'),'FLAC lossless', $if($strcmp(%codec%,'PCM'),'PCM uncompressed', %codec% ))))
This is the codec part. Remember what we did when creating the codec info in the Artist/Album Column and the script should look familiar. Actually the first two lines about AC3 and Lame MP3 are identical, the following lines work according to the same principle: watching out for a specific codec and changing the displayed codec name. Of course you can extend this section in any way you like.
[%bitrate%' kbit/s'][' '%samplerate%' Hz'] $ifgreater(%channels%,2, ' '%channels%' channels', [' '%channels%])
The second section contains technical details about the audio stream. The first line displays bitrate and samplerate of the track, both only if present. The $ifgreater()
part first looks for files with more than two channels. A 5.1 track would be displayed as 6 channels. For anything below three channels we use the raw %channels%
field which, thanks to remapping, outputs mono for one channel and stereo for two channels anyway. The additional if-test (the square brackets) is necessary because above we tested only for more than two channels but not for the general existence of channel info. For MIDI and soundtracker files for example the usual concept of channels doesn’t apply and %channels%
is non-existent.
%_time_elapsed%[' of '%_time_total%]
In the third section we first display elapsed playback time and, if possible, remaining playback time. Again the brackets take care of the cases of files with un-determinable length.
%_Time_total%
exists only for the currently playing file and usually is equivalent to %_length%
. The former gets its value directly from Foobar2000’s playback routines and the latter from the media library/playlist. In some cases the numbers might be different, but that’s nothing to worry about too much.
Which brings us to the fourth section containing the progress bar. For clarity let’s look at the progress bar’s general syntax first:
$progress2(position,range,length,char1,char2)
Range
defines the 100 % mark and position
tells up to which position we have progressed. In simpler words this is the definition, up to which percentage the progress bar should be filled. Length
defines the length of the bar in characters and char1
and char2
are the characters used for the full and empty states respectively. The resulting script is this:
$progress2(%_time_elapsed_seconds%,%_time_total_seconds%, 10, $get(pbarColour1)$char(9679),$get(pbarColour2)$char(9679) )
The two arguments %_time_elapsed_seconds%
and %_time_total_seconds%
give the percentage up to which the song is played and the progress bar shall be filled. The bar is 10
characters long and the third line defines the characters. Actually they are identical: $char(9679)
, which is a large round bullet. Additionally we define a different colour for each of the characters, resulting in red bullets for the filled state and grey bullets for the empty state; we’ll define the variables for the colours in a minute. Provided with all this information the $progress2()
function takes care of drawing the progress bar. Also a different version called $progress()
is available, taking the same arguments but resulting in a different look.
$if(%_time_total_seconds%
,
$progress2(%_time_elapsed_seconds%,%_time_total_seconds%,
10,
$get(pbarColour1)$char(9679),$get(pbarColour2)$char(9679)
)
$rgb()' '
$muldiv(%_time_elapsed_seconds%,100,%_time_total_seconds%)
' %'
)
This is the complete progress bar script. The $if(%_time_total_seconds%,,)
is nothing special anymore, once again preventing errors on files with no determinable length. The part after the $progress2()
construction displays the elapsed playback time as a percentage after the progress bar. First we need to switch back to default text colour with $rgb()
, otherwise the progress bar’s red or grey would still be active. Then we use $muldiv(x,y,z)
to calculate the actual percentage number. $Muldiv()
takes its first argument x
, multiplies it by y
and divides the result by z
. The function actually is a shortcut for using a nested $div($mul(x,y),z)
. Last step is to display the percent sign. And we move on to the fifth and final section.
$if(%_ispaused%,$char(9)'pause {on} ')
The %_ispaused%
field works like %isplaying%
but indicates a paused song. If playback is paused, we display pause {on}. The $char(9)
inserts a tabulator character moving the string to the right side of the status bar.
Now that we have all the bits and pieces we need to combine them into a final script and adding two more things: First some separator character between the sections, which in our case will be two simple spaces; second red text colour for the tech info part (second section) to distinguish the sections even further. Below is the finished script.
$puts(AltColour,$rgb(126,16,16)) $puts(pbarColour1,$rgb(126,16,16)) $puts(pbarColour2,$rgb(170,170,170)) $if($strcmp(%codec%,'ATSC A/52'),'Dolby Digital AC3', $if($info(lame_version),'Lame MP3', $if($strcmp(%codec%,'FLAC'),'FLAC lossless', $if($strcmp(%codec%,'PCM'),'PCM uncompressed', %codec% )))) $get(AltColour)' ' [%bitrate%' kbit/s'][' '%samplerate%' Hz'] $ifgreater(%channels%,2, ' '%channels%' channels', [' '%channels%]) $rgb()' ' %_time_elapsed%[' of '%_time_total%] ' ' // progress bar $if(%_time_total_seconds% , $progress2(%_time_elapsed_seconds%,%_time_total_seconds%, 10, $get(pbarColour1)$char(9679),$get(pbarColour2)$char(9679) ) $rgb()' ' $muldiv(%_time_elapsed_seconds%,100,%_time_total_seconds%) ' %' ) // paused status $if(%_ispaused%,$char(9)'pause {on} ')
Why use the $puts()
at the beginning? Well, defining variables for the colours up there and accessing them further down makes editing the colours easier as we won’t have to scan through the entire script for all $rgb()
functions. The most logical and consistent thing to do would be defining the variables together with all the others on Columns UI’s Globals tab. Unfortunately the Display / Title formatting page is not part of Columns UI but Foobar2000’s core. Accessing Columns UI’s variables from here simply is not possible.
One last script concerns the tooltip popping up when moving the mouse over Foobar2000’s icon in the system tray.
The script goes into the Notification area icon tooltip tab.
[%artist%' - ']%title% [$crlf()%album%][' #'%tracknumber%]
This is the complete script displaying artist and title on one line and if applicable album and tracknumber info in a second line. The $crlf()
function inserts the line break. You might have noticed that I decided against using the hexadecimal tracknumber here. While it looked good in the context of the whole album, it was kind of confusing here. So I settled on a simple # followed by the tracknumber.
This concludes Dark Connections, Stage 4. The look is not too different from stage 3, but here’s a screenshot anyway.
For the first time now we have a complete design. If you never deal with incomplete albums, you can very well skip the next chapter.