58cb67074b5c6276f3718e5a5095c452ca3e5f89
[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("-i \t\t use/print file info\n");
26         printf("-r <file> \t record to <file>\n");
27         printf("-p <file> \t play from <file>\n");
28         printf("-s \t\t stereo\n");
29         printf("-m \t\t mono\n");
30         printf("-f <format> \t 1=8bit - 2=16bit (le)\n");
31         printf("-F <hz> \t frequency\n");
32         
33         return 1;
34 }
35
36 int main(int argc,char **argv) {
37         int sfile_fd;
38         int pfile_fd;
39         int audio_fd;
40         char device[MAX_C_DEVICE];
41         int i,j,k;
42         int rw;
43         unsigned char mode=0;
44         int o_mode=0;
45         char record_file[MAX_C_FILE];
46         char play_file[MAX_C_FILE];
47         dsp_set set;
48         unsigned char *buf;
49         unsigned char print_info=0;
50         unsigned char info[8];
51         int info_int;
52
53         /* defaults */
54         strcpy(device,"");
55         set.format=AFMT_S16_LE;
56         set.freq=44100;
57         set.channel=STEREO;
58
59         /* read argv */
60         for(i=1;i<argc;i++) {
61                 if(argv[i][0]=='-') {
62                         switch(argv[i][1]) {
63                                 case 'h':
64                                         usage();
65                                         return 1;
66                                 case 'i':
67                                         print_info=1;
68                                         break;
69                                 case 'r':
70                                         mode=RECORD;
71                                         strcpy(record_file,argv[++i]);
72                                         break;
73                                 case 'p':
74                                         mode=PLAY;
75                                         strcpy(play_file,argv[++i]);
76                                         break;
77                                 case 's':
78                                         set.channel=STEREO;
79                                         break;
80                                 case 'm':
81                                         set.channel=MONO;
82                                         break;
83                                 case 'f':
84                                         i++;
85                                         if(atoi(argv[i])==1) set.format=AFMT_U8;
86                                         if(atoi(argv[i])==2) set.format=AFMT_S16_LE;
87                                         break;
88                                 case 'F':
89                                         set.freq=atoi(argv[++i]);
90                                         break;
91                                 case 'd':
92                                         strncpy(device,argv[++i],MAX_C_DEVICE-1);
93                                         break;
94                                 default:
95                                         usage();
96                                         return -1;
97                         }
98                 } else usage();
99         }
100
101         if(!strcmp("",device)) {
102                 printf("you have to specify a device!\n");
103                 return -1;
104         }
105
106         /* open audio fd */
107         if(mode&RECORD) o_mode=O_RDONLY;
108         if(mode&PLAY) o_mode=O_WRONLY;
109         if(mode&RECORD && mode&PLAY) o_mode=O_RDWR;
110         if((audio_fd=open_sound_dev(device,o_mode))==-1) {
111                 printf("unable to open %s\n",device);
112                 return -1;
113         }
114
115         /* file fd's */
116         if(mode&PLAY) {
117                 if((pfile_fd=open_file(play_file,O_RDONLY))==-1) {
118                         printf("unable to open file %s for reading\n",play_file);
119                         return -1;
120                 }
121         }
122         if(mode&RECORD) {
123                 if((sfile_fd=open_file(record_file,O_CREAT|O_WRONLY))==-1) {
124                         printf("unable to open file %s for writing\n",record_file);
125                         return -1;
126                 }
127         }
128
129         if(print_info) {
130                 if(mode&PLAY) {
131                         printf("file info:\n");
132                         lseek(pfile_fd,4,SEEK_SET);
133                         read(pfile_fd,&info_int,4);
134                         printf("file size: %d\n",info_int);
135                         lseek(pfile_fd,8,SEEK_CUR);
136                         read(pfile_fd,&info_int,4);
137                         printf("fmtsize: %d\n",info_int);
138                         read(pfile_fd,&info_int,4);
139                         printf("format tag: %d\n",(info_int>>16)&1<<16);
140                         printf("channels: %d\n",info_int&1<<16);
141                         read(pfile_fd,&info_int,4);
142                         printf("samples/sec: %d\n",info_int);
143                         read(pfile_fd,&info_int,4);
144                         printf("bytes/sec: %d\n",info_int);
145                         read(pfile_fd,&info_int,4);
146                         printf("block allign: %d\n",(info_int>>16)&1<<16);
147                         printf("bits/sample: %d\n",info_int&1<<16);
148                         lseek(pfile_fd,4,SEEK_CUR);
149                         read(pfile_fd,&info_int,4);
150                         printf("datasize: %d\n\n",info_int);
151                         /* return to start */
152                         lseek(pfile_fd,0,SEEK_SET);
153                 }
154         }
155
156         /* set dsp and get capabilities */
157         if(get_dsp_cap(audio_fd,&set,1)==-1) {
158                 printf("unable to get capabilities :(\n");
159                 return -1;
160         }
161         if(set_dsp(audio_fd,&set)==-1) {
162                 printf("unable to set dsp :(\n");
163                 return -1;
164         }
165
166         /* allocating buffer */
167         if((buf=malloc(set.bufsize*sizeof(unsigned char)))==NULL) {
168                 printf("allocating memory failed :(\n");
169                 perror("malloc");
170                 return -1;
171         }
172
173         if(mode&PLAY) {
174                 printf("playing file %s ...\n",play_file);
175                 rw=1;
176                 while(rw) {
177                         rw=read(pfile_fd,buf,set.bufsize);
178                         write(audio_fd,buf,set.bufsize);
179                 }
180         }
181
182         if(mode&RECORD) {
183                 printf("recording to file %s ...\n",record_file);
184                 rw=1;
185                 while(rw) {
186                         rw=read(audio_fd,buf,set.bufsize);
187                         write(sfile_fd,buf,set.bufsize);
188                 }
189         }
190
191         return 1;
192 }