• python手记(46)


    #!/usr/bin/env python  
    # -*- coding: utf-8 -*-
    #http://blog.csdn.net/myhaspl  
    #code:myhaspl@qq.com 
    import wave
    import pylab as pl
    import numpy as np
    
    
    print 'http://blog.csdn.net/myhaspl'  
    print 'myhaspl@qq.com'  
    print  
      
    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)")
    
    
     
    # 绘制波形
    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()
    

    本博客所有内容是原创,未经书面许可,严禁任何形式的转载

    http://blog.csdn.net/u010255642

    python对2个声音进行混合,背景声音是鸟叫流水,说话声音是“机器学习”的语音,混合原理是各取原声音数值的50%相加而成






    相关程序及声音资源下载地址:http://download.csdn.net/detail/u010255642/6014649

  • 相关阅读:
    【BUG】java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone
    IntelliJ IDEA控制台输出中文乱码问题
    CMD命令
    MongoDB学习笔记
    MyBatis生成序列ID
    MongoDB配置问题
    正确处理下载文件时HTTP头的编码问题(Content-Disposition)
    SpringJPA主键生成采用自定义ID,自定义ID采用年月日时间格式
    Java根据经纬度算出附近正方形的四个角的经纬度
    gradle
  • 原文地址:https://www.cnblogs.com/riskyer/p/3283454.html
Copyright © 2020-2023  润新知