Friday, January 25, 2013

Squeeze Me Lite


Latest update: 16-Aug-2017 

 

I'd like to introduce you to a my favorite audio application - a squeezebox streaming client - called squeezelite.

squeezelite is a versatile, highly efficient squeezebox emulator that runs on all PC platforms, micro computers, like a Raspberry Pi,  even on routers or NAS and also on the Squeezebox Touch .  Even commercial streamers make use of it.

It's opensource and it's free. And it's IMO been and still is a major enabler for streaming quality audio at home.  





Squeezelite was originally written and maintained by Adrian Smith also known 
as Triode.  Adrian Smith, the person, who also contributed the EDO plugin 
(USB support) fothe Squeezebox Touch and numerous other great SW enhancements. 
From one day to another Adrian disappeared a couple of years back for (to me) not known reasons. 
Since then Ralph Irving, also known as "Ralphy", maintains squeezelite. Several community
members are involved in evolving and enhancing squeezelite.

squeezelite is a so-called headless client or some might also called it audio-deamon. 
Headless because it acts like a system-service or daemon in the background of your OS.
There's no graphical user interface.  It acts pretty much the same way as MusicPlayerDaemon (MPD). MPD is a standalone audio solution though. And  comes with numerous limitations in comparison to a squeezelite/Logitechmediaserver setup.

squeezelite requires the LogitechMediaServer (LMS) - a nowadays pretty much 
community maintained media server - to work.
LMS and squeezelite can be installed on the same machine building a standalone playback environment or separately building a typical client/server environment. 


You can control squeezelite and the server via numerous squeezebox control applications for PCs, Android, iOS or just straight from any webbrowser. 
At startup squeezelite tries to figure out all audio and DAc relevant parameters automatically.  The only thing you actually do is configuring the output device of choice.

The audio traffic will be routed to your audio interface of choice at up to "32/384khz"! 
Your DAC (or OS driver) sets the limits. 

DSD/DOP support can be enabled through a DSD-plugin on the server - also written by Adrian.
 
squeezelite you'll find pretty much on all platforms - OSX/WIN/Linux and many different processor architectures (even on your Open-Wrt router or Raspberry PI devices it'll work).

 
On Windows there's no ASIO, Kernel Streaming or WASAPI exclusive support at this stage of the project. 
I wouldn't consider Windows or OSX the preferred platforms for squeezelite anyhow.

Let's concentrate on the Raspberry PI (which is still my preferred micro client) and squeezelite.

There are several projects out there offering squeezelite as an audio service

To name a few:

*  PiCorePlayer 
*  Moode Audio 
*  Diet Pi
* ......

Note: Volumio supports MPD (music player daemon) only!

These projects basically take a basic OS - usually based on Raspbian or Arch Linux Arm -, install squeezelite, create a web interface to be able to control the environment and put 
the image on the net. 
Some of them introduce more, some less audio related optimizations or extra features. 
In case of questions or need for support the community and the maintainers are happy to help.


As a user with a I2S-HAT or USB DAC at hand, you basically buy a Raspberry PI, 
copy the provided .iso-image on a SD-card, plug it in, boot your PI up and you're 
almost ready to go. 

You won't get around doing some minor configurations to get your own 
environment going. 

These projects and related efforts must be highly appreciated. 
With these setups you can compete with streaming solutions as provided 
by commercial companies - at a much lower cost and a much higher flexibility.


Just to mention it: 

iOS and Droid tablets and phones can already be used as squeezeboxes through commercial apps like iPeng-player and squeezeplayer. 
These apps are basic squeezebox emulators and usually address a slightly different target group then squeezelite.
You'll find these apps for a couple of bucks in the known app stores or as in-app purchases.
If you own a tablet or phone you should consider to use it as streaming client. 
These might even do better than a "basic" Raspberry Pi installation.

Bottom line.



Nowadays, we basically can make every linux based coffeemaker in the house part of the Squeeze Streaming Universe at very little efforts. There's no need to spent thousands of $ on commercial streaming clients.



With squeezelite at hand there's no need to have a Logitech Squeezebox or any other commercial streaming hardware anymore. 


 
Adrian and Ralphy did and still do a great job in providing such a package to us.

From my perspective this software wasn't just an evolution, no it was an revolutionary 
step forward.



Great stuff.    Enjoy.    I do.


Stay tuned. 




#######################################################################

Annex 1:

A basic squeezelite installation on Ubuntu 

(duration: approx.  20 minutes on top of Ubuntu  installation)
 
Let's assume you've got an Ubuntu server 16.10 
You'll get very recent kernel!!! 
It's key to have latest Alsa, drivers and networking in place. 



Let's get started.


