-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert44100to48000.py
426 lines (341 loc) · 17.2 KB
/
convert44100to48000.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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#coding:utf-8
#
# stero WAV file convert sampling rate from 44.1KHz to 48KHz, or to 96KHz
#--------------------------------------------------------------
# Using
# Python 3.6.4, 64bit on Win32 (Windows 10)
# numpy 1.18.4
# scipy 1.4.1
# soundfile 0.10.3
# ------------------------------------------------------------
# 2023/9/19 add: gain_adjust option: gain up 1,2,or,3 dB until non-clip
import sys
import os
import glob
import copy
import argparse
import numpy as np
from scipy import signal
from scipy.fftpack import fft, ifft
from scipy.io.wavfile import read as wavread
from scipy.io.wavfile import write as wavwrite
class convert441to480(object):
def __init__(self,path_input_wav, path_output_wav=None, output_bit=16, factor=1, method="COWM", gain_adjust=False):
# initalize
self.wavfile= path_input_wav # input wav file name
if path_output_wav is None:
new_dir= os.path.join(os.path.dirname(self.wavfile), method)
if not os.path.exists( new_dir):
os.mkdir( new_dir)
if factor == 1:
sufix0='_48KHz'
elif factor == 2:
sufix0='_96KHz'
else:
sufix0='_out'
self.wavfile2= os.path.join(new_dir ,os.path.splitext(os.path.basename(self.wavfile))[0] + sufix0 + '.wav')
else:
self.wavfile2= path_output_wav # output file name
self.output_bit= output_bit
self.factor= factor # 48KHz is 1, 96KHz is 2.
###############################
# sampling rate
self.original= 44100
self.target= 48000
self.duration= 0.08 # frame length uint is second
# multiplication coefficient
self.original_mc = int(160 * self.factor) # for 44100
self.target_mc = int(147 * self.factor) # for 48000
self.original_points = self.original * self.duration
if self.original_points - int(self.original_points) != 0.:
print ("Error: original_points is not integral number.", self.original_points )
sys.exit()
elif int(self.original_points) % 2 != 0:
print ("Error: original_points must be even number.", int(self.original_points) % 2 )
sys.exit()
else:
self.original_points= int(self.original_points)
print ('original points ', self.original_points )
self.original_points_mc= int(self.original_points * self.original_mc)
print ('original points mc ', self.original_points_mc )
self.target_points = self.target * self.factor * self.duration
if self.target_points - int(self.target_points) != 0.:
print ("Error: target_points is not integral number.", self.target_points )
sys.exit()
elif int(self.target_points) % 2 != 0:
print ("Error: target_points must be even number.", int(self.target_points) % 2 )
sys.exit()
else:
self.target_points= int(self.target_points)
print ('target points ', self.target_points )
self.target_points_mc= int(self.target_points * self.target_mc / self.factor)
print ('target points mc ', self.target_points_mc )
if self.original_points_mc != self.target_points_mc:
print ("Error: original_points_mc and target_points_mc must be same." )
sys.exit()
# FFT point and every shift
self.N = self.original_points
self.SHIFT= int(self.N/2) # shift must be N/2
self.N2= self.target_points # output point
self.SHIFT2= int(self.N2/2)
# read input wav file
self.wdata, self.fs= self.read_wav( self.wavfile)
self.stmono= self.wdata.shape[1]
self.size0= self.wdata.shape[0]
# show WAV information
print ("sampling rate ", self.fs)
print ("original size ", self.size0)
if self.fs != self.original:
print ("Sorry, sampling rate should be ", self.original, self.wavfile)
sys.exit()
if self.stmono != 2:
print ("Sorry, only stereo wav file is available")
sys.exit()
# count shift number
self.num0= int((self.size0 - self.N)/ self.SHIFT) + 1
print ("number ", self.num0)
self.size0_new = self.SHIFT2 * (self.num0 + 1)
print ("target size ", self.size0_new)
# process
self.method= method
if self.method == "SDOM":
print ("method", self.method)
self.wavo= self.sub_main_SDOM()
elif self.method == "COWM":
print ("method", self.method)
if 0: # check window combination response
self.plot_COWM()
self.wavo= self.sub_main_COWM()
else:
print ("Error: there is no such method.", self.method )
sys.exit()
# gain adjustment
MAX_ADJUST_GAIN=3
if gain_adjust:
peak0=np.max(np.abs(self.wavo))
gain0=0
for i in range(1,int(MAX_ADJUST_GAIN+1)): # 1,2,3
if peak0 * np.power(10, i /20.0) >= 1.0: # when clip
break
else: # when non clip
gain0=gain0+1
if gain0 > 0:
self.wavo=self.wavo* np.power(10, gain0 /20.0)
print("gain up ", gain0, " dB")
else:
print("gain no adjust")
# write output wav
if self.output_bit == 16:
self.save_wav16(self.wavfile2, self.wavo, sr= int(self.target * self.factor) )
elif self.output_bit == 24:
import soundfile as sf
sf.write(self.wavfile2, self.wavo, int(self.target * self.factor), 'PCM_24')
print ('wrote ', self.wavfile2)
def sub_main_SDOM(self):
###############################################################
#
# SHIFT DATA OVERLAP METHOD:
#
#
# BBBMMMCCCCCCMMMBBB
# BBBMMMCCCCCCMMMBBB
# B: zero, ignore, Suteru
# M: linearly MIX
# C: sonomama tukau
#
###############################################################
# MIX value
M=3 # bunkatu suu of half duration
NL0=int(self.N2/(M*2)) # duration CCC and BBB, ex It's 682 when N=4096
NL= int(self.N2/2 - (NL0 *2)) # duration MMM ex It's 684 when N=4096
print ("NL0, NL ", NL0, NL)
k0=np.linspace(0,1,NL)
k1=np.linspace(1,0,NL)
N_0= int(self.N)
N_M1= int(self.N-1)
N_2= int(self.N/2)
N_2P1= int(self.N/2+1)
N_MC = int( self.N * (self.original_mc -2))
# output data
wavo=np.zeros( (int(self.size0_new), self.stmono) )
for loop in range(self.num0):
print (" " + str(loop)+"/" + str(self.num0) +"\r",end="")
sp0= int(self.SHIFT * loop) # input point
sp2= int(self.SHIFT2 * loop) # output point
for ch0 in range( self.stmono ):
# read N points via every SHIFT
fw1= self.wdata[sp0: int(sp0 + self.N), ch0]
# Fourier transform via FFT
yf = fft(fw1)
# 1/N ga kakarukara node *2baisuru, center Value ha ryouhou ni huru
yf2=np.concatenate([yf[0:1] , yf[1:N_2], yf[N_2:N_2P1]*0.5, np.zeros(N_M1), np.zeros(N_MC), yf[N_2:N_2P1]*0.5, yf[N_2+1:N_0] ])
iyf2_all= ifft(yf2 * self.original_mc).real
iyf2= iyf2_all.reshape(self.N2, int(self.target_mc/self.factor))[:,0]
# 1st loop
if loop == 0:
wavo[0:int(self.N2/2+NL0),ch0]=iyf2[0:int(self.N2/2+NL0)]
else:
# mix, duration of mmm
if ch0 == 0:
dmix=(iyf2[int(self.N2/2-NL0-NL):int(self.N2/2-NL0)] * k0) + dch0[:] # for ch0
elif ch0 == 1:
dmix=(iyf2[int(self.N2/2-NL0-NL):int(self.N2/2-NL0)] * k0) + dch1[:] # for ch0
wavo[int(sp2+self.N2/2-NL0-NL) : int(sp2+ self.N2/2-NL0) ,ch0]=dmix[:]
# duration of ccc
wavo[int(sp2+self.N2/2-NL0) : int(sp2+self.N2/2+NL0) ,ch0]=iyf2[ int(self.N2/2-NL0) : int(self.N2/2+NL0)]
# copy to backup
if ch0 == 0:
dch0=iyf2[int( self.N2/2+NL0) : int(self.N2/2+NL0+NL) ] * k1
elif ch0 == 1:
dch1=iyf2[int( self.N2/2+NL0) : int(self.N2/2+NL0+NL) ] * k1
print(' ')
return np.clip( wavo,-1., 1.)
def sub_main_COWM(self):
###############################################################
#
# combine OVERLAP WINDOW METHOD:
#
###############################################################
N_0= int(self.N)
N_M1= int(self.N-1)
N_2= int(self.N/2)
N_2P1= int(self.N/2+1)
N_MC = int( self.N * (self.original_mc -2))
# make Hann window
original_win1= self.evenHANNwindow(self.N)
original_win1st= np.concatenate( [np.ones( self.SHIFT), original_win1[self.SHIFT:] ])
original_win1last= np.concatenate( [ original_win1[0:self.SHIFT], np.ones( self.SHIFT)])
# output data
wavo=np.zeros( (int(self.size0_new), self.stmono) )
for loop in range(self.num0):
print (" " + str(loop)+"/" + str(self.num0) +"\r",end="")
sp0= int(self.SHIFT * loop) # input point
sp2= int(self.SHIFT2 * loop) # output point
for ch0 in range( self.stmono ):
# read N points via every SHIFT
if loop == 0: # 1st loop
fw1= self.wdata[sp0: int(sp0 + self.N), ch0] * original_win1st
elif loop == ( self.num0 -1): # last
fw1= self.wdata[sp0: int(sp0 + self.N), ch0] * original_win1last
else:
fw1= self.wdata[sp0: int(sp0 + self.N), ch0] * original_win1
# Fourier transform via FFT
yf = fft(fw1)
# 1/N ga kakarukara node *2baisuru, center Value ha ryouhou ni huru
yf2=np.concatenate([yf[0:1] , yf[1:N_2], yf[N_2:N_2P1]*0.5, np.zeros(N_M1), np.zeros(N_MC), yf[N_2:N_2P1]*0.5, yf[N_2+1:N_0] ])
iyf2_all= ifft(yf2 * self.original_mc).real
iyf2= iyf2_all.reshape(self.N2, int(self.target_mc/self.factor))[:,0]
if loop == 0: # 1st loop
wavo[0:self.SHIFT2,ch0]= iyf2[0:self.SHIFT2]
elif loop == ( self.num0 -1): # last
if ch0 == 0:
wavo[sp2 : int(sp2+ self.SHIFT2) ,ch0] = dch0[self.SHIFT2:] + iyf2[0:self.SHIFT2]
elif ch0 == 1:
wavo[sp2 : int(sp2+ self.SHIFT2) ,ch0] = dch1[self.SHIFT2:] + iyf2[0:self.SHIFT2]
wavo[int(sp2+ self.SHIFT2) : int(sp2 + self.N2),ch0] = iyf2[self.SHIFT2:]
else:
# combine with window
if ch0 == 0:
wavo[sp2 : int(sp2+ self.SHIFT2) ,ch0] = dch0[self.SHIFT2:] + iyf2[0:self.SHIFT2]
elif ch0 == 1:
wavo[sp2 : int(sp2+ self.SHIFT2) ,ch0] = dch1[self.SHIFT2:] + iyf2[0:self.SHIFT2]
# copy to backup
if ch0 == 0:
dch0=iyf2.copy()
elif ch0 == 1:
dch1=iyf2.copy()
print(' ')
return np.clip( wavo,-1., 1.)
def evenHANNwindow(self, size0):
# size0 should be even number.
# return a similar to HANN window
size0_half= int(size0/2)
x=np.linspace(0, np.pi/2.0, size0_half)
y=np.square( np.sin(x))
window=np.concatenate([y,y[::-1]])
if 0:
for i in range(size0_half):
print( window[i] + window[size0_half+i])
return window
def plot_COWM(self,):
from matplotlib import pyplot as plt
color_list = ["r", "g", "b", "c", "m", "y", "k", "w"]
x_time= np.linspace(0, self.N, self.N)
LNG0=5
x_timex2= np.linspace(0, self.SHIFT * LNG0, int(self.SHIFT * LNG0))
x_time2= np.linspace(0, self.N2, self.N2)
fig = plt.figure()
ax1 = fig.add_subplot(2, 1, 1)
original_win1= self.evenHANNwindow(self.N)
original_win1st= np.concatenate( [np.ones( self.SHIFT), original_win1[self.SHIFT:] ])
original_win1last= np.concatenate( [ original_win1[0:self.SHIFT], np.ones( self.SHIFT)])
ax1.plot(x_time, original_win1st, color=color_list[0])
ax1.plot(x_time+self.SHIFT, original_win1, color=color_list[1])
ax1.plot(x_time+self.SHIFT*2, original_win1, color=color_list[2])
ax1.plot(x_time+self.SHIFT*3, original_win1last, color=color_list[3])
ax2 = fig.add_subplot(2, 1, 2)
y= np.zeros( int(self.SHIFT * LNG0) )
for loop in range(LNG0):
sp0= int(self.SHIFT * loop)
if loop == 0:
y[0:self.SHIFT]= original_win1st[0:self.SHIFT]
elif loop==(LNG0-1):
y[ sp0: int(sp0+ self.SHIFT)]= original_win1last[self.SHIFT:]
else:
y[ sp0: int(sp0+ self.SHIFT)]= original_win1[self.SHIFT:self.N] + original_win1[0:self.SHIFT]
ax2.plot(x_timex2, y, color=color_list[0])
ax2.plot(original_win1[self.SHIFT:self.N] + original_win1[0:self.SHIFT], color=color_list[1] )
ax1.grid(which='both', axis='both')
plt.tight_layout()
plt.show()
plt.close()
def read_wav(self, file_path ):
try:
sr, w = wavread( file_path)
except:
print ('error: wavread ', file_path)
sys.exit()
else:
if w.dtype == np.int16:
#print('np.int16')
w= w / (2 ** 15)
elif w.dtype == np.int32:
#print('np.int32')
w= w / (2 ** 31)
#print ('sampling rate ', sr)
#print ('size', w.shape) # [xxx,2]
return w, sr
def save_wav16(self, file_path, data, sr=48000):
amplitude = np.iinfo(np.int16).max
try:
wavwrite( file_path , sr, np.array( amplitude * data , dtype=np.int16))
except:
print ('error: wavwrite ', file_path)
sys.exit()
print ('wrote ', file_path)
if __name__ == '__main__':
import datetime
parser = argparse.ArgumentParser(description='WAV file convert sampling rate from 44.1KHz to 48KHz/96KHz')
parser.add_argument('--input_wav', '-i', default='sample_wav/test_44100Hz.wav', help='specify input wav filename')
parser.add_argument('--output_wav', '-o', default=None, help='specify output wav filename')
parser.add_argument('--factor', '-f', type=int, default=1, help='specify 2 when convert to 44.1KHz to 96KHz (defualt 1)')
parser.add_argument('--output_bit', '-b', type=int, default=16, help='output bit 16 or 24 (defualt 16bit)')
parser.add_argument('--dir', '-d', default=None, help='specify input wav directory. This is alternative of --output_wav.')
parser.add_argument('--method', '-m', type=str, default='COWM', help='specify method COWM or SDOM (default COWM) ')
parser.add_argument('--gain_adjust', '-a', action='store_true', help='if set true, gain up 1,2, or 3 dB until non-clip')
args = parser.parse_args()
# record start time
dt_now0 = datetime.datetime.now()
if args.dir is not None and os.path.exists(args.dir):
flist= glob.glob( os.path.join(args.dir, '*.wav'))
for i,file_path in enumerate(flist):
# create instance
conv1= convert441to480(file_path, output_bit=args.output_bit, factor=args.factor, method=args.method, gain_adjust=args.gain_adjust)
# destruct instance
del conv1
else: # do once
# create instance
conv1= convert441to480(args.input_wav, args.output_wav, output_bit=args.output_bit, factor=args.factor, method=args.method, gain_adjust=args.gain_adjust )
# record finish time
dt_now1 = datetime.datetime.now()
print ('time ', dt_now1-dt_now0)