initial checkin
[sound-tools/hdrec.git] / hdrec.c
1 /*
2  * hdrec -- some sort of multi tracker (not right now, but in the future :p)
3  *
4  * author: hackbard@hackdaworld.dyndns.org
5  *
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/ioctl.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <sys/soundcard.h>
15 #include <fcntl.h>
16 #include <unistd.h>
17 #include <errno.h>
18
19 #include "oss_api.h"
20 #include "hdrec.h"
21
22 int usage(void) {
23         printf("usage:\n\n");
24         printf("-h \t\t print this help\n");
25         printf("-r <file> \t record to <file>\n");
26         printf("-p <file> \t play from <file>\n");
27         printf("-s \t\t stereo\n");
28         printf("-m \t\t mono\n");
29         printf("-f <format> \t 1=8bit - 2=16bit (le)\n");
30         printf("-F <hz> \t frequency\n");
31         
32         return 1;
33 }
34
35 int main(int argc,char **argv) {
36         int sfile_fd;
37         int pfile_fd;
38         int audio_fd;
39         char device[MAX_C_DEVICE];
40         int i,j,k;
41         int rw;
42         unsigned char mode=0;
43         int o_mode=0;
44         char record_file[MAX_C_FILE];
45         char play_file[MAX_C_FILE];
46         dsp_set set;
47         unsigned char *buf;
48
49         /* defaults */
50         strcpy(device,"");
51         set.format=AFMT_S16_LE;
52         set.freq=44100;
53         set.channel=STEREO;
54
55         /* read argv */
56         for(i=1;i<argc;i++) {
57                 if(argv[i][0]=='-') {
58                         switch(argv[i][1]) {
59                                 case 'h':
60                                         usage();
61                                         return 1;
62                                 case 'r':
63                                         mode=RECORD;
64                                         strcpy(record_file,argv[++i]);
65                                         break;
66                                 case 'p':
67                                         mode=PLAY;
68                                         strcpy(play_file,argv[++i]);
69                                         break;
70                                 case 's':
71                                         set.channel=STEREO;
72                                         break;
73                                 case 'm':
74                                         set.channel=MONO;
75                                         break;
76                                 case 'f':
77                                         i++;
78                                         if(atoi(argv[i])==1) set.format=AFMT_U8;
79                                         if(atoi(argv[i])==2) set.format=AFMT_S16_LE;
80                                         break;
81                                 case 'F':
82                                         set.freq=atoi(argv[++i]);
83                                         break;
84                                 case 'd':
85                                         strncpy(device,argv[++i],MAX_C_DEVICE-1);
86                                         break;
87                                 default:
88                                         usage();
89                                         return -1;
90                         }
91                 } else usage();
92         }
93
94         if(!strcmp("",device)) {
95                 printf("you have to specify a device!\n");
96                 return -1;
97         }
98
99         /* open audio fd */
100         if(mode&RECORD) o_mode=O_RDONLY;
101         if(mode&PLAY) o_mode=O_WRONLY;
102         if(mode&RECORD && mode&PLAY) o_mode=O_RDWR;
103         if((audio_fd=open_sound_dev(device,o_mode))==-1) {
104                 printf("unable to open %s\n",device);
105                 return -1;
106         }
107
108         /* file fd's */
109         if(mode&PLAY) {
110                 if((pfile_fd=open_file(play_file,O_RDONLY))==-1) {
111                         printf("unable to open file %s for reading\n",play_file);
112                         return -1;
113                 }
114         }
115         if(mode&RECORD) {
116                 if((sfile_fd=open_file(record_file,O_CREAT|O_WRONLY))==-1) {
117                         printf("unable to open file %s for writing\n",record_file);
118                         return -1;
119                 }
120         }
121
122         /* set dsp and get capabilities */
123         if(get_dsp_cap(audio_fd,&set,1)==-1) {
124                 printf("unable to get capabilities :(\n");
125                 return -1;
126         }
127         if(set_dsp(audio_fd,&set)==-1) {
128                 printf("unable to set dsp :(\n");
129                 return -1;
130         }
131
132         /* allocating buffer */
133         if((buf=malloc(set.bufsize*sizeof(unsigned char)))==NULL) {
134                 printf("allocating memory failed :(\n");
135                 perror("malloc");
136                 return -1;
137         }
138         
139         if(mode&PLAY) {
140                 printf("playing file %s ...\n",play_file);
141                 rw=1;
142                 while(rw) {
143                         rw=read(pfile_fd,buf,set.bufsize);
144                         write(audio_fd,buf,set.bufsize);
145                 }
146         }
147
148         if(mode&RECORD) {
149                 printf("recording to file %s ...\n",record_file);
150                 rw=1;
151                 while(rw) {
152                         rw=read(audio_fd,buf,set.bufsize);
153                         write(sfile_fd,buf,set.bufsize);
154                 }
155         }
156
157         return 1;
158 }