there is a new event api ...
[sound-tools/hdrec.git] / oss_api.c
1 /*
2  * oss_api.c -- useful functions to record playback using oss driver
3  *
4  * author: hackbard@hackdaworld.dyndns.org
5  *
6  */
7
8 #include <stdio.h>
9 #include <sys/ioctl.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <sys/soundcard.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include <errno.h>
16
17 #include "oss_api.h"
18
19 int get_dsp_cap(int fd,dsp_set *set,unsigned char print) {
20         if(ioctl(fd,SNDCTL_DSP_GETCAPS,&(set->cap))==-1) {
21                 perror("SNDCTL_DSP_GETCAPS");
22                 return -1;
23         }
24         
25         if(print) {
26                 printf("device capabilities:\n\n");
27                 printf("revision: %x\n",set->cap&DSP_CAP_REVISION);
28                 printf("full duplex:\t%c\n",set->cap&DSP_CAP_DUPLEX?'y':'n');
29                 printf("real time:\t%c\n",set->cap&DSP_CAP_REALTIME?'y':'n');
30                 printf("int. buf:\t%c\n",set->cap&DSP_CAP_BATCH?'y':'n');
31                 printf("coprociessor:\t%c\n",set->cap&DSP_CAP_COPROC?'y':'n');
32                 printf("trigger:\t%c\n",set->cap&DSP_CAP_TRIGGER?'y':'n');
33                 printf("support mmap:\t%c\n",set->cap&DSP_CAP_MMAP?'y':'n');
34                 printf("multiple open:\t%c\n",set->cap&DSP_CAP_MULTI?'y':'n');
35                 printf("chan binding:\t%c\n",set->cap&DSP_CAP_BIND?'y':'n');
36         }
37                 
38         return 1;
39 }
40
41 int set_dsp(int fd,dsp_set *set) {
42         int tmp;
43
44         tmp=set->format;
45         if(ioctl(fd,SNDCTL_DSP_SETFMT,&(set->format))==-1) {
46                 perror("SNDCTL_DSP_SETFMT");
47                 return -1;
48         }
49         if(set->format!=tmp) {
50                 printf("format (%d) not supported!\n",tmp);
51                 return -1;
52         }
53         tmp=set->channel;
54         if(ioctl(fd,SNDCTL_DSP_CHANNELS,&(set->channel))==-1) {
55                 perror("SNDCTL_DSP_CHANNELS");
56                 return -1;
57         }
58         if(set->channel!=tmp) {
59                 printf("%d-channel use not supported!\n",tmp);
60                 return -1;
61         }
62         tmp=set->freq;
63         if(ioctl(fd,SNDCTL_DSP_SPEED,&(set->freq))==-1) {
64                 perror("SNDCTL_DSP_SPEED");
65                 return -1;
66         }
67         if(set->freq!=tmp) {
68                 printf("%d hz not suported!\n",tmp);
69                 return -1;
70         }
71         if(ioctl(fd,SNDCTL_DSP_GETBLKSIZE,&(set->bufsize))==-1) {
72                 perror("SOUNDCTL_DSP_GETBLKSIZE");
73                 return -1;
74         }
75         printf("\n\nSOUNDCTL_DSP_GETBLKSIZE = %d\n\n",set->bufsize);
76
77         return 1;
78 }
79
80 int open_sound_dev(char *dev,int mode) {
81         int fd;
82
83         if((fd=open(dev,mode))==-1) {
84                 perror("open");
85                 return -1;
86         }
87
88         return fd;
89 }
90
91 int open_file(char *file,int mode) {
92         int fd;
93         
94         if(mode) {
95                 if((fd=open(file,mode,S_IRWXU))==-1) {
96                         perror("open");
97                         return -1;
98                 }
99         } else {
100                 if((fd=open(file,mode))==-1) {
101                         perror("open");
102                         return -1;
103                 }
104         }
105
106         return fd;
107 }
108
109 int close_it(int fd) {
110         if(close(fd)==-1) {
111                 perror("close");
112                 return -1;
113         }
114         
115         return 1;
116 }
117