Skip to content

Instantly share code, notes, and snippets.

@lefirea
Created September 18, 2018 16:17
Show Gist options
  • Save lefirea/cb0e7dec8bdce511e5ba6e57a5921990 to your computer and use it in GitHub Desktop.
Save lefirea/cb0e7dec8bdce511e5ba6e57a5921990 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import numpy as np
import pyaudio
import pyworld as pw
import pysptk as sptk
import wave
import time
import threading
import struct
import matplotlib.pyplot as plot
import librosa
from pynput.keyboard import Key, Controller, Listener
from scipy.io import wavfile
from pysas import excite
from pysptk.synthesis import MGLSADF, Synthesizer
def RestoreWave(data):
x=np.frombuffer(data, dtype=np.int16).astype(np.float)
hop_length=80
frame_length=len(x)
order=25
alpha=0.41
stage=5
ganma=-1.0/stage
#print(x.shape)
frames=librosa.util.frame(x, frame_length=frame_length, hop_length=hop_length).astype(np.float64).T
frames*=sptk.blackman(frame_length)
f0=sptk.swipe(x.astype(np.float64), fs=Rate, hopsize=hop_length, min=50, max=500)
generator=excite.ExcitePulse(Rate, hop_length, False)
source_excitation=generator.gen(f0)
c=0
for i in range(frames.shape[1]):
if frames[0,i]==0:
c+=1
f0=sptk.swipe(x.astype(np.float64), fs=Rate, hopsize=hop_length, min=50, max=500)
generator=excite.ExcitePulse(Rate, hop_length, False)
source_excitation=generator.gen(f0)
#この部分がおかしい。mgcepが計算できない理由を特定すること
if c!=0:
mgc=np.zeros((1,order+1))
else:
#mgc=np.array(1,order+1)
mgc=np.apply_along_axis(sptk.mgcep, 1, frames, order, alpha, ganma)
b=np.apply_along_axis(sptk.mgc2b, 1, mgc, alpha, ganma)
synthe=Synthesizer(MGLSADF(order=order, alpha=alpha), hop_length)
xsynthe=synthe.synthesis(source_excitation, b)
return xsynthe.tobytes()
wavefile='materials/bitcoinY.wav'
p=pyaudio.PyAudio()
wr=wave.open(wavefile,'r')
Format=p.get_format_from_width(wr.getsampwidth())
Channels=wr.getnchannels()
Rate=wr.getframerate()
d=1.0/Rate
size=int(1024)
stream=p.open(format=Format,
channels=Channels,
rate=Rate,
output=True,
#stream_callback=callback
)
stream.start_stream()
i=0
while stream.is_active():
#print('a',end="")
data=wr.readframes(size)
xdata=RestoreWave(data)
stream.write(xdata)
stream.stop_stream()
stream.close()
wr.close()
p.terminate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment