PythonC API 3.8.0参考手册pdf下载
将音频文件拷贝到程序所在目录即可。
如下所示:
#!/usr/bin/env python # encoding: utf-8 """ @Company:华中科技大学电气学院聚变与等离子研究所 @version: V1.0 @author: Victor @contact: 1650996069@qq.com or yexin@hust.edu.cn 2018--2020 @software: PyCharm @file: VoiceDataProcessing.py @time: 2018/9/8 13:08 @Desc:处理声音数据 """ import wave import pylab as pl import numpy as np print('1650996069@qq.com or yexin@hust.edu.cn') print('working...') print("read wav data....") # 打开WAV文档,声音文件 f = wave.open(r"speak.wav", "rb") fo = wave.open(r"jg.wav", "wb") fi = wave.open(r"back.wav", "rb") # 读取波形数据 # (nchannels, sampwidth, framerate, nframes, comptype, compname) params = f.getparams() nchannels, sampwidth, framerate, nframes = params[:4] str_data = f.readframes(nframes) fi_params = fi.getparams() fi_nframes = fi_params[3] fi_str_data = fi.readframes(fi_nframes) # 将波形数据转换为数组,并更改 print("update wav data....") wave_data = np.fromstring(str_data, dtype=np.short) fi_wave_data = np.fromstring(fi_str_data, dtype=np.short) # 复制并合并声音 new_wave_data = np.hstack((wave_data, wave_data, wave_data, wave_data, wave_data, wave_data, wave_data, wave_data)) temp_wavedata = fi_wave_data[:len(new_wave_data)] new_wave_data = temp_wavedata * 0.5 + new_wave_data * 0.5 new_wave_data = np.array(new_wave_data) new_wave_data = new_wave_data.astype(wave_data.dtype) new_str_data = new_wave_data.tostring() # 写波形数据参数 print("save new wav files....") fo.setnchannels(nchannels) fo.setframerate(framerate) fo.setsampwidth(sampwidth) fo.writeframes(new_str_data) # 绘制波形 wave_data.shape = -1, 2 wave_data = wave_data.T time = np.arange(0, nframes) * (1.0 / framerate) pl.subplot(221) pl.plot(time, wave_data[0]) pl.subplot(222) pl.plot(time, wave_data[1], c="g") pl.xlabel("time (seconds)") pl.show() # 绘制波形 new_wave_data.shape = -1, 2 new_wave_data = new_wave_data.T new_time = np.arange(0, nframes * 8) * (1.0 / framerate) pl.subplot(223) pl.plot(new_time, new_wave_data[0]) pl.subplot(224) pl.plot(new_time, new_wave_data[1], c="g") pl.xlabel("time (seconds)") pl.show()
以上这篇Python3分析处理声音数据的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持谷谷点程序。
转载请注明:谷谷点程序 » Python3分析处理声音数据的例子