3
- runs only on little endian machines for WAV files
4
- Not all WAV files are recognized
10
#include <sys/types.h>
15
typedef signed short stereo [2];
16
typedef signed short mono;
26
void analyze_stereo ( const stereo* p, size_t len, korr_t* k )
28
long double _x = 0, _x2 = 0, _y = 0, _y2 = 0, _xy = 0;
34
for ( ; len--; p++ ) {
35
_x += (t1 = (*p)[0]); _x2 += t1 * t1;
36
_y += (t2 = (*p)[1]); _y2 += t2 * t2;
47
void analyze_dstereo ( const stereo* p, size_t len, korr_t* k )
51
long double _x = 0, _x2 = 0, _y = 0, _y2 = 0, _xy = 0;
57
for ( ; len--; p++ ) {
58
_x += (t1 = (*p)[0] - l0); _x2 += t1 * t1;
59
_y += (t2 = (*p)[1] - l1); _y2 += t2 * t2;
73
void analyze_mono ( const mono* p, size_t len, korr_t* k )
75
long double _x = 0, _x2 = 0;
80
for ( ; len--; p++ ) {
81
_x += (t1 = (*p)); _x2 += t1 * t1;
91
void analyze_dmono ( const mono* p, size_t len, korr_t* k )
94
long double _x = 0, _x2 = 0;
99
for ( ; len--; p++ ) {
100
_x += (t1 = (*p) - l0); _x2 += t1 * t1;
111
int sgn ( long double x )
113
if ( x > 0 ) return +1;
114
if ( x < 0 ) return -1;
118
int report ( const korr_t* k )
120
long double scale = sqrt ( 1.e5 / (1<<29) ); // Sine Full Scale is +10 dB, 7327 = 100%
129
r = (k->x2*k->n - k->x*k->x) * (k->y2*k->n - k->y*k->y);
130
r = r > 0.l ? (k->xy*k->n - k->x*k->y) / sqrt (r) : 1.l;
131
sx = k->n > 1 ? sqrt ( (k->x2 - k->x*k->x/k->n) / (k->n - 1) ) : 0.l;
132
sy = k->n > 1 ? sqrt ( (k->y2 - k->y*k->y/k->n) / (k->n - 1) ) : 0.l;
133
x = k->n > 0 ? k->x/k->n : 0.l;
134
y = k->n > 0 ? k->y/k->n : 0.l;
136
b = atan2 ( sy, sx * sgn (r) ) * ( 8 / M_PI);
139
// _______________________________
145
// 8 |--------------+--------------| 0
150
// |/_____________|_____________\|
155
printf ("-mm"); // disguised mono file
158
if ( fabs (b-2) > 0.666 ) {
159
printf ("-ms"); // low profit for joint stereo
163
printf ("-ms"); // low profit for joint stereo
168
void readfile ( const char* name, int fd )
170
unsigned short header [22];
177
memset ( &k0, 0, sizeof(k0) );
178
memset ( &k1, 0, sizeof(k1) );
180
read ( fd, header, sizeof(header) );
182
switch ( header[11] ) {
188
while ( ( samples = read (fd, s, sizeof(s)) ) > 0 ) {
189
analyze_stereo ( s, samples / sizeof (*s), &k0 );
190
analyze_dstereo ( s, samples / sizeof (*s), &k1 );
197
fprintf ( stderr, "%u Channels not supported: %s\n", header[11], name );
202
int main ( int argc, char** argv )
208
readfile ( "<stdin>", 0 );
210
while ( (name = *++argv) != NULL ) {
211
if ( (fd = open ( name, O_RDONLY )) >= 0 ) {
212
readfile ( name, fd );
215
fprintf ( stderr, "Can't open: %s\n", name );