關於C語言播放音樂的原始碼

來源:互聯網
上載者:User

      我們不得不說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[]= {
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
/*以上是曲子代碼,你可以嵌入自己喜歡的歌曲代碼*/
/*注意:數組中的偶數元素代表音符奇數元素代表拍長.*/
};
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;

/*以下是播放程式碼*/

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]是具體的指標*/
}


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.