Created
October 14, 2018 15:07
-
-
Save lefirea/cf54d6ef54df0a58097f1601c558ca37 to your computer and use it in GitHub Desktop.
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
# -*- coding: utf-8 -*- | |
# ============================================================================= | |
# どうがんばってもブツブツ途切れ途切れになってしまう。 | |
# 方法が悪いだけ?キューに積んでくだけじゃ足りない? | |
# よく分からん。とりあえずsptk使うのは中止。worldで行こう。 | |
# ============================================================================= | |
import numpy as np | |
import pyaudio | |
import pyworld as pw | |
import pysptk as sptk | |
import time | |
import threading | |
import queue | |
import librosa | |
from pynput.keyboard import Key, Controller, Listener | |
from scipy.io import wavfile | |
from pysas import excite | |
from pysptk.synthesis import MLSADF, Synthesizer | |
Chunk=1024*2 | |
Format=pyaudio.paInt16 | |
Channels=1 | |
Rate=16000 | |
QuitKeyPushed=False #終了キー入力 | |
def on_press(key): | |
global listener | |
global QuitKeyPushed | |
try: | |
char=key.char | |
except: | |
#修飾キー | |
char=key | |
finally: | |
print(char) | |
if char==Key.esc or char=='q': | |
print('quit...') | |
QuitKeyPushed=True | |
listener.stop() | |
thread1.join() | |
RecordThread.join() | |
PlayThread.join() | |
#exit(); | |
#pass | |
def on_release(key): | |
pass | |
def KeyLogger(): | |
try: | |
with Listener(on_press=on_press, on_release=on_release) as listener: | |
listener.join() | |
except: | |
pass | |
def ChangeVoice(): | |
global RecordQ | |
global PlayQ | |
flg=0 | |
c=1 | |
while True: | |
start=time.time() | |
data=RecordQ.get() | |
if data==None: | |
print('RecordQ is None.') | |
#listener.join()を誘発 | |
keyboard=Controller() | |
keyboard.press(Key.esc) | |
keyboard.release(Key.esc) | |
thread1.join() | |
break; | |
fs=Rate | |
data=np.frombuffer(data, dtype=np.int16) | |
#ホワイトノイズを乗せる関数 | |
def AddWhiteNoise(x, rate=0.02): | |
x=x.astype(np.float64) | |
return x+rate*np.random.randn(len(x)) | |
#ホワイトノイズ加算 | |
data=AddWhiteNoise(data) | |
frame_length=1024 | |
hop_length=64 | |
frames=librosa.util.frame(data, frame_length=frame_length, hop_length=hop_length).astype(np.float64).T | |
frames*=sptk.blackman(frame_length) | |
f0=sptk.swipe(data.astype(np.float64), fs=fs, hopsize=hop_length, min=25, max=2000) | |
generator=excite.ExcitePulse(fs, hop_length, False) | |
source_excitation=generator.gen(f0) | |
order=25 | |
alpha=0.41 | |
mc=sptk.mcep(frames, order, alpha) | |
b=sptk.mc2b(mc, alpha) | |
synthe=Synthesizer(MLSADF(order=order, alpha=alpha), hop_length) | |
#波形再生成 | |
xsynthe=synthe.synthesis(source_excitation, b).astype(np.int16) | |
print('Record',c,':',time.time()-start) | |
c+=1 | |
PlayQ.put(xsynthe.tobytes()) | |
if flg<2: | |
flg+=1 | |
elif flg==2: | |
PlayThread.start() | |
flg+=1 | |
def StreamWriter(): | |
global stream | |
global PlayQ | |
c=1 | |
while True: | |
start=time.time() | |
data=PlayQ.get() | |
if data==None: | |
print('PlayQ is None.') | |
#listener.join()を誘発 | |
keyboard=Controller() | |
keyboard.press(Key.esc) | |
keyboard.release(Key.esc) | |
thread1.join() | |
break; | |
stream.write(data) | |
print('Write ',c,':',time.time()-start,'\n') | |
c+=1 | |
thread1=threading.Thread(target=KeyLogger) | |
thread1.start() | |
p=pyaudio.PyAudio() | |
stream=p.open(format=Format, | |
channels=Channels, | |
rate=Rate, | |
input=True, | |
output=True, | |
#frames_per_buffer=Chunk | |
) | |
RecordQ=queue.Queue() | |
PlayQ=queue.Queue() | |
flg=True | |
#再生スレッド | |
PlayThread=threading.Thread(target=StreamWriter) | |
#変換スレッド | |
RecordThread=threading.Thread(target=ChangeVoice) | |
stream.start_stream() | |
print('Start Streaming...') | |
while stream.is_active(): | |
try: | |
data=stream.read(2048) | |
RecordQ.put(data) | |
if flg==True: | |
RecordThread.start() | |
flg=False | |
if QuitKeyPushed==True: | |
break; | |
except: | |
print('except') | |
#listener.join()を誘発 | |
keyboard=Controller() | |
keyboard.press(Key.esc) | |
keyboard.release(Key.esc) | |
thread1.join() | |
break; | |
finally: | |
pass | |
stream.stop_stream() | |
stream.close() | |
p.terminate() | |
while not RecordQ.empty(): | |
RecordQ.get() | |
while not PlayQ.empty(): | |
PlayQ.get() | |
print('Stop Streaming...') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment