输出音频不过是静态的

时间:2019-05-22 19:31:22

标签: python scipy signal-processing audio-processing

下面的

是获取音频文件,对其应用过滤器然后将声音播放回用户的代码。该代码的“创建音频”部分应采用Python播放的内容并输出音频文件。当我收听输出的音频文件时,它是一堆静电,我不确定是什么原因造成的

from io import BytesIO
import tempfile
import requests
import librosa
import numpy as np
import scipy.signal as sg
import pydub
import matplotlib.pyplot as plt
from IPython.display import Audio, display
import wave
%matplotlib inline

def speak(voice):
    audio = pydub.AudioSegment.from_mp3(BytesIO(voice))    #takes audiofile     and creates a mutable file

    with tempfile.TemporaryFile() as fn:
        wavef = audio.export(fn, format='wav')
        wavef.seek(0)                            
        wave = wavef.read()

    x = np.frombuffer(wave, np.int16)[24:] / 2.**15     #creates a numpy array by taking info of audio from temp memory. 
    return x, audio.frame_rate


def play(x, fr, autoplay=False):
    display(Audio(x, rate=fr, autoplay=autoplay))




voice = open("pathToMp3", "rb").read()  

x, fr = speak(voice)
play(x, fr)

b, a = sg.butter(4, 500. / (fr / 2.), 'low')   
x_fil = sg.filtfilt(b, a, x)
play(x_fil, fr)


#create audio file
wav_out = wave.open("output2.wav", "wb")
wav_out.setnchannels(2) # stereo has 2 channels
wav_out.setframerate(44100)
wav_out.setsampwidth(2)

wav_out.writeframes(x_fil.tobytes())
wav_out.close()

0 个答案:

没有答案