0
所以我喜歡工作在大學分配(保留一些我的技能銳利)和我決定來解決這一之一:我的DirectX聲音緩衝器具有靜態噪聲
http://introcs.cs.princeton.edu/java/assignments/dsp.html
我跑MSVS2015 C#/ Console應用程序以及SharpDX包,使我可以訪問一些底層DirectSound功能。我只是想在第一個例子中創建並播放2秒鐘的音符'A'。當我運行下面的代碼時,它會播放2秒,但它非常靜態-y。我假設我的計算有些問題,但我無法弄清楚究竟是什麼。有沒有人有經驗編寫自己的數字聲音緩衝區?
謝謝, - 傑夫
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();
}
}