-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
108 lines (89 loc) · 3.16 KB
/
util.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
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
99
100
101
102
103
104
105
106
107
108
import tensorflow as tf
import numpy as np
from numpy import pi
def colorize(z):
r = np.abs(z)
arg = np.angle(z)
h = (arg + pi) / (2 * pi) + 0.5
l = 1.0 - 1.0/(1.0 + r**0.3)
s = 0.8
c = np.vectorize(hls_to_rgb) (h,l,s) # --> tuple
c = np.array(c) # --> array of (3,n,m) shape, but need (n,m,3)
c = c.swapaxes(0,2)
return c
def cplot(w, show=False):
import pylab as plt
from colorsys import hls_to_rgb
plt.figure()
img = colorize(np.squeeze(w))
plt.imshow(img)
if show: plt.show()
def diff_plot(wf, show=False):
mags, phase = voc.topolar(voc.encode(wf))
mags = mags*mags
phase = voc.consec_diff(phase)
fs = sess.run(voc.frompolar(mags, phase))
cplot(fs, show)
def read_sound(path, fmt='wav', rate=16000, channels=1):
if fmt != 'wav': raise 'unsupported in tf2'
audio_binary = tf.io.read_file(path)
waveform, sr = tf.audio.decode_wav(audio_binary, desired_channels=channels)
# TODO use this outside graph
#if not tf.math.reduce_all(sr == rate): raise 'sample rate mismatch'
return tf.transpose(a=waveform)
def write_sound(waveform, path, fmt='wav', rate=16000):
if fmt != 'wav': raise 'unsupported in tf2'
output_binary = tf.audio.encode_wav(
tf.transpose(a=waveform),
sample_rate=rate
)
return tf.io.write_file(path, output_binary)
def log_eps(x, eps=0.001):
return tf.math.log(x+eps)-tf.math.log(eps)
def unlog_eps(x, eps=0.001):
return tf.exp(x+tf.math.log(eps))-eps
def quartiles(x):
#not needed anymore, probably works in tf2?
import tensorflow_probability as tfp
qs = [0., 25., 50., 75., 100.]
return tfp.stats.percentile(x, qs)
def write_png(mags, path, scale=13.):
mags = tf.convert_to_tensor(value=mags)
scale = tf.convert_to_tensor(value=scale)
mags = log_eps(mags) / scale
i16 = 2.**16
maximum = tf.reduce_max(input_tensor=mags)
valid = maximum <= 1 - 1/i16
valid = tf.Assert(valid, [maximum])
with tf.control_dependencies([valid]):
words = tf.cast(mags * i16, dtype=tf.uint16)
words = tf.expand_dims(tf.transpose(a=tf.squeeze(words, 0)), -1)
pshape(words)
encoded = tf.image.encode_png(words)
write = tf.io.write_file(path, encoded)
return write
def read_png(path, scale=13.):
scale = tf.convert_to_tensor(value=scale)
encoded = tf.io.read_file(path)
words = tf.image.decode_png(encoded, dtype=tf.uint16)
words = tf.expand_dims(tf.transpose(a=tf.squeeze(words, -1)), 0)
i16 = 2.**16
mags = tf.cast(words, dtype=tf.float32) / i16
mags = unlog_eps(mags * scale)
return mags
def read_flac(iflac):
import soundfile as sf
data, sr = sf.read(iflac)
waveform = np.expand_dims(data.astype('f'), 0)
return waveform
def waveform_to_png(waveform, opng):
import sigproc as sig
ofreqs = sig.encode(waveform)
omags, ophase = sig.topolar(ofreqs)
output = write_png(omags, opng)
return output
def flac_to_png(iflac, opng):
waveform = tf.compat.v1.py_func(read_flac, [iflac], tf.float32)
return waveform_to_png(waveform, opng)
def pshape(t, name='tensor'):
print('{}: {} {}'.format(name, t.dtype, t.get_shape()))