Mixing two PCM tracks to produce wav on Android

797 views Asked by At

I've been working on the two audio files(PCM) mix together, but when I go to play it I just hear a high pitched sound approximately the same size at the file should be. I've been following a couple of similar questions but none of them seem to be producing the same results for me. Here is what i'm working with.

EDIT

private void mixTracks() throws IOException {
    float percent = 0;
    showSnackBar("Mixing up your track...  ");

    InputStream in1 = new FileInputStream(mVoiceFile.getAbsoluteFile());
    InputStream in2 = new FileInputStream(mDecodedBeat.getAbsoluteFile());

    short[] mVoiceArray  = bytetoshort(convertStreamToByteArray(in1));
    short[] mBeatArray = bytetoshort(convertStreamToByteArray(in2));
    short[] mRap        = new short[mVoiceArray.length];

    for(int i=0; i < mRap.length; i++){
        float samplef1 = mVoiceArray[i] / 32768.0f;
        float samplef2 = mBeatArray[i] / 32768.0f;
        float mixed = samplef2 + samplef1;

        // reduce the volume a bit:
        mixed *= 0.8F;

        // hard clipping
        if (mixed > 1.0f)
            mixed = 1.0f;

        if (mixed < -1.0f)
            mixed = -1.0f;

        short outputSample = (short)(mixed * 32768.0f);
        mRap[i] = outputSample;
    }
    showSnackBar("Mixing Complete...  ");
    Log.d("Mixing", "Done");
    //audioTrack.play();
    writeShortToFile(mRap, mTempRapFile);
    rawToWave(mTempRapFile, mSilenceFile);
    encodeRap();
    //play(mRap);
}


public short[] bytetoshort(byte[]bite){
    int size = bite.length;
    short[] shortArray = new short[size];

    for (int index = 0; index < size; index++)
        shortArray[index] = (short) bite[index];

    return shortArray;
}


public byte[] convertStreamToByteArray(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buff = new byte[10240];
    int i = Integer.MAX_VALUE;
    while ((i = is.read(buff, 0, buff.length)) > 0) {
        baos.write(buff, 0, i);
    }

    return baos.toByteArray(); // be sure to close InputStream in calling function

}


public static void writeShortToFile(short[] fileBytes, File mTempRapFile) throws IOException {
    ByteBuffer byteMyShorts = ByteBuffer.allocate(fileBytes.length * 2);
    //byteMyShorts.order(ByteOrder.LITTLE_ENDIAN);

    ShortBuffer shortBytes = byteMyShorts.asShortBuffer();
    shortBytes.put(fileBytes);

    FileChannel out = new FileOutputStream(mTempRapFile).getChannel();
    out.write(byteMyShorts);
    out.close();
}

And here is what is output on my last attempt https://www.rapchat.me/share-rap.html?rapid=32113DD0-C7A9-11E6-A77D-891043651D85

Any help would be greatly appreciated!

0

There are 0 answers