]>
Commit | Line | Data |
---|---|---|
9cd9f6b7 AG |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com> | |
5 | * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de> | |
6 | * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
21 | */ | |
22 | ||
9cd9f6b7 AG |
23 | #include "libsigrok.h" |
24 | #include "libsigrok-internal.h" | |
721ecf3d | 25 | #include "protocol.h" |
9cd9f6b7 | 26 | |
65faa197 AG |
27 | /* |
28 | * There is no way to get a list of supported samplerates from ALSA. We could | |
29 | * use the 'plughw' interface of ALSA, in which case any format and/or | |
30 | * samplerate conversion would be performed by ALSA. However, we are interested | |
31 | * in the hardware capabilities, and have the infrastructure in sigrok to do so. | |
32 | * We therefore use the 'hw' interface. The downside is that the code gets a | |
33 | * little bulkier, as we have to keep track of the hardware capabilities, and | |
34 | * only use those that the hardware supports. Case in point, ALSA will not give | |
35 | * us a list of capabilities; we have to test for each one individually. Hence, | |
36 | * we keep lists of the capabilities we are interested in. | |
37 | */ | |
38 | static const unsigned int rates[] = { | |
39 | 5512, | |
40 | 8000, | |
41 | 11025, | |
42 | 16000, | |
43 | 22050, | |
44 | 32000, | |
45 | 44100, | |
46 | 48000, | |
47 | 64000, | |
48 | 88200, | |
49 | 96000, | |
50 | 176400, | |
51 | 192000, | |
721ecf3d | 52 | 384000, /* Yes, there are sound cards that go this high. */ |
65faa197 AG |
53 | }; |
54 | ||
6944b2d0 AG |
55 | static void alsa_scan_handle_dev(GSList **devices, |
56 | const char *cardname, const char *alsaname, | |
57 | struct sr_dev_driver *di, | |
58 | snd_pcm_info_t *pcminfo) | |
59 | { | |
60 | struct drv_context *drvc = NULL; | |
61 | struct sr_dev_inst *sdi = NULL; | |
62 | struct dev_context *devc = NULL; | |
63 | struct sr_probe *probe; | |
64 | int ret; | |
65faa197 AG |
65 | unsigned int i, offset, channels, minrate, maxrate, rate; |
66 | uint64_t hwrates[ARRAY_SIZE(rates)]; | |
67 | uint64_t *devrates = NULL; | |
6944b2d0 AG |
68 | snd_pcm_t *temp_handle = NULL; |
69 | snd_pcm_hw_params_t *hw_params = NULL; | |
721ecf3d | 70 | char p_name[32]; |
6944b2d0 AG |
71 | |
72 | drvc = di->priv; | |
73 | ||
74 | /* | |
65faa197 AG |
75 | * Get hardware parameters: |
76 | * The number of channels, for example, are our sigrok probes. Getting | |
77 | * this information needs a detour. We need to open the device, then | |
78 | * query it and/or test different parameters. A side-effect of is that | |
79 | * we create a snd_pcm_hw_params_t object. We take advantage of the | |
6944b2d0 AG |
80 | * situation, and pass this object in our dev_context->hw_params, |
81 | * eliminating the need to free() it and malloc() it later. | |
82 | */ | |
83 | ret = snd_pcm_open(&temp_handle, alsaname, SND_PCM_STREAM_CAPTURE, 0); | |
84 | if (ret < 0) { | |
85 | sr_err("Cannot open device: %s.", snd_strerror(ret)); | |
86 | goto scan_error_cleanup; | |
87 | } | |
88 | ||
89 | ret = snd_pcm_hw_params_malloc(&hw_params); | |
90 | if (ret < 0) { | |
91 | sr_err("Error allocating hardware parameter structure: %s.", | |
92 | snd_strerror(ret)); | |
93 | goto scan_error_cleanup; | |
94 | } | |
95 | ||
96 | ret = snd_pcm_hw_params_any(temp_handle, hw_params); | |
97 | if (ret < 0) { | |
98 | sr_err("Error initializing hardware parameter structure: %s.", | |
99 | snd_strerror(ret)); | |
100 | goto scan_error_cleanup; | |
101 | } | |
102 | ||
103 | snd_pcm_hw_params_get_channels_max(hw_params, &channels); | |
104 | ||
65faa197 AG |
105 | /* |
106 | * We need to test if each samplerate between min and max is supported. | |
107 | * Unfortunately, ALSA won't just throw a list at us. | |
108 | */ | |
109 | snd_pcm_hw_params_get_rate_min(hw_params, &minrate, 0); | |
110 | snd_pcm_hw_params_get_rate_max(hw_params, &maxrate, 0); | |
721ecf3d | 111 | for (i = 0, offset = 0; i < ARRAY_SIZE(rates); i++) { |
65faa197 AG |
112 | rate = rates[i]; |
113 | if (rate < minrate) | |
114 | continue; | |
115 | if (rate > maxrate) | |
116 | break; | |
117 | ret = snd_pcm_hw_params_test_rate(temp_handle, hw_params, | |
118 | rate, 0); | |
119 | if (ret >= 0) | |
120 | hwrates[offset++] = rate; | |
121 | } | |
122 | hwrates[offset++] = 0; | |
123 | ||
6944b2d0 AG |
124 | snd_pcm_close(temp_handle); |
125 | temp_handle = NULL; | |
126 | ||
127 | /* | |
65faa197 | 128 | * Now we are done querying the hardware parameters. |
721ecf3d UH |
129 | * If we made it here, we know everything we want to know, and it's |
130 | * time to create our sigrok device. | |
6944b2d0 AG |
131 | */ |
132 | sr_info("Device %s has %d channels.", alsaname, channels); | |
133 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "ALSA:", | |
134 | cardname, snd_pcm_info_get_name(pcminfo)))) { | |
721ecf3d | 135 | sr_err("Failed to create device instance."); |
6944b2d0 AG |
136 | goto scan_error_cleanup; |
137 | } | |
138 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
139 | sr_err("Device context malloc failed."); | |
140 | goto scan_error_cleanup; | |
141 | } | |
65faa197 AG |
142 | if (!(devrates = g_try_malloc(offset * sizeof(uint64_t)))) { |
143 | sr_err("Samplerate list malloc failed."); | |
144 | goto scan_error_cleanup; | |
145 | } | |
6944b2d0 AG |
146 | |
147 | devc->hwdev = g_strdup(alsaname); | |
148 | devc->num_probes = channels; | |
149 | devc->hw_params = hw_params; | |
65faa197 AG |
150 | memcpy(devrates, hwrates, offset * sizeof(uint64_t)); |
151 | devc->supp_rates.list = devrates; | |
152 | ||
6944b2d0 AG |
153 | sdi->priv = devc; |
154 | sdi->driver = di; | |
155 | ||
156 | for (i = 0; i < devc->num_probes; i++) { | |
6944b2d0 AG |
157 | snprintf(p_name, sizeof(p_name), "Ch_%d", i); |
158 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, p_name))) | |
159 | goto scan_error_cleanup; | |
160 | sdi->probes = g_slist_append(sdi->probes, probe); | |
161 | } | |
162 | ||
163 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
164 | *devices = g_slist_append(*devices, sdi); | |
165 | return; | |
166 | ||
167 | scan_error_cleanup: | |
168 | if (devc) { | |
169 | if (devc->hwdev) | |
721ecf3d | 170 | g_free(devc->hwdev); |
6944b2d0 AG |
171 | g_free(devc); |
172 | } | |
65faa197 AG |
173 | if (devrates) |
174 | g_free(devrates); | |
6944b2d0 AG |
175 | if (sdi) |
176 | sr_dev_inst_free(sdi); | |
177 | if (hw_params) | |
178 | snd_pcm_hw_params_free(hw_params); | |
179 | if (temp_handle) | |
180 | snd_pcm_close(temp_handle); | |
6944b2d0 AG |
181 | } |
182 | ||
183 | /** | |
721ecf3d UH |
184 | * Scan all alsa devices, and translate them to sigrok devices. |
185 | * | |
186 | * Each alsa device (not alsa card) gets its own sigrok device. | |
6944b2d0 | 187 | * |
6944b2d0 AG |
188 | * For example, |
189 | * hw:1,0 == sigrok device 0 | |
190 | * hw:1,1 == sigrok device 1 | |
191 | * hw:2,0 == sigrok device 2 | |
192 | * hw:2,1 == sigrok device 3 | |
193 | * hw:2,2 == sigrok device 4 | |
194 | * [...] | |
721ecf3d | 195 | * |
6944b2d0 | 196 | * We don't currently look at alsa subdevices. We only use subdevice 0. |
721ecf3d | 197 | * Every input device will have its own channels (left, right, etc). Each of |
6944b2d0 AG |
198 | * those channels gets mapped to a different sigrok probe. A device with 4 |
199 | * channels will have 4 probes from sigrok's perspective. | |
200 | */ | |
201 | SR_PRIV GSList *alsa_scan(GSList *options, struct sr_dev_driver *di) | |
202 | { | |
203 | GSList *devices = NULL; | |
204 | snd_ctl_t *handle; | |
205 | int card, ret, dev; | |
206 | snd_ctl_card_info_t *info; | |
207 | snd_pcm_info_t *pcminfo; | |
721ecf3d UH |
208 | const char *cardname; |
209 | char hwcard[32], hwdev[32]; | |
210 | ||
6944b2d0 AG |
211 | /* TODO */ |
212 | (void)options; | |
213 | ||
214 | if (snd_ctl_card_info_malloc(&info) < 0) { | |
215 | sr_err("Cannot malloc card info."); | |
216 | return NULL; | |
217 | } | |
218 | if (snd_pcm_info_malloc(&pcminfo) < 0) { | |
219 | sr_err("Cannot malloc pcm info."); | |
220 | return NULL; | |
221 | } | |
222 | ||
223 | card = -1; | |
224 | while (snd_card_next(&card) >= 0 && card >= 0) { | |
6944b2d0 AG |
225 | snprintf(hwcard, sizeof(hwcard), "hw:%d", card); |
226 | if ((ret = snd_ctl_open(&handle, hwcard, 0)) < 0) { | |
721ecf3d | 227 | sr_err("Cannot open (%d): %s.", card, snd_strerror(ret)); |
6944b2d0 AG |
228 | continue; |
229 | } | |
230 | if ((ret = snd_ctl_card_info(handle, info)) < 0) { | |
721ecf3d | 231 | sr_err("Cannot get hardware info (%d): %s.", |
6944b2d0 AG |
232 | card, snd_strerror(ret)); |
233 | snd_ctl_close(handle); | |
234 | continue; | |
235 | } | |
236 | dev = -1; | |
237 | while (snd_ctl_pcm_next_device(handle, &dev) >= 0 && dev >= 0) { | |
6944b2d0 AG |
238 | snprintf(hwdev, sizeof(hwdev), "%s,%d", hwcard, dev); |
239 | /* | |
240 | * TODO: We always use subdevice 0, but we have yet to | |
241 | * explore the possibilities opened up by other | |
242 | * subdevices. Most hardware only has subdevice 0. | |
243 | */ | |
244 | snd_pcm_info_set_device(pcminfo, dev); | |
245 | snd_pcm_info_set_subdevice(pcminfo, 0); | |
246 | snd_pcm_info_set_stream(pcminfo, | |
247 | SND_PCM_STREAM_CAPTURE); | |
248 | if ((ret = snd_ctl_pcm_info(handle, pcminfo)) < 0) { | |
721ecf3d | 249 | sr_err("Cannot get device info: %s.", |
6944b2d0 AG |
250 | snd_strerror(ret)); |
251 | continue; | |
252 | } | |
253 | ||
254 | cardname = snd_ctl_card_info_get_name(info); | |
721ecf3d | 255 | sr_info("card %d: %s [%s], device %d: %s [%s]", |
6944b2d0 AG |
256 | card, snd_ctl_card_info_get_id(info), cardname, |
257 | dev, snd_pcm_info_get_id(pcminfo), | |
258 | snd_pcm_info_get_name(pcminfo)); | |
259 | ||
260 | alsa_scan_handle_dev(&devices, cardname, hwdev, | |
261 | di, pcminfo); | |
262 | } | |
263 | snd_ctl_close(handle); | |
264 | } | |
265 | ||
266 | snd_pcm_info_free(pcminfo); | |
267 | snd_ctl_card_info_free(info); | |
268 | ||
269 | return devices; | |
270 | } | |
271 | ||
272 | /* | |
273 | * Helper to be used with g_slist_free_full(); for properly freeing an alsa | |
274 | * dev instance. | |
275 | */ | |
276 | SR_PRIV void alsa_dev_inst_clear(struct sr_dev_inst *sdi) | |
277 | { | |
278 | struct dev_context *devc; | |
279 | ||
280 | if (!(devc = sdi->priv)) | |
281 | return; | |
282 | ||
283 | snd_pcm_hw_params_free(devc->hw_params); | |
65faa197 | 284 | g_free((void*)devc->supp_rates.list); |
6944b2d0 AG |
285 | sr_dev_inst_free(sdi); |
286 | } | |
287 | ||
721ecf3d UH |
288 | /** |
289 | * Set the samplerate of the ALSA device. | |
65faa197 AG |
290 | * |
291 | * Changes the samplerate of the given ALSA device if the specified samplerate | |
292 | * is supported by the hardware. | |
721ecf3d | 293 | * |
65faa197 AG |
294 | * The new samplerate is recorded, but it is not applied to the hardware. The |
295 | * samplerate is applied to the hardware only when acquisition is started via | |
296 | * dev_acquisition_start(), and cannot be changed during acquisition. To change | |
297 | * the samplerate, several steps are needed: | |
721ecf3d | 298 | * |
65faa197 AG |
299 | * 1) If acquisition is running, it must first be stopped. |
300 | * 2) dev_config_set() must be called with the new samplerate. | |
301 | * 3) When starting a new acquisition, the new samplerate is applied. | |
721ecf3d | 302 | * |
65faa197 AG |
303 | */ |
304 | SR_PRIV int alsa_set_samplerate(const struct sr_dev_inst *sdi, | |
721ecf3d | 305 | uint64_t newrate) |
65faa197 AG |
306 | { |
307 | struct dev_context *devc; | |
308 | size_t i; | |
309 | uint64_t rate = 0; | |
310 | ||
311 | if (!(devc = sdi->priv)) | |
312 | return SR_ERR_ARG; | |
313 | ||
314 | i = 0; | |
315 | do { | |
316 | if (newrate == devc->supp_rates.list[i]) { | |
317 | rate = newrate; | |
318 | break; | |
319 | } | |
320 | } while (devc->supp_rates.list[i++] != 0); | |
321 | ||
322 | if (!rate) { | |
323 | sr_err("Sample rate " PRIu64 " not supported.", newrate); | |
324 | return SR_ERR_ARG; | |
325 | } | |
326 | ||
327 | devc->cur_samplerate = rate; | |
328 | return SR_OK; | |
329 | } | |
330 | ||
9cd9f6b7 AG |
331 | SR_PRIV int alsa_receive_data(int fd, int revents, void *cb_data) |
332 | { | |
333 | struct sr_dev_inst *sdi; | |
334 | struct dev_context *devc; | |
335 | struct sr_datafeed_packet packet; | |
336 | struct sr_datafeed_analog analog; | |
729850c9 | 337 | int16_t inbuf[4096]; |
9cd9f6b7 | 338 | int i, x, count, offset, samples_to_get; |
729850c9 | 339 | int16_t tmp16; |
721ecf3d | 340 | const float s16norm = 1 / (float)(1 << 15); |
9cd9f6b7 AG |
341 | |
342 | (void)fd; | |
343 | (void)revents; | |
344 | ||
345 | sdi = cb_data; | |
346 | devc = sdi->priv; | |
347 | ||
348 | memset(&analog, 0, sizeof(struct sr_datafeed_analog)); | |
349 | memset(inbuf, 0, sizeof(inbuf)); | |
350 | ||
351 | samples_to_get = MIN(4096 / 4, devc->limit_samples); | |
352 | ||
353 | sr_spew("Getting %d samples from audio device.", samples_to_get); | |
354 | count = snd_pcm_readi(devc->capture_handle, inbuf, samples_to_get); | |
355 | ||
356 | if (count < 0) { | |
357 | sr_err("Failed to read samples: %s.", snd_strerror(count)); | |
358 | return FALSE; | |
359 | } else if (count != samples_to_get) { | |
360 | sr_spew("Only got %d/%d samples.", count, samples_to_get); | |
361 | } | |
362 | ||
363 | analog.data = g_try_malloc0(count * sizeof(float) * devc->num_probes); | |
364 | if (!analog.data) { | |
365 | sr_err("Failed to malloc sample buffer."); | |
366 | return FALSE; | |
367 | } | |
368 | ||
369 | offset = 0; | |
729850c9 AG |
370 | /* |
371 | * It's impossible to know what voltage levels the soundcard handles. | |
372 | * Some handle 0 dBV rms, some 0dBV peak-to-peak, +4dbmW (600 ohm), etc | |
373 | * Each of these corresponds to a different voltage, and there is no | |
374 | * mechanism to determine this voltage. The best solution is to send all | |
375 | * audio data as a normalized float, and let the frontend or user worry | |
376 | * about the calibration. | |
377 | */ | |
378 | for (i = 0; i < count; i += devc->num_probes) { | |
9cd9f6b7 | 379 | for (x = 0; x < devc->num_probes; x++) { |
721ecf3d | 380 | tmp16 = inbuf[i + x]; |
729850c9 | 381 | analog.data[offset++] = tmp16 * s16norm; |
9cd9f6b7 AG |
382 | } |
383 | } | |
384 | ||
385 | /* Send a sample packet with the analog values. */ | |
386 | analog.num_samples = count; | |
387 | analog.mq = SR_MQ_VOLTAGE; /* FIXME */ | |
388 | analog.unit = SR_UNIT_VOLT; /* FIXME */ | |
389 | packet.type = SR_DF_ANALOG; | |
390 | packet.payload = &analog; | |
391 | sr_session_send(devc->cb_data, &packet); | |
392 | ||
393 | g_free(analog.data); | |
394 | ||
395 | devc->num_samples += count; | |
396 | ||
397 | /* Stop acquisition if we acquired enough samples. */ | |
35e199da UH |
398 | if (devc->limit_samples && devc->num_samples >= devc->limit_samples) { |
399 | sr_info("Requested number of samples reached."); | |
400 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
9cd9f6b7 AG |
401 | } |
402 | ||
403 | return TRUE; | |
404 | } |