forked from carpedm20/DCGAN-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tensorflow_wav.py
82 lines (66 loc) · 2.08 KB
/
tensorflow_wav.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
import wave
import numpy as np
import tensorflow as tf
from scipy.io.wavfile import read, write
import scipy
import ops
import math
import pickle
FRAME_SIZE=(64/2048)
HOP=(2048-64)/(2048*64)
# Returns the file object in complex64
def get_wav(path):
wav = wave.open(path, 'rb')
rate, data = read(path)
results={}
results['rate']=rate
results['channels']=wav.getnchannels()
results['sampwidth']=wav.getsampwidth()
results['framerate']=wav.getframerate()
results['nframes']=wav.getnframes()
results['compname']=wav.getcompname()
processed = np.array(data).astype(np.int16, copy=False)
results['data']=processed
return results
def save_wav(in_wav, path):
wav = wave.open(path, 'wb')
wav.setnchannels(in_wav['channels'])
wav.setsampwidth(in_wav['sampwidth'])
wav.setframerate(in_wav['framerate'])
wav.setnframes(in_wav['nframes'])
wav.setcomptype('NONE', 'processed')
processed = np.array(in_wav['data'], dtype=np.int16)
wav.writeframes(processed)
def save_stft(in_wav, path):
f = open(path, "wb")
try:
pickle.dump(in_wav, f, pickle.HIGHEST_PROTOCOL)
print("DUMPED")
finally:
f.close()
def get_stft(filename):
f = open(filename, "rb")
data = pickle.load(f)
f.close()
return data
def compose(input, rank=3):
return input
real = tf.real(input)
imag = tf.imag(input)
return tf.concat(rank, [real, imag])
def encode(input,bitrate=4096):
output = input
output = tf.reshape(output, [-1, 64,64,1])
output = compose(output)
return output
def scale_up(input):
with tf.variable_scope('scale'):
input = tf.reshape(input, [-1, 4096])
w=tf.get_variable('scale_w', [1,1], initializer=tf.constant_initializer(0.01))
#bias=tf.get_variable('scale_bias', [input.get_shape()[-1]], initializer=tf.constant_initializer(0))
output = tf.nn.tanh(input)
#output = tf.pow(input,w)
output = input / w
#output = tf.nn.bias_add(output, bias)
print(output.get_shape())
return tf.reshape(output, [-1, 64, 64, 1])