【问题标题】:Raw Sound playing原始声音播放
【发布时间】:2011-04-21 11:17:09
【问题描述】:

我研究图像格式已经有一段时间了,我知道图像是一个像素数组(24 位 - 可能是 32 位长)。问题是:声音文件的表示方式是什么?老实说,我什至不确定我应该搜索什么。我也很感兴趣你如何使用这些数据,我的意思是实际播放文件中的声音。对于图像文件,您有各种抽象设备来绘制图像(图形:java,c#,HDC:cpp(win32)等)。我希望我已经足够清楚了。

【问题讨论】:

    标签: audio directsound


    【解决方案1】:

    以下是 .wav 存储方式的简要概述。我是通过在 google 中输入“wave file format”找到的。

    http://www.sonicspot.com/guide/wavefiles.html

    【讨论】:

    • 感谢您的回答。我发现 wav 或多或少等同于 BMP(没有压缩)。
    【解决方案2】:

    WAV 文件也可以存储压缩音频,但我相信大多数时候它们没有被压缩。但是 WAV 格式被设计为一个容器,用于存储有关如何存储音频的许多选项。

    这是我在 stackoverflow 的另一个问题中找到的代码片段,我喜欢在 C# 中构建 WAV 格式的音频 MemoryStream,然后播放该流(不将其保存到文件中,就像许多其他答案一样依赖在)。但是,如果您希望将其保存到文件中,可以很容易地使用一行代码将其保存到文件中,但我认为大多数时候,这是不可取的。

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Windows.Forms;
    
    public static void PlayBeep(UInt16 frequency, int msDuration, UInt16 volume = 16383)
    {
        var mStrm = new MemoryStream();
        BinaryWriter writer = new BinaryWriter(mStrm);
    
        const double TAU = 2 * Math.PI;
        int formatChunkSize = 16;
        int headerSize = 8;
        short formatType = 1;
        short tracks = 1;
        int samplesPerSecond = 44100;
        short bitsPerSample = 16;
        short frameSize = (short)(tracks * ((bitsPerSample + 7) / 8));
        int bytesPerSecond = samplesPerSecond * frameSize;
        int waveSize = 4;
        int samples = (int)((decimal)samplesPerSecond * msDuration / 1000);
        int dataChunkSize = samples * frameSize;
        int fileSize = waveSize + headerSize + formatChunkSize + headerSize + dataChunkSize;
        // var encoding = new System.Text.UTF8Encoding();
        writer.Write(0x46464952); // = encoding.GetBytes("RIFF")
        writer.Write(fileSize);
        writer.Write(0x45564157); // = encoding.GetBytes("WAVE")
        writer.Write(0x20746D66); // = encoding.GetBytes("fmt ")
        writer.Write(formatChunkSize);
        writer.Write(formatType);
        writer.Write(tracks);
        writer.Write(samplesPerSecond);
        writer.Write(bytesPerSecond);
        writer.Write(frameSize);
        writer.Write(bitsPerSample);
        writer.Write(0x61746164); // = encoding.GetBytes("data")
        writer.Write(dataChunkSize);
        {
            double theta = frequency * TAU / (double)samplesPerSecond;
            // 'volume' is UInt16 with range 0 thru Uint16.MaxValue ( = 65 535)
            // we need 'amp' to have the range of 0 thru Int16.MaxValue ( = 32 767)
            // so we simply set amp = volume / 2
            double amp = volume >> 1; // Shifting right by 1 divides by 2
            for (int step = 0; step < samples; step++)
            {
                short s = (short)(amp * Math.Sin(theta * (double)step));
                writer.Write(s);
            }
        }
    
        mStrm.Seek(0, SeekOrigin.Begin);
        new System.Media.SoundPlayer(mStrm).Play();
        writer.Close();
        mStrm.Close();
    } // public static void PlayBeep(UInt16 frequency, int msDuration, UInt16 volume = 16383)
    

    但这段代码显示了对 WAV 格式的一些了解,甚至是允许人们在 C# 源代码中构建自己的 WAV 格式的代码。

    【讨论】:

      猜你喜欢
      • 2012-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多