How to play music and music with C program

Source: Internet
Author: User

How to play music and music with C program

How to play music with C program

First, we will introduce a function:

Beep (Frequency, time)

If we run this function in the program

Int main (void)

{

Beep (Frequency, time );

}

Then, the system sends a sound at the Frequency of Frequency Hz and continues for time ms.

Obviously, after calling this function multiple times, we can simply play music.

It's easy, right!

But when you start to practice, you will find that your music level is almost zero, and you cannot understand the average music.

So let's add a bit of music knowledge.

Based on the function, we only need to understand the frequency and duration of each sound in the music to solve the problem.

Let's talk about the tone first:

This is too complicated. It means that after reading a lot of information about the tone, a program is developed based on the music spectrum, and the overall result is half an octal reduction. Finally, my solution is: directly find a reliable frequency in most cases, and adjust it in case of failure.

I am looking for the following frequencies:

Do re mi fa sol la si

440 495 550 587 660 733 825 unit: Hz

As for how to adjust it, in view of my poor level of music, I will not miss my children here. I pray you don't need to adjust it.

In addition to the basic frequency, sometimes there is a small dot above or below the note, which indicates that the sound is increased or decreased by eight degrees, the frequency is the frequency of the sound multiplied by 2 or divided by two.

For example, if the frequency of 1 is 440Hz, the frequency of 1 (·) is 440*2 = 880Hz, and the frequency of 1 is 440/2 = 220Hz, now there is basically no problem with the tone.

Let's talk about the time:

In the profile

A normal note represents 1 shot.

If a note is followed by a-symbol like this, there are several symbols, which extend several beats.

There are also underlines under some notes, and there are two or three underscores.

For such a note, each time it has an underline, its time will be divided by two

In this way, there is basically no problem with the time.

But the most important thing is how many seconds does 1 beat?

According to the information I found, the length of one shot is not fixed. It may be 60 beats per minute or 120 beats. You can adjust the length of 1 shot to find the best one.

After learning about the basic music theory, the rest is simple, but there are countless Beep music records.

 

But what does this mechanical thing mean? We will never duplicate the wheel. Therefore, considering the required program structure and its simplicity, we can use another program to generate this program. However, we only need to enter the symbols and time of each note in the program and the length of one shot.

Now I am using a program as an example. After the program runs, a required program source code that can play music will be generated.

 

# Include <stdio. h>

Int T; // use a global variable to indicate the time of a single shot. Here, T indicates the number of beats in one minute.

Int Hz [8] = {0,440,495,550,587,660,733,825}; // use global variables to store the basic frequency of each note

FILE * p; // pointer to the target FILE

Void music (int hz, int I, int t) // defines a function to print the Beep function in the target program

{

Hz = Hz [hz];

If (I = 0) // These if statements are used to determine whether the frequency is increased to eight degrees, unchanged, or decreased to eight degrees.

Hz = hz/2;

If (I = 2)

Hz = hz * 2;

Fprintf (p, "Beep (% d, % d); \ n", hz, 60000/T/4 * t); // print Beep

}

Int main (void)

{

Int n = 1;

Int hz, t, x;

P = fopen ("music. c", "w"); // create the target file

Scanf ("% d", & T); // input T from the external to adjust the length of 1 shot.

Fprintf (p, "int main (void) \ n {\ n"); // print the Target Program

While (n)

{

Scanf ("% d", & hz, & x, & t, & n); // enter the tone of each note, whether to adjust the tone, whether to adjust the time, and used to indicate that the input is not over

Music (hz, x, t );

}

Fprintf (p, "return 0; \ n }");

Fclose (p );

Return 0;

}

 

My input data is "my mountain, my home":

80

3 1 2 1

3 1 2 1

3 1 2 1

2 1 2 1

3 1 8 1

2 1 2 1

2 1 2 1

2 1 2 1

6 0 2 1

1 1 8 1

6 0 2 1

5 0 2 1

6 0 2 1

2 1 2 1

2 1 8 1

3 1 2 1

5 1 2 1

5 1 2 1

3 1 2 1

