File size: 2,311 Bytes
0936134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/python -tt
#
# Copyright (c) 2009, Adam Simpkins
#
"""
Merge several different ripped versions of a track.  Useful when cdparanoia
reports some uncorrectable errors.  If you have ripped the track several times,
this tool can merge them all, by choosing the most popular value from all files
for each byte value.

Usage: merge.py FILE1 FILE2 [...] > merged.wav
"""

import sys

def msg(s):
    print >> sys.stderr, str(s)


def handle_difference(outf, bytes, offset):
    values = {}
    for b in bytes:
        try:
            values[b] += 1
        except KeyError:
            values[b] = 1

    max_count = 0
    max_byte = []
    for (b, count) in values.items():
        if count > max_count:
            max_count = count
            max_byte = [b]
        elif count == max_count:
            max_byte.append(b)

    if len(max_byte) != 1:
        raise Exception('tie at offset %d: %s' % (offset, max_byte))

    msg('%d: %s --> %r' % (offset, values, max_byte[0]))
    outf.write(max_byte[0])


def handle_chunks(outf, chunks, offset):
    # See if there are any differences
    v = chunks[0]
    for c in chunks:
        if c != v:
            break
    else:
        # Great, all identical
        outf.write(v)
        sys.stderr.write('%d\r' % (offset,))
        return

    # Hmm.  We found differences.
    # Try to split the chunks in half, and recurse on each half.
    l = len(v)
    if l == 1:
        # Down to single byte chunks.
        # Handle the difference.
        handle_difference(outf, chunks, offset)
    else:
        h = l / 2
        first_half = [c[:h] for c in chunks]
        second_half = [c[h:] for c in chunks]
        handle_chunks(outf, first_half, offset)
        handle_chunks(outf, second_half, offset + h)


def main(argv):
    filenames = argv[1:]
    msg(filenames)

    outf = sys.stdout

    files = [open(name, 'r') for name in filenames]

    block_size = 2048
    offset = 0
    while True:
        blocks = [f.read(block_size) for f in files]

        # FIXME: detect end of file better.
        # If some end before others, handle the error.
        if not blocks[0]:
            break

        handle_chunks(outf, blocks, offset)
        offset += block_size

    return 0


if __name__ == '__main__':
    rc = main(sys.argv)
    sys.exit(rc)