Share The Piano pronunciation applet in the console

Source: Internet
Author: User
Think about how to implement those nice notes in the program? Is this interesting! If we can make a small program to simulate the pronunciation of seven notes in the music, can you do it? Think about how to implement those nice notes in the program? Is this interesting! If we can make a small program to simulate the pronunciation of seven notes in the music, can you do it?

Next, let's take a look at the source of this magical sound...

First, create a console project. The core of this program is how to make the system pronounce, which uses the Beep () method in the Console class. It has two parameters. The first one is to control the sound frequency, the second is to control the length of time.

Another method is ReadKey (). it reads a character each time and has a bool parameter to control whether the pressed key is displayed in the console window.

It is interesting that we can also record the input notes for recording the music we just played. Here, we use the timespan type to calculate the time interval and Thread pause (Thread. Sheep ).

The following is the code of the entire program for your reference.

///


/// Obtain the sound and change the background color
///
///
Static void Sound (int I)
{
Int fre = (Convert. ToInt32 (I) + 13) * 37;
Switch (I)
{
Case '1 ':
Console. Beep (fre, 300); // The first parameter specifies the sound frequency, and the second parameter specifies the sound duration.

Console. BackgroundColor = ConsoleColor. Blue;
Console. Clear ();
Break;
Case '2 ':
Console. Beep (fre, 300 );
Console. BackgroundColor = ConsoleColor. Cyan;
Console. Clear ();
Break;
Case '3 ':
Console. Beep (fre, 300 );
Console. BackgroundColor = ConsoleColor. Green;
Console. Clear ();
Break;
Case '4 ':
Console. Beep (fre, 300 );
Console. BackgroundColor = ConsoleColor. Red;
Console. Clear ();
Break;
Case '5 ':
Console. Beep (fre, 300 );
Console. BackgroundColor = ConsoleColor. Yellow;
Console. Clear ();
Break;
Case '6 ':
Console. Beep (fre, 300 );
Console. BackgroundColor = ConsoleColor. White;
Console. Clear ();
Break;
Case '7 ':
Console. Beep (fre, 300 );
Console. BackgroundColor = ConsoleColor. Blue;
Console. Clear ();
Break;

Default:
Break;
}
}
// This class defines the input characters and time interval of the record
Class LL
{
Public char c; // character
Public DateTime d; // time interval
}
Static void Main (string [] args)
{
Console. SetWindowSize (100,30 );

List Record = new List ();
While (true)
{
ConsoleKeyInfo cki = Console. ReadKey (true );
Char I = cki. KeyChar;

If (I! = 'Q') // If q is input, start playing by recording.
{
LL l = new LL ();
L. c = I;
L. d = DateTime. Now;
Record. Add (l );
Sound (I );

}
Else
{

For (int k = 0; k <record. Count; k ++)
{

TimeSpan tspan;
If (k! = 0) // if it is the first one, the time interval is calculated.
{
Tspan = record [k]. d-record [k-1]. d; // calculate the input interval
Console. WriteLine (tspan );
Thread. Sleep (tspan); // specifies the time when the current Thread is blocked.
}
Sound (record [k]. c );
}
Record. Clear (); // Clear records
}


}


}

The above is the details of the piano pronunciation applet in the console. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.