All pastes #164901 Raw Edit

Anonymous

public text v1 · immutable
·published 2006-09-09 04:52 UTC
rendered paste body
#include <sys/types.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/soundcard.h>

#define BUFSIZE 1024

char buf[BUFSIZE];

main()
{
  int fd,r,val,bp;
  fd = open("/dev/dsp",O_RDONLY);  
  if(fd<0) {
     fprintf(stderr,"Can't open audio driver.\n");
     exit(999);
  }
  r = ioctl(fd,SNDCTL_DSP_RESET,NULL);
  if(r>=0) {
     val = 16;
     r = ioctl(fd,SNDCTL_DSP_SAMPLESIZE,&val);
  }
  if(r>=0) {
     val = AFMT_S16_LE;
     r = ioctl(fd,SNDCTL_DSP_SETFMT,&val);
  }
  if(r>=0) {
     val = 1;
     r = ioctl(fd,SNDCTL_DSP_STEREO,&val);
  }
  if(r>=0) {
     val = 44100;
     r = ioctl(fd,SNDCTL_DSP_SPEED,&val);
  }
  if(r<0) {
     fprintf(stderr,"Sound device did not accept settings.\n");
     exit(999);
  }

  bp = 0;
  do {
     while(bp<BUFSIZE) {
        r = read(fd,buf+bp,BUFSIZE-bp);
        if(r<=0) break;
        bp += r;
     }
     fwrite(buf,bp,1,stdout);
     bp = 0;
  } while(r);
  
  close(fd);
}