-
Notifications
You must be signed in to change notification settings - Fork 1
/
processors.py
243 lines (191 loc) · 8.7 KB
/
processors.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# Copyright 2020 The DDSP Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This file has been modified from the original
# Lint as: python3
"""Library of base Processor and ProcessorGroup.
ProcessorGroup exists as an alternative to manually specifying the forward
propagation in python. The advantage is that a variety of configurations can be
programmatically specified via external dependency injection, such as with the
`gin` library.
"""
from typing import Dict, Sequence, Tuple, Text
import core
import synths
import effects
import gin
import tensorflow.compat.v2 as tf
tfkl = tf.keras.layers
# Define Types.
TensorDict = Dict[Text, tf.Tensor]
# Processor Base Class ---------------------------------------------------------
class Processor(tfkl.Layer):
"""Abstract base class for signal processors.
Since most effects / synths require specificly formatted control signals
(such as amplitudes and frequenices), each processor implements a
get_controls(inputs) method, where inputs are a variable number of tensor
arguments that are typically neural network outputs. Check each child class
for the class-specific arguments it expects. This gives a dictionary of
controls that can then be passed to get_signal(controls). The
get_outputs(inputs) method calls both in succession and returns a nested
output dictionary with all controls and signals.
"""
def __init__(self, name: Text, trainable: bool = False):
super().__init__(name=name, trainable=trainable, autocast=False)
def call(self, *args: tf.Tensor, **kwargs: tf.Tensor) -> tf.Tensor:
"""Convert input tensors arguments into a signal tensor."""
# Don't use `training` or `mask` arguments from keras.Layer.
for k in ['training', 'mask']:
if k in kwargs:
_ = kwargs.pop(k)
controls = self.get_controls(*args, **kwargs)
signal = self.get_signal(**controls)
return signal
def get_controls(self, *args: tf.Tensor, **kwargs: tf.Tensor) -> TensorDict:
"""Convert input tensor arguments into a dict of processor controls."""
raise NotImplementedError
def get_signal(self, *args: tf.Tensor, **kwargs: tf.Tensor) -> tf.Tensor:
"""Convert control tensors into a signal tensor."""
raise NotImplementedError
# ProcessorGroup Class ---------------------------------------------------------
# Define Types.
Node = Tuple[Processor, Sequence[Text]]
DAG = Sequence[Node]
@gin.configurable
class ProcessorGroup(tfkl.Layer):
"""String Proccesor() objects together into a processor_group."""
def __init__(self, dag: DAG, name: Text = 'processor_group'):
"""Constructor.
Args:
dag: A directed acyclical graph in the form of an iterable of tuples or
dictionaries. Tuples are intepreted as (processor, [inputs]).
"Processor" should be an instance of a Processor() object. "Inputs" is
an iterable of strings each of which is a nested dict key. For example,
"synth_additive/controls/f0_hz" would correspond to the value
{"synth_additive": {"controls": {"f0_hz": value}}}. The graph is read
sequentially and must be topologically sorted. This means that all
inputs for a processor must already be generated by earlier processors
(or inputs to the processor_group).
name: Name of processor_group.
"""
super(ProcessorGroup, self).__init__(name=name)
# dag = [(synths.Additive(), ['amps', 'harmonic_distribution', 'f0_hz']), (synths.FilteredNoise(), ['noise_magnitudes']), (Add(), ['filtered_noise/signal', 'additive/signal']), (effects.Reverb(), ['add/signal']),]
self.dag = dag
# Collect a list of processors.
self.processors = [node[0] for node in self.dag]
def call(self, dag_inputs: TensorDict) -> tf.Tensor:
"""Like Processor, but specific to having an input dictionary."""
dag_inputs = core.copy_if_tf_function(dag_inputs)
dag_outputs = self.get_controls(dag_inputs)
signal = self.get_signal(dag_outputs)
return signal
def get_controls(self, dag_inputs: TensorDict) -> TensorDict:
"""Run the DAG and get complete outputs dictionary for the processor_group.
Args:
dag_inputs: A dictionary of input tensors fed to the signal processing
processor_group.
Returns:
A nested dictionary of all the output tensors.
"""
# Also build layer on get_controls(), instead of just __call__().
self.built = True
# Initialize the outputs with inputs to the processor_group.
outputs = dag_inputs
# Run through the DAG nodes in sequential order.
for node in self.dag:
# Get the node processor and keys to the node input.
processor, keys = node
# Get the inputs to the node.
inputs = [core.nested_lookup(key, outputs) for key in keys]
# Build the processor only if called the first time in a @tf.function.
# Need to explicitly build because we use get_controls() and get_signal()
# seperately, (to get intermediates) rather than directly using call().
if not processor.built:
processor.build([tensor.shape for tensor in inputs])
# Run processor.
controls = processor.get_controls(*inputs)
signal = processor.get_signal(**controls)
# Add outputs to the dictionary.
outputs[processor.name] = {'controls': controls, 'signal': signal}
# Get output signal from last processor.
output_name = self.processors[-1].name
outputs[self.name] = {'signal': outputs[output_name]['signal']}
return outputs
def get_signal(self, dag_outputs: TensorDict) -> tf.Tensor:
"""Extract the output signal from the dag outputs.
Args:
dag_outputs: A dictionary of tensors output from self.get_controls().
Returns:
Signal tensor.
"""
# Initialize the outputs with inputs to the processor_group.
return dag_outputs[self.name]['signal']
# Routing processors for manipulating signals in a processor_group -------------
@gin.register
class Add(Processor):
"""Sum two signals."""
def __init__(self, name: Text = 'add'):
super(Add, self).__init__(name=name)
def get_controls(self, signal_one: tf.Tensor,
signal_two: tf.Tensor) -> TensorDict:
"""Just pass signals through."""
return {'signal_one': signal_one, 'signal_two': signal_two}
def get_signal(self, signal_one: tf.Tensor,
signal_two: tf.Tensor) -> tf.Tensor:
return signal_one + signal_two
@gin.register
class Mix(Processor):
"""Constant-power crossfade between two signals."""
def __init__(self, name: Text = 'mix'):
super(Mix, self).__init__(name=name)
def get_controls(self, signal_one: tf.Tensor,
signal_two: tf.Tensor,
nn_out_mix_level: tf.Tensor) -> TensorDict:
"""Standardize inputs to same length, mix_level to range [0, 1].
Args:
signal_one: 2-D or 3-D tensor.
signal_two: 2-D or 3-D tensor.
nn_out_mix_level: Tensor of shape [batch, n_time, 1] output of the network
determining relative levels of signal one and two.
Returns:
Dict of control parameters.
Raises:
ValueError: If signal_one and signal_two are not the same length.
"""
n_time_one = int(signal_one.shape[1])
n_time_two = int(signal_two.shape[1])
if n_time_one != n_time_two:
raise ValueError('The two signals must have the same length instead of'
'{} and {}'.format(n_time_one, n_time_two))
mix_level = tf.nn.sigmoid(nn_out_mix_level)
mix_level = core.resample(mix_level, n_time_one)
return {
'signal_one': signal_one,
'signal_two': signal_two,
'mix_level': mix_level
}
def get_signal(self, signal_one: tf.Tensor, signal_two: tf.Tensor,
mix_level: tf.Tensor) -> tf.Tensor:
"""Constant-power cross fade between two signals.
Args:
signal_one: 2-D or 3-D tensor.
signal_two: 2-D or 3-D tensor.
mix_level: Tensor of shape [batch, n_time, 1] determining relative levels
of signal one and two. Must have same number of time steps as the other
signals and be in the range [0, 1].
Returns:
Tensor of mixed output signal.
"""
mix_level_one = tf.sqrt(tf.abs(mix_level))
mix_level_two = 1.0 - tf.sqrt(tf.abs(mix_level - 1.0))
return mix_level_one * signal_one + mix_level_two * signal_two