1

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!

Jeremy
  • 241
  • 4
  • 17
  • Related SO post :http://stackoverflow.com/questions/376036/algorithm-to-mix-sound – Morrison Chang Dec 21 '16 at 19:14
  • Hi Morrison, I took a look at that link and have edited my code. I readjusted the algorithm and now I have a lower volume white noise as opposed to the high pitched static. – Jeremy Dec 22 '16 at 01:44
  • 1
    Make sure you have the correct endian format for reading your PCM files: http://stackoverflow.com/questions/18675801/signed-16-bit-pcm-transformations-arent-working-why – Morrison Chang Dec 22 '16 at 05:09
  • Hi @Jeremy, were you able to solve this problem? I am looking forward to the solution of the same problem. – w2lame Feb 19 '18 at 19:17

0 Answers0