Debian Codegraph @

gtk/audio_alsa.cc

raw
1 ///////////////////////////////////////////////////////////////////////////////
2 // Free42 -- an HP-42S calculator simulator
3 // Copyright (C) 2004-2026 Thomas Okken
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License, version 2,
7 // as published by the Free Software Foundation.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, see http://www.gnu.org/licenses/.
16 ///////////////////////////////////////////////////////////////////////////////
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <math.h>
23 #include <alsa/asoundlib.h>
24 #include <sys/time.h>
25 #include <pthread.h>
26 #include <dlfcn.h>
27 #include <math.h>
28
29 // We want to be able to run even if libasound is not present, so we have to
30 // link it manually using dlopen() and dlsym(). These are the functions we are
31 // going to need; the pointer names are the same as the actual function names
32 // in libasound, with _dl_ prefixed.
33 static int libasound_state = 0; // 0=closed, 1=open, 2=missing
34
35 typedef int (*_ptr_snd_pcm_close)(snd_pcm_t *pcm);
36 typedef int (*_ptr_snd_pcm_format_big_endian)(snd_pcm_format_t format);
37 typedef int (*_ptr_snd_pcm_format_physical_width)(snd_pcm_format_t format);
38 typedef int (*_ptr_snd_pcm_format_unsigned)(snd_pcm_format_t format);
39 typedef int (*_ptr_snd_pcm_format_width)(snd_pcm_format_t format);
40 typedef int (*_ptr_snd_pcm_hw_params)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
41 typedef int (*_ptr_snd_pcm_hw_params_malloc)(snd_pcm_hw_params_t **ptr);
42 typedef void (*_ptr_snd_pcm_hw_params_free)(snd_pcm_hw_params_t *obj);
43 typedef int (*_ptr_snd_pcm_hw_params_any)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
44 typedef int (*_ptr_snd_pcm_hw_params_get_buffer_size)(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val);
45 typedef int (*_ptr_snd_pcm_hw_params_set_access)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t _access);
46 typedef int (*_ptr_snd_pcm_hw_params_set_buffer_size_near)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val);
47 typedef int (*_ptr_snd_pcm_hw_params_set_channels)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val);
48 typedef int (*_ptr_snd_pcm_hw_params_set_format)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t val);
49 typedef int (*_ptr_snd_pcm_hw_params_set_rate_near)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
50 typedef int (*_ptr_snd_pcm_open)(snd_pcm_t **pcm, const char *name, snd_pcm_stream_t stream, int mode);
51 typedef int (*_ptr_snd_pcm_prepare)(snd_pcm_t *pcm);
52 typedef int (*_ptr_snd_pcm_resume)(snd_pcm_t *pcm);
53 typedef int (*_ptr_snd_pcm_sw_params)(snd_pcm_t *pcm, snd_pcm_sw_params_t *params);
54 typedef int (*_ptr_snd_pcm_sw_params_malloc)(snd_pcm_sw_params_t **ptr);
55 typedef void (*_ptr_snd_pcm_sw_params_free)(snd_pcm_sw_params_t *obj);
56 typedef int (*_ptr_snd_pcm_sw_params_current)(snd_pcm_t *pcm, snd_pcm_sw_params_t *params);
57 typedef int (*_ptr_snd_pcm_sw_params_get_boundary)(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val);
58 typedef int (*_ptr_snd_pcm_sw_params_set_silence_size)(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val);
59 typedef int (*_ptr_snd_pcm_sw_params_set_silence_threshold)(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val);
60 typedef snd_pcm_sframes_t (*_ptr_snd_pcm_writei)(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size);
61 typedef const char *(*_ptr_snd_strerror)(int errnum);
62
63 _ptr_snd_pcm_close _dl_snd_pcm_close;
64 _ptr_snd_pcm_format_big_endian _dl_snd_pcm_format_big_endian;
65 _ptr_snd_pcm_format_physical_width _dl_snd_pcm_format_physical_width;
66 _ptr_snd_pcm_format_unsigned _dl_snd_pcm_format_unsigned;
67 _ptr_snd_pcm_format_width _dl_snd_pcm_format_width;
68 _ptr_snd_pcm_hw_params _dl_snd_pcm_hw_params;
69 _ptr_snd_pcm_hw_params_malloc _dl_snd_pcm_hw_params_malloc;
70 _ptr_snd_pcm_hw_params_free _dl_snd_pcm_hw_params_free;
71 _ptr_snd_pcm_hw_params_any _dl_snd_pcm_hw_params_any;
72 _ptr_snd_pcm_hw_params_get_buffer_size _dl_snd_pcm_hw_params_get_buffer_size;
73 _ptr_snd_pcm_hw_params_set_access _dl_snd_pcm_hw_params_set_access;
74 _ptr_snd_pcm_hw_params_set_buffer_size_near _dl_snd_pcm_hw_params_set_buffer_size_near;
75 _ptr_snd_pcm_hw_params_set_channels _dl_snd_pcm_hw_params_set_channels;
76 _ptr_snd_pcm_hw_params_set_format _dl_snd_pcm_hw_params_set_format;
77 _ptr_snd_pcm_hw_params_set_rate_near _dl_snd_pcm_hw_params_set_rate_near;
78 _ptr_snd_pcm_open _dl_snd_pcm_open;
79 _ptr_snd_pcm_prepare _dl_snd_pcm_prepare;
80 _ptr_snd_pcm_resume _dl_snd_pcm_resume;
81 _ptr_snd_pcm_sw_params _dl_snd_pcm_sw_params;
82 _ptr_snd_pcm_sw_params_malloc _dl_snd_pcm_sw_params_malloc;
83 _ptr_snd_pcm_sw_params_free _dl_snd_pcm_sw_params_free;
84 _ptr_snd_pcm_sw_params_current _dl_snd_pcm_sw_params_current;
85 _ptr_snd_pcm_sw_params_get_boundary _dl_snd_pcm_sw_params_get_boundary;
86 _ptr_snd_pcm_sw_params_set_silence_size _dl_snd_pcm_sw_params_set_silence_size;
87 _ptr_snd_pcm_sw_params_set_silence_threshold _dl_snd_pcm_sw_params_set_silence_threshold;
88 _ptr_snd_pcm_writei _dl_snd_pcm_writei;
89 _ptr_snd_strerror _dl_snd_strerror;
90
91 #define TDIFF(begin,end) (((double)(end.tv_sec - begin.tv_sec)*1000.0) + ((end.tv_usec - begin.tv_usec)/1000.0))
92
93 static unsigned int audio_sample_rate = 22050;
94 static snd_pcm_format_t audio_format = SND_PCM_FORMAT_S16;
95 static const char *audio_device = "default";
96 static int audio_channels = 1;
97
98 static snd_pcm_t *playback_handle = NULL;
99 static bool audio_initialized = false;
100 static snd_pcm_uframes_t buffer_size;
101
102 static struct timeval last_use;
103
104 static pthread_mutex_t closer_mutex = PTHREAD_MUTEX_INITIALIZER;
105 static pthread_cond_t closer_cond = PTHREAD_COND_INITIALIZER;
106 static pthread_t closer_thread;
107
108 static int audio_set_hw_params(snd_pcm_hw_params_t *hw_params) {
109 int err;
110 snd_pcm_uframes_t size;
111
112 if ((err = _dl_snd_pcm_hw_params_any (playback_handle, hw_params)) < 0) {
113 fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n", _dl_snd_strerror (err));
114 return err;
115 }
116 if ((err = _dl_snd_pcm_hw_params_set_access (playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
117 fprintf (stderr, "cannot set access type (%s)\n", _dl_snd_strerror (err));
118 return err;
119 }
120 if ((err = _dl_snd_pcm_hw_params_set_format (playback_handle, hw_params, audio_format)) < 0) {
121 fprintf (stderr, "cannot set sample format (%s)\n", _dl_snd_strerror (err));
122 return err;
123 }
124 if ((err = _dl_snd_pcm_hw_params_set_rate_near (playback_handle, hw_params, &audio_sample_rate, 0)) < 0) {
125 fprintf (stderr, "cannot set sample rate (%s)\n", _dl_snd_strerror (err));
126 return err;
127 }
128 if ((err = _dl_snd_pcm_hw_params_set_channels (playback_handle, hw_params, audio_channels)) < 0) {
129 fprintf (stderr, "cannot set channel count (%s)\n", _dl_snd_strerror (err));
130 return err;
131 }
132 // set audio buffer size to about 125ms
133 buffer_size = (audio_sample_rate + 7) / 8;
134 if((err = _dl_snd_pcm_hw_params_set_buffer_size_near(playback_handle, hw_params, &buffer_size)) < 0) {
135 printf("Unable to set buffer size %li for playback: %s\n", buffer_size, _dl_snd_strerror(err));
136 return err;
137 }
138 if ((err = _dl_snd_pcm_hw_params (playback_handle, hw_params)) < 0) {
139 fprintf (stderr, "cannot set parameters (%s)\n", _dl_snd_strerror (err));
140 return err;
141 }
142 if((err = _dl_snd_pcm_hw_params_get_buffer_size(hw_params, &size)) < 0) {
143 printf("Unable to get buffer size for playback: %s\n", _dl_snd_strerror(err));
144 return err;
145 }
146 buffer_size = size;
147 return 0;
148 }
149
150 static int audio_set_sw_params(snd_pcm_sw_params_t *sw_params) {
151 int err;
152 snd_pcm_uframes_t boundary;
153
154 /* get the current swparams */
155 if((err = _dl_snd_pcm_sw_params_current(playback_handle, sw_params)) < 0) {
156 fprintf(stderr, "Unable to determine current swparams for playback: %s\n", _dl_snd_strerror(err));
157 return err;
158 }
159 if((err = _dl_snd_pcm_sw_params_set_silence_threshold(playback_handle, sw_params, 0)) < 0) {
160 fprintf(stderr, "Unable to set silence threshold for playback: %s\n", _dl_snd_strerror(err));
161 return err;
162 }
163 if((err = _dl_snd_pcm_sw_params_get_boundary(sw_params, &boundary)) < 0) {
164 fprintf(stderr, "unable to get ring pointer boundary for playback: %s\n", _dl_snd_strerror(err));
165 return err;
166 }
167 if((err = _dl_snd_pcm_sw_params_set_silence_size(playback_handle, sw_params, boundary)) < 0) {
168 fprintf(stderr, "Unable to set silence size playback: %s\n", _dl_snd_strerror(err));
169 return err;
170 }
171 if ((err = _dl_snd_pcm_sw_params (playback_handle, sw_params)) < 0) {
172 fprintf (stderr, "cannot set sw parameters (%s)\n", _dl_snd_strerror (err));
173 return err;
174 }
175
176 return 0;
177 }
178
179 static void *closer(void *) {
180 pthread_mutex_lock(&closer_mutex);
181 while (true) {
182 struct timespec when;
183 when.tv_sec = last_use.tv_sec + 5;
184 when.tv_nsec = last_use.tv_usec * 1000L;
185 struct timeval now;
186 gettimeofday(&now, NULL);
187 if (when.tv_sec < now.tv_sec || when.tv_sec == now.tv_sec && when.tv_nsec < now.tv_usec * 1000L)
188 break;
189 pthread_cond_timedwait(&closer_cond, &closer_mutex, &when);
190 }
191 _dl_snd_pcm_close(playback_handle);
192 audio_initialized = false;
193 pthread_mutex_unlock(&closer_mutex);
194 return NULL;
195 }
196
197 static bool open_libasound() {
198 void *lib = dlopen(ALSALIB, RTLD_NOW);
199 if (lib == NULL) {
200 fprintf(stderr, "Could not open " ALSALIB "\nusing gdk_beep() for BEEP and TONE.\n");
201 return false;
202 }
203 _dl_snd_pcm_close = (_ptr_snd_pcm_close) dlsym(lib, "snd_pcm_close");
204 _dl_snd_pcm_format_big_endian = (_ptr_snd_pcm_format_big_endian) dlsym(lib, "snd_pcm_format_big_endian");
205 _dl_snd_pcm_format_physical_width = (_ptr_snd_pcm_format_physical_width) dlsym(lib, "snd_pcm_format_physical_width");
206 _dl_snd_pcm_format_unsigned = (_ptr_snd_pcm_format_unsigned) dlsym(lib, "snd_pcm_format_unsigned");
207 _dl_snd_pcm_format_width = (_ptr_snd_pcm_format_width) dlsym(lib, "snd_pcm_format_width");
208 _dl_snd_pcm_hw_params = (_ptr_snd_pcm_hw_params) dlsym(lib, "snd_pcm_hw_params");
209 _dl_snd_pcm_hw_params_malloc = (_ptr_snd_pcm_hw_params_malloc) dlsym(lib, "snd_pcm_hw_params_malloc");
210 _dl_snd_pcm_hw_params_free = (_ptr_snd_pcm_hw_params_free) dlsym(lib, "snd_pcm_hw_params_free");
211 _dl_snd_pcm_hw_params_any = (_ptr_snd_pcm_hw_params_any) dlsym(lib, "snd_pcm_hw_params_any");
212 _dl_snd_pcm_hw_params_get_buffer_size = (_ptr_snd_pcm_hw_params_get_buffer_size) dlsym(lib, "snd_pcm_hw_params_get_buffer_size");
213 _dl_snd_pcm_hw_params_set_access = (_ptr_snd_pcm_hw_params_set_access) dlsym(lib, "snd_pcm_hw_params_set_access");
214 _dl_snd_pcm_hw_params_set_buffer_size_near = (_ptr_snd_pcm_hw_params_set_buffer_size_near) dlsym(lib, "snd_pcm_hw_params_set_buffer_size_near");
215 _dl_snd_pcm_hw_params_set_channels = (_ptr_snd_pcm_hw_params_set_channels) dlsym(lib, "snd_pcm_hw_params_set_channels");
216 _dl_snd_pcm_hw_params_set_format = (_ptr_snd_pcm_hw_params_set_format) dlsym(lib, "snd_pcm_hw_params_set_format");
217 _dl_snd_pcm_hw_params_set_rate_near = (_ptr_snd_pcm_hw_params_set_rate_near) dlsym(lib, "snd_pcm_hw_params_set_rate_near");
218 _dl_snd_pcm_open = (_ptr_snd_pcm_open) dlsym(lib, "snd_pcm_open");
219 _dl_snd_pcm_prepare = (_ptr_snd_pcm_prepare) dlsym(lib, "snd_pcm_prepare");
220 _dl_snd_pcm_resume = (_ptr_snd_pcm_resume) dlsym(lib, "snd_pcm_resume");
221 _dl_snd_pcm_sw_params = (_ptr_snd_pcm_sw_params) dlsym(lib, "snd_pcm_sw_params");
222 _dl_snd_pcm_sw_params_malloc = (_ptr_snd_pcm_sw_params_malloc) dlsym(lib, "snd_pcm_sw_params_malloc");
223 _dl_snd_pcm_sw_params_free = (_ptr_snd_pcm_sw_params_free) dlsym(lib, "snd_pcm_sw_params_free");
224 _dl_snd_pcm_sw_params_current = (_ptr_snd_pcm_sw_params_current) dlsym(lib, "snd_pcm_sw_params_current");
225 _dl_snd_pcm_sw_params_get_boundary = (_ptr_snd_pcm_sw_params_get_boundary) dlsym(lib, "snd_pcm_sw_params_get_boundary");
226 _dl_snd_pcm_sw_params_set_silence_size = (_ptr_snd_pcm_sw_params_set_silence_size) dlsym(lib, "snd_pcm_sw_params_set_silence_size");
227 _dl_snd_pcm_sw_params_set_silence_threshold = (_ptr_snd_pcm_sw_params_set_silence_threshold) dlsym(lib, "snd_pcm_sw_params_set_silence_threshold");
228 _dl_snd_pcm_writei = (_ptr_snd_pcm_writei) dlsym(lib, "snd_pcm_writei");
229 _dl_snd_strerror = (_ptr_snd_strerror) dlsym(lib, "snd_strerror");
230
231 if (dlerror() == NULL)
232 return true;
233 fprintf(stderr, "Could not load all required symbols from " ALSALIB "\nusing gdk_beep() for BEEP and TONE.\n");
234 dlclose(lib);
235 return false;
236 }
237
238 static bool audio_init() {
239 if (libasound_state == 0)
240 libasound_state = open_libasound() ? 1 : 2;
241 if (libasound_state == 2)
242 return false;
243
244 if (audio_initialized)
245 return true;
246
247 int err;
248
249 if ((err = _dl_snd_pcm_open(&playback_handle, audio_device, SND_PCM_STREAM_PLAYBACK, 0)) == 0) {
250 snd_pcm_hw_params_t *hw_params;
251 _dl_snd_pcm_hw_params_malloc(&hw_params);
252 err = audio_set_hw_params(hw_params);
253 _dl_snd_pcm_hw_params_free(hw_params);
254 if (err < 0) {
255 _dl_snd_pcm_close(playback_handle);
256 goto fail;
257 }
258
259 snd_pcm_sw_params_t *sw_params;
260 _dl_snd_pcm_sw_params_malloc(&sw_params);
261 err = audio_set_sw_params(sw_params);
262 _dl_snd_pcm_sw_params_free(sw_params);
263 if (err < 0) {
264 _dl_snd_pcm_close(playback_handle);
265 goto fail;
266 }
267 } else {
268 fail:
269 fprintf (stderr, "cannot open audio device %s (%s)\n", audio_device, _dl_snd_strerror(err));
270 return false;
271 }
272
273 audio_initialized = true;
274
275 pthread_create(&closer_thread, NULL, closer, NULL);
276 return true;
277 }
278
279 static int xrun_recovery(snd_pcm_t *handle, int err)
280 {
281 if (err == -EPIPE) { /* under-run */
282 err = _dl_snd_pcm_prepare(handle);
283 if (err < 0)
284 fprintf(stderr, "Can't recovery from underrun, prepare failed: %s\n", _dl_snd_strerror(err));
285 return 0;
286 } else if (err == -ESTRPIPE) {
287 while ((err = _dl_snd_pcm_resume(handle)) == -EAGAIN)
288 sleep(1); /* wait until the suspend flag is released */
289 if (err < 0) {
290 err = _dl_snd_pcm_prepare(handle);
291 if (err < 0)
292 fprintf(stderr, "Can't recovery from suspend, prepare failed: %s\n", _dl_snd_strerror(err));
293 }
294 return 0;
295 }
296 return err;
297 }
298
299 bool alsa_beeper(int frequency, int duration) {
300 pthread_mutex_lock(&closer_mutex);
301 if(!audio_init()) {
302 pthread_mutex_unlock(&closer_mutex);
303 return false;
304 }
305
306 /* roughly benchmark the function so that we can return only after the
307 * sound has been played */
308 struct timeval begin, end;
309 gettimeofday(&begin, NULL);
310
311 int format_bits = _dl_snd_pcm_format_width(audio_format);
312 unsigned int maxval = (1 << (format_bits - 1)) - 1;
313 int bps = format_bits / 8;
314 int phys_bps = _dl_snd_pcm_format_physical_width(audio_format) / 8;
315 int big_endian = _dl_snd_pcm_format_big_endian(audio_format) == 1;
316 int to_unsigned = _dl_snd_pcm_format_unsigned(audio_format) == 1;
317 int numSamples = duration * audio_sample_rate / 1000;
318 int bufferSize, err, x;
319 char *buffer, *p;
320
321 bufferSize = numSamples * audio_channels * phys_bps;
322 buffer = (char *)malloc(bufferSize);
323 if(buffer != NULL) {
324 // generate a triangle waveform
325 p = buffer;
326 for(x = 0; x < numSamples; x++) {
327 int res, i, chn;
328 double v;
329
330 v = fmod(((double) x) / audio_sample_rate * frequency, 1);
331 if (v >= 0.75)
332 v -= 1;
333 else if (v >= 0.25)
334 v = 0.5 - v;
335 res = (int) (v * maxval);
336 if (to_unsigned)
337 res ^= 1U << (format_bits - 1);
338 for(chn = 0; chn < audio_channels; chn ++) {
339 if (big_endian) {
340 for (i = 0; i < bps; i++)
341 *(p + phys_bps - 1 - i) = (res >> i * 8) & 0xff;
342 } else {
343 for (i = 0; i < bps; i++)
344 *(p + i) = (res >> i * 8) & 0xff;
345 }
346 p += phys_bps;
347 }
348 }
349
350 /* play the waveform */
351 p = buffer;
352 while(numSamples > 0) {
353 if((err = _dl_snd_pcm_writei (playback_handle, p, numSamples)) < 0) {
354 if(err == -EAGAIN) {
355 continue;
356 }
357 if((err = xrun_recovery(playback_handle, err)) < 0) {
358 fprintf (stderr, "write to audio interface failed (%s)\n", _dl_snd_strerror (err));
359 break;
360 }
361 }
362 else {
363 numSamples -= err;
364 p += err * phys_bps * audio_channels;
365 }
366 }
367
368 free(buffer);
369
370 gettimeofday(&end, NULL);
371 duration -= (int) TDIFF(begin, end);
372 if(duration > 0) usleep(duration * 1000);
373 }
374
375 gettimeofday(&last_use, NULL);
376 pthread_mutex_unlock(&closer_mutex);
377 return true;
378 }