Source code for playing music in C Language

Source: Internet
Author: User

We have to say that C language is a powerful language. Today we will show you how to play music through C language: There are a lot of materials on the Internet, I just sorted it out a little and tried it.

Let's take a look at the first routine, which is relatively simple:

# Include <stdio. h>
# Include <dos. h>
# Include <bios. h>
# Include <time. h>
# Include <graphics. h>
# Define N1 32/* single shot */
# Define N2 16
# Define N4 8
# Define n8 4
# Define n16 2
# Define end 0
/* Where N1, N2, N4, N6, n8, and n16 represent
One shot, 1/2 shots, 1/4 shots,
1/8 beats, 1/16 beats. */
/* Declare a graphic display */
Void printtext (unsigned char * temp [], int I, int color);/* used to display strings on the screen */
Enum notes
{
C10 = 131, D10 = 147, E10 = 165, F10 = 175, G10 = 196, A10 = 220, B10 = 247,
/* Low tones: 1, 2, 3, 4, 5, 6, 7 */
C0 = 262, D0 = 286, e0 = 330, f0 = 349, G0 = 440, a0 = 440, b0 = 494,
/* Moderate tones: 1, 2, 3, 4, 5, 6, 7 */
C1 = 523, d1 = 158, e1 = 659, F1 = 698, G1 = 784, a1 = 880, b1 = 998,
/* The height tones are: 1, 2, 3, 4, 5, 6, 7 */
C2 = 1047, D2 = 1175, E2 = 1319, f2 = 1397, G2 = 1568, a2 = 1760, b2 = 1976
/* High octal tones: 1, 2, 3, 4, 5, 6, 7 */
}
Song1 [] = {
G0, n16, G0, n8, A0, n8, G0, n8, C1, n8, B0, N4, G0, n16,
G0, n8, A0, n8, G0, n8, D1, n8, C1, N4, G0, n16, G0, n8, G1, n8,
E1, n8, C1, n8, B0, n16, B0, n8, A0, N4, F1, n16, F1, n8, E1,
N8, C1, n8, D1, n8, C1, N4, end, end
/* The above is the song code. You can embed your favorite song Code */
/* Note: The even elements in the array represent the odd elements of the notes .*/
};
Song2 [] = {
C10, N1, D10, N1, E10, N1, F10, N1, G10, N1, A10, N1, B10, N1,
C10, N2, D10, N2, E10, N2, F10, N2, G10, N2, A10, N2, B10, N2,
C10, N4, D10, N4, E10, N4, F10, N4, G10, N4, A10, N4, B10, N4,
C10, n8, D10, n8, E10, n8, F10, n8, G10, n8, A10, n8, B10, n8,
C10, n16, D10, n16, E10, n16, F10, n16, G10, n16, A10, n16, B10, n16,
C0, N1, D0, N1, E0, N1, F0, N1, G0, N1, A0, N1, B0, N1,
C0, N2, D0, N2, E0, N2, F0, N2, G0, N2, A0, N2, B0, N2,
C0, N4, D0, N4, E0, N4, F0, N4, G0, N4, A0, N4, B0, N4,
C0, n8, D0, n8, E0, n8, F0, n8, G0, n8, A0, n8, B0, n8,
C0, n16, D0, n16, E0, n16, F0, n16, G0, n16, A0, n16, B0, n16,
C1, N1, D1, N1, E1, N1, F1, N1, G1, N1, A1, N1, B1, N1,
C1, N2, D1, N2, E1, N2, F1, N2, G1, N2, A1, N2, B1, N2,
C1, N4, D1, N4, E1, N4, F1, N4, G1, N4, A1, N4, B1, N4,
C1, n8, D1, n8, E1, n8, F1, n8, G1, n8, A1, n8, B1, n8,
C1, n16, D1, n16, E1, n16, F1, n16, G1, n16, A1, n16, B1, n16,
C2, N1, D2, N1, E2, N1, F2, N1, G2, N1, A2, N1, B2, N1,
C2, N2, D2, N2, E2, N2, F2, N2, G2, N2, A2, N2, B2, N2,
C2, N4, D2, N4, E2, N4, F2, N4, G2, N4, A2, N4, B2, N4,
C2, n8, D2, n8, E2, n8, F2, n8, G2, n8, A2, n8, B2, n8,
C2, n16, D2, n16, E2, n16, F2, n16, G2, n16, A2, n16, B2, n16, end
/* The above is the note test code. You can select the note you need through the system's tone */
/* Note: The even elements in the array represent the odd elements of the notes .*/
};
Song3 [] = {
C0, n8, C0, n8, G0, n8, G0, n8, A0, n8, A0, n8, G0, N4,
F0, n8, F0, n8, E0, n8, E0, n8, D0, n8, D0, n8, C0, N4,
G0, n8, G0, n8, F0, N4, E0, n8, E0, n8, D0, N4, G0, n8,
G0, n8, F0, n8, F0, n8, E0, n8, E0, n8, D0, N4, C0, n8,
E0, n8, G0, N4, A0, n8, C1, n8, G0, N4, F0, n8, F0, n8,
E0, n8, E0, N4, D0, n8, D0, n8, C0, N4, C0, n8, C0, n8,
G0, n8, G0, n8, A0, n8, A0, n8, G0, N4, F0, n8, F0, n8,
E0, n8, E0, n8, D0, n8, D0, n8, C0, N4, G0, n8, G0, n8,
F0, N4, E0, n8, E0, n8, D0, N4, G0, n8, G0, n8, F0, n8,
F0, n8, E0, n8, E0, n8, D0, N4, C0, n8, E0, n8, G0, N4,
A0, n8, C1, n8, G0, N4, F0, n8, F0, n8, E0, n8, E0, N4,
D0, n8, D0, n8, C0, N4, end
/* The above is the song code. You can embed your favorite song Code */
/* Note: The even elements in the array represent the odd elements of the notes .*/
};
Song4 [] = {
C10, N4, B0, N4, G0, n8, A0, N4, E0, n16, D0, n8, C0, n8,
D0, n8, G0, n8, E0, n8, E0, N4, E0, n8, G0, n8, E0, n8,
D0, n8, D0, N4, A0, n8, A0, n8, G0, n8, A0, n8, B0, n8,
G0, n8, G0, n8, E0, N4, C10, n8, B0, n8, G0, n8, A0, N4,
E0, n8, D0, n8, C0, n8, D0, n8, G0, n8, E0, N4, C0, n8, D0, n8,
E0, N4, G0, n8, E0, n8, D0, N4, C0, n8, B10, n8, C0, n8, B10, n8,
A10, N4, A0, n8, A0, n8, A0, n8, C1, n8, D1, n8, E1, n8, E1, n8,
C1, n8, A0, n8, A0, n8, B0, n16, A0, n16, G0, n8, E0, n8, A0, n8,
A0, n8, C1, n8, D1, n8, E1, n8, E1, n8, C1, n8, A0, n8, A0, n8,
B0, n8, A0, n8, B0, n16, A0, n16, G0, n8, E0, n8, D1, n16, D1, n16, D1, n8,
D1, n8, A0, n16, B0, n16, A0, n8, G0, n8, E0, n8, D1, n16, D1, n16, D1, n8,
A0, n16, B0, n16, A0, n8, G0, n8, E0, n8, A0, n8, A0, n8, A0, N4, A0, n8,
A0, n8, A0, N4, C10, n8, B0, n8, G0, n8, A0, N4, E0, n16, D0, n16, C0, n8,
D0, n8, G0, n8, E0, N4, E0, N4, E0, n16, G0, n16, E0, n16,
D0, n16, D0, N4, A0, n16, A0, n16, G0, n8, A0, n8, B0, n8,
G0, n16, G0, n16, E0, N4, C10, n8, B0, n8, G0, n8, A0, N4,
E0, n16, D0, n16, C0, n8, D0, n8, G0, n8, E0, N4, C0, n16, D0, n16,
E0, N4, G0, n8, E0, n8, D0, N4, C0, n8, B10, n8, C0, n16, B10, n16,
A10, N4, end };

Union {
Unsigned fre;
Unsigned char C [2];
} Tone;

/* The following is the player code */

Void musicplay (int * song)
{
Int note = 0, dur, control;
Clock_t goal;
While (song [note]! = 0)
{
Tone. Fre = song [note];
Dur = song [Note + 1];/* beat */
If (kbhit () break;
/* The int kbhit () function returns the recently typed button */
If (tone. FRE)
{
Outportb (0x43, 0xb6 );
/* Function void outportb (INT port, char byte)
Write byte to the specified output port */
Tone. Fre = (unsigned) (1193180l/tone. FRE );
Outportb (0x42, Tone. C [0]);
Outportb (0x42, Tone. C [1]);
Control = inportb (0x61 );
/* Function int inportb (INT port) from the specified
The input port reads a byte and returns this Byte */
Outportb (0x61, (Control) | 0x3 );
}
Goal = (clock_t) dur + clock ();
While (Goal> clock ());
If (tone. FRE)
Outportb (0x61, control );
Goal = (clock_t) 0;
Note = Note + 2;
}
}
/* The following are the main functions, including Exception Handling */
Void main (void)
{Int N;
Int graphdriver, graphmode;
Unsigned char * Text [7];
Text [0] = "";
Text [1] = "Start music ........";
Text [2] = "Play the one music ";
Text [3] = "play the two music ";
Text [4] = "Play the Three Music ";
Text [5] = "Play the four music ";
Text [6] = "good_bye ............";
Graphdriver = detect;
Graphmode = 0;
Initgraph (& graphdriver, & graphmode, "");/* system initialization */
Cleardevice ();/* clear screen */

Settextjustify (left_text, center_text);/* set the character arrangement mode */


Printtext (text, 1, 1 );
Getch ();
Printtext (text, 2, 62 );
Musicplay (song1 );
Getch ();
Printtext (text, 3, 4 );
Musicplay (song2 );
Getch ();
Printtext (text, 4, 58 );
Musicplay (song3 );
Getch ();
Printtext (text, 5, 61 );
Musicplay (song4 );
Getch ();
Printtext (text, 6, 63 );
Getch ();
Closegraph ();
}
Void printtext (unsigned char * temp [], int I, int color)
{
Setcolor (color );
Settextstyle (triplex_font, horiz_dir, 3 );
Outtextxy (100, 40 + I * 50, temp [I]);/* The display string temp [I] is a specific pointer */
}


 

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.