Chello,

I am quite new to programming in Linux and have encountered the first problem.

I have been playing a little bit with the sound device /dev/dsp. Everything works well, and I can record sounds and replay them. But that is actually not what I want. I want to read the signals and analyse them and - let's assume - take some action when the surrounding sound gets too loud.

However, I am not able to interpret the signals correctly.
I have the following code in which I print the values that I receive from my microphone.
But the values only spin around and never become really high even when I scream into my microphone.

If someone could help me I would really appreciate that.

Here's the code:

Code:
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <stdio.h>
#include <linux/soundcard.h>

#define LENGTH 3    /* how many seconds of speech to store */
#define RATE 44000   /* the sampling rate */
#define SIZE 16      /* sample size: 8 or 16 bits */
#define CHANNELS 1  /* 1 = mono 2 = stereo */

/* this buffer holds the digitized audio */
unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8];

int main()
{
  int fd;	/* sound device file descriptor */
  int arg;	/* argument for ioctl calls */
  int status;   /* return status of system calls */

  unsigned short sig;

printf("%d\n", sizeof(sig));

  /* open sound device */
  fd = open("/dev/dsp", O_RDONLY);
  if (fd < 0) {
    perror("open of /dev/dsp failed");
    exit(1);
  }


 arg = AFMT_U16_LE;
 ioctl(fd, SOUND_PCM_SETFMT, &arg);

  /* 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");



  ioctl(fd, SOUND_PCM_READ_BITS, &arg);
printf("%d\n",arg);
 

  while (1) { /* loop until Control-C */
	read(fd,&sig,1);
	printf("%d\n",sig);

 

}
}