AS3点击声音记录开始声音

2023-09-08 13:55:04 作者:十八闲客

目前,我正在录音和存储字节数组进行播放和随后的编码为MP3。

不幸的是,虽然我得到一个点击的声音在录音的开始。

我已经尝试了一些方法,试图消除这种如:

与mic.gain记录第一0.3秒= 0;然后设置mic.gain = 50;

后第1.3秒钟,然后继续写的ByteArray清除的字节数组(实际上删除第1.3秒的记录。)

不论这些方法已经停止所添加的点击

会有人有一个想法如何,我可以prevent点击被添加?

下面是我的记录/存储code:

 公共变种MIC:麦克风= Microphone.getMicrophone();
公共变种micSilence:单元;
私人VAR吵闹:ByteArray的=新的ByteArray();
私人VAR soundBA:ByteArray的=新的ByteArray();

mic.gain = 50;
mic.setSilenceLevel(1,2000);
mic.rate = 44;
Security.showSettings(2);
mic.setLoopBack(假);
mic.setUseEchoSup pression(假);

私有函数startRecordingAfterCountdown():无效{
    mic.addEventListener(的SampleDataEvent.SAMPLE_DATA,micSampleDataHandler);
}

私有函数micSampleDataHandler(事件:的SampleDataEvent):无效{
    而(event.data.bytesAvailable){
        VAR样:数= event.data.readFloat();
        soundBytes.writeFloat(样品);
    }
}

民营功能STO precord():无效{
    mic.removeEventListener(的SampleDataEvent.SAMPLE_DATA,micSampleDataHandler);
    soundBytes.position = 0;
    soundBA.clear();
    soundBA.writeBytes(吵闹);
    soundBA.position = 0;
    soundBytes.clear();

    VAR newBA:ByteArray的=新的ByteArray();
    newBA.writeBytes(soundBA);
    recordingsArray [0] = newBA;
}
 

解决方案

虽然我不能重现一下,我想这可能是由音量在录制开始时的急剧增加引起。因此有可能顺利通过增加的体积,以消除影响。事情是这样的:

容积

  //量增加时间(ms)
公共静态常量VOLUME_INC_TIME_MS:UINT = 200;
//以字节为单位
公共静态常量VOLUME_INC_BYTES:UINT = VOLUME_INC_TIME_MS * 44.1 * 4;

私有函数micSampleDataHandler(事件:的SampleDataEvent):无效
{
    VAR bytesRecorded:UINT = soundBytes.length;
    而(event.data.bytesAvailable)
    {
        VAR样:数= event.data.readFloat();
        如果(bytesRecorded< VOLUME_INC_BYTES)
        {
            //使用线性相关,但当然你也可以使用一个不同的
            VAR体积:数= bytesRecorded / VOLUME_INC_BYTES;
            soundBytes.writeFloat(样品*量);
            bytesRecorded + = 4;
        }其他
        {
            soundBytes.writeFloat(样品);
        }
    }
}
 
AS3基础教程 第34课 处理声音 下 SoundChannel

希望这有助于。

I'm currently recording sound and storing the byte array for playback and subsequent encoding to mp3.

Unfortunately though I am getting a click sound at the very beginning of the recording.

I have attempted a couple of methods to try to eliminate this such as:

recording the first .3 seconds with mic.gain = 0; then setting mic.gain = 50;

clearing the byteArray after the first .3 seconds then continuing to write the bytearray (in effect deleting the first .3 seconds of the recording.)

Neither of those methods has stopped the click being added.

Would anyone have an idea how I can prevent the click from being added?

Here is my recording/storing code:

public var mic:Microphone = Microphone.getMicrophone();
public var micSilence:uint;
private var soundBytes:ByteArray = new ByteArray();
private var soundBA:ByteArray = new ByteArray();

mic.gain = 50;
mic.setSilenceLevel(1, 2000);
mic.rate = 44;
Security.showSettings("2");
mic.setLoopBack(false);
mic.setUseEchoSuppression(false);

private function startRecordingAfterCountdown():void {      
    mic.addEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler);            
}

private function micSampleDataHandler(event:SampleDataEvent):void {     
    while (event.data.bytesAvailable){
        var sample:Number = event.data.readFloat();
        soundBytes.writeFloat(sample);
    }
}

private function stopRecord():void {        
    mic.removeEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler);     
    soundBytes.position = 0;
    soundBA.clear();
    soundBA.writeBytes(soundBytes);
    soundBA.position = 0;
    soundBytes.clear();

    var newBA:ByteArray = new ByteArray();
    newBA.writeBytes(soundBA);
    recordingsArray[0] = newBA;     
}

解决方案

Though I can't reproduce the click, I think it might be induced by a sharp increase of sound volume at the beginning of the recording. So it might be possible to eliminate the effect by increasing the volume smoothly. Something like this:

// the amount of volume increasing time in ms
public static const VOLUME_INC_TIME_MS:uint = 200;
// in bytes
public static const VOLUME_INC_BYTES:uint = VOLUME_INC_TIME_MS * 44.1 * 4;

private function micSampleDataHandler(event:SampleDataEvent):void
{
    var bytesRecorded:uint = soundBytes.length;
    while( event.data.bytesAvailable )
    {
        var sample:Number = event.data.readFloat();
        if( bytesRecorded < VOLUME_INC_BYTES )
        {
            // using linear dependence, but of course you can use a different one
            var volume:Number = bytesRecorded / VOLUME_INC_BYTES;
            soundBytes.writeFloat(sample * volume);
            bytesRecorded += 4;
        }else
        {
            soundBytes.writeFloat(sample);
        }
    }
}

Hope this helps.