[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

gcc using Pipes Some Progress



Ivan Jager writes:
> Rather than using fwrite() you could just use write(), which takes a
> file descriptor. Alternately, if you really want to use fwrite or
> other stdio functions rather than the *nix syscalls, you could
> use fdopen() to get a FILE* corresponding to the pipe's fd.
> 
> If you check the pipe manpage, you'll find an example of how to
> use pipes.

	What I ended up doing was to use write as you suggested.
I had confused the file descriptor with the file so write was
what I really needed. In the write command, I forgot the
"address of" & operator so once I fixed that I had:

                if( write(leftchannel_pipe[1], &leftbyte, 1) < 0)
                {
                        printf("write error!\n");
exit (1);
                }

	That appears to work but I need the pipe to show up as a
newly-created file so that a separate program can attach to it
and that's where it looks like I need fdopen. In the case of
this particular program, the pipe function gave me 7 and 8 as
the descriptors for leftchannel_pipe[0] and leftchannel_pipe[1]
so I added the following line to the code:

 if ((leftchannel_pipe[0] = fdopen(leftdata,"r")) == NULL) {
  perror(leftdata);
  exit(1);
 }

Leftdata is a string set to "leftchannel" and should have been
the file name that shows up when the code runs.

	I may be misunderstanding how this all works, but it
should have opened the file discriptor already created by the
pipe() function.

	At that point, I get the "bad file descriptor" error.


	One interesting point. the man page for pipe on my
debian lenny system exists in section 7 and discusses pipes and
fifo's but there is no example. I did, however, find an man page
by doing a Google search which did include an example of a
program that uses fork to let data flow from whatever wrote it
to whatever needs to read it.

	I think I am on the wrong track, here, because I want to
run this code and see 2 new entries pop up in the file system
which are each data streams, one to the left channel of my sound
card and the other, to the right channel.

mkfifo is probably what I need.

	Except for the I/O, this project is almost done. For the
curious, I wrote some code years ago that uses /dev/dsp which,
when read or written without any fcntl commands, either produces
or plays 8-bit audio samples at 8,000 samples per second. It's
horrible for music, but fine for communications-level audio such
as from short wave radios, scanner receivers and amateur radio
gear.

	You only get the input from the left channel, however.
If you also want the right channel, you have to set the sound
card to stereo at 32,000 16-bit samples per second so to get the
8-KHZ samples, you must throw away 3 out of 4 samples and then
mask off the 8 LSB's from each channel to get the same thing as
before plus change from signed to unsined data. I actually got
that to work so what's left is to build the I/O or the O, to be
specific so that this code is sort of a railroad switch to route
all the left-channel data and right-channel data to their
respective outlets.

	Finally, I wrote a VOX or Voice Activated Relay program
that takes one stream and only writes to a file when there is
sound. When I get the splitter to work, I will be able to put a
VOX on each channel and double the number of streams to record
from.


Reply to: