我一直在研究两种音频文件(PCM)混合在一起,但是当我去播放它时,我只是听到一个高音调声音大致相同的文件应该是。我一直在关注几个类似的问题,但似乎没有一个问题对我产生相同的结果。这是我正在使用的。
修改
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();
}
这是我上一次尝试的输出 https://www.rapchat.me/share-rap.html?rapid=32113DD0-C7A9-11E6-A77D-891043651D85
非常感谢任何帮助!