| 1 | using Microsoft.DirectX.DirectSound; |
| 2 | using System; |
| 3 | |
| 4 | namespace Orchestra.Audio |
| 5 | { |
| 6 | |
| 7 | // The class |
| 8 | public class Oscillator |
| 9 | { |
| 10 | |
| 11 | // Private Properties: |
| 12 | private BufferDescription bufferDesc; |
| 13 | private Device device; |
| 14 | private WaveFormat waveFormat; |
| 15 | |
| 16 | // Constructor: |
| 17 | public Oscillator(Device device) |
| 18 | { |
| 19 | |
| 20 | // Get the device |
| 21 | // (Create the device and set its priority in |
| 22 | // the class containing the program window) |
| 23 | this.device = device; |
| 24 | |
| 25 | // Set up wave format |
| 26 | waveFormat = new WaveFormat(); |
| 27 | waveFormat.FormatTag = WaveFormatTag.Pcm; |
| 28 | waveFormat.Channels = 1; |
| 29 | waveFormat.BitsPerSample = 16; |
| 30 | waveFormat.SamplesPerSecond = 44100; |
| 31 | waveFormat.BlockAlign = (short)(waveFormat.Channels * waveFormat.BitsPerSample / 8); |
| 32 | waveFormat.AverageBytesPerSecond = waveFormat.BlockAlign * waveFormat.SamplesPerSecond; |
| 33 | |
| 34 | // Set up buffer description |
| 35 | bufferDesc = new BufferDescription(waveFormat); |
| 36 | bufferDesc.Control3D = false; |
| 37 | bufferDesc.ControlEffects = false; |
| 38 | bufferDesc.ControlFrequency = true; |
| 39 | bufferDesc.ControlPan = true; |
| 40 | bufferDesc.ControlVolume = true; |
| 41 | bufferDesc.DeferLocation = true; |
| 42 | bufferDesc.GlobalFocus = true; |
| 43 | |
| 44 | |
| 45 | } |
| 46 | |
| 47 | // Play a sound (start frequency, end frequency (portamento), pan, hold time (ms), decay time (ms)) |
| 48 | public void play(double frequency1, double frequency2, int pan, double time, int decay) |
| 49 | { |
| 50 | |
| 51 | // Convert decay to bytes |
| 52 | decay *= waveFormat.SamplesPerSecond / 1000; |
| 53 | |
| 54 | // Set buffer length |
| 55 | bufferDesc.BufferBytes = (int)(time * waveFormat.SamplesPerSecond * waveFormat.Channels * waveFormat.BlockAlign / 1000) + decay; |
| 56 | |
| 57 | // Create a byte buffer |
| 58 | int intLength = (int)(bufferDesc.BufferBytes); |
| 59 | char[] buffer = new char[intLength]; |
| 60 | |
| 61 | // Set initial amplitude and frequency |
| 62 | double frequency = frequency1; |
| 63 | |
| 64 | // Get the step amount between frequencies |
| 65 | double freqStep = (frequency2 - frequency1) / intLength; |
| 66 | |
| 67 | // Iterate through time |
| 68 | for (int i = 0; i < intLength; i++) |
| 69 | { |
| 70 | |
| 71 | // Curve amp on each side |
| 72 | double amplitude = 300.0; |
| 73 | if (i < amplitude * 2) amplitude = (double)i / 2.0; |
| 74 | if (i > intLength - decay) amplitude = (((double)intLength - (double)i) / (double)decay) * 300; |
| 75 | |
| 76 | // Add to sine |
| 77 | buffer[i] = (char)(amplitude * Math.Sin((double)i * 2.0 * Math.PI * frequency / (double)waveFormat.SamplesPerSecond)); |
| 78 | |
| 79 | // Increment the frequency |
| 80 | frequency += freqStep; |
| 81 | |
| 82 | } |
| 83 | |
| 84 | // Check buffer size |
| 85 | int intMinBufferSize = (int)(150 * waveFormat.SamplesPerSecond * waveFormat.Channels * waveFormat.BlockAlign / 1000); |
| 86 | if (intLength < intMinBufferSize) |
| 87 | { |
| 88 | |
| 89 | // Create larger buffer |
| 90 | char[] buffer2 = new char[intMinBufferSize]; |
| 91 | buffer.CopyTo(buffer2, 0); |
| 92 | |
| 93 | // Iterate through rest of buffer |
| 94 | for (int i = intLength; i < intMinBufferSize; i++) buffer2[i] = (char)0; |
| 95 | |
| 96 | // Copy over original buffer |
| 97 | buffer = null; |
| 98 | buffer = new char[buffer2.Length]; |
| 99 | buffer2.CopyTo(buffer, 0); |
| 100 | |
| 101 | // Update buffer size |
| 102 | bufferDesc.BufferBytes = buffer.Length; |
| 103 | |
| 104 | } |
| 105 | |
| 106 | // Set up audio buffer and play sound |
| 107 | try |
| 108 | { |
| 109 | SecondaryBuffer bufferSound = new SecondaryBuffer(bufferDesc, device); |
| 110 | bufferSound.Volume = (int)Volume.Max; |
| 111 | bufferSound.Pan = pan; |
| 112 | bufferSound.Write(0, buffer, LockFlag.None); |
| 113 | bufferSound.Play(0, BufferPlayFlags.Default); |
| 114 | } |
| 115 | catch (Exception ex) |
| 116 | { |
| 117 | Console.WriteLine(ex.Message); |
| 118 | } |
| 119 | |
| 120 | } |
| 121 | |
| 122 | } |
| 123 | |
| 124 | } |
| 125 | |