Emulate VGA HRetrace
[jpcrr.git] / streamtools / testpcmtodump.lua
blob3b4ee3b04933d1788fd2c8d6909b5200d99f16d4
1 infile, err = io.open(arg[1], "r");
2 if not infile then
3 error("Can't open input file: " .. err);
4 end
6 outfile, err = io.open(arg[2], "w");
7 if not outfile then
8 error("Can't open output file: " .. err);
9 end
11 is_high = false;
12 last_trans_time = 0;
13 samplenum = 0;
15 emit_transition = function(time, tohigh)
16 x = time - last_trans_time;
17 s = "";
18 while x >= 4294967295 do
19 print("Skipping time ahead");
20 s = s .. string.char(255, 255, 255, 255);
21 x = x - 4294967295;
22 last_trans_time = last_trans_time + 4294967295;
23 end
25 x4 = x % 256;
26 x = (x - x4) / 256;
27 x3 = x % 256;
28 x = (x - x3) / 256;
29 x2 = x % 256;
30 x = (x - x2) / 256;
31 x1 = x % 256;
34 if is_high ~= tohigh then
35 s = s .. string.char(x1, x2, x3, x4);
36 if is_high then
37 s = s .. string.char(255, 255, 255, 255);
38 else
39 s = s .. string.char(0, 0, 0, 0);
40 end
41 if tohigh then
42 s = s .. string.char(0, 0, 0, 0, 255, 255, 255, 255);
43 else
44 s = s .. string.char(0, 0, 0, 0, 0, 0, 0, 0);
45 end
46 last_trans_time = time;
47 end
48 outfile:write(s);
49 is_high = tohigh;
50 end
52 emit_transition(0, false);
54 while true do
55 inb = infile:read(1);
56 if not inb then
57 break;
58 end
59 scaled = math.floor(50 * string.byte(inb) / 255 + 0.5);
60 if scaled == 0 and is_high then
61 emit_transition(50000 * samplenum, false);
62 elseif scaled == 0 then
63 -- Do nothing.
64 x = 6;
65 else
66 if not is_high then
67 emit_transition(50000 * samplenum, true);
68 end
69 if scaled < 50 then
70 emit_transition(50000 * samplenum + 1000 * scaled, false);
71 end
72 end
74 samplenum = samplenum + 1;
75 end