File size: 15,602 Bytes
d1d6816
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
from typing import List

import librosa

from data_generation.encoding import ParameterDescription, Sample
from melody_synth.random_midi import RandomMidi
from melody_synth.melody_generator import MelodyGenerator
from scipy.io.wavfile import write
from pathlib import Path
from tqdm import tqdm
import json
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import matplotlib
from configurations.read_configuration import parameter_range, is_discrete, get_conf_stft_hyperparameter
import shutil

# from model.log_spectrogram import power_to_db
from tools import power_to_db

num_params = 16


def plot_spectrogram(signal: np.ndarray,
                     path: str,
                     frame_length=512,
                     frame_step=256):
    """Computes the spectrogram of the given signal and saves it.

    Parameters
    ----------
    signal: np.ndarray
        The signal for which to compute the spectrogram.
    path: str
        Path to save the the computed spectrogram.
    frame_length:
        Window size of the FFT.
    frame_step:
        Hop size of the FFT.
    """

    # Compute spectrum for each frame. Returns complex tensor.
    # todo: duplicate code in log_spectrogram.py. Move this somewhere else perhaps.
    spectrogram = tf.signal.stft(signal,
                                 frame_length=frame_length,
                                 frame_step=frame_step,
                                 pad_end=False)  # Returns 63 frames instead of 64 otherwise

    # Compute the magnitudes
    magnitude_spectrum = tf.abs(spectrogram)
    log_spectrum = power_to_db(magnitude_spectrum)
    matplotlib.pyplot.imsave(path, np.transpose(log_spectrum), vmin=-100, vmax=0, origin='lower')


def plot_mel_spectrogram(signal: np.ndarray,
                     path: str,
                     frame_length=512,
                     frame_step=256):

    spectrogram = librosa.feature.melspectrogram(signal, sr=16384, n_fft=2048, hop_length=frame_step, win_length=frame_length)
    matplotlib.pyplot.imsave(path, spectrogram, vmin=-100, vmax=0, origin='lower')


# List of ParameterDescription objects that specify the parameters for generation
param_descriptions: List[ParameterDescription]
param_descriptions = [

    # Oscillator levels
    ParameterDescription(name="osc1_amp",
                         values=parameter_range('osc1_amp'),
                         discrete=is_discrete('osc1_amp')),
    ParameterDescription(name="osc2_amp",
                         values=parameter_range('osc2_amp'),
                         discrete=is_discrete('osc2_amp')),

    # ADSR params
    ParameterDescription(name="attack",
                         values=parameter_range('attack'),
                         discrete=is_discrete('attack')),
    ParameterDescription(name="decay",
                         values=parameter_range('decay'),
                         discrete=is_discrete('decay')),
    ParameterDescription(name="sustain",
                         values=parameter_range('sustain'),
                         discrete=is_discrete('sustain')),
    ParameterDescription(name="release",
                         values=parameter_range('release'),
                         discrete=is_discrete('release')),

    ParameterDescription(name="cutoff_freq",
                         values=parameter_range('cutoff_freq'),
                         discrete=is_discrete('cutoff_freq')),

    # Oscillators types
    # 0 for sin saw, 1 for sin square, 2 for saw square
    # 3 for sin triangle, 4 for triangle saw, 5 for triangle square
    ParameterDescription(name="osc_types",
                         values=parameter_range('osc_types'),
                         discrete=is_discrete('osc_types')),
]


def generate_dataset_for_cnn(n: int,
                             path_name="./data/data_cnn_model",
                             sample_rate=16384,
                             n_samples_for_note=16384 * 4,
                             n_samples_for_melody=16384 * 4, write_parameter=True, write_spectrogram=True):
    """
    Generate dataset of size n for 'Inversynth' cnn model

    :param n: Int
    :param path_name: String--path to save the dataset
    :param sample_rate: Int
    :param n_samples_for_note:  Int
    :param n_samples_for_melody:  Int
    :param write_parameter: Boolean--if write parameter values in a .txt file
    :param write_spectrogram: Boolean--write spectrogram with parameter values in the file name
    :return:
    """

    shutil.rmtree(path_name)
    Path(path_name).mkdir(parents=True, exist_ok=True)
    print("Generating dataset...")

    synth = MelodyGenerator(sample_rate,
                            n_samples_for_note, n_samples_for_melody)
    randomMidi = RandomMidi()

    for i in tqdm(range(n)):
        parameter_values = [param.generate() for param in param_descriptions]

        # Dict of parameter values, what our synthesizer expects as input
        parameter_values_raw = {param.name: param.value for param in parameter_values}

        strategy = {"rhythm_strategy": "free_rhythm",
                    "pitch_strategy": "free_pitch",
                    "duration_strategy": "random_duration",
                    }
        midi_encode, midi = randomMidi(strategy)
        signal = synth.get_melody(parameter_values_raw, midi=midi).numpy()

        # Path to store each sample with its label
        path = path_name + f"/{i}"
        Path(path).mkdir(parents=True, exist_ok=True)

        if write_parameter:
            suffix = 'spectrogram'
            for parameter_value in parameter_values:
                suffix += f'_{parameter_value.name}={"%.3f" % parameter_value.value}'
            if write_spectrogram:
                plot_spectrogram(signal, path=path + f"/{suffix}.png", frame_length=1024, frame_step=256)
            else:
                with open(path + f"/{suffix}.txt", "w") as f:
                    f.write("test")
                    f.close()

        write(path + f"/{i}.wav", synth.sample_rate, signal)

        sample = Sample(parameter_values)

        # Dump label as json
        with open(path + "/label.json", "w") as label_file:
            label = sample.get_values()
            label['midi'] = midi
            # print(len(label["encoding"]))
            json.dump(label, label_file, ensure_ascii=True)

    print('Data generation done!')


