问题描述
所以我喜欢从事大学任务(保持我的一些技能),我决定解决这个问题:
[]
我正在运行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();
}
}
推荐答案
这篇关于我的directx声音缓冲区有静电噪声。请帮忙。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!