Пример #1
0
        static void ToneGenerator()
        {
            List <Byte> tempBytes = new List <byte>();

            Generators.WaveHeader  header = new Generators.WaveHeader();
            Generators.FormatChunk format = new Generators.FormatChunk();
            Generators.DataChunk   data   = new Generators.DataChunk();

            // Create 1 second of tone at 697Hz
            Generators.SineGenerator leftData = new Generators.SineGenerator(627.0f,
                                                                             SAMPLE_FREQUENCY, AUDIO_LENGTH_IN_SECONDS);
            // Create 1 second of tone at 1209Hz
            Generators.SineGenerator rightData = new Generators.SineGenerator(1209.0f,
                                                                              SAMPLE_FREQUENCY, AUDIO_LENGTH_IN_SECONDS);

            data.AddSampleData(leftData.Data, rightData.Data);

            header.FileLength += format.Length() + data.Length();

            tempBytes.AddRange(header.GetBytes());
            tempBytes.AddRange(format.GetBytes());
            tempBytes.AddRange(data.GetBytes());

            myWaveData = tempBytes.ToArray();

            myAudio.Play(myWaveData, AudioPlayMode.WaitToComplete);
        }
Пример #2
0
        /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         * Function:
         * Pre:
         * Post:
         * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
        public void ToneGenerator(float Gen1, float Gen2, string Generator1, string Generator2, string amp1, string amp2)
        {
            List <Byte> tempBytes = new List <byte>();

            //test to define length of sound
            myModel.Audio_Length_Sec = 1;

            //For amplitude/volume of note
            int       Amplit1     = iVolume;
            int       Amplit2     = iVolume;
            const int VOLUME_MULT = 1600;


            //Creates wave, format, and data chunks
            Generators.WaveHeader  header = new Generators.WaveHeader();
            Generators.FormatChunk format = new Generators.FormatChunk();
            Generators.DataChunk   data   = new Generators.DataChunk();
            //Sine and square waves
            Generators.SineGenerator   leftData1;
            Generators.SquareGenerator leftData2;
            Generators.SineGenerator   rightData1;
            Generators.SquareGenerator rightData2;

            //Try to parse amplitude, set to new variables
            //Try to parse amplitude, set to new variables
            if (Int32.TryParse(amp1, out Amplit1))
            {
                Amplit1 = Amplit1 / 10;
            }
            else
            {
                Amplit1 = iVolume;
            }
            if (Int32.TryParse(amp2, out Amplit2))
            {
                Amplit2 = Amplit2 / 10;
            }
            else
            {
                Amplit2 = iVolume;
            }



            //Test for sawtooth generator
            Generators.PulseGenerator ExtraData = new Generators.PulseGenerator(Gen2,  //12098.0f
                                                                                (uint)myModel.Frequency, (ushort)myModel.Audio_Length_Sec);


            if (Generator1 == "01" && Generator2 == "01")
            {
                leftData1 = new Generators.SineGenerator(Gen1,  //627.0f
                                                         (uint)myModel.Frequency, (ushort)myModel.Audio_Length_Sec, Amplit1 * VOLUME_MULT, sBPM);

                rightData1 = new Generators.SineGenerator(Gen2,  //627.0f
                                                          (uint)myModel.Frequency, (ushort)myModel.Audio_Length_Sec, Amplit2 * VOLUME_MULT, sBPM);

                //Add the data
                data.AddSampleData(leftData1.Data, rightData1.Data);
            }
            else if (Generator1 == "01" && Generator2 == "02" || Generator1 == "01" && Generator2 == "--")
            {
                //Create Sine wave
                leftData1 = new Generators.SineGenerator(Gen1,  //627.0f
                                                         (uint)myModel.Frequency, (ushort)myModel.Audio_Length_Sec, Amplit1 * VOLUME_MULT, sBPM);

                //Create square wave
                rightData2 = new Generators.SquareGenerator(Gen2,  //627.0f
                                                            (uint)myModel.Frequency, (ushort)myModel.Audio_Length_Sec, Amplit2 * VOLUME_MULT, sBPM);

                //Add the data
                data.AddSampleData(leftData1.Data, rightData2.Data);
            }
            else if (Generator1 == "02" && Generator2 == "01" || Generator1 == "--" && Generator2 == "01")
            {
                //Create Square wave
                leftData2 = new Generators.SquareGenerator(Gen1,  //627.0f
                                                           (uint)myModel.Frequency, (ushort)myModel.Audio_Length_Sec, Amplit1 * VOLUME_MULT, sBPM);

                //Create Sine wave
                rightData1 = new Generators.SineGenerator(Gen1,  //627.0f
                                                          (uint)myModel.Frequency, (ushort)myModel.Audio_Length_Sec, Amplit2 * VOLUME_MULT, sBPM);

                //Add the data
                data.AddSampleData(leftData2.Data, rightData1.Data);
            }
            else if (Generator1 == "02" && Generator2 == "02" || Generator1 == "--" && Generator2 == "--")
            {
                leftData2 = new Generators.SquareGenerator(Gen1,  //627.0f
                                                           (uint)myModel.Frequency, (ushort)myModel.Audio_Length_Sec, Amplit1 * VOLUME_MULT, sBPM);

                //Create square wave
                rightData2 = new Generators.SquareGenerator(Gen1,  //627.0f
                                                            (uint)myModel.Frequency, (ushort)myModel.Audio_Length_Sec, Amplit2 * VOLUME_MULT, sBPM);

                //Add the data
                data.AddSampleData(leftData2.Data, rightData2.Data);
            }
            else
            {
                leftData1 = new Generators.SineGenerator(Gen1,  //627.0f
                                                         (uint)myModel.Frequency, (ushort)myModel.Audio_Length_Sec, Amplit1 * VOLUME_MULT, sBPM);

                rightData1 = new Generators.SineGenerator(Gen2,  //627.0f
                                                          (uint)myModel.Frequency, (ushort)myModel.Audio_Length_Sec, Amplit2 * VOLUME_MULT, sBPM);

                //Add the data
                data.AddSampleData(leftData1.Data, rightData1.Data);
            }

            //Sets header length
            header.FileLength += format.Length() + data.Length();

            //Adds header, format, and data chunks to list
            tempBytes.AddRange(header.GetBytes());
            tempBytes.AddRange(format.GetBytes());
            tempBytes.AddRange(data.GetBytes());

            //Sets list to WaveData array
            myModel.WaveData = tempBytes.ToArray();

            /* //Defines new filestream to write Wave to file. Overwrites file if not created, or creates new file
             * //By default it goes into the same folder as exe. If using debug it is the debug folder
             * //in bin.
             * FileStream f = new FileStream("Test.wav", FileMode.Create);
             * BinaryWriter wr = new BinaryWriter(f);
             *
             * //Writes to new file
             * wr.Write(myModel.WaveData);
             *
             * File.WriteAllBytes("Sound.wav", myModel.WaveData);*/

            //Plays bytes like WAVE file
            myAudio.Play(myModel.WaveData, AudioPlayMode.WaitToComplete);

            // myModel.Amplitude = 3200;
        }