blogs tagged "linux"

ALSA recording of device output

Sat Jan 02 2016 23:54:31 GMT-0800 (Pacific Standard Time)

tags: linux audio sound alsa looprec

On more than a few occasions, I've wanted to be able to software-record the sound coming out of my computer speakers.  Many users faced with similar problems resort to using pulseaudio as their sound system, which is reasonble, because it provides a very extensible/pluggable framework for sound.  Unfortunately, my experience with pulseaudio in the past has been "meh", probably due in large part to my heavy use of Pd.  So I've stuck with ALSA through the years when doing simple stuff, resorting to jack when doing more complicated routing between applications.  Simply recording what's playing seems simple enough...right?  Not so much...

I guess some (nicer?) sound cards provide a built-in hardware recording channel that can mix back in the currently playing audio.  Most built-in ones, like the one in my aging T410, do not.  After some sleuthing, I discovered that ALSA's plugin system does, in fact, provide a way to do this.  I'll describe the process here, but it's basically ripped from this thread where kokoko3k serves up the right approach:  https://bbs.archlinux.org/viewtopic.php?id=147852

There's an ALSA kernel module called snd_aloop that "provides a pair of cross-connected devices, forming a full-duplex loopback soundcard".  With just a little fiddling, you can create a "looprec" device that has loops back the audio output into a new recordable ALSA device.  The steps, just like in the above-mentioned post, are:

  1. $ sudo modprobe snd_aloop
    (this inserts the relevant kernel module into the kernel)

  2. create/edit ~/.asoundrc and paste in the following (a bit of alsa black magic):

    pcm.!default {
      type asym
      playback.pcm "LoopAndReal"
      #capture.pcm "looprec"
      capture.pcm "hw:0,0"
    }
    
    pcm.looprec {
        type hw
        card "Loopback"
        device 1
        subdevice 0
    }
    
    pcm.LoopAndReal {
      type plug
      slave.pcm mdev
      route_policy "duplicate"
    }
    
    pcm.mdev {
      type multi
      slaves.a.pcm pcm.MixReale
      slaves.a.channels 2
      slaves.b.pcm pcm.MixLoopback
      slaves.b.channels 2
      bindings.0.slave a
      bindings.0.channel 0
      bindings.1.slave a
      bindings.1.channel 1
      bindings.2.slave b
      bindings.2.channel 0
      bindings.3.slave b
      bindings.3.channel 1
    }
    
    pcm.MixReale {
      type dmix
      ipc_key 1024
      slave {
        pcm "hw:0,0"
        rate 48000
        #rate 44100
        periods 128
        period_time 0
        period_size 1024 # must be power of 2
        buffer_size 8192
      }
    }
    
    pcm.MixLoopback {
      type dmix
      ipc_key 1025
      slave {
        pcm "hw:Loopback,0,0"
        rate 48000
        #rate 44100
        periods 128
        period_time 0
        period_size 1024 # must be power of 2
        buffer_size 8192
      }
    }
    

That's it!  Your recording software should now have a device available called "looprec", and if you record from it you'll get whatever is playing on your speakers.  You can make this permanent by adding the snd_aloop module to /etc/modprobe.d/sound.conf.

Since you've made it this far, I'll share what I was trying to record:  http://websdr.ewi.utwente.nl:8901 -- which is pretty much the raddest thing ever.

MPlayer with bluetooth headphones

Thu Jan 27 2011 21:39:39 GMT-0800 (Pacific Standard Time)

tags: bluetooth bluez alsa linux mplayer headphones audio

Hmmm. Way too complicated probably, but if you can actually get the device to pair and sink it as an audio device, then you can do this: mplayer -ao alsa:device=bluetooth and it may or may not require ~/.asoundrc that looks like

pcm.bluetooth {
    type bluetooth
}

I heard audio at least one time from my headphones via mplayer that way...at least before the batteries ran out. Open question: Why doesn't it just show up like a normal alsa device? Meh.

T410 Linux audio volume problem

Fri Oct 22 2010 22:57:32 GMT-0700 (Pacific Daylight Time)

tags: hda_intel alsa linux audio volume conexant CX20585

Ok, enough is enough. You gotta help me out. I can't be the only person running Linux on a Lenovo Thinkpad T410 (Conexant CX20585) that has audio volume problems. How is it that I've owned this machine for 6+ months and have tried kernel upon kernel, scoured message boards, compiled alsa from trunk, tweaked endless permutations of snd-intel-hda kernel module options, yet still can't get a reasonable audio volume from this Intel HDA audio card on my T410!? It's unthinkable! I know, I know, the driver is still relatively "new", and the good work that has been done has been based on hacking some undocumented codecs without support from the manufacturers...but come on! Output volume is critical! You see, sound works just fine...but the output power is lacking. Something, somewhere, is preventing the output amplifier from being turned on or otherwise being configured correctly. I have to crank the alsa master and pcm output to 100% for music or movies to even be listenable, and even then it's not quiet loud enough. Thinkpads are still considered "good", Linux friendly laptops, so how is it that people are living with this problem? Certainly I can't be the only one! I've tinkered with hda-analyzer and hda-verb, and have some vague indication that an output amplifier is muted, but there's no apparent way to enable it. I guess its time to dig in and try and grok the source...but I kinda dread it. Don't get me wrong, I love hacking source and learning and trying things, but sometimes I just want stuff to work. I assumed 5 or 6 years ago that the "oooooh, audio on Linux is so hard" problem had been solved...but I guess I was wrong. After so many years of ALSA "just working" and doing what I needed, I guess it is actually a problem again...and that sucks. If you have any insights into this issue or want to steer me down a path, please send me an email...otherwise wish me luck.

