Pages

Tuesday, November 5, 2013

Bluetooth headphones and Arch Linux: the endless battle

So a few years back I had the brilliant idea to get on the bluetooth bandwagon and get me a nice pair of headphones, because, you know, wires are for suckers. Pretty standard pair of Sony DR-BT50. And pretty much for as long as I've had the headphones I've had headaches. Not that they're physically uncomfortable... quite the opposite. The cushion is made of the softest skin, probably ripped straight off of a newborn baby's ass. No, it's the bluetooth that's been giving me the problems. Not so much in Windows, but who uses Windows anyway? They worked for a while on my MacBook, although with a bunch of caveats and weird behavior (I could lock my computer up if I accidentally closed the lid before turning the headphones off... WHAT?!) until the screen broke and while I was fixing it I accidentally nicked the bluetooth antenna that's weirdly placed behind the LCD.

But mostly I use Linux on a day to day basis, especially at work where I listen to most of my music. And I need headphones, because me blasting Cynic before noon would get the rats all agitated and whatnot. And let me tell you, blutooth on Linux is about as developed as cuisine on an airplane. Except where airplane food has stayed the same over, I dunno, the past 50 years or so (has there been any time when they did not offer peanuts and soda?) bluetooth on Linux changes about every six months. The main stack is called bluez and the developers are churning the versions and modifying the API something fierce. Not so much with the documentation. In fact it seems like the documentation has been dwindling in size. For example, there used to be a wiki. Was it amazing? No, but it gave people a starting point. Now the only way to get to the wiki, which is mostly about bluez4 (the version most people are still using) is to use the way back machine. Anyway, this guy here does a much better job than me at describing all the ridiculousness surrounding the topic.

What I meant to say before I got off the rails is that, every time some update to bluez or pulseaudio rolls in, my poor headphones stop working. And even if they do work, there's a lot of crackling and just general instability. So the other day I said "enough is enough!" and started looking at the issue like a researcher. I did not get as far as James Bond (see above) but I'll tell you what I did find. This is how my setup works now, it's relatively simple, and I might simplify it even further later, as things start breaking more.

For one, I am running Arch, and somehow Arch decided that bluez5 is the default version of the bluetooth stack even though nothing really seems to work with it yet. Whatever, first step is to install bluez4, which is bluez 4.101 or something
along those lines. Next you need something to help you pair and connect to your headphones. I've heard you can use the command line for this, but the actual commands seem to change every few months so I haven't been able to get it to work. Instead I installed blueman. Now, I paired my headphones, it asked for a PIN, had to look it up and it turns most headphones use 0000 a their PIN. Next, in order to get them to connect I had to add
Enable=Sink,Source,Socket
in /etc/bluetooth/audio.conf. Connected like magic!

One big step done, we move on to getting sound through. For the longest time I used pulseaudio, which is supposed to work
out of the box with bluez. Blueman even has a nice plugin to switch the output to your headphones when you connect them and then switch it back when you disconnect. The problem with this setup is that everything is magic. I have no idea how any of this works, it's code that talks to other code on dbus in ways I'd have to spend a month or more to figure out. So, after having some issues with this and being able to figure out nothing, I completely ditched pulse and switched to good old ALSA. What this means is I added this file in my ~/.asoundrc:
pcm.btheadset {
   type plug
   slave {
       pcm {
           type bluetooth
           device 00:1D:BA:29:50:D3
           profile "auto"
           bitpool "31"
       }  
   }  
   hint {
       show on
       description "BT Headset"
   }  
}

ctl.btheadset {
  type bluetooth
}


You'll want to change the device to match the mac address that blueman gives you for your particular headphones. Everything else is pretty self explanatory except for profile (I have no idea what it does) and bitpool, which I will explain momentarily.

Having done the above you can check your also read it by connecting your headphones and running aplay -L. Your new bluetooth sink should come up. You can even get sound by running speaker-test -D btheadset. Not very pleasant, but it tests that things are working. Now you should be able to select the btheadset output in your media player of choice.

