Flash 10.1 AS3 - 将实时效果应用于麦克风 - 口吃问题

发布于 2024-10-17 00:27:39 字数 501 浏览 5 评论 0原文

我正在尝试编写一个闪存应用程序,它采用麦克风流并应用实时效果并将其输出回扬声器。

我发现从麦克风获取输出时遇到口吃问题,将其复制到 ByteArray amd 中,然后使用单独的

声音 = new Sound();

sound.addEventListener(SampleDataEvent.SAMPLE_DATA, processSound);

声音.play();

从此 ByteArray 读取并播放声音。

我注意到麦克风的 bytesAvailable 的输入发生了变化,而且两个事件(麦克风的 SAMPLE_DATA 和声音的 SAMPLE_DATA)并没有像需要的那样触发 ABABABAB,而是更加随机。

我是否正确地认为 mic.SAMPLE_DATA 事件以不同的时间间隔触发不同数量的数据,并且工作实现需要读取可用数据并缓冲输入,以便 Sound SampleDataEvent 始终有一些内容可以播放以避免馅料?

I'm trying to write a flash application which takes a Microphone stream and applies realtime effects and outputs this back to the speakers.

I'm finding I'm having problems with stuttering when taking the output from the mic, copying this into a ByteArray amd then using a seperate

sound = new Sound();

sound.addEventListener(SampleDataEvent.SAMPLE_DATA, processSound);

sound.play();

to read from this ByteArray and play back the sound.

I have noticed that the input from the mic's bytesAvailable changes, and also that the two events (the mic's SAMPLE_DATA and the sound's SAMPLE_DATA) aren't firing A B A B A B A B like would be needed but is more random.

Am I right in thinking that the mic.SAMPLE_DATA event fires at different intervals with different amounts of data and a working implementation would need to read the available data in and buffer the input so that the Sound SampleDataEvent would always have something to play back to avoid the stuffering?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北凤男飞 2024-10-24 00:27:39

我找到了一个解决方案,所以我想我会将其发布在这里,以防其他人遇到类似的问题(许多谷歌搜索对我来说没有任何结果)。

麦克风的输入似乎不一致,并且需要发送所有字节才能正确处理声音。让它工作的关键是使用数组来缓冲输入。

private var mic:Microphone;
private var micBuffer:Array;
private var playBackSound:Sound;
//the effect I was applying needed to be applied to 
private var _leftChannel:Vector.<Number> = new Vector.<Number>(8192/2);

private function init() {
    micBuffer = new Array();
    mic = Microphone.getMicrophone();
    mic.rate = 22; 
    mic.addEventListener(SampleDataEvent.SAMPLE_DATA, micSampleData);
}

private function micSampleData(e:SampleDataEvent) {

    //store everything sent from the microphone into the array - this will vary in length
    while  (e.data.bytesAvailable > 0) micBuffer.push(e.data.readFloat());

    //can wait here for the array to reach a certain size if needed
    if (!playBackSound) {
        playBackSound = new Sound();
        playBackSound.addEventListener(SampleDataEvent.SAMPLE_DATA, processSound);
        playBackSound.play();
    }
}

private function playBackSoundSampleData(e:SampleDataEvent) {

    //this number will change depending on the sample rate of microphone
    var numberOfFloats = 8192/8;
    for (var i = 0; i<numberOfFloats && micBuffer.length > 0; i++) 
    {
        _leftChannel[i] = micBuffer.shift();
        count++;
    }

    //apply effect

    //sample rate here is half of 44 so write twice + twice again to make stereo
    for(var i = 0 ; i < count ; ++i )
    {
        e.data.writeFloat( _leftChannel[i] );
        e.data.writeFloat( _leftChannel[i] );
        e.data.writeFloat( _leftChannel[i] );
        e.data.writeFloat( _leftChannel[i] );
    }
}

I found a solution so thought I'd post it on here incase anyone else had similar problems (many google searches turned up nothing for me).

It does seem that the input from the mic is fed through inconsistently, and all the bytes sent through are needed in order to properly process the sound. The key to getting it to work was to use an array to buffer the input.

private var mic:Microphone;
private var micBuffer:Array;
private var playBackSound:Sound;
//the effect I was applying needed to be applied to 
private var _leftChannel:Vector.<Number> = new Vector.<Number>(8192/2);

private function init() {
    micBuffer = new Array();
    mic = Microphone.getMicrophone();
    mic.rate = 22; 
    mic.addEventListener(SampleDataEvent.SAMPLE_DATA, micSampleData);
}

private function micSampleData(e:SampleDataEvent) {

    //store everything sent from the microphone into the array - this will vary in length
    while  (e.data.bytesAvailable > 0) micBuffer.push(e.data.readFloat());

    //can wait here for the array to reach a certain size if needed
    if (!playBackSound) {
        playBackSound = new Sound();
        playBackSound.addEventListener(SampleDataEvent.SAMPLE_DATA, processSound);
        playBackSound.play();
    }
}

private function playBackSoundSampleData(e:SampleDataEvent) {

    //this number will change depending on the sample rate of microphone
    var numberOfFloats = 8192/8;
    for (var i = 0; i<numberOfFloats && micBuffer.length > 0; i++) 
    {
        _leftChannel[i] = micBuffer.shift();
        count++;
    }

    //apply effect

    //sample rate here is half of 44 so write twice + twice again to make stereo
    for(var i = 0 ; i < count ; ++i )
    {
        e.data.writeFloat( _leftChannel[i] );
        e.data.writeFloat( _leftChannel[i] );
        e.data.writeFloat( _leftChannel[i] );
        e.data.writeFloat( _leftChannel[i] );
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文