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.

96 comments:

  1. Great tutorial, I ran into a problem at the end of the documentation.

    When running require 'mumble-ruby' I get.

    cannot load such file -- mumble-ruby
    from/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'

    Any insight on the issue? thanks.

    ReplyDelete
    Replies
    1. It seems that you don't have the mumble ruby gem installed.
      Did you by chance miss the "sudo gem install mumble-ruby" step?

      Delete
  2. Thanks for the quick reply, I too thought the same thing so I uninstalled and re-installed everything with the same error.

    This is what I get after "sudo gem install mumble-ruby"

    Successfully installed mumble-ruby-0.0.3
    1 gem installed
    Installing ri documentation for mumble-ruby-0.0.3...
    Installing RDoc documentation for mumble-ruby-0.0.3...

    Thanks again for the fast reply.

    ReplyDelete
    Replies
    1. A few questions:
      What operating system and version are you using?
      What is the output of your "gem environment" command.
      What is the output of your "ruby --version".
      What is in /var/lib/gems/?

      Delete
  3. Also if it helps, I am running a mumble server on the same box, and its 12.04 LTS

    ReplyDelete
  4. Thanks again for your help...sorry had a long day today.

    1.) Ubuntu Server (headless) 12.04 LST

    2.) RubyGems Environment:
    - RUBYGEMS VERSION: 1.8.15
    - RUBY VERSION: 1.8.7 (2011-06-30 patchlevel 352) [i686-linux]
    - INSTALLATION DIRECTORY: /var/lib/gems/1.8
    - RUBY EXECUTABLE: /usr/bin/ruby1.8
    - EXECUTABLE DIRECTORY: /usr/local/bin
    - RUBYGEMS PLATFORMS:
    - ruby
    - x86-linux
    - GEM PATHS:
    - /var/lib/gems/1.8
    - /home/cory/.gem/ruby/1.8
    - GEM CONFIGURATION:
    - :update_sources => true
    - :verbose => true
    - :benchmark => false
    - :backtrace => false
    - :bulk_threshold => 1000
    - REMOTE SOURCES:
    - http://rubygems.org/

    3.) ruby 1.8.7 (2011-06-30 patchlevel 352) [i686-linux]

    4.) drwxr-xr-x 3 root root 4096 Apr 17 13:50 .
    drwxr-xr-x 68 root root 4096 Apr 17 13:52 ..
    drwxr-xr-x 6 root root 4096 Apr 17 13:50 1.8

    ReplyDelete
    Replies
    1. It seems your ruby version is old, ensure you installed 1.9.
      If it is installed you may need to make it the default version.
      See the following link for how to do that. http://stackoverflow.com/questions/1892877/how-do-i-make-ruby-1-9-the-default-ruby-on-ubuntu

      Alternatively you can call it directly by invoking "ruby1.9.1" instead of just "ruby".

      You can also invoke gem1.9.1 directly.
      Once you have installed the gem file into 1.9 then invoke your script with "ruby1.9.1 script.rb"

      Delete
  5. Make that 12.04.2 LST not sure if that means anything.

    ReplyDelete
  6. Ok, here is where I believe I stand now.

    I chose to try and uninstall all ruby, rubygem versions from my system. After going back through your tutorial I insalled the ruby1.9.1-full successfully but trying to install rubygems forces ruby1.8 as well.

    If you can explain how to just "invoke" ruby1.9.1 instead of "ruby" I would appreciate it.

    Also you said you can invoke gem1.9.1 directly, it seems that when I install rubygems the only version available is 1.8.

    Thanks

    ReplyDelete
    Replies
    1. At the terminal, rather than type "ruby" type "ruby1.9.1" same goes for rubygems.
      eg
      # ruby --version
      ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]

      # ruby1.9.1 --version
      ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]


      If you change your alternatives config, you shouldn't need to do that.

      You will probably need to install the gem with version 1.9.1 and run it with 1.9.1, that should solve your issue.

      Delete
  7. I will give that a try thank you, I will post my results when I get a moment.

    ReplyDelete
  8. Hi,

    I am getting the following errors when trying to start mumble-ruby from the ruby shell.

    irb(main):001:0> require 'mumble-ruby'
    LoadError: Could not open library 'celt0': celt0: cannot open shared object file: No such file or directory.
    Could not open library 'libcelt0.so': libcelt0.so: cannot open shared object file: No such file or directory
    from /var/lib/gems/1.9.1/gems/ffi-1.8.1/lib/ffi/library.rb:123:in `block in ffi_lib'
    from /var/lib/gems/1.9.1/gems/ffi-1.8.1/lib/ffi/library.rb:90:in `map'
    from /var/lib/gems/1.9.1/gems/ffi-1.8.1/lib/ffi/library.rb:90:in `ffi_lib'
    from /var/lib/gems/1.9.1/gems/celt-ruby-0.0.1/lib/celt-ruby.rb:8:in `'
    from /var/lib/gems/1.9.1/gems/celt-ruby-0.0.1/lib/celt-ruby.rb:5:in `'
    from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /var/lib/gems/1.9.1/gems/mumble-ruby-0.0.3/lib/mumble-ruby.rb:1:in `'
    from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:60:in `require'
    from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:60:in `rescue in require'
    from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
    from (irb):1
    from /usr/bin/irb1.9.1:12:in `'

    Any tips?

    ReplyDelete
    Replies
    1. You will need to install celt.

      sudo apt-get install celt

      I will add this to the guide.

      Delete
    2. cory@CSServ:~$ update-alternatives --config gem
      There are 2 choices for the alternative gem (providing /usr/bin/gem).

      Selection Path Priority Status
      ------------------------------------------------------------
      0 /usr/bin/gem1.8 180 auto mode
      1 /usr/bin/gem1.8 180 manual mode
      * 2 /usr/bin/gem1.9.1 10 manual mode

      Press enter to keep the current choice[*], or type selection number:
      cory@CSServ:~$ update-alternatives --config ruby
      There are 2 choices for the alternative ruby (providing /usr/bin/ruby).

      Selection Path Priority Status
      ------------------------------------------------------------
      0 /usr/bin/ruby1.8 50 auto mode
      1 /usr/bin/ruby1.8 50 manual mode
      * 2 /usr/bin/ruby1.9.1 10 manual mode

      Hey their Magnus its been a little while I have been busy.

      I was able to set the alternatives for gem and ruby to 1.9.1 but as Chet said above I get the same error he is getting even after installing celt. We are stretching you thin I am sure :) here is my info:

      LoadError: Could not open library 'celt0': celt0: cannot open shared object file: No such file or directory.
      Could not open library 'libcelt0.so': libcelt0.so: cannot open shared object file: No such file or directory
      from /var/lib/gems/1.9.1/gems/ffi-1.8.1/lib/ffi/library.rb:123:in `block in ffi_lib'
      from /var/lib/gems/1.9.1/gems/ffi-1.8.1/lib/ffi/library.rb:90:in `map'
      from /var/lib/gems/1.9.1/gems/ffi-1.8.1/lib/ffi/library.rb:90:in `ffi_lib'
      from /var/lib/gems/1.9.1/gems/celt-ruby-0.0.1/lib/celt-ruby.rb:8:in `'
      from /var/lib/gems/1.9.1/gems/celt-ruby-0.0.1/lib/celt-ruby.rb:5:in `'
      from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
      from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
      from /var/lib/gems/1.9.1/gems/mumble-ruby-0.0.3/lib/mumble-ruby.rb:1:in `'
      from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:59:in `require'
      from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:59:in `rescue in require'
      from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
      from (irb):1
      from /usr/bin/irb1.9.1:12:in `'

      cory@CSServ:~$ gem environment
      RubyGems Environment:
      - RUBYGEMS VERSION: 1.8.11
      - RUBY VERSION: 1.9.3 (2011-10-30 patchlevel 0) [i686-linux]
      - INSTALLATION DIRECTORY: /var/lib/gems/1.9.1
      - RUBY EXECUTABLE: /usr/bin/ruby1.9.1
      - EXECUTABLE DIRECTORY: /usr/local/bin
      - RUBYGEMS PLATFORMS:
      - ruby
      - x86-linux
      - GEM PATHS:
      - /var/lib/gems/1.9.1
      - /home/cory/.gem/ruby/1.9.1
      - GEM CONFIGURATION:
      - :update_sources => true
      - :verbose => true
      - :benchmark => false
      - :backtrace => false
      - :bulk_threshold => 1000
      - REMOTE SOURCES:
      - http://rubygems.org/

      cory@CSServ:~$ ruby --version
      ruby 1.9.3p0 (2011-10-30 revision 33570) [i686-linux]

      Delete
    3. Can you try apt-getting "libcelt-dev" or "libcelt0-0"?
      Let me know if it works after that.

      Delete
  9. Well sir, you have done a great job THANK YOU!
    (just 1 problem left)

    sudo apt-get install libcelt-dev was the fix I already had libcelt0-0

    I ran the music file which is an .mp3 and connected to my mumble client as soon as I told it to play the music I got a bunch of:

    "\x00", "\x00", "\x00", maybe over 100 of them and the sound coming from my headset sounded like I was being mind controlled by the aliens in Independence Day (hope you've seen the movie, first thing I thought of)

    So we are past the mumble-ruby and gems part which I am sure you are happy about, but I do not hear any music its just weird static.

    Thanks again for all your help you have done a great job.

    ReplyDelete
    Replies
    1. Are you using MPD to stream the mp3, or are you piping the mp3 file to the fifo?
      It seems like the audio being streamed to the fifo is using the wrong encoding.
      The audio needs to be encoded as 48000hz 16 bit mono wav(note the format line in the MPD config).
      MPD will play any format audio file and then stream it as wav o the fifo file.

      Delete
  10. I followed your instructions as you described for setting up MPD config.

    I commented out every output line and created the one you said too:

    # other audio outputs.
    #
    #fifo output
    audio_output {
    type "fifo"
    name "My FIFO"
    path "/tmp/mumble.fifo"
    format "48000:16:1"
    }
    #

    I am not sure what else to do, could it be my servers sound card?

    And yes I am using MPD to stream the mp3

    How do you set it to mono wav or is that what the 48000hz 16 bit does?

    Thanks

    ReplyDelete
    Replies
    1. Your server sound card shouldn't matter at all.
      The "format" line broken up means 48000hz:16bit:1channel(mono).

      Im not sure what your current problem is all I can ask you to do is try the following and let me know how it goes.

      Try doing the following steps in this order:
      1. Restart the mpd service, ensure it has stopped playing music.
      2. Delete/recreate the fifo file.
      3. Start the ruby script.
      4. Start playing music.

      Delete
  11. Another interesting thing I just added 4 new mp3 files to my /var/lib/mpd/music folder and if I ls -la the folder my first mp3 is a turquoise color where the new ones are light green and I am unable to add the files using "mpc add"...I get error: directory or file not found.

    I tried doing sudo chown -R mpd:audio /var/lib/mpd/music

    Also restarted the server the only difference I can see is:

    Blue file shows -rw-r--r-- 1
    Green file shows -rwxr-xr-- 1

    ReplyDelete
    Replies
    1. Do a "mpc listall" and see if your music in in the database.
      If its not then do a "mpc update" then listall again, it should pickup all your music in the music directory.
      As far as your permissions go, all you should need to ensure is that the file is readable by mdp, which from what you have shown, should be the case.
      "mpc add" only adds songs from your mpd database to the current playlist.

      Delete
  12. Ok lol this is such a pain we are getting closer.

    mpc listall and mpc update worked for my second problem.

    my first problem I think we worked out 1 issue my fifo file didn't have the proper permissions set not sure why it didn't take the first time but now when I start playing music and run the script I dont get the annoying noise anymore but I just dont hear anything here is a printout of what is playing I am wondering if it has something to do with the volume?

    [playing] #1/4 0:00/0:02 (0%)
    volume: n/a repeat: off random: off single: off consume: off

    ReplyDelete
  13. The volume should read "n/a", AFIAK there is no easy way of controlling volume using a mpd fifo output.
    Other than the fact your track is 2 seconds long and you don't have repeat turned on, I don't know why it isn't working.

    ReplyDelete
  14. sudo rm /var/lib/mpd/tag_cache worked!

    listening to music right now, I have no idea why mpd was showing 2 seconds. Maybe with me just messing around with it so much the cache was all garbled.

    I can't thank you enough for all your hard work and support, I hope if nothing else you learned something and I noticed you added a few more lines to your guide.

    Now that we are out of fix mode I do have a few more questions that I will research myself but figured maybe you have messed with it along your travels:

    1.) Sound is great but its LOUD is their a way to lower the volume from my musicBot in mumble without lowering other people.

    2.) My server goes off at 3 a.m. and turns back on at 10 a.m., can I set a script to add the songs I want for the day and have the script autorun on startup I travel for my job and friends I am sure would like to log in to listen while I am gone.

    3.) Is their a shuffle command so it doesn't just play the same songs over and over in the same order.

    Again a huge Thank You!

    ReplyDelete
    Replies
    1. You should just be able to add the script as an upstart job or something and MPD has a random function, type "mpc random on".

      Delete
  15. After doing a bit of research, it seems Mumble doesn't support peer volume control so I guess my follow up question is can we add some code to audio_output for FIFO such as:

    mixer_device
    mixer_control
    or some form of volume output for MPC

    Or does FIFO even support that, I know those are features I keep seeing for alsa.

    Another thing do we have to use FIFO for this contraption to work?

    ReplyDelete
    Replies
    1. I don't know how to control volume from within MPD, mumble-ruby has a hard coded volume within "lib/mumble-ruby/audio_stream.rb".

      I haven't experimented with it, but you can probably set that to your desired level, or try raising an issue on the github page and seeing if someone can make it more easily accessible.

      Delete
    2. On further investigation mumble-ruby does have a way of controlling the volume.
      The stream_raw_audio method returns the audiostream object.
      You can then set the volume level on the stream.
      For example, to set the volume to 20% you would do the following:

      stream = cli.stream_raw_audio('/tmp/myfifo')
      stream.volume = 20

      Note that the volume control is linear, unlike most volume controls which are exponential. IE, setting the volume to 50% won't sound like half the volume.

      Read the below article to understand what I mean:
      http://www.dr-lex.be/info-stuff/volumecontrols.html

      I might add volume control to the guide later today.

      Delete
  16. This is very interesting, and I might try this out.

    Question though, is there a way to interface the mumble chat to mpc/d? As in, be able to "request" songs or change the music in some way with this method?

    ReplyDelete
    Replies
    1. mumble-ruby has an on_text_message callback which receives text messages.
      You can also send text messages.
      You should probably check out the github page for more information on that, but it should be entirely possible to implement your desired functionality.

      Delete
  17. Its funny you raided that question Anonymous, just had my friend tell me yesterday that it would be cool if he could control MPD from somewhere to change/request a playlist.

    If you find out how to do this please post here again with what you did I will continue to work the volume issue in MPD.

    Thanks everyone for your help

    ReplyDelete
  18. A few of my findings:

    Volume can be controlled via the volume method of the audiostream object. An example would be in your already existing ruby script add:

    #!/usr/bin/env ruby
    require 'mumble-ruby'
    cli = Mumble::Client.new('localhost', 64738, 'Botty')
    cli.connect
    sleep(1)
    cli.join_channel('General')
    stream = cli.stream_raw_audio('/tmp/mumble.fifo')
    stream.volume = 20
    print 'Press enter to terminate script';
    gets
    cli.disconnect

    A few things I am still having problems automating and would love some advice if anyone knows a way.

    I would like to make mpc start playing music automatically on startup as well as the ./Mumble.rb script

    My /tmp/mumble.fifo also seems to lose permissions from time to time not sure if it is something I am doing or not. I tried making the mumble.fifo file in /home/user but after changing the Mumble.rb and mpd.conf file to reflect the new location it wouldn't work. Anyone know why? Could the permission changes be because its in the /tmp folder?

    Thanks

    ReplyDelete
  19. Hello !

    First of all, thank's for your tutorial ! But I have a huge problem :/ Here it is :

    I did all well, and when I do "require 'mumble-ruby', I have this :

    irb(main):001:0> require 'mumble-ruby'
    LoadError: cannot load such file -- mumble-ruby
    from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from (irb):1
    from /usr/bin/irb1.9.1:12:in `'



    I did all two times, I did "sudo apt-get install ruby1.9.1-full" and "sudo apt-get install ruby", I followed the instructions you said to "Stunner1984", but nothing .. I'm kind of stuck in there :/

    Do you have any idea ? ( Here some useful things for you : http://pastebin.com/BLJA2Rag

    prototype@Jarvis:~$ gem environment
    RubyGems Environment:
    - RUBYGEMS VERSION: 1.8.15
    - RUBY VERSION: 1.8.7 (2011-06-30 patchlevel 352) [i686-linux]
    - INSTALLATION DIRECTORY: /var/lib/gems/1.8
    - RUBY EXECUTABLE: /usr/bin/ruby1.8
    - EXECUTABLE DIRECTORY: /usr/local/bin
    - RUBYGEMS PLATFORMS:
    - ruby
    - x86-linux
    - GEM PATHS:
    - /var/lib/gems/1.8
    - /home/prototype/.gem/ruby/1.8
    - GEM CONFIGURATION:
    - :update_sources => true
    - :verbose => true
    - :benchmark => false
    - :backtrace => false
    - :bulk_threshold => 1000
    - REMOTE SOURCES:
    - http://rubygems.org/




    prototype@Jarvis:~$ ruby --version
    ruby 1.9.3p0 (2011-10-30 revision 33570) [i686-linux]


    I got a directory called "1.8", with "cache", "doc", "gems" and "specifications" in it. In /var/lib/gems/1.8/gems/, i have "activesupport-3.2.13", "celt-ruby-0.0.1", "ffi-1.9.0", "i18n-0.6.1", "multi_json-1.7.7", "mumble-ruby-0.0.3" and "ruby_protobuf0.4.11".


    Any idea ? :/ )

    ReplyDelete
    Replies
    1. You have installed mumble-ruby into your ruby 1.8 version.
      You need to install the gems using gem1.9.x.
      On 12.04 you can just type "sudo gem1.9.1 install mumble-ruby".
      If that doesn't work let me know.

      Delete
  20. It works ! :D

    But the script.rb does not work .. Even with the chmod +x. Nothing happens.

    ReplyDelete
    Replies
    1. You probably don't have 1.9 set as the os default.
      Try running the script with ruby directly.
      "ruby1.9.1 script.rb"

      Delete
  21. I had a lot of trouble installing ruby, but eventually it seemed to work.

    Then comes the problem. MP3 playback is extremely choppy. Does anyone know any possible fixes? The file playback with foobar2000 is great.

    ReplyDelete
    Replies
    1. It sounds like an encoding issue. I experienced a similar thing when i had the wrong sample rate.
      Make sure your mpd fifo output format configuration is "48000:16:1" 48000hz 16 bit mono.

      Delete
  22. Would it be possible to input from USB port ?? If I had my MP3 player connected to analog to digital cable into USB port ?

    ReplyDelete
    Replies
    1. I haven't tested this but it might work.
      You should be able to pipe your microphone directly to the fifo file.
      First list your audio devices and find your usb mic.
      arecord -L
      Then pipe its output (16bit mono 48000hz) to your fifo.
      arecord -D DEVICENAME -f S16_LE -c1 -r48000 | myfifo.fifo

      Delete
    2. Thanks, will try and let you know...

      Delete
  23. Didnt get it to work..here is what I tried

    1. Did a test file to verify arecord, I was able to play wav file so thinking arecord setup/parameters ok

    arecord -f dat -d 20 -D hw:1,0 test.wav

    2. Made fifo and chmod
    mkfifo /tmp/mumble.fifo
    chmod 777 /tmp/mumble.fifo

    3 Piped arecord to fifo.. had to change channel to 2, was gettign error when attemptd 1 ..set_params:1087: Channels count non available.. also change reference to to fifo which I created

    arecord -D hw:1,0 -f S16_LE -c2 -r48000 | /tmp/mumble.fifo

    4. Executed ruby steps

    irb1.9.1

    require 'mumble-ruby'

    cli = Mumble::Client.new('localhost', 64738, 'ruby_client', 'password')

    cli.connect

    cli.channels

    cli.join_channel(1)

    cli.stream_raw_audio('/tmp/mumble.fifo')


    I can see the client connect, but no audio..when I run through the standard mumble client, I can get the audio

    If you see any errors or omissions, let me know.. I thought the pipe would work.

    Thanks

    ReplyDelete
    Replies
    1. Have you been able to get it to work using MPD?
      I would first try make sure you can get ti working with mpd or you could try piping a correctly encoded wav file to your fifo.
      mumble-ruby only supports the CELT codec, since writing this article mumble has made the Opus codec the default which may be causing you issues.
      As far as arecord goes, try changing the | to >. I think I used the wrong syntax. If it wont let you specify a single channel, I would just omit the -c flag rather than specify 2.

      You might even be able to use your first command with the fifo file name rather than a wav file. Im sorry I cant test this out, I don't have a USB mic, or a Linux system with any real sound devices.

      Delete
    2. Thanks for reply... I did manage to get the MPD working, but very choppy.. changed the other client I had listening buffer, now BohemianRhapsody.mp3 playing clear...will work on the arecord piping

      Delete
    3. The pipe > worked somewhat.. I have a really SLOW signal processing.. like playing a 45 record at 33 1/3 if you know what that means..and then fail and some peculiar output...first part ok, I think, but then glibc error and big dump of memory stuff.. didnt want to post the entire thing

      irb(main):006:0> cli.stream_raw_audio('/tmp/mumbleruby.fifo')
      => #, @encoder=#, @prediction_request=0, @vbr_rate=60000>, @file=#, @conn=#, @sock=#>, @seq=0, @num_frames=6, @compressed_size=75, @pds=#, @volume=1.0, @queue=#>, @producer=#, @consumer=#>

      irb(main):007:0> *** glibc detected *** irb1.9.1: free(): invalid next size (fast): 0x00007f335c015f40 ***
      ======= Backtrace: =========
      /lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7f3377150b96]
      /var/lib/gems/1.9.1/gems/ffi-1.9.0/lib/ffi_c.so(+0x1613e)[0x7f33751fb13e]
      /usr/lib/libruby-1.9.1.so.1.9(+0x6aed2)[0x7f33774fbe

      ======= Memory map: ========
      00400000-00401000 r-xp 00000000 08:02 2764029 /usr/bin/ruby1.9.1
      00600000-00601000 r--p 00000000 08:02 2764029 /usr/bin/ruby1.9.1
      00601000-00602000 rw-p 00001000 08:02 2764029 /usr/bin/ruby1.

      Delete
    4. Looking at teh github comments:
      # Start streaming from a FIFO queue of raw PCM data
      cli.stream_raw_audio('/tmp/mpd.fifo')
      # => #

      Looking arecord:
      -t, --file-type TYPE
      File type (voc, wav, raw or au). If this parameter is omitted the WAVE format is used.

      I'll try again with -t raw

      Delete
    5. I tried the raw type and experienced similar results, really slow signal
      For grins, I did a arecord and aplay on the FIFO, rec'd overrun and underrun.. added a buffer setting, still no luck...

      Delete
    6. If its working but the audio just plays slowly, then that suggests to me that the encoding is wrong.
      If you have 2 channel audio and mumble-ruby is reading it as one channel then the audio will probably play twice as slow.
      If you have 41khz audio and mumble-ruby is reading it at 48khz then the audio will play fast.
      Since you have been unsuccessful with changing the channels to mono, that would be my best guess.

      Delete
    7. You could try splitting the channel with sox and discarding the second channel.

      arecord -D hw:1,0 -f S16_LE -c2 -r48000 | sox - -p remix - > /tmp/mumble.fifo

      Delete
    8. Pardon my ignorance..a little blury on sox and parms.. error below... also, on my arecord, added -t raw to send raw vs wav

      arecord -D hw:1,0 -f S16_LE -t raw -c2 -r48000 | sox -p remix - > /tmp/mumbleruby.fifo
      sox FAIL sox: Not enough input filenames specified

      Delete
    9. If you copy my command verbatim it should work.
      Note the double hyphen.
      SOX normally accepts an input and output filename.

      The first dash means there is no input file, rather you are piping input to it and the -p means there is no output file, rather you are piping the output. The remix - means you are mixing all channels into a single channel. Alternatively you could do remix 1 or remix 2, to get the left and right channels accordingly.

      Delete
    10. Thanks.. I did try with you original command, just went through a few variations try to get it to work.. so tried again, including inputs/output .. first one without -t parm WAVE Riff header not found.. makes some sense, arecord by default formats wav, so I tried all the others, raw, au, voc and back to wav

      arecord -D hw:1,0 -f S16_LE -c2 -r48000 | sox - -p remix - > /tmp/mumble.fifo
      Recording WAVE 'stdin' : Signed 16 bit Little Endian, Rate 48000 Hz, Stereo
      sox FAIL formats: can't open input `-': WAVE: RIFF header not found

      arecord -D hw:1,0 -f S16_LE -c2 -r48000 -t raw | sox - -p remix - > /tmp/mumble.fifo
      Recording raw data 'stdin' : Signed 16 bit Little Endian, Rate 48000 Hz, Stereo
      sox FAIL formats: can't determine type of `-'

      arecord -D hw:1,0 -f S16_LE -c2 -r48000 -t au | sox - -p remix - > /tmp/mumble.fifo
      Recording Sparc Audio 'stdin' : Signed 16 bit Little Endian, Rate 48000 Hz, Stereo
      arecord: begin_au:2308: Sparc Audio doesn't support S16_LE format...
      sox FAIL formats: can't determine type of `-'

      arecord -D hw:1,0 -f S16_LE -c2 -r48000 -t voc | sox - -p remix - > /tmp/mumble.fifo
      Recording VOC 'stdin' : Signed 16 bit Little Endian, Rate 48000 Hz, Stereo
      sox FAIL formats: can't open input `-': VOC file header incorrect

      arecord -D hw:1,0 -f S16_LE -c2 -r48000 -t wav | sox - -p remix - > /tmp/mumble.fifo
      Recording WAVE 'stdin' : Signed 16 bit Little Endian, Rate 48000 Hz, Stereo
      sox FAIL formats: can't open input `-': WAVE: RIFF header not found

      Delete
    11. You could try the following

      arecord -D hw:1,0 -f S16_LE -c2 -r48000 -t raw | sox -t raw -e signed-integer -b 16 -c 2 -r 48k -L - -p remix - > /tmp/mumble.fifo

      or

      arecord -D hw:1,0 -f S16_LE -c2 -r48000 -t wav | sox -t wav -e signed-integer -b 16 -c 2 -r 48k -L - -p remix - > /tmp/mumble.fifo

      If its still not working could you do the following.
      Take the command that was working but playing the audio slowly, pipe it to a new file for a few seconds while making some noise on your microphone eg "> test.file", then upload that file somewhere so I can play around with it.

      Delete
    12. Thanks.. neither had success..I'll upload to dropbox.. did you want the out put of arecord or after the sox

      Delete
    13. Just the output before sox.

      Delete
  24. https://www.dropbox.com/sh/6s0kj9rg6clwbvg/8OybAavHJN?n=207972662

    ReplyDelete
  25. For reference, iPhone mp3 to ADC to Linux .. ADC is this one http://www.ambery.com/usbantodiauc.html

    ReplyDelete
  26. I took the raw file and imported into Audcity File>import > Raw Data .. when I chose 16 bit, LE, 2 channel, 48k, sounds good, when import 16, LE,1 Channel, 48k, very similar to the sound that I was getting.. so not sure if that combined with buffer or something else ultimately causing my bad sound...

    ReplyDelete
  27. Appearing more and more to me to be a channel issue. I redirected arecord -D hw:1,0 -f S16_LE -c2 -r48000 -t raw | sox -t raw -e signed-integer -b 16 -c 2 -r 48k -L - -p remix - > /tmp/mumble.fifo to a file vs fifo i.e mumble.file. Then imported to audacity, even with the remix - which should go to mono, when I configure import with channel 1, still slow, import with channel 2, sounds normal

    ReplyDelete
  28. I think I got it working
    cat test_raw.file | pv -L 192k | sox -t raw -e signed-integer -b 16 -c 2 -r 48k -L - -b 16 -t raw ~/myfifo remix -

    The above command pipes your file (at 192kB/s to simulate realtime audio) into sox then sox outputs it to the fifo file.
    This seemed to work well with mumble-ruby, although it was a little quiet.

    You can pump up the volume with sox by appending "i4" to the end of the command.

    Anyway, try running this as
    arecord -D hw:1,0 -f S16_LE -c2 -r48000 -t raw | sox -t raw -e signed-integer -b 16 -c 2 -r 48k -L - -b 16 -t raw /tmp/mumble.fifo remix -

    ReplyDelete
    Replies
    1. DING DING DING !!!!

      Thank you very much for your time and effort... A pint or two will be hoisted in your name !

      Thanks!

      Delete
  29. Hey guys I tried to make this bot work aswell. Here is what happens when I run it:

    /var/lib/gems/1.9.1/gems/mumble-ruby-0.0.3/lib/mumble-ruby/client.rb:53:in `stream_raw_audio': Mumble::NoSupportedCodec (Mumble::NoSupportedCodec)
    from ./music-bot.rb:7:in `'

    It doesn't even connect to the server. Is it because of the Opus codec?

    Thanks

    ReplyDelete
    Replies
    1. I haven't seen that error myself, although your theory seems like the most obvious answer.

      You could try connecting to a bunch of mumble servers and see if you get the same error.

      Delete
    2. The thing is that I did the exact process on a brand new Ubuntu non-server system and the same thing happened.

      I mean maybe the repository's version is an issue since I have tested it on an Ubuntu Server and on a simple ubuntu.

      I will try to connect to random servers

      Delete
    3. I think its the opus. I tried many servers with 1.2.4 and 1.2.5 versions with no success. Then i found one of 1.2.5 with opus enabled. It didn't pop the error, but it also didn't connect since it was password protected. But then I tried to connect to my server without a password and the no supported codec error popped.

      Which means that this server with opus enabled worked. Why though this doesn't make sense.

      Delete
    4. Do you have the libcelt dependencies installed?
      After looking at the code it looks like if the celt encoder is not initialized properly then it might raise this exception.

      Delete
    5. Nah it can't be that I found a server 1.2.4 with no pwd and was able to connect, the bot did not report errors but never connected.

      On the debugging side of things I've put a "puts" in codec_negotiation function of client.rb in mumble-ruby to check if this is even called, and its not. The text I've entered is not displayed. I mean this has to be of relevance right

      Delete
    6. Oh I am replying to myself didn't read your post. Dunno I did apt-get install libcelt, shouldn't that do it with the dependancies?

      Delete
    7. You need libcelt-dev not just libcelt.
      As far as debugging the codec negotiation you can add this after you define cli but before you call connect.

      cli.on_codec_version do |msg|
      puts "\n" + msg.inspect
      end

      Delete
    8. Yea yea I meant libcelt-dev my bad sorry. I have libcelt-dev.

      Err I did what you said and nothing is displayed, not even the new line. Just the same error. I put a sleep(10) after it to be sure, but nothing came through

      Delete
    9. Also I have a chat bot aswell. When this is connected and I right click its info it says CELT version 0.7.0, shouldn't that be 0.7.1?

      Delete
    10. I don't understand what you mean by "Nah it can't be that I found a server 1.2.4 with no pwd and was able to connect, the bot did not report errors but never connected. "
      Did it connect or not?
      If you use the standard mumble client and then try to connect with ruby, do you see the bot join the server?

      I picked a random server from the server list 216.185.109.66:1562
      It works for me, try connecting to that with your normal client then your script and let me know what happens.

      Delete
    11. To anyone that is wondering I still haven't solved the issue, we got with magnus on chat and we couldn't end up with anything. (Big props to him for doing that btw)

      I will post here if i find sth.

      Delete
    12. Yea so I've put a sleep(10) between cli.connectt and cli.stream_raw_audio and it worked. I mean that's some weird stuff right there, but I guess since my server is in the local network it connected way to fast for it to initialize the codec.

      However this is a code issue right? Shouldn't it check if the codec was initialized? This explains why the script never entered the on_codec_version function, It simply didn't have enough time.

      Delete
    13. This comment has been removed by the author.

      Delete
    14. Here is a slightly more robust implementation that might help you.
      It will sleep until it receives the server_sync message.
      It also prints out text messages and has volume control.
      Let me know how it works for you.

      https://gist.github.com/magJ/50bbdbf6ab01274079e8

      Delete
    15. Hey, thanks for the reply and for the script. Unfortunately I won't be able to look into it until October 5th when I'll finish with my exams. I will get back to you when I am done, I am going to fully focus on mumble at October since we are also trying to edit the client to support custom emoticons.

      Again thanks, afk for 2 weeks.

      Delete
    16. Hello again, I sat down with a clear mind after the exams and reviewed the situation. Apparently sleep(1) does the trick too. No sleep doesn't.

      Also, regarding my certificate implementation, it turns out that while trying to solve the issue, I've put a "puts" after "then" at lines 173 and 174 in client.rb of mumble-ruby, which I totally forgot. This made things really complicated and weird since I had multiple files, some with and some without "puts", leading to the situation where some worked and some didn't.

      Long story short the code for the bot in the your tutorial works perfectly.

      The verdict of the story is, don't edit code while having exams, study instead. Anyway I would like to thank you again for helping me through this and also for this very nice tutorial. Your inspect script is pretty neat too.

      If anyone is interested in my certificate implementation, so that he can register his bots, look here http://www.techpowerup.com/forums/showthread.php?t=190627

      Delete
  30. This comment has been removed by a blog administrator.

    ReplyDelete
  31. Awesome post.

    Just my 2 cents:

    You can even queue youtube urls using youtube-dl... try using this command:

    mpc add $(youtube-dl -g http://www.youtube.com/watch?v=unb3m_6ceZQ)

    Just change the youtube url to yours.

    Next step, create a mumble bot so users can manage playlist and submit urls to play

    Maybe mix this proyect : https://github.com/FreeApophis/Ruby-Mumble-Bot with this one https://github.com/perrym5/mumble-ruby ... lets see what happens, stay tunned

    ReplyDelete
    Replies
    1. the youtube thing is pretty cool, although I had some robotic sounds with some vids. Didn't look at it thoroughly though. Regarding the project mixing, I've taken the certificate generation engine from Ruby-Mumble-Bot and put it into mumble-ruby. Check my post @ October 16, 2013 at 3:16 PM (last two lines)

      Delete
  32. I am running into an error as well. I have tried both 1.9.3 and 2.0.0 and get the same results. I am running centos 6, and installed:

    celt-devel-0.8.0-1.el6.i686
    libcelt-0.11.1-4.el6.i686
    celt-0.8.0-1.el6.i686
    libcelt-0.11.1-4.el6.x86_64
    celt-devel-0.8.0-1.el6.x86_64
    celt-0.8.0-1.el6.x86_64

    I also have:

    mumble-ruby
    celt-ruby

    I am able to connect to the mumble server, join a channel, however when i attempt to stream I get the following error:

    http://apaste.info/83D0

    Any help would be greatly appreciated

    ReplyDelete
  33. Its been a while Magnus:

    Have had my server running Mumble-ruby for quite some time with your help.
    Recently decided to update it for the newest opus codec and now again I am having issues.

    I am getting:
    /var/lib/gems/1.9.1/gems/ffi-1.8.1/lib/ffi/library.rb:123:in `block in ffi_lib': Could not open library 'opus': opus: cannot open shared object file: No such file or directory. (LoadError)
    Could not open library 'libopus.so': libopus.so: cannot open shared object file: No such file or directory
    from /var/lib/gems/1.9.1/gems/ffi-1.8.1/lib/ffi/library.rb:90:in `map'
    from /var/lib/gems/1.9.1/gems/ffi-1.8.1/lib/ffi/library.rb:90:in `ffi_lib'
    from /var/lib/gems/1.9.1/gems/opus-ruby-0.0.1/lib/opus-ruby.rb:8:in `'
    from /var/lib/gems/1.9.1/gems/opus-ruby-0.0.1/lib/opus-ruby.rb:5:in `'
    from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /var/lib/gems/1.9.1/gems/mumble-ruby-1.0.2/lib/mumble-ruby.rb:1:in `'
    from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:60:in `require'
    from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:60:in `rescue in require'
    from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
    from ./Mumble.rb:2:in `'

    I installed opus codec as far as i know but still not working. Wondering if it is similar to back when we did Celt and you also needed celt-dev. Any suggestions?

    ReplyDelete
    Replies
    1. You can probably just install libopus-dev.
      I haven’t used the new version, but libopus-dev contains the file it says is missing.
      http://packages.ubuntu.com/saucy/amd64/libopus-dev/filelist

      Delete
    2. I actually just did a reboot of the server, did an update and upgrade and it found the files it was missing, all is working now.

      I didn't see how to install opus from the sources. I ended up doing a wget from their website and just installing from their.

      All is working well though, thanks for the quick response.

      Delete
  34. Anybody update
    Ubuntu 14.04
    Ruby 2.1.0
    Rails 4.1.8
    Just did a rebuild with newer version, looks like I will be rolling back.. can't bet anything to work worth darn...

    ReplyDelete
  35. I only get one error..

    ./audio.rb:7:in `': undefined method `stream_raw_audio' for # (NoMethodError)

    its like it doesnt know "cli.stream_raw_audio('/tmp/mumble.fifo')"

    ReplyDelete
    Replies
    1. I fixed the problem.. everything working.. but audio is really choppy.. i can confirm i'm using 48khz mono 16bit...

      Delete
  36. Still getting choppy audio with the following
    arecord -D hw:1,0 -f S16_LE -c2 -r48000 -t raw | sox -t raw -e signed-integer -b 16 -c 2 -r 48k -L - -b 16 -t raw /tmp/mumble.fifo remix -

    ReplyDelete
    Replies
    1. Its hard to say what the issue is, this post hasn't been updated in a while so there may of been changes im not aware of.
      In my experience if the audio is playing, but its choppy, it usually indicates an encoding missmatch, whether it be sample rate, bit depth or channel amount.

      Delete
  37. On "sudo apt-get install libcelt-dev" I get "sudo apt-get install libcelt-dev" Is there a repo I need?

    ReplyDelete
    Replies
    1. I meant, i get "E: Unable to locate package libcelt-dev". it's late...

      Delete
  38. i have a problem. when i start is this in the log file :

    /home/botmaster/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- mumble-ruby (LoadError)
    from /home/botmaster/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /home/botmaster/src/mumble-ruby-pluginbot/pluginbot.rb:7:in `


    ReplyDelete
    Replies
    1. Seems like you need to install the mumble-ruby gem.

      Delete
    2. sudo gem install mumble-ruby
      Successfully installed mumble-ruby-1.1.3
      1 gem installed


      /home/botmaster/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- mumble-ruby (LoadError)

      whats rong ?

      Delete
  39. Thanks and that i have a neat offer you: House Renovation What To Do First home renovation near me

    ReplyDelete