关于C语言播放音乐的源代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

关于C语⾔播放⾳乐的源代码
我们不得不说C语⾔是⼀种还算得上强⼤的语⾔,今天就来展⽰⼀下如何通过C语⾔去播放⾳乐:⽹上有很多这⽅⾯的资料,我只是稍加整理了⼀下,随便玩⼀下。

先来看第⼀个例程,这是⽐较简单的:
#include<stdio.h>
#include<dos.h>
#include<bios.h>
#include<time.h>
#include<graphics.h>
#define N1 32 /*⼀拍*/
#define N2 16
#define N4 8
#define N8 4
#define N16 2
#define END 0
/*其中:N1,N2,N4,N6,N8,N16分别代表
⼀拍,⼆分之⼀拍,四分之⼀拍,
⼋分之⼀拍,⼗六分之⼀拍。

*/
/*声明⼀个图形显⽰*/
void printtext(unsigned char *temp[],int i, int color); /* ⽤于在屏幕上显⽰字符串 */
enum NOTES
{
C10=131,D10=147,E10=165,F10=175,G10=196,A10=220,B10=247,
/*低度⾳调分别是:1,2,3,4,5,6,7*/
C0=262, D0=286, E0=330, F0=349, G0=440, A0=440, B0=494,
/*中度⾳调分别是:1,2,3,4,5,6,7*/
C1=523, D1=158, E1=659, F1=698, G1=784, A1=880, B1=998,
/*⾼度⾳调分别是:1,2,3,4,5,6,7*/
C2=1047,D2=1175,E2=1319,F2=1397,G2=1568,A2=1760,B2=1976
/*⾼⼋度⾳调分别是: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
/*以上是曲⼦代码,你可以嵌⼊⾃⼰喜欢的歌曲代码*/
/*注意:数组中的偶数元素代表⾳符奇数元素代表拍长.*/
};
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
/*以上是⾳符测试代码,你可以通过系统发出的⾳调选择你需要的⾳符*/
/*注意:数组中的偶数元素代表⾳符奇数元素代表拍长.*/
};
song3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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;
/*以下是播放程序代码*/
void MusicPlay(int *song)
{
int note=0,dur,control;
clock_t goal;
while(song[note]!=0)
{
tone.fre=song[note];
dur=song[note+1]; /*节拍*/
if(kbhit())break;
/*int kbhit()函数返回最近所敲的按键*/
if(tone.fre)
{
outportb(0x43,0xB6);
/*函数void outportb(int port,char byte)
将字节byte写⼊指定的输出端⼝port*/
tone.fre=(unsigned)(1193180L/tone.fre);
outportb(0x42,tone.c[0]);
outportb(0x42,tone.c[1]);
control=inportb(0x61);
/*函数int inportb(int port)从指定的
输⼊端⼝读⼊⼀个字节,并返回这个字节*/
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;
}
}
/*以下是主函数其中包括异常处理*/
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,""); /*系统初始化 */
cleardevice(); /* 清除屏幕 */
settextjustify(LEFT_TEXT,CENTER_TEXT); /*设置字符排列⽅式*/
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]); /* 显⽰字符串 temp[i]是具体的指针*/ }。

相关文档
最新文档