【发布时间】:2016-12-03 01:02:55
【问题描述】:
所以我喜欢完成大学作业(以保持我的一些技能敏锐),我决定解决这个问题:
http://introcs.cs.princeton.edu/java/assignments/dsp.html
我正在运行 MSVS2015 C#/Console 应用程序以及 SharpDX 包,它使我能够访问一些底层 DirectSound 功能。我只是想在第一个示例中创建和播放 2 秒音符“A”。当我运行以下代码时,它会播放 2 秒,但它是非常静态的。我假设我的计算有问题,但我无法弄清楚到底是什么。有没有人有编写自己的数字声音缓冲区的经验?
谢谢, - 杰夫
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();
}
}
【问题讨论】:
标签: c# directx sharpdx directsound