-
Notifications
You must be signed in to change notification settings - Fork 1
/
timelapse_image_sizes.py
55 lines (39 loc) · 1.44 KB
/
timelapse_image_sizes.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
from struct import pack
import wave
import random
# Parameters for wave file
RATE=44100 # Samples per second
CHANNELS=1 # Number of channels
WIDTH=2 # Number of bytes per sample
FRAMES=0 # Starting number of frames
COMPRESSION_TYPE='NONE' # Compression type of wave file
COMPRESSION_NAME='not compressed' # Name of compression type
# 16-bit signed integer values
MAXVOL = 2**15-1.0
MINVOL = -2**15
def get_sizes():
sizes = []
with open('size.txt') as size_file:
for size in size_file:
sizes.append(int(size))
return sizes
def sizes_to_percentages(sizes):
sizes = [size for size in sizes if size > 0] # No empties
min_size = min(sizes)
max_size = max(sizes)
return [(float(size) - min_size)/(max_size - min_size) for size in sizes]
def percentages_to_volumes(size_percs):
return [(size_perc - 0.5) * MAXVOL * 2.0 for size_perc in size_percs]
def main():
# Create a new wave file
wv = wave.open('output/timelapse_image_sizes.wav', 'w')
wv.setparams((CHANNELS, WIDTH, RATE, FRAMES, COMPRESSION_TYPE, COMPRESSION_NAME))
sizes = get_sizes()
samples = percentages_to_volumes(sizes_to_percentages(sizes))
wvData = ""
for sample in samples:
wvData += pack('h', int(round(sample)))
wv.writeframes(wvData)
wv.close()
if __name__ == '__main__':
main()