Skip to content

Commit

Permalink
Merge pull request #6 from Enterprise-Neurosystem/fix/issue-5
Browse files Browse the repository at this point in the history
Fix integer overflow on large wav files and issue #5.
  • Loading branch information
daw3rd authored Aug 19, 2022
2 parents 4aa4855 + 5e1d0ea commit 75c71d6
Showing 1 changed file with 3 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ public static double[] pcm2Double(byte[] samples, int channels, int bitsPerSampl
throw new IllegalArgumentException("bits per sample must be 8, 16,24, or 32");
if (channels <= 0)
throw new IllegalArgumentException("channels must be larger than 0");
int sampleCount = samples.length * 8 / (bitsPerSample * channels);
int sampleCount = 8 * samples.length / (bitsPerSample * channels); // Multiply by 8 first for small sample lengths
if (sampleCount < 0) // If sample length is too large, multiply first by 8 can cause overflow, so do it last.
sampleCount = samples.length / (bitsPerSample * channels) * 8;
double result[] = new double[sampleCount];
ByteBuffer byteBuffer = ByteBuffer.wrap(samples);
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
Expand Down

0 comments on commit 75c71d6

Please sign in to comment.