我一直在工作一段时间的图像格式,我知道一个图像是一个像素数组(24-也许32位长)。问题是:声音文件的表现方式是什么?说实话,我甚至不知道我应该使用Google搜索。另外我会感兴趣你如何使用数据,我的意思是实际上播放文件中的声音。对于一个图像文件,你有各种抽象设备来绘制图像(Graphics:java,c#,HDC:cpp(win32)等)。我希望我已经清楚了。原始声音播放
Q
原始声音播放
1
A
回答
2
下面是如何存储.wav的完美概述。我通过在Google中输入“wave file format”找到了它。
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格式。
相关问题
- 1. 原始音频播放5
- 2. 如何从声音的开始播放/暂停声音和声音播放?
- 3. 播放声音
- 4. 播放声音
- 5. 播放声音?
- 6. 播放声音从原始文件在科特林
- 7. Can DirectSound可以播放原始二进制声音文件吗?
- 8. 意图从原始文件夹播放声音
- 9. 如何正确知道声音开始播放的原因?
- 10. 无法播放原始音频文件
- 11. Android从C++端播放原始音频
- 12. 用QPython audiostream原始PCM音频播放
- 13. VC++播放声音
- 14. Ruby声音播放
- 15. MonoTouch:播放声音
- 16. 声音未播放
- 17. 声音未播放
- 18. 另一个声音开始播放时的Java停止声音
- 19. 使用系统声音播放声音
- 20. c中声音播放后的声音#
- 21. 播放声音作为系统声音
- 22. 播放和暂停以播放声音
- 23. UIScrollView滚动播放声音
- 24. 在C++中播放声音
- 25. 用AKAudioPlayer播放声音 - iOS
- 26. 和弦声音播放
- 27. FMOD播放重叠声音
- 28. 没有onClickListener播放声音
- 29. SoundPool不播放声音
- 30. AudioServicesPlaySystemSound不播放声音
谢谢你的答案。我发现wav相当于一个或多或少的BMP(没有压缩)。 – 2010-10-08 08:15:01