I successfully could mix two 8 bit files with the code below.(with compiled Musicg 1.4.2.0 jar lib) But when I try to mix two 22050 (sample rate) and 16bit files the resulting file plays faster. Tried everything in the header code or in the mix equation, but still plays faster (maybe twice). Tried to change 128 to 35768( 2^ to 2^15), but I noticed it's related to the amplitude, the length remains the same. So it keeps playing faster. Any suggestion?
InputStream in1 = getResources().openRawResource(R.raw.myfile);
InputStream in2 = getResources().openRawResource(R.raw.myfile2);
Wave w1 = new Wave(in1);
short[] music1 = w1.getSampleAmplitudes();
in1.close();
Wave w2 = new Wave(in2);
short[] music2 = w2.getSampleAmplitudes();
in2.close();
byte[] output = new byte[(music1.length > music2.length) ? music2.length
: music1.length];
for (int i = 0; i < output.length; i++) {
float samplef1 = music1[i] / 32768.0f; // 2^7=128
float samplef2 = music2[i] / 32768.0f;
float mixed = (samplef1 + samplef2)/2;
//reduce the volume a bit:
mixed *= 0.8;
//hard clipping
if (mixed > 1.0f)
mixed = 1.0f;
if (mixed < -1.0f)
mixed = -1.0f;
byte outputSample = (byte) (mixed * 128.0f);
output[i] = outputSample;
} // for loop
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+"result.wav");
if (f.exists()) {
f.delete();
}
FileOutputStream fo = new FileOutputStream(f);
WaveHeader wh = w2.getWaveHeader();
MyWaveHeader mwh = new MyWaveHeader((short) wh.getAudioFormat(),
(short) wh.getChannels(), wh.getSampleRate(),
(short) wh.getBitsPerSample(), output.length);
mwh.write(fo);
fo.write(output);
fo.flush();
fo.close();
// MusicPlayer
MediaPlayer mp = new MediaPlayer();
// Listeners
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
}
}); // Important
mp.reset();
mp.setDataSource(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+"birdbellhigh.wav");
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepare();
mp.start();
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
22050, AudioFormat.CHANNEL_OUT_DEFAULT,
AudioFormat.ENCODING_PCM_16BIT, output.length,
AudioTrack.MODE_STREAM);
audioTrack.play();
audioTrack.write(output, 0, output.length);