Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 音乐输入,垃圾输出?_Python_Audio_Scipy - Fatal编程技术网

Python 音乐输入,垃圾输出?

Python 音乐输入,垃圾输出?,python,audio,scipy,Python,Audio,Scipy,我已经将我的问题隔离到了最低限度:读取WAV文件,然后立即将其写回。输出是噪声,尽管输入是音乐。这让我困惑。代码如下: import scipy.io.wavfile as wavfile rate, data = wavfile.read("myinput.wav") wavfile.write("myoutput.wav", rate, data) 大概我在做一些非常愚蠢的事情。有人能告诉我怎么让它工作吗 另外,在读取和写入之间添加“打印数据”会产生 [ 889195140 456589

我已经将我的问题隔离到了最低限度:读取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位的问题,但在四处搜索时,我看到了许多与此问题相关的线程和建议的修复。对我来说,我将使用用户LMO在中描述的方式使用scikits.audiolab,我通过MacPorts和easy_安装在Mac上使用Python 2.7

sudo port install libsndfile; sudo easy_install-2.7 scikits.audiolab
然后,最终的代码使用audiolab进行读入(可以使其在编写时也这样做)


这适用于所讨论的文件和许多其他文件。

通过一些额外的转换,您可以将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)
导入波
将numpy作为np导入
从上下文库导入关闭
def pcm24to32(数据,nchannels=1):
temp=np.zero((len(data)/3,4),dtype='b')
temp[:,1:]=np.frombuffer(数据,dtype='b')。重塑(-1,3)

return temp.view('我无法重现您的问题。我的输入和输出WAV文件具有完全相同的MD5哈希。您可以将您的输入文件上载到某个位置吗?您使用的是哪个版本的scipy?WAV文件中的数据是每个样本多少位?您可以将文件发布到某个位置吗?当然,这里有一个指向该文件的链接:它是左通道(mono)我正在使用Python 2.7.5和SciPy 0.13.0,这两个版本都来自MacPorts,在OS X 10.8.5上。在阅读了您的评论后,我尝试了一些其他文件,发现其中一些文件可以工作,而另一些则无法工作。不确定决定因素是什么。所有这些文件都是通过Audacity以48 kHz或44.1 kHz的频率导出的,带有元数据留空。该文件报告每个样本24位,这不起作用;代码显然只支持整数类型的大小(8、16、32、64位)。此外,我还收到一个异常
ValueError:读取链接的文件时,字符串大小必须是元素大小的倍数。您是收到异常,还是静默传递?
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)