-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathin_wav.c
212 lines (178 loc) · 5.14 KB
/
in_wav.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* This file is part of the libsigrok 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 "../libsigrok-internal.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <strings.h>
#include "../log.h"
#include <stdlib.h>
#undef LOG_PREFIX
#define LOG_PREFIX "input/wav: "
#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_channel *probe;
struct context *ctx;
char buf[40], probename[15];
int i;
if (get_wav_header(filename, buf) != SR_OK)
return SR_ERR;
if (!(ctx = x_malloc(sizeof(struct context)))){
sr_err("%s,ERROR:failed to alloc memory.", __func__);
return SR_ERR_MALLOC;
}
memset(ctx, 0, sizeof(struct context));
/* Create a virtual device. */
in->sdi = sr_dev_inst_new(LOGIC, 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++) {
sprintf(probename,"CH%d", i + 1);
if (!(probe = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, probename)))
return SR_ERR;
in->sdi->channels = g_slist_append(in->sdi->channels, 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;
packet.status = SR_PKT_OK;
/* Send header packet to the session bus. */
std_session_send_df_header(in->sdi, LOG_PREFIX);
packet.type = SR_DF_META;
packet.payload = &meta;
src = sr_config_new(SR_CONF_SAMPLERATE,
g_variant_new_uint64(ctx->samplerate));
meta.config = g_slist_append(NULL, src);
ds_data_forward(in->sdi, &packet);
sr_config_free(src);
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->channels;
analog.num_samples = chunk_samples;
analog.mq = 0;
analog.unit = 0;
analog.data = fdata;
ds_data_forward(in->sdi, &packet);
}
close(fd);
packet.type = SR_DF_END;
ds_data_forward(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,
};