none
Slurring Tones?

    Question

  • Is there a way to make real-time tones other than the Console.Beep method that let's you slur notes? To make the beep start at one frequency but gradually end at a different one?
    Wednesday, April 26, 2017 9:20 PM

All replies

  • Hi,

    you can use JohnWein's BeepBeep for it:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/18fe83f0-5658-4bcf-bafc-2e02e187eb80/beep-beep?forum=csharpgeneral

    Modify it to increment the frequency in the loop...

    needed at the top of the code file:

    using System.IO;
    using System.Media;

    then eg:

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    this.Load += Form1_Load; } private void Form1_Load(object sender, EventArgs e) { //protect your ears, leave the amplitude low while testing. Beep.BeepBeep(100, 500, 2000); } } public class Beep { //orig by JohnWein, https://social.msdn.microsoft.com/Forums/vstudio/en-US/18fe83f0-5658-4bcf-bafc-2e02e187eb80/beep-beep?forum=csharpgeneral public static void BeepBeep(int Amplitude, double Frequency, int Duration) { double A = ((Amplitude * (System.Math.Pow(2, 15))) / 1000) - 1; double DeltaFT = 2 * Math.PI * Frequency / 44100.0; int Samples = 441 * Duration / 10; double increment = Frequency / (double)Samples; //be careful with setting this, might hurt your ears when producing chirp sounds, so dont use with ear buds in. int Bytes = Samples * 4; int[] Hdr = { 0X46464952, 36 + Bytes, 0X45564157, 0X20746D66, 16, 0X20001, 44100, 176400, 0X100004, 0X61746164, Bytes }; using (MemoryStream MS = new MemoryStream(44 + Bytes)) { using (BinaryWriter BW = new BinaryWriter(MS)) { for (int I = 0; I < Hdr.Length; I++) { BW.Write(Hdr[I]); } for (int T = 0; T < Samples; T++) { Frequency += increment; DeltaFT = 2 * Math.PI * Frequency / 44100.0; short Sample = System.Convert.ToInt16(A * Math.Sin(DeltaFT * T)); BW.Write(Sample); BW.Write(Sample); } BW.Flush(); MS.Seek(0, SeekOrigin.Begin); using (SoundPlayer SP = new SoundPlayer(MS)) { SP.PlaySync(); } } } } }

    Regards,

      Thorsten


    Wednesday, April 26, 2017 9:54 PM
  • ... here's a different one, playing a short melody:

    needed at the top of the code file:

    using System.IO;
    using System.Media;

    then eg:

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                this.Load += Form1_Load;
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                //protect your ears, leave the amplitude low while testing.
                Beep.BeepBeep3_DSept_T_D_T_Ext_2(100, 440, 10000);
            }
        }
    
        public class Beep
        {
            //orig by JohnWein, https://social.msdn.microsoft.com/Forums/vstudio/en-US/18fe83f0-5658-4bcf-bafc-2e02e187eb80/beep-beep?forum=csharpgeneral
            public static void BeepBeep3_DSept_T_D_T_Ext_2(int Amplitude, int Frequency1, int Duration)
            {
                double A = ((Amplitude * (System.Math.Pow(2, 15))) / 1000) - 1;
                double DeltaFT1 = 2 * Math.PI * Frequency1 * 3 / 2 / 44100.0;
                double DeltaFT2 = 2 * Math.PI * Frequency1 * 15 / 8 / 44100.0;
                double DeltaFT3 = 2 * Math.PI * Frequency1 * 5 / 4 / 44100.0;
    
                double DeltaFT4 = 2 * Math.PI * Frequency1 / 44100.0;
                double DeltaFT5 = 2 * Math.PI * Frequency1 * 5 / 4 / 44100.0;
                double DeltaFT6 = 2 * Math.PI * Frequency1 * 3 / 2 / 44100.0;
    
                double DeltaFT7 = 2 * Math.PI * Frequency1 * 3 / 2 / 44100.0;
                double DeltaFT8 = 2 * Math.PI * Frequency1 * 5 / 4 / 44100.0;
                double DeltaFT9 = 2 * Math.PI * Frequency1 * 9 / 8 / 44100.0;
    
                double DeltaFT13 = 2 * Math.PI * Frequency1 / 44100.0;
                double DeltaFT14 = 2 * Math.PI * Frequency1 * 4 / 3 / 44100.0;
                double DeltaFT15 = 2 * Math.PI * Frequency1 * 5 / 3 / 44100.0;
    
                int Samples = 441 * Duration / 10;
                int Bytes = Samples * 4;
                int[] Hdr = { 0X46464952, 36 + Bytes, 0X45564157, 0X20746D66, 16, 0X20001, 44100, 176400, 0X100004, 0X61746164, Bytes };
    
                using (System.IO.MemoryStream MS = new System.IO.MemoryStream(44 + Bytes))
                {
                    using (System.IO.BinaryWriter BW = new System.IO.BinaryWriter(MS))
                    {
                        for (int I = 0; I < Hdr.Length; I++)
                        {
                            BW.Write(Hdr[I]);
                        }
    
                        for (int T = 0; T < Samples / 16; T++)
                        {
                            short Sample = System.Convert.ToInt16(((A * Math.Sin(DeltaFT1 * T)) + (A * Math.Sin(DeltaFT2 * T)) + (A * Math.Sin(DeltaFT3 * T))) / 3);
                            BW.Write(Sample);
                            BW.Write(Sample);
                        }
                        for (int T = Samples / 16; T < Samples / 8; T++)
                        {
                            short Sample = System.Convert.ToInt16(((A * Math.Sin(DeltaFT4 * T)) + (A * Math.Sin(DeltaFT5 * T)) + (A * Math.Sin(DeltaFT6 * T))) / 3);
                            BW.Write(Sample);
                            BW.Write(Sample);
                        }
                        for (int T = Samples / 8; T < Samples / 8 + Samples / 16; T++)
                        {
                            short Sample = System.Convert.ToInt16(((A * Math.Sin(DeltaFT7 * T)) + (A * Math.Sin(DeltaFT8 * T)) + (A * Math.Sin(DeltaFT9 * T))) / 3);
                            BW.Write(Sample);
                            BW.Write(Sample);
                        }
                        for (int T = Samples / 8 + Samples / 16; T < Samples / 4; T++)
                        {
                            short Sample = System.Convert.ToInt16(((A * Math.Sin(DeltaFT4 * T)) + (A * Math.Sin(DeltaFT5 * T)) + (A * Math.Sin(DeltaFT6 * T))) / 3);
                            BW.Write(Sample);
                            BW.Write(Sample);
                        }
                        for (int T = Samples / 4; T < Samples / 4 + Samples / 8; T++)
                        {
                            short Sample = System.Convert.ToInt16(((A * Math.Sin(DeltaFT13 * T)) + (A * Math.Sin(DeltaFT14 * T)) + (A * Math.Sin(DeltaFT15 * T))) / 3);
                            BW.Write(Sample);
                            BW.Write(Sample);
                        }
                        for (int T = Samples / 4 + Samples / 8; T < Samples / 2; T++)
                        {
                            short Sample = System.Convert.ToInt16(((A * Math.Sin(DeltaFT4 * T)) + (A * Math.Sin(DeltaFT5 * T)) + (A * Math.Sin(DeltaFT6 * T))) / 3);
                            BW.Write(Sample);
                            BW.Write(Sample);
                        }
    
                        for (int T = Samples / 2; T < Samples / 2 + Samples / 16; T++)
                        {
                            short Sample = System.Convert.ToInt16(((A * Math.Sin(DeltaFT1 * T)) + (A * Math.Sin(DeltaFT2 * T)) + (A * Math.Sin(DeltaFT3 * T))) / 3);
                            BW.Write(Sample);
                            BW.Write(Sample);
                        }
                        for (int T = Samples / 2 + Samples / 16; T < Samples / 2 + Samples / 8; T++)
                        {
                            short Sample = System.Convert.ToInt16(((A * Math.Sin(DeltaFT4 * T)) + (A * Math.Sin(DeltaFT5 * T)) + (A * Math.Sin(DeltaFT6 * T))) / 3);
                            BW.Write(Sample);
                            BW.Write(Sample);
                        }
                        for (int T = Samples / 2 + Samples / 8; T < Samples / 2 + Samples / 8 + Samples / 16; T++)
                        {
                            short Sample = System.Convert.ToInt16(((A * Math.Sin(DeltaFT7 * T)) + (A * Math.Sin(DeltaFT8 * T)) + (A * Math.Sin(DeltaFT9 * T))) / 3);
                            BW.Write(Sample);
                            BW.Write(Sample);
                        }
                        for (int T = Samples / 2 + Samples / 8 + Samples / 16; T < Samples / 2 + Samples / 4; T++)
                        {
                            short Sample = System.Convert.ToInt16(((A * Math.Sin(DeltaFT4 * T)) + (A * Math.Sin(DeltaFT5 * T)) + (A * Math.Sin(DeltaFT6 * T))) / 3);
                            BW.Write(Sample);
                            BW.Write(Sample);
                        }
                        for (int T = Samples / 2 + Samples / 4; T < Samples / 2 + Samples / 4 + Samples / 8; T++)
                        {
                            short Sample = System.Convert.ToInt16(((A * Math.Sin(DeltaFT13 * T)) + (A * Math.Sin(DeltaFT14 * T)) + (A * Math.Sin(DeltaFT15 * T))) / 3);
                            BW.Write(Sample);
                            BW.Write(Sample);
                        }
                        for (int T = Samples / 2 + Samples / 4 + Samples / 8; T < Samples; T++)
                        {
                            short Sample = System.Convert.ToInt16(((A * Math.Sin(DeltaFT4 * T)) + (A * Math.Sin(DeltaFT5 * T)) + (A * Math.Sin(DeltaFT6 * T))) / 3);
                            BW.Write(Sample);
                            BW.Write(Sample);
                        }
    
                        BW.Flush();
    
                        MS.Seek(0, SeekOrigin.Begin);
                        using (SoundPlayer SP = new SoundPlayer(MS))
                        {
                            SP.PlaySync();
                        }
                    }
                }
            }
        }

    Regards,

      Thorsten

    Wednesday, April 26, 2017 10:16 PM