#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <stdio.h>
#include <linux/soundcard.h>

//int LENGTH = 3;    /* how many seconds of speech to store */
int RATE = 8000;   /* the sampling rate */
int SIZE = 8;      /* sample size: 8 or 16 bits */
int CHANNELS = 1;  /* 1 = mono 2 = stereo */

/* this buffer holds the digitized audio */
//unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8];

int main(int argc, char * argv[])
{
	if (argc == 4)
	{
		RATE = atoi(argv[1]);
		SIZE = atoi(argv[2]);
		CHANNELS = atoi(argv[3]);
	}


  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\n maybe you should set audio group to your account\n");
    exit(1);
  }
  else
  {
  	printf("opened /dev/dsp\n");
  }

  /* 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");
  else
  {
  	printf("bitsrate set set\n");
  }

  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");
  else
  {
  	printf("channels set\n");
  }

  arg = RATE;	   /* sampling rate */
  status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);
  if (status == -1)
    perror("SOUND_PCM_WRITE_WRITE ioctl failed");
  else
  {
  	printf("samplerate set set\n");
  }


	char c;
  	while (c != EOF) 
  	{
  		c = getchar();
  		write(fd,&c,sizeof(char));
    //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");
  }
}
