音效、音樂
一行播音樂,從頭到尾寫出do re mi
上網搜尋wav檔的do re mi的音源
打開CodeBlocks
新增空專案(貝殼專案)
程式碼
#include <iostream>後面加上兩行
記得windows要在mmsystem(多媒體)前面
#include <windows.h>
#include <mmsystem.h>
main裡面
int main()
{
int a;
for(int i=0;i<10;i++)
{
cout << "Hello world!" << endl;
PlaySoundA("Do.wav", NULL, SND_ASYNC);
cin>>a;
}
return 0;
}
把剛剛下載的do re mi音檔放進project的目錄裡
回到CodeBlocks
編輯屬性
專案按右鍵
選Build option...
Linker Setting裡
咒語"winmm"(windows裡的多媒體)
執行程式碼
按任意鍵在Enter會播放DO.wav
課堂作業二
比較SND_ASYNC(非同步)與SND_SYNC(同步)的差別
SND_SYNC(同步)需要播完之後才能播下一個音檔
課堂作業三
鍵盤=>鋼琴
用原本的專案改就行
最開頭加上#include <GL/glut.h>
把Do~Si都寫好程式碼(數字鍵1~7)
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') PlaySoundA("Do.wav", NULL, SND_ASYNC);
if(key=='2') PlaySoundA("Re.wav", NULL, SND_ASYNC);
if(key=='3') PlaySoundA("Mi.wav", NULL, SND_ASYNC);
if(key=='4') PlaySoundA("Fa.wav", NULL, SND_ASYNC);
if(key=='5') PlaySoundA("Sol.wav", NULL, SND_ASYNC);
if(key=='6') PlaySoundA("La.wav", NULL, SND_ASYNC);
if(key=='7') PlaySoundA("Si.wav", NULL, SND_ASYNC);
}
main裡面:
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("3D sound version");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
因為用到glut所以要在Linker Setting裡新增咒語
跟連結上include
專案按右鍵
選Build option...
在Searth directories標籤下的Compiler標籤加入
在freeglut資料夾裡面的include資料夾路徑
Linker標簽下加入在freeglut資料夾裡面的lib資料夾路徑
Linker Setting加新咒語
freeglut
opengl32
gul32
gdi32
最後把freeglut.dll移進專案資料夾裡
執行後就能把鍵盤數字鍵當Piano
播放mp3檔案
用原本的專案改就行
先把老師給的CMP3_MCI放進專案資料夾
找一個mp3音檔
放進專案資料夾
開頭加上
#include "CMP3_MCI.h"
CMP3_MCI myMP3;
main裡加上
myMP3.Load("aine.mp3"); //讀入音檔
myMP3.Play(); //播放
執行就會播放














沒有留言:
張貼留言