summaryrefslogtreecommitdiff
path: root/cras/src/dsp/tests/raw.c
blob: d75e28fa01a3831289c380b1c167bddfddd534ab (plain)
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
/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

float *read_raw(const char *filename, size_t *frames)
{
	struct stat st;
	int16_t *buf;
	size_t f, n;
	int fd;
	float *data;
	int i;

	if (stat(filename, &st) < 0) {
		fprintf(stderr, "cannot stat file %s\n", filename);
		return NULL;
	}

	fd = open(filename, O_RDONLY);
	if (fd < 0) {
		fprintf(stderr, "cannot open file %s\n", filename);
		return NULL;
	}

	f = st.st_size / 4;
	n = f * 4;
	buf = (int16_t *)malloc(n);
	if (read(fd, buf, n) != n) {
		fprintf(stderr, "short read %zu\n", n);
		free(buf);
		return NULL;
	}
	close(fd);

	/* deinterleave and convert to float */
	data = (float *)malloc(sizeof(float) * f * 2);
	for (i = 0; i < f; i++) {
		data[i] = buf[2 * i] / 32768.0f;
		data[i + f] = buf[2 * i + 1] / 32768.0f;
	}
	free(buf);
	*frames = f;
	return data;
}

static int16_t f2s16(float f)
{
	int i;
	f *= 32768;
	i = (int)((f > 0) ? (f + 0.5f) : (f - 0.5f));
	if (i < -32768)
		i = -32768;
	else if (i > 32767)
		i = 32767;
	return (int16_t)i;
}

int write_raw(const char *filename, float *input, size_t frames)
{
	int16_t *buf;
	int rc = -1;
	int n = frames * 4;
	int i;

	buf = (int16_t *)malloc(n);
	for (i = 0; i < frames; i++) {
		buf[2 * i] = f2s16(input[i]);
		buf[2 * i + 1] = f2s16(input[i + frames]);
	}

	int fd = open(filename, O_WRONLY | O_CREAT, 0644);
	if (fd < 0) {
		fprintf(stderr, "cannot open file %s\n", filename);
		goto quit;
	}
	if (write(fd, buf, n) != n) {
		fprintf(stderr, "short write file %s\n", filename);
		goto quit;
	}
	rc = 0;
quit:
	close(fd);
	free(buf);
	return rc;
}