After all this hassle there is one thing left: sound quality. I was getting interruptions and hangs every few seconds with the default config. This is where the bitpool value comes in. I don't actually know what it means and what it does, but reading a bunch of forums I realized it might be helpful. It's not even documented very well... I had to read the source file in C to see that it was in fact set to some magic numbers at compile time by default. Anyway, I played around with it, and it seems to be some sort of quality vs. skips control. The higher the number the higher the chance of a skip, but the better the sound quality between skips. Make the number too low and you'll get phone quality on your headset. Since I listen to my music over ssh, and have it downsampled to 128bits anyway, I care not about high quality, so I probably made that number slightly smaller than I needed to, but I get absolutely no skips now. And that does it. This should hold me for a while, until bluez4 goes completely out the window and bluez5 forces us to use pulseaudio with its magic touch and cryptic error messages that nobody understands... Anyway, I leave you off with a track from the new Cynic album. Enjoy!

20 comments:

  1. Spy Bluetooth Earpiece in Alwar especially for students to crack out their competitive exam & secure highest marks. For more information
    Spy Bluetooth earpiece dealers

    ReplyDelete
  2. Nice article on Bluetooth Headset . It is very easy to handle. They have good audio quality so that we can enjoy music. Thanks for sharing this article.

    ReplyDelete
  3. Awesome post it was !! hope you will keep continue it .. and also visit Credit Repair in Federal Way

    ReplyDelete
  4. Great post !! Thanks for sharing with us !!
    Ans also visit Circuloncanada.ca and get more information about Cookware sets

    ReplyDelete
  5. Awesome post !Thanks for sharing it, its very useful article for me . :) and also visit www.thegoodtimber.com and get detailed information about log home furniture . Try us now.

    ReplyDelete
  6. Nice post! Thanks for sharing it with us . and also visit beknownworld.com and get more information about product development

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Awesome post!! Thanks for sharing it with us .And also visit www.kineticdrivingschool.co.uk and get more information about driving lessons rochester .

    ReplyDelete
  9. Very nice article! Thanks for sharing . I hope you will keep continue it .And also visit us and get information about Driving instructor training in Medway

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. Great post ! Thanks for sharing with us ! Hope you will keep continue it . And also Visit Silvercreekonlineclinic.com and get detailed information about Online medical treatment

    ReplyDelete
  12. This is very well written up article ! Thanks for sharing with us ! And also Visit www.iwebdev.com.au and get information about Mobile app development services

    ReplyDelete
  13. What a concept! Thanks for sharing with us hope you will keep continue it. Visit Joshuaperets and get info about Girls Graphic Tees Online

    ReplyDelete
  14. Nice article !Keep sharing such useful things with us . And also Fashion Scene offers mother of a bride outfits in Kent .Dresses you can ware again and again .For further information visit us !

    ReplyDelete
  15. I find this article very interesting. thanks for sharing the information.
    And visit confidential banking for more information about Merchant processing service

    ReplyDelete
  16. When it comes to full sized over ear noise cancelling headphones, the market is saturated with more name brands than you can imagine. Today we look at the PSB M4U noise cancelling headphones to see how they compare both in sound quality, noise cancelling effectiveness, durability and ergonomics. Do the PSB's sound. latestone.com but quite a bit of attention has been paid to sound quality via software.

    ReplyDelete
  17. Nice article about bluetooth headsets. If you are one who want to known more about headphone, earphones then visit : www.vplak.com

    ReplyDelete
  18. Jesus Christ, the amount of spam here is fucking obnoxious. Anyway, I didn't know that ALSA supported bluetooth at all. Good to know! Thanks for writing this up :)

    ReplyDelete
  19. Click on the link below to shopping online of huion graphic tablet -
    Huion kamvas
    Huion pen tablet

    ReplyDelete