pratool / phase-vocoder Goto Github PK
View Code? Open in Web Editor NEWSignals and Systems Spring 2015 final project
Signals and Systems Spring 2015 final project
In my case, I found that I have some problems with the modified data using pyaudio.
The code is shown as below:
import pyaudio
import wave
import time
import numpy as np
import struct
def speedx(snd_array, factor):
""" Speeds up / slows down a sound, by some factor. """
indices = np.round(np.arange(0, len(snd_array), factor))
indices = indices[indices < len(snd_array)].astype(int)
return snd_array[indices]
def stretch(snd_array, factor, window_size, h):
""" Stretches/shortens a sound, by some factor. """
phase = np.zeros(window_size)
hanning_window = np.hanning(window_size)
result = np.zeros(len(snd_array) / factor + window_size)
for i in np.arange(0, len(snd_array) - (window_size + h), h*factor):
# Two potentially overlapping subarrays
a1 = snd_array[i: i + window_size]
a2 = snd_array[i + h: i + window_size + h]
# The spectra of these arrays
s1 = np.fft.fft(hanning_window * a1)
s2 = np.fft.fft(hanning_window * a2)
# Rephase all frequencies
phase = (phase + np.angle(s2/s1)) % 2*np.pi
a2_rephased = np.fft.ifft(np.abs(s2)*np.exp(1j*phase))
i2 = int(i/factor)
result[i2: i2 + window_size] += hanning_window*a2_rephased.real
# normalize (16bit)
result = ((2**(16-4)) * result/result.max())
return result.astype('int16')
def pitchshift(snd_array, n, window_size=2**13, h=2**11):
""" Changes the pitch of a sound by ``n`` semitones. """
factor = 2**(2.0 * n / 12.0)
stretched = stretch(snd_array, 1.0/factor, window_size, h)
return speedx(stretched[window_size:], factor)
wf = wave.open("/Users/miooo/Desktop/CHP_Glissandos_MinorSamples/Min_dn_sl_F_f.wav", 'rb')
# instantiate PyAudio (1)
p = pyaudio.PyAudio()
# open stream (2)
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
# read data
data = wf.readframes(1024)
data = np.fromstring(data, np.int16)
# play stream (3)
while len(data) > 0:
new_data = pitchshift(data, 1)
print new_data
out_data = new_data.astype(np.int16).tostring()
print out_data
stream.write(out_data)
data = wf.readframes(1024)
data = np.fromstring(data, np.int16)
# stop stream (4)
stream.stop_stream()
stream.close()
# close PyAudio (5)
p.terminate()
When I print the out_data, it's always 0.
Do you know how to fix it?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.