Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do "\a" and the Console.Beep() method play the same beep?

\a is an escape sequence which represents a bell alert on character constants. On Console.Beep() method explanation from MSDN:

Plays the sound of a beep through the console speaker.

Let's consider this sample code:

public static void Main(string[] args)
{
     Console.WriteLine("\a");
     Console.Beep();
}

This code creates 2 beeps and they sounds the same to me. Now I need to see the proof.

When I decompile the Console.Beep() method, it uses Win32Native.Beep from kernel32.dll;

public static void Beep(int frequency, int duration)
{
    Win32Native.Beep(frequency, duration);
}

Their frequencies and durations can be different, I don't say anything about these stuff but are their sources the same (Win32Native.Beep)? Which source does \a use for the bell alert?

like image 815
Soner Gönül Avatar asked Feb 10 '13 13:02

Soner Gönül


1 Answers

This is as far as I could gather from examining the audio (Windows 7 64-bit (yes, I had to reboot into windows, I'm that dedicated :) ). I recorded the audio using Fraps, so it should be reliable.

This is the code I used to check them:

using System;
using System.Threading;

namespace StackOverflow
{
    class Program
    {
        private static void Main(string[] args)
        {
            Console.Write("\a");
            Thread.Sleep(500);
            Console.Beep();
        }
    }
}

These are the two tracks in Audacity (Console.Write('\a') being the upper one, Console.Beep() the lower).

Two beeps

At any point where I zoomed in on the tracks, the sine waves exactly matched each other, and they had the exact same duration, so I have to conclude that they are, in fact, the same (on Windows 7 that is).

like image 63
antonijn Avatar answered Sep 28 '22 17:09

antonijn