MCPcopy Create free account
hub / github.com/DreamSourceLab/DSView / loadfile

Function loadfile

libsigrok4DSL/input/in_wav.c:131–201  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

129}
130
131static int loadfile(struct sr_input *in, const char *filename)
132{
133 struct sr_datafeed_packet packet;
134 struct sr_datafeed_meta meta;
135 struct sr_datafeed_analog analog;
136 struct sr_config *src;
137 struct context *ctx;
138 float fdata[CHUNK_SIZE];
139 uint64_t sample;
140 int num_samples, chunk_samples, s, c, fd, l;
141 char buf[CHUNK_SIZE];
142
143 ctx = in->sdi->priv;
144 packet.status = SR_PKT_OK;
145
146 /* Send header packet to the session bus. */
147 std_session_send_df_header(in->sdi, LOG_PREFIX);
148
149 packet.type = SR_DF_META;
150 packet.payload = &meta;
151 src = sr_config_new(SR_CONF_SAMPLERATE,
152 g_variant_new_uint64(ctx->samplerate));
153 meta.config = g_slist_append(NULL, src);
154 ds_data_forward(in->sdi, &packet);
155 sr_config_free(src);
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->channels;

Callers

nothing calls this directly

Calls 4

sr_config_newFunction · 0.85
ds_data_forwardFunction · 0.85
sr_config_freeFunction · 0.85

Tested by

no test coverage detected