-
Notifications
You must be signed in to change notification settings - Fork 1
/
toonzscript_music.py
63 lines (47 loc) · 1.42 KB
/
toonzscript_music.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
# -*- coding: utf-8 -*-
import argparse
import scipy.io.wavfile
import pylab
import math
# FREQ = 44100
FREQ = 48000
parser = argparse.ArgumentParser()
parser.add_argument("music_file")
args = parser.parse_args()
"""
@see http://bastian.rieck.ru/blog/posts/2014/simple_experiments_speech_detection/
"""
def shortTermEnergy(frame):
return sum( [ abs(x)**2 for x in frame ] ) / len(frame)
def calc_energy(sound, framelen=10000, skip=10000):
frame = [0 for i in range(framelen)]
energy = []
for i, val in enumerate(sound):
frame.append(val)
frame.pop(0)
if(i % skip == 0):
# energy.append(math.log(max(shortTermEnergy(frame), 0.001)))
energy.append(shortTermEnergy(frame))
return energy
data = scipy.io.wavfile.read( "Test.wav" )[1][:, 0]
energy = calc_energy(data, skip=int(float(FREQ)/30))
max_energy = max(energy)
normalized_energy = [max(0.1, float(e)*3/max_energy) for e in energy]
# pylab.plot(energy)
# pylab.show()
part = """
builder = new ImageBuilder();
builder.add(f_orig, (new Transform).scale({scale}));
f = builder.image;
l.setFrame({i}, f);
"""
sub = ""
for i,en in enumerate(normalized_energy):
sub += part.format(i=i+2, scale=en)
entire = """
l = new Level("/Users/keisuke_ogaki/Documents/opentoonz/music/testras.0001.tif");
f_orig = l.getFrame(1);
{}
l.save("/Users/keisuke_ogaki/Documents/opentoonz/music/testras2.0001.tif");
"""
print(entire.format(sub))