2 * This file is part of the sigrok project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <sys/types.h>
25 #include "libsigrok.h"
26 #include "libsigrok-internal.h"
28 /* Message logging helpers with driver-specific prefix string. */
29 #define DRIVER_LOG_DOMAIN "input/wav: "
30 #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
31 #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
32 #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
33 #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
34 #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
35 #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
37 #define CHUNK_SIZE 4096
45 static int get_wav_header(const char *filename, char *buf)
51 if (l <= 4 || strcasecmp(filename + l - 4, ".wav"))
54 if (stat(filename, &st) == -1)
57 /* Minimum size of header + 1 8-bit mono PCM sample. */
60 if ((fd = open(filename, O_RDONLY)) == -1)
63 l = read(fd, buf, 40);
71 static int format_match(const char *filename)
75 if (get_wav_header(filename, buf) != SR_OK)
78 if (strncmp(buf, "RIFF", 4))
80 if (strncmp(buf + 8, "WAVE", 4))
82 if (strncmp(buf + 12, "fmt ", 4))
84 if (GUINT16_FROM_LE(*(uint16_t *)(buf + 20)) != 1)
87 if (strncmp(buf + 36, "data", 4))
93 static int init(struct sr_input *in, const char *filename)
95 struct sr_probe *probe;
97 char buf[40], probename[8];
100 if (get_wav_header(filename, buf) != SR_OK)
103 if (!(ctx = g_try_malloc0(sizeof(struct context))))
104 return SR_ERR_MALLOC;
106 /* Create a virtual device. */
107 in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL);
110 ctx->samplerate = GUINT32_FROM_LE(*(uint32_t *)(buf + 24));
111 ctx->samplesize = GUINT16_FROM_LE(*(uint16_t *)(buf + 34)) / 8;
112 if (ctx->samplesize != 1 && ctx->samplesize != 2 && ctx->samplesize != 4) {
113 sr_err("only 8, 16 or 32 bits per sample supported.");
117 if ((ctx->num_channels = GUINT16_FROM_LE(*(uint16_t *)(buf + 22))) > 20) {
118 sr_err("%d channels seems crazy.", ctx->num_channels);
122 for (i = 0; i < ctx->num_channels; i++) {
123 snprintf(probename, 8, "CH%d", i + 1);
124 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, probename)))
126 in->sdi->probes = g_slist_append(in->sdi->probes, probe);
132 static int loadfile(struct sr_input *in, const char *filename)
134 struct sr_datafeed_packet packet;
135 struct sr_datafeed_meta meta;
136 struct sr_datafeed_analog analog;
137 struct sr_config *src;
139 float fdata[CHUNK_SIZE];
141 int num_samples, chunk_samples, s, c, fd, l;
142 char buf[CHUNK_SIZE];
146 /* Send header packet to the session bus. */
147 std_session_send_df_header(in->sdi, DRIVER_LOG_DOMAIN);
149 packet.type = SR_DF_META;
150 packet.payload = &meta;
151 src = sr_config_new(SR_CONF_SAMPLERATE,
152 g_variant_new_uint64(ctx->samplerate));
153 meta.config = g_slist_append(NULL, src);
154 sr_session_send(in->sdi, &packet);
157 if ((fd = open(filename, O_RDONLY)) == -1)
160 lseek(fd, 40, SEEK_SET);
161 l = read(fd, buf, 4);
162 num_samples = GUINT32_FROM_LE((uint32_t)*(buf));
163 num_samples /= ctx->samplesize / ctx->num_channels;
165 if ((l = read(fd, buf, CHUNK_SIZE)) < 1)
167 chunk_samples = l / ctx->samplesize / ctx->num_channels;
168 for (s = 0; s < chunk_samples; s++) {
169 for (c = 0; c < ctx->num_channels; c++) {
171 memcpy(&sample, buf + s * ctx->samplesize + c, ctx->samplesize);
172 switch (ctx->samplesize) {
174 /* 8-bit PCM samples are unsigned. */
175 fdata[s + c] = (uint8_t)sample / 255.0;
178 fdata[s + c] = GINT16_FROM_LE(sample) / 32767.0;
181 fdata[s + c] = GINT32_FROM_LE(sample) / 65535.0;
186 packet.type = SR_DF_ANALOG;
187 packet.payload = &analog;
188 analog.probes = in->sdi->probes;
189 analog.num_samples = chunk_samples;
193 sr_session_send(in->sdi, &packet);
197 packet.type = SR_DF_END;
198 sr_session_send(in->sdi, &packet);
204 SR_PRIV struct sr_input_format input_wav = {
206 .description = "WAV file",
207 .format_match = format_match,
209 .loadfile = loadfile,