input/wav: use our own endian macros.

These should work better on non-aligned memory locations.
This commit is contained in:
Bert Vermeulen 2014-08-26 22:48:12 +02:00
parent 5bf0dd6aff
commit 962d43440a
1 changed files with 10 additions and 9 deletions

View File

@ -23,6 +23,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
#include <stdint.h>
#include "libsigrok.h" #include "libsigrok.h"
#include "libsigrok-internal.h" #include "libsigrok-internal.h"
@ -58,10 +59,10 @@ static int parse_wav_header(GString *buf, struct context *inc)
return SR_ERR; return SR_ERR;
} }
fmt_code = GUINT16_FROM_LE(*(uint16_t *)(buf->str + 20)); fmt_code = RL16(buf->str + 20);
samplerate = GUINT32_FROM_LE(*(uint32_t *)(buf->str + 24)); samplerate = RL32(buf->str + 24);
samplesize = GUINT16_FROM_LE(*(uint16_t *)(buf->str + 32)); samplesize = RL16(buf->str + 32);
num_channels = GUINT16_FROM_LE(*(uint16_t *)(buf->str + 22)); num_channels = RL16(buf->str + 22);
/* TODO div0 */ /* TODO div0 */
unitsize = samplesize / num_channels; unitsize = samplesize / num_channels;
@ -139,7 +140,7 @@ static int find_data_chunk(GString *buf, int initial_offset)
return -1; return -1;
} }
/* Skip past this chunk. */ /* Skip past this chunk. */
offset += 8 + GUINT32_FROM_LE(*(uint32_t *)(buf->str + offset + 4)); offset += 8 + RL32(buf->str + offset + 4);
} }
return offset; return offset;
@ -204,13 +205,13 @@ static void send_chunk(const struct sr_input *in, int offset, int num_samples)
switch (inc->samplesize) { switch (inc->samplesize) {
case 1: case 1:
/* 8-bit PCM samples are unsigned. */ /* 8-bit PCM samples are unsigned. */
fdata[samplenum] = (uint8_t)sample / 255.0; fdata[samplenum] = (uint8_t)sample / (float)255;
break; break;
case 2: case 2:
fdata[samplenum] = GINT16_FROM_LE(sample) / 32767.0; fdata[samplenum] = RL16S(&sample) / (float)INT16_MAX;
break; break;
case 4: case 4:
fdata[samplenum] = GINT32_FROM_LE(sample) / 65535.0; fdata[samplenum] = RL32S(&sample) / (float)INT32_MAX;
break; break;
} }
} else { } else {
@ -258,7 +259,7 @@ static int receive(const struct sr_input *in, GString *buf)
if (!inc->found_data) { if (!inc->found_data) {
/* Skip past size of 'fmt ' chunk. */ /* Skip past size of 'fmt ' chunk. */
i = 20 + GUINT32_FROM_LE(*(uint32_t *)(in->buf->str + 16)); i = 20 + RL32(in->buf->str + 16);
offset = find_data_chunk(in->buf, i); offset = find_data_chunk(in->buf, i);
if (offset < 0) { if (offset < 0) {
if (in->buf->len > MAX_DATA_CHUNK_OFFSET) { if (in->buf->len > MAX_DATA_CHUNK_OFFSET) {