.NET Framework Class Library
Console.Beep Method (Int32, Int32)

Note: This method is new in the .NET Framework version 2.0.

Plays the sound of a beep of a specified frequency and duration through the console speaker.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

Syntax

Visual Basic (Declaration)
Public Shared Sub Beep ( _
    frequency As Integer, _
    duration As Integer _
)
Visual Basic (Usage)
Dim frequency As Integer
Dim duration As Integer

Console.Beep(frequency, duration)
C#
public static void Beep (
    int frequency,
    int duration
)
C++
public:
static void Beep (
    int frequency, 
    int duration
)
J#
public static void Beep (
    int frequency, 
    int duration
)
JScript
public static function Beep (
    frequency : int, 
    duration : int
)

Parameters

frequency

The frequency of the beep, ranging from 37 to 32767 hertz.

duration

The duration of the beep measured in milliseconds.

Exceptions

Exception typeCondition

ArgumentOutOfRangeException

frequency is less than 37 or more than 32767 hertz.

-or-

duration is less than or equal to zero.

HostProtectionException

This method was executed on a server, such as SQL Server, that does not permit access to the console.

Remarks

Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition Platform Note: The frequency and duration parameters are ignored for these operating systems.

Example

This example demonstrates the Beep method by playing the first few notes of a song through the console speaker.

Visual Basic
' This example demonstrates the Console.Beep(Int32, Int32) method
Imports System
Imports System.Threading

Class Sample
   Public Shared Sub Main()
      ' Declare the first few notes of the song, "Mary Had A Little Lamb".
      Dim Mary As Note() =  { _
            New Note(Tone.B, Duration.QUARTER), _
            New Note(Tone.A, Duration.QUARTER), _
            New Note(Tone.GbelowC, Duration.QUARTER), _
            New Note(Tone.A, Duration.QUARTER), _
            New Note(Tone.B, Duration.QUARTER), _
            New Note(Tone.B, Duration.QUARTER), _
            New Note(Tone.B, Duration.HALF), _
            New Note(Tone.A, Duration.QUARTER), _
            New Note(Tone.A, Duration.QUARTER), _
            New Note(Tone.A, Duration.HALF), _
            New Note(Tone.B, Duration.QUARTER), _
            New Note(Tone.D, Duration.QUARTER), _
            New Note(Tone.D, Duration.HALF)}
      ' Play the song
      Play(Mary)
   End Sub 'Main
   
   ' Play the notes in a song.
   Protected Shared Sub Play(tune() As Note)
      Dim n As Note
      For Each n In  tune
         If n.NoteTone = Tone.REST Then
            Thread.Sleep(CInt(n.NoteDuration))
         Else
            Console.Beep(CInt(n.NoteTone), CInt(n.NoteDuration))
         End If
      Next n
   End Sub 'Play 
   ' Define the frequencies of notes in an octave, as well as 
   ' silence (rest).
   
   Protected Enum Tone
      REST = 0
      GbelowC = 196
      A = 220
      Asharp = 233
      B = 247
      C = 262
      Csharp = 277
      D = 294
      Dsharp = 311
      E = 330
      F = 349
      Fsharp = 370
      G = 392
      Gsharp = 415
   End Enum 'Tone
   
   ' Define the duration of a note in units of milliseconds.
   
   Protected Enum Duration
      WHOLE = 1600
      HALF = WHOLE / 2
      QUARTER = HALF / 2
      EIGHTH = QUARTER / 2
      SIXTEENTH = EIGHTH / 2
   End Enum 'Duration
   
   ' Define a note as a frequency (tone) and the amount of 
   ' time (duration) the note plays.
   Protected Structure Note
      Private toneVal As Tone
      Private durVal As Duration
      
      ' Define a constructor to create a specific note.
      Public Sub New(frequency As Tone, time As Duration)
         toneVal = frequency
         durVal = time
      End Sub 'New
      
      ' Define properties to return the note's tone and duration.
      Public ReadOnly Property NoteTone() As Tone
         Get
            Return toneVal
         End Get
      End Property
      
      Public ReadOnly Property NoteDuration() As Duration
         Get
            Return durVal
         End Get
      End Property
   End Structure 'Note '
'This example produces the following results:
'
'This example plays the first few notes of "Mary Had A Little Lamb" 
'through the console speaker.
'
End Class 'Sample
C#
// This example demonstrates the Console.Beep(Int32, Int32) method
using System;
using System.Threading;

