]> sigrok.org Git - libsigrok.git/blob - input/wav.c
build: Portability fixes.
[libsigrok.git] / input / wav.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include "libsigrok.h"
26 #include "libsigrok-internal.h"
27
28 #define LOG_PREFIX "input/wav"
29
30 #define CHUNK_SIZE 4096
31
32 struct context {
33         uint64_t samplerate;
34         int samplesize;
35         int num_channels;
36 };
37
38 static int get_wav_header(const char *filename, char *buf)
39 {
40         struct stat st;
41         int fd, l;
42
43         l = strlen(filename);
44         if (l <= 4 || strcasecmp(filename + l - 4, ".wav"))
45                 return SR_ERR;
46
47         if (stat(filename, &st) == -1)
48                 return SR_ERR;
49         if (st.st_size <= 45)
50                 /* Minimum size of header + 1 8-bit mono PCM sample. */
51                 return SR_ERR;
52
53         if ((fd = open(filename, O_RDONLY)) == -1)
54                 return SR_ERR;
55
56         l = read(fd, buf, 40);
57         close(fd);
58         if (l != 40)
59                 return SR_ERR;
60
61         return SR_OK;
62 }
63
64 static int format_match(const char *filename)
65 {
66         char buf[40];
67
68         if (get_wav_header(filename, buf) != SR_OK)
69                 return FALSE;
70
71         if (strncmp(buf, "RIFF", 4))
72                 return FALSE;
73         if (strncmp(buf + 8, "WAVE", 4))
74                 return FALSE;
75         if (strncmp(buf + 12, "fmt ", 4))
76                 return FALSE;
77         if (GUINT16_FROM_LE(*(uint16_t *)(buf + 20)) != 1)
78                 /* Not PCM. */
79                 return FALSE;
80         if (strncmp(buf + 36, "data", 4))
81                 return FALSE;
82
83         return TRUE;
84 }
85
86 static int init(struct sr_input *in, const char *filename)
87 {
88         struct sr_channel *ch;
89         struct context *ctx;
90         char buf[40], channelname[8];
91         int i;
92
93         if (get_wav_header(filename, buf) != SR_OK)
94                 return SR_ERR;
95
96         if (!(ctx = g_try_malloc0(sizeof(struct context))))
97                 return SR_ERR_MALLOC;
98
99         /* Create a virtual device. */
100         in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL);
101         in->sdi->priv = ctx;
102
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.");
107                 return SR_ERR;
108         }
109
110         if ((ctx->num_channels = GUINT16_FROM_LE(*(uint16_t *)(buf + 22))) > 20) {
111                 sr_err("%d channels seems crazy.", ctx->num_channels);
112                 return SR_ERR;
113         }
114
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)))
118                         return SR_ERR;
119                 in->sdi->channels = g_slist_append(in->sdi->channels, ch);
120         }
121
122         return SR_OK;
123 }
124
125 static int loadfile(struct sr_input *in, const char *filename)
126 {
127         struct sr_datafeed_packet packet;
128         struct sr_datafeed_meta meta;
129         struct sr_datafeed_analog analog;
130         struct sr_config *src;
131         struct context *ctx;
132         float fdata[CHUNK_SIZE];
133         uint64_t sample;
134         int num_samples, chunk_samples, s, c, fd, l;
135         char buf[CHUNK_SIZE];
136
137         ctx = in->sdi->priv;
138
139         /* Send header packet to the session bus. */
140         std_session_send_df_header(in->sdi, LOG_PREFIX);
141
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);
148         sr_config_free(src);
149
150         if ((fd = open(filename, O_RDONLY)) == -1)
151                 return SR_ERR;
152
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;
157         while (TRUE) {
158                 if ((l = read(fd, buf, CHUNK_SIZE)) < 1)
159                         break;
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++) {
163                                 sample = 0;
164                                 memcpy(&sample, buf + s * ctx->samplesize + c, ctx->samplesize);
165                                 switch (ctx->samplesize) {
166                                 case 1:
167                                         /* 8-bit PCM samples are unsigned. */
168                                         fdata[s + c] = (uint8_t)sample / 255.0;
169                                         break;
170                                 case 2:
171                                         fdata[s + c] = GINT16_FROM_LE(sample) / 32767.0;
172                                         break;
173                                 case 4:
174                                         fdata[s + c] = GINT32_FROM_LE(sample) / 65535.0;
175                                         break;
176                                 }
177                         }
178                 }
179                 packet.type = SR_DF_ANALOG;
180                 packet.payload = &analog;
181                 analog.channels = in->sdi->channels;
182                 analog.num_samples = chunk_samples;
183                 analog.mq = 0;
184                 analog.unit = 0;
185                 analog.data = fdata;
186                 sr_session_send(in->sdi, &packet);
187         }
188
189         close(fd);
190         packet.type = SR_DF_END;
191         sr_session_send(in->sdi, &packet);
192
193         return SR_OK;
194 }
195
196
197 SR_PRIV struct sr_input_format input_wav = {
198         .id = "wav",
199         .description = "WAV file",
200         .format_match = format_match,
201         .init = init,
202         .loadfile = loadfile,
203 };
204