I'm trying to take a raw byte array of audio and trim it.
The user selects a start and end value using a range slider.
// args are doubles from 0.0 - 1.0
// audio is uncompressed, 16-bit, 44,100hz, MONO
public static byte[] trimBytes(byte[] fullLength, double from, double to) {
int startingByteIndex = (int) ((double) fullLength.length * from);
// samples/frames are two bytes long, so...
// we make sure we don't include half a sample (corrupt audio)
if (startingByteIndex %2 != 0) {
startingByteIndex ++; // use plus, because minus could overflow
}
int endingByteIndex = (int) ((double) fullLength.length * to);
int finalByteArrayLength = endingByteIndex - startingByteIndex;
// make sure we don't include half a sample (corrupt audio)
if (finalByteArrayLength %2 != 0) {
finalByteArrayLength --; // use minus because plus could overflow
}
byte[] result = new byte[finalByteArrayLength];
for (int i=0; i<finalByteArrayLength; i++) {
int indexToCopy = startingByteIndex + i;
result[i] = fullLength[indexToCopy];
}
return result;
}
Is this going to crash for any reason or is there a better way to do this?
Note: I cannot use Java.nio.