What's needed are roughly 10 commands on command-line to get it working.

What you do is following: 



#################

1. Login as user X.


2. we do everything as superuser (root) from now on


sudo su
cd /usr/local/bin

3. let's install some apps, libraries, tools  and a lowlatency kernel first


apt-get -y install alsa-base alsa-tools alsa-utils alsa-firmware-loaders
apt-get -y install flac sox libsoxr0 libsoxr-lsr0 linux-lowlatency
apt-get -y install build-essential git wget patch checkinstall  libasound2-dev libmad0-dev
apt-get -y install libflac-dev  libogg-dev libvorbis-dev libfaad-dev libmpg123-dev libsoxr-dev


NOTE: I highly recommend to install most recent Ubuntu kernels !
             if you don't use the latest Ubuntu.
            You'll find instructions about it on the net. For now you can skip it.
            Unless your audio device is not recognized or you experience dropouts
            you won't get around a newest kernel anyhow.


4.  now we fetch the squeezelite sources.

git clone https://github.com/ralph-irving/squeezelite.git
cd squeezelite
          make
         cp ./squeezelite /usr/local/bin


5. now we make the binary executable.

chmod 777 /usr/local/bin/squeezelite

6. now we can test if squeezelite is working.

squeezelite -h

    All squeezelite commandline options should be listed.


7. now we backup a file that we use for autostarting squeezelite at boot.

cp /etc/rc.local /etc/rc.local.orig

8. find out about your soundcards and the index linux uses for each of them.
    we do not use the device IDs as listed by squeezelite.
    if your soundcard does not show up you have a problem!

cat /proc/asound/cards

     let's assume your output looks similar to this:

     me@sl:~$ cat /proc/asound/cards


      0 [UD501          ]: USB-Audio - TEAC UD-501
                          TEAC Corporation TEAC UD-501 at usb-0000:00:1a.0-1.3, high speed
 

    now we know that our soundcard is recognized and got index 0 assigned.
    the first column in above printout shows a 0. That reflects its index.

9. now we've to prepare the squeezelite command string that goes into /etc/rc.local
    below string should do. In my case it even worked, if run in parallel on the LMS server
    
    My base squeezelite setup: 
    squeezelite runs at realtime-prio 95, the output thread at 97, 
    SL streams bitperfect straight to output hw:0,0 and shows up as yabber1


    The fastest way to get the string into /etc/rc.local, type

echo -e "chrt -f -p 44 /usr/local/bin/squeezelite -o hw:0,0 -p 45 -n sl1 -W  -z\n$(cat /etc/rc.local | grep -v "^#" | grep -v "^$")" >/etc/rc.local
  
     check the result, by typing

cat /etc/rc.local

      as first line of the printout you'd find our commandline:

      chrt -f 44 /usr/local/bin/squeezelite -o hw:0,0 -p 45 -n sl1 -W -z 

      If not, something went wrong! Make sure you get it fixed first before continuing.


      If you're familiar with commandline editors such as nano/vi/vim or similar, you 
      could also use one of those to add above command to /etc/rc.local.

      You can edit /etc/rc.local by running

      nano /etc/rc.local
      to apply any changes.


10. if all looks ok, you can execute rc.local now. Just enter:

/etc/rc.local 

     NO error messages at this point ??? Great. Almost done.
     Check if the squeezelite process is running:

ps -edf | grep squeezelite

11.  now reboot the machine, to activate your new low-latency kernel.

sudo shutdown -r now

       Login again. Check what priorities are assigned to SL AND its threads
       and the new kernel:

ps -eo pid,pri,rtprio,cmd  -L  | grep squeezelite
uname -a

       you'll see the playback thread of squeezelite running @ rtprio 97 and all other
       threads @ rtprio 95.

       Hint:
       you properly shutdown a linux machine by running!!!!


sudo shutdown -h now

12.   To restore the system pretty close to its initial state, just run:


sudo cp /etc/rc.local.orig  /etc/rc.local
sudo apt-get remove low-latency 
sudo shutdown -r 0


13. TRAPS:

      Usually you can't avoid falling into this or that trap.

      1. soundcard not recognised: newer kernel/driver required

      2. no sound even if process is running: alsamixer volume control low

      3. /etc/rc.local messup: restore to original
   



Good Luck, Enjoy


#######################################################################
Annexx 3:
 
A wrapper script for Linux  - called "squeeze".

To make life easier, it's good to (automatically) start the squeezelite binary with all its options by a wrapper script

Below you'll find a little guideline. It'll need some minor adjustments from your side.


This exercise is really optional.


Open a Terminal.

cd ~
touch squeeze
nano squeeze

