-
Notifications
You must be signed in to change notification settings - Fork 0
/
ops.py
67 lines (55 loc) · 2.58 KB
/
ops.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
# Copyright Igor Babuschkin 2016
# MIT License
from __future__ import division
import tensorflow as tf
def time_to_batch(value, dilation, name=None):
with tf.name_scope('time_to_batch'):
shape = tf.shape(value)
pad_elements = dilation - 1 - (shape[1] + dilation - 1) % dilation
padded = tf.pad(value, [[0, 0], [0, pad_elements], [0, 0]])
reshaped = tf.reshape(padded, [-1, dilation, shape[2]])
transposed = tf.transpose(reshaped, perm=[1, 0, 2])
return tf.reshape(transposed, [shape[0] * dilation, -1, shape[2]])
def batch_to_time(value, dilation, name=None):
with tf.name_scope('batch_to_time'):
shape = tf.shape(value)
prepared = tf.reshape(value, [dilation, -1, shape[2]])
transposed = tf.transpose(prepared, perm=[1, 0, 2])
return tf.reshape(transposed,
[tf.div(shape[0], dilation), -1, shape[2]])
def causal_conv(value, filter_, dilation, name='causal_conv'):
with tf.name_scope(name):
# Pad beforehand to preserve causality.
filter_width = tf.shape(filter_)[0]
padding = [[0, 0], [(filter_width - 1) * dilation, 0], [0, 0]]
padded = tf.pad(value, padding)
if dilation > 1:
transformed = time_to_batch(padded, dilation)
conv = tf.nn.conv1d(transformed, filter_, stride=1, padding='SAME')
restored = batch_to_time(conv, dilation)
else:
restored = tf.nn.conv1d(padded, filter_, stride=1, padding='SAME')
# Remove excess elements at the end.
result = tf.slice(restored,
[0, 0, 0],
[-1, tf.shape(value)[1], -1])
return result
def mu_law_encode(audio, quantization_channels):
'''Quantizes waveform amplitudes.'''
with tf.name_scope('encode'):
mu = quantization_channels - 1
# Perform mu-law companding transformation (ITU-T, 1988).
magnitude = tf.log(1 + mu * tf.abs(audio)) / tf.log(1. + mu)
signal = tf.sign(audio) * magnitude
# Quantize signal to the specified number of levels.
return tf.cast((signal + 1) / 2 * mu + 0.5, tf.int32)
def mu_law_decode(output, quantization_channels):
'''Recovers waveform from quantized values.'''
with tf.name_scope('decode'):
mu = quantization_channels - 1
# Map values back to [-1, 1].
casted = tf.cast(output, tf.float32)
signal = 2 * (casted / mu) - 1
# Perform inverse of mu-law transformation.
magnitude = (1 / mu) * ((1 + mu)**abs(signal) - 1)
return tf.sign(signal) * magnitude