使用 scipy 构建 wav 文件并将其写入磁盘 [英] constructing a wav file and writing it to disk using scipy

查看:81
本文介绍了使用 scipy 构建 wav 文件并将其写入磁盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将波形文件解构成小块,以不同的顺序重新组合,然后将其写入磁盘.在重新组装这些部分后,我似乎在编写它时遇到了问题,因此现在我只是尝试调试此部分,然后再担心其余部分.基本上,我将原始 wav 读入一个 2D numpy 数组,将其分成 100 个存储在较小的 2D numpy 数组列表中,然后使用 vstack 垂直堆叠这些数组:

I wish to deconstruct a wave file into small chunks, reassemble it in a different order and then write it to disk. 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:

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)

到目前为止,我确认 new_data 看起来与stereo_data 相似,但有两个例外:1. 它在开头填充了 [0,0].2. 因为len(stereo_data)/nparts 不是无余除法,所以缩短了88个样本.

So far I verified that new_data looks similar to stereo_data with two exceptions: 1. it has [0,0] padded at the beginning. 2. It is 88 samples shorter because len(stereo_data)/nparts does not divide without remainder.

当我尝试聆听生成的 new_data eave 文件时,我听到的只是沉默,我认为这没有多大意义.

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.

感谢您的帮助!奥美

推荐答案

很可能 dtype 不同.当您在开始时生成要填充的零时,您没有指定 dtype,因此它们可能是 np.int32.你的原始数据可能是np.uint8np.uint16,所以整个数组被提升为np.int32,这是不对的您的数据的位深度.只需:

It is very likely the dtype that is different. When you generate the zeros to pad at the beggining, you are not specifying a dtype, so they are probably np.int32. Your original data is probably np.uint8or np.uint16, so the whole array gets promoted to np.int32, which is not the right bit depth for your data. Simply do:

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

实际上我宁愿这样做:

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

顺便说一句,您可以大大简化您的代码,并摆脱很多 for 循环:

You could, by the way, streamline your code quite a bit, and get rid of a lot of for loops:

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)

这篇关于使用 scipy 构建 wav 文件并将其写入磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