-
Notifications
You must be signed in to change notification settings - Fork 39
/
utils.py
88 lines (69 loc) · 2.39 KB
/
utils.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
# -*- coding: utf-8 -*-
#/usr/bin/python2
'''
By kyubyong park. kbpark.linguist@gmail.com.
https://www.github.com/kyubyong/tacotron_asr
'''
from __future__ import print_function
import codecs
import copy
import re
import librosa
from hyperparams import Hyperparams as hp
import numpy as np
import tensorflow as tf
def get_spectrogram(sound_fpath):
'''Extracts melspectrogram and magnitude from given `sound_file`.
Args:
sound_fpath: A string. Full path of a sound file.
Returns:
Transposed S: A 2d array. A transposed melspectrogram with shape of (T, n_mels)
Transposed magnitude: A 2d array. A transposed magnitude spectrogram
with shape of (T, 1+hp.n_fft//2)
'''
# Loading sound file
y, sr = librosa.load(sound_fpath, sr=None) # or set sr to hp.sr.
# stft. D: (1+n_fft//2, T)
D = librosa.stft(y=y,
n_fft=hp.n_fft,
hop_length=hp.hop_length,
win_length=hp.win_length)
# magnitude spectrogram
magnitude = np.abs(D) #(1+n_fft/2, T)
# power spectrogram
power = magnitude**2
# mel spectrogram
S = librosa.feature.melspectrogram(S=power, n_mels=hp.n_mels) #(n_mels, T)
return np.transpose(S.astype(np.float32))
def shift_by_one(inputs):
'''Shifts the content of `inputs` to the right by one
so that it becomes the decoder inputs.
Args:
inputs: A 3d tensor with shape of [N, T, C]
Returns:
A 3d tensor with the same shape and dtype as `inputs`.
'''
return tf.concat((tf.zeros_like(inputs[:, :1]), inputs[:, :-1]), 1)
def reduce_frames(arry, r):
'''Reduces and adjust the shape and content of `arry` according to r.
Args:
arry: A 2d array with shape of [T, C]
r: Reduction factor
Returns:
A 2d array with shape of [-1, C*r]
'''
T, C = arry.shape
num_paddings = hp.r - (T % r) if T % r != 0 else 0
padded = np.pad(arry, [[0, num_paddings], [0, 0]], 'constant')
output = np.reshape(padded, (-1, C*r))
return output
def restore_shape(arry, r):
'''Restore and adjust the shape and content of `inputs` according to r.
Args:
arry: A 3d array with shape of [N, T, C]
r: Reduction factor
Returns:
A 3d tensor with shape of [-1, C*r]
'''
N, T, C = arry.shape
return arry.reshape((N, -1, C//r))