]>
Commit | Line | Data |
---|---|---|
1d36b4d2 | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
1d36b4d2 BV |
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 | ||
29a27196 UH |
28 | /* Message logging helpers with subsystem-specific prefix string. */ |
29 | #define LOG_PREFIX "input/wav: " | |
30 | #define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args) | |
31 | #define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args) | |
32 | #define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args) | |
33 | #define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args) | |
34 | #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args) | |
35 | #define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args) | |
1d36b4d2 BV |
36 | |
37 | #define CHUNK_SIZE 4096 | |
38 | ||
39 | struct context { | |
40 | uint64_t samplerate; | |
41 | int samplesize; | |
42 | int num_channels; | |
43 | }; | |
44 | ||
45 | static int get_wav_header(const char *filename, char *buf) | |
46 | { | |
47 | struct stat st; | |
48 | int fd, l; | |
49 | ||
50 | l = strlen(filename); | |
51 | if (l <= 4 || strcasecmp(filename + l - 4, ".wav")) | |
52 | return SR_ERR; | |
53 | ||
54 | if (stat(filename, &st) == -1) | |
55 | return SR_ERR; | |
56 | if (st.st_size <= 45) | |
57 | /* Minimum size of header + 1 8-bit mono PCM sample. */ | |
58 | return SR_ERR; | |
59 | ||
60 | if ((fd = open(filename, O_RDONLY)) == -1) | |
61 | return SR_ERR; | |
62 | ||
63 | l = read(fd, buf, 40); | |
64 | close(fd); | |
65 | if (l != 40) | |
66 | return SR_ERR; | |
67 | ||
68 | return SR_OK; | |
69 | } | |
70 | ||
71 | static int format_match(const char *filename) | |
72 | { | |
73 | char buf[40]; | |
74 | ||
75 | if (get_wav_header(filename, buf) != SR_OK) | |
76 | return FALSE; | |
77 | ||
78 | if (strncmp(buf, "RIFF", 4)) | |
79 | return FALSE; | |
80 | if (strncmp(buf + 8, "WAVE", 4)) | |
81 | return FALSE; | |
82 | if (strncmp(buf + 12, "fmt ", 4)) | |
83 | return FALSE; | |
84 | if (GUINT16_FROM_LE(*(uint16_t *)(buf + 20)) != 1) | |
85 | /* Not PCM. */ | |
86 | return FALSE; | |
87 | if (strncmp(buf + 36, "data", 4)) | |
88 | return FALSE; | |
89 | ||
90 | return TRUE; | |
91 | } | |
92 | ||
93 | static int init(struct sr_input *in, const char *filename) | |
94 | { | |
95 | struct sr_probe *probe; | |
96 | struct context *ctx; | |
97 | char buf[40], probename[8]; | |
98 | int i; | |
99 | ||
100 | if (get_wav_header(filename, buf) != SR_OK) | |
101 | return SR_ERR; | |
102 | ||
103 | if (!(ctx = g_try_malloc0(sizeof(struct context)))) | |
104 | return SR_ERR_MALLOC; | |
105 | ||
106 | /* Create a virtual device. */ | |
107 | in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL); | |
108 | in->sdi->priv = ctx; | |
109 | ||
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."); | |
114 | return SR_ERR; | |
115 | } | |
116 | ||
117 | if ((ctx->num_channels = GUINT16_FROM_LE(*(uint16_t *)(buf + 22))) > 20) { | |
118 | sr_err("%d channels seems crazy.", ctx->num_channels); | |
119 | return SR_ERR; | |
120 | } | |
121 | ||
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))) | |
125 | return SR_ERR; | |
126 | in->sdi->probes = g_slist_append(in->sdi->probes, probe); | |
127 | } | |
128 | ||
129 | return SR_OK; | |
130 | } | |
131 | ||
132 | static int loadfile(struct sr_input *in, const char *filename) | |
133 | { | |
134 | struct sr_datafeed_packet packet; | |
135 | struct sr_datafeed_meta meta; | |
136 | struct sr_datafeed_analog analog; | |
137 | struct sr_config *src; | |
138 | struct context *ctx; | |
139 | float fdata[CHUNK_SIZE]; | |
140 | uint64_t sample; | |
141 | int num_samples, chunk_samples, s, c, fd, l; | |
142 | char buf[CHUNK_SIZE]; | |
143 | ||
144 | ctx = in->sdi->priv; | |
145 | ||
146 | /* Send header packet to the session bus. */ | |
29a27196 | 147 | std_session_send_df_header(in->sdi, LOG_PREFIX); |
1d36b4d2 BV |
148 | |
149 | packet.type = SR_DF_META; | |
150 | packet.payload = &meta; | |
ec4063b8 BV |
151 | src = sr_config_new(SR_CONF_SAMPLERATE, |
152 | g_variant_new_uint64(ctx->samplerate)); | |
1d36b4d2 BV |
153 | meta.config = g_slist_append(NULL, src); |
154 | sr_session_send(in->sdi, &packet); | |
ec4063b8 | 155 | sr_config_free(src); |
1d36b4d2 BV |
156 | |
157 | if ((fd = open(filename, O_RDONLY)) == -1) | |
158 | return SR_ERR; | |
159 | ||
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; | |
164 | while (TRUE) { | |
165 | if ((l = read(fd, buf, CHUNK_SIZE)) < 1) | |
166 | break; | |
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++) { | |
170 | sample = 0; | |
171 | memcpy(&sample, buf + s * ctx->samplesize + c, ctx->samplesize); | |
172 | switch (ctx->samplesize) { | |
173 | case 1: | |
174 | /* 8-bit PCM samples are unsigned. */ | |
175 | fdata[s + c] = (uint8_t)sample / 255.0; | |
176 | break; | |
177 | case 2: | |
178 | fdata[s + c] = GINT16_FROM_LE(sample) / 32767.0; | |
179 | break; | |
180 | case 4: | |
181 | fdata[s + c] = GINT32_FROM_LE(sample) / 65535.0; | |
182 | break; | |
183 | } | |
184 | } | |
185 | } | |
186 | packet.type = SR_DF_ANALOG; | |
187 | packet.payload = &analog; | |
188 | analog.probes = in->sdi->probes; | |
189 | analog.num_samples = chunk_samples; | |
190 | analog.mq = 0; | |
191 | analog.unit = 0; | |
192 | analog.data = fdata; | |
193 | sr_session_send(in->sdi, &packet); | |
194 | } | |
195 | ||
196 | close(fd); | |
197 | packet.type = SR_DF_END; | |
198 | sr_session_send(in->sdi, &packet); | |
199 | ||
200 | return SR_OK; | |
201 | } | |
202 | ||
203 | ||
204 | SR_PRIV struct sr_input_format input_wav = { | |
205 | .id = "wav", | |
206 | .description = "WAV file", | |
207 | .format_match = format_match, | |
208 | .init = init, | |
209 | .loadfile = loadfile, | |
210 | }; | |
211 |