-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHackPitch.py
110 lines (79 loc) · 2.39 KB
/
HackPitch.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
#Crazy F0 return
#Eng Eder de Souza - ederwander
from struct import pack
import wave
import numpy as np
import math
import pyaudio
import sys
#Here are all the math equations described in patent
def GetPitch(frame, maxP, minP):
minimum=np.Inf
periodo = -1
for pos in range(minP, maxP):
nolag=frame[0:(maxP*2)-pos]
onelag=frame[pos:maxP*2]
twolag=frame[np.round(np.arange(pos*2, (maxP*2)*2, 2))]
H=np.sum((nolag * onelag)-(onelag * twolag))
E=np.sum((nolag**2)-(twolag**2))
V=E-(2.0*H)
if V<minimum and V <= (np.finfo(np.float32).eps * E):
minimum=V
periodo=pos;
return periodo;
#its is just for fun ;)
def BuildSine(F, phase):
phaseInc = 2.0*np.pi*F/Fs;
signal2=[]
for i in range(0, frameSize):
signal2.append(np.sin(phase))
phase = phase + phaseInc;
#place the phaser between the 0 and 2pi range
phase = np.mod(phase, 2.0*np.pi);
return signal2, phase
spf = wave.open('teste.wav','r');
Fs = spf.getframerate();
signal = spf.readframes(-1);
#if stereo lets get just one channel
if spf.getnchannels() == 2:
signal=signal[::1]
intsignal = np.frombuffer(signal, dtype=np.int16)
floatsignal = np.float32(intsignal) / (1<<15)
# Initialize PyAudio
pyaud = pyaudio.PyAudio()
# Open stream
stream = pyaud.open(format = pyaudio.paFloat32,
channels = spf.getnchannels(),
rate = spf.getframerate(),
output = True)
phase=0.0;
signal2=[];
frameSize=1<<12;
minF = 50
maxF = 900
minP = int(Fs/maxF);
maxP = int(Fs/minF);
if frameSize < maxP:
frameSize = maxP
lastP0=Fs/minF
hiSine=[]
print("Collecting Pitch and build a Sine Melody...")
for i in range(0, len(floatsignal), frameSize):
if i+frameSize > len(floatsignal):
break
chunk = (floatsignal[i:i+frameSize])
P0=GetPitch(chunk, maxP, minP)
if Fs/P0 > maxF:
P0=lastP0
hi, phase=BuildSine((Fs/P0), phase)
hiSine=np.concatenate([hiSine, hi])
'''
#I tryed play inside this loop but I get clicks sometimes, than lets append all Sine Sounds to play later
out = pack("%df"%len(hi), *(hi))
stream.write(out)
'''
lastP0=P0;
print("Done ...")
print("Playing...")
out = pack("%df"%len(hiSine), *(hiSine))
stream.write(out)