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

gcc using Pipes Done!



lee writes:
> And leftchannel_pipe is supposed to be an array?  Without seeing more of
> your program, we're left in the dark ...

	Now that I got it working, here it is. All this does is
to give you an 8-bit 8000 sample per second audio feed from both
the left and right channels of the sound card as
/tmp/leftchannel and /tmp/rightchannel. For it to work, you must
cat both files in to either a program that continuously reads
them or cat both in to respective files because the fifo I ended
up using instead of pipes blocks if you stuff bytes in to the
input and there is nothing to read the fifo yet.

	If you start this code without anything connected to
both fifos, it patiently blocks until both streams are going,
then it works fine.

	There is a catch as to the type of audio you can use
with this code because it actually has to configure the sound
card to sample in stereo at 32000 samples per second. The
Nyquist low-pass filter needed on both the input and output of
the sound card is set to block sounds above 16000 HZ but the
sound streams we want can only carry audio at 4000 HZ. I do not
have any kind of Nyquist filtering built in to this code so the
best audio to feed it is audio from radio scanners,
communications receivers and any other device with a limited
audio bandwidth so that the low-pass filtering is already there.
Do anything else such as feeding music from a FM broadcast radio
or television sound source in to the splitter gives you sound
that is peculiar to listen to because frequencies above 4 KHZ
heterodyne with the sampling rate and produce what are called
aliases. Trust me. S sounds sound like tearing paper and chimes
or bells put one in mind of the world through a bad ear ache.

	Here is the splitter code. I used sample code from
others to learn how to setup the sound card. Many thanks to all.

/*
Create 2 independent 8-bit sound streams
*/
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <linux/soundcard.h>

#define RATE 32000   /* the sampling rate */
#define rightchbits 0b11111111111111110000000000000000 /*all left channel bits*/
#define highleftchbits 0b00000000000000001111111100000000 /*all high left channel bits*/
#define highrightchbits 0b11111111000000000000000000000000 /*all left channel bits*/
#define leftchbits 0b00000000000000001111111111111111 /*all left channel bits*/
#define SIZE 16      /* sample size: 8 or 16 bits */
#define CHANNELS 2  /* 1 = mono 2 = stereo */

signed long int sample = 0;
signed long int prevsample = 0;
signed long int rawleft = 0;
signed long int rawright = 0;
signed long int prevleft = 0;
signed long int prevright = 0;
signed long testsample = 0;
void cleanup();
int main(int argc, char **argv)
{ /*main start*/
  int fd;       /* sound device file descriptor */
  int arg;      /* argument for ioctl calls */
  int status;   /* return status of system calls */
signed long lefthighbytestore = 0;
signed long righthighbytestore = 0;
signed long highbytestore = 0;
unsigned char leftbyte = 0;
unsigned char rightbyte = 0;
char rightswitch = 0;
char *leftchannel_fifo = "/tmp/leftchannel";
char *rightchannel_fifo = "/tmp/rightchannel";
/*Code starts*/
/*Get rid of any old fifos.*/
cleanup();
  /* open sound device */
  fd = open("/dev/dsp", O_RDONLY);
  if (fd < 0) {
    perror("open of /dev/dsp failed");
    exit(1);
  }
  /* set sampling parameters */
  arg = SIZE;      /* sample size */
  status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);
  if (status == -1)
    perror("SOUND_PCM_WRITE_BITS ioctl failed");
  if (arg != SIZE)
    perror("unable to set sample size");

  arg = CHANNELS;  /* mono or stereo */
  status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg);
  if (status == -1)
    perror("SOUND_PCM_WRITE_CHANNELS ioctl failed");
  if (arg != CHANNELS)
    perror("unable to set number of channels");

  arg = RATE;      /* sampling rate */
  status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);
  if (status == -1)
    perror("SOUND_PCM_WRITE_WRITE ioctl failed");

if (mkfifo( leftchannel_fifo, 0600) < 0) {
  if (errno == EEXIST)
    printf("Message Queue (i.e. FIFO) %s already exists\n", leftchannel_fifo);
  else {
    perror("Unable to create UNIX FIFO\n");
    return -1;
}
}
if (mkfifo( rightchannel_fifo, 0600) < 0) {
  if (errno == EEXIST)
    printf("Message Queue (i.e. FIFO) %s already exists\n", rightchannel_fifo);
  else {
    perror("Unable to create UNIX FIFO\n");
    return -1;
}
}
 if ((leftchannel_fifo = fopen(leftchannel_fifo,"w")) == NULL) {
  perror("/tmp/leftchannel");
  exit(1);
 }
 if ((rightchannel_fifo = fopen(rightchannel_fifo,"w")) == NULL) {
  perror("/tmp/rightchannel");
  exit(1);
 }
  while (1) { /* loop until Control-C */
    status = read(fd,& sample, sizeof(sample)); /*Get sample.*/
if (!(rightswitch & 3))
{/*process every 4TH sample.*/
/*This is how we get back to 8000 samples per second.*/
/*Start process here.*/
rawleft = (sample & leftchbits);
rawright = (sample & rightchbits);
righthighbytestore = (sample & highrightchbits);
lefthighbytestore = (sample & highleftchbits);
	rightbyte = righthighbytestore>>24;
	leftbyte = lefthighbytestore>>8;
/*Go from signed to unsigned.*/
leftbyte = leftbyte^0x80;
rightbyte = rightbyte^0x80;
/*Stuff in to the fifo's.*/
fwrite(&leftbyte,sizeof(leftbyte),1,leftchannel_fifo);
fwrite(&rightbyte,sizeof(rightbyte),1,rightchannel_fifo);
}/*process every 4TH sample.*/
rightswitch++;
} /*read loop*/
} /*main end*/
void cleanup()
{ /*Remove the Pipes.*/
	char command[256];
   sprintf (command,"rm -f /tmp/leftchannel /tmp/rightchannel");
   system (command);
return;
} /*Remove the Pipes.*/


Reply to: