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[] = {
44 "Channel 0", "Channel 1",
48 SR_PRIV struct sr_dev_driver alsa_driver_info;
49 static struct sr_dev_driver *di = &alsa_driver_info;
51 static int clear_instances(void)
58 static int hw_init(struct sr_context *sr_ctx)
60 struct drv_context *drvc;
62 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
63 sr_err("Driver context malloc failed.");
67 drvc->sr_ctx = sr_ctx;
73 static GSList *hw_scan(GSList *options)
75 struct drv_context *drvc;
76 struct dev_context *devc;
77 struct sr_dev_inst *sdi;
78 struct sr_probe *probe;
85 drvc->instances = NULL;
89 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
90 sr_err("Device context malloc failed.");
94 if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, "alsa", NULL, NULL))) {
95 sr_err("Failed to create device instance.");
99 /* Set the samplerate to a default value for now. */
100 devc->cur_samplerate = DEFAULT_SAMPLERATE;
101 devc->num_probes = DEFAULT_PROBES;
106 for (i = 0; probe_names[i]; i++) {
107 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
109 sr_err("Failed to create probe.");
112 sdi->probes = g_slist_append(sdi->probes, probe);
115 drvc->instances = g_slist_append(drvc->instances, sdi);
116 devices = g_slist_append(devices, sdi);
121 static GSList *hw_dev_list(void)
123 struct drv_context *drvc;
127 return drvc->instances;
130 static int hw_dev_open(struct sr_dev_inst *sdi)
132 struct dev_context *devc;
137 sr_dbg("Opening audio device '%s' for stream capture.", AUDIO_DEV);
138 ret = snd_pcm_open(&devc->capture_handle, AUDIO_DEV,
139 SND_PCM_STREAM_CAPTURE, 0);
141 sr_err("Can't open audio device: %s.", snd_strerror(ret));
145 sr_dbg("Allocating hardware parameter structure.");
146 ret = snd_pcm_hw_params_malloc(&devc->hw_params);
148 sr_err("Can't allocate hardware parameter structure: %s.",
150 return SR_ERR_MALLOC;
153 sr_dbg("Initializing hardware parameter structure.");
154 ret = snd_pcm_hw_params_any(devc->capture_handle, devc->hw_params);
156 sr_err("Can't initialize hardware parameter structure: %s.",
164 static int hw_dev_close(struct sr_dev_inst *sdi)
167 struct dev_context *devc;
171 sr_dbg("Closing device.");
173 if (devc->hw_params) {
174 sr_dbg("Freeing hardware parameters.");
175 snd_pcm_hw_params_free(devc->hw_params);
177 sr_dbg("No hardware parameters, no need to free.");
180 if (devc->capture_handle) {
181 sr_dbg("Closing PCM device.");
182 if ((ret = snd_pcm_close(devc->capture_handle)) < 0) {
183 sr_err("Failed to close device: %s.",
187 sr_dbg("No capture handle, no need to close audio device.");
193 static int hw_cleanup(void)
200 static int hw_info_get(int info_id, const void **data,
201 const struct sr_dev_inst *sdi)
203 struct dev_context *devc;
205 if (info_id != SR_DI_HWCAPS) /* For SR_DI_HWCAPS sdi will be NULL. */
212 case SR_DI_NUM_PROBES:
213 *data = GINT_TO_POINTER(DEFAULT_PROBES);
215 case SR_DI_PROBE_NAMES:
218 case SR_DI_CUR_SAMPLERATE:
219 *data = &devc->cur_samplerate;
222 sr_err("Invalid info_id: %d.", info_id);
229 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
232 struct dev_context *devc;
237 case SR_HWCAP_SAMPLERATE:
238 devc->cur_samplerate = *(const uint64_t *)value;
240 case SR_HWCAP_LIMIT_SAMPLES:
241 devc->limit_samples = *(const uint64_t *)value;
244 sr_err("Unknown capability: %d.", hwcap);
251 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
254 struct sr_datafeed_packet packet;
255 struct sr_datafeed_header header;
256 struct sr_datafeed_meta_analog meta;
257 struct dev_context *devc;
261 devc->cb_data = cb_data;
263 sr_dbg("Setting audio access type to RW/interleaved.");
264 ret = snd_pcm_hw_params_set_access(devc->capture_handle,
265 devc->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
267 sr_err("Can't set audio access type: %s.", snd_strerror(ret));
271 /* FIXME: Hardcoded for 16bits. */
272 sr_dbg("Setting audio sample format to signed 16bit (little endian).");
273 ret = snd_pcm_hw_params_set_format(devc->capture_handle,
274 devc->hw_params, SND_PCM_FORMAT_S16_LE);
276 sr_err("Can't set audio sample format: %s.", snd_strerror(ret));
280 sr_dbg("Setting audio samplerate to %" PRIu64 "Hz.",
281 devc->cur_samplerate);
282 ret = snd_pcm_hw_params_set_rate(devc->capture_handle, devc->hw_params,
283 (unsigned int)devc->cur_samplerate, 0);
285 sr_err("Can't set audio sample rate: %s.", snd_strerror(ret));
289 sr_dbg("Setting audio channel count to %d.", devc->num_probes);
290 ret = snd_pcm_hw_params_set_channels(devc->capture_handle,
291 devc->hw_params, devc->num_probes);
293 sr_err("Can't set channel count: %s.", snd_strerror(ret));
297 sr_dbg("Setting audio parameters.");
298 ret = snd_pcm_hw_params(devc->capture_handle, devc->hw_params);
300 sr_err("Can't set parameters: %s.", snd_strerror(ret));
304 sr_dbg("Preparing audio interface for use.");
305 ret = snd_pcm_prepare(devc->capture_handle);
307 sr_err("Can't prepare audio interface for use: %s.",
312 count = snd_pcm_poll_descriptors_count(devc->capture_handle);
314 sr_err("Unable to obtain poll descriptors count.");
317 sr_spew("Obtained poll descriptor count: %d.", count);
319 if (!(devc->ufds = g_try_malloc(count * sizeof(struct pollfd)))) {
320 sr_err("Failed to malloc ufds.");
321 return SR_ERR_MALLOC;
324 sr_err("Getting %d poll descriptors.", count);
325 ret = snd_pcm_poll_descriptors(devc->capture_handle, devc->ufds, count);
327 sr_err("Unable to obtain poll descriptors: %s.",
333 /* Send header packet to the session bus. */
334 sr_dbg("Sending SR_DF_HEADER packet.");
335 packet.type = SR_DF_HEADER;
336 packet.payload = (uint8_t *)&header;
337 header.feed_version = 1;
338 gettimeofday(&header.starttime, NULL);
339 sr_session_send(devc->cb_data, &packet);
341 /* Send metadata about the SR_DF_ANALOG packets to come. */
342 sr_dbg("Sending SR_DF_META_ANALOG packet.");
343 packet.type = SR_DF_META_ANALOG;
344 packet.payload = &meta;
345 meta.num_probes = devc->num_probes;
346 sr_session_send(devc->cb_data, &packet);
348 /* Poll every 10ms, or whenever some data comes in. */
349 sr_source_add(devc->ufds[0].fd, devc->ufds[0].events, 10,
350 alsa_receive_data, (void *)sdi);
352 // g_free(devc->ufds); /* FIXME */
357 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
359 struct sr_datafeed_packet packet;
360 struct dev_context *devc;
363 devc->cb_data = cb_data;
365 sr_source_remove(devc->ufds[0].fd);
367 /* Send end packet to the session bus. */
368 sr_dbg("Sending SR_DF_END packet.");
369 packet.type = SR_DF_END;
370 sr_session_send(cb_data, &packet);
375 SR_PRIV struct sr_dev_driver alsa_driver_info = {
377 .longname = "ALSA driver",
380 .cleanup = hw_cleanup,
382 .dev_list = hw_dev_list,
383 .dev_clear = clear_instances,
384 .dev_open = hw_dev_open,
385 .dev_close = hw_dev_close,
386 .info_get = hw_info_get,
387 .dev_config_set = hw_dev_config_set,
388 .dev_acquisition_start = hw_dev_acquisition_start,
389 .dev_acquisition_stop = hw_dev_acquisition_stop,