How to write the code for audio loopback in linux

Programming /dev/dsp

(Obsolute,But latest oss still support the old versions)

/dev/dsp is the digital sampling and digital recording device, and probably the most important for multimedia applications. Writing to the device accesses the D/A converter to produce sound. Reading the device activates the A/D converter for sound recording and analysis.

The name DSP comes from the term digital signal processor, a specialized processor chip optimized for digital signal analysis. Sound cards may use a dedicated DSP chip, or may implement the functions with a number of discrete devices. Other terms that may be used for this device are digitized voice and PCM

Some sounds cards provide more than one digital sampling device; in this case a second device is available as /dev/dsp1. Unless noted otherwise, this device operates in the same manner as /dev/dsp

The DSP device is really two devices in one. Opening for read-only access allows you to use the A/D converter for sound input. Opening for write only will access the D/A converter for sound output. Generally speaking you should open the device either for read only or for write only. It is possible to perform both read and write on the device, albeit with some restrictions; this will be covered in a later section.

Only one process can have the DSP device open at a time. Attempts by another process to open it will fail with an error code of EBUSY. 

Reading from the DSP device returns digital sound samples obtained from the A/D converter.  Analog data is converted to digital samples by the analog to digital converter under control of the kernel sound driver and stored in a buffer internal to the kernel. When an application program invokes the read system call, the data is transferred to the calling program’s data buffer. It is important to understand that the sampling rate is dependent on the kernel driver, and not the speed at which the application program reads it. 

When reading from /dev/dsp you will never encounter an end-of-file condition. If data is read too slowly (less than the sampling rate), the excess data will be discarded, resulting in gaps in the digitized sound. If you read the device too quickly, the kernel sound driver will block your process until the required amount of data is available.

The input source depends on the mixer setting (which I will look at shortly); the default is the microphone input. The format of the digitized data depends on which ioctl calls have been used to set up the device. Each time the device is opened, its parameters are set to default values. The default is 8-bit unsigned samples, using one channel (mono), and an 8 kHz sampling rate.

 Writing a sequence of digital sample values to the DSP device produces sound output. Again, the format can be defined using ioctl calls, but defaults to the values given above for the read system call (8-bit unsigned data, mono, 8 kHz sampling).

If the data are written too slowly, there will be dropouts or pauses in the sound output. Writing the data faster than the sampling rate will simply cause the kernel sound driver to block the calling process until the sound card hardware is ready to process the new data. Unlike some devices, there is no support for non-blocking I/O.

 If you don’t like the defaults, you can change them through ioctl calls. In general you should set the parameters after opening the device, and before any calls to read or write. You should also set the parameters in the order in which they are described below.

All DSP ioctl calls take a third argument that is a pointer to an integer. Don’t try to pass a constant; you must use a variable. The call will return -1 if an error occurs, and set the global variable errno.

 If the hardware doesn’t support the exact value you call for, the sound driver will try to set the parameter to the closest allowable value. For example, with my sound card, selecting a sampling rate of 9000 Hz will result in an actual rate of 9009 Hz being used.

If a parameter is out of range, the driver will set it to the closest value (i.e., the upper or lower limit). For example, attempting to use 16-bit sampling with an 8-bit sound card will result in the driver selecting 8 bits, but no error will be returned. It is up to you, the programmer, to verify that the value returned is acceptable to your application.

 All of the ioctl calls for the DSP device are names starting with SOUND_PCM. Calls in the form SOUND_PCM_READ_XXX are used to return just the current value of a parameter. To change the values, the ioctl calls are named like SOUND_PCM_WRITE_XXX. As discussed above, these calls also return the selected value, which is not necessarily the same as the value passed to the sound driver.

The ioctl constants are defined in the header file linux/soundcard.h. Let’s examine each of them in detail.

 SOUND_PCM_WRITE_BITS

Sets the sample size, in bits. Valid choices are 8 and 16, but some cards do not support 16.

SOUND_PCM_READ_BITS

Returns the current sample size, which should be either 8 or 16 bits.

SOUND_PCM_WRITE_CHANNELS

Sets the number of channels–1 for mono, 2 for stereo. When running in stereo mode, the data is interleaved when read or written, in the format left-right-left-right…. Remember that some sound cards do not support stereo; check the actual number of channels returned in the argument.

SOUND_PCM_READ_CHANNELS

Returns the current number of channels, either 1 or 2.

SOUND_PCM_WRITE_RATE

Sets the sampling rate in samples per second. Remember that all sound cards have a limit on the range; the driver will round the rate to the nearest speed supported by the hardware, returning the actual (rounded) rate in the argument. Typical lower limits are 4 kHz; upper limits are 13, 15, 22, or 44 kHz.

SOUND_PCM_READ_RATE

Returns just the current sampling rate. This is the rate used by the kernel, which may not be exactly the rate given in a previous call to SOUND_PCM_WRITE_RATE, because of the previously discussed rounding.

Sample Application

#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 8000   /* the sampling rate */

#define SIZE 8      /* 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 */

  /* open sound device */

  fd = open(“/dev/dsp”, O_RDWR);

  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”);

  while (1)

  { /* loop until Control-C */

       printf(“Say something:n”);

       status = read(fd, buf, sizeof(buf)); /* record some sound */

       if (status != sizeof(buf))

           perror(“read wrong number of bytes”);

       printf(“You said:n”);

       status = write(fd, buf, sizeof(buf)); /* play it back */

        if (status != sizeof(buf))

         perror(“wrote wrong number of bytes”);

        /* wait for playback to complete before recording again */

       status = ioctl(fd, SOUND_PCM_SYNC, 0); 

        if (status == -1)

            perror(“SOUND_PCM_SYNC ioctl failed”);

  }

 }

 Enhancements

Try compiling and running this program. Then make some enhancements:

  1. Make the parameters selectable using command-line options (sample rate, size, time). See the effect on sound quality with different sampling rates.
  2. Reverse the sound samples (and listen for hidden messages), or play them back at a different sampling rate from the one at which they were recorded.
  3. Automatically start recording when the voice starts and stop when silence occurs (or a maximum time is reached). Hints: for 8-bit unsigned data the zero value is 0x80, but you will likely see values that vary around this level due to noise. Set a noise threshold (or better yet, measure the background noise level at the start of the program).
  4. Bonus question: modify the program so that it can recognize the words that are spoken.

Comments