/// <summary> /// Initializes the object and generates a wave. /// </summary> /// <param name="type">The type of wave to generate</param> public WaveGenerator(WaveExampleType type, int frequency, double volume) { // Init chunks header = new WaveHeader(); format = new WaveFormatChunk(); data = new WaveDataChunk(); // Number of samples = sample rate * channels * bytes per sample uint numSamples = format.dwSamplesPerSec * format.wChannels; // Initialize the 16-bit array data.shortArray = new short[numSamples]; data.shortArrayLength = numSamples; // Calculate data chunk size in bytes and store it in the data chunk data.dwChunkSize = (uint)(numSamples * (format.wBitsPerSample / 8)); // Max amplitude for 16-bit audio int amplitude = (int)(MAX_AMPLITUDE_16BIT * volume); // Create a double version of the frequency for easier math double freq = (double)frequency; // The "angle" used in the function, adjusted for the number of channels and sample rate. // This value is like the period of the wave. double t = (Math.PI * 2 * freq) / (format.dwSamplesPerSec * format.wChannels); // Fill the data array with sample data if (type == WaveExampleType.ExampleSineWave) { ExampleSineWave(numSamples, amplitude, t); return; } if (type == WaveExampleType.ExampleSquareWave) { ExampleSquareWave(numSamples, amplitude, t); return; } if (type == WaveExampleType.ExampleWhiteNoise) { ExampleWhiteNoise(numSamples, amplitude); return; } if (type == WaveExampleType.ExampleSawtoothWave) { ExampleSawtoothWave(frequency, numSamples, amplitude); return; } if (type == WaveExampleType.ExampleTriangleWave) { ExampleTriangleWave(frequency, numSamples, amplitude); return; } }