Skip to main content
added 32 characters in body
Source Link
user177754
user177754

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.

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?

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.

Source Link
user177754
user177754

Safely and Quickly Trim a Byte Array of Audio in Java

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?