2 * hdrec -- some sort of multi tracker (not right now, but in the future :p)
4 * author: hackbard@hackdaworld.dyndns.org
11 #include <sys/ioctl.h>
12 #include <sys/types.h>
14 #include <sys/select.h>
16 #include <sys/soundcard.h>
26 printf("-h \t\t print this help\n");
27 printf("-i \t\t use/print file info\n");
28 printf("-d <device> \t sound device (eg: /dev/dsp)\n");
29 printf("-r <file> \t record to <file>\n");
30 printf("-p <file> \t play from <file>\n");
31 printf("-s \t\t stereo\n");
32 printf("-m \t\t mono\n");
33 printf("-f <format> \t 1=8bit - 2=16bit (le)\n");
34 printf("-F <hz> \t frequency\n");
35 printf("-c <src> <dst> \t converts raw to wav (specify f/m,s/F)\n");
40 int main(int argc,char **argv) {
44 char device[MAX_C_DEVICE];
49 char record_file[MAX_C_FILE];
50 char play_file[MAX_C_FILE];
53 unsigned char *buf2=NULL;
54 unsigned char print_info=0;
55 unsigned char info[8];
58 fd_set read_fds,write_fds;
59 struct timeval fds_tv;
64 set.format=AFMT_S16_LE;
80 strcpy(record_file,argv[++i]);
84 strcpy(play_file,argv[++i]);
94 if(atoi(argv[i])==1) set.format=AFMT_U8;
95 if(atoi(argv[i])==2) set.format=AFMT_S16_LE;
98 set.freq=atoi(argv[++i]);
101 strncpy(device,argv[++i],MAX_C_DEVICE-1);
105 strcpy(play_file,argv[++i]);
106 strcpy(record_file,argv[++i]);
115 if(!strcmp("",device)) {
116 printf("you have to specify a device!\n");
121 if(mode&RECORD) o_mode=O_RDONLY;
122 if(mode&PLAY) o_mode=O_WRONLY;
123 if(mode&RECORD && mode&PLAY) o_mode=O_RDWR;
124 if((audio_fd=open_sound_dev(device,o_mode))==-1) {
125 printf("unable to open %s\n",device);
130 if((mode&PLAY) || (mode&CONVERT)) {
131 if((pfile_fd=open_file(play_file,O_RDONLY))==-1) {
132 printf("unable to open file %s for reading\n",play_file);
136 if((mode&RECORD) || (mode&CONVERT)) {
137 if((sfile_fd=open_file(record_file,O_CREAT|O_WRONLY))==-1) {
138 printf("unable to open file %s for writing\n",record_file);
145 printf("file info:\n");
146 lseek(pfile_fd,4,SEEK_SET);
147 read(pfile_fd,&info_int,4);
148 printf("file size: %d\n",info_int);
149 lseek(pfile_fd,8,SEEK_CUR);
150 read(pfile_fd,&info_int,4);
151 printf("fmtsize: %d\n",info_int);
152 if(info_int==16) set.format=AFMT_S16_LE;
153 if(info_int==8) set.format=AFMT_U8;
154 read(pfile_fd,&info_int,4);
155 printf("format tag: %d\n",info_int&0xffff);
156 printf("channels: %d\n",(info_int>>16)&0xffff);
157 set.channel=(info_int>>16)&0xffff;
158 read(pfile_fd,&info_int,4);
159 printf("samples/sec: %d\n",info_int);
161 read(pfile_fd,&info_int,4);
162 printf("bytes/sec: %d\n",info_int);
163 read(pfile_fd,&info_int,4);
164 printf("block allign: %d\n",info_int&0xffff);
165 printf("bits/sample: %d\n",(info_int>>16)&0xffff);
166 lseek(pfile_fd,4,SEEK_CUR);
167 read(pfile_fd,&info_int,4);
168 printf("datasize: %d\n\n",info_int);
169 /* return to start */
170 lseek(pfile_fd,0,SEEK_SET);
174 /* set dsp and get capabilities */
175 if(get_dsp_cap(audio_fd,&set,1)==-1) {
176 printf("unable to get capabilities :(\n");
179 if(set_dsp(audio_fd,&set)==-1) {
180 printf("unable to set dsp :(\n");
184 /* allocating buffer */
185 if((buf=malloc(set.bufsize*sizeof(unsigned char)))==NULL) {
186 printf("allocating memory failed :(\n");
190 if((mode&RECORD) && (mode&PLAY)) {
191 if((buf2=malloc(set.bufsize*sizeof(unsigned char)))==NULL) {
192 printf("allocating 2nd memory failed :(\n");
198 if((mode&PLAY) && (!(mode&RECORD))) {
199 printf("playing file %s ...\n",play_file);
202 rw=read(pfile_fd,buf,set.bufsize);
203 write(audio_fd,buf,set.bufsize);
207 if((mode&RECORD) && (!(mode&PLAY))) {
208 printf("recording to file %s ...\n",record_file);
211 rw=read(audio_fd,buf,set.bufsize);
212 write(sfile_fd,buf,set.bufsize);
216 if((mode&RECORD) && (mode&PLAY)) {
221 FD_SET(pfile_fd,&read_fds);
222 FD_SET(audio_fd,&read_fds);
224 FD_SET(sfile_fd,&write_fds);
225 FD_SET(audio_fd,&write_fds);
229 k=select(sfile_fd+1,&read_fds,&write_fds,NULL,&fds_tv);
230 if(k==0) printf("itz zZzero :p\n");
232 if(FD_ISSET(0,&read_fds)) {
233 puts("reading from stdin");
236 if(FD_ISSET(pfile_fd,&read_fds)) {
237 puts("read from sound file");
238 read(pfile_fd,buf2,set.bufsize);
240 if(FD_ISSET(audio_fd,&read_fds)) {
241 puts("reading from audio device");
242 read(audio_fd,buf,set.bufsize);
244 if(FD_ISSET(sfile_fd,&write_fds)) {
245 puts("writing to file");
246 write(sfile_fd,buf,set.bufsize);
248 if(FD_ISSET(audio_fd,&write_fds)) {
249 puts("writing to audio device");
250 write(audio_fd,buf2,set.bufsize);
257 if((tmp=lseek(pfile_fd,0,SEEK_END))==-1) {
258 printf("cannot determine filesize :(\n");
262 lseek(pfile_fd,0,SEEK_SET);
264 write(sfile_fd,info,4);
266 write(sfile_fd,&info_int,4);
267 strcpy(info,"WAVEfmt ");
268 write(sfile_fd,info,8);
269 if(set.format==AFMT_S16_LE) info_int=16;
270 if(set.format==AFMT_U8) info_int=8;
271 write(sfile_fd,&info_int,4);
272 info_int=set.channel<<16;
274 write(sfile_fd,&info_int,4);
276 write(sfile_fd,&info_int,4);
277 info_int=set.freq*set.channel;
278 if(set.format==AFMT_S16_LE) info_int*=2;
279 write(sfile_fd,&info_int,4);
280 info_int=(set.channel*8)<<16;
281 info_int|=set.channel;
282 write(sfile_fd,&info_int,4);
284 write(sfile_fd,info,4);
286 write(sfile_fd,&info_int,4);
287 /* write data now ... */
288 for(j=0;j<tmp/set.bufsize;j++) {
289 i=read(pfile_fd,buf,set.bufsize);
290 k=write(sfile_fd,buf,set.bufsize);
291 printf("read %d, wrote %d\n",i,k);
293 printf("\ndone ...\n");