MadCow42
Posts: 106
Joined: Sun Jul 01, 2012 12:48 am

writing directly to Pi's audio output?

Sat Aug 25, 2012 3:29 pm

I'm trying to output some very simple audio directly to the Pi's audio device, but having some "newbie" problems...

The Pi uses ALSA audio, so I can't simply write to /dev/audio (unfortunately).

/dev/snd/pcmC0D0p seems to be the "right" sound device, but apparently you can't just write to it like you can with /dev/audio on other Unix systems. If you do, you get an error "file descriptor in bad state". If I could just write to it like /dev/audio I'd be a happy camper.

Can anyone suggest a way to write directly to audio out? All I'm trying to output is simple square waves to left/right audio tracks, to be used as a timing signal. I've also been looking at tkSnack (through Python), but that's a very heavy way to do this, and I'm having issues doing independent stereo output - and it forces you to have a GUI, while I'm running headless.

And... I'm doing all this progamatically through Python.

Thanks for any suggestions!
Kevin.

toxibunny
Posts: 1382
Joined: Thu Aug 18, 2011 9:21 pm

Re: writing directly to Pi's audio output?

Sat Aug 25, 2012 4:20 pm

You might be better off using GPIO. The raspi audio is basically just GPIO anyway, or so I've heard...
note: I may or may not know what I'm talking about...

cleverca22
Posts: 5074
Joined: Sat Aug 18, 2012 2:33 pm

Re: writing directly to Pi's audio output?

Sat Aug 25, 2012 5:33 pm

try doing modprobe snd-pcm-oss as root
that will setup a kernel side emulated OSS device, routed to the ALSA device
on my system, it does create /dev/audio, but i havent tested to see if it actualy works

User avatar
PeterO
Posts: 6095
Joined: Sun Jul 22, 2012 4:14 pm

Re: writing directly to Pi's audio output?

Sat Aug 25, 2012 6:57 pm

There is some example code on the ALSA site at
http://www.alsa-project.org/alsa-doc/al ... ample.html

As you've found ALSA is not as simple as the old oss sound libraries, but it is not to hard to trim down that example to just the bits you need.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

MadCow42
Posts: 106
Joined: Sun Jul 01, 2012 12:48 am

Re: writing directly to Pi's audio output?

Sun Aug 26, 2012 2:38 am

Thanks for the suggestions everyone, at least I have some things to dig into deeper now. The snd-pcm-oss module trick works to create a /dev/audio device, but unfortunately my first test ended up locking up the Pi. I'll have to repeat that to see what's going on (now repeated: and it locked up again... :(

I'll also look at that ALSA info - thx! When I get it all working I'll post a follow up too. I did find a Python ALSA interface module that looks fairly light and will check that out too.

Kevin.

omenie
Posts: 103
Joined: Fri Dec 09, 2011 5:09 pm
Contact: Website

Re: writing directly to Pi's audio output?

Sun Aug 26, 2012 9:10 am

I have to date not had any success with ALSA - probably entirely my fault but I'm impatient, needed immediate results and moved on - but the OMX IL example in hello_audio works a treat and was for me a good place to begin. You could certainly take that example, drop it into a thread of its own and then insert a really simple 'poke to interface' abstraction to hide the implementation complexity.

MadCow42
Posts: 106
Joined: Sun Jul 01, 2012 12:48 am

Re: writing directly to Pi's audio output?

Mon Aug 27, 2012 1:38 pm

Just a small update for anyone else trying to play with direct sound generation:

I did find PyAlsaAudio, a module for interfacing from Python to the ALSA sound interface: http://pyalsaaudio.sourceforge.net

It built no problem on Raspbian, and seems to work quite fine. *** if anyone wants a code sample let me know *** It took a little while to work out the data formatting for proper output, and figure out the noise generated when the buffer under-runs.

However, for my needs there's a catch - every time you start outputting sound, and every time you finish outputting sound there is a "click" of noise on the audio channel. This is known and documented, and happens whenever the audio buffer runs out. For most sound needs this isn't an issue if you keep putting data into the output buffer. However, I was hoping to just create a "click" sound from time to time as a timing signal.

So, for it to work for my needs, I need to continually feed the buffer with data. The trick will be to keep the buffer from running out, but keep the amount of data in the buffer low enough that my "click" isn't delayed either. What a pain.

So - I may go the GPIO route after all. I've already done that on another board, but was trying to avoid it here because there's no room on the GPIO board I've already built for this part of my project. :(

Thanks again for the help all!
Kevin.

yeahbox
Posts: 36
Joined: Sun May 27, 2012 10:35 pm
Location: Manchester, UK

Re: writing directly to Pi's audio output?

Mon Aug 27, 2012 3:53 pm

You might want to check out portaudio http://www.portaudio.com which will abstract away the hardware layer. If ALSA behaves then it is quite nice to work with portaudio. The advantage is that your application could be developed and tested on another machine (win/mac/lin) using another soundcard. There is a very nice python interface for portaudio also called pyAudio http://people.csail.mit.edu/hubert/pyaudio/

You also might want to investigate using a ring buffer for your audio samples. This is not in the portaudio API but there are some examples in there that uses a ringbuffer.

toxibunny
Posts: 1382
Joined: Thu Aug 18, 2011 9:21 pm

Re: writing directly to Pi's audio output?

Mon Aug 27, 2012 4:51 pm

Maybe there's a way to revert the Pi audio jack back to GPIO. I wouldn't really know who to ask about that though.
note: I may or may not know what I'm talking about...

MadCow42
Posts: 106
Joined: Sun Jul 01, 2012 12:48 am

Re: writing directly to Pi's audio output?

Mon Aug 27, 2012 4:56 pm

toxibunny wrote:Maybe there's a way to revert the Pi audio jack back to GPIO. I wouldn't really know who to ask about that though.
Ooh... that would be very slick indeed, but would depend on the voltage/current output. I had to put in some big resistors when using a normal GPIO for output. I'll have to dig into that option a bit and see!

Kevin.

omenie
Posts: 103
Joined: Fri Dec 09, 2011 5:09 pm
Contact: Website

Re: writing directly to Pi's audio output?

Mon Aug 27, 2012 5:35 pm

The 'hello_audio' OMX IL example already does all you need - has a ring buffer (it's implicit, you just ask it to create N buffers) and it queries the FIFO read pointer to determine live real-time latency so things don't get delayed. I don't know how to bridge that into Python, I only ever use C++ ... but I'm sure someone else can help with that.

Return to “Graphics, sound and multimedia”