]> sigrok.org Git - libsigrok.git/blobdiff - hardware/alsa/protocol.c
Replace 'probe' with 'channel' in most places.
[libsigrok.git] / hardware / alsa / protocol.c
index 1c1d8849dd2a626d69bc752c106aedb60a95bf8a..d06a10efad7926e243a9eded08dae805e49035fd 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
-#include "protocol.h"
 #include "libsigrok.h"
 #include "libsigrok-internal.h"
+#include "protocol.h"
+
+/*
+ * There is no way to get a list of supported samplerates from ALSA. We could
+ * use the 'plughw' interface of ALSA, in which case any format and/or
+ * samplerate conversion would be performed by ALSA. However, we are interested
+ * in the hardware capabilities, and have the infrastructure in sigrok to do so.
+ * We therefore use the 'hw' interface. The downside is that the code gets a
+ * little bulkier, as we have to keep track of the hardware capabilities, and
+ * only use those that the hardware supports. Case in point, ALSA will not give
+ * us a list of capabilities; we have to test for each one individually. Hence,
+ * we keep lists of the capabilities we are interested in.
+ */
+static const unsigned int rates[] = {
+       5512,
+       8000,
+       11025,
+       12000,
+       16000,
+       22050,
+       24000,
+       32000,
+       44100,
+       48000,
+       64000,
+       88200,
+       96000,
+       176400,
+       192000,
+       384000,
+       768000, /* Yes, there are sound cards that go this high. */
+};
 
 static void alsa_scan_handle_dev(GSList **devices,
                                 const char *cardname, const char *alsaname,
@@ -32,19 +63,23 @@ static void alsa_scan_handle_dev(GSList **devices,
        struct drv_context *drvc = NULL;
        struct sr_dev_inst *sdi = NULL;
        struct dev_context *devc = NULL;
-       struct sr_probe *probe;
+       struct sr_channel *ch;
        int ret;
-       unsigned int i, channels;
+       unsigned int i, offset, channels, minrate, maxrate, rate;
+       uint64_t hwrates[ARRAY_SIZE(rates)];
+       uint64_t *devrates = NULL;
        snd_pcm_t *temp_handle = NULL;
        snd_pcm_hw_params_t *hw_params = NULL;
+       char p_name[32];
 
        drvc = di->priv;
 
        /*
-        * Get the number of input channels. Those are our sigrok probes.
-        * Getting this information needs a detour. We need to open the device,
-        * then query it for the number of channels. A side-effect of is that we
-        * create a snd_pcm_hw_params_t object. We take advantage of the
+        * Get hardware parameters:
+        * The number of channels, for example, are our sigrok channels. Getting
+        * this information needs a detour. We need to open the device, then
+        * query it and/or test different parameters. A side-effect of is that
+        * we create a snd_pcm_hw_params_t object. We take advantage of the
         * situation, and pass this object in our dev_context->hw_params,
         * eliminating the need to free() it and malloc() it later.
         */
@@ -70,36 +105,63 @@ static void alsa_scan_handle_dev(GSList **devices,
 
        snd_pcm_hw_params_get_channels_max(hw_params, &channels);
 
-       snd_pcm_close(temp_handle);
+       /*
+        * We need to test if each samplerate between min and max is supported.
+        * Unfortunately, ALSA won't just throw a list at us.
+        */
+       snd_pcm_hw_params_get_rate_min(hw_params, &minrate, 0);
+       snd_pcm_hw_params_get_rate_max(hw_params, &maxrate, 0);
+       for (i = 0, offset = 0; i < ARRAY_SIZE(rates); i++) {
+               rate = rates[i];
+               if (rate < minrate)
+                       continue;
+               if (rate > maxrate)
+                       break;
+               ret = snd_pcm_hw_params_test_rate(temp_handle, hw_params,
+                                                 rate, 0);
+               if (ret >= 0)
+                       hwrates[offset++] = rate;
+       }
+       hwrates[offset++] = 0;
+
+       if ((ret = snd_pcm_close(temp_handle)) < 0)
+               sr_err("Failed to close device: %s.", snd_strerror(ret));
        temp_handle = NULL;
 
        /*
-        * Now we are done querying the number of channels
-        * If we made it here, then it's time to create our sigrok device.
+        * Now we are done querying the hardware parameters.
+        * If we made it here, we know everything we want to know, and it's
+        * time to create our sigrok device.
         */
        sr_info("Device %s has %d channels.", alsaname, channels);
        if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "ALSA:",
                cardname, snd_pcm_info_get_name(pcminfo)))) {
-               sr_err("Device instance malloc failed.");
+               sr_err("Failed to create device instance.");
                goto scan_error_cleanup;
        }
        if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
                sr_err("Device context malloc failed.");
                goto scan_error_cleanup;
        }
