]> sigrok.org Git - libsigrok.git/blob - hardware/alsa/api.c
alsa: Fix sample acquisition and send normalized values
[libsigrok.git] / hardware / alsa / api.c
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
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30
31 #define DEFAULT_PROBES          2
32 #define SAMPLE_WIDTH            16
33 #define DEFAULT_SAMPLERATE      44100
34
35 static const int hwcaps[] = {
36         SR_HWCAP_SAMPLERATE,
37         SR_HWCAP_LIMIT_SAMPLES,
38         SR_HWCAP_CONTINUOUS,
39         0,
40 };
41
42 static const char *probe_names[] = {
43         "Channel 0", "Channel 1",
44         NULL,
45 };
46
47 SR_PRIV struct sr_dev_driver alsa_driver_info;
48 static struct sr_dev_driver *di = &alsa_driver_info;
49
50 static int clear_instances(void)
51 {
52         struct drv_context *drvc;
53
54         if (!(drvc = di->priv))
55                 return SR_OK;
56
57         g_slist_free_full(drvc->instances, (GDestroyNotify)alsa_dev_inst_clear);
58         drvc->instances = NULL;
59
60         return SR_OK;
61 }
62
63 static int hw_init(struct sr_context *sr_ctx)
64 {
65         struct drv_context *drvc;
66
67         if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
68                 sr_err("Driver context malloc failed.");
69                 return SR_ERR_MALLOC;
70         }
71
72         drvc->sr_ctx = sr_ctx;
73         di->priv = drvc;
74
75         return SR_OK;
76 }
77
78 static GSList *hw_scan(GSList *options)
79 {
80         return alsa_scan(options, di);
81 }
82
83 static GSList *hw_dev_list(void)
84 {
85         struct drv_context *drvc;
86
87         drvc = di->priv;
88
89         return drvc->instances;
90 }
91
92 static int hw_dev_open(struct sr_dev_inst *sdi)
93 {
94         struct dev_context *devc;
95         int ret;
96
97         devc = sdi->priv;
98
99         if (!(devc->hwdev)) {
100                 sr_err("devc->hwdev was NULL.");
101                 return SR_ERR_BUG;
102         }
103
104         sr_dbg("Opening audio device '%s' for stream capture.", devc->hwdev);
105         ret = snd_pcm_open(&devc->capture_handle, devc->hwdev,
106                            SND_PCM_STREAM_CAPTURE, 0);
107         if (ret < 0) {
108                 sr_err("Can't open audio device: %s.", snd_strerror(ret));
109                 return SR_ERR;
110         }
111
112         sr_dbg("Initializing hardware parameter structure.");
113         ret = snd_pcm_hw_params_any(devc->capture_handle, devc->hw_params);
114         if (ret < 0) {
115                 sr_err("Can't initialize hardware parameter structure: %s.",
116                        snd_strerror(ret));
117                 return SR_ERR;
118         }
119
120         return SR_OK;
121 }
122
123 static int hw_dev_close(struct sr_dev_inst *sdi)
124 {
125         int ret;
126         struct dev_context *devc;
127
128         devc = sdi->priv;
129
130         sr_dbg("Closing device.");
131
132         if (devc->capture_handle) {
133                 sr_dbg("Closing PCM device.");
134                 if ((ret = snd_pcm_close(devc->capture_handle)) < 0) {
135                         sr_err("Failed to close device: %s.",
136                                snd_strerror(ret));
137                         devc->capture_handle = NULL;
138                 }
139         } else {
140                 sr_dbg("No capture handle, no need to close audio device.");
141         }
142
143         return SR_OK;
144 }
145
146 static int hw_cleanup(void)
147 {
148         clear_instances();
149
150         return SR_OK;
151 }
152
153 static int hw_info_get(int info_id, const void **data,
154                        const struct sr_dev_inst *sdi)
155 {
156         struct dev_context *devc;
157
158         if (info_id != SR_DI_HWCAPS) /* For SR_DI_HWCAPS sdi will be NULL. */
159                 devc = sdi->priv;
160
161         switch (info_id) {
162         case SR_DI_HWCAPS:
163                 *data = hwcaps;
164                 break;
165         case SR_DI_NUM_PROBES:
166                 *data = &devc->num_probes;
167                 break;
168         case SR_DI_PROBE_NAMES:
169                 *data = probe_names;
170                 break;
171         case SR_DI_CUR_SAMPLERATE:
172                 *data = &devc->cur_samplerate;
173                 break;
174         default:
175                 sr_err("Invalid info_id: %d.", info_id);
176                 return SR_ERR_ARG;
177         }
178
179         return SR_OK;
180 }
181
182 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
183                              const void *value)
184 {
185         struct dev_context *devc;
186
187         devc = sdi->priv;
188
189         switch (hwcap) {
190         case SR_HWCAP_SAMPLERATE:
191                 devc->cur_samplerate = *(const uint64_t *)value;
192                 break;
193         case SR_HWCAP_LIMIT_SAMPLES:
194                 devc->limit_samples = *(const uint64_t *)value;
195                 break;
196         default:
197                 sr_err("Unknown capability: %d.", hwcap);
198                 return SR_ERR;
199         }
200
201         return SR_OK;
202 }
203
204 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
205                                     void *cb_data)
206 {
207         struct sr_datafeed_packet packet;
208         struct sr_datafeed_header header;
209         struct sr_datafeed_meta_analog meta;
210         struct dev_context *devc;
211         int count, ret;
212         char *endianess;
213
214         devc = sdi->priv;
215         devc->cb_data = cb_data;
216         devc->num_samples = 0;
217
218         sr_dbg("Setting audio access type to RW/interleaved.");
219         ret = snd_pcm_hw_params_set_access(devc->capture_handle,
220                         devc->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
221         if (ret < 0) {
222                 sr_err("Can't set audio access type: %s.", snd_strerror(ret));
223                 return SR_ERR;
224         }
225
226         /* FIXME: Hardcoded for 16bits. */
227         if (SND_PCM_FORMAT_S16 == SND_PCM_FORMAT_S16_LE)
228                 endianess = "lilltle endian";
229         else
230                 endianess = "big endian";
231         sr_dbg("Setting audio sample format to signed 16bit (%s).", endianess);
232         ret = snd_pcm_hw_params_set_format(devc->capture_handle,
233                                            devc->hw_params,
234                                            SND_PCM_FORMAT_S16);
235         if (ret < 0) {
236                 sr_err("Can't set audio sample format: %s.", snd_strerror(ret));
237                 return SR_ERR;
238         }
239
240         sr_dbg("Setting audio samplerate to %" PRIu64 "Hz.",
241                devc->cur_samplerate);
242         ret = snd_pcm_hw_params_set_rate(devc->capture_handle, devc->hw_params,
243                                          (unsigned int)devc->cur_samplerate, 0);
244         if (ret < 0) {
245                 sr_err("Can't set audio sample rate: %s.", snd_strerror(ret));
246                 return SR_ERR;
247         }
248
249         sr_dbg("Setting audio channel count to %d.", devc->num_probes);
250         ret = snd_pcm_hw_params_set_channels(devc->capture_handle,
251                                              devc->hw_params, devc->num_probes);
252         if (ret < 0) {
253                 sr_err("Can't set channel count: %s.", snd_strerror(ret));
254                 return SR_ERR;
255         }
256
257         sr_dbg("Setting audio parameters.");
258         ret = snd_pcm_hw_params(devc->capture_handle, devc->hw_params);
259         if (ret < 0) {
260                 sr_err("Can't set parameters: %s.", snd_strerror(ret));
261                 return SR_ERR;
262         }
263
264         sr_dbg("Preparing audio interface for use.");
265         ret = snd_pcm_prepare(devc->capture_handle);
266         if (ret < 0) {
267                 sr_err("Can't prepare audio interface for use: %s.",
268                        snd_strerror(ret));
269                 return SR_ERR;
270         }
271
272         count = snd_pcm_poll_descriptors_count(devc->capture_handle);
273         if (count < 1) {
274                 sr_err("Unable to obtain poll descriptors count.");
275                 return SR_ERR;
276         }
277         sr_spew("Obtained poll descriptor count: %d.", count);
278
279         if (!(devc->ufds = g_try_malloc(count * sizeof(struct pollfd)))) {
280                 sr_err("Failed to malloc ufds.");
281                 return SR_ERR_MALLOC;
282         }
283
284         sr_err("Getting %d poll descriptors.", count);
285         ret = snd_pcm_poll_descriptors(devc->capture_handle, devc->ufds, count);
286         if (ret < 0) {
287                 sr_err("Unable to obtain poll descriptors: %s.",
288                        snd_strerror(ret));
289                 g_free(devc->ufds);
290                 return SR_ERR;
291         }
292
293         /* Send header packet to the session bus. */
294         sr_dbg("Sending SR_DF_HEADER packet.");
295         packet.type = SR_DF_HEADER;
296         packet.payload = (uint8_t *)&header;
297         header.feed_version = 1;
298         gettimeofday(&header.starttime, NULL);
299         sr_session_send(devc->cb_data, &packet);
300
301         /* Send metadata about the SR_DF_ANALOG packets to come. */
302         sr_dbg("Sending SR_DF_META_ANALOG packet.");
303         packet.type = SR_DF_META_ANALOG;
304         packet.payload = &meta;
305         meta.num_probes = devc->num_probes;
306         sr_session_send(devc->cb_data, &packet);
307
308         /* Poll every 10ms, or whenever some data comes in. */
309         sr_source_add(devc->ufds[0].fd, devc->ufds[0].events, 10,
310                       alsa_receive_data, (void *)sdi);
311
312         // g_free(devc->ufds); /* FIXME */
313
314         return SR_OK;
315 }
316
317 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
318 {
319         struct sr_datafeed_packet packet;
320         struct dev_context *devc;
321
322         devc = sdi->priv;
323         devc->cb_data = cb_data;
324
325         sr_source_remove(devc->ufds[0].fd);
326
327         /* Send end packet to the session bus. */
328         sr_dbg("Sending SR_DF_END packet.");
329         packet.type = SR_DF_END;
330         sr_session_send(cb_data, &packet);
331
332         return SR_OK;
333 }
334
335 SR_PRIV struct sr_dev_driver alsa_driver_info = {
336         .name = "alsa",
337         .longname = "ALSA driver",
338         .api_version = 1,
339         .init = hw_init,
340         .cleanup = hw_cleanup,
341         .scan = hw_scan,
342         .dev_list = hw_dev_list,
343         .dev_clear = clear_instances,
344         .dev_open = hw_dev_open,
345         .dev_close = hw_dev_close,
346         .info_get = hw_info_get,
347         .dev_config_set = hw_dev_config_set,
348         .dev_acquisition_start = hw_dev_acquisition_start,
349         .dev_acquisition_stop = hw_dev_acquisition_stop,
350         .priv = NULL,
351 };