-
Notifications
You must be signed in to change notification settings - Fork 3
/
unfmt.c
165 lines (133 loc) · 2.8 KB
/
unfmt.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "synth.h"
/* unfmt: convert integers on stdin to internal float format */
static int monosrc; /* convert from mono? */
static void conv_u8(void);
static void conv_s8(void);
static void conv_16(void);
static void conv_24(void);
static void conv_32(void);
#define PERCHANNEL(code) (code); if (monosrc) { (code); }
int main(int argc, char *argv[])
{
int fmt = 2;
int i;
/* read options */
for (i = 1; i < argc; i++)
{
if (!strcmp(argv[i], "-u8")) fmt = 0; /* 8-bit unsigned */
else if (!strcmp(argv[i], "-s8")) fmt = 1; /* 8-bit signed */
else if (!strcmp(argv[i], "-16")) fmt = 2; /* 16-bit signed */
else if (!strcmp(argv[i], "-24")) fmt = 3; /* 24-bit signed */
else if (!strcmp(argv[i], "-32")) fmt = 4; /* 32-bit signed */
else if (!strcmp(argv[i], "-mono")) monosrc = 1;
else if (!strcmp(argv[i], "-help"))
{
fprintf(stderr, "options: -u8, -s8, -16, -24, -32, "
"-mono\n");
exit(0);
}
}
SET_BINARY_MODE
if (fmt == 0)
conv_u8();
else if (fmt == 1)
conv_s8();
else if (fmt == 2)
conv_16();
else if (fmt == 3)
conv_24();
else if (fmt == 4)
conv_32();
return 0;
}
static void conv_u8(void)
{
float f;
unsigned char s;
int c;
while ((c = getchar()) != EOF)
{
s = (unsigned char)c;
f = s;
/* Move range from [0, 255] to [-128, 127]. */
f -= 128.0f;
/* Squish from [-128, 127] to [-1, 1). */
f /= 128.0f;
PERCHANNEL(fwrite(&f, sizeof f, 1, stdout))
}
}
static void conv_s8(void)
{
float f;
signed char s;
int c;
while ((c = getchar()) != EOF)
{
s = (signed char)c;
f = s;
/* Squish from [-128, 127] to [-1, 1). */
f /= 128.0f;
PERCHANNEL(fwrite(&f, sizeof f, 1, stdout))
}
}
static void conv_16(void)
{
float f;
short s;
while (fread(&s, sizeof s, 1, stdin) == 1)
{
f = s;
/* Squish from [-32768, 32767] to [-1, 1). */
f /= 32768.0f;
PERCHANNEL(fwrite(&f, sizeof f, 1, stdout))
}
}
static void conv_24(void)
{
float f;
double d;
int s;
unsigned char *sr;
int c;
sr = (unsigned char *)&s;
for (;;)
{
#if !WORDS_BIGENDIAN
if ((c = getchar()) == EOF) return;
sr[0] = c;
#endif
if ((c = getchar()) == EOF) return;
sr[1] = c;
if ((c = getchar()) == EOF) return;
sr[2] = c;
#if WORDS_BIGENDIAN
if ((c = getchar()) == EOF) return;
sr[3] = c;
sr[0] = (sr[1] & 0x80) ? 0xff : 0;
#else
sr[3] = (sr[2] & 0x80) ? 0xff : 0;
#endif
d = (double)s;
/* Squish from [-8388608, 8388607] to [-1, 1). */
d /= 8388608.0f;
f = (float)d;
PERCHANNEL(fwrite(&f, sizeof f, 1, stdout))
}
}
static void conv_32(void)
{
float f;
double d;
int s;
while (fread(&s, sizeof s, 1, stdin) == 1)
{
d = (double)s;
/* Squish from [-2147483648, 2147483647] to [-1, 1). */
d /= 2147483648.0f;
f = (float)d;
PERCHANNEL(fwrite(&f, sizeof f, 1, stdout))
}
}