# 将树莓派打造成音乐播放服务器

- URL: https://wuming.si/blog/687/
- Author: Aaron
- Published: 2014-07-15
- Language: zh-Hans
- Categories: Uncategorized
- Originally published at: http://blog.wo.ai/archives/687

几个月前，我发布了一篇文章《我对探寻终极音乐播放器的探寻》（[My Quest to Build the Ultimate Music Player](http://andrewkelley.me/post/quest-build-ultimate-music-player.html)）,在那篇文章里面描述了我对一款开源的音乐播放服务器[Groove Basin](http://groovebasin.com/)断断续续将近4年的痛苦折腾过程。

它的客户端基于web页面，如下图：

[![groovebasin-screenshot](http://blog.wo.ai/wp-content/plugins/wp-o-matic/cache/626f65b73a_db5622a15b1f8a5efa9f5b4958483a761.png)](http://blog.wo.ai/wp-content/plugins/wp-o-matic/cache/626f65b73a_db5622a15b1f8a5efa9f5b4958483a761.png "将树莓派打造成音乐播放服务器")

你也可以在这里尝试它的[在线demo版本](http://demo.groovebasin.com/)。

如果你把它安装在树莓派里面，你可以把speakers附加上去，使用它你可以远程控制，也可以通过在浏览器中的点击“stream”就可以远程收听自己的音乐。

然而，在鼓捣这个之前，我需要指出的是，如果你在决定是否入手一台树莓派，答案是，不要。树莓派被过度夸大了，其实还有更好的选择。这就是为什么我写这篇教程。在这里我要提到一个，就是[Beagle Bone Black ](http://beagleboard.org/Products/BeagleBone+Black)。

你可以用这个来代替的原因如下：

  * 更快的cpu和内存 
    * 1GHz处理器，而非700MHz
    * DDR3内存，而非SDRAM
  * 它可以直接运行Debian和Ubuntu armhf，而非运行类似Respbian的系统。在Debian已经有armhf端口的情况下仍然存在Raspbian简直就是愚蠢的行为。如果你只是安装普通的armhf Ubuntu在Beagle Bone Black上，这篇文章的剩余部分就没有必要再看下去了，你只需 
    
        # apt-add-repository ppa:andrewrk/libgroove
    # apt-get update
    # apt-get install libgroove-dev

然后你就已经做完了。

实际上，libgroove在[Debian Testing](http://packages.qa.debian.org/libg/libgroove.html)和[Ubuntu Utopic Unicorn](https://launchpad.net/ubuntu/utopic/+source/libgroove)两个版本上面已经存在了，所以一年半载之内这些发行版升级了，你也无需添加额外的个人软件包档案

  * Debian[官方不推荐树莓派](https://wiki.debian.org/RaspberryPi)，因为它在运行时候需要非自由软件。Debian特别推荐了Beagle Bone Black。

如果你像我一样，同时在你收到更好建议之前不幸的买了一台树莓派，你需要跳过很多坑来让它运行起来。这篇文章就会手把手的交给你怎么绕过所有的坑，而无需自己浪费时间去自己费劲琢磨。

这篇文章开始时会假设你手上有一台全新的甚至还未装操作系统的树莓派。如果你已经做过了这一步，可以直接跳到[下一部分](http://www.geekfan.net#install-groove-basin)。

## 目录

  1. [目录](http://www.geekfan.net#table-of-contents)
  2. [安装Raspbian并获取SSH连接](http://www.geekfan.net#install-raspbian)
  3. [安装Groove Basin](http://www.geekfan.net#install-groove-basin)

## 安装Raspbian并获取SSH连接

转到树莓派[下载页面](http://www.raspberrypi.org/downloads/)，下载Raspbian Debian Wheezy种子（或者直接下载，如果你不是大虾的话）

解压压缩包，得到img文件后将其刷入你有的最大的SD卡上面。因为你需要很大的空间存放音乐！

我是在Ubuntu上面，我所需要做的仅仅是在文件管理器中右击img文件， open with， Disk Image Writer：

[![disk-image-writer](http://blog.wo.ai/wp-content/plugins/wp-o-matic/cache/43804b25fb_88c28ec51ebee80d9a94ea614c1ab1951.png)](http://blog.wo.ai/wp-content/plugins/wp-o-matic/cache/43804b25fb_88c28ec51ebee80d9a94ea614c1ab1951.png "将树莓派打造成音乐播放服务器")

我确信做这一步有很多种方式，我使用的这种方式只是对我来说最简单的。

这步完成后，找一个键盘、显示器和HDMI线，你就可以看到你正在做的事情了。我们的目标是可以尽快的使用上SSH连接，从而使树莓派可以除了电源线和网线之外什么都不用插就可以工作。

树莓派第一次启动后它会给您一个清单，包含你可以做的事情。下面是我做的事情：

  * Expand the file system to fit the full SD card.
  * Set pi user password.
  * Advanced Options, enable SSH server.

现在我们将其设置为每次启动时候都有固定ip。下面是我使用的设置，你可以按照自己的需要照猫画虎：
    
    
    sudo vi /etc/network/interfaces

将`iface eth0 inet dhcp`代替为：
    
    
    auto eth0
    iface eth0 inet static
        address 192.168.1.99
        netmask 255.255.255.0
        gateway 192.168.1.1

现在我们可以拔除TV和键盘了，我们已经无须这些废物了。直接插入网线然后开机吧！

在你现在用的电脑上面你可以用ssh连接到树莓派上面了，就像这样：
    
    
    ssh pi@192.1.68.1.99

我喜欢在我的`~/.ssh/config`文件中添加传送门，就像这样：
    
    
    host pi
    hostname 192.168.1.99
    user pi

它需要你输入密码，但我们也可以解决：
    
    
    ssh-copy-id pi

现在连接树莓派已经可以ssh pi这么简单了。

你需要做的第一件事就是更新所有的过时的软件到最新版：
    
    
    $ sudo apt-get update
    $ sudo apt-get dist-upgrade

额，我在这里看到了什么？
    
    
    ...
    Unpacking replacement raspberrypi-bootloader
    ...

Bootloader都被代替了？最好重启一下看看他是不是还能用。

好了，这个时候我们还是可以通过ssh连接上我们的树莓派，所有的软件包也完全更新了。

## 安装Groove Basin

首先安装一些软件包：
    
    
    $ sudo apt-get install htop vim git cmake screen

建议你在做这个的时候在screen或者tmux这类环境下，因为一旦你的网络断了，我们的命令还可以继续。这也让我们可以在树莓派运行时候去做点其他的事情。

我需要一次解释清楚如何执行这一步。然而，记住有3个重要的编译需要用好长时间，所以如果你想同时开始这3个任务并且离开电脑8个小时或更久，你可以跳过这个教程并开始同时执行它们了。这3个软件的make步骤为：

  * SDL2
  * libav
  * Node.js

libgroove,第一部分

得到libgroove源代码，在源码里面创建build文件夹：
    
    
    $ cd
    $ git clone https://github.com/andrewrk/libgroove
    $ cd libgroove
    $ mkdir build
    $ cd build

我们在debug模式build，这样在有任何错误的时候就可以得到有用的堆栈信息。

记住，你可以跳过下面的步骤，它仅花一两分钟时间就可以完成，这些命令仅供参考，我也已经在下面贴出了在我机器的输出：
    
    
    $ cmake .. -DCMAKE_BUILD_TYPE=Debug
    
    
    Installation Summary
    ——————–
    * Install Directory : /usr/local
    * Build libgroove : missing dependencies
    * Build libgrooveplayer : missing dependencies
    * Build libgrooveloudness : missing dependencies
    * Build libgroovefingerprinter : yes
    
    Bundled Dependencies
    ——————–
    * SDL2 : ready to build
    * libav : missing dependencies, see below
    * libebur128 : ready to build
    
    System Dependencies
    ——————-
    * C99 Compiler : OK
    * threads : OK
    * SDL2 : not found – will use bundled version
    * ebur128 : not found – will use bundled version
    * chromaprint : not found
    * libavformat : not found – will use bundled version
    * libavcodec : not found – will use bundled version
    * libavfilter : not found – will use bundled version
    * libavutil : not found – will use bundled version
    * yasm : not found
    * bzip2 : not found
    * mp3lame : not found
    * zlib : OK

信息显示缺失了以下库：

  * chromaprint
  * libebur128
  * SDL2
  * libav

我们可以让安装libgroove自动解决依赖问题，但是在系统上面事先安装这些依赖的软件包更简单。我们就这么做。

### chromaprint

幸运的是，chromaprint已经在软件仓库里面了：
    
    
    $ sudo apt-get install libchromaprint-dev

### libebur128

下一步我们编译最简单的一个，libebur128.
    
    
    $ cd
    $ git clone https://github.com/jiixyj/libebur128
    $ cd libebur128
    $ mkdir build
    $ cd build
    $ cmake .. -DCMAKE_BUILD_TYPE=Debug

矮油，好像我们缺少了一些依赖软件：
    
    
    – checking for module ’speexdsp’
    – package ’speexdsp’ not found

最好安装这个：
    
    
    $ sudo apt-get install libspeexdsp-dev

重新试试那一行配置行：
    
    
    $ cmake .. -DCMAKE_BUILD_TYPE=Debug
    
    
    – checking for module ’speexdsp’
    – found speexdsp, version 1.2rc1
    – speexdsp library dirs:
    – speexdsp cflags:
    – speexdsp include dirs:
    – speexdsp libraries: speexdsp
    – speexdsp ldflags:
    – status found / disabled –
    – queue.h: yes using system copy of queue.h
    – speexdsp: yes no
    – not building tests, set ENABLE_TESTS to ON to enable
    – Configuring done
    – Generating done
    – Build files have been written to: /home/pi/libebur128/build

这样好多了。

现在编译并安装源码：
    
    
    $ make
    $ sudo make install
    
    
    [ 50%] Built target ebur128
    [100%] Built target ebur128_static
    Install the project…
    – Install configuration: ”Debug”
    – Up-to-date: /usr/local/include/ebur128.h
    – Installing: /usr/local/lib/arm-linux-gnueabihf/libebur128.so.1.0.1
    – Installing: /usr/local/lib/arm-linux-gnueabihf/libebur128.so.1
    – Installing: /usr/local/lib/arm-linux-gnueabihf/libebur128.so
    – Installing: /usr/local/lib/arm-linux-gnueabihf/libebur128.a

由于Debian cmake包中的一个[bug](https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=739876i)，它把库文件放在`/usr/local/lib/arm-linux-gnueabihf/`

我们来改一下：
    
    
    $ sudo mv /usr/local/lib/arm-linux-gnueabihf/* /usr/local/lib/
    $ sudo rmdir /usr/local/lib/arm-linux-gnueabihf
    $ sudo ldconfig

### SDL2

接下来我们让SDL2跑起来。树莓派自带的SDL有什么问题？是因为它自带的是1.2版本，而libgroove是基于版本2的。

在[SDK下载页面](https://www.libsdl.org/download-2.0.php)找到最新的SDL2的源码URL：
    
    
    $ cd
    $ wget https://www.libsdl.org/release/SDL2-2.0.3.tar.gz
    $ tar xvf SDL2-2.0.3.tar.gz
    $ cd SDL2-2.0.3/

SDL需要音频后端才可以工作，现在我们在配置命令之前先安装它：
    
    
    $ sudo apt-get install libasound2-dev

我们只需要SDK2的音频特性，而且实际上，它的视频的一些功能还会带有编译的问题。所以我们在configure的时候禁止掉所有我们不需要的功能：
    
    
    $ ./configure –enable-audio –disable-video –disable-render –disable-events –disable-joystick –disable-haptic –disable-power –disable-file –disable-timers –disable-loadso –disable-cpuinfo

这步要费一些时间，但是在完成后，你可以看到这样的一行：
    
    
    Audio drivers : disk dummy oss alsa(dynamic)

这很重要，你除了有声卡驱动外还有disk、dummy和oss
    
    
    $ make

找点事情做，这一步要个把小时才可以完成。或者如果你想让你的树莓派投降，你可以跳过本教程，同时编译libav。但是我要假设你正在按顺序做这些，而且让你自己来决定如何跳过这篇文章。

这个时候，冗长的编译过程终于成功了，我们可以安装SDL了：
    
    
    $ sudo make install

### libav

我们回到home文件夹（或者我们决定安装的目录）：
    
    
    $ cd

从[libav下载页面](http://www.libav.org/download.html)（http://www.libav.org/download.html）得到libav最新的版本10的URL：
    
    
    $ wget http://www.libav.org/releases/libav-10.1.tar.gz
    $ tar xvf libav-10.1.tar.gz
    $ cd libav-10.1/

先做些预备工作，然后开始configure：
    
    
    $ sudo apt-get install libmp3lame-dev libvorbis-dev
    $ ./configure –enable-shared –enable-debug –disable-static –enable-gpl –enable-libmp3lame –enable-libvorbis

同样，树莓派还是会非常困难的完成configure命令，特别是你同时也在编译SDL2，所以如果它用了一两分钟，不要着急
    
    
    $ make

这个过程要长达8个小时。认真的说，我会开始这个过程后直接去睡觉。如果你希望同时开始所有的编译，你可能会同时开始Node.js的编译。

libav编译成功后：
    
    
    $ sudo make install

我们终于完成了libgroove的依赖软件的安装，我们终于可以开始安装libgroove本身了。

### libgroove，第二部分

这个时候，你已经等了很长时间了，树莓派也已经编译libav和SDL2成功，你也已经安装了它们。如果这些还没做完，你需要搞明白为什么，然后在接下来这个教程开始之前将其修复。
    
    
    $ sudo ldconfig
    $ cd ~/libgroove/build/
    $ cmake .. -DCMAKE_BUILD_TYPE=Debug
    
    
    Installation Summary
    ——————–
    * Install Directory : /usr/local
    * Build libgroove : yes
    * Build libgrooveplayer : yes
    * Build libgrooveloudness : yes
    * Build libgroovefingerprinter : yes
    
    Bundled Dependencies
    ——————–
    * SDL2 : using system library
    * libav : using system libraries
    * libebur128 : using system library
    
    System Dependencies
    ——————-
    * C99 Compiler : OK
    * threads : OK
    * SDL2 : OK
    * ebur128 : OK
    * chromaprint : OK
    * libavformat : OK
    * libavcodec : OK
    * libavfilter : OK
    * libavutil : OK

这个输出比前面的好看多了。
    
    
    $ make

这个make相对而言应该快多了。
    
    
    $ sudo make install
    $ sudo ldconfig

这个时候我们已经把必要的库安装好了：
    
    
    $ ls /usr/local/lib/
    
    
    libavcodec.so libebur128.a libgroove.so.4
    libavcodec.so.55 libebur128.so libgroove.so.4.1.0
    libavcodec.so.55.34.1 libebur128.so.1 libSDL2-2.0.so.0
    libavdevice.so libebur128.so.1.0.1 libSDL2-2.0.so.0.2.1
    libavdevice.so.54 libgroove.a libSDL2.a
    libavdevice.so.54.0.0 libgroovefingerprinter.a libSDL2.la
    libavfilter.so libgroovefingerprinter.so libSDL2main.a
    libavfilter.so.4 libgroovefingerprinter.so.4 libSDL2.so
    libavfilter.so.4.2.0 libgroovefingerprinter.so.4.1.0 libSDL2_test.a
    libavformat.so libgrooveloudness.a libswscale.so
    libavformat.so.55 libgrooveloudness.so libswscale.so.2
    libavformat.so.55.12.0 libgrooveloudness.so.4 libswscale.so.2.1.2
    libavresample.so libgrooveloudness.so.4.1.0 pkgconfig
    libavresample.so.1 libgrooveplayer.a python2.7
    libavresample.so.1.1.0 libgrooveplayer.so python3.2
    libavutil.so libgrooveplayer.so.4 site_ruby
    libavutil.so.53 libgrooveplayer.so.4.1.0
    libavutil.so.53.3.0 libgroove.so

### Node.js

我们现在需要Node.js。从它的下载页面（http://nodejs.org/download/）找到最新的stable源码
    
    
    $ cd
    $ wget http://nodejs.org/dist/v0.10.29/node-v0.10.29.tar.gz
    $ tar xvf node-v0.10.29.tar.gz
    $ cd node-v0.10.29/
    $ ./configure
    $ make

编译过程要持续数个小时。

安装完成后需要执行：
    
    
    $ sudo make install

### Groove Basin

现在是开始音乐播放服务器Groove Basin的时间了。
    
    
    $ cd
    $ git clone https://github.com/andrewrk/groovebasin
    $ cd groovebasin/
    $ npm run build

这个过程要持续数分钟，它会下载和编译Groove Basin依赖的软件。

我们建立音乐目录，如果没有的话：
    
    
    $ mkdir ~/music/

把你所有的音乐都拷贝到这个目录下。
    
    
    $ node lib/server.js

现在需要启动服务器了。如果你想要改变configure选项，使用Ctrl+C结束服务器然后编辑`config.js`

享用吧！请自由的关注[GitHub上面的GrooveBasin主页](https://github.com/andrewrk/groovebasin)，[报告bug report或者需要的特性](https://github.com/andrewrk/groovebasin/issues/new),或者加入在irc.freenode.org上面的`#libgroove `讨论获取故障帮助。

我们对pull request非常欢迎，特别是可以让[groovebasin.com](https://github.com/andrewrk/groovebasin.com)看起来更漂亮的Pull request。

[将树莓派打造成音乐播放服务器](http://www.geekfan.net/10419/)，首发于[极客范 – GeekFan.net](http://www.geekfan.net)。
