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(); } }
float
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)