Reading/playing .wav files - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Reading/playing .wav files

Hi all,
A friend of mine gave me a C program that opens and plays .wav files. It follows:

#include
#include
#include
#include
#include

#define BUFSZ 4096

int main(int argc,char *argv[])
{
int devfd, sampfd;
int len,i;
char *dev="/dev/dsp";
unsigned char sampbuf[BUFSZ];
/* Open /dev/dsp/ */
if((devfd = open(dev,O_WRONLY)) < 0)
{
if(errno == EBUSY)
fprintf(stderr, "%s in use
", dev);
fprintf(stderr, "%s: %s
",dev,strerror(errno));
exit(EXIT_FAILURE);
}
if(argc==2)
{
if((sampfd=open(argv[1],O_RDONLY)) < 0)
{
perror(argv[1]);
exit(EXIT_FAILURE);
}
/* Read a block, write a block */
while((len=read(sampfd,sampbuf,BUFSZ)) > 0)
write(devfd,sampbuf,len);
/* Close the descriptors and get outta here */
close(sampfd);
}
close(devfd);
exit(EXIT_SUCCESS);
}

First off, I'm using Mandrake.

If I use the command syntax cc play.c -o play I get an executable file that is able to 'play' standard .wav files, i.e. play mywave.wav.
If I use the command syntax cc play.c I get a.out of course, but when I try to execute it as a.out mywave.wav it plays this horrible screaching noise. The original executable file still works but I can't copy it to another filename w/o the same problem happening, i.e. cp play hold, etc. It also does weird stuff like if I move between default consoles and try to play the file by calling the previously executed command by using the 'up' arrow keys it tells me when I hit enter that the /dev/dsp device is busy. Someone told me that he thinks that it has to do w/the argv, argc variables and that the a.out file is attempting to play itself. Any suggestions. Thanks.
jbowers9

Comments

  • abcabc Posts: 443Member
    : Hi all,
    : A friend of mine gave me a C program that opens and plays .wav files. It follows:
    :
    : #include
    : #include
    : #include
    : #include
    : #include
    :
    : #define BUFSZ 4096
    :
    : int main(int argc,char *argv[])
    : {
    : int devfd, sampfd;
    : int len,i;
    : char *dev="/dev/dsp";
    : unsigned char sampbuf[BUFSZ];
    : /* Open /dev/dsp/ */
    : if((devfd = open(dev,O_WRONLY)) < 0)
    : {
    : if(errno == EBUSY)
    : fprintf(stderr, "%s in use
    ", dev);
    : fprintf(stderr, "%s: %s
    ",dev,strerror(errno));
    : exit(EXIT_FAILURE);
    : }
    : if(argc==2)
    : {
    : if((sampfd=open(argv[1],O_RDONLY)) < 0)
    : {
    : perror(argv[1]);
    : exit(EXIT_FAILURE);
    : }
    : /* Read a block, write a block */
    : while((len=read(sampfd,sampbuf,BUFSZ)) > 0)
    : write(devfd,sampbuf,len);
    : /* Close the descriptors and get outta here */
    : close(sampfd);
    : }
    : close(devfd);
    : exit(EXIT_SUCCESS);
    : }
    :
    : First off, I'm using Mandrake.
    :
    : If I use the command syntax cc play.c -o play I get an executable file that is able to 'play' standard .wav files, i.e. play mywave.wav.
    : If I use the command syntax cc play.c I get a.out of course, but when I try to execute it as a.out mywave.wav it plays this horrible screaching noise. The original executable file still works but I can't copy it to another filename w/o the same problem happening, i.e. cp play hold, etc. It also does weird stuff like if I move between default consoles and try to play the file by calling the previously executed command by using the 'up' arrow keys it tells me when I hit enter that the /dev/dsp device is busy. Someone told me that he thinks that it has to do w/the argv, argc variables and that the a.out file is attempting to play itself. Any suggestions. Thanks.
    : jbowers9
    :

    err ... i really doubt that just dumping the file on dsp will work - you might as well do cat filename>/dev/dsp (the non-playable wav header for starters; then what kind of endianness the file has, etc.)

    as for your 2 different results, the screeching is probably the 'correct' program output; on the other hand, if you do just 'play file.wav' you'll get /usr/bin/play (your location may vary) to (correctly) play the file. (if you do have '.' in the path, it's probably at the end, not at the beginning). try './play filename.wav'
    for the screeching effects.
  • Jeff P.Jeff P. Posts: 196Member
    : : Hi all,
    : : A friend of mine gave me a C program that opens and plays .wav files. It follows:

    : err ... i really doubt that just dumping the file on dsp will work - you might as well do cat filename>/dev/dsp (the non-playable wav header for starters; then what kind of endianness the file has, etc.)
    :
    : as for your 2 different results, the screeching is probably the 'correct' program output; on the other hand, if you do just 'play file.wav' you'll get /usr/bin/play (your location may vary) to (correctly) play the file. (if you do have '.' in the path, it's probably at the end, not at the beginning). try './play filename.wav'
    : for the screeching effects.



    Maybe try this:
    [code]
    #include
    int main(int argc, char **argv){
    esd_play_file("wav", argv[1], 0);
    }
    [/code]

    To compile you need something like this:
    [code]
    gcc -L/usr/local/lib -lesd -laudiofile -lm -lasound esdtest.c
    [/code]
Sign In or Register to comment.