AS3麦克风录音机 - 清除旧数据

时间:2014-06-11 23:16:16

标签: actionscript-3

我正在使用来自http://www.jordansthings.com/blog/?p=5的优秀课程,该课程运作良好。

我遇到的麻烦是,如果用户进行2次录制,则第二次录制时会有第一次录制,因此数据不会被清除。

我尝试了很多东西,将buffer设置为null,将player设置为null。在Jordans的评论中,他还被要求清除旧数据。

任何人都看过这个并且知道如何清除旧内容?

由于

public function startRecording():void
    {           
        if (!recording) 
        {
        //  TweenMax.to(btnrec, .3, {glowFilter:{color:0xFF0000, alpha:1, blurX:50, blurY:50}} )
            recorder.record() 

        } else if (recording) {
            recorder.stop()
            recording = false
            buttonTxt = "Start Recording";
        //  TweenMax.to(btnrec, .3, {glowFilter:{color:0xFF0000, alpha:0, blurX:10, blurY:10}} )                
        }
    }
    public function onRecording(e:RecordingEvent):void
    {           
        statusTxt = "make some noise!";
        buttonTxt = "Stop Recording";
        var al:Number = recorder.microphone.activityLevel;
    //  TweenMax.to(soundMeter, .1, {scaleX:al * .01, onUpdate:onActivitylevelUpdate});//, onUpdateParams:[al]})
        if (!recording) recording = true;
    }       
    public function onActivitylevelUpdate(al):void
    {
        //statusTxt = _activityLevel
        // draw a cool sine wave!
        xpos += speedX;
        ypos = centerY + Math.sin(angle) * amplitude * ((al > 20)? al / 100 : 1)
        angle += speedAngle;
        graphics.lineTo(xpos,ypos)
    }
    private function onRecordComplete(e:Event):void
    {
    //  soundMeter.scaleX = 0

        recording = false;
        statusTxt = "recording complete"    

        var src = WaveFile.createSample(recorder.output) // this is fine

        // I think im not clearing out the old audio properly here somehow...
        var sequence = new ListPerformance()
        sequence.addSourceAt(0, src)
        var ap = new AudioPerformer(sequence, new AudioDescriptor())
        //player.play(ap)

        renderWav(ap, true)

        // save to wav?
        // new FileReference().save (recorder.output, "VOCariousRecording.wav")
    }
    private function renderWav(src, convertToMp3 = false):void
    {
        var innerTimer = new Timer(10,0)
        var framesPerChunk:uint = 8192;

        innerTimer.addEventListener(TimerEvent.TIMER, handleRenderTimer)
        innerTimer.start()

        function handleRenderTimer(e:TimerEvent)
        {
            src.getSample(framesPerChunk).writeWavBytes(myWavData)

            var m = Math.min(src.frameCount, src.position + framesPerChunk)
            var n = Math.max(0, m - src.position)

            if (n == 0)
            {
                if (src.position > 0) finishRender() else trace("cancel rendering")

            } else {
             statusTxt = "rendering audio: "+ Math.floor(src.position * 100 / src.frameCount) + "%";
            }
        }               
        function finishRender():void
        {
            innerTimer.stop()
            statusTxt = "finishing audio render"
            WaveFile.writeBytesToWavFile(myWavFile, myWavData, 44100, 2, 16)

            if (!convertToMp3)
            {
                wavbtn.enabled = true;
            } else {
                makeIntoMp3(myWavFile)
            }
        }               
    }
    private function makeIntoMp3(wav):void
    {
        wav.position = 0
        mp3Encoder = new ShineMP3Encoder(wav);
        mp3Encoder.addEventListener(Event.COMPLETE, mp3EncodeComplete);
        mp3Encoder.addEventListener(ProgressEvent.PROGRESS, mp3EncodeProgress);
        //mp3Encoder.addEventListener(ErrorEvent.ERROR, mp3EncodeError);
        mp3Encoder.start(); 
    }

    function mp3EncodeProgress(e:ProgressEvent) : void 
    {
        statusTxt = "encoding mp3: " + e.bytesLoaded + "%"
    }

    function mp3EncodeComplete(e: Event) : void 
    {
        statusTxt = "mp3 encoding complete\n"
        uploadVoiceover()
    }

    function uploadVoiceover() : void 
    {
        var sba:ByteArray = mp3Encoder.mp3Data;
        sba.position =  sba.length - 128;
        sba.writeByte(57);  
        statusTxt = "Uploading to server"

        var fileUpload:FileUploader = new FileUploader();
        fileUpload.upload("1","Voice_Recording.mp3",sba);
        fileUpload.addEventListener(Event.COMPLETE, onUploaderComplete);

    }

        private function onUploaderComplete(event:Event):void
        {
            var uploader:FileUploader = FileUploader(event.target);

            var id:String = new Uuid().toString();
            var soundData:SoundData = Model.guideScene != null 
                ? SoundData.createForGuide(id, Model.guideScene.id)
                : SoundData.createForScene(id, Model.currentScene.id);
            soundData.file = uploader.fileData;
            soundData.ambience = 1;
            soundData.pan = 0;
            soundData.tilt = 0;
            soundData.volume = 1;
            soundData.title = "Voice Recording";

            statusTxt = "Hotspot added"


            var hotspot:HotspotData = soundData;

            if(Model.isEditableGuide)
                Model.guideScene.addHotspot(hotspot);

            else if(Model.isEditableScene)
                Model.currentScene.addHotspot(soundData);
        }

}