def generate_dataset_for_triplet(n: int,
                                 path_name="./data/data_triplet_val_10_500",
                                 sample_rate=16384,
                                 n_samples_for_note=16384 * 4,
                                 n_samples_for_melody=16384 * 4,
                                 n_labels=30,
                                 write_spectrogram=True):
    """
    Generate dataset of size n for triplet model

    :param write_spectrogram: Boolean--if write spectrogram
    :param n: Int :param path_name: String--path to save the dataset :param sample_rate: Int :param
    n_samples_for_note:  Int :param n_samples_for_melody:  Int :param n_labels: Int--number of synthesizer parameter
    combinations contained in the dataset (a hyper parameter of triplet model)
    """

    shutil.rmtree(path_name)
    Path(path_name).mkdir(parents=True, exist_ok=True)
    print("Generating dataset...")
    synth = MelodyGenerator(sample_rate,
                            n_samples_for_note, n_samples_for_melody)
    randomMidi = RandomMidi()

    parameter_values_examples = [[param.generate() for param in param_descriptions] for i in range(n_labels)]
    parameter_values_raw_examples = [{param.name: param.value for param in parameter_values} for parameter_values in
                                     parameter_values_examples]

    np.random.seed()
    for i in tqdm(range(n)):
        label_index = np.random.randint(0, n_labels)
        parameter_values = parameter_values_examples[label_index]
        parameter_values_raw = parameter_values_raw_examples[label_index]

        strategy = {"rhythm_strategy": "free_rhythm",
                    "pitch_strategy": "free_pitch",
                    "duration_strategy": "random_duration",
                    }
        midi_encode, midi = randomMidi(strategy)
        signal = synth.get_melody(parameter_values_raw, midi=midi).numpy()

        # Path to store each sample with its label
        path = path_name + f"/{i}"
        Path(path).mkdir(parents=True, exist_ok=True)

        write(path + f"/{i}.wav", synth.sample_rate, signal)
        suffix = 'spectrogram'
        for parameter_value in parameter_values:
            suffix += f'_{parameter_value.name}={"%.3f" % parameter_value.value}'

        if write_spectrogram:
            hp = get_conf_stft_hyperparameter()
            frame_l = hp['frame_length']
            frame_s = hp['frame_length']
            plot_spectrogram(signal, path=path + f"/{suffix}.png", frame_length=frame_l, frame_step=frame_s)
        else:
            with open(path + f"/{suffix}.txt", "w") as f:
                f.write("test")
                f.close()

        with open(path + "/label_index.json", "w") as label_index_file:
            index_json = {'index': label_index}
            json.dump(index_json, label_index_file, ensure_ascii=False)

        # save midi as .txt file
        with open(path + "/midi.txt", "w") as midi_file:
            midi_file.write(str(midi))
            midi_file.close()

    print('Data generation done!')


def manhattan_distance(SP1, SP2):
    """
    :param SP1: first input synthesizer parameter combination
    :param SP2: second input synthesizer parameter combination
    :return: Float--manhattan distance between SP1 and SP2
    """

    md = []
    for key in SP1:
        parameter_name = key
        value1 = SP1[parameter_name]
        value2 = SP2[parameter_name]
        bins = parameter_range(parameter_name)
        bin_index1 = np.argmin(np.abs(np.array(bins) - value1))
        bin_index2 = np.argmin(np.abs(np.array(bins) - value2))

        if parameter_name == "osc_types":
            if bin_index1 == bin_index2:
                d = 0
            else:
                d = 1
        else:
            d = np.abs(bin_index1 - bin_index2) / (len(bins) - 1)
        md.append(d)

    return np.average(md)


