rochars / wavefile

Create, read and write wav files according to the specs. :star: :notes: :heart:
MIT License
226 stars 48 forks source link

getSamples() has wrong return type declaration #36

Open brun0ne opened 1 year ago

brun0ne commented 1 year ago

This is how currently getSamples() is defined in index.d.ts getSamples(interleaved?:boolean, OutputObject?: Function): Float64Array;

It is supposed to return a Float64Array. But, according to the docs, when there are more than 2 channels, it returns an array of Float64Arrays.

If the file is stereo or have more than one channel then the samples will be returned de-interleaved in a Array of Float64Array objects, one Float64Array for each channel.

This creates confusion when you try to create some logic around it, for example, if you need to go through all channels, you might write something like this:

const numChannels = (wave.fmt as any).numChannels;
let sampleChannels: Float64Array[] = [];

if (numChannels > 1) {
    sampleChannels = wave.getSamples() as Float64Array[];
}
else {
    sampleChannels.push(wave.getSamples() as Float64Array);
}

Unfortunately, this fails. I had to change the return type in index.d.ts to: Float64Array | Float64Array[].

I think this would be the right declaration and should be changed, or the function could be changed so that it always returns an array of arrays.