如何在 ActionScript 中播放 WAV?

发布于 2024-09-03 13:06:44 字数 960 浏览 8 评论 0原文

请参阅我在 http://textsnip.com/see/WAVinAS3 创建的用于解析ActionScript 3.0 中的 WAVE 文件。

这个类正确地从文件头和文件头中分离出信息。 fmt chunks,隔离数据块,并创建一个新的ByteArray来存储数据块。它采用格式标记为 1 的未压缩 WAVE 文件。 WAVE 文件使用以下 Flex 嵌入标记嵌入到我的 SWF 中:

[Embed(source="some_sound.wav", mimeType="application/octet-stream")]
public var sound_class:Class;
public var wave:WaveFile = new WaveFile(new sound_class());

分离数据块后,该类尝试创建一个 Sound 对象,该对象可以从以下位置流式传输样本:数据块。我在流处理过程中遇到问题,可能是因为我不擅长数学,也不知道位/字节等发生了什么。

这是我用作 WAVE 参考的两个文档文件格式: http://www.lightlink.com/tjweber/StripWav/Canon.html https://ccrma.stanford.edu/courses/422/projects/WaveFormat/< /a>

现在,文件正在播放!甚至是实时的!但是……声音实在是太失真了。这是怎么回事?

Please see the class I have created at http://textsnip.com/see/WAVinAS3 for parsing a WAVE file in ActionScript 3.0.

This class is correctly pulling apart info from the file header & fmt chunks, isolating the data chunk, and creating a new ByteArray to store the data chunk. It takes in an uncompressed WAVE file with a format tag of 1. The WAVE file is embedded into my SWF with the following Flex embed tag:

[Embed(source="some_sound.wav", mimeType="application/octet-stream")]
public var sound_class:Class;
public var wave:WaveFile = new WaveFile(new sound_class());

After the data chunk is separated, the class attempts to make a Sound object that can stream the samples from the data chunk. I'm having issues with the streaming process, probably because I'm not good at math and don't really know what's happening with the bits/bytes, etc.

Here are the two documents I'm using as a reference for the WAVE file format:
http://www.lightlink.com/tjweber/StripWav/Canon.html
https://ccrma.stanford.edu/courses/422/projects/WaveFormat/

Right now, the file IS playing back! In real time, even! But...the sound is really distorted. What's going on?

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

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

发布评论

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

