Python – Hooking into a wave-out on different platforms


I am going to apologize in advance for being extremely vague, but my knowledge in this area is somewhat limited so I don't know the neccessary "keywords" to make my point/question clear. Sorry.

What I want to do is to find a way to obtain access to raw audio data as it is being output, say, when some external application is playing back music, when data is passed from the microphone to the output device and so on. I want to have access to that data in real time (well, almost real time, anyway), so that I can analyze/visualize it, etc.

I know, that it is certainly possible, as I've done it a long time ago in Java (I think I was using Processing and Minim libraries). This was done in windows by enabling a sort of "loopback" input device on the sound card (I think my vendor called it "Wave Out Mix" or something). How would one approach the same problem under a Linux-based OS?

I intend to use python for the analyzing/visualization part, however I can work in C if it is neccessary to get to the raw data.

Thanks in advance for any information.

Best Solution

Whether you can access the "wave out" or "loopback" depends on your sound card and drivers.

The native sound API on Linux is called ALSA. Search for the ALSA docs and sample code and you should be able to get some code to record from your sound card, then hopefully set up your mixer so that you're recording from "wave out" instead of from the microphone.

The older Linux sound API (/dev/dsp) is called OSS. It's slightly simpler and ALSA emulates most of this API - but ALSA is the preferred solution if you want full access to your sound card.

You may want to check out Jack - it's a system for routing the audio output from one application to the input of another, in a big chain or all sorts of other configurations. There are lots and lots of compatible programs, and if everything you need already supports Jack, then you'll find it's by far the most straightforward API for this type of thing.

Finally, this may sound stupid, but you can get an analog loopback cable - a male-to-male stereo mini jack - and loop it from your headphone jack to your line in jack, and record "wave out" that way, no matter what soundcard you have. It's a stupid hack with an analog hole, but it does work, and depending on what you're trying to record, it may be good enough.