python – SciPy wavfile:音乐输入,垃圾输出?
发布时间:2020-12-20 13:38:14  所属栏目:Python  来源:网络整理 
            导读:我把我的问题隔离到了最低限度:读取WAV文件,并立即将其写回.输出是噪音,尽管输入是音乐.这让我很困惑.这是代码: import scipy.io.wavfile as wavfilerate,data = wavfile.read("myinput.wav")wavfile.write("myoutput.wav",rate,data) 据推测,我做的事情非
                
                
                
            | 
 我把我的问题隔离到了最低限度:读取WAV文件,并立即将其写回.输出是噪音,尽管输入是音乐.这让我很困惑.这是代码: 
  
  
  import scipy.io.wavfile as wavfile
rate,data = wavfile.read("myinput.wav")
wavfile.write("myoutput.wav",rate,data)据推测,我做的事情非常愚蠢.有人可以告诉我如何使这个工作? 附:在读入和写出之间添加“打印数据”会产生…… [ 889195140 456589342 2605824 ...,221785355 1292756287 873860659] 解决方法
 通过一些额外的转换,您可以将24位WAV文件与标准库中的wave模块一起使用. 
  
  
  import wave
import numpy as np
from contextlib import closing
def pcm24to32(data,nchannels=1):
    temp = np.zeros((len(data) / 3,4),dtype='b')
    temp[:,1:] = np.frombuffer(data,dtype='b').reshape(-1,3)
    return temp.view('<i4').reshape(-1,nchannels)
def pcm2float(sig,dtype=np.float64):
    sig = np.asarray(sig)  # make sure it's a NumPy array
    assert sig.dtype.kind == 'i',"'sig' must be an array of signed integers!"
    dtype = np.dtype(dtype)  # allow string input (e.g. 'f')
    # Note that 'min' has a greater (by 1) absolute value than 'max'!
    # Therefore,we use 'min' here to avoid clipping.
    return sig.astype(dtype) / dtype.type(-np.iinfo(sig.dtype).min)
with closing(wave.open('my_24bit_input.wav')) as w:
    framerate = w.getframerate()
    nframes = w.getnframes()
    nchannels = w.getnchannels()
    width = w.getsampwidth()
    data = w.readframes(nframes)
assert width == 3
pcm = pcm24to32(data,nchannels)
# You can also use np.float64,if you prefer:
normalized = pcm2float(pcm,np.float32)我创建了一个IPython notebook with some more information. 当然,您也可以使用scikits.audiolab,但请注意当使用除np.float64以外的类型时,目前(版本0.11.0)存在错误(https://github.com/cournape/audiolab/issues/3)! 你也可以试试https://github.com/bastibe/PySoundFile,但我自己还没试过(还). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 