修改

问题类似乎就是这个

package com.noteflight.standingwave3.performance
{
    import __AS3__.vec.Vector;

    import com.noteflight.standingwave3.elements.*;

    /**
     * A ListPerformance is an ordered list of PerformableAudioSources, each of which possesses an onset relative to the
     * start of the performance.  The list is optimized for the case where elements are appended in order of start time.
     */
    public class ListPerformance implements IPerformance
    {

        private var _elements:Vector.<PerformableAudioSource> = new Vector.<PerformableAudioSource>;

        private var _dirty:Boolean = false;

        private var _frameCount:Number = 0;  

        private var _lastIndex:Number = 0;

        public function ListPerformance() {

        }
        /* added by jordan */
        public function removeSource(elementnum:int):void
        {
            _elements.length = 0;
            _elements.splice(elementnum, 1)
        }
        /**
         * Add a Performance Element to this Performance. 
         */
        public function addElement(element:PerformableAudioSource):void
        {

            if ((! _dirty)
                && _elements.length > 0
                && element.start < _elements[_elements.length - 1].start)
            {
                // If we add an element which is out of order, note that we will have
                // to re-sort the performance later.
                //
                _dirty = true;
            }
            _elements.push(element);

            // Maintain a cached duration for the overall performance.  Note that
            // the "long straw" element whose end determines the performance end may
            // not be the last element.
            //
            _frameCount = Math.max(_frameCount, element.end);
        }

        /**
         * Add an IAudioSource to this performance, to start at a particular start time.
         */
        public function addSourceAt(startTime:Number, source:IAudioSource, gain:Number=0, pan:Number=0):void
        {
            addElement(new PerformableAudioSource(startTime, source, gain, pan));
        }

        /**
         * The list of PerformableAudioSources within this Performance, sorted by onset. 
         */        
        public function get elements():Vector.<PerformableAudioSource>
        {
            ensureSorted();
            return _elements;
        }

        /**
         * The start of the last performance element in the Performance. 
         */
        public function get lastStart():Number
        {
            var el:Vector.<PerformableAudioSource> = elements;
            return (el.length == 0) ? 0 : el[el.length-1].start;
        }

        /**
         * The frame count of the entire Performance. 
         */
        public function get frameCount():Number
        {
            return _frameCount;
        }

        //
        // IPerformance interface implementation
        //

        /**
         * @inheritDoc 
         */        
        public function getElementsInRange(start:Number, end:Number):Vector.<PerformableAudioSource>
        {
            // This makes use of _lastIndex as a memory of what was last queried to optimize
            // the search for the first matching element, since queries will in general run
            // in forward order.
            //
            var el:Vector.<PerformableAudioSource> = elements;
            var result:Vector.<PerformableAudioSource> = new Vector.<PerformableAudioSource>();             
            _lastIndex = Math.max(0, Math.min(_lastIndex, el.length - 1));

            // back up if prior element is ahead of starting frame
            while (_lastIndex > 0 && el[_lastIndex - 1].start >= start)
            {
                _lastIndex--;
            }

            // advance if our current element is prior to starting frame
            while (_lastIndex < el.length && el[_lastIndex].start < start)
            {
                _lastIndex++;
            } 

            // Return elements that start in this time window (and may also end in it)
            while (_lastIndex < el.length && el[_lastIndex].start < end)
            {
                result.push(el[_lastIndex++]);
            }

            return result;
        }

        public function clone():IPerformance
        {
            var p:ListPerformance = new ListPerformance();
            for each (var element:PerformableAudioSource in elements)
            {
                p.addElement(new PerformableAudioSource(element.startTime, element.source.clone()));
            }
            return p;
        }

        private function ensureSorted():void
        {
            if (_dirty)
            {
                _elements.sort(sortByStart);
                _dirty = false;
            }
        }

        private static function sortByStart(a:PerformableAudioSource, b:PerformableAudioSource):Number
        {
            var aStart:Number = a.start;
            var bStart:Number = b.start;
            if(aStart > bStart)
            {
                return 1;
            }
            else if(aStart < bStart)
            {
                return -1;
            }
            else
            {
                return 0;
            }
        }
    }
}

Jordan尝试用他在顶部添加的类清除序列(removeSource)我试图对此进行处理,但无法使序列返回到零,并在录制之前删除旧数据一个新的。

任何人都知道如何实现这一目标?

1 个答案:

答案 0 :(得分:0)

这样做了

myWav Data = new Byte Array(); WavFile = new ByteArray();