Question

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)

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.

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.

Thanks for the help! omer

Was it helpful?

Solution

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)

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:

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top