Friday, June 1, 2012

Getting started with mumble-ruby, the headless mumble client.

In this guide I will cover installing version 0.0.3 of  mumble-ruby on Ubuntu server 11.10 as a headless client and streaming music through it using MPD (Music Player Daemon).
These steps may slightly differ depending on your Linux distro/version.

This guide will demonstrate how to:
  • Install mumble-ruby and its requirements.
  • Create a fifo file
  • Run mumble-ruby from the Interactive Ruby shell and via a basic script.
  • Stream audio from Music Player Daemon (MPD) through your fifo and to your mumble server. 
This guide does not include indepth information on using MPD or much information about mumble-ruby api.
Credit to perrym5 for creating mumble-ruby and helping me get this working.

Installing mumble-ruby

Mumble-ruby requires version 1.9.2 of ruby to work. In Ubuntu 11.10 it can be installed with:
sudo apt-get update
sudo apt-get install ruby1.9.1-full
Yes, ruby 1.9.2 is packaged as 1.9.1 for some reason.
Next lets get rubygems. Ruby gems is a ruby package manager.
sudo apt-get install rubygems
Next lets use gem to install the mumble-ruby codebase and its dependencies.
sudo gem install mumble-ruby
You will also need celt, a low latency audio library used by mumble-ruby.
sudo apt-get install libcelt-dev

Creating your FIFO file.

A fifo file is a buffer that we can send audio to. MPD will send audio to this buffer and mumble-ruby will read from it and send the audio to your server.
mkfifo /tmp/mumble.fifo
You can name/put your FIFO file wherever you want.
We need to ensure that it can be accessed by MPD and mumble-ruby.
Because I am lazy:
chmod 777 /tmp/mumble.fifo

Installing Music Player Daemon (MPD)

Music Player Daemon is quite a rather complex music playing application. There are lots of different clients you can control it with. This guide will just show you how to use mpc (Music Player Client).
Install MPD and MPC
sudo apt-get install mpd mpc
Next we need to edit the MPD conf file.
You can find indepth information about MPD configuration on thier wiki.
The important bit for us is the audio output settings.
sudo nano /etc/mpd.conf
Scroll down and comment out with a # any
audio_output { }
sections.

Next lets add our own.
#fifo output
audio_output {
        type            "fifo"
        name            "My FIFO"
        path            "/tmp/mumble.fifo"
        format          "48000:16:1"
}
If you want to connect remotely to your daemon you might also want to comment the
#bind_to_address                "localhost"
line (you might want to add a password if you do this, consider your network security)

Next you will need to add some music to your mpd music directory.
If you havent changed it
sudo cp TunnelSnakesRule.mp3 /var/lib/mpd/music
Make sure that mpd can read the music:
sudo chown -R mpd:audio /var/lib/mpd/music
Start the MPD service use restart if it is already running.
sudo service mpd start
Next lets use MPC to start playing our first song on loop
mpc add TunnelSnakesRule.mp3
mpc repeat on
mpc play 1

Running mumble-ruby from ruby shell

Now we are playing our audio to our fifo file, we need to start mumble-ruby and connect to our mumble server.
Start the Interactive Ruby Shell.
irb1.9.1
You will now be in an irb prompt like
irb(main):001:0>
Load the mumble-ruby libary.
require 'mumble-ruby'
Create an instance of the mumble-ruby client (hostname or IP, port, client name, password).
cli = Mumble::Client.new('mumble.example.com', 64738, 'Botty', 'password123')
Connect to your server
cli.connect
Fire up your local mumble application, You should now be able to see your bot in your mumble server root channel.
To get a list of channels from the shell type:
cli.channels
Note the channel id of the channel you want to join and use:
cli.join_channel(1)
Finally, lets play the audio:
cli.stream_raw_audio('/tmp/mumble.fifo')
If everything went well you should now be able to hear your music playing

Running mumble-ruby as a script

Save the below text as an .rb file.
#!/usr/bin/env ruby
require 'mumble-ruby'
cli = Mumble::Client.new('localhost', 64738, 'Botty')
cli.connect
sleep(1)
cli.join_channel('General')
cli.stream_raw_audio('/tmp/mumble.fifo')
print 'Press enter to terminate script';
gets
cli.disconnect

Make the script executable.
chmod +x script.rb

Then run the script. 
./script.rb

Problems

Helpful MPD commands
Lists music in your mpd database
mpc listall
Rescans your music directory and refreshes the MPD database
mpc update
If you are still having trouble with MPD check your file permissions and try deleting the database file.
sudo service mpd stop
sudo rm /var/lib/mpd/tag_cache
sudo service mpd start
Check your ruby version
ruby --version 
gem environment
Make sure ruby returns version 1.9.2 or higher. Ensure you are using the right rubygems and that the gems are being installed into the correct ruby version directory.

Further Reference

For more information about mumble-ruby have a look at the readme on the project page or have a browse of the source code.
For more information about Music Player Daemon (There is a lot more you can do with it) visit the MPD wiki.

Finally you can post questions here and I will try and respond. Note that I have zero ruby experience other what I have learned trying to get this working.
Please let me know if there any problems with this documentation.