class Sample 
{
    public static void Main() 
    {
// Declare the first few notes of the song, "Mary Had A Little Lamb".
    Note[] Mary = 
        {
        new Note(Tone.B, Duration.QUARTER),
        new Note(Tone.A, Duration.QUARTER),
        new Note(Tone.GbelowC, Duration.QUARTER),
        new Note(Tone.A, Duration.QUARTER),
        new Note(Tone.B, Duration.QUARTER),
        new Note(Tone.B, Duration.QUARTER),
        new Note(Tone.B, Duration.HALF),
        new Note(Tone.A, Duration.QUARTER),
        new Note(Tone.A, Duration.QUARTER),
        new Note(Tone.A, Duration.HALF),
        new Note(Tone.B, Duration.QUARTER),
        new Note(Tone.D, Duration.QUARTER),
        new Note(Tone.D, Duration.HALF)
        };
// Play the song
    Play(Mary);
    }

// Play the notes in a song.
    protected static void Play(Note[] tune)
    {
    foreach (Note n in tune)
        {
        if (n.NoteTone == Tone.REST)
            Thread.Sleep((int)n.NoteDuration);
        else
            Console.Beep((int)n.NoteTone, (int)n.NoteDuration);
        }
    }

// Define the frequencies of notes in an octave, as well as 
// silence (rest).
    protected enum Tone
    {
    REST   = 0,
    GbelowC = 196,
    A      = 220,
    Asharp = 233,
    B      = 247,
    C      = 262,
    Csharp = 277,
    D      = 294,
    Dsharp = 311,
    E      = 330,
    F      = 349,
    Fsharp = 370,
    G      = 392,
    Gsharp = 415, 
    }

// Define the duration of a note in units of milliseconds.
    protected enum Duration
    {
    WHOLE     = 1600,
    HALF      = WHOLE/2,
    QUARTER   = HALF/2,
    EIGHTH    = QUARTER/2,
    SIXTEENTH = EIGHTH/2,
    }

// Define a note as a frequency (tone) and the amount of 
// time (duration) the note plays.
    protected struct Note
    {
    Tone     toneVal;
    Duration durVal;

// Define a constructor to create a specific note.
    public Note(Tone frequency, Duration time)
        {
        toneVal = frequency;
        durVal  = time;
        }

// Define properties to return the note's tone and duration.
    public Tone NoteTone { get{ return toneVal; } }
    public Duration NoteDuration { get{ return durVal; } }
    }
}
/*
This example produces the following results:

This example plays the first few notes of "Mary Had A Little Lamb" 
through the console speaker.
*/
C++
// This example demonstrates the Console.Beep(Int32, Int32) method
using namespace System;
using namespace System::Threading;
ref class Sample
{
protected:

   // Define the frequencies of notes in an octave, as well as 
   // silence (rest).
   enum class Tone
   {
      REST = 0,
      GbelowC = 196,
      A = 220,
      Asharp = 233,
      B = 247,
      C = 262,
      Csharp = 277,
      D = 294,
      Dsharp = 311,
      E = 330,
      F = 349,
      Fsharp = 370,
      G = 392,
      Gsharp = 415
   };


   // Define the duration of a note in units of milliseconds.
   enum class Duration
   {
      WHOLE = 1600,
      HALF = Duration::WHOLE / 2,
      QUARTER = Duration::HALF / 2,
      EIGHTH = Duration::QUARTER / 2,
      SIXTEENTH = Duration::EIGHTH / 2
   };


public:

   // Define a note as a frequency (tone) and the amount of 
   // time (duration) the note plays.
   value struct Note
   {
   public:
      Tone toneVal;
      Duration durVal;

      // Define a constructor to create a specific note.
      Note( Tone frequency, Duration time )
      {
         toneVal = frequency;
         durVal = time;
      }


      property Tone NoteTone 
      {

         // Define properties to return the note's tone and duration.
         Tone get()
         {
            return toneVal;
         }

      }

      property Duration NoteDuration 
      {
         Duration get()
         {
            return durVal;
         }

      }

   };


protected:

   // Play the notes in a song.
   static void Play( array<Note>^ tune )
   {
      System::Collections::IEnumerator^ myEnum = tune->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         Note n =  *safe_cast<Note ^>(myEnum->Current);
         if ( n.NoteTone == Tone::REST )
                  Thread::Sleep( (int)n.NoteDuration );
         else
                  Console::Beep( (int)n.NoteTone, (int)n.NoteDuration );
      }
   }


