Reputation: 2496
I did test mix two PCM audio file. but don't get true audio file.
I used this example So, my code:
private void mixSound() throws IOException {
byte[] music1 = null;
music1 = new byte[in1.available()];
music1 = convertStreamToByteArray(in1);
in1.close();
byte[] music2 = null;
music2 = new byte[in2.available()];
music2 = convertStreamToByteArray(in2);
in2.close();
byte[] output = new byte[music1.length];
for (int i = 0; i < output.length; i++) {
samplef1 = music1[i] / 128.0f;
samplef2 = music2[i] / 128.0f;
float mixed = samplef1 + samplef2;
// 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
save = openFileOutput(filename, Context.MODE_PRIVATE);
save.write(output);
save.flush();
save.close();
}
public byte[] convertStreamToByteArray(InputStream is) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buff = new byte[8000];
int i;
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
}
2 audio files with bit rate 64000 & sampling rate 16000 GH & sterio
in1 = getResources().openRawResource(R.raw.a_2);
in2 = getResources().openRawResource(R.raw.a_diz_2);
Also try to convert
bytes array to short array -> then calculate-> then convert short to byte
using converts methods
like bytes2Shorts(byte[] buf) and shorts2Bytes(short[] s).
But steel have a fail result.
Someone can say me Where is my wrong?
Upvotes: 1
Views: 2211
Reputation: 2119
There are a number of issues here and I'll try to address some of them
First, using byte[]
suggests that your PCM wave data format
is AudioFormat.ENCODING_PCM_8BIT
(or it should be this format if it already isn't). This format uses 8-bit (1 byte) unsigned
, which means that the sound
samples are stored in the [0, 255]
range (not in the [-127, +128] or [-128,+127]
range).
This means that the negative values are in the [0, 127]
range and the positive samples are in the [128,255]
range.
When mixing values, it's best to prevent clipping
right from the start so I'd use
byte mixed = (music1[i] + music2[i])/2; //this ensures that mixed remains within the `correct range` for your PCM format
You can also divide your samples by 128 (if you want to convert them to floating point values)
float samplef1 = (((float)music1[i]-127)/128 ; //converting samples to [-1, +1] range -- -1 corresponds a sample value of 0 and +1 to 255
float samplef2 = (((float)music2[i]-127)/128;
float mixed = (samplef1+samplef2)/2;
Note that you now have 2 options to play data(samples) generated in this way. Either, convert floats
back to bytes
or use the AudioFormat.ENCODING_PCM_FLOAT
format.
audio files with bit rate 64000 & sampling rate 16000 GH & sterio
This can't be correct. Typical sampling rates are 4000Hz, 8000Hz, 11000Hz, 16000Hz, 22050Hz or 44100Hz
. For bit depths, audio usually uses 8 bits, 16 bits or 32 bits
.
For instance, CD quality audio uses 44100Hz, 16bit, stereo
format.
Upvotes: 2