|
9 | 9 |
|
10 | 10 | import sys,struct,argparse
|
11 | 11 |
|
| 12 | +def round_up(x, y): |
| 13 | + return (x + y - 1) & ~(y - 1) |
| 14 | + |
12 | 15 | def main(argv):
|
13 |
| - parser = argparse.ArgumentParser( |
14 |
| - formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
15 |
| - parser.add_argument("--lead-in", type=float, default=0.5, |
16 |
| - help="lead-in, seconds") |
17 |
| - parser.add_argument("--window", type=float, default=5.5, |
18 |
| - help="data window, seconds") |
19 |
| - parser.add_argument("--total", type=float, default=8.0, |
20 |
| - help="total length, seconds") |
21 |
| - parser.add_argument("outfile", help="output filename") |
22 |
| - args = parser.parse_args(argv[1:]) |
23 |
| - |
24 |
| - bit_ms = 0.004916 |
25 |
| - total_bytes = int(args.total * 1000.0 / bit_ms / 8) |
26 |
| - window_bytes = int(args.window * 1000.0 / bit_ms / 8) |
27 |
| - init_bytes = int(args.lead_in * 1000.0 / bit_ms / 8) |
28 |
| - |
29 |
| - assert (2*init_bytes + window_bytes) < total_bytes, "Window too large" |
30 |
| - print("Lead-In: %.2f sec -> %u bytes" % (args.lead_in, init_bytes)) |
31 |
| - print("Window: %.2f sec -> %u bytes" % (args.window, window_bytes)) |
32 |
| - print("TOTAL: %.2f sec -> %u bytes" % (args.total, total_bytes)) |
33 |
| - |
34 |
| - # Header |
35 |
| - out_f = open(args.outfile, "wb") |
36 |
| - out_f.write(struct.pack("<3x2s3x", b"QD")) |
37 |
| - out_f.write(bytearray(b'\x00'*(512-8))) |
38 |
| - |
39 |
| - # Track |
40 |
| - out_f.write(struct.pack("<4I", 1024, total_bytes, init_bytes, |
41 |
| - init_bytes + window_bytes)) |
42 |
| - out_f.write(bytearray(b'\x00'*(512-16))) |
43 |
| - |
44 |
| - # Data |
45 |
| - blocks = (total_bytes + 511) // 512 |
46 |
| - out_f.write(bytearray(b'\x11'*(blocks*512))) |
| 16 | + parser = argparse.ArgumentParser( |
| 17 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 18 | + parser.add_argument("--lead-in", type=float, default=0.5, |
| 19 | + help="lead-in, seconds") |
| 20 | + parser.add_argument("--window", type=float, default=5.5, |
| 21 | + help="data window, seconds") |
| 22 | + parser.add_argument("--total", type=float, default=8.0, |
| 23 | + help="total length, seconds") |
| 24 | + parser.add_argument("--round", action="store_true", |
| 25 | + help="round values up to 512-byte block size") |
| 26 | + parser.add_argument("outfile", help="output filename") |
| 27 | + args = parser.parse_args(argv[1:]) |
| 28 | + |
| 29 | + lead_in, window, total = args.lead_in, args.window, args.total |
| 30 | + |
| 31 | + assert lead_in >= 0.1, "Insufficient lead-in" |
| 32 | + assert total - window - lead_in >= 0.1, "Insufficient lead-out" |
| 33 | + |
| 34 | + bit_ms = 0.004916 |
| 35 | + total_bytes = int(total * 1000.0 / bit_ms / 8) |
| 36 | + window_bytes = int(window * 1000.0 / bit_ms / 8) |
| 37 | + init_bytes = int(lead_in * 1000.0 / bit_ms / 8) |
| 38 | + |
| 39 | + if args.round: |
| 40 | + total_bytes = round_up(total_bytes, 512) |
| 41 | + window_bytes = round_up(window_bytes, 512) |
| 42 | + init_bytes = round_up(init_bytes, 512) |
| 43 | + |
| 44 | + print("Lead-In: %.2f sec -> %u bytes" % (lead_in, init_bytes)) |
| 45 | + print("Window: %.2f sec -> %u bytes" % (window, window_bytes)) |
| 46 | + print("TOTAL: %.2f sec -> %u bytes" % (total, total_bytes)) |
| 47 | + |
| 48 | + # Header |
| 49 | + out_f = open(args.outfile, "wb") |
| 50 | + out_f.write(struct.pack("<3x2s3x", b"QD")) |
| 51 | + out_f.write(bytearray(b'\x00'*(512-8))) |
| 52 | + |
| 53 | + # Track |
| 54 | + out_f.write(struct.pack("<4I", 1024, total_bytes, init_bytes, |
| 55 | + init_bytes + window_bytes)) |
| 56 | + out_f.write(bytearray(b'\x00'*(512-16))) |
| 57 | + |
| 58 | + # Data |
| 59 | + blocks = (total_bytes + 511) // 512 |
| 60 | + out_f.write(bytearray(b'\x11'*(blocks*512))) |
47 | 61 |
|
48 | 62 | if __name__ == "__main__":
|
49 | 63 | main(sys.argv)
|
| 64 | + |
| 65 | +# Local variables: |
| 66 | +# python-indent: 4 |
| 67 | +# End: |
0 commit comments