0 Replies Latest reply on Jun 15, 2011 11:46 AM by will streeter

    Changing  Position of byteArray problems

    will streeter

      I am developing an AIR application that enables a user to recording audio through a microphone, and then edit their recording. Simply recording the audio data into a byteArray and playing it back to the user unedited is not a problem.  A very strange problem begin, however, once I change the  start position of the byteArray. I must implement a scrubber that will enable the user to stop the sound in mid play back and start again from a different point.   Thus, I have a byteArray containing some 4 million bytes (4000000) , I begin the play back of the byteArray at 0 position and when I play it back from a starting position of 0 it plays back each time perfectly. However, when I change the starting position to start at 2 million (2000000) for instance, the play back of the byteArray from the position of 2 million to the end, works the first time. When I close the application and recompile and try it again, and create another  audio recording,  the playback starting from the 0 position of the byteArray  works perfectly, but the moment I change the starting position again to say 2 million out of million, all I get is hissing static when I play it back. Recompiling and repeating the process a third time, the play back from the 0 position is perfect, however, once again when I change the starting position of the byteArray, I get no sound at all.

      I started by just modifying the various examples that were used for showing how to playback recorded data from a byteArray that was demonstrated in the following project.

      http://download.macromedia.com/pub/developer/air/sample_apps/microphone.zip

      currentBufferingData.readBytes(recordedData, 0, currentBufferingData.length);

      changing the offset to a dynamic variable (currentVocalPosition) and subtracting the
      the currentVocalPosition from the total length of the byteArray for the length or amount of data to read.

      currentBufferingData.readBytes(recordedData, currentVocalPosition, (currentBufferingData.length-currentVocalPosition))

      as this did not work I tried using writeBytes and leaving of the length;

      recordedData.writeBytes(currentBufferingData,currentVocalPosition)


      All these different attempts yielded the same result:
      1st recompile: works
      2n recompile: static
      3rd reompile : silence nothing.





      Thinking that the byteArray was getting corrupted so how, I decided to create a clone of the byteArray each time I play it back. Thus, I create an array and assign the first index of the array with an empty byteArray to store an edited version of the original cloned copy of the original byteArray recording,
      this._dynamicArray[0]                   =  new ByteArray();

      I then create a second cloned copy of the original byteArray recording and assign it to the second index of the dynamic array.


      this._dynamicArray[1]                   =  this.micAssetMngr.getMicDataClone();

      I used this copy of the original byteArray to copy the parts of the byteArray that I want to playback. Thus, when a user has changed the current position of the byteArray, I take from this position to end of the byteArray as the portion of the sound I want to playback..

      Of course the results of this do not yield any better results. The same thing happens, it works the first time, and then after I recompile and try again all I get is hissing static when I change the starting position, and then of course on the third recompile and playback of new starting position I get nothing but silence.

      private function playRecordedData():void{
             this.playingVocalStarted  = false;
             if(this.beat && this.hasEventListener(CuePointEvent.CUE_POINT)){
      this.beat.removeEventListener(CuePointEvent.CUE_POINT,                                           onCuePoint);
             }

            if(this._dynamicArray.length>0){
        this._dynamicArray[0].position  = 0;
                    this._dynamicArray[0].clear();   
                    delete this._dynamicArray[0];
        this._dynamicArray.pop();
        System.gc();
             }

             this._dynamicArray[0]                   =  new ByteArray();
             this._dynamicArray[1]                   =  this.micAssetMngr.getMicDataClone();
             this._dynamicArray[0].clear();
             this._dynamicArray[0].length            = 0;
             this._dynamicArray[0].writeBytes(this._dynamicArray[1], this.currentVocalPosition);
             this._dynamicArray[0].position          = 0;

                this._dynamicArray[1].position          = 0;
                this._dynamicArray[1].clear();   
                delete this._dynamicArray[1];
                this._dynamicArray.pop();
                System.gc();

                /*2nd try
      this._dynamicArray[0]                   =  new ByteArray();
      this._dynamicArray[1]                   =  this.micAssetMngr.getMicDataClone();
      this._dynamicArray[0].clear();
      this._dynamicArray[0].length            = 0;
      this._dynamicArray[0].writeBytes(this._dynamicArray[1]);
      this._dynamicArray[0].position          = this.currentVocalPosition;

      this._dynamicArray[1].position          = 0;
      this._dynamicArray[1].clear();   
      delete this._dynamicArray[1];
      this._dynamicArray.pop();
      System.gc();
      */

      /*3rd try
      this._dynamicArray[0]             =  new ByteArray();
      this._dynamicArray[1]             =  this.micAssetMngr.getMicDataClone();
      var length:uint                          =  this._dynamicArray[1].length - this.currentVocalPosition;
                  this._dynamicArray[1].position          =  this.currentVocalPosition;
      this._dynamicArray[0].clear();
      this._dynamicArray[1].readBytes(this._dynamicArray[0], 0, length);
      this._dynamicArray[0].position          = 0;

      if(this._dynamicArray.length>0){
                     this._dynamicArray[1].position     = 0;
         this._dynamicArray[1].clear();   
         delete this._dynamicArray[1];
         this._dynamicArray.pop();
         System.gc();
      }
             */

            /*4th try
      var temp:ByteArray      =  this.micAssetMngr.getMicDataClone();

                 while (temp.bytesAvailable >  this.currentVocalPosition){
                this._dynamicArray[0].writeFloat(temp.readFloat());
         }
                  */
                   this.effectDriver.effectInit();
      if(this.vocal && this.vocal.hasEventListener(Event.SOUND_COMPLETE)){
      this.vocal.removeEventListener(Event.SOUND_COMPLETE, onVocalPlaybackComplete);
      }
      if(this.vocal && this.vocal.hasEventListener(SampleDataEvent.SAMPLE_DATA)){
             this.vocal.removeEventListener(SampleDataEvent.SAMPLE_DATA, playSoundHandler);
      }

                 this.vocal = new SoundAssetManager();
      this.vocal.addEventListener(SampleDataEvent.SAMPLE_DATA,   playSoundHandler);
      this.vocal.recordedPlayBack();
      this.vocal.name                     = SoundAssetManager.PLAYING_VOCAL;
                 this.vocal.play(0);
                    this.vocal.addEventListener(Event.SOUND_COMPLETE, onVocalPlaybackComplete);
      }