+       if (!(devrates = g_try_malloc(offset * sizeof(uint64_t)))) {
+               sr_err("Samplerate list malloc failed.");
+               goto scan_error_cleanup;
+       }
 
        devc->hwdev = g_strdup(alsaname);
-       devc->num_probes = channels;
+       devc->num_channels = channels;
        devc->hw_params = hw_params;
+       memcpy(devrates, hwrates, offset * sizeof(uint64_t));
+       devc->samplerates = devrates;
+
        sdi->priv = devc;
        sdi->driver = di;
 
-       for (i = 0; i < devc->num_probes; i++) {
-               char p_name[32];
+       for (i = 0; i < devc->num_channels; i++) {
                snprintf(p_name, sizeof(p_name), "Ch_%d", i);
-               if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, p_name)))
+               if (!(ch = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, p_name)))
                        goto scan_error_cleanup;
-               sdi->probes = g_slist_append(sdi->probes, probe);
+               sdi->channels = g_slist_append(sdi->channels, ch);
        }
 
        drvc->instances = g_slist_append(drvc->instances, sdi);
@@ -109,22 +171,27 @@ static void alsa_scan_handle_dev(GSList **devices,
 scan_error_cleanup:
        if (devc) {
                if (devc->hwdev)
-                       g_free((void*)devc->hwdev);
+                       g_free(devc->hwdev);
                g_free(devc);
        }
+       if (devrates)
+               g_free(devrates);
        if (sdi)
                sr_dev_inst_free(sdi);
        if (hw_params)
                snd_pcm_hw_params_free(hw_params);
        if (temp_handle)
-               snd_pcm_close(temp_handle);
-       return;
+               if ((ret = snd_pcm_close(temp_handle)) < 0) {
+                       sr_err("Failed to close device: %s.",
+                              snd_strerror(ret));
+               }
 }
 
 /**
- * \brief Scan all alsa devices, and translate them to sigrok devices
+ * Scan all alsa devices, and translate them to sigrok devices.
+ *
+ * Each alsa device (not alsa card) gets its own sigrok device.
  *
- * Each alsa device (not alsa card) gets its own sigrok device
  * For example,
  *     hw:1,0 == sigrok device 0
  *     hw:1,1 == sigrok device 1
@@ -132,11 +199,11 @@ scan_error_cleanup:
  *     hw:2,1 == sigrok device 3
  *     hw:2,2 == sigrok device 4
  *     [...]
- * \n
+ *
  * We don't currently look at alsa subdevices. We only use subdevice 0.
- * Every input device will have a its own channels (Left, Right, etc). Each of
- * those channels gets mapped to a different sigrok probe. A device with 4
- * channels will have 4 probes from sigrok's perspective.
+ * Every input device will have its own channels (left, right, etc). Each of
+ * those channels gets mapped to a different sigrok channel. A device with 4
+ * channels will have 4 channels from sigrok's perspective.
  */
 SR_PRIV GSList *alsa_scan(GSList *options, struct sr_dev_driver *di)
 {
@@ -145,36 +212,39 @@ SR_PRIV GSList *alsa_scan(GSList *options, struct sr_dev_driver *di)
        int card, ret, dev;
        snd_ctl_card_info_t *info;
        snd_pcm_info_t *pcminfo;
-       const char* cardname;
+       const char *cardname;
+       char hwcard[32], hwdev[32];
+
        /* TODO */
        (void)options;
 
-       if (snd_ctl_card_info_malloc(&info) < 0) {
-               sr_err("Cannot malloc card info.");
+       if ((ret = snd_ctl_card_info_malloc(&info)) < 0) {
+               sr_dbg("Failed to malloc card info: %s.", snd_strerror(ret));
                return NULL;
        }
-       if (snd_pcm_info_malloc(&pcminfo) < 0) {
-               sr_err("Cannot malloc pcm info.");
+       if ((ret = snd_pcm_info_malloc(&pcminfo) < 0)) {
+               sr_dbg("Cannot malloc pcm info: %s.", snd_strerror(ret));
                return NULL;
        }
 
        card = -1;
        while (snd_card_next(&card) >= 0 && card >= 0) {
-               char hwcard[32];
                snprintf(hwcard, sizeof(hwcard), "hw:%d", card);
                if ((ret = snd_ctl_open(&handle, hwcard, 0)) < 0) {
-                       sr_err("Cannot open (%i): %s", card, snd_strerror(ret));
+                       sr_dbg("Cannot open (%d): %s.", card, snd_strerror(ret));
                        continue;
                }
                if ((ret = snd_ctl_card_info(handle, info)) < 0) {
-                       sr_err("Cannot get hardware info (%i): %s",
+                       sr_dbg("Cannot get hardware info (%d): %s.",
                               card, snd_strerror(ret));
-                       snd_ctl_close(handle);
+                       if ((ret = snd_ctl_close(handle)) < 0) {
+                               sr_dbg("Cannot close device (%d): %s.",
+                                      card, snd_strerror(ret));
+                       }
                        continue;
                }
                dev = -1;
                while (snd_ctl_pcm_next_device(handle, &dev) >= 0 && dev >= 0) {
-                       char hwdev[32];
                        snprintf(hwdev, sizeof(hwdev), "%s,%d", hwcard, dev);
                        /*
                         * TODO: We always use subdevice 0, but we have yet to
@@ -186,13 +256,13 @@ SR_PRIV GSList *alsa_scan(GSList *options, struct sr_dev_driver *di)
                        snd_pcm_info_set_stream(pcminfo,
                                                SND_PCM_STREAM_CAPTURE);
                        if ((ret = snd_ctl_pcm_info(handle, pcminfo)) < 0) {
-                               sr_err("Cannot get device info: %s",
-                                      snd_strerror(ret));
+                               sr_dbg("Cannot get device info (%s): %s.",
+                                      hwdev, snd_strerror(ret));
                                continue;
                        }
 
                        cardname = snd_ctl_card_info_get_name(info);
-                       sr_info("card %i: %s [%s], device %i: %s [%s]",
+                       sr_info("card %d: %s [%s], device %d: %s [%s]",
                               card, snd_ctl_card_info_get_id(info), cardname,
                               dev, snd_pcm_info_get_id(pcminfo),
                               snd_pcm_info_get_name(pcminfo));
@@ -200,7 +270,10 @@ SR_PRIV GSList *alsa_scan(GSList *options, struct sr_dev_driver *di)
                        alsa_scan_handle_dev(&devices, cardname, hwdev,
                                             di, pcminfo);
                }
-               snd_ctl_close(handle);
+               if ((ret = snd_ctl_close(handle)) < 0) {
+                       sr_dbg("Cannot close device (%d): %s.",
+                              card, snd_strerror(ret));
+               }
        }
 
        snd_pcm_info_free(pcminfo);
@@ -209,19 +282,47 @@ SR_PRIV GSList *alsa_scan(GSList *options, struct sr_dev_driver *di)
        return devices;
 }
 
-/*
- * Helper to be used with g_slist_free_full(); for properly freeing an alsa
- * dev instance.
+/**
+ * Set the samplerate of the ALSA device.
+ *
+ * Changes the samplerate of the given ALSA device if the specified samplerate
+ * is supported by the hardware.
+ *
+ * The new samplerate is recorded, but it is not applied to the hardware. The
+ * samplerate is applied to the hardware only when acquisition is started via
+ * dev_acquisition_start(), and cannot be changed during acquisition. To change
+ * the samplerate, several steps are needed:
+ *
+ *    1) If acquisition is running, it must first be stopped.
+ *    2) dev_config_set() must be called with the new samplerate.
+ *    3) When starting a new acquisition, the new samplerate is applied.
+ *
  */
-SR_PRIV void alsa_dev_inst_clear(struct sr_dev_inst *sdi)
+SR_PRIV int alsa_set_samplerate(const struct sr_dev_inst *sdi,
+                               uint64_t newrate)
 {
        struct dev_context *devc;
+       size_t i;
+       uint64_t rate = 0;
 
        if (!(devc = sdi->priv))
-               return;
+               return SR_ERR_ARG;
 
-       snd_pcm_hw_params_free(devc->hw_params);
-       sr_dev_inst_free(sdi);
+       i = 0;
+       do {
+               if (newrate == devc->samplerates[i]) {
+                       rate = newrate;
+                       break;
+               }
+       } while (devc->samplerates[i++] != 0);
+
+       if (!rate) {
+               sr_err("Sample rate %" PRIu64 " not supported.", newrate);
+               return SR_ERR_ARG;
+       }
+
+       devc->cur_samplerate = rate;
+       return SR_OK;
 }
 
 SR_PRIV int alsa_receive_data(int fd, int revents, void *cb_data)
@@ -230,9 +331,10 @@ SR_PRIV int alsa_receive_data(int fd, int revents, void *cb_data)
        struct dev_context *devc;
        struct sr_datafeed_packet packet;
        struct sr_datafeed_analog analog;
-       char inbuf[4096];
+       int16_t inbuf[4096];
        int i, x, count, offset, samples_to_get;
-       uint16_t tmp16;
+       int16_t tmp16;
+       const float s16norm = 1 / (float)(1 << 15);
 
        (void)fd;
        (void)revents;
@@ -255,22 +357,30 @@ SR_PRIV int alsa_receive_data(int fd, int revents, void *cb_data)
                sr_spew("Only got %d/%d samples.", count, samples_to_get);
        }
 
-       analog.data = g_try_malloc0(count * sizeof(float) * devc->num_probes);
+       analog.data = g_try_malloc0(count * sizeof(float) * devc->num_channels);
        if (!analog.data) {
                sr_err("Failed to malloc sample buffer.");
                return FALSE;
        }
 
        offset = 0;
-
-       for (i = 0; i < count; i++) {
-               for (x = 0; x < devc->num_probes; x++) {
-                       tmp16 = *(uint16_t *)(inbuf + (i * 4) + (x * 2));
-                       analog.data[offset++] = (float)tmp16;
+       /*
+        * It's impossible to know what voltage levels the soundcard handles.
+        * Some handle 0 dBV rms, some 0dBV peak-to-peak, +4dbmW (600 ohm), etc
+        * Each of these corresponds to a different voltage, and there is no
+        * mechanism to determine this voltage. The best solution is to send all
+        * audio data as a normalized float, and let the frontend or user worry
+        * about the calibration.
+        */
+       for (i = 0; i < count; i += devc->num_channels) {
+               for (x = 0; x < devc->num_channels; x++) {
+                       tmp16 = inbuf[i + x];
+                       analog.data[offset++] = tmp16 * s16norm;
                }
        }
 
        /* Send a sample packet with the analog values. */
+       analog.channels = sdi->channels;
        analog.num_samples = count;
        analog.mq = SR_MQ_VOLTAGE; /* FIXME */
        analog.unit = SR_UNIT_VOLT; /* FIXME */
@@ -290,4 +400,3 @@ SR_PRIV int alsa_receive_data(int fd, int revents, void *cb_data)
 
        return TRUE;
 }
-