mixing 2 sounds from ByteArray

前端 未结 3 557
逝去的感伤
逝去的感伤 2021-01-05 17:36

I have build a mixer and save all the sequence in an array and then play it again, now I want to save the mix as an MP3, I have looked for any way to save the mix and the an

相关标签:
3条回答
  • 2021-01-05 17:56

    if you have uncompressed audio, you can just add up the values of individual array elements in your ByteArray's. But you also have to handle capping for max/min values (no overflows). Pretty sure there is nothing equivalent for mixing MP3s - so you might have to decode, mix, and encode to MP3 again.

    0 讨论(0)
  • 2021-01-05 18:06

    If I understand your question properly, you need read the floating point data for each sample, sum them, and write the resulting value into your new audio stream. This would give a stright 50/50 mix.

    I don't have access to a machine with a compiler right now, but it should be something like this (assuming bytes1 and bytes2 are two ByteArray objects of equal length):

    for (int bcnt = bytes1.size(); bcnt; bcnt--)
        bytes1.setByte(bcnt - 1, bytes2.getByte(bcnt - 1) + bytes1.getByte(bcnt - 1));
    

    Of course, you would probably want to do some sort of overflow check, but that should be enough to put you on the right track.

    0 讨论(0)
  • 2021-01-05 18:07

    been working on a similar system for a while, I'll do my best to give you some direction:

    Your example code is not really mixing the MP3's - it's creating 2 more sounds to playback the loaded MP3's via the SampleDataEvent. What you need to do is create just one "output" Sound file that will hold/playback the resulting mixed sound. You can easily save that data as it happens and subsequently save that file as a new WAV/MP3/what-have-you.

    real/psuedo-code (read:lazy) :

    output = new Sound();
    
    output.addEventListener( SampleDataEvent.SAMPLE_DATA , mixAudio );
    
    song1 = new Sound / load the first mp3
    
    song2 = new Sound / load the second mp3
    
    // a byteArray for "recording" the output and subsequent file creation
    recordedBytes = new ByteArray();
    

    either wait until both mp3's are completely loaded, or run an enter-frame to determine when both Sounds are no longer buffering (Sound.isBuffering )

    when the mp3's are ready:

    // numbers to store some values
    
    var left1:Number;
    var right1:Number;
    
    var left2:Number;
    var right2:Number;
    
    // bytearrays for extracting and mixing
    var bytes1:ByteArray = new ByteArray( );
    var bytes2:ByteArray = new ByteArray( );
    
    // start the show
    output.play();
    
    
    function mixAudio( e:SampleDataEvent ):void{
      //set bytearray positions to 0
    
      bytes1.position = 0;
      bytes2.position = 0;
    
      //extract
    
      song1.extract( bytes1, 8192 );
      song2.extract( bytes2, 8192 );
    
      // reset bytearray positions to 0 for reading the floats
    
      bytes1.position = 0;
      bytes2.position = 0;
    
      // run through all the bytes/floats
    
      var b:int = 0;
    
      while( b < 8192 ){
    
         left1 = bytes1.readFloat();  // gets "left" signal
         right1 = bytes1.readFloat(); // gets "right" signal
    
         left2 = bytes2.readFloat();
         right2 = bytes2.readFloat();
    
    
         // mix!
    
         var newLeft:Number = ( left1 + left2 ) * .5; 
         var newRight:Number = ( right1 + right2 ) * .5;
    
         // write the new stuff to the output sound's
    
         e.data.writeFloat( newLeft );
         e.data.writeFloat( newRight );
    
         // write numbers to the "recording" byteArray
         recordedBytes.writeFloat( newLeft );
         recordedBytes.writeFloat( newRight );
    
         b++;
    
      }
    }
    

    Yes - you should really cap the possible output at -1/1. Do it. This is extremely un-optimized!

    Ok. so that's the easy part! The tough part is really converting the final byteArray to MP3. The audio exists within Flash as PCM/uncompressed data, MP3 is obviously a compressed format. This "answer" is already way too long and all this info I've gleaned from several very smart folks.

    You can easily adapt 'MicRecorder' to be a generic Sound data recorder: http://www.bytearray.org/?p=1858

    converting to MP3 will be a bitch: Thibault has another post on ByteArray.org - search for LAME MP3.

    Excellent example/resource: http://www.anttikupila.com/flash/soundfx-out-of-the-box-audio-filters-with-actionscript-3/

    Look up Andre Michelle's open source 'Tonfall' project on Google code.

    Look up Kevin Goldsmith's blog and labs - he's got great example on utilizing Pixel Bender with all this madness.

    hope this helps!

    PS - taking a cue from Andre, the optimal length of the audio buffer should be 3072. Give it a try on your machine!

    0 讨论(0)
提交回复
热议问题