2 1 8 1

3 1 2 1

3 1 2 1

2 1 2 1

5 1 2 1

3 1 8 1

2 1 2 1

2 1 2 1

1 1 2 1

2 1 2 1

6 0 8 1

6 0 2 1

5 0 2 1

6 0 2 1

2 1 2 1

2 1 8 1

2 1 2 1

2 1 1 1

3 1 1 1

2 1 2 1

6 0 2 1

1 1 8 1

3 1 2 1

3 1 2 1

2 1 2 1

3 1 2 1

3 1 8 1

2 1 2 1

2 1 1 1

3 1 1 1

2 1 2 1

6 0 2 1

1 1 8 1

6 0 2 1

1 1 2 1

1 1 2 1

6 0 2 1

2 1 8 1

5 1 2 1

5 1 2 1

1 1 2 1

3 1 2 1

2 1 8 1

3 1 2 1

3 1 2 1

2 1 2 1

5 1 2 1

3 1 8 1

2 1 2 1

2 1 2 1

1 1 2 1

2 1 2 1

6 0 8 1

6 0 2 1

5 0 2 1

6 0 2 1

2 1 2 1

2 1 8 1

2 1 2 1

6 0 2 1

1 1 2 1

2 1 2 1

1 1 8 1

5 1 4 1

5 1 2 1

3 1 2 1

5 1 2 1

6 1 2 1

1 2 2 1

1 2 1 1

6 1 1 1

5 1 2 1

5 1 1 1

6 1 1 1

5 1 2 1

1 1 2 1

2 1 2 1

6 1 1 1

5 1 1 1

5 1 8 1

6 1 2 1

6 1 2 1

5 1 2 1

3 1 1 1

2 1 1 1

1 1 2 1

3 1 2 1

2 1 2 1

2 1 1 1

3 1 1 1

5 1 2 1

5 1 2 1

1 1 2 1

3 1 2 1

2 1 8 1

5 1 2 1

5 1 1 1

3 1 1 1

5 1 2 1

6 1 2 1

1 2 2 1

1 2 1 1

6 1 1 1

5 1 2 1

5 1 1 1

6 1 1 1

5 1 2 1

1 1 2 1

2 1 2 1

1 1 2 1

6 0 8 1

2 1 2 1

2 1 2 1

2 1 2 1

1 1 2 1

2 1 2 1

6 1 1 1

5 1 1 1

5 1 2 1

2 1 1 1

3 1 1 1

2 1 2 1

2 1 2 1

6 0 2 1

1 1 2 1

1 1 8 1

5 1 4 1

3 1 2 1

6 1 1 1

5 1 1 1

5 1 8 1

3 1 4 1

3 1 2 1

3 1 2 1

2 1 8 1

5 1 4 1

1 1 2 1

2 1 2 1

6 0 8 1

2 1 4 1

2 1 2 1

1 1 2 1

2 1 8 1

5 1 4 1

3 1 2 1

6 1 1 1

5 1 1 1

5 1 8 1

3 1 4 1

3 1 2 1

3 1 2 1

2 1 8 1

5 1 4 1

1 1 2 1

2 1 2 1

6 0 8 1

2 1 4 1

2 1 2 1

6 0 2 1

1 1 8 1

2 1 8 1

2 1 4 1

6 0 4 1

1 1 16 0

The final source code:

Int main (void)

