[linux-audio-user] Opening Stereo 8-bit 8-K Audio Streams

Martin McCormick martin at dc.cis.okstate.edu
Mon Jan 8 14:38:53 EST 2007


	I wrote a VOX or Voice Actuated Relay recording program
about 4 years ago and have been adding to it.  It occurred to me
that I may be able to get 2 VOX's for the price of one by opening
/dev/dsp in stereo mode instead of the straight /dev/dsp mode I
have been using.  This should give me a 16-bit value each time
with half of it being the left channel audio sample and the other
half being the right channel.  To cut to the chase, I can't seem
to open /dev/dsp in this manner.  I found some 90's era
documentation about oss-sound and Linux audio.  Of course, there
is mention of /dev/dspw (my salvation!).  As with many older and
direct  methods, it no longer exists.  At least modern Debian
systems running the 2.6.5 kernel with ALSA don't have it so there
must be another way to do it.

	Another 90's document has a snippet of code using ioctl
and it compiles, but I will show you what happens.

	Here is what blows up.  The source has the following
includes:

#include <stdio.h>
#include <signal.h>
#include	<ctype.h>
#include <sys/types.h>
#include <sys/soundcard.h>
#include <sys/ioctl.h>
       #include <time.h>
       #include <unistd.h>

	This next variable is defined inside main

int format = 0;

The audio device file descriptor is defined as

FILE *audio_fd;

	The part of the code that fails is taken right from the
example.

/*Set 8-K stereo mode.*/
   format = AFMT_S16_LE;
/*On my system, that sets format to 16.*/

   if (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &format)==-1)
   { /* Fatal error */
   perror("SNDCTL_DSP_SETFMT");
   exit(1);
   }

	The failure appears to have nothing to do with sound
capabilities.  Here is what gdb shows when stepping through the
program.  /dev/dsp successfully opens first for reading so that
is no problem.  The very next thing I do is to try to use the
ioctl to set the number of channels.  That is the code you see
above.  Here is what happens when we get there:

Breakpoint 2, main (argc=2, argv=0xbffff684) at vx2ch.c:118
118        if (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &format)==-1)
(gdb) step
120        perror("SNDCTL_DSP_SETFMT");
(gdb)
SNDCTL_DSP_SETFMT: Bad file descriptor
121        exit(1);

The file descriptor being passed to SNDCTL_DSP_SETFMT is
audio_fd.  If I take all this code out, you have the original
program which runs just fine but only uses straight /dev/dsp
(8-bit, 8-K mono).  I'd stick the whole source in here, but it's
322 lines and all of it works except what I have demonstrated.

	With  alsa installed, what is the best practice for
opening some version of /dev/dsp for two 8-K 8-bit streams such
that you get a 16-bit stream?

	Thanks for any suggestions or pointers to newer
documentation.  

Martin McCormick WB5AGZ  Stillwater, OK 
Systems Engineer
OSU Information Technology Department Network Operations Group


-- 
To UNSUBSCRIBE, email to debian-user-REQUEST at lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact listmaster at lists.debian.org




More information about the Linux-audio-user mailing list