]> hackdaworld.org Git - sound-tools/hdrec.git/commitdiff
initial checkin
authorhackbard <hackbard>
Sun, 6 Jul 2003 12:01:06 +0000 (12:01 +0000)
committerhackbard <hackbard>
Sun, 6 Jul 2003 12:01:06 +0000 (12:01 +0000)
Makefile [new file with mode: 0644]
hdrec [new file with mode: 0755]
hdrec.c [new file with mode: 0644]
hdrec.h [new file with mode: 0644]
oss_api.c [new file with mode: 0644]
oss_api.h [new file with mode: 0644]
oss_api.o [new file with mode: 0644]

diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..6bd5fd0
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,16 @@
+# Makefile of hdrec
+
+CFLAGS = -O3 -Wall
+CC = gcc
+
+TARGETS = hdrec
+
+all: $(TARGETS)
+
+hdrec:
+       $(CC) $(CFLAGS) -Wall oss_api.c hdrec.c -o hdrec
+
+clean:
+       rm $(OBJS) hdrec
+
+remake: clean all
diff --git a/hdrec b/hdrec
new file mode 100755 (executable)
index 0000000..41a6b8c
Binary files /dev/null and b/hdrec differ
diff --git a/hdrec.c b/hdrec.c
new file mode 100644 (file)
index 0000000..ac6348e
--- /dev/null
+++ b/hdrec.c
@@ -0,0 +1,158 @@
+/*
+ * hdrec -- some sort of multi tracker (not right now, but in the future :p)
+ *
+ * author: hackbard@hackdaworld.dyndns.org
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/soundcard.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include "oss_api.h"
+#include "hdrec.h"
+
+int usage(void) {
+       printf("usage:\n\n");
+       printf("-h \t\t print this help\n");
+       printf("-r <file> \t record to <file>\n");
+       printf("-p <file> \t play from <file>\n");
+       printf("-s \t\t stereo\n");
+       printf("-m \t\t mono\n");
+       printf("-f <format> \t 1=8bit - 2=16bit (le)\n");
+       printf("-F <hz> \t frequency\n");
+       
+       return 1;
+}
+
+int main(int argc,char **argv) {
+       int sfile_fd;
+       int pfile_fd;
+       int audio_fd;
+       char device[MAX_C_DEVICE];
+       int i,j,k;
+       int rw;
+       unsigned char mode=0;
+       int o_mode=0;
+       char record_file[MAX_C_FILE];
+       char play_file[MAX_C_FILE];
+       dsp_set set;
+       unsigned char *buf;
+
+       /* defaults */
+       strcpy(device,"");
+       set.format=AFMT_S16_LE;
+       set.freq=44100;
+       set.channel=STEREO;
+
+       /* read argv */
+       for(i=1;i<argc;i++) {
+               if(argv[i][0]=='-') {
+                       switch(argv[i][1]) {
+                               case 'h':
+                                       usage();
+                                       return 1;
+                               case 'r':
+                                       mode=RECORD;
+                                       strcpy(record_file,argv[++i]);
+                                       break;
+                               case 'p':
+                                       mode=PLAY;
+                                       strcpy(play_file,argv[++i]);
+                                       break;
+                               case 's':
+                                       set.channel=STEREO;
+                                       break;
+                               case 'm':
+                                       set.channel=MONO;
+                                       break;
+                               case 'f':
+                                       i++;
+                                       if(atoi(argv[i])==1) set.format=AFMT_U8;
+                                       if(atoi(argv[i])==2) set.format=AFMT_S16_LE;
+                                       break;
+                               case 'F':
+                                       set.freq=atoi(argv[++i]);
+                                       break;
+                               case 'd':
+                                       strncpy(device,argv[++i],MAX_C_DEVICE-1);
+                                       break;
+                               default:
+                                       usage();
+                                       return -1;
+                       }
+               } else usage();
+       }
+
+       if(!strcmp("",device)) {
+               printf("you have to specify a device!\n");
+               return -1;
+       }
+
+       /* open audio fd */
+       if(mode&RECORD) o_mode=O_RDONLY;
+       if(mode&PLAY) o_mode=O_WRONLY;
+       if(mode&RECORD && mode&PLAY) o_mode=O_RDWR;
+       if((audio_fd=open_sound_dev(device,o_mode))==-1) {
+               printf("unable to open %s\n",device);
+               return -1;
+       }
+
+       /* file fd's */
+       if(mode&PLAY) {
+               if((pfile_fd=open_file(play_file,O_RDONLY))==-1) {
+                       printf("unable to open file %s for reading\n",play_file);
+                       return -1;
+               }
+       }
+       if(mode&RECORD) {
+               if((sfile_fd=open_file(record_file,O_CREAT|O_WRONLY))==-1) {
+                       printf("unable to open file %s for writing\n",record_file);
+                       return -1;
+               }
+       }
+
+       /* set dsp and get capabilities */
+       if(get_dsp_cap(audio_fd,&set,1)==-1) {
+               printf("unable to get capabilities :(\n");
+               return -1;
+       }
+       if(set_dsp(audio_fd,&set)==-1) {
+               printf("unable to set dsp :(\n");
+               return -1;
+       }
+
+       /* allocating buffer */
+       if((buf=malloc(set.bufsize*sizeof(unsigned char)))==NULL) {
+               printf("allocating memory failed :(\n");
+               perror("malloc");
+               return -1;
+       }
+       
+       if(mode&PLAY) {
+               printf("playing file %s ...\n",play_file);
+               rw=1;
+               while(rw) {
+                       rw=read(pfile_fd,buf,set.bufsize);
+                       write(audio_fd,buf,set.bufsize);
+               }
+       }
+
+       if(mode&RECORD) {
+               printf("recording to file %s ...\n",record_file);
+               rw=1;
+               while(rw) {
+                       rw=read(audio_fd,buf,set.bufsize);
+                       write(sfile_fd,buf,set.bufsize);
+               }
+       }
+
+       return 1;
+}      
diff --git a/hdrec.h b/hdrec.h
new file mode 100644 (file)
index 0000000..119fdbd
--- /dev/null
+++ b/hdrec.h
@@ -0,0 +1,12 @@
+/* hdrec.h */
+
+#ifndef HDREC_H
+#define HDREC_H
+
+#define PLAY 1
+#define RECORD 2
+
+#define MAX_C_FILE 32
+#define MAX_C_DEVICE 32
+
+#endif /* HDREC_H */
diff --git a/oss_api.c b/oss_api.c
new file mode 100644 (file)
index 0000000..413e2a2
--- /dev/null
+++ b/oss_api.c
@@ -0,0 +1,117 @@
+/*
+ * oss_api.c -- useful functions to record playback using oss driver
+ *
+ * author: hackbard@hackdaworld.dyndns.org
+ *
+ */
+
+#include <stdio.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/soundcard.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include "oss_api.h"
+
+int get_dsp_cap(int fd,dsp_set *set,unsigned char print) {
+       if(ioctl(fd,SNDCTL_DSP_GETCAPS,&(set->cap))==-1) {
+               perror("SNDCTL_DSP_GETCAPS");
+               return -1;
+       }
+       
+       if(print) {
+               printf("device capabilities:\n\n");
+               printf("revision: %x\n",set->cap&DSP_CAP_REVISION);
+               printf("full duplex:\t%c\n",set->cap&DSP_CAP_DUPLEX?'y':'n');
+               printf("real time:\t%c\n",set->cap&DSP_CAP_REALTIME?'y':'n');
+               printf("int. buf:\t%c\n",set->cap&DSP_CAP_BATCH?'y':'n');
+               printf("coprociessor:\t%c\n",set->cap&DSP_CAP_COPROC?'y':'n');
+               printf("trigger:\t%c\n",set->cap&DSP_CAP_TRIGGER?'y':'n');
+               printf("support mmap:\t%c\n",set->cap&DSP_CAP_MMAP?'y':'n');
+               printf("multiple open:\t%c\n",set->cap&DSP_CAP_MULTI?'y':'n');
+               printf("chan binding:\t%c\n",set->cap&DSP_CAP_BIND?'y':'n');
+       }
+               
+       return 1;
+}
+
+int set_dsp(int fd,dsp_set *set) {
+       int tmp;
+
+       tmp=set->format;
+       if(ioctl(fd,SNDCTL_DSP_SETFMT,&(set->format))==-1) {
+               perror("SNDCTL_DSP_SETFMT");
+               return -1;
+       }
+       if(set->format!=tmp) {
+               printf("format (%d) not supported!\n",tmp);
+               return -1;
+       }
+       tmp=set->channel;
+       if(ioctl(fd,SNDCTL_DSP_CHANNELS,&(set->channel))==-1) {
+               perror("SNDCTL_DSP_CHANNELS");
+               return -1;
+       }
+       if(set->channel!=tmp) {
+               printf("%d-channel use not supported!\n",tmp);
+               return -1;
+       }
+       tmp=set->freq;
+       if(ioctl(fd,SNDCTL_DSP_SPEED,&(set->freq))==-1) {
+               perror("SNDCTL_DSP_SPEED");
+               return -1;
+       }
+       if(set->freq!=tmp) {
+               printf("%d hz not suported!\n",tmp);
+               return -1;
+       }
+       if(ioctl(fd,SNDCTL_DSP_GETBLKSIZE,&(set->bufsize))==-1) {
+               perror("SOUNDCTL_DSP_GETBLKSIZE");
+               return -1;
+       }
+       printf("\n\nSOUNDCTL_DSP_GETBLKSIZE = %d\n\n",set->bufsize);
+
+       return 1;
+}
+
+int open_sound_dev(char *dev,int mode) {
+       int fd;
+
+       if((fd=open(dev,mode))==-1) {
+               perror("open");
+               return -1;
+       }
+
+       return fd;
+}
+
+int open_file(char *file,int mode) {
+       int fd;
+       
+       if(mode) {
+               if((fd=open(file,mode,S_IRWXU))==-1) {
+                       perror("open");
+                       return -1;
+               }
+       } else {
+               if((fd=open(file,mode))==-1) {
+                       perror("open");
+                       return -1;
+               }
+       }
+
+       return fd;
+}
+
+int close_it(int fd) {
+       if(close(fd)==-1) {
+               perror("close");
+               return -1;
+       }
+       
+       return 1;
+}
+
diff --git a/oss_api.h b/oss_api.h
new file mode 100644 (file)
index 0000000..67dbe65
--- /dev/null
+++ b/oss_api.h
@@ -0,0 +1,27 @@
+/* oss_api.h */
+
+#ifndef OSS_API_H
+#define OSS_API_H
+
+/* defines */
+#define MONO 1
+#define STEREO 2
+#define SAMPLES 100000
+
+/* variables */
+typedef struct __dsp_set {
+       int cap;
+       int format;
+       int channel;
+       int freq;
+       int bufsize;
+} dsp_set;
+
+/* function prototypes */
+int get_dsp_cap(int fd,dsp_set *set,unsigned char print);
+int set_dsp(int fd,dsp_set *dsp_set);
+int open_sound_dev(char *dev,int mode);
+int open_file(char *file,int mode);
+int close_it(int fd);
+
+#endif /* OSS_API_H */
diff --git a/oss_api.o b/oss_api.o
new file mode 100644 (file)
index 0000000..5065496
Binary files /dev/null and b/oss_api.o differ