How to write the code for audio loopback in linux

How to write the code for  audio loopback in linux

/* Standard Linux headers */

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <sys/ioctl.h>

#include <fcntl.h>

#include <signal.h>

#include <getopt.h>

#include <pthread.h>

#include <errno.h>

 

#include <linux/soundcard.h>

 

#define FAILURE            -1

/* The number of channels of the audio codec */

#define NUM_CHANNELS           2

 

/* The sample rate of the audio codec */

#define SAMPLE_RATE            8000

 

/* The gain (0-100) of the left and right channels */

#define LEFT_GAIN              100

#define RIGHT_GAIN             100

 

/* Number of samples to process */

#define NUMSAMPLES             160

 

 

/* Mono 16 bit */

#define RAWBUFSIZE             NUMSAMPLES * 2

 

 

/* Stereo 16 bit */

#define INPUTBUFSIZE           NUMSAMPLES * 2

 

unsigned short rawbuf[RAWBUFSIZE],inputbuf[INPUTBUFSIZE];

 

int main()

{

    int     vol         = LEFT_GAIN | (RIGHT_GAIN << 8);

    int     channels    = NUM_CHANNELS;

    int     sampleRate  = SAMPLE_RATE;

    int format = AFMT_S16_LE;

    int     soundFd;

    int     mixerFd;

    int     recMask;

 

 

    /* Open the sound device for writing */

    if ((soundFd = open(“/dev/dsp”, O_RDWR)) == -1) {

        printf(“Failed to open the sound device (/dev/dsp)n”);

        return FAILURE;

    }

 

    /* Set the sound format (only AFMT_S16_LE supported) */

    if (ioctl(soundFd, SNDCTL_DSP_SETFMT, &format) == -1) {

        printf(“Could not set format %dn”, format);

        return FAILURE;

    }

    printf(“error while setting the format no=%dn”,errno);

 

    /* Set the number of channels */

    if (ioctl(soundFd, SNDCTL_DSP_CHANNELS, &channels) == -1) {

        printf(“Could not set mixer to %d channelsn”, channels);

        return FAILURE;

    }

  printf(“error while setting the channels =%dn”,errno);

 

    /* Set the sample rate */

    if (ioctl(soundFd, SNDCTL_DSP_SPEED, &sampleRate) == -1) {

        printf(“Could not set sample rate (%d)n”, sampleRate);

        return FAILURE;

    }

 

    /* If we are sampling select the right capture device and volume */

    if ((mixerFd = open(“/dev/mixer”, O_RDONLY)) == -1) {

            printf(“Failed to open /dev/mixern”);

            return FAILURE;

   }

 

    recMask |= SOUND_MASK_MIC;

        if (ioctl(mixerFd, SOUND_MIXER_WRITE_RECSRC, &recMask) == -1) {

            perror(“Failed to ask mixer for available recorders — 1.n”);

            return FAILURE;

        }

 

        if (ioctl(mixerFd, SOUND_MIXER_READ_RECMASK, &recMask) == -1) {

            printf(“Failed to ask mixer for available recorders — 2.n”);

            return FAILURE;

        }

 

        //if ((recMask & SOUND_MASK_LINE) == 0) {

        if ((recMask & SOUND_MASK_MIC) == 0) {

            printf(“Line input not a supported recorder.n”);

            return FAILURE;

        }

 

        if (ioctl(mixerFd, SOUND_MIXER_READ_RECSRC, &recMask) == -1) {

            printf(“Failed to read the current recorder.n”);

            return FAILURE;

        }

 

        //recMask |= SOUND_MASK_LINE;

        recMask |= SOUND_MASK_MIC;

 

        if (ioctl(mixerFd, SOUND_MIXER_WRITE_RECSRC, &recMask) == -1) {

            printf(“Failed to set line input as recorder.n”);

            return FAILURE;

        }

 

        if (ioctl(mixerFd, SOUND_MIXER_READ_RECSRC, &recMask) == -1) {

            printf(“Failed to read the current recorder.n”);

            return FAILURE;

        }

 

        //if ((recMask & SOUND_MASK_LINE) == 0) {

        if ((recMask & SOUND_MASK_MIC) == 0) {

            printf(“Failed to read the current recorder.n”);

            return FAILURE;

        }

 

        if (ioctl(mixerFd, SOUND_MIXER_WRITE_IGAIN, &vol) == -1) {

            printf(“Failed to set the volume of line in.n”);

            return FAILURE;

        }

 

        close(mixerFd);

 

 

 

    printf(“Audio sample rate set to %dHzn”, SAMPLE_RATE);

int outputFd = open(“audio”,O_WRONLY | O_CREAT | O_TRUNC, 00644);

int i=0,numBytes;

    if (outputFd == -1) {

        printf(“Failed to open file for writingn”);

        exit(0);

    }

    while(i++<500)

    {

        numBytes=read(soundFd, inputbuf, INPUTBUFSIZE);

        if(numBytes==-1)

        {

            printf(“errorn”);

            exit(0);

            close(outputFd);

 

        }

        else{

            printf(“numBytes=%dn”,numBytes);

        }

        usleep(10);

        write(soundFd, inputbuf, INPUTBUFSIZE);

    //stereoToMono(inputbuf, rawbuf, NUMSAMPLES);

       if (write(outputFd, inputbuf, INPUTBUFSIZE) == -1) {

            printf(“Error writing the encoded n”);

        close(outputFd);

            exit(0);

        }

    }   

close(outputFd);

    close(soundFd);

 

    return 0;

}

Comments