locked
Beep Beep RRS feed

  • General discussion

  • A simple class that plays a tone of varying amplitude, frequency and duration through the PC's speakers:

    using System;
    using System.Windows.Forms;
    using System.IO;
    using System.Media;
    namespace BeepCS
    {
      
    public partial class Form1 : Form
      {
        
    public Form1()
        {
          InitializeComponent();
        }
        
    private void button1_Click(object sender, EventArgs e)
        {
          
    Beep.BeepBeep(1000, 1000, 1000);
        }
      }
    }
    public class Beep
    {
      
    public static void BeepBeep(int Amplitude, int 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;
        
    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++)
            {
              
    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();
            }
          }
        }
      }
    }


    • Edited by JohnWein Sunday, January 2, 2011 4:54 PM
    Saturday, January 1, 2011 3:25 PM

All replies

  • search keywords

    beep audio tone sound frequency speaker C# .NET

     

    TABLE OF MUSICAL NOTE FREQUENCIES (Hz)
    
    	 Octave 0 1 2 3 4 5 6 7
    
    	Note
    
    	 C 16 33 65 131 262 523 1046 2093
    
    	 C# 17 35 69 139 277 554 1109 2217
    
    	 D 18 37 73 147 294 587 1175 2349
    
    	 D# 19 39 78 155 311 622 1244 2489
    
    	 E 21 41 82 165 330 659 1328 2637
    
    	 F 22 44 87 175 349 698 1397 2794
    
    	 F# 23 46 92 185 370 740 1480 2960
    
    	 G 24 49 98 196 392 784 1568 3136
    
    	 G# 26 52 104 208 415 831 1661 3322
    
    	 A 27 55 110 220 440 880 1760 3520
    
    	 A# 29 58 116 233 466 932 1865 3729
    
    	 B 31 62 123 245 494 988 1975 3951
    
    
    
    

    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx


    • Edited by Rudedog2 Saturday, July 23, 2011 2:03 PM added 'beep'
    Saturday, January 1, 2011 5:13 PM
  • By the way, I don't understand how this weird binarywriter that writes to somewhere in the space of 44 + Bytes and the soundplayer can still read it and play... Can you give me a tutorial on how to use soundplayer with binarywriter and int arrays? Thanks in advance.

     

    And also, how to set subchunk2's ID and size? And what's the data format?


    Have a Byte on some Pi!

    Monday, August 15, 2011 10:02 AM