评论(1

青瓷清茶倾城歌 2024-09-10 13:06:44

问题出在 onSampleData 处理程序中。

在您的 wav 文件中,幅度存储为有符号短整型,即 16 位整数。您将它们读取为 32 位带符号浮点数。整数和浮点数在二进制中的表示方式不同,因此永远无法正常工作。

现在,玩家期望浮动。他们为什么使用花车?不确定,但一个很好的理由是它允许玩家接受每个样本的标准化值。这样你就不必关心或知道玩家正在使用什么位:最大值是 1,最小值是 -1,仅此而已。

所以,你的问题是你必须将你的有符号短值转换为标准化有符号浮点数。 Short 需要 16 位,因此它可以存储 2 ^ 16(或 65,536)个不同的值。由于它是有符号的并且符号占用一位,因此最大值将为 2 ^ 15。因此,您知道您的输入范围是 -32,768 ... 32,767。

另一方面,样本值已标准化,并且必须在 -1 ... 1 范围内。

因此,您必须标准化您的输入。这很容易。只需将读取值除以最大值,即可将输入幅度转换为范围 -1 ... 1。

如下所示:有

    private function onSampleData(evt:SampleDataEvent):void 
    { 
        var amplitude:int = 0;
        var maxAmplitude:int = 1 << (bitsPerSample - 1); // or Math.pow(2, bitsPerSample - 1);
        var sample:Number = 0; 
        var actualSamples:int = 8192;
        var samplesPerChannel:int = actualSamples / channels;

        for ( var c:int = 0; c < samplesPerChannel ; c++ ) { 
            var i:int = 0;
            while(i < channels && data.bytesAvailable >= 2) {
                amplitude = data.readShort();
                sample = amplitude / maxAmplitude;
                evt.data.writeFloat(sample); 
                i++;
            }
        } 
    }  

几件事需要注意:

  1. maxAmplitude 可以(并且可能
    应该)在阅读时计算
    位深度。我正在做
    方法只是为了让你可以在
    粘贴的代码。

  2. 虽然计算了maxAmplitude
    基于读取的位深度,因此
    对于任何位深度都是正确的,
    我正在循环阅读短裤,所以
    如果你的 wav 文件碰巧使用
    不同的位深度,这个函数
    将无法正常工作。你可以
    添加一个开关并读取必要的内容
    数据量(即 readInt 如果
    位深度为 32)。然而,16位是
    如此广泛使用的标准,以至于我
    怀疑这实际上是否需要。

  3. 该函数适用于
    立体声波形。如果你想让它发挥作用
    对于单声道,重新编写它以编写
    相同的样品两次。也就是说,对于每个
    读,你做了两次写(你的输入
    是单声道,但玩家期望 2
    示例)。

  4. 我删除了 EOF 捕获,你也可以
    知道您是否有足够的数据可供读取
    从你的缓冲区检查
    字节可用。到达终点
    流在任何方面都不例外
    方式,IMO,所以我宁愿控制它
    没有异常处理程序的情况,
    但这只是个人的
    偏好。

The problem is in the onSampleData handler.

In your wav file, the amplitudes are stored as signed shorts, that is 16 bit integers. You are reading them as 32 bit signed floats. Integers and floats are represented differently in binary, so that will never work right.

Now, the player expects floats. Why did they use floats? Don't know for sure, but one good reason is that it allows the player to accept a normalized value for each sample. That way you don't have to care or know what bitdept the player is using: the max value is 1, and the min value is -1, and that's it.

So, your problem is you have to convert your signed short to a normalized signed float. A short takes 16 bits, so it can store 2 ^ 16 (or 65,536) different values. Since it's signed and the sign takes up one bit, the max value will be 2 ^ 15. So, you know your input is the range -32,768 ... 32,767.

The sample value is normalized and must be in the range -1 ... 1, on the other hand.

So, you have to normalize your input. It's quite easy. Just take the read value and divide it by the max value, and you have your input amplitude converted to the range -1 ... 1.

Something like this:

    private function onSampleData(evt:SampleDataEvent):void 
    { 
        var amplitude:int = 0;
        var maxAmplitude:int = 1 << (bitsPerSample - 1); // or Math.pow(2, bitsPerSample - 1);
        var sample:Number = 0; 
        var actualSamples:int = 8192;
        var samplesPerChannel:int = actualSamples / channels;

        for ( var c:int = 0; c < samplesPerChannel ; c++ ) { 
            var i:int = 0;
            while(i < channels && data.bytesAvailable >= 2) {
                amplitude = data.readShort();
                sample = amplitude / maxAmplitude;
                evt.data.writeFloat(sample); 
                i++;
            }
        } 
    }  

A couple of things to note:

  1. maxAmplitude could (and probably
    should) be calculated when you read
    the bitdepth. I'm doing it in the
    method just so you can see it in the
    pasted code.

  2. Although maxAmplitude is calculated
    based on the read bitdepth and thus
    will be correct for any bitdepth,
    I'm reading shorts in the loop, so
    if your wav file happens to use a
    different bitdepth, this function
    will not work correctly. You could
    add a switch and read the necessary
    ammount of data (i.e., readInt if
    bitdepth is 32). However, 16 bits is
    such a widely used standard, that I
    doubt this is practically needed.

  3. This function will work for
    stereo wavs. If you want it to work
    for mono, re write it to write the
    same sample twice. That is, for each
    read, you do two writes (your input
    is mono, but the player expects 2
    samples).

  4. I removed the EOF catch, as you can
    know if you have enough data to read
    from your buffer checking
    bytesAvailable. Reaching the end of
    stream is not exceptional in any
    way, IMO, so I'd rather control that
    case without an exception handler,
    but this is just a personal
    preference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文