Copy/paste below code into the editor. You need to fill in appropriate parameter in the PARAMETER section below and save it.


#!/bin/sh
#
#
# squeezlite wrapper script
# written by Klaus Schulz
#
# Rev 1: 01/28/2013
#
Rev 2: 09/09/2013
#
###PARAMETER##############################################################################################

LOCALETHERNETPORT=ethX          ## run "ifconfig" to find out the active ethernet port, e.g. eth0, wlan0 or similar
 
AUDIOIF="hw:0,0"                              ## audiointerface as listed by squeezelite -l, or cat /proc/asound/cards
                                                           ## standard alsa naming conventions can be used. E.g. hw:0,0 or plughw:1,0

## below parameters are optional! To activate or disable minimal command line or  extened command line,
## insert/remove a "#" on respective lines in the code

SERVERIP="127.0.0.1"                      ## (optional) This IP is used if squeezelite runs on the server, otherwise put "#" in front of line
#SERVERIP="XXX.XXX.XXX.XXX"    ## (optional )server IP on remote client, run ifconfig to find out, if run on server put "#" in front of line
PLAYERNAME=sl1                             ## (optional-default squeezelite) choose whatever name you want to show up on the controller

MAXSAMPLERATE=96000                ## (optional) This sets maximum samplerate as supported by the local DAC - up 2 384000
ALSABUFFER=32                            ## (optional-default 20000) Alsa outputbuffer in milliseconds
PERIODS=4                                       ## (optional-default 4) Alsa buffer devided into number of periods
BIT=24                                             ## (optional-detected by SL) bit depth of output 16, 32, 24_3 = 3 bytes, 24 = 4 bytes
STREAMINGBUFFERIN=2048           ## (optional-default=2048) streaming buffer input kilobytes

STREAMINGBUFFEROUT=2048       ## (optional-default=3446) streaming buffer output in  kilobytes
MMAP=1                                            ## (optional-default=1) Alsa memory management off/on  0/1
RTPRIO=70                                        ## (optional) realtimepriority of squeezelite

CODEC=pcm                                      ## (optional-default all codecs (flac,pcm... see squeezlite -h) are loaded)

###MAIN#####################################################################################################

BIN="$(which squeezelite)"

[ -f "$BIN" ] || { echo "squeezelite binary not found" ; exit 1 ; } ;

LOCALMAC="$(cat /sys/class/net/$LOCALETHERNETPORT/address)"

[ "$( pgrep squeezelite )" != "" ] &&  killall -9 squeezelite


sleep 1   

## minimal command line - put hash in fronmt of next line, if you use 2nd command line
$BIN -m "$LOCALMAC" -o "$AUDIOIF" -z

## extended command line -- use that if you use script on Touch - remove hashes from next two lines.
#killall jive; sleep1
#chrt -f $RTPRIO $BIN -m "$LOCALMAC" -o "$AUDIOIF" -n "$PLAYERNAME" -c  $CODEC -a "$ALSABUFFER:$PERIODS:$BIT:$MMAP" -b "$STREAMINGBUFFERIN:$STREAMINGBUFFEROUT" -r $MAXSAMPLERATE -z $SERVERIP

exit 0

############################################################################################################ 
#END########################################################################################################
 

Then you copy the script to /usr/local/bin 

sudo cp squeeze /usr/local/bin
 
and make it executable

sudo chmod 777 /usr/local/bin/squeeze

Now you can start it:

squeeze  

Type

ps -edf | grep squeeze

You should see the squeezelite process printed. 
Now check your prefered remote, if you can find the new player id listed. 

Done. 

For automatic start at boot you might add ( above "exit" )  to /etc/rc.local


sudo nano /etc/rc.local

add

/usr/local/bin/squeeze &

above exit 0

save and reboot.


NOTE :  Low-Latency resp. Realtime kernels.

             You might have realized that I lift the process into realtime spheres when
             starting it up.
             That requires an rt-kernel or lowlatency kernel as supplied by Ubuntu Studio.
             Over here you'll find some useful information how to set such kernel up. 
             Keep in mind that those preempted kernels are meant for single-purpose
             machines. These kernels are not recommended for desktop machines or

             servers that run all kind of server duties.

             squeezelite lifts the output thread up to sched_fifo (kernel scheduler) if you
             run a so called preemptible kernel. squeezelite applies priority 45 internally. 
  
 


########################################################################
####END
########################################################################

2 comments:

  1. Is it possible to publish a tutorial on how to install squeezelite on a SB Touch? I haven't got a clou about where to start.

    ReplyDelete
  2. Copy that! I have been searching for a tutorial or description how to install squeezelite on a touch... cant find it :-(
    Would be very much appreciated!

    ReplyDelete