Debian Codegraph @

3rdparty/iaxclient/lib/audio_alsa.c

raw symbols SCIP
1 /*
2 * iaxclient: a cross-platform IAX softphone library
3 *
4 * Copyrights:
5 * Copyright (C) 2006 Panfilov Dmitry
6 *
7 * Contributors:
8 * Panfilov Dmitry
9 *
10 * This program is free software, distributed under the terms of
11 * the GNU Lesser (Library) General Public License.
12 *
13 */
14
15 #include "iaxclient_lib.h"
16 #include <alsa/asoundlib.h>
17
18 static snd_pcm_t *stream_out;
19 static snd_pcm_t *stream_in;
20
21 #define FRAMES_PER_BUFFER 80 /* 80 frames == 10ms */
22
23
24 static int alsa_play_sound(struct iaxc_sound *inSound, int ring) {
25 return 0;
26 }
27
28 int alsa_stop_sound(int soundID) {
29 return 0;
30 }
31
32
33 int alsa_start (struct iaxc_audio_driver *d ) {
34 return 0;
35 }
36
37 int alsa_stop (struct iaxc_audio_driver *d ) {
38 return 0;
39 }
40
41 void alsa_shutdown_audio()
42 {
43 return;
44 }
45
46
47 int alsa_input(struct iaxc_audio_driver *d, void *samples, int *nSamples) {
48
49 /* we don't return partial buffers */
50 long r;
51 long byteread=*nSamples;
52 static int h;
53 *nSamples=0;
54 snd_pcm_start(stream_in);
55 if(h==1) { h=0; return 0;}
56 do{
57 r = snd_pcm_readi(stream_in, samples, byteread);
58 if (r == -EAGAIN){
59 continue;
60 }
61 if (r == - EPIPE) {
62 snd_pcm_prepare(stream_in);
63 continue;
64 }
65 samples += (r * 2);
66 byteread -= r;
67 *nSamples += r;
68 }while(r >=0 && byteread >0);
69 h=1;
70 return 0;
71 }
72
73 int alsa_output(struct iaxc_audio_driver *d, void *samples, int nSamples) {
74
75 long r;
76 snd_pcm_start(stream_out);
77 while (nSamples > 0) {
78 r = snd_pcm_writei(stream_out, samples, nSamples);
79 if (r == -EAGAIN){
80 continue;
81 }
82 if (r == - EPIPE) {
83 snd_pcm_prepare(stream_out);
84 continue;
85 }
86 if (r < 0) {
87 fprintf(stderr, "r=%d\n",r);
88 }
89 samples += r * 2;
90 nSamples -= r;
91 }
92 return 0;
93 }
94
95 int alsa_select_devices (struct iaxc_audio_driver *d, int input, int output, int ring) {
96 return 0;
97 }
98
99 int alsa_selected_devices (struct iaxc_audio_driver *d, int *input, int *output, int *ring) {
100 *input = 0;
101 *output = 0;
102 *ring = 0;
103 return 0;
104 }
105
106 int alsa_destroy (struct iaxc_audio_driver *d )
107 {
108 /* TODO: something should happen here */
109 return 0;
110 }
111
112 double alsa_input_level_get(struct iaxc_audio_driver *d){
113 return -1;
114 }
115
116 double alsa_output_level_get(struct iaxc_audio_driver *d){
117 return -1;
118 }
119
120 int alsa_input_level_set(struct iaxc_audio_driver *d, double level){
121 return -1;
122 }
123
124 int alsa_output_level_set(struct iaxc_audio_driver *d, double level){
125 return -1;
126 }
127
128
129 /* initialize audio driver */
130 int alsa_initialize (struct iaxc_audio_driver *d ,int sample_rate) {
131 int i;
132 int err;
133 short buf[128];
134 snd_pcm_hw_params_t *hw_params;
135 snd_pcm_sw_params_t *sw_params;
136
137 if ((err = snd_pcm_open (&stream_out, "default", SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
138 fprintf (stderr, "cannot open audio device default (%s)\n",
139 snd_strerror (err));
140 exit (1);
141 }
142 if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
143 fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
144 snd_strerror (err));
145 exit (1);
146 }
147 if ((err = snd_pcm_hw_params_any (stream_out, hw_params)) < 0) {
148 fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
149 snd_strerror (err));
150 exit (1);
151 }
152 if ((err = snd_pcm_hw_params_set_access (stream_out, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
153 fprintf (stderr, "cannot set access type (%s)\n",
154 snd_strerror (err));
155 exit (1);
156 }
157 if ((err = snd_pcm_hw_params_set_format (stream_out, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
158 fprintf (stderr, "cannot set sample format (%s)\n",
159 snd_strerror (err));
160 exit (1);
161 }
162 if ((err = snd_pcm_hw_params_set_rate (stream_out, hw_params, sample_rate, 0)) < 0) {
163 fprintf (stderr, "cannot set sample rate (%s)\n",
164 snd_strerror (err));
165 exit (1);
166 }
167 if ((err = snd_pcm_hw_params_set_channels (stream_out, hw_params, 1)) < 0) {
168 fprintf (stderr, "cannot set channel count (%s)\n",
169 snd_strerror (err));
170 exit (1);
171 }
172 if ((err = snd_pcm_hw_params (stream_out, hw_params)) < 0) {
173 fprintf (stderr, "cannot set parameters (%s)\n",
174 snd_strerror (err));
175 exit (1);
176 }
177
178 snd_pcm_sw_params_malloc(&sw_params);
179
180 err = snd_pcm_sw_params_current(stream_out, sw_params);
181 if (err < 0) {
182 printf("Unable to determine current swparams for playback: %s\n", snd_strerror(err));
183 return err;
184 }
185 err = snd_pcm_sw_params_set_start_threshold(stream_out, sw_params, 80);
186 if (err < 0) {
187 fprintf(stderr, "Unable to set start threshold mode for playback: %s\n", snd_strerror(err));
188 return err;
189 }
190 err = snd_pcm_sw_params(stream_out, sw_params);
191 if (err < 0) {
192 fprintf(stderr, "Unable to set sw params for playback: %s\n", snd_strerror(err));
193 return err;
194 }
195
196 if ((err = snd_pcm_open (&stream_in, "default", SND_PCM_STREAM_CAPTURE, 0)) < 0) {
197 fprintf (stderr, "cannot open audio device default (%s)\n",
198 snd_strerror (err));
199 exit (1);
200 }
201 if ((err = snd_pcm_hw_params_any (stream_in, hw_params)) < 0) {
202 fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
203 snd_strerror (err));
204 exit (1);
205 }
206 if ((err = snd_pcm_hw_params_set_access (stream_in, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
207 fprintf (stderr, "cannot set access type (%s)\n",
208 snd_strerror (err));
209 exit (1);
210 }
211 if ((err = snd_pcm_hw_params_set_format (stream_in, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
212 fprintf (stderr, "cannot set sample format (%s)\n",
213 snd_strerror (err));
214 exit (1);
215 }
216 if ((err = snd_pcm_hw_params_set_rate (stream_in, hw_params, sample_rate, 0)) < 0) {
217 fprintf (stderr, "cannot set sample rate (%s)\n",
218 snd_strerror (err));
219 exit (1);
220 }
221 if ((err = snd_pcm_hw_params_set_channels (stream_in, hw_params, 1)) < 0) {
222 fprintf (stderr, "cannot set channel count (%s)\n",
223 snd_strerror (err));
224 exit (1);
225 }
226 if ((err = snd_pcm_hw_params (stream_in, hw_params)) < 0) {
227 fprintf (stderr, "cannot set parameters (%s)\n",
228 snd_strerror (err));
229 exit (1);
230 }
231
232 err = snd_pcm_sw_params_current(stream_in, sw_params);
233 if (err < 0) {
234 printf("Unable to determine current swparams for playback: %s\n", snd_strerror(err));
235 return err;
236 }
237 err = snd_pcm_sw_params_set_start_threshold(stream_in, sw_params, 80);
238 if (err < 0) {
239 fprintf(stderr, "Unable to set start threshold mode for playback: %s\n", snd_strerror(err));
240 return err;
241 }
242 err = snd_pcm_sw_params(stream_in, sw_params);
243 if (err < 0) {
244 fprintf(stderr, "Unable to set sw params for playback: %s\n", snd_strerror(err));
245 return err;
246 }
247
248
249 if ((err = snd_pcm_prepare (stream_in)) < 0) {
250 fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
251 snd_strerror (err));
252 exit (1);
253 }
254
255 if ((err = snd_pcm_prepare (stream_out)) < 0) {
256 fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
257 snd_strerror (err));
258 exit (1);
259 }
260
261 d->initialize = alsa_initialize;
262 d->destroy = alsa_destroy;
263 d->select_devices = alsa_select_devices;
264 d->selected_devices = alsa_selected_devices;
265 d->start = alsa_start;
266 d->stop = alsa_stop;
267 d->output = alsa_output;
268 d->input = alsa_input;
269 d->input_level_get = alsa_input_level_get;
270 d->input_level_set = alsa_input_level_set;
271 d->output_level_get = alsa_output_level_get;
272 d->output_level_set = alsa_output_level_set;
273 d->play_sound = alsa_play_sound;
274 d->stop_sound = alsa_stop_sound;
275
276 return 0;
277 }