2 * This file is part of the sigrok project.
4 * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com>
5 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libsigrok.h"
24 #include "libsigrok-internal.h"
30 #define DEFAULT_PROBES 2
31 #define SAMPLE_WIDTH 16
32 #define DEFAULT_SAMPLERATE 44100
33 // #define AUDIO_DEV "plughw:0,0"
34 #define AUDIO_DEV "default"
36 static const int hwcaps[] = {
38 SR_HWCAP_LIMIT_SAMPLES,
43 static const char *probe_names[] = {
49 SR_PRIV struct sr_dev_driver alsa_driver_info;
50 static struct sr_dev_driver *di = &alsa_driver_info;
52 static int clear_instances(void)
59 static int hw_init(struct sr_context *sr_ctx)
61 struct drv_context *drvc;
63 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
64 sr_err("Driver context malloc failed.");
68 drvc->sr_ctx = sr_ctx;
74 static GSList *hw_scan(GSList *options)
76 struct drv_context *drvc;
77 struct dev_context *devc;
78 struct sr_dev_inst *sdi;
79 struct sr_probe *probe;
86 drvc->instances = NULL;
90 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
91 sr_err("Device context malloc failed.");
95 if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, "alsa", NULL, NULL))) {
96 sr_err("Failed to create device instance.");
100 /* Set the samplerate to a default value for now. */
101 devc->cur_samplerate = DEFAULT_SAMPLERATE;
102 devc->num_probes = DEFAULT_PROBES;
107 for (i = 0; probe_names[i]; i++) {
108 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
110 sr_err("Failed to create probe.");
113 sdi->probes = g_slist_append(sdi->probes, probe);
116 drvc->instances = g_slist_append(drvc->instances, sdi);
117 devices = g_slist_append(devices, sdi);
122 static GSList *hw_dev_list(void)
124 struct drv_context *drvc;
128 return drvc->instances;
131 static int hw_dev_open(struct sr_dev_inst *sdi)
133 struct dev_context *devc;
138 sr_dbg("Opening audio device '%s' for stream capture.", AUDIO_DEV);
139 ret = snd_pcm_open(&devc->capture_handle, AUDIO_DEV,
140 SND_PCM_STREAM_CAPTURE, 0);
142 sr_err("Can't open audio device: %s.", snd_strerror(ret));
146 sr_dbg("Allocating hardware parameter structure.");
147 ret = snd_pcm_hw_params_malloc(&devc->hw_params);
149 sr_err("Can't allocate hardware parameter structure: %s.",
151 return SR_ERR_MALLOC;
154 sr_dbg("Initializing hardware parameter structure.");
155 ret = snd_pcm_hw_params_any(devc->capture_handle, devc->hw_params);
157 sr_err("Can't initialize hardware parameter structure: %s.",
165 static int hw_dev_close(struct sr_dev_inst *sdi)
168 struct dev_context *devc;
172 sr_dbg("Closing device.");
174 if (devc->hw_params) {
175 sr_dbg("Freeing hardware parameters.");
176 snd_pcm_hw_params_free(devc->hw_params);
178 sr_dbg("No hardware parameters, no need to free.");
181 if (devc->capture_handle) {
182 sr_dbg("Closing PCM device.");
183 if ((ret = snd_pcm_close(devc->capture_handle)) < 0) {
184 sr_err("Failed to close device: %s.",
188 sr_dbg("No capture handle, no need to close audio device.");
194 static int hw_cleanup(void)
201 static int hw_info_get(int info_id, const void **data,
202 const struct sr_dev_inst *sdi)
204 struct dev_context *devc;
206 if (info_id != SR_DI_HWCAPS) /* For SR_DI_HWCAPS sdi will be NULL. */
213 case SR_DI_NUM_PROBES:
214 *data = GINT_TO_POINTER(DEFAULT_PROBES);
216 case SR_DI_PROBE_NAMES:
219 case SR_DI_CUR_SAMPLERATE:
220 *data = &devc->cur_samplerate;
223 sr_err("Invalid info_id: %d.", info_id);
230 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
233 struct dev_context *devc;
238 case SR_HWCAP_SAMPLERATE:
239 devc->cur_samplerate = *(const uint64_t *)value;
241 case SR_HWCAP_LIMIT_SAMPLES:
242 devc->limit_samples = *(const uint64_t *)value;
245 sr_err("Unknown capability: %d.", hwcap);
252 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
255 struct sr_datafeed_packet packet;
256 struct sr_datafeed_header header;
257 struct sr_datafeed_meta_analog meta;
258 struct dev_context *devc;
262 devc->cb_data = cb_data;
264 sr_dbg("Setting audio access type to RW/interleaved.");
265 ret = snd_pcm_hw_params_set_access(devc->capture_handle,
266 devc->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
268 sr_err("Can't set audio access type: %s.", snd_strerror(ret));
272 /* FIXME: Hardcoded for 16bits. */
273 sr_dbg("Setting audio sample format to signed 16bit (little endian).");
274 ret = snd_pcm_hw_params_set_format(devc->capture_handle,
275 devc->hw_params, SND_PCM_FORMAT_S16_LE);
277 sr_err("Can't set audio sample format: %s.", snd_strerror(ret));
281 sr_dbg("Setting audio samplerate to %" PRIu64 "Hz.",
282 devc->cur_samplerate);
283 ret = snd_pcm_hw_params_set_rate_near(devc->capture_handle,
284 devc->hw_params, (unsigned int *)&devc->cur_samplerate, 0);
286 sr_err("Can't set audio sample rate: %s.", snd_strerror(ret));
290 sr_dbg("Setting audio channel count to %d.", devc->num_probes);
291 ret = snd_pcm_hw_params_set_channels(devc->capture_handle,
292 devc->hw_params, devc->num_probes);
294 sr_err("Can't set channel count: %s.", snd_strerror(ret));
298 sr_dbg("Setting audio parameters.");
299 ret = snd_pcm_hw_params(devc->capture_handle, devc->hw_params);
301 sr_err("Can't set parameters: %s.", snd_strerror(ret));
305 sr_dbg("Preparing audio interface for use.");
306 ret = snd_pcm_prepare(devc->capture_handle);
308 sr_err("Can't prepare audio interface for use: %s.",
313 count = snd_pcm_poll_descriptors_count(devc->capture_handle);
315 sr_err("Unable to obtain poll descriptors count.");
318 sr_spew("Obtained poll descriptor count: %d.", count);
320 if (!(devc->ufds = g_try_malloc(count * sizeof(struct pollfd)))) {
321 sr_err("Failed to malloc ufds.");
322 return SR_ERR_MALLOC;
325 sr_err("Getting %d poll descriptors.", count);
326 ret = snd_pcm_poll_descriptors(devc->capture_handle, devc->ufds, count);
328 sr_err("Unable to obtain poll descriptors: %s.",
334 /* Send header packet to the session bus. */
335 sr_dbg("Sending SR_DF_HEADER packet.");
336 packet.type = SR_DF_HEADER;
337 packet.payload = (uint8_t *)&header;
338 header.feed_version = 1;
339 gettimeofday(&header.starttime, NULL);
340 sr_session_send(devc->cb_data, &packet);
342 /* Send metadata about the SR_DF_ANALOG packets to come. */
343 sr_dbg("Sending SR_DF_META_ANALOG packet.");
344 packet.type = SR_DF_META_ANALOG;
345 packet.payload = &meta;
346 meta.num_probes = devc->num_probes;
347 sr_session_send(devc->cb_data, &packet);
349 /* Poll every 10ms, or whenever some data comes in. */
350 sr_source_add(devc->ufds[0].fd, devc->ufds[0].events, 10,
351 alsa_receive_data, (void *)sdi);
353 // g_free(devc->ufds); /* FIXME */
358 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
360 struct sr_datafeed_packet packet;
361 struct dev_context *devc;
364 devc->cb_data = cb_data;
366 sr_source_remove(devc->ufds[0].fd);
368 /* Send end packet to the session bus. */
369 sr_dbg("Sending SR_DF_END packet.");
370 packet.type = SR_DF_END;
371 sr_session_send(cb_data, &packet);
376 SR_PRIV struct sr_dev_driver alsa_driver_info = {
378 .longname = "ALSA driver",
381 .cleanup = hw_cleanup,
383 .dev_list = hw_dev_list,
384 .dev_clear = clear_instances,
385 .dev_open = hw_dev_open,
386 .dev_close = hw_dev_close,
387 .info_get = hw_info_get,
388 .dev_config_set = hw_dev_config_set,
389 .dev_acquisition_start = hw_dev_acquisition_start,
390 .dev_acquisition_stop = hw_dev_acquisition_stop,