AS3麦克风录音/保存工作,闪存内PCM播放双倍速

发布于 2024-08-30 21:04:14 字数 1299 浏览 5 评论 0原文

我在 AS3 中有一个有效的麦克风录音脚本,我已经能够成功地使用它通过 AMF 将 .wav 文件保存到服务器。这些文件在任何音频播放器中都可以正常播放,没有奇怪的效果。

作为参考,这是我正在做的捕获麦克风的 ByteArray 的操作:(在名为 AudioRecorder 的类中)

public function startRecording():void {
_rawData = new ByteArray();
_microphone
 .addEventListener(SampleDataEvent.SAMPLE_DATA,_samplesCaptured, false, 0, true);
}

private function _samplesCaptured(e:SampleDataEvent):void {
  _rawData.writeBytes(e.data);
}

这可以正常工作。录音完成后,我可以获取 _rawData 变量并通过 WavWriter 类运行它,等等。

但是,如果我使用以下代码(我改编自 adobe Cookbook)将同一个 ByteArray 作为声音运行:(在名为 WavPlayer 的类中)

public function playSound(data:ByteArray):void {
  _wavData = data;
  _wavData.position = 0;
  _sound.addEventListener(SampleDataEvent.SAMPLE_DATA, _playSoundHandler);
  _channel = _sound.play();
  _channel
    .addEventListener(Event.SOUND_COMPLETE, _onPlaybackComplete, false, 0, true);
}

private function _playSoundHandler(e:SampleDataEvent):void {
  if(_wavData.bytesAvailable <= 0) return;
  for(var i:int = 0; i < 8192; i++) {
    var sample:Number = 0;
    if(_wavData.bytesAvailable > 0) sample = _wavData.readFloat();
    e.data.writeFloat(sample);
  }
}

音频文件以双倍速度播放!我检查了记录比特率等,并且非常确定这些都是正确的,并且我尝试更改缓冲区大小以及我能想到的任何其他数字。这可能是单声道与立体声的区别吗?

希望我在这里说得足够清楚,谢谢!

I have a working mic recording script in AS3 which I have been able to successfully use to save .wav files to a server through AMF. These files playback fine in any audio player with no weird effects.

For reference, here is what I am doing to capture the mic's ByteArray: (within a class called AudioRecorder)

public function startRecording():void {
_rawData = new ByteArray();
_microphone
 .addEventListener(SampleDataEvent.SAMPLE_DATA,_samplesCaptured, false, 0, true);
}

private function _samplesCaptured(e:SampleDataEvent):void {
  _rawData.writeBytes(e.data);
}

This works with no problems. After the recording is complete I can take the _rawData variable and run it through a WavWriter class, etc.

However, if I run this same ByteArray as a sound using the following code which I adapted from the adobe cookbook: (within a class called WavPlayer)

public function playSound(data:ByteArray):void {
  _wavData = data;
  _wavData.position = 0;
  _sound.addEventListener(SampleDataEvent.SAMPLE_DATA, _playSoundHandler);
  _channel = _sound.play();
  _channel
    .addEventListener(Event.SOUND_COMPLETE, _onPlaybackComplete, false, 0, true);
}

private function _playSoundHandler(e:SampleDataEvent):void {
  if(_wavData.bytesAvailable <= 0) return;
  for(var i:int = 0; i < 8192; i++) {
    var sample:Number = 0;
    if(_wavData.bytesAvailable > 0) sample = _wavData.readFloat();
    e.data.writeFloat(sample);
  }
}

The audio file plays at double speed! I checked recording bitrates and such and am pretty sure those are all correct, and I tried changing the buffer size and whatever other numbers I could think of. Could it be a mono vs stereo thing?

Hope I was clear enough here, thanks!

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

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

发布评论

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

评论(2

长亭外,古道边 2024-09-06 21:04:14

问题是 ByteArray 必须包含两个通道(左通道和右通道)的数据,一个值紧接另一个值。因此,如果您的录音是单声道的,您的代码应该是这样的:

for(var i:int = 0; i < 8192; i++) {
    var sample:Number = 0;
    if(_wavData.bytesAvailable > 0) sample = _wavData.readFloat();
        e.data.writeFloat(sample);
        e.data.writeFloat(sample);
}

如果是立体声,您将需要进行相应的调整。

The problem is that the ByteArray has to contain data for both channels (left and right), one value immediately after the other. Thus, if your recording is mono, your code should be this:

for(var i:int = 0; i < 8192; i++) {
    var sample:Number = 0;
    if(_wavData.bytesAvailable > 0) sample = _wavData.readFloat();
        e.data.writeFloat(sample);
        e.data.writeFloat(sample);
}

If it is stereo, you will need to adjust accordingly.

静若繁花 2024-09-06 21:04:14

我已经测试过。
您需要确保麦克风的速率为 44kHz:
_microphone.rate = 44;
这听起来应该是正确的。

我用过:

    private function playSound(data:ByteArray):void
    {
        rawData = data;
        rawData.position = 0;
        var sound:Sound = new Sound();
        sound.addEventListener(SampleDataEvent.SAMPLE_DATA, playSoundHandler);
        var channel:SoundChannel = sound.play();
        channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete, false, 0, true);
    }

    private function playSoundHandler(e:SampleDataEvent):void
    {
        if(rawData.bytesAvailable <= 0)
        {
            return;
        }
        for(var i:int = 0; i < 8192; i++)
        {
            var sample:Number = 0;
            if(rawData.bytesAvailable > 0)
            {
                sample = rawData.readFloat();
            }
            e.data.writeFloat(sample);
            e.data.writeFloat(sample);
        }
    }

I've tested it.
You need to make sure the Microphone's rate is at 44kHz:
_microphone.rate = 44;
This should sound right.

I used:

    private function playSound(data:ByteArray):void
    {
        rawData = data;
        rawData.position = 0;
        var sound:Sound = new Sound();
        sound.addEventListener(SampleDataEvent.SAMPLE_DATA, playSoundHandler);
        var channel:SoundChannel = sound.play();
        channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete, false, 0, true);
    }

    private function playSoundHandler(e:SampleDataEvent):void
    {
        if(rawData.bytesAvailable <= 0)
        {
            return;
        }
        for(var i:int = 0; i < 8192; i++)
        {
            var sample:Number = 0;
            if(rawData.bytesAvailable > 0)
            {
                sample = rawData.readFloat();
            }
            e.data.writeFloat(sample);
            e.data.writeFloat(sample);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文