public:
   static void Main()
   {
      
      // Declare the first few notes of the song, "Mary Had A Little Lamb".
      array<Note>^ Mary = {Note( Tone::B, Duration::QUARTER ),Note( Tone::A, Duration::QUARTER ),Note( Tone::GbelowC, Duration::QUARTER ),Note( Tone::A, Duration::QUARTER ),Note( Tone::B, Duration::QUARTER ),Note( Tone::B, Duration::QUARTER ),Note( Tone::B, Duration::HALF ),Note( Tone::A, Duration::QUARTER ),Note( Tone::A, Duration::QUARTER ),Note( Tone::A, Duration::HALF ),Note( Tone::B, Duration::QUARTER ),Note( Tone::D, Duration::QUARTER ),Note( Tone::D, Duration::HALF )};
      
      // Play the song
      Play( Mary );
   }

};

int main()
{
   Sample::Main();
}

/*
This example produces the following results:

This example plays the first few notes of "Mary Had A Little Lamb" 
through the console speaker.
*/
J#
// This example demonstrates the Console.Beep(Int32, Int32) method
import System.*;
import System.Threading.*;

class Sample
{
    public static void main(String[] args)
    {
        // Declare the first few notes of the song, "Mary Had A Little Lamb".
        Note mary[] =  {
            new Note(new Tone(Tone.b),new Duration(Duration.quarter)),
            new Note(new Tone(Tone.a), new Duration(Duration.quarter)),
            new Note(new Tone(Tone.gbelowC),new Duration(Duration.quarter)),
            new Note(new Tone(Tone.a),new Duration(Duration.quarter)),
            new Note(new Tone(Tone.b),new Duration(Duration.quarter)),
            new Note(new Tone(Tone.b),new Duration(Duration.quarter)),
            new Note(new Tone(Tone.b),new Duration(Duration.half)),
            new Note(new Tone(Tone.a),new Duration(Duration.quarter)),
            new Note(new Tone(Tone.a),new Duration(Duration.quarter)),
            new Note(new Tone(Tone.a),new Duration(Duration.half)),
            new Note(new Tone(Tone.b),new Duration(Duration.quarter)),
            new Note(new Tone(Tone.d),new Duration(Duration.quarter)),
            new Note(new Tone(Tone.d),new Duration(Duration.half)) };

        // Play the song
        Play(mary);
    } //main

    // Play the notes in a song.
    protected static void Play(Note tune[])
    {
        for (int iCtr = 0; iCtr < tune.length; iCtr++) {
            Note n = tune[iCtr];
            if (n.get_NoteTone().get_member() == Tone.rest) {
                System.Threading.Thread.Sleep((n.get_NoteDuration().get_member()));
            }
            else {
                Console.Beep(n.get_NoteTone().get_member(),
                    n.get_NoteDuration().get_member());
            }
        }
    } //Play

    // Define the frequencies of notes in an octave, as well as 
    // silence (rest).
    protected static class Tone
    {
        private int member;
        Tone()
        {
            member = 0;
        } //Tone
        Tone(int n)
        {
            member = n;
        } //Tone
        public int get_member()
        {
            return member;
        } //get_member
        public void set_member(int n)
        {
            member = n;
        } //set_member
        public static int rest = 0;
        public static int gbelowC = 196;
        public static int a = 220;
        public static int asharp = 233;
        public static int b = 247;
        public static int c = 262;
        public static int csharp = 277;
        public static int d = 294;
        public static int dsharp = 311;
        public static int e = 330;
        public static int f = 349;
        public static int fsharp = 370;
        public static int g = 392;
        public static int gsharp = 415;
    } //Tone

    // Define the duration of a note in units of milliseconds.
    protected static class Duration
    {
        private int member;
        Duration()
        {
            member = 1600;
        } //Duration
        Duration(int n)
        {
            member = n;
        } //Duration
        public int get_member()
        {
            return member;
        }// get_member
        public void set_member(int n)
        {
            member = n;
        }//set_member
        public static int whole = 1600;
        public static int half = whole / 2;
        public static int quarter = half / 2;
        public static int eighth = quarter / 2;
        public static int sixteenth = eighth / 2;
    } //Duration

    // Define a note as a frequency (tone) and the amount of 
    // time (duration) the note plays.
    protected static class Note
    {
        private Tone toneVal;
        private Duration durVal;

        // Define a constructor to create a specific note.
        public Note(Tone frequency, Duration time)
        {
            toneVal = frequency;
            durVal = time;
        } //Note

        // Define properties to return the note's tone and duration.
        /** @property 
         */
        public Tone get_NoteTone()
        {
            return toneVal;
        } //get_NoteTone
        /** @property 
         */
        public Duration get_NoteDuration()
        {
            return durVal;
        } //get_NoteDuration
    } //Note
} //Sample 
/*
This example produces the following results:

This example plays the first few notes of "Mary Had A Little Lamb" 
through the console speaker.
*/
Platforms

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

Version Information

.NET Framework

Supported in: 2.0
See Also

Tags :


Page view tracker