Exemple #1
0
        public WaveGenerator(IEnumerable <Note> notes)
        {
            // Init chunks (basis waarden)
            header = new WaveHeader();
            format = new WaveFormatChunk();
            data   = new WaveDataChunk();

            //totale tijdsduur
            uint milliseconds = 0;

            foreach (Note n in notes)
            {
                milliseconds += Convert.ToUInt32(n.Duration.TotalMilliseconds);
            }


            // aantal samples = samples per sec * aantal sec * channels
            // maal mili sec en DAN delen door 1000 voor afrondings fouten
            uint numSamples = (uint)(((format.dwSamplesPerSec * milliseconds) / 1000) * format.wChannels);

            // Initialize the 16-bit array (waarden van sinus)
            data.shortArray = new short[numSamples];

            // Max amplitude for 16-bit audio
            int amplitude = 32760;



            //Hoever je al zit in je Wav file
            uint sampleCount = 0;

            //pernoot
            foreach (var n in notes)
            {
                // 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 * n.Frequency) / (format.dwSamplesPerSec * format.wChannels);

                //aantal samples voor DEZE noot (samples per sec * kanalen * sec)
                var samplesThisNote = format.dwSamplesPerSec * format.wChannels * n.Duration.TotalSeconds;

                // START op juiste plaats WAV (cursor sampleCOunt)
                // Zolag je nog niet elke sample voor deze noot gedaan hebt (-1 wegens anders 1 te ver)
                // the sine wave for repeated notes of the same pitch
                for (uint i = sampleCount; i < (samplesThisNote + sampleCount) - 1; i++)
                {
                    // per kanaal (Links/rechts)
                    for (int channel = 0; channel < format.wChannels; channel++)
                    {
                        data.shortArray[i + channel] = Convert.ToInt16(amplitude * Math.Sin(t * i));
                    }
                }
                sampleCount += (uint)samplesThisNote;
            }
            // Calculate data chunk size in bytes
            data.dwChunkSize = (uint)(data.shortArray.Length * (format.wBitsPerSample / 8));
        }
Exemple #2
0
        // JJR: create empty wav
        public WaveGenerator(uint sampleCount, string infoText)
        {
            // Init chunks
            header = new WaveHeader();
            format = new WaveFormatChunk();
            data   = new WaveDataChunk();
            info   = new InfoChunk(infoText);

            // Fill the data array with sample data

            uint numSamples = sampleCount;

            // Initialize the 16-bit array; default initialization to 0.
            data.shortArray = new short[numSamples];

            // Calculate data chunk size in bytes
            data.dwChunkSize = (uint)(data.shortArray.Length * (format.wBitsPerSample / 8));
        }
Exemple #3
0
        /// <summary>
        /// Initializes the object and generates a wave.
        /// </summary>
        /// <param name="type">The type of wave to generate</param>
        public WaveGenerator(WaveExampleType type)
        {
            // Init chunks
            header = new WaveHeader();
            format = new WaveFormatChunk();
            data   = new WaveDataChunk();

            // Fill the data array with sample data
            switch (type)
            {
            case WaveExampleType.ExampleSineWave:

                // 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];

                int    amplitude = 32760;                       // Max amplitude for 16-bit audio
                double freq      = 4400.0f;                     // Concert A: 440Hz

                // 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);

                for (uint i = 0; i < numSamples - 1; i++)
                {
                    // Fill with a simple sine wave at max amplitude
                    for (int channel = 0; channel < format.wChannels; channel++)
                    {
                        data.shortArray[i + channel] = Convert.ToInt16(amplitude * Math.Sin(t * i));
                    }
                }

                // Calculate data chunk size in bytes
                data.dwChunkSize = (uint)(data.shortArray.Length * (format.wBitsPerSample / 8));

                break;
            }
        }
Exemple #4
0
 public WaveHeader()
 {
     header = new WaveHeaderChunk();
     format = new WaveFormatChunk();
     data   = new WaveDataChunk();
 }