我的directx声音缓冲区有静电噪声。请帮忙。 [英] My directx sound buffer has static noise. Please help.
问题描述
所以我喜欢从事大学任务(保持我的一些技能),我决定解决这个问题:
COS 126编程作业:数字信号处理作业 [ ^ ]
我正在运行MSVS2015 C#/ Console应用程序以及SharpDX软件包,这使我可以访问一些基础DirectSound功能。我只是想在第一个例子中创建并播放2秒音符'A'。当我运行以下代码时,它会播放2秒,但它非常静态。我假设我的计算中有一些东西,但我无法弄清楚到底是什么。有没有人有写自己的数字声音缓冲区的经验?
谢谢,
- 杰夫
我尝试过:
So I enjoy working on college assignments (to keep some of my skills sharp) and I've decided to tackle this one:
COS 126 Programming Assignment: Digital Signal Processing Assignment[^]
I'm running MSVS2015 C#/Console app along with the SharpDX package that gives me access to some underlying DirectSound capabilities. I'm just trying to create and play a 2 second note 'A' as they go over in the first example. When I run the following code it plays the 2 seconds but it is very static-y. I'm assuming there is something off with my calculations but I can't figure out what exactly. Does anyone have experience writing their own digital sound buffers?
Thanks,
- Jeff
What I have tried:
public class Execution : IDisposable
{
IntPtr Handle;
DirectSound Device;
SecondarySoundBuffer Buffer;
public Execution()
{
Handle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
Device = new DirectSound();
Device.SetCooperativeLevel(Handle, CooperativeLevel.Priority);
var rate = 44100;
var bits = 16;
var channels = 1;
var waveFormat = new WaveFormat(rate, bits, channels);
// Create a buffer with 2 seconds of sample data
var seconds = 2;
var bufferDescription = new SoundBufferDescription() { Format = waveFormat, BufferBytes = waveFormat.AverageBytesPerSecond * seconds };
Buffer = new SecondarySoundBuffer(Device, bufferDescription);
var noteFrequency = 440f; // A
var bufferData = new float[bufferDescription.BufferBytes];
var count = 0;
for (var sample = 0; sample < bufferDescription.BufferBytes; sample++)
{
var sampleInSeconds = (float)sample / (float)bufferDescription.BufferBytes * (float)seconds;
var value = (float)Math.Sin(2f * Math.PI * noteFrequency * sampleInSeconds );
bufferData[sample] = value;
}
Buffer.Write(bufferData, 0, LockFlags.EntireBuffer);
}
public void Execute()
{
Buffer.Play(0, 0);
}
public void Dispose()
{
Buffer.Dispose();
Device.Dispose();
}
}
推荐答案
好吧,我认为你是以不正确的格式编写数据。典型的16位波形文件使用整数而不是浮点数。
另外,显然float
是32位因此它不能成为16位波形文件的正确数据。
鉴于广泛和类型的数据都不同于预期的数据,你会怎么做?期望得到好的结果?
一个好的起点是从分析包含这种波形的波形文件开始,并将数据与数据进行比较。找到生成wave的应用程序或代码应该不难。
另外,通过使用一个不错的声音编辑器,显然数据是正确的如果你将数据放大到足以看到波浪本身的点。
Well, I would think that you are writing the data in an incorrect format. A typical 16 bit wave file uses integers and not floating points.
Also, obviously afloat
is 32 bit so it cannot be the proper data for a 16 bit wave file.
Given that both the wide and the type of data are different than the expected one, how would you expect to get good result?
A good starting point would be to start by analysing a wave file that contain such wave form and compare the data with your data. It should not be hard to find either application or code to generated a wave.
Also, by using a decent sound editor, it should be obvious it the data is correct if you zoom enough on data to the point you see the wave itself.
这篇关于我的directx声音缓冲区有静电噪声。请帮忙。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!