added fast(er) list to be included (just in header) + small bug fix
[my-code/api.git] / audio / audio.c
1 /* audio.c -- audio management stuff
2  *
3  * author: hackbard@hackdaworld.dyndns.org
4  *
5  */
6
7 #include "audio.h"
8
9 int audio_init(t_audio *audio,int outfd) {
10
11   audio->outfd=outfd;
12
13   dprintf(outfd,"[audio] initializing audio ...\n");
14
15   if((audio->dsp_fd=open(audio->dsp_dev,O_RDWR))<0) {
16     dprintf(audio->outfd,"[audio] open call");
17     return A_ERROR;
18   }
19
20   if(ioctl(audio->dsp_fd,SNDCTL_DSP_GETCAPS,&(audio->dsp_cap))==-1) {
21     dprintf(audio->outfd,"[audio] ioctl call");
22     return A_ERROR;
23   }
24
25   if(!(audio->dsp_cap&DSP_CAP_DUPLEX)) {
26     dprintf(audio->outfd,"[audio] no duplex support\n");
27     return A_ERROR;
28   }
29
30   return A_SUCCESS;
31 }
32
33 int audio_setup(t_audio *audio) {
34
35   int tmp;
36
37   dprintf(audio->outfd,"[audio] setting up sound device & allocating record/playback buffer\n");
38
39   tmp=audio->fmt;
40   if(ioctl(audio->dsp_fd,SNDCTL_DSP_SETFMT,&tmp)==-1) {
41     dprintf(audio->outfd,"[audio] ioctl call (SNDCTL_DSP_SETFMT)");
42     return A_ERROR;
43   }
44   if(tmp!=audio->fmt) {
45     dprintf(audio->outfd,"[audio] fmt (%d) not supported\n",tmp);
46     return A_ERROR;
47   }
48
49   tmp=audio->speed;
50   if(ioctl(audio->dsp_fd,SNDCTL_DSP_SPEED,&tmp)==-1) {
51     dprintf(audio->outfd,"[audio] ioctl call (SNDCTL_DSP_SPEED)");
52     return A_ERROR;
53   }
54   if(tmp!=audio->speed) {
55     dprintf(audio->outfd,"[audio] speed (%d) not supported\n",tmp);
56     return A_ERROR;
57   }
58
59   if(ioctl(audio->dsp_fd,SNDCTL_DSP_GETBLKSIZE,&(audio->blksize))==-1) {
60     dprintf(audio->outfd,"[audio] ioctl call (SNDCTL_DSP_GETBLKSIZE)");
61     return A_ERROR;
62   }
63
64   if((audio->play_data=(unsigned char *)malloc(audio->blksize))==NULL) {
65     dprintf(audio->outfd,"[audio] malloc call");
66     return A_ERROR;
67   }
68   if((audio->rec_data=(unsigned char *)malloc(audio->blksize))==NULL) {
69     dprintf(audio->outfd,"[audio] malloc call");
70     return A_ERROR;
71   }
72
73   return A_SUCCESS;
74 }
75
76 int audio_shutdown(t_audio *audio) {
77
78   dprintf(audio->outfd,"[audio] shutdown\n");
79
80   free(audio->play_data);
81   free(audio->rec_data);
82
83   if(close(audio->dsp_fd)==-1) {
84     dprintf(audio->outfd,"[audio] close call");
85     return A_ERROR;
86   }
87
88   return A_SUCCESS;
89 }
90
91 int audio_play(t_audio *audio,int len) {
92
93   int count,left;
94
95   count=0;
96   left=len;
97
98   while(left) {
99     if((count=write(audio->dsp_fd,audio->play_data+len-left,left))==-1) {
100       dprintf(audio->outfd,"[audio] write call");
101       return A_ERROR;
102     }
103     left-=count;
104   }
105
106   return A_SUCCESS;
107 }
108
109 int audio_record(t_audio *audio,int len) {
110
111   int count,left;
112
113   count=0;
114   left=len;
115
116   while(left) {
117     if((count=read(audio->dsp_fd,audio->rec_data+len-left,left))==-1) {
118       dprintf(audio->outfd,"[audio] read call");
119       return A_ERROR;
120     }
121     left-=count;
122   }
123
124   return A_SUCCESS;
125 }