13 Replies Latest reply on Jan 14, 2018 10:33 AM by Robert Mc Dowell

    About SoundMixer.computeSpectrum()

    Robert Mc Dowell Level 1
      • Operating system version: ALL
      • Flash Player version : ALL
      • Explain your problem in step-by-step detail if possible

      Hi,

       

      I'm currently trying to grab the byteArray from SoundMixer.computeSpectrum()

      for further raw audio data treatment, but actually after tried many loop (enterframe,timer)

      short, long changed the samplerate I just get a tiny fraction of audio every frame,

      maybe 10ms, nothing more and thus with all type of sound, RTMP, Sound Object() etc..

       

      any clue?

       

      thanks

       

      • A direct link to a web page that demonstrates the problem: NONE
        • 1. Re: About SoundMixer.computeSpectrum()
          kglad Adobe Community Professional & MVP

          are you looping through all 512 readFloats?

           

          SoundMixer.computeSpectrum(byteA,true,3);

           

          for (var k:int=0; k<255; k++) {

          byteA.readFloat();

          }

          for (var m:int=0; m<255; m++) {

          byteA.readFloat();

          }

          • 2. Re: About SoundMixer.computeSpectrum()
            Robert Mc Dowell Level 1

            Hi Kglad,

             

            until now I didn't do 2 loops for L and R

            I did

            for (var m:int=0; m<512; m++) {

                 byteA.readFloat();

            byteA.readFloat();

            }

             

            ok I try your suggestion, what is your advice to get those 512 bytes correctly?

            TImer,setInterval,enterFrame?

             

            Also I want to be clear that the byteArray wont' be used for further graphical representation

            but use the pure raw data to apply some audio effect like pitch or else.

            so it should be SoundMixer.computeSpectrum(byteA,false,0); right?

            • 3. Re: About SoundMixer.computeSpectrum()
              kglad Adobe Community Professional & MVP

              your code is problematic: you're trying to read 1024 values from a bytearray that has 1/2 those values.

               

              use the code i suggested.  the first loop reads the left channel values and the 2nd loop reads the right channel values.  i typically store the values in arrays that i then use as i wish:

               

              var k:int;

              var m:int;

              var obj:Object={};

              obj.leftA=[];

              obj.rightA=[];

               

              function loopF():void{

              SoundMixer.computeSpectrum(byteA,true,3);

              obj.leftA.length=0

              obj.rightA.length=0

               

              for (k=0; k<255; k++) {

              obj.leftA.push(byteA.readFloat());

              }

              for (m=0; m<255; m++) {

              obj.rightA.push(byteA.readFloat());

              }

              // do whatever

              }

              • 4. Re: About SoundMixer.computeSpectrum()
                Robert Mc Dowell Level 1

                Same results

                 

                I also tried a cleaner code like

                   var byteArray:ByteArray = new ByteArray();
                  

                var spectrumArray:Array = new Array();

                SoundMixer.computeSpectrum(byteArray,false,_spectrumRate);

                   for(var l:int=0;l<255;l++){
                   _spectrumArray[l] = new Array(byteArray.readFloat(),0);
                   }
                   for(var r:int=0;r<255;r++){
                   _spectrumArray[r][1] = byteArray.readFloat();
                   }

                 

                when I play the spectrumArray I get very small clics in the audio

                I used the loop in enterframe at 30fps, Timer at 50 ms, same result..

                • 5. Re: About SoundMixer.computeSpectrum()
                  Robert Mc Dowell Level 1

                  a good test that would help to solve the issue:

                  read the spectrumByteArray into a sound object and play it.

                  • 7. Re: About SoundMixer.computeSpectrum()
                    kglad Adobe Community Professional & MVP

                    i don't download and correct files unless i'm hired.  however, someone else may do that for free if you're patient.

                    • 8. Re: About SoundMixer.computeSpectrum()
                      Robert Mc Dowell Level 1

                      it's online... you don't need to download.. it's just for test

                      and hear the result of the sound in flv

                      • 9. Re: About SoundMixer.computeSpectrum()
                        Robert Mc Dowell Level 1

                        kglad,

                         

                        here is a simple example taken from adobe doc with a sound object added to play the raw audio data coming from SoundMixer.computeSpectrum()

                         

                        import flash.display.Graphics;

                        import flash.events.Event;

                        import flash.media.Sound;

                        import flash.media.SoundChannel;

                        import flash.media.SoundMixer;

                        import flash.net.URLRequest;

                         

                        const PLOT_HEIGHT:int = 200;

                        const CHANNEL_LENGTH:int = 256;

                         

                        var timer:Timer = new Timer(44,0);

                        timer.addEventListener(TimerEvent.TIMER,onEnterTime);

                         

                        var snd:Sound = new Sound();

                        var req:URLRequest = new URLRequest("piano.mp3");

                        snd.load(req);

                         

                        var channel:SoundChannel = new SoundChannel();

                        snd.addEventListener(Event.SOUND_COMPLETE,onPlaybackComplete);

                         

                        var bytes:ByteArray = new ByteArray();

                        var beep:BeepSound = new BeepSound();

                        var spectrumByteArray:ByteArray = new ByteArray();

                        spectrumByteArray.endian = Endian.LITTLE_ENDIAN;

                        var globalSound:Sound = new Sound();

                        var globalChannel:SoundChannel = new SoundChannel();

                         

                        play_btn.addEventListener(MouseEvent.CLICK,doPlay);

                        stop_btn.addEventListener(MouseEvent.CLICK,doStop);

                        beep_btn.addEventListener(MouseEvent.CLICK,doBeep);

                        replay_btn.addEventListener(MouseEvent.CLICK,doReplay);

                        replayStop_btn.addEventListener(MouseEvent.CLICK,doReplayStop);

                         

                        function doPlay(event:MouseEvent):void{

                            //addEventListener(Event.ENTER_FRAME,onEnterFrame);

                            timer.start();

                            channel = snd.play();

                        }

                         

                        function doStop(event:MouseEvent):void{

                            channel.stop();

                            timer.stop();

                            //removeEventListener(Event.ENTER_FRAME,onEnterFrame);

                        }

                         

                        function doBeep(event:MouseEvent):void{

                            beep.play();

                        }

                         

                        function doReplay(event:MouseEvent):void{

                            spectrumByteArray.position = 0;

                            globalSound.loadPCMFromByteArray(spectrumByteArray,spectrumByteArray.length/8,"float",tru e,44100.0);

                            globalChannel = globalSound.play();

                        }

                         

                        function doReplayStop(event:MouseEvent):void{

                            globalChannel.stop();

                            spectrumByteArray.position = 0;

                        }

                         

                        //function onEnterFrame(event:Event):void{

                        function onEnterTime(event:TimerEvent):void{

                         

                            var bytes:ByteArray = new ByteArray();

                            var tmpArray:Array = new Array();

                         

                            SoundMixer.computeSpectrum(bytes,false,0);

                            trace(bytes.bytesAvailable);

                            for(var L:int=0;L<255;L++){

                                tmpArray[L] = [bytes.readFloat()];

                            }

                            for(var R:int=0;R<255;R++){

                                tmpArray[R][1] = bytes.readFloat();

                            }

                            for(var e:int=0;e<tmpArray.length;e++){

                                spectrumByteArray.writeFloat(tmpArray[e][0]);

                                spectrumByteArray.writeFloat(tmpArray[e][1]);

                            }

                            trace(spectrumByteArray.length);

                            ///////////////////////////////

                          

                            bytes.position = 0;

                            var g:Graphics = this.graphics;

                            g.clear();

                            g.lineStyle(0,0x6600CC);

                            g.beginFill(0x6600CC);

                            g.moveTo(0,PLOT_HEIGHT);

                            var n:Number = 0;

                            // left channel

                            for(var i:int = 0; i < CHANNEL_LENGTH; i++){

                                n =(bytes.readFloat() * PLOT_HEIGHT);

                                g.lineTo(i * 2,PLOT_HEIGHT - n);

                            }

                            g.lineTo(CHANNEL_LENGTH * 2,PLOT_HEIGHT);

                            g.endFill();

                            // right channel

                            g.lineStyle(0,0xCC0066);

                            g.beginFill(0xCC0066,0.5);

                            g.moveTo(CHANNEL_LENGTH * 2,PLOT_HEIGHT);

                            for(i = CHANNEL_LENGTH; i > 0; i--){

                                n =(bytes.readFloat() * PLOT_HEIGHT);

                                g.lineTo(i * 2,PLOT_HEIGHT - n);

                            }

                            g.lineTo(0,PLOT_HEIGHT);

                            g.endFill();

                        }

                        function onPlaybackComplete(event:Event){

                            doStop(null);

                        }

                         

                        //////////////////

                         

                        I'm afraid that grab 256 samples from each channel and try to play it with Sound object is impossible.

                        I tried the timer event at 1,10,100 msec with same results, a tiny portion of audio is heard.

                        Do you think it's worth to update the doc and say that SoundMixer.computeSpectrum() is not made to manipulate the sample data

                        but it's just for graphical representation? or am I missing a piece of the puzzle?

                         

                        thanks

                        • 10. Re: About SoundMixer.computeSpectrum()
                          kglad Adobe Community Professional & MVP

                          i've only used to for graphics:  http://www.kglad.com > snippets > sound display

                          • 11. Re: About SoundMixer.computeSpectrum()
                            Robert Mc Dowell Level 1

                            yes, it's quite confusing to have a class SoundMixer without any SoundMixer export stereo method.

                            a SoundMixer.export("float",[stereo/mono],samplerate) = global bytearray mix chunk would be fantastic.

                            any link where we can suggest new feature on adobe labs?