testing display related segfaults .. :(
[my-code/ivac.git] / src / 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) {
10
11   puts("[audio] initializing audio ...");
12
13   if((audio->dsp_fd=open(audio->dsp_dev,O_RDWR))==-1) {
14     perror("[audio] open call");
15     return A_ERROR;
16   }
17
18   if(ioctl(audio->dsp_fd,SNDCTL_DSP_GETCAPS,&(audio->dsp_cap))==-1) {
19     perror("[audio] ioctl call");
20     return A_ERROR;
21   }
22
23   if(!(audio->dsp_cap&DSP_CAP_DUPLEX)) {
24     puts("[audio] no duplex support");
25     return A_ERROR;
26   }
27
28   return A_SUCCESS;
29 }
30
31 int audio_setup(t_audio *audio) {
32
33   int tmp;
34
35   puts("[audio] setting up sound device & allocating record/playback buffer");
36
37   tmp=audio->fmt;
38   if(ioctl(audio->dsp_fd,SNDCTL_DSP_SETFMT,&tmp)==-1) {
39     perror("[audio] ioctl call (SNDCTL_DSP_SETFMT)");
40     return A_ERROR;
41   }
42   if(tmp!=audio->fmt) {
43     puts("[audio] FMT not supported");
44     return A_ERROR;
45   }
46
47   tmp=audio->speed;
48   if(ioctl(audio->dsp_fd,SNDCTL_DSP_SPEED,&tmp)==-1) {
49     perror("[audio] ioctl call (SNDCTL_DSP_SPEED)");
50     return A_ERROR;
51   }
52   if(tmp!=audio->speed) {
53     puts("[audio] SPEED not supported");
54     return A_ERROR;
55   }
56
57   if(ioctl(audio->dsp_fd,SNDCTL_DSP_GETBLKSIZE,&(audio->blksize))==-1) {
58     perror("[audio] ioctl call (SNDCTL_DSP_GETBLKSIZE)");
59     return A_ERROR;
60   }
61
62   if((audio->play_data=(unsigned char *)malloc(audio->blksize))==NULL) {
63     perror("[audio] malloc call");
64     return A_ERROR;
65   }
66   if((audio->rec_data=(unsigned char *)malloc(audio->blksize))==NULL) {
67     perror("[audio] malloc call");
68     return A_ERROR;
69   }
70
71   return A_SUCCESS;
72 }
73
74 int audio_shutdown(t_audio *audio) {
75
76   puts("[audio] shutdown");
77
78   free(audio->play_data);
79   free(audio->rec_data);
80
81   if(close(audio->dsp_fd)==-1) {
82     perror("[audio] close call");
83     return A_ERROR;
84   }
85
86   return A_SUCCESS;
87 }
88
89 int audio_play(t_audio *audio,int len) {
90
91   int count,left;
92
93   count=0;
94   left=len;
95
96   while(left) {
97     if((count=write(audio->dsp_fd,audio->play_data+len-left,left))==-1) {
98       perror("[audio] write call");
99       return A_ERROR;
100     }
101     left-=count;
102   }
103
104   return A_SUCCESS;
105 }
106
107 int audio_record(t_audio *audio,int len) {
108
109   int count,left;
110
111   count=0;
112   left=len;
113
114   while(left) {
115     if((count=read(audio->dsp_fd,audio->rec_data+len-left,left))==-1) {
116       perror("[audio] read call");
117       return A_ERROR;
118     }
119     left-=count;
120   }
121
122   return A_SUCCESS;
123 }