Make sound on Fluxbox volume changes.

Wed Apr 01 2009 23:38:20 GMT-0700 (Pacific Daylight Time)

tags: msi msiwind wind fluxbox debian linux sound audio

scream that sound

I recently acquired a certain netbook so that I could be frugal but still have the ability to hack while mobile. Of course, I have to run Debian...it's a curse...but it rules.

Surprisingly, things work altogether very very well. I still don't think I'm that savvy, but I've gotten things to play together quite nicely. I will create a real page and post my results/settings/findings soon....but I made a stupid simple hack tonight that I thought I'd share.

So I'm running Fluxbox again. It's clean, simple, and takes up very little screen real-estate. It's been a few years, but it's as elegant and non-intrusive as I remember it.

The default keys file for fluxbox has commands that grok asla (via alsactl) and are able to nudge the volume up/down based on the magical function modifier keys (like Fn+F7 or Fn+F8 on the Wind). That's all great...but at least with the sound apps I run (including Flash!), the defaults aren't enough because this "Front" control keeps stomping on things (eg. it apparently needs to be massaged after the Master is massaged). I wish this wasn't the case, it's stupid, I should complain...Whatever.

Scripts to the rescue.

For the MSI Wind, I made three scripts, one does muting, the other two do volume up/down. They look something like this:

#!/bin/bash
for control in Master Front ; do
        amixer sset ${control},0 1+
done
aplay ~/media/sounds/volume_change.wav

The 1+ indicates volume up, change to 1- for volume down.

And then in ~/.fluxbox/keys we can map things like:

176 :Exec ~/bin/volume_up
174 :Exec ~/bin/volume_down
160 :Exec ~/bin/volume_mute

What this provides is two things. First, when you press Fn+F7, for example, the Master and Front volume levels will both decrement. The Front is changed last, so that the actual volume output level is applied. Secondly, the 'aplay' line gives a little chime to audibly indicate the volume level...something akin to the sound that those $2000 Macintoshes make.

I found this bink sound on freesound and think it works quite well.

Until I can create a real page for my Wind setup, scream loud and enjoy the hack.

Open source WIN

Wed Feb 25 2009 22:49:58 GMT-0800 (Pacific Standard Time)

tags: linux sanyo katana foss

Another victory for open source. I've been trying to stay fairly cutting edge again at home -- with my Debian unstable and pretty up-to-date kernel builds. I'm usually surprised at just how easy things are now...

sanyo phone

But my crappy little phone stopped automounting for some reason when I plugged it in. I managed to search and find this bug thread which suggested a similar problem and a patch for Nokia phones. A few minutes later I had downloaded the patch, applied it, and then tweaked it to match the manufacturer/product IDs of my silly phone.

It ended up looking something like this:

UNUSUAL_DEV(  0x0474, 0x0749, 0x0000, 0x0481,
        "Sanyo",
        "Sanyo Mass Storage",
        US_SC_DEVICE, US_PR_DEVICE, NULL,
        US_FL_FIX_CAPACITY),

One kernel compile and reboot later and it worked like a champ.

Of course, all this begs the question "Why did it happen in the first place?"...which is another topic completely. Having the ability to take ownership and fix the problem to get work done (until the upstream [kernel]) is priceless. You'll never get that same experience with your closed source OS.

Inkscape for great success.

Sun Mar 04 2007 22:52:21 GMT-0800 (Pacific Standard Time)

tags: linux software graphics vector svg

I've been getting into and exploring with Inkscape lately. Although I was first tipped off to it a couple (?) years ago (now already?), I hadn't actually installed/tried it until recently. Now that I have put it through some test runs, I'm quite impressed! Of course the main idea (at least to me), is to be able to draw nice bezier curves and have them be smooth and be able to arbitrarily scale them and to edit them later (which you can't do with traditional bitmap/raster editing software...even thought Photoshop and Gimp have incorporated "paths"). It does this beautifully...things are highly intiutive, I got up to speed pretty quickly I think...and although I hit the occasional quirk, it did just what I wanted. As with other software, like Gimp, I'm sure the Adobe design zealots will soon start ranting about how inferior it is and how it lacks all these great features that product has...but whatever...I've never claimed to be a designer, but I can recommend it. I used Inkscape to draw up a series of logos for DorkbotPDX and for the PDXbot event this year. You can view them on my dorkbot subpage. A week or so ago I tracked down the LM709 op-amp from HSC Electronics and hacked up a cracklebox of my very own. I haven't put up a proper page for it yet, but I have some content online and will bang a page out soon. Do superworms molt? I've got a bin of 20 of them in oatmeal and I've been adding potato chunks, but I'm worried that they're not doing well. I once read that they'll eat each other if they get desperate, and I've seen what now look like carcases, but it might just be exoskin or something. I haven't even had time to really take care of them properly, which also implies that I haven't yet had time to record them.