]> sigrok.org Git - libsigrok.git/blame - hardware/alsa/protocol.c
alsa: Fix sample acquisition and send normalized values
[libsigrok.git] / hardware / alsa / protocol.c
CommitLineData
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
23#include "protocol.h"
24#include "libsigrok.h"
25#include "libsigrok-internal.h"
26
6944b2d0
AG
27static void alsa_scan_handle_dev(GSList **devices,
28 const char *cardname, const char *alsaname,
29 struct sr_dev_driver *di,
30 snd_pcm_info_t *pcminfo)
31{
32 struct drv_context *drvc = NULL;
33 struct sr_dev_inst *sdi = NULL;
34 struct dev_context *devc = NULL;
35 struct sr_probe *probe;
36 int ret;
37 unsigned int i, channels;
38 snd_pcm_t *temp_handle = NULL;
39 snd_pcm_hw_params_t *hw_params = NULL;
40
41 drvc = di->priv;
42
43 /*
44 * Get the number of input channels. Those are our sigrok probes.
45 * Getting this information needs a detour. We need to open the device,
46 * then query it for the number of channels. A side-effect of is that we
47 * create a snd_pcm_hw_params_t object. We take advantage of the
48 * situation, and pass this object in our dev_context->hw_params,
49 * eliminating the need to free() it and malloc() it later.
50 */
51 ret = snd_pcm_open(&temp_handle, alsaname, SND_PCM_STREAM_CAPTURE, 0);
52 if (ret < 0) {
53 sr_err("Cannot open device: %s.", snd_strerror(ret));
54 goto scan_error_cleanup;
55 }
56
57 ret = snd_pcm_hw_params_malloc(&hw_params);
58 if (ret < 0) {
59 sr_err("Error allocating hardware parameter structure: %s.",
60 snd_strerror(ret));
61 goto scan_error_cleanup;
62 }
63
64 ret = snd_pcm_hw_params_any(temp_handle, hw_params);
65 if (ret < 0) {
66 sr_err("Error initializing hardware parameter structure: %s.",
67 snd_strerror(ret));
68 goto scan_error_cleanup;
69 }
70
71 snd_pcm_hw_params_get_channels_max(hw_params, &channels);
72
73 snd_pcm_close(temp_handle);
74 temp_handle = NULL;
75
76 /*
77 * Now we are done querying the number of channels
78 * If we made it here, then it's time to create our sigrok device.
79 */
80 sr_info("Device %s has %d channels.", alsaname, channels);
81 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "ALSA:",
82 cardname, snd_pcm_info_get_name(pcminfo)))) {
83 sr_err("Device instance malloc failed.");
84 goto scan_error_cleanup;
85 }
86 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
87 sr_err("Device context malloc failed.");
88 goto scan_error_cleanup;
89 }
90
91 devc->hwdev = g_strdup(alsaname);
92 devc->num_probes = channels;
93 devc->hw_params = hw_params;
94 sdi->priv = devc;
95 sdi->driver = di;
96
97 for (i = 0; i < devc->num_probes; i++) {
98 char p_name[32];
99 snprintf(p_name, sizeof(p_name), "Ch_%d", i);
100 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, p_name)))
101 goto scan_error_cleanup;
102 sdi->probes = g_slist_append(sdi->probes, probe);
103 }
104
105 drvc->instances = g_slist_append(drvc->instances, sdi);
106 *devices = g_slist_append(*devices, sdi);
107 return;
108
109scan_error_cleanup:
110 if (devc) {
111 if (devc->hwdev)
112 g_free((void*)devc->hwdev);
113 g_free(devc);
114 }
115 if (sdi)
116 sr_dev_inst_free(sdi);
117 if (hw_params)
118 snd_pcm_hw_params_free(hw_params);
119 if (temp_handle)
120 snd_pcm_close(temp_handle);
121 return;
122}
123
124/**
125 * \brief Scan all alsa devices, and translate them to sigrok devices
126 *
127 * Each alsa device (not alsa card) gets its own sigrok device
128 * For example,
129 * hw:1,0 == sigrok device 0
130 * hw:1,1 == sigrok device 1
131 * hw:2,0 == sigrok device 2
132 * hw:2,1 == sigrok device 3
133 * hw:2,2 == sigrok device 4
134 * [...]
135 * \n
136 * We don't currently look at alsa subdevices. We only use subdevice 0.
137 * Every input device will have a its own channels (Left, Right, etc). Each of
138 * those channels gets mapped to a different sigrok probe. A device with 4
139 * channels will have 4 probes from sigrok's perspective.
140 */
141SR_PRIV GSList *alsa_scan(GSList *options, struct sr_dev_driver *di)
142{
143 GSList *devices = NULL;
144 snd_ctl_t *handle;
145 int card, ret, dev;
146 snd_ctl_card_info_t *info;
147 snd_pcm_info_t *pcminfo;
148 const char* cardname;
149 /* TODO */
150 (void)options;
151
152 if (snd_ctl_card_info_malloc(&info) < 0) {
153 sr_err("Cannot malloc card info.");
154 return NULL;
155 }
156 if (snd_pcm_info_malloc(&pcminfo) < 0) {
157 sr_err("Cannot malloc pcm info.");
158 return NULL;
159 }
160
161 card = -1;
162 while (snd_card_next(&card) >= 0 && card >= 0) {
163 char hwcard[32];
164 snprintf(hwcard, sizeof(hwcard), "hw:%d", card);
165 if ((ret = snd_ctl_open(&handle, hwcard, 0)) < 0) {
166 sr_err("Cannot open (%i): %s", card, snd_strerror(ret));
167 continue;
168 }
169 if ((ret = snd_ctl_card_info(handle, info)) < 0) {
170 sr_err("Cannot get hardware info (%i): %s",
171 card, snd_strerror(ret));
172 snd_ctl_close(handle);
173 continue;
174 }
175 dev = -1;
176 while (snd_ctl_pcm_next_device(handle, &dev) >= 0 && dev >= 0) {
177 char hwdev[32];
178 snprintf(hwdev, sizeof(hwdev), "%s,%d", hwcard, dev);
179 /*
180 * TODO: We always use subdevice 0, but we have yet to
181 * explore the possibilities opened up by other
182 * subdevices. Most hardware only has subdevice 0.
183 */
184 snd_pcm_info_set_device(pcminfo, dev);
185 snd_pcm_info_set_subdevice(pcminfo, 0);
186 snd_pcm_info_set_stream(pcminfo,
187 SND_PCM_STREAM_CAPTURE);
188 if ((ret = snd_ctl_pcm_info(handle, pcminfo)) < 0) {
189 sr_err("Cannot get device info: %s",
190 snd_strerror(ret));
191 continue;
192 }
193
194 cardname = snd_ctl_card_info_get_name(info);
195 sr_info("card %i: %s [%s], device %i: %s [%s]",
196 card, snd_ctl_card_info_get_id(info), cardname,
197 dev, snd_pcm_info_get_id(pcminfo),
198 snd_pcm_info_get_name(pcminfo));
199
200 alsa_scan_handle_dev(&devices, cardname, hwdev,
201 di, pcminfo);
202 }
203 snd_ctl_close(handle);
204 }
205
206 snd_pcm_info_free(pcminfo);
207 snd_ctl_card_info_free(info);
208
209 return devices;
210}
211
212/*
213 * Helper to be used with g_slist_free_full(); for properly freeing an alsa
214 * dev instance.
215 */
216SR_PRIV void alsa_dev_inst_clear(struct sr_dev_inst *sdi)
217{
218 struct dev_context *devc;
219
220 if (!(devc = sdi->priv))
221 return;
222
223 snd_pcm_hw_params_free(devc->hw_params);
224 sr_dev_inst_free(sdi);
225}
226
9cd9f6b7
AG
227SR_PRIV int alsa_receive_data(int fd, int revents, void *cb_data)
228{
229 struct sr_dev_inst *sdi;
230 struct dev_context *devc;
231 struct sr_datafeed_packet packet;
232 struct sr_datafeed_analog analog;
729850c9 233 int16_t inbuf[4096];
9cd9f6b7 234 int i, x, count, offset, samples_to_get;
729850c9
AG
235 int16_t tmp16;
236 const float s16norm = 1 / (float)(1<<15);
9cd9f6b7
AG
237
238 (void)fd;
239 (void)revents;
240
241 sdi = cb_data;
242 devc = sdi->priv;
243
244 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
245 memset(inbuf, 0, sizeof(inbuf));
246
247 samples_to_get = MIN(4096 / 4, devc->limit_samples);
248
249 sr_spew("Getting %d samples from audio device.", samples_to_get);
250 count = snd_pcm_readi(devc->capture_handle, inbuf, samples_to_get);
251
252 if (count < 0) {
253 sr_err("Failed to read samples: %s.", snd_strerror(count));
254 return FALSE;
255 } else if (count != samples_to_get) {
256 sr_spew("Only got %d/%d samples.", count, samples_to_get);
257 }
258
259 analog.data = g_try_malloc0(count * sizeof(float) * devc->num_probes);
260 if (!analog.data) {
261 sr_err("Failed to malloc sample buffer.");
262 return FALSE;
263 }
264
265 offset = 0;
729850c9
AG
266 /*
267 * It's impossible to know what voltage levels the soundcard handles.
268 * Some handle 0 dBV rms, some 0dBV peak-to-peak, +4dbmW (600 ohm), etc
269 * Each of these corresponds to a different voltage, and there is no
270 * mechanism to determine this voltage. The best solution is to send all
271 * audio data as a normalized float, and let the frontend or user worry
272 * about the calibration.
273 */
274 for (i = 0; i < count; i += devc->num_probes) {
9cd9f6b7 275 for (x = 0; x < devc->num_probes; x++) {
729850c9
AG
276 tmp16 = inbuf[i+x];
277 analog.data[offset++] = tmp16 * s16norm;
9cd9f6b7
AG
278 }
279 }
280
281 /* Send a sample packet with the analog values. */
282 analog.num_samples = count;
283 analog.mq = SR_MQ_VOLTAGE; /* FIXME */
284 analog.unit = SR_UNIT_VOLT; /* FIXME */
285 packet.type = SR_DF_ANALOG;
286 packet.payload = &analog;
287 sr_session_send(devc->cb_data, &packet);
288
289 g_free(analog.data);
290
291 devc->num_samples += count;
292
293 /* Stop acquisition if we acquired enough samples. */
35e199da
UH
294 if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
295 sr_info("Requested number of samples reached.");
296 sdi->driver->dev_acquisition_stop(sdi, cb_data);
9cd9f6b7
AG
297 }
298
299 return TRUE;
300}
6944b2d0 301