zoukankan      html  css  js  c++  java
  • Python对wav文件的重采样

    例如从2channel,4.41k hz 重采样到 1 channel,16k hz

    def downsampleWav(src, dst, inrate=44100, outrate=16000, inchannels=2, outchannels=1):
        import os,wave,audioop
        if not os.path.exists(src):
            print ('Source not found!')
            return False
    
        if not os.path.exists(os.path.dirname(dst)):
            os.makedirs(os.path.dirname(dst))
    
        try:
            s_read = wave.open(src, 'r')
            s_write = wave.open(dst, 'w')
        except:
            print ('Failed to open files!')
            return False
    
        n_frames = s_read.getnframes()
        data = s_read.readframes(n_frames)
    
        try:
            converted = audioop.ratecv(data, 2, inchannels, inrate, outrate, None)
            if outchannels == 1:
                converted = audioop.tomono(converted[0], 2, 1, 0)
        except:
            print ('Failed to downsample wav')
            return False
    
        try:
            s_write.setparams((outchannels, 2, outrate, 0, 'NONE', 'Uncompressed'))
            s_write.writeframes(converted)
        except:
            print ('Failed to write wav')
            return False
    
        try:
            s_read.close()
            s_write.close()
        except:
            print ('Failed to close wav files')
            return False
    
        return True
    

     

    若in和out都是单通道:

    def downsampleWav(src, dst, inrate=48000, outrate=16000, inchannels=1, outchannels=1):
        import os,wave,audioop
        if not os.path.exists(src):
            print ('Source not found!')
            return False
    
        if not os.path.exists(os.path.dirname(dst)):
            os.makedirs(os.path.dirname(dst))
    
        try:
            s_read = wave.open(src, 'rb')
            params = s_read.getparams()
            nchannels, sampwidth, framerate, nframes = params[:4]
            print(nchannels,sampwidth, framerate,nframes)
            s_write = wave.open(dst, 'wb')
        except:
            print ('Failed to open files!')
            return False
    
        n_frames = s_read.getnframes()
        data = s_read.readframes(n_frames)
    
        try:
            converted = audioop.ratecv(data, 2, inchannels, inrate, outrate, None)
            if outchannels == 1 and inchannels != 1:
                converted = audioop.tomono(converted[0], 2, 1, 0)
        except:
            print ('Failed to downsample wav')
            return False
    
        try:
            s_write.setparams((outchannels, 2, outrate, 0, 'NONE', 'Uncompressed'))
            s_write.writeframes(converted[0])
        except Exception as e:
            print(e)
            print ('Failed to write wav')
            return False
    
        try:
            s_read.close()
            s_write.close()
        except:
            print ('Failed to close wav files')
            return False
    
        return True
    

    方案二

    y为下采样的结果,类型np.ndarray

    You can use Librosa's load() function,

    import librosa    
    y, s = librosa.load('test.wav', sr=8000) # Downsample 44.1kHz to 8kHz

    The extra effort to install Librosa is probably worth the peace of mind.

    Pro-tip: when installing Librosa on Anaconda, you need to install ffmpeg as well, so

    pip install librosa
    conda install -c conda-forge ffmpeg

     

     

      

  • 相关阅读:
    docker 介绍,安装,镜像操作, docker换源
    go语言5 接口, 并发与并行, go协程, 信道, 缓冲信道, 异常处理, python进程线程
    [编织消息框架]目录
    2017总结
    赚钱方法[信息红利]
    面单 全单 单板 批发吉他民谣 知乎 百度知道 百度贴吧 吉他批发
    看第三部杀破狼感想
    海豚极货店 淘宝店开张啦
    我上头条了
    尤克里里 ukulele 单板 非kaka tom uma
  • 原文地址:https://www.cnblogs.com/eniac1946/p/9051227.html
Copyright © 2011-2022 走看看