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


#define  DSP_IN    "/dev/dsp"
#define  DSP_OUT   "/dev/dsp"

int     dsp_oopen(void)
{
       int   fd;
   
       if ((fd = open(DSP_OUT, O_RDWR|O_NONBLOCK)) < 0) {
                  perror(DSP_OUT);
                  exit(1);
       }
   
       return fd;
}

int     dsp_iopen(void)
{
   
           int  fd;
   
       if ((fd = open(DSP_IN, O_RDWR|O_NONBLOCK)) < 0) {
                  perror(DSP_IN);
                  exit(1);
       }
       return fd;
}

int     dsp_put1(int fd_put,char buffer)
{
       int   flg;
   
   
       flg = write(fd_put,&buffer,1);
//           fprintf(stderr,"<%x",buffer);//^s^N^G^g^M^Q^d^N^}^P^N^O
//      sleep(1);
        usleep(200);
     if (flg == -1){
          fprintf(stderr,"flg error\n");
                          exit(-1);
       }
       if(flg != 1){
                  fprintf(stderr,"flg incorect\n");
       }
       return flg;
}





int     dsp_get1(int fd_get)
{
       int flg;
       char buffer;
   
       do{
          buffer = 0;
          flg = read(fd_get,&buffer,1);
       }while((flg == 0)||(flg == -1));
     if(flg > 0){
          fprintf(stderr,"|%x",buffer);
                  return buffer;
       }
}

int   dsp_getn(int n,int fd_get,char buffer[])
{
   
       int  flg,i,j;
       char gbuffer;
   
       i = 0;
       do{
          flg = read(fd_get,&gbuffer,1);
              if (flg == -1){
                         //fprintf(stderr,"flag error\n");
                         //exit(-1);
              }
          
              if(flg > 0){
                         buffer[i] = gbuffer;
                 i++;
              }
          
       }while(i < n);
       return flg;


}





int   dsp_gets(int fd_get,char buffer[])
{
       int  i,h,j,k,flg;
       char gbuffer;
       h = 1;
       i = 0;
       k = 0;
       while(h){
                  flg = read(fd_get,&gbuffer,1);
          if (flg == -1){
      //     fprintf(stderr,"flag error\n");
            //     exit(-1);
          }
          
          if(flg > 0){
                 buffer[i] = gbuffer;
                 i++;
          }
          
          
          if( gbuffer == '\0' ) 
            h=0;
       }
       return flg;
}


int  dsp_mono_set(int fd_get)
{
 /* set mono */
  int parm,status;
  parm = 0;//1
  status = ioctl(fd_get , SNDCTL_DSP_STEREO, &parm);
  if (status < 0) {
    perror("error from sound device ioctl");
    
    exit(1);
  }
}


int  dsp_n_bit_set(int fd_get , int bits)
{
  /* set 8-bit samples */
  int status;
  
  status = ioctl(fd_get, SOUND_PCM_WRITE_BITS, &bits);
  if (status < 0) {
    perror("error from sound device ioctl");
    
    exit(1);
  }
}


int  dsp_n_sampling_rate(int fd_get , int sampling)
{

 /* set sampling rate */
  int status;
  status = ioctl(fd_get , SOUND_PCM_WRITE_RATE, &sampling);
  if (status < 0) {
    perror("error from sound device ioctl");
  
    exit(1);
  }
}
