+/*
+ * 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;
+}