python手记(48)

#!/usr/bin/env python  

# -*- coding: utf-8 -*-

#http://blog.csdn.net/myhaspl  

#code:[email protected] 

import wave

import pylab as pl

import numpy as np

import copy



print 'http://blog.csdn.net/myhaspl'  

print '[email protected]'  

print  

  

print 'working...' 







print "read wav data...."

# 打开WAV文档

f = wave.open(r"speak.wav", "rb")

fo = wave.open(r"jghy.wav", "wb")





# 读取波形数据

# (nchannels, sampwidth, framerate, nframes, comptype, compname)

params = f.getparams()

nchannels, sampwidth, framerate, nframes = params[:4]

str_data = f.readframes(nframes)







#将波形数据转换为数组,并更改

print "update wav data...."

wave_data = np.fromstring(str_data, dtype=np.short)











#产生回音



new_wave_data=copy.deepcopy(wave_data)

hystart=1

temp_wavedata=new_wave_data[hystart:len(new_wave_data)/2]

new_wave_data[hystart:len(new_wave_data)/2]+=temp_wavedata*0.4

new_wave_data[len(new_wave_data)/2+hystart:len(new_wave_data)]+=temp_wavedata*0.4



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)")



 

# 绘制波形

new_wave_data.shape = -1, 2

new_wave_data =new_wave_data.T

new_time = np.arange(0, nframes) * (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()


python  声音产生回声

python手记(48)

 

你可能感兴趣的:(python)