def generate_dataset_for_mixed_input_model(n: int,
                                           path_name="./data/data_mixed_input",
                                           sample_rate=16384,
                                           n_samples_for_note=16384 * 4,
                                           n_samples_for_melody=16384 * 4
                                           ):
    """
    Generate dataset of size n for mixed_input_model model

    :param n: Int
    :param path_name: String--path to save the dataset
    :param sample_rate: Int
    :param n_samples_for_note:  Int
    :param n_samples_for_melody:  Int
    :return:
    """

    shutil.rmtree(path_name)
    Path(path_name).mkdir(parents=True, exist_ok=True)
    print("Generating dataset...")
    synth = MelodyGenerator(sample_rate,
                            n_samples_for_note, n_samples_for_melody)
    randomMidi = RandomMidi()

    strategy = {"rhythm_strategy": "free_rhythm",
                "pitch_strategy": "free_pitch",
                "duration_strategy": "random_duration",
                }
    strategy0 = {"rhythm_strategy": "single_note_rhythm",
                 "pitch_strategy": "fixed_pitch",
                 "duration_strategy": "fixed_duration",
                 }
    strategy1 = {"rhythm_strategy": "single_note_rhythm",
                 "pitch_strategy": "fixed_pitch1",
                 "duration_strategy": "fixed_duration",
                 }
    strategy2 = {"rhythm_strategy": "single_note_rhythm",
                 "pitch_strategy": "fixed_pitch2",
                 "duration_strategy": "fixed_duration",
                 }
    strategy3 = {"rhythm_strategy": "single_note_rhythm",
                 "pitch_strategy": "fixed_pitch3",
                 "duration_strategy": "fixed_duration",
                 }
    strategy4 = {"rhythm_strategy": "single_note_rhythm",
                 "pitch_strategy": "fixed_pitch4",
                 "duration_strategy": "fixed_duration",
                 }

    np.random.seed()
    for i in tqdm(range(n)):
        path = path_name + f"/{i}"
        Path(path).mkdir(parents=True, exist_ok=True)
        parameter_values = [param.generate() for param in param_descriptions]
        parameter_values_raw = {param.name: param.value for param in parameter_values}

        # generate query music
        midi_encode, midi = randomMidi(strategy)
        signal_query = synth.get_melody(parameter_values_raw, midi=midi).numpy()
        write(path + f"/{i}.wav", synth.sample_rate, signal_query)
        # plot_spectrogram(signal, path=path + f"/{i}_input.png", frame_length=512, frame_step=256)

        if np.random.rand() < 0.01:  # 50% positive
            with open(path + "/label.json", "w") as label_file:
                sample = Sample(parameter_values)
                label = sample.get_values()
                label['manhattan_distance'] = 0.
                json.dump(label, label_file, ensure_ascii=False)
        else:
            with open(path + "/label.json", "w") as label_file:
                query_sp = parameter_values_raw
                parameter_values = [param.generate() for param in param_descriptions]
                parameter_values_raw = {param.name: param.value for param in parameter_values}
                sample = Sample(parameter_values)
                label = sample.get_values()
                md = manhattan_distance(query_sp, parameter_values_raw)
                label['manhattan_distance'] = md
                json.dump(label, label_file, ensure_ascii=False)

        # generate query music
        midi_encode, midi = randomMidi(strategy0)
        signal_single_note = synth.get_melody(parameter_values_raw, midi=midi).numpy()
        write(path + f"/{i}_0.wav", synth.sample_rate, signal_single_note)
        # plot_spectrogram(signal, path=path + f"/{i}_input.png", frame_length=512, frame_step=256)

        # generate query music
        midi_encode, midi = randomMidi(strategy1)
        signal_single_note = synth.get_melody(parameter_values_raw, midi=midi).numpy()
        write(path + f"/{i}_1.wav", synth.sample_rate, signal_single_note)
        # plot_spectrogram(signal, path=path + f"/{i}_input.png", frame_length=512, frame_step=256)

        # generate query music
        midi_encode, midi = randomMidi(strategy2)
        signal_single_note = synth.get_melody(parameter_values_raw, midi=midi).numpy()
        write(path + f"/{i}_2.wav", synth.sample_rate, signal_single_note)
        # plot_spectrogram(signal, path=path + f"/{i}_input.png", frame_length=512, frame_step=256)

        # generate query music
        midi_encode, midi = randomMidi(strategy3)
        signal_single_note = synth.get_melody(parameter_values_raw, midi=midi).numpy()
        write(path + f"/{i}_3.wav", synth.sample_rate, signal_single_note)
        # plot_spectrogram(signal, path=path + f"/{i}_input.png", frame_length=512, frame_step=256)

        # generate query music
        midi_encode, midi = randomMidi(strategy4)
        signal_single_note = synth.get_melody(parameter_values_raw, midi=midi).numpy()
        write(path + f"/{i}_4.wav", synth.sample_rate, signal_single_note)
        # plot_spectrogram(signal, path=path + f"/{i}_input.png", frame_length=512, frame_step=256)

    print('Data generation done!')