语音信号处理(声速,回声,音高,界面设计PyQt5)_Python语言

本文介绍了一种使用Python实现语音速度调整的方法,并展示了如何通过叠加法和滤波法添加回声效果。文中详细解释了从读取音频文件到调整语速及添加回声的具体步骤,并提供了完整的代码实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

def YuSu(self):     #语速设置       
        f = wave.open(r"test.wav", "rb")
        # 读取格式信息
        # (nchannels, sampwidth, framerate, nframes, comptype, compname)
        params = f.getparams()
        nchannels, sampwidth, framerate, nframes = params[:4]
        # 读取波形数据
        str_data = f.readframes(nframes)
        f.close()
        # 将波形数据转换为数组
        wave_data = np.fromstring(str_data, dtype=np.short)
        time = np.arange(0, nframes) * (1.0 / framerate)

        # 要通过我们设定的名字来调用这些组件
        # price_box.toPlainText() 是一个内置的可以读取输入框中的值的函数。
        # 通过函数读取到的是一个 string 类型的值,所以我们需要把他转换成 integer 类型并存在一个price 变量中
        # price = int(self.price_box.toPlainText())
        # value() 是读取spinbox 中值的函数
        yusu = self.yusu_doubleSpinBox.value()
        # 语速设置
        wave_data1 = wave_data.astype(np.short)
        f = wave.open(r"test_yusu.wav", "wb")

        # 配置声道数、量化位数和取样频率
        f.setnchannels(1)
        f.setsampwidth(2)
        f.setframerate(yusu * framerate)  #改变采样率
        # 将wav_data转换为二进制数据写入文件
        f.writeframes(wave_data1.tostring())
        f.close()

        chunk = 1024
        f = wave.open(r"test_yusu.wav", "rb")
        p = pyaudio.PyAudio()

        # 打开声音输出流
        stream = p.open(format=p.get_format_from_width(f.getsampwidth()),
                        channels=f.getnchannels(),
                        rate=f.getframerate(),
                        output=True)

        # 写声音输出流进行播放
        while True:
            data = f.readframes(chunk)
            if data == "": break
            stream.write(data)
        stream.close()
        p.terminate()

        f = wave.open(r"test_yusu.wav", "rb")
        # 读取格式信息
        # (nchannels, sampwidth, framerate, nframes, comptype, compname)
        params = f.getparams()
        nchannels, sampwidth, framerate, nframes = params[:4]
        # 读取波形数据
        str_data = f.readframes(nframes)
        f.close()
        # 将波形数据转换为数组
        wave_data = np.fromstring(str_data, dtype=np.short)
        time = np.arange(0, nframes) * (1.0 / framerate)
        self.curve2.setData(time, wave_data)
        self.curve2.attach(self.qwtPlot_2)
        self.qwtPlot_2.replot()    

回声添加,两种方法:叠加法和滤波法

def HuiSheng(self):
        f = wave.open(r"test.wav", "rb")
        # 读取格式信息
        # (nchannels, sampwidth, framerate, nframes, comptype, compname)
        params = f.getparams()
        nchannels, sampwidth, framerate, nframes = params[:4]
        # 读取波形数据
        str_data = f.readframes(nframes)
        f.close()

        # 将波形数据转换为数组
        wave_data = np.fromstring(str_data, dtype=np.short)
        time = np.arange(0, nframes) * (1.0 / framerate)
        huisheng = self.huisheng_spinBox.value()
        # 滤波,一次回声
        b1 = np.array([1])
        b2 = np.zeros([1, 6000])
        b3 = np.array([0.7])
        b = np.append(b1, b2)
        b = np.append(b, b3)
        a = 1
        wave_data = np.append(wave_data, np.zeros([1, nframes]))
        wave_filter = signal.lfilter(b, a, wave_data)

        # 写Wave文件
        # t = np.arange(0,np.max(time)*2,1.0/framerate)

        # 打开WAV文档
        wave_filter = wave_filter.astype(np.short)
        f = wave.open(r"test_huisheng.wav", "wb")

        # 配置声道数、量化位数和取样频率
        f.setnchannels(1)
        f.setsampwidth(2)
        f.setframerate(framerate)
        # 将wav_data转换为二进制数据写入文件
        f.writeframes(wave_filter.tostring())
        f.close()

        # 播放声音
        chunk = 1024
        f = wave.open(r"test_huisheng.wav", "rb")
        p = pyaudio.PyAudio()

        # 打开声音输出流
        stream = p.open(format=p.get_format_from_width(f.getsampwidth()),
                        channels=f.getnchannels(),
                        rate=f.getframerate(),
                        output=True)

        # 写声音输出流进行播放
        while True:
            data = f.readframes(chunk)
            if data == "": break
            stream.write(data)

        stream.close()
        p.terminate()

        f = wave.open(r"test_huisheng.wav", "rb")
        # 读取格式信息
        # (nchannels, sampwidth, framerate, nframes, comptype, compname)
        params = f.getparams()
        nchannels, sampwidth, framerate, nframes = params[:4]
        # 读取波形数据
        str_data = f.readframes(nframes)
        f.close()
        # 将波形数据转换为数组
        wave_data = np.fromstring(str_data, dtype=np.short)
        time = np.arange(0, nframes) * (1.0 / framerate)
        
        self.curve2.setData(time, wave_data)
        self.curve2.attach(self.qwtPlot_2)
        self.qwtPlot_2.replot()

 界面:

 

转载于:https://www.cnblogs.com/oucxlw/p/9223030.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值