-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsampleconv.c
177 lines (145 loc) · 3.11 KB
/
sampleconv.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
// converts samples between text and binary formats
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
typedef uint16_t u16;
typedef int16_t i16;
typedef unsigned char uchar;
void error (const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
fprintf (stderr, "sampleconv: ");
vfprintf (stderr, fmt, ap);
fputc ('\n', stderr);
va_end (ap);
exit (1);
}
// READ FUNCTIONS
typedef bool(*read_t)(FILE *, i16 *);
bool read_float (FILE *file, i16 *x)
{
char line[32], *endp;
double d;
long i;
if (fgets (line, sizeof (line), file) == NULL)
return false;
d = strtod (line, &endp);
if (*endp != '\0' && *endp != '\n')
error ("invalid float: %s", line);
if (d < -1.0 || d > 1.0)
error ("out of range: %s", line);
i = (long)(d * 32767);
if (i < -32768 || i > 32767)
error ("out of range: %s", line);
*x = (i16)i;
return true;
}
bool read_int (FILE *file, i16 *x)
{
char line[32], *endp;
long i;
if (fgets (line, sizeof (line), file) == NULL)
return false;
i = strtol (line, &endp, 10);
if (*endp != '\0' && *endp != '\n')
error ("invalid int: %s", line);
if (i < -32768 || i > 32767)
error ("out of range: %s", line);
*x = (i16)i;
return true;
}
bool read_bin (FILE *file, i16 *x)
{
uchar b[2];
if (fread (b, 2, 1, file) != 1)
return false;
*x = (u16)b[0] | ((u16)b[1] << 8);
return true;
}
// WRiTE FUNCTIONS
typedef void(*write_t)(FILE *, i16);
void write_float (FILE *file, i16 x)
{
fprintf (file, "%f\n", (float)x / 32767);
}
void write_int (FILE *file, i16 x)
{
fprintf (file, "%d\n", (int)x);
}
void write_bin (FILE *file, i16 x)
{
uchar b[2];
b[0] = x & 0xff;
b[1] = x >> 8;
fwrite (b, 2, 1, file);
}
int usage (void)
{
fputs ("usage: sampleconv [-i filetype] [-o filetype] [input [output]]\n", stderr);
return 1;
}
FILE *openfile (const char *path, bool out)
{
FILE *file;
if (strcmp (path, "-") == 0) {
return out ? stdout : stdin;
} else {
file = fopen (path, out ? "w" : "r");
if (file == NULL)
error ("fopen('%s')", path);
return file;
}
}
int main (int argc, char *argv[])
{
read_t rfunc = read_float;
write_t wfunc = write_bin;
FILE *in = stdin, *out = stdout;
int option;
i16 x;
while ((option = getopt (argc, argv, "i:o:")) != -1) {
switch (option) {
case 'i':
if (strcmp (optarg, "float") == 0) {
rfunc = read_float;
} else if (strcmp (optarg, "int") == 0) {
rfunc = read_int;
} else if (strcmp (optarg, "bin") == 0) {
rfunc = read_bin;
} else {
error ("invalid input type: %s", optarg);
}
break;
case 'o':
if (strcmp (optarg, "float") == 0) {
wfunc = write_float;
} else if (strcmp (optarg, "int") == 0) {
wfunc = write_int;
} else if (strcmp (optarg, "bin") == 0) {
wfunc = write_bin;
} else {
error ("invalid input type: %s", optarg);
}
break;
default:
return usage ();
}
}
argc -= optind;
argv += optind;
if (argc >= 1) {
in = openfile (argv[0], false);
if (argc >= 2)
out = openfile (argv[1], true);
}
while (rfunc (in, &x))
wfunc (out, x);
fclose (in);
fclose (out);
return 0;
}