{

Beep (550,374 );

Beep (550,374 );

Beep (550,374 );

Beep (495,374 );

Beep (550,1496 );

Beep (495,374 );

Beep (495,374 );

Beep (495,374 );

Beep (366,374 );

Beep (expires, 1496 );

Beep (366,374 );

Beep (330,374 );

Beep (366,374 );

Beep (495,374 );

Beep (495, 1496 );

Beep (550,374 );

Beep (660,374 );

Beep (660,374 );

Beep (550,374 );

Beep (495, 1496 );

Beep (550,374 );

Beep (550,374 );

Beep (495,374 );

Beep (660,374 );

Beep (550,1496 );

Beep (495,374 );

Beep (495,374 );

Beep (440,374 );

Beep (495,374 );

Beep (366, 1496 );

Beep (366,374 );

Beep (330,374 );

Beep (366,374 );

Beep (495,374 );

Beep (495, 1496 );

Beep (495,374 );

Beep (495,187 );

Beep (550,187 );

Beep (495,374 );

Beep (366,374 );

Beep (expires, 1496 );

Beep (550,374 );

Beep (550,374 );

Beep (495,374 );

Beep (550,374 );

Beep (550,1496 );

Beep (495,374 );

Beep (495,187 );

Beep (550,187 );

Beep (495,374 );

Beep (366,374 );

Beep (expires, 1496 );

Beep (366,374 );

Beep (440,374 );

Beep (440,374 );

Beep (366,374 );

Beep (495, 1496 );

Beep (660,374 );

Beep (660,374 );

Beep (440,374 );

Beep (550,374 );

Beep (495, 1496 );

Beep (550,374 );

Beep (550,374 );

Beep (495,374 );

Beep (660,374 );

Beep (550,1496 );

Beep (495,374 );

Beep (495,374 );

Beep (440,374 );

Beep (495,374 );

Beep (366, 1496 );

Beep (366,374 );

Beep (330,374 );

Beep (366,374 );

Beep (495,374 );

Beep (495, 1496 );

Beep (495,374 );

Beep (366,374 );

Beep (440,374 );

Beep (495,374 );

Beep (expires, 1496 );

Beep (660,748 );

Beep (660,374 );

Beep (550,374 );

Beep (660,374 );

Beep (733,374 );

Beep (880,374 );

Beep (880,187 );

Beep (733,187 );

Beep (660,374 );

Beep (660,187 );

Beep (733,187 );

Beep (660,374 );

Beep (440,374 );

Beep (495,374 );

Beep (733,187 );

Beep (660,187 );

Beep (660,1496 );

Beep (733,374 );

Beep (733,374 );

Beep (660,374 );

Beep (550,187 );

Beep (495,187 );

Beep (440,374 );

Beep (550,374 );

Beep (495,374 );

Beep (495,187 );

Beep (550,187 );

Beep (660,374 );

Beep (660,374 );

Beep (440,374 );

Beep (550,374 );

Beep (495, 1496 );

Beep (660,374 );

Beep (660,187 );

Beep (550,187 );

Beep (660,374 );

Beep (733,374 );

Beep (880,374 );

Beep (880,187 );

Beep (733,187 );

Beep (660,374 );

Beep (660,187 );

Beep (733,187 );

Beep (660,374 );

Beep (440,374 );

Beep (495,374 );

Beep (440,374 );

Beep (366, 1496 );

Beep (495,374 );

Beep (495,374 );

Beep (495,374 );

Beep (440,374 );

Beep (495,374 );

Beep (733,187 );

Beep (660,187 );

Beep (660,374 );

Beep (495,187 );

Beep (550,187 );

Beep (495,374 );

Beep (495,374 );

Beep (366,374 );

Beep (440,374 );

Beep (expires, 1496 );

Beep (660,748 );

Beep (550,374 );

Beep (733,187 );

Beep (660,187 );

Beep (660,1496 );

Beep (550,748 );

Beep (550,374 );

Beep (550,374 );

Beep (495, 1496 );

Beep (660,748 );

Beep (440,374 );

Beep (495,374 );

Beep (366, 1496 );

Beep (495,748 );

Beep (495,374 );

Beep (440,374 );

Beep (495, 1496 );

Beep (660,748 );

Beep (550,374 );

Beep (733,187 );

Beep (660,187 );

Beep (660,1496 );

Beep (550,748 );

Beep (550,374 );

Beep (550,374 );

Beep (495, 1496 );

Beep (660,748 );

Beep (440,374 );

Beep (495,374 );

Beep (366, 1496 );

Beep (495,748 );

Beep (495,374 );

Beep (366,374 );

Beep (expires, 1496 );

Beep (495, 1496 );

Beep (495,748 );

Beep (366,748 );

Beep (random, 2992 );

Return 0;

}

Related Article

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.