input: new WAV file module

This commit is contained in:
Bert Vermeulen 2013-02-21 15:20:50 +01:00
parent 543d45c581
commit 1d36b4d27f
3 changed files with 214 additions and 1 deletions

View File

@ -2,6 +2,7 @@
## This file is part of the sigrok project. ## This file is part of the sigrok project.
## ##
## Copyright (C) 2011 Uwe Hermann <uwe@hermann-uwe.de> ## Copyright (C) 2011 Uwe Hermann <uwe@hermann-uwe.de>
## Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
## ##
## This program is free software: you can redistribute it and/or modify ## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by ## it under the terms of the GNU General Public License as published by
@ -23,8 +24,9 @@ noinst_LTLIBRARIES = libsigrokinput.la
libsigrokinput_la_SOURCES = \ libsigrokinput_la_SOURCES = \
binary.c \ binary.c \
chronovu_la8.c \ chronovu_la8.c \
input.c \
vcd.c \ vcd.c \
input.c wav.c
libsigrokinput_la_CFLAGS = \ libsigrokinput_la_CFLAGS = \
-I$(top_srcdir) -I$(top_srcdir)

View File

@ -38,11 +38,13 @@
extern SR_PRIV struct sr_input_format input_chronovu_la8; extern SR_PRIV struct sr_input_format input_chronovu_la8;
extern SR_PRIV struct sr_input_format input_binary; extern SR_PRIV struct sr_input_format input_binary;
extern SR_PRIV struct sr_input_format input_vcd; extern SR_PRIV struct sr_input_format input_vcd;
extern SR_PRIV struct sr_input_format input_wav;
/* @endcond */ /* @endcond */
static struct sr_input_format *input_module_list[] = { static struct sr_input_format *input_module_list[] = {
&input_vcd, &input_vcd,
&input_chronovu_la8, &input_chronovu_la8,
&input_wav,
/* This one has to be last, because it will take any input. */ /* This one has to be last, because it will take any input. */
&input_binary, &input_binary,
NULL, NULL,

209
input/wav.c Normal file
View File

@ -0,0 +1,209 @@
/*
* This file is part of the sigrok project.
*
* Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include "libsigrok.h"
#include "libsigrok-internal.h"
/* Message logging helpers with driver-specific prefix string. */
#define DRIVER_LOG_DOMAIN "input/wav: "
#define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
#define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
#define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
#define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
#define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
#define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
#define CHUNK_SIZE 4096
struct context {
uint64_t samplerate;
int samplesize;
int num_channels;
};
static int get_wav_header(const char *filename, char *buf)
{
struct stat st;
int fd, l;
l = strlen(filename);
if (l <= 4 || strcasecmp(filename + l - 4, ".wav"))
return SR_ERR;
if (stat(filename, &st) == -1)
return SR_ERR;
if (st.st_size <= 45)
/* Minimum size of header + 1 8-bit mono PCM sample. */
return SR_ERR;
if ((fd = open(filename, O_RDONLY)) == -1)
return SR_ERR;
l = read(fd, buf, 40);
close(fd);
if (l != 40)
return SR_ERR;
return SR_OK;
}
static int format_match(const char *filename)
{
char buf[40];
if (get_wav_header(filename, buf) != SR_OK)
return FALSE;
if (strncmp(buf, "RIFF", 4))
return FALSE;
if (strncmp(buf + 8, "WAVE", 4))
return FALSE;
if (strncmp(buf + 12, "fmt ", 4))
return FALSE;
if (GUINT16_FROM_LE(*(uint16_t *)(buf + 20)) != 1)
/* Not PCM. */
return FALSE;
if (strncmp(buf + 36, "data", 4))
return FALSE;
return TRUE;
}
static int init(struct sr_input *in, const char *filename)
{
struct sr_probe *probe;
struct context *ctx;
char buf[40], probename[8];
int i;
if (get_wav_header(filename, buf) != SR_OK)
return SR_ERR;
if (!(ctx = g_try_malloc0(sizeof(struct context))))
return SR_ERR_MALLOC;
/* Create a virtual device. */
in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL);
in->sdi->priv = ctx;
ctx->samplerate = GUINT32_FROM_LE(*(uint32_t *)(buf + 24));
ctx->samplesize = GUINT16_FROM_LE(*(uint16_t *)(buf + 34)) / 8;
if (ctx->samplesize != 1 && ctx->samplesize != 2 && ctx->samplesize != 4) {
sr_err("only 8, 16 or 32 bits per sample supported.");
return SR_ERR;
}
if ((ctx->num_channels = GUINT16_FROM_LE(*(uint16_t *)(buf + 22))) > 20) {
sr_err("%d channels seems crazy.", ctx->num_channels);
return SR_ERR;
}
for (i = 0; i < ctx->num_channels; i++) {
snprintf(probename, 8, "CH%d", i + 1);
if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, probename)))
return SR_ERR;
in->sdi->probes = g_slist_append(in->sdi->probes, probe);
}
return SR_OK;
}
static int loadfile(struct sr_input *in, const char *filename)
{
struct sr_datafeed_packet packet;
struct sr_datafeed_meta meta;
struct sr_datafeed_analog analog;
struct sr_config *src;
struct context *ctx;
float fdata[CHUNK_SIZE];
uint64_t sample;
int num_samples, chunk_samples, s, c, fd, l;
char buf[CHUNK_SIZE];
ctx = in->sdi->priv;
/* Send header packet to the session bus. */
std_session_send_df_header(in->sdi, DRIVER_LOG_DOMAIN);
packet.type = SR_DF_META;
packet.payload = &meta;
src = sr_config_make(SR_CONF_SAMPLERATE, (const void *)&ctx->samplerate);
meta.config = g_slist_append(NULL, src);
sr_session_send(in->sdi, &packet);
if ((fd = open(filename, O_RDONLY)) == -1)
return SR_ERR;
lseek(fd, 40, SEEK_SET);
l = read(fd, buf, 4);
num_samples = GUINT32_FROM_LE((uint32_t)*(buf));
num_samples /= ctx->samplesize / ctx->num_channels;
while (TRUE) {
if ((l = read(fd, buf, CHUNK_SIZE)) < 1)
break;
chunk_samples = l / ctx->samplesize / ctx->num_channels;
for (s = 0; s < chunk_samples; s++) {
for (c = 0; c < ctx->num_channels; c++) {
sample = 0;
memcpy(&sample, buf + s * ctx->samplesize + c, ctx->samplesize);
switch (ctx->samplesize) {
case 1:
/* 8-bit PCM samples are unsigned. */
fdata[s + c] = (uint8_t)sample / 255.0;
break;
case 2:
fdata[s + c] = GINT16_FROM_LE(sample) / 32767.0;
break;
case 4:
fdata[s + c] = GINT32_FROM_LE(sample) / 65535.0;
break;
}
}
}
packet.type = SR_DF_ANALOG;
packet.payload = &analog;
analog.probes = in->sdi->probes;
analog.num_samples = chunk_samples;
analog.mq = 0;
analog.unit = 0;
analog.data = fdata;
sr_session_send(in->sdi, &packet);
}
close(fd);
packet.type = SR_DF_END;
sr_session_send(in->sdi, &packet);
return SR_OK;
}
SR_PRIV struct sr_input_format input_wav = {
.id = "wav",
.description = "WAV file",
.format_match = format_match,
.init = init,
.loadfile = loadfile,
};