-
Notifications
You must be signed in to change notification settings - Fork 10
/
serato_overview.py
executable file
·72 lines (55 loc) · 1.73 KB
/
serato_overview.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import struct
import io
import sys
import mutagen
from PIL import Image
from PIL import ImageColor
FMT_VERSION = 'BB'
def parse(fp):
version = struct.unpack(FMT_VERSION, fp.read(2))
assert version == (0x01, 0x05)
for x in iter(lambda: fp.read(16), b''):
assert len(x) == 16
yield bytearray(x)
def draw_waveform(data):
img = Image.new('RGB', (240, 16), "black")
pixels = img.load()
for i in range(img.size[0]):
rowdata = next(data)
factor = (len([x for x in rowdata if x < 0x80]) / len(rowdata))
for j, value in enumerate(rowdata):
# The algorithm to derive the colors from the data has no real
# mathematical background and was found by experimenting with
# different values.
color = 'hsl({hue:.2f}, {saturation:d}%, {luminance:.2f}%)'.format(
hue=(factor * 1.5 * 360) % 360,
saturation=40,
luminance=(value / 0xFF) * 100,
)
pixels[i, j] = ImageColor.getrgb(color)
return img
def main(argv=None):
parser = argparse.ArgumentParser()
parser.add_argument('file', metavar='FILE')
args = parser.parse_args(argv)
tagfile = mutagen.File(args.file)
if tagfile is not None:
try:
tag = tagfile['GEOB:Serato Overview']
except KeyError:
print('File is missing "GEOB:Serato Overview" tag')
return 1
else:
fp = io.BytesIO(tag.data)
else:
fp = open(args.file, mode='rb')
with fp:
data = parse(fp)
img = draw_waveform(data)
img.show()
return 0
if __name__ == '__main__':
sys.exit(main())