简体   繁体   English

构造一个wav文件并使用scipy将其写入磁盘

[英]constructing a wav file and writing it to disk using scipy

I wish to deconstruct a wave file into small chunks, reassemble it in a different order and then write it to disk. 我希望将wave文件解构成小块,以不同的顺序重新组装,然后将其写入磁盘。 I seem to have problems with writing it after reassembling the pieces so for now I just try to debug this section and worry about the rest later. 重新组装零件后,我似乎在编写它时遇到问题,所以现在我只是尝试调试此部分,然后再担心其余部分。 Basically I read the original wav into a 2D numpy array, break it into 100 piece stored within a list of smaller 2D numpy arrays, and then stack these arrays vertically using vstack: 基本上,我将原始的wav读入2D numpy数组中,将其分成100个存储在较小的2D numpy数组列表中,然后使用vstack垂直堆叠这些数组:

import scipy.io.wavfile as sciwav
import numpy
[sr,stereo_data] = sciwav.read('filename')
nparts = 100
stereo_parts = list()
part_length = len(stereo_data) / nparts 

for i in range(nparts):
    start = i*part_length
    end = (i+1)*part_length
    stereo_parts.append(stereo_data[start:end])

new_data = numpy.array([0,0])
for i in range(nparts):
    new_data = numpy.vstack([new_data, stereo_parts[i]])
sciwav.write('new_filename', sr, new_data)

So far I verified that new_data looks similar to stereo_data with two exceptions: 1. it has [0,0] padded at the beginning. 到目前为止,我已验证new_data看起来与stereo_data相似,但有两个例外:1.它在开始时填充了[0,0]。 2. It is 88 samples shorter because len(stereo_data)/nparts does not divide without remainder. 2.因为len(stereo_data)/ nparts不会除以余数,所以它要短88个样本。

When I try to listen to the resulting new_data eave file all I hear is silence, which I think does not make much sense. 当我尝试收听生成的new_data eave文件时,我听到的只是寂静,我认为这没有多大意义。

Thanks for the help! 谢谢您的帮助! omer 奥马尔

It is very likely the dtype that is different. dtype很可能是不同的。 When you generate the zeros to pad at the beggining, you are not specifying a dtype, so they are probably np.int32 . 当您生成要在开始时填充的零时,您未指定np.int32 ,因此它们可能是np.int32 Your original data is probably np.uint8 or np.uint16 , so the whole array gets promoted to np.int32 , which is not the right bit depth for your data. 您的原始数据可能是np.uint8np.uint16 ,因此整个数组被提升为np.int32 ,这不是您数据的正确位深度。 Simply do: 只需做:

new_data = numpy.array([0,0], dtype=stereo_data)

I would actually rather do: 我实际上宁愿这样做:

new_data = numpy.zeros((1, 2), dtype=stereo_data.dtype)

You could, by the way, streamline your code quite a bit, and get rid of a lot of for loops: 顺便说一下,您可以大大简化代码,并摆脱很多for循环:

sr, stereo_data = sciwav.read('filename')
nparts = 100
part_length = len(stereo_data) // nparts 

stereo_parts = numpy.split(stereo_data[:part_length*nparts], nparts)

new_data = numpy.vstack([numpy.zeros((1, 2), dtype=stereo_data.dtype)] +
                        stereo_parts)

sciwav.write('new_filename', sr, new_data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM