summaryrefslogtreecommitdiff
path: root/appl/cmd/raw2iaf.b
blob: 11c29e00fe5087a50b0bf9fadec7732bd3c87fdd (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
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
implement Raw2Iaf;

include "sys.m";
include "draw.m";

sys:	Sys;
FD:	import sys;
stderr:	ref FD;

rateK:	con "rate";
rateV:	string = "44100";
chanK:	con "chans";
chanV:	string = "2";
bitsK:	con "bits";
bitsV:	string = "16";
encK:	con "enc";
encV:	string = "pcm";

progV:	string;
inV:	string = nil;
outV:	string = nil;
inf:	ref FD;
outf:	ref FD;

pad	:= array[] of { "  ", " ", "", "   " };

Raw2Iaf: module
{
	init:	fn(ctxt: ref Draw->Context, argv: list of string);
};

usage()
{
	sys->fprint(stderr, "usage: %s -8124 -ms -bw -aup -o out in\n", progV);
	exit;
}

options(s: string)
{
	for (i := 0; i < len s; i++) {
		case s[i] {
		'8' =>	rateV = "8000";
		'1' =>	rateV = "11025";
		'2' =>	rateV = "22050";
		'4' =>	rateV = "44100";
		'm' =>	chanV = "1";
		's' =>	chanV = "2";
		'b' =>	bitsV = "8";
		'w' =>	bitsV = "16";
		'a' =>	encV = "alaw";
		'u' =>	encV = "ulaw";
		'p' =>	encV = "pcm";
		* =>	usage();
		}
	}
}

init(nil: ref Draw->Context, argv: list of string)
{
	sys = load Sys Sys->PATH;
	stderr = sys->fildes(2);
	progV = hd argv;
	v := tl argv;

	while (v != nil) {
		a := hd v;
		v = tl v;
		if (len a == 0)
			continue;
		if (a[0] == '-') {
			if (len a == 1) {
				if (inV == nil)
					inV = "-";
				else
					usage();
			}
			else if (a[1] == 'o') {
				if (outV != nil)
					usage();
				if (len a > 2)
					outV = a[2:len a];
				else if (v == nil)
					usage();
				else {
					outV = hd v;
					v = tl v;
				}
			}
			else
				options(a[1:len a]);
		}
		else if (inV == nil)
			inV = a;
		else
			usage();
	}
	if (inV == nil || inV == "-")
		inf = sys->fildes(0);
	else {
		inf = sys->open(inV, Sys->OREAD);
		if (inf == nil) {
			sys->fprint(stderr, "%s: could not open %s: %r\n", progV, inV);
			exit;
		}
	}
	if (outV == nil || outV == "-")
		outf = sys->fildes(1);
	else {
		outf = sys->create(outV, Sys->OWRITE, 8r666);
		if (outf == nil) {
			sys->fprint(stderr, "%s: could not create %s: %r\n", progV, outV);
			exit;
		}
	}
	s := rateK + "\t" + rateV + "\n"
		+  chanK + "\t" + chanV + "\n"
		+  bitsK + "\t" + bitsV + "\n"
		+  encK + "\t" + encV;
	sys->fprint(outf, "%s%s\n\n", s, pad[len s % 4]);
	if (sys->stream(inf, outf, Sys->ATOMICIO) < 0)
		sys->fprint(stderr, "%s: data copy error: %r\n", progV);
}