4 Replies Latest reply on Jul 16, 2010 2:59 PM by Kevin Goldsmith

    Mix Audio

    jochendd

      I am building a Flash Application which will allow a user to mix two mp3 files and send the mixed result to our server.

       

      Using Pixel Bender I am already able to create a mix:

       

      public class Mixer
      {
         [Embed(source="mix.pbj", mimeType="application/octet-stream")]
      protected var NewFilter:Class;
         private var shader:Shader;
         private var shaderJob:ShaderJob;
         protected var _output:ByteArray;
         shader = new Shader(new NewFilter() as ByteArray);

       

         private function onSampleDataHandler(event:SampleDataEvent):void
         {
              var width:int = 1;
              var height:Vector.<int> = new Vector.<int>(numOfTracks);
              for (var i:int = 0; i < numOfTracks; i++)
              {
                  buffer[i] = new ByteArray();
                  sound[i].extract(buffer[i],BUFFER_SIZE * 4);
                  height[i] = buffer[i].length >> 4;
                  buffer[i].position = 0;
                  shader.data["src"+i]["input"] = buffer[i];
                  shader.data["src"+i]["width"] = width;
                  shader.data["src"+i]["height"] = height[i];
              }

       

              shaderJob = new ShaderJob(shader, event.data,width, height[0]);
           shaderJob.addEventListener(Event.COMPLETE, onShaderJobComplete, false, 0, true);
              shaderJob.start(false);

       

      protected function onShaderJobComplete(event:ShaderEvent):void
      {
              // event.byteArray is the result

       

      }

       

      Not sure if the event.byteArray can be transformed to an mp3 file? Ideally we'd send the mp3 to our server for uploading.

        • 1. Re: Mix Audio
          Kevin Goldsmith Level 3

          I have seen mp3 encoders written in actionscript. Googling "Actionscript MP3 Encoder" turns up one as the first result. So it is possible. There isn't one that is part of the Adobe Actionscript APIs however.

          1 person found this helpful
          • 2. Re: Mix Audio
            jochendd Level 1

            Hmm according to me, the code I posted is not correct. Ideally the code should mix the 2 audio files in one job, right now it's mixing on every SampleDataEvent .

             

            I'm looking for something like:

             

            var ba:ByteArray = new ByteArray();
            ba.endian = Endian.LITTLE_ENDIAN;

            sound[0].extract( ba, 44100 * 2 );

             

            var ba1:ByteArray = new ByteArray();
            ba1.endian = Endian.LITTLE_ENDIAN;

            sound[1].extract( ba1, 44100 * 2  );

             

            // mix ba and ba1 together...

             

            shaderJob = new ShaderJob(shader, ba,width, height[0]);

            shaderJob.start();

             

            // capture bytearray result of the entire mix

             

            // send to server

             

            any ideas?

            • 3. Re: Mix Audio
              Kevin Goldsmith Level 3

              I read your question as specifically asking about MP3 encoding, not about the processing itself.

               

              It is pretty simple to append the results of each SampleDataEvent to a separate ByteArray. When you are done processing the input, this new ByteArray will have the entire audio file.

              • 4. Re: Mix Audio
                Kevin Goldsmith Level 3

                The ShaderJob would work, if you had the entire MP3 file decoded into a byte array of audio samples before you passed it to the shaderJob.