2010-10-05 110 views
1

我一直在工作一段时间的图像格式,我知道一个图像是一个像素数组(24-也许32位长)。问题是:声音文件的表现方式是什么?说实话,我甚至不知道我应该使用Google搜索。另外我会感兴趣你如何使用数据,我的意思是实际上播放文件中的声音。对于一个图像文件,你有各种抽象设备来绘制图像(Graphics:java,c#,HDC:cpp(win32)等)。我希望我已经清楚了。原始声音播放

回答

2

下面是如何存储.wav的完美概述。我通过在Google中输入“wave file format”找到了它。

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

+0

谢谢你的答案。我发现wav相当于一个或多或少的BMP(没有压缩)。 – 2010-10-08 08:15:01

1

WAV文件也可以存储压缩的音频,但我相信他们大多不压缩的时间。但是WAV格式被设计成一个容器,用于存储音频的一些选项。

这里是我在另一个问题在这里找到的另一个问题,我喜欢在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格式。