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
|
implement Wav2Iaf;
include "sys.m";
include "draw.m";
include "bufio.m";
sys: Sys;
FD: import sys;
bufio: Bufio;
Iobuf: import bufio;
stderr: ref FD;
inf: ref Iobuf;
prog: string;
buff4: array of byte;
pad := array[] of { " ", " ", "", " " };
Wav2Iaf: module
{
init: fn(ctxt: ref Draw->Context, argv: list of string);
};
ioerror()
{
sys->fprint(stderr, "%s: read error: %r\n", prog);
exit;
}
shortfile(diag: string)
{
sys->fprint(stderr, "%s: short read: %s\n", prog, diag);
exit;
}
error(s: string)
{
sys->fprint(stderr, "%s: bad wave file: %s\n", prog, s);
exit;
}
get(c: int, s: string)
{
n := inf.read(buff4, c);
if (n < 0)
ioerror();
if (n != c)
shortfile("expected " + s);
}
gets(c: int, s: string) : string
{
get(c, s);
return string buff4[0:c];
}
need(s: string)
{
get(4, s);
if (string buff4 != s) {
sys->fprint(stderr, "%s: not a wave file\n", prog);
exit;
}
}
getl(s: string) : int
{
get(4, s);
return int buff4[0] + (int buff4[1] << 8) + (int buff4[2] << 16) + (int buff4[3] << 24);
}
getw(s: string) : int
{
get(2, s);
return int buff4[0] + (int buff4[1] << 8);
}
skip(n: int)
{
while (n > 0) {
inf.getc();
n--;
}
}
bufcp(s, d: ref Iobuf, n: int)
{
while (n > 0) {
b := s.getb();
if (b < 0) {
if (b == Bufio->EOF)
sys->fprint(stderr, "%s: short input file\n", prog);
else
sys->fprint(stderr, "%s: read error: %r\n", prog);
exit;
}
d.putb(byte b);
n--;
}
}
init(nil: ref Draw->Context, argv: list of string)
{
l: int;
a: string;
sys = load Sys Sys->PATH;
stderr = sys->fildes(2);
prog = hd argv;
argv = tl argv;
bufio = load Bufio Bufio->PATH;
if (bufio == nil)
sys->fprint(stderr, "%s: could not load %s: %r\n", prog, Bufio->PATH);
if (argv == nil) {
inf = bufio->fopen(sys->fildes(0), Bufio->OREAD);
if (inf == nil) {
sys->fprint(stderr, "%s: could not fopen stdin: %r\n", prog);
exit;
}
}
else if (tl argv != nil) {
sys->fprint(stderr, "usage: %s [infile]\n", prog);
exit;
}
else {
inf = bufio->open(hd argv, Sys->OREAD);
if (inf == nil) {
sys->fprint(stderr, "%s: could not open %s: %r\n", prog, hd argv);
exit;
}
}
buff4 = array[4] of byte;
need("RIFF");
getl("length");
need("WAVE");
for (;;) {
a = gets(4, "tag");
l = getl("length");
if (a == "fmt ")
break;
skip(l);
}
if (getw("format") != 1)
error("not PCM");
chans := getw("channels");
rate := getl("rate");
getl("AvgBytesPerSec");
getw("BlockAlign");
bits := getw("bits");
l -= 16;
do {
skip(l);
a = gets(4, "tag");
l = getl("length");
}
while (a != "data");
outf := bufio->fopen(sys->fildes(1), Sys->OWRITE);
if (outf == nil) {
sys->fprint(stderr, "%s: could not fopen stdout: %r\n", prog);
exit;
}
s := "rate\t" + string rate + "\n"
+ "chans\t" + string chans + "\n"
+ "bits\t" + string bits + "\n"
+ "enc\tpcm";
outf.puts(s);
outf.puts(pad[len s % 4]);
outf.puts("\n\n");
bufcp(inf, outf, l);
outf.flush();
}
|