Develop music programs in C Language

Source: Internet
Author: User

Develop music in C LanguageProgram

Using C language to develop music programs mainly uses two library functions:

Sound () enables the speaker of a PC at a specified frequency. The frequency is in Hz.

Nosound () Close the speaker.

Preparation of a music score File

When writing a music program, you must first create a music score file for the music to be played. The music score file can be marked with different symbols, as long as the program is easy to recognize.

The following is a rule for preparing music score documents: Add "*" to the front of each tone, and add "H" to the front of each tone, add "M" to the front of each sound, and "L" to the front of each sound ".

"*", "H", "M", and "L" constitute the pitch and determine the voice frequency. The pitch is followed by the sound length. An integer or decimal number can be used to control the delay, but spaces must be used to separate the pitch. The front end of the music score file is an integer, indicating the base number of the music length, which is generally a multiple of 300. The end of a music score file is the music score file Terminator "#", which indicates the end of the music score file.

The length of each sound = the base number of the sound length * The number of beats. The base number is the first character of the music score file. For example, the music score File above is 3600, the audio of each audio can be input with a simulated frequency value. Below are the frequency values of each note in the center C and the four before and after 8 degrees.

Low 8 degrees 1 2 3 4 5 6 7

131 147 165 176 196 220 247

Central C 1 2 3 4 5 6 7

262 296 329.7 349.2 392 440 493.9

High 8 degrees 1 2 3 4 5 6 7

523.3 587.3 659.3 698.5 784.0 880 987.8

Maximum 8 degrees 1 2 3 4 5 6 7

1046.5 1174.7 1318.5 1396.9 1568.0 1760 1975.5

Source code 2

# Include <stdio. h>

# Include <stdlib. h>

# Include <dos. h>

Void main (INT argc, char * argv [])

{

File * FP;

Int rate;

Char sound_high [3];

Float sound_long;

Register int I = 0, J;

Int Sign = 0;

Float STR [200] [2];

If (argc! = 2)/* incorrect number of command line parameters */

{

Printf ("parameters errors! \ N ");

Exit (1 );

}

If (FP = fopen (argv [1], "R") = NULL)/* file opening failure */

{

Printf ("Open File music.doc errors! \ N ");

Exit (1 );

}

Fscanf (FP, "% d", & rate);/* read the Base Value of the audio length */

While (! Feof (FP )&&! Sign)/* the file is not over and the data is still music score */

{

Fscanf (FP, "% S % F", sound_high, & sound_long);/* obtain the audio and audio length value */

STR [1] = Rate * sound_long;/* Sound Length = base * Number of beats */<brtch (sound_high [0])

{

Case '*':/* Top Note */

{

Case '1 ':

STR [I ++] [0] = 1046.5;

Break;

Case '2 ':

STR [I ++] [0] = 1174.7;

Break;

Case '3 ':

STR [I ++] [0] = 1318.5;

Break;

Case '4 ':

STR [I ++] [0] = 1396.9;

Break;

Case '5 ':

STR [I ++] [0] = 1568;

Break;

Case '6 ':

STR [I ++] [0] = 1760;

Break;

Case '7 ':

STR [I ++] [0] = 1975.5;

Break;

Default:

Printf ("\ nerrors in music.doc \ n ");

Exit (1 );

}

Case 'H':/* tweeter */<brtch (sound_high [1])

{

Case '1 ':

STR [I ++] [0] = 523.3;

Break;

Case '2 ':

STR [I ++] [0] = 587.3;

Break;

Case '3 ':

STR [I ++] [0] = 659.3;

Break;

Case '4 ':

STR [I ++] [0] = 698.5;

Break;

Case '5 ':

STR [I ++] [0] = 784.0;

Break;

Case '6 ':

STR [I ++] [0] = 880;

Break;

Case '7 ':

STR [I ++] [0] = 987.8;

Break;

Default:

Printf ("\ nerrors in music.doc \ n ");

Exit (1 );

}

Case 'M':/* Zhongyin */<brtch (sound_high [1])

{

Case '1 ':

STR [I ++] [0] = 262;

Break;

Case '2 ':

STR [I ++] [0] = 296;

Break;

Case '3 ':

STR [I ++] [0] = 329.6;

Break;

Case '4 ':

STR [I ++] [0] = 349.2;

Break;

Case '5 ':

STR [I ++] [0] = 392;

Break;

Case '6 ':

STR [I ++] [0] = 440;

Break;

Case '7 ':

STR [I ++] [0] = 493.9;

Break;

Default:

Printf ("\ nerrors in music.doc \ n ");

Exit (1 );

}

Case 'l':/* Bass */<brtch (sound_high [1])

{

Case '1 ':

STR [I ++] [0] = 131;

Break;

Case '2 ':

STR [I ++] [0] = 147;

Break;

Case '3 ':

STR [I ++] [0] = 165;

Break;

Case '4 ':

STR [I ++] [0] = 176;

Break;

Case '5 ':

STR [I ++] [0] = 196;

Break;

Case '6 ':

STR [I ++] [0] = 220;

Break;

Case '7 ':

STR [I ++] [0] = 247;

Break;

Default:

Printf ("\ nerrors in music.doc \ n ");

Exit (1 );

}

Case '#':

If (sound_high [1] = '#') Sign = 1;

Break;

Default:

Printf ("\ nerrors in music.doc \ n ");

Exit (1 );

}

}

For (j = 0; j <= I-1; j ++)

{

Sound (STR [J] [0]);/* Frequency-based song tones */

Delay (50 * STR [J] [1]);

}

Nosound ();/* Turn off the speaker */

}

three music scores: bach's Mini dance
600 H2 4 M5 1 M6 1 M7 1 H1 1 H2 4 M5 4 M5 4 H3 4 H1 1 H2 1 H3 1 H4 1 H5 4 M5 4 M5 4 H1 4 H2 1 H1 1 M7 1 M6 1 M7 4 H1 1 M7 1 M6 1 M5 1 M4 4 M5 1 M6 1 M7 1 M5 1 M6 6 L2 1 m1 1 L7 1 L6 1 H2 4 M5 1 M6 1 M7 1 H1 1 H2 4 M5 4 M5 4 H3 4 H1 1 H2 1 H3 1 H4 1 H5 4 M5 4 M5 4 H1 4 H2 4 H2 1 H1 1 M7 1 M6 1 M7 4 H1 1 M7 1 M6 1 M5 1 M6 4 M7 1 M6 1 M5 1 M4 1 M5 10 ##< br>

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.