Android Question mix multiple sound files

mohsen nasrabady

Active Member
Licensed User
Longtime User
i found this code for java anybody can convert it to b4a?

B4X:
    byte[] output = new byte[(music1.length > music2.length) ? music2.length
            : music1.length];
    for (int i = 0; i < output.length; i++) {

        float samplef1 = music1[i] / 128.0f; // 2^7=128
        float samplef2 = music2[i] / 128.0f;

        float mixed = (samplef1 + samplef2) / 2;

                if (mixed > 1.0f)
            mixed = 1.0f;

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

        byte outputSample = (byte) (mixed * 128.0f);
        output[i] = outputSample;
    }
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
In what format are your files ?
Explanation of the code above:
- Transform the samples of each signal from Short to Float: float samplef1 = music1 / 128.0f; (between -1 to 1)
- Add the two values and divide by 2 : float mixed = (samplef1 + samplef2) / 2;
- Limit the signals to 1 and -1
if (mixed < -1.0f)
mixed = -1.0f;

- Transform the sum sample back to Short: byte outputSample = (byte) (mixed * 128.0f);
 
Upvote 0

mohsen nasrabady

Active Member
Licensed User
Longtime User
hi thnx for your help
i have no information about music but most of my files is wav
i think in forum that i copied the code from is about mix wav files
 
Upvote 0
Top