[LAD] reading and writing to /dev/dsp

Girish Hilage girish_hilage at persistent.co.in
Thu Jul 23 10:09:34 UTC 2009


Hi,

I am running a .mp3 file using 'xine' without any issues on my Fedora
Core 8 machine.

I am reading /dev/dsp and storing it in some file.
i.e. cat /dev/dsp > musicfile

Now I am stopping 'xine' and then writing 'musicfile' to /dev/dsp.
i.e. cat musicfile > /dev/dsp

In this case I can hear the music but with a lot of noise in it.
Is there any way I can remove/reduce this noise from 'musicfile'?

I also tried following program (it records the voice and plays it
immediately) that I got from net to understand how to write to /dev/dsp
but it does not seem to record my voice and hence plays nothing and
there is noise only.

-------------------------------------------------------
#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");
    }
}
---------------------------------------------------------------------------------------
Regards,
Girish

DISCLAIMER
==========
This e-mail may contain privileged and confidential information which is the property of Persistent Systems Ltd. It is intended only for the use of the individual or entity to which it is addressed. If you are not the intended recipient, you are not authorized to read, retain, copy, print, distribute or use this message. If you have received this communication in error, please notify the sender and delete all copies of this message. Persistent Systems Ltd. does not accept any liability for virus infected mails.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linuxaudio.org/pipermail/linux-audio-dev/attachments/20090723/c7921271/attachment.html>


More information about the Linux-audio-dev mailing list