Created
November 27, 2015 07:43
-
-
Save axlevisu/d52f0a0e172ab4003c3b to your computer and use it in GitHub Desktop.
Autotuner application using GNU Radio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Test using wav file | |
import sys | |
from scipy import * | |
from pylab import * | |
from scipy.io import wavfile | |
import numpy as np | |
samp_rate, inputa = wavfile.read("test.wav"); | |
size = 2**16; | |
# size = len(inputa) | |
outputa =[] | |
def frequency(sub_array): | |
# sub_array = sound_array | |
hanning_window = np.hanning(len(sub_array)) | |
fft = np.fft.fft(hanning_window*sub_array) | |
abs_fft = np.abs(fft) | |
m = max(abs_fft) | |
# print [k for k,j in enumerate(abs_fft) if j==m] | |
index = [k for k,j in enumerate(abs_fft) if j==m][0] | |
if(index>size/2):index = size-index | |
return index*samp_rate/size | |
def speedx(sound_array, factor): | |
indices = np.round( np.arange(0, len(sound_array), factor) ) | |
indices = indices[indices < len(sound_array)].astype(int) | |
return sound_array[ indices.astype(int) ] | |
def stretch(sound_array, f, window_size, h): | |
phase = np.zeros(window_size) | |
hanning_window = np.hanning(window_size) | |
result = np.zeros( len(sound_array) /f) | |
for i in np.arange(0, len(sound_array)-(window_size+h), h*f): | |
a1 = sound_array[i: i + window_size] | |
a2 = sound_array[i + h: i + window_size + h] | |
s1 = np.fft.fft(hanning_window * a1) | |
s2 = np.fft.fft(hanning_window * a2) | |
phase = (phase + np.angle(s2/s1)) % 2*np.pi | |
a2_rephased = np.fft.ifft(np.abs(s2)*np.exp(1j*phase)) | |
i2 = int(i/f) | |
result[i2 : i2 + window_size] += hanning_window*a2_rephased | |
result = ((2**(14)) * result/result.max()) # normalize (16bit) | |
return result.astype('int16') | |
for i in xrange(0, len(inputa),size): | |
sub_array = inputa[i:i+size] | |
f = frequency(sub_array) | |
print f | |
n_pitch =12*np.log2(f/440.0) | |
n_pitch = np.rint(n_pitch) | |
near_frequency = 440.0*np.exp2(n_pitch/12) | |
factor = near_frequency/f | |
print near_frequency | |
stretched = stretch(inputa[i:i+size], factor, 2**12, 2**10) | |
# outputa[i*size:(i+1)*size] = speedx(stretched[2**12:],1/factor) | |
nfout = speedx(stretched,1/factor) | |
print frequency(nfout),"\n" | |
outputa = np.concatenate([outputa,nfout]).astype(int16) | |
wavfile.write("zest.wav",samp_rate,outputa) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment