2 * This file is part of the libsigrok 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 #define LOG_PREFIX "input/wav"
30 #define CHUNK_SIZE 4096
38 static int get_wav_header(const char *filename, char *buf)
44 if (l <= 4 || strcasecmp(filename + l - 4, ".wav"))
47 if (stat(filename, &st) == -1)
50 /* Minimum size of header + 1 8-bit mono PCM sample. */
53 if ((fd = open(filename, O_RDONLY)) == -1)
56 l = read(fd, buf, 40);
64 static int format_match(const char *filename)
68 if (get_wav_header(filename, buf) != SR_OK)
71 if (strncmp(buf, "RIFF", 4))
73 if (strncmp(buf + 8, "WAVE", 4))
75 if (strncmp(buf + 12, "fmt ", 4))
77 if (GUINT16_FROM_LE(*(uint16_t *)(buf + 20)) != 1)
80 if (strncmp(buf + 36, "data", 4))
86 static int init(struct sr_input *in, const char *filename)
88 struct sr_channel *ch;
90 char buf[40], channelname[8];
93 if (get_wav_header(filename, buf) != SR_OK)
96 if (!(ctx = g_try_malloc0(sizeof(struct context))))
99 /* Create a virtual device. */
100 in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL);
103 ctx->samplerate = GUINT32_FROM_LE(*(uint32_t *)(buf + 24));
104 ctx->samplesize = GUINT16_FROM_LE(*(uint16_t *)(buf + 34)) / 8;
105 if (ctx->samplesize != 1 && ctx->samplesize != 2 && ctx->samplesize != 4) {
106 sr_err("only 8, 16 or 32 bits per sample supported.");
110 if ((ctx->num_channels = GUINT16_FROM_LE(*(uint16_t *)(buf + 22))) > 20) {
111 sr_err("%d channels seems crazy.", ctx->num_channels);
115 for (i = 0; i < ctx->num_channels; i++) {
116 snprintf(channelname, 8, "CH%d", i + 1);
117 if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, channelname)))
119 in->sdi->channels = g_slist_append(in->sdi->channels, ch);
125 static int loadfile(struct sr_input *in, const char *filename)
127 struct sr_datafeed_packet packet;
128 struct sr_datafeed_meta meta;
129 struct sr_datafeed_analog analog;
130 struct sr_config *src;
132 float fdata[CHUNK_SIZE];
134 int num_samples, chunk_samples, s, c, fd, l;
135 char buf[CHUNK_SIZE];
139 /* Send header packet to the session bus. */
140 std_session_send_df_header(in->sdi, LOG_PREFIX);
142 packet.type = SR_DF_META;
143 packet.payload = &meta;
144 src = sr_config_new(SR_CONF_SAMPLERATE,
145 g_variant_new_uint64(ctx->samplerate));
146 meta.config = g_slist_append(NULL, src);
147 sr_session_send(in->sdi, &packet);
150 if ((fd = open(filename, O_RDONLY)) == -1)
153 lseek(fd, 40, SEEK_SET);
154 l = read(fd, buf, 4);
155 num_samples = GUINT32_FROM_LE((uint32_t)*(buf));
156 num_samples /= ctx->samplesize / ctx->num_channels;
158 if ((l = read(fd, buf, CHUNK_SIZE)) < 1)
160 chunk_samples = l / ctx->samplesize / ctx->num_channels;
161 for (s = 0; s < chunk_samples; s++) {
162 for (c = 0; c < ctx->num_channels; c++) {
164 memcpy(&sample, buf + s * ctx->samplesize + c, ctx->samplesize);
165 switch (ctx->samplesize) {
167 /* 8-bit PCM samples are unsigned. */
168 fdata[s + c] = (uint8_t)sample / 255.0;
171 fdata[s + c] = GINT16_FROM_LE(sample) / 32767.0;
174 fdata[s + c] = GINT32_FROM_LE(sample) / 65535.0;
179 packet.type = SR_DF_ANALOG;
180 packet.payload = &analog;
181 analog.channels = in->sdi->channels;
182 analog.num_samples = chunk_samples;
186 sr_session_send(in->sdi, &packet);
190 packet.type = SR_DF_END;
191 sr_session_send(in->sdi, &packet);
197 SR_PRIV struct sr_input_format input_wav = {
199 .description = "WAV file",
200 .format_match = format_match,
202 .loadfile = loadfile,