2 * This file is part of the libsigrok project.
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>
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.
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.
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
23 #include "libsigrok.h"
24 #include "libsigrok-internal.h"
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.
38 static const unsigned int rates[] = {
52 384000, /* Yes, there are sound cards that go this high. */
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)
60 struct drv_context *drvc = NULL;
61 struct sr_dev_inst *sdi = NULL;
62 struct dev_context *devc = NULL;
63 struct sr_probe *probe;
65 unsigned int i, offset, channels, minrate, maxrate, rate;
66 uint64_t hwrates[ARRAY_SIZE(rates)];
67 uint64_t *devrates = NULL;
68 snd_pcm_t *temp_handle = NULL;
69 snd_pcm_hw_params_t *hw_params = NULL;
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
80 * situation, and pass this object in our dev_context->hw_params,
81 * eliminating the need to free() it and malloc() it later.
83 ret = snd_pcm_open(&temp_handle, alsaname, SND_PCM_STREAM_CAPTURE, 0);
85 sr_err("Cannot open device: %s.", snd_strerror(ret));
86 goto scan_error_cleanup;
89 ret = snd_pcm_hw_params_malloc(&hw_params);
91 sr_err("Error allocating hardware parameter structure: %s.",
93 goto scan_error_cleanup;
96 ret = snd_pcm_hw_params_any(temp_handle, hw_params);
98 sr_err("Error initializing hardware parameter structure: %s.",
100 goto scan_error_cleanup;
103 snd_pcm_hw_params_get_channels_max(hw_params, &channels);
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.
109 snd_pcm_hw_params_get_rate_min(hw_params, &minrate, 0);
110 snd_pcm_hw_params_get_rate_max(hw_params, &maxrate, 0);
111 for (i = 0, offset = 0; i < ARRAY_SIZE(rates); i++) {
117 ret = snd_pcm_hw_params_test_rate(temp_handle, hw_params,
120 hwrates[offset++] = rate;
122 hwrates[offset++] = 0;
124 if ((ret = snd_pcm_close(temp_handle)) < 0)
125 sr_err("Failed to close device: %s.", snd_strerror(ret));
129 * Now we are done querying the hardware parameters.
130 * If we made it here, we know everything we want to know, and it's
131 * time to create our sigrok device.
133 sr_info("Device %s has %d channels.", alsaname, channels);
134 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "ALSA:",
135 cardname, snd_pcm_info_get_name(pcminfo)))) {
136 sr_err("Failed to create device instance.");
137 goto scan_error_cleanup;
139 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
140 sr_err("Device context malloc failed.");
141 goto scan_error_cleanup;
143 if (!(devrates = g_try_malloc(offset * sizeof(uint64_t)))) {
144 sr_err("Samplerate list malloc failed.");
145 goto scan_error_cleanup;
148 devc->hwdev = g_strdup(alsaname);
149 devc->num_probes = channels;
150 devc->hw_params = hw_params;
151 memcpy(devrates, hwrates, offset * sizeof(uint64_t));
152 devc->supp_rates.list = devrates;
157 for (i = 0; i < devc->num_probes; i++) {
158 snprintf(p_name, sizeof(p_name), "Ch_%d", i);
159 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, p_name)))
160 goto scan_error_cleanup;
161 sdi->probes = g_slist_append(sdi->probes, probe);
164 drvc->instances = g_slist_append(drvc->instances, sdi);
165 *devices = g_slist_append(*devices, sdi);
177 sr_dev_inst_free(sdi);
179 snd_pcm_hw_params_free(hw_params);
181 if ((ret = snd_pcm_close(temp_handle)) < 0) {
182 sr_err("Failed to close device: %s.",
188 * Scan all alsa devices, and translate them to sigrok devices.
190 * Each alsa device (not alsa card) gets its own sigrok device.
193 * hw:1,0 == sigrok device 0
194 * hw:1,1 == sigrok device 1
195 * hw:2,0 == sigrok device 2
196 * hw:2,1 == sigrok device 3
197 * hw:2,2 == sigrok device 4
200 * We don't currently look at alsa subdevices. We only use subdevice 0.
201 * Every input device will have its own channels (left, right, etc). Each of
202 * those channels gets mapped to a different sigrok probe. A device with 4
203 * channels will have 4 probes from sigrok's perspective.
205 SR_PRIV GSList *alsa_scan(GSList *options, struct sr_dev_driver *di)
207 GSList *devices = NULL;
210 snd_ctl_card_info_t *info;
211 snd_pcm_info_t *pcminfo;
212 const char *cardname;
213 char hwcard[32], hwdev[32];
218 if ((ret = snd_ctl_card_info_malloc(&info)) < 0) {
219 sr_err("Failed to malloc card info: %s.", snd_strerror(ret));
222 if ((ret = snd_pcm_info_malloc(&pcminfo) < 0)) {
223 sr_err("Cannot malloc pcm info: %s.", snd_strerror(ret));
228 while (snd_card_next(&card) >= 0 && card >= 0) {
229 snprintf(hwcard, sizeof(hwcard), "hw:%d", card);
230 if ((ret = snd_ctl_open(&handle, hwcard, 0)) < 0) {
231 sr_err("Cannot open (%d): %s.", card, snd_strerror(ret));
234 if ((ret = snd_ctl_card_info(handle, info)) < 0) {
235 sr_err("Cannot get hardware info (%d): %s.",
236 card, snd_strerror(ret));
237 if ((ret = snd_ctl_close(handle)) < 0) {
238 sr_err("Cannot close device (%d): %s.",
239 card, snd_strerror(ret));
244 while (snd_ctl_pcm_next_device(handle, &dev) >= 0 && dev >= 0) {
245 snprintf(hwdev, sizeof(hwdev), "%s,%d", hwcard, dev);
247 * TODO: We always use subdevice 0, but we have yet to
248 * explore the possibilities opened up by other
249 * subdevices. Most hardware only has subdevice 0.
251 snd_pcm_info_set_device(pcminfo, dev);
252 snd_pcm_info_set_subdevice(pcminfo, 0);
253 snd_pcm_info_set_stream(pcminfo,
254 SND_PCM_STREAM_CAPTURE);
255 if ((ret = snd_ctl_pcm_info(handle, pcminfo)) < 0) {
256 sr_err("Cannot get device info (%s): %s.",
257 hwdev, snd_strerror(ret));
261 cardname = snd_ctl_card_info_get_name(info);
262 sr_info("card %d: %s [%s], device %d: %s [%s]",
263 card, snd_ctl_card_info_get_id(info), cardname,
264 dev, snd_pcm_info_get_id(pcminfo),
265 snd_pcm_info_get_name(pcminfo));
267 alsa_scan_handle_dev(&devices, cardname, hwdev,
270 if ((ret = snd_ctl_close(handle)) < 0) {
271 sr_err("Cannot close device (%d): %s.",
272 card, snd_strerror(ret));
276 snd_pcm_info_free(pcminfo);
277 snd_ctl_card_info_free(info);
283 * Helper to be used with g_slist_free_full(); for properly freeing an alsa
286 SR_PRIV void alsa_dev_inst_clear(struct sr_dev_inst *sdi)
288 struct dev_context *devc;
290 if (!(devc = sdi->priv))
293 snd_pcm_hw_params_free(devc->hw_params);
294 g_free((void*)devc->supp_rates.list);
295 sr_dev_inst_free(sdi);
299 * Set the samplerate of the ALSA device.
301 * Changes the samplerate of the given ALSA device if the specified samplerate
302 * is supported by the hardware.
304 * The new samplerate is recorded, but it is not applied to the hardware. The
305 * samplerate is applied to the hardware only when acquisition is started via
306 * dev_acquisition_start(), and cannot be changed during acquisition. To change
307 * the samplerate, several steps are needed:
309 * 1) If acquisition is running, it must first be stopped.
310 * 2) dev_config_set() must be called with the new samplerate.
311 * 3) When starting a new acquisition, the new samplerate is applied.
314 SR_PRIV int alsa_set_samplerate(const struct sr_dev_inst *sdi,
317 struct dev_context *devc;
321 if (!(devc = sdi->priv))
326 if (newrate == devc->supp_rates.list[i]) {
330 } while (devc->supp_rates.list[i++] != 0);
333 sr_err("Sample rate %" PRIu64 " not supported.", newrate);
337 devc->cur_samplerate = rate;
341 SR_PRIV int alsa_receive_data(int fd, int revents, void *cb_data)
343 struct sr_dev_inst *sdi;
344 struct dev_context *devc;
345 struct sr_datafeed_packet packet;
346 struct sr_datafeed_analog analog;
348 int i, x, count, offset, samples_to_get;
350 const float s16norm = 1 / (float)(1 << 15);
358 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
359 memset(inbuf, 0, sizeof(inbuf));
361 samples_to_get = MIN(4096 / 4, devc->limit_samples);
363 sr_spew("Getting %d samples from audio device.", samples_to_get);
364 count = snd_pcm_readi(devc->capture_handle, inbuf, samples_to_get);
367 sr_err("Failed to read samples: %s.", snd_strerror(count));
369 } else if (count != samples_to_get) {
370 sr_spew("Only got %d/%d samples.", count, samples_to_get);
373 analog.data = g_try_malloc0(count * sizeof(float) * devc->num_probes);
375 sr_err("Failed to malloc sample buffer.");
381 * It's impossible to know what voltage levels the soundcard handles.
382 * Some handle 0 dBV rms, some 0dBV peak-to-peak, +4dbmW (600 ohm), etc
383 * Each of these corresponds to a different voltage, and there is no
384 * mechanism to determine this voltage. The best solution is to send all
385 * audio data as a normalized float, and let the frontend or user worry
386 * about the calibration.
388 for (i = 0; i < count; i += devc->num_probes) {
389 for (x = 0; x < devc->num_probes; x++) {
390 tmp16 = inbuf[i + x];
391 analog.data[offset++] = tmp16 * s16norm;
395 /* Send a sample packet with the analog values. */
396 analog.num_samples = count;
397 analog.mq = SR_MQ_VOLTAGE; /* FIXME */
398 analog.unit = SR_UNIT_VOLT; /* FIXME */
399 packet.type = SR_DF_ANALOG;
400 packet.payload = &analog;
401 sr_session_send(devc->cb_data, &packet);
405 devc->num_samples += count;
407 /* Stop acquisition if we acquired enough samples. */
408 if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
409 sr_info("Requested number of samples reached.");
410 sdi->driver->dev_acquisition_stop(sdi, cb_data);