3

I'm trying to develop an Android aplication that uses some sounds and mix them together for creating music compositions. At the beggining I tried just to reproduce sounds at the same time, but when I was doing that, they got disinchronized, and after several hours reading forums I realized that the best way to reproduce several sounds at the same time in loop mode is just to merge the audio files in one and then reproduce them as a single file.

Here is the code I'm using:

        //Oppening file 1
        InputStream is = getAssets().open("sounds/sound_1.ogg");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        //End Oppening file 1

        //Oppening file 2
        InputStream is_2 = getAssets().open("sounds/sound_2.ogg");
        int size_2 = is_2.available();
        byte[] buffer_2 = new byte[size_2];
        is_2.read(buffer_2);
        is_2.close();
        //End Oppening file 2

        //Get Bigger size and Merge Files
        int total_size =0;

        byte[] buffer_final;
        if(buffer.length>buffer_2.length){
            total_size = buffer_2.length;   
        }else{
            total_size = buffer.length-1;
        }

        buffer_final= new byte[total_size];

        for(int i=30; i<total_size-30;i++){


            float a =buffer[i];
            float b =buffer_2[i];
            float c = (a+b)/2;
            buffer_final[i]=(byte) c;      

        }

        FileOutputStream fos = new FileOutputStream(f);        
        fos.write(buffer_final);
        fos.close();

And after that, using soundPool I read the resulting file and try to reproduce it.

If i use this formula without merging the files (so, using only one sound, creating a new file, and then reading it, it works perfectly, so I assume that the problem is in the file merging, that maybe is not as easy as just adding the bytes together and then divide by 2.

After doing this, the log tells me this:

Unable to load sample (null).

And ofc, when I try to reproduce the log says that the sample is not ready.

Any help about this please? I've been googleing for it for weeks and I couldn't solve it ...

It's my first time programming Android, and also Audio so I'm a bit lost.

The question would be, how can I mix this sounds? I'm I in the right way for doing it? Where is the problem?

Thanks for reading and helping :)

TurboTi
  • 111
  • 2
  • 6

2 Answers2

1

If you were using raw PCM data, averaging the samples would work fine. What you need to do is convert the OGG file into a PCM buffer first. If you'd like to keep the output as OGG, you'll need to convert that back after you're done.

My experience is mostly with MP3 libraries rather than OGG, but I hear good things about JOrbis.

Geobits
  • 22,218
  • 6
  • 59
  • 103
  • Actually I don't care abot the format (it can be .mp3, wav, ogg, ...) anythig. The main thing is try to mix two sounds and be able to reproduce this final result in a loop. So, which is the best way to do that? Thanks! – TurboTi Oct 04 '12 at 09:35
  • Well, no matter what format it is, you need to get the raw(PCM) sound. Some formats are easier than others. For instance, WAV is mainly PCM with some header info, so getting the raw data from that isn't much problem. Some(MP3,OGG,etc) are compressed/encoded, so you'll need a library to convert them to raw audio. – Geobits Oct 04 '12 at 11:31
  • Once you have the *raw* audio in a buffer, you can add/divide them like you were trying. Again, though, you'll have to do some reconverting with the result if you want it to play on most players. – Geobits Oct 04 '12 at 11:32
  • Thank you all, and sorry about my late answer, at the end I could manage to reproduce several sounds at the same time without latency using audiopool so I didnt' have to merge them at all :) So the problem for me is solved!! – TurboTi Oct 17 '12 at 07:43
  • @TurboTi - hi, could u please explain how u did it using audiopool? In android I know only SoundPool... – Braj Jan 10 '13 at 11:25
  • @TurboTi hi,please explain how did u merge two audio file ? – sandeep Feb 11 '13 at 10:12
  • Guys I'm sorry about my late answer, I was outside the country till now! Basically I implemented a timer manually, that calculates the lenght of the largest file, and uses it for seting it as if it was a clock. Then, at the beggining of each iteration, I play all the sounds at the same time using the soundPool, this makes the effect of creating a loop that's not delayed and it works perfectly with at least 15 sounds : ) If it's not clear (since I'm writting this in the morning and I just woke up hehe... ) just tell me and I'll try to send you a diagram or sthing! :) – TurboTi Feb 13 '13 at 07:01
  • @TurboTi thanks for ur answer.but i need to implement audio mixer just like above code but not like sound pool.By using the above code i got the output but it is giving blur sound with out any music or voice. – sandeep Feb 13 '13 at 07:11
  • @sandeep Have you solved your problem? I need the same functionality as you needed.So please help me. – Arun Badole Jun 07 '13 at 06:08
0

In this way

soundPool = new SoundPool(NUM_SOUNDS, AudioManager.STREAM_MUSIC, 0);

NUM_SOUNDS is the number sounds soundPool can play togheter.

Boris Karloff
  • 1,190
  • 12
  • 20