-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_test_file.py
95 lines (70 loc) · 3.46 KB
/
generate_test_file.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu May 19 17:00:00 2017
@author: buckler
DESCRIPTION:
This script take a folder with one or more keras models (modelBasePath) and a folder (dataset_path) with some spectrogram (.npy).
Then for each model generatethe the output file using all the input file. The results can be found in destination path
(destBasePath), divided in folders named with the id of the model.
"""
import numpy as np
import os
import keras
import dataset_manupulation as dm
import librosa
import utility as u
import autoencoder
frame_context = 2
module_len = 2049
hops = 2048
nfft = 4096
sr = 16000
# set mix reconstruction params
aS = 0
bS = 0.5
aP = 1 - aS
bP = 1 - bS
aM = 0
root_dir = os.getcwd()
dataset_path = os.path.join(root_dir,'dataset','source','stft-2048','Various_audio_tracks','16kHz','app')
modelBasePath = os.path.join(root_dir,'result','model','magenta')
destBasePath = os.path.join(root_dir,'result','reconstructedWav_for_paper','tracks_to_magenta')
for root, dir, modelNames in os.walk(modelBasePath):
for modelName in modelNames:
modelPath = os.path.join(modelBasePath, modelName)
strID = modelName.replace('model_','').replace('.hd5','')
model = autoencoder.autoencoder_fall_detection(strID)
model.define_sequential_arch(params=None, path=modelPath)
destPath = os.path.join(destBasePath, strID)
u.makedir(destPath)
for root, dirs, files in os.walk(dataset_path):
for file in files:
print("loading "+file)
#sourceStftPath = [os.path.join(dataset_path, file)]
source_stft = dm.load_DATASET(dataset_path, [file])
source = dm.reshape_set(source_stft, net_type='dense')
source_sig = source[0].T.view().T
source_sig_module = np.absolute(source_sig)
source_sig_phase = np.angle(source_sig)
cos_source_sig = np.cos(source_sig_phase)
sin_source_sig = np.sin(source_sig_phase)
source_sig_input = np.concatenate([source_sig_module, cos_source_sig, sin_source_sig], axis=1)
#source_sig_input = np.hstack([source_sig_module, source_sig_phase])
source_sig_input, _ = dm.create_context(source_sig_input, look_back=frame_context)
source_sig_module = source_sig_module[: - frame_context - 1, :]
source_sig_phase = source_sig_phase[: - frame_context - 1, :]
prediction = np.asarray(model.reconstruct_spectrogram(source_sig_input), order="C")
prediction_module = prediction[:, 0:module_len]
prediction_cos = prediction[:, module_len:(module_len*2)]
prediction_sin = prediction[:, (module_len*2):(module_len*3)]
prediction_phase = prediction_cos + 1j * prediction_sin
#prediction_phase = prediction[:, module_len:]
Mx = aS * source_sig_module + aP * prediction_module + aM * np.sqrt( source_sig_module * prediction_module)
Phix = bS * source_sig_phase + bP * prediction_phase
prediction_complex = Mx * Phix
S = librosa.core.istft(prediction_complex.T, hop_length=hops, win_length=nfft)
out_filename = "reconstruction_" + file.replace('.npy','.wav')
librosa.output.write_wav(os.path.join(destPath,out_filename), S, sr)
print('recostruction of {0} done'.format(file))
print("END.")