checked in buggy bullshit .. 2lame
[sound-tools/hdrec.git] / hdrec.c
diff --git a/hdrec.c b/hdrec.c
index 42d86a7..77b222b 100644 (file)
--- a/hdrec.c
+++ b/hdrec.c
@@ -11,6 +11,8 @@
 #include <sys/ioctl.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/select.h>
+#include <sys/time.h>
 #include <sys/soundcard.h>
 #include <fcntl.h>
 #include <unistd.h>
@@ -23,6 +25,7 @@ int usage(void) {
        printf("usage:\n\n");
        printf("-h \t\t print this help\n");
        printf("-i \t\t use/print file info\n");
+       printf("-d <device> \t sound device (eg: /dev/dsp)\n");
        printf("-r <file> \t record to <file>\n");
        printf("-p <file> \t play from <file>\n");
        printf("-s \t\t stereo\n");
@@ -47,10 +50,14 @@ int main(int argc,char **argv) {
        char play_file[MAX_C_FILE];
        dsp_set set;
        unsigned char *buf;
+       unsigned char *buf2=NULL;
        unsigned char print_info=0;
        unsigned char info[8];
        int info_int;
        int tmp;
+       fd_set read_fds,write_fds;
+       struct timeval fds_tv;
+       char c;
 
        /* defaults */
        strcpy(device,"");
@@ -69,11 +76,11 @@ int main(int argc,char **argv) {
                                        print_info=1;
                                        break;
                                case 'r':
-                                       mode=RECORD;
+                                       mode|=RECORD;
                                        strcpy(record_file,argv[++i]);
                                        break;
                                case 'p':
-                                       mode=PLAY;
+                                       mode|=PLAY;
                                        strcpy(play_file,argv[++i]);
                                        break;
                                case 's':
@@ -180,8 +187,15 @@ int main(int argc,char **argv) {
                perror("malloc");
                return -1;
        }
+       if((mode&RECORD) && (mode&PLAY)) {
+               if((buf2=malloc(set.bufsize*sizeof(unsigned char)))==NULL) {
+                       printf("allocating 2nd memory failed :(\n");
+                       perror("malloc");
+                       return -1;
+               }
+       }
 
-       if(mode&PLAY) {
+       if((mode&PLAY) && (!(mode&RECORD))) {
                printf("playing file %s ...\n",play_file);
                rw=1;
                while(rw) {
@@ -190,7 +204,7 @@ int main(int argc,char **argv) {
                }
        }
 
-       if(mode&RECORD) {
+       if((mode&RECORD) && (!(mode&PLAY))) {
                printf("recording to file %s ...\n",record_file);
                rw=1;
                while(rw) {
@@ -199,6 +213,46 @@ int main(int argc,char **argv) {
                }
        }
 
+       if((mode&RECORD) && (mode&PLAY)) {
+               FD_ZERO(&read_fds);
+               FD_ZERO(&write_fds);
+               /* read_fds */
+               FD_SET(0,&read_fds);
+               FD_SET(pfile_fd,&read_fds);
+               FD_SET(audio_fd,&read_fds);
+               /* write_fds */
+               FD_SET(sfile_fd,&write_fds);
+               FD_SET(audio_fd,&write_fds);
+               fds_tv.tv_sec=0;
+               fds_tv.tv_usec=1000;
+               while(c!='q') {
+                       k=select(sfile_fd+1,&read_fds,&write_fds,NULL,&fds_tv);
+                       if(k==0) printf("itz zZzero :p\n");
+                       if(k) {
+                               if(FD_ISSET(0,&read_fds)) {
+                                       puts("reading from stdin");
+                                       read(0,&c,1);
+                               }
+                               if(FD_ISSET(pfile_fd,&read_fds)) {
+                                       puts("read from sound file");
+                                       read(pfile_fd,buf2,set.bufsize);
+                               }
+                               if(FD_ISSET(audio_fd,&read_fds)) {
+                                       puts("reading from audio device");
+                                       read(audio_fd,buf,set.bufsize);
+                               }
+                               if(FD_ISSET(sfile_fd,&write_fds)) {
+                                       puts("writing to file");
+                                       write(sfile_fd,buf,set.bufsize);
+                               }
+                               if(FD_ISSET(audio_fd,&write_fds)) {
+                                       puts("writing to audio device");
+                                       write(audio_fd,buf2,set.bufsize);
+                               }
+                       }
+               }
+       }
+
        if(mode&CONVERT) {
                if((tmp=lseek(pfile_fd,0,SEEK_END))==-1) {
                        printf("cannot determine filesize :(\n");