2023年4月26日 星期三

ww-week11-鍵盤_聲音(wav,mp3)

1.加入按鍵:

新增Project / 選擇GLUT project

Go / Next / 先設定存放位置 / 再更改檔案名稱week11-1_keyboard


小葉老師上課軟體 / freeglut檔案複製到桌面 / 將lib/freeglut.a複製貼上後改成libglut32.a



按一下Esc(=27)茶壺視窗會結束



#include <GL/glut.h>
void display()
{
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
    if(key==27) exit(0);//按Esc結束視窗
}
int main(int argc, char **argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week11");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);

    glutMainLoop();
} 
2.加入音樂:
新增Project / 選擇GLUT project / Go / Next / 先設定存放位置 / 再更改檔案名稱week11-2_keyboard_wav_playsound / 複製上一個程式 / 加入PlaySound函式撥放音樂


#include <windows.h>//放上面
#include <GL/glut.h>

void display()
{
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
if(key=='1')PlaySound("C:\\Users\\Administrator\\Desktop\\do_re_mi\\do.wav",NULL,SND_ASYNC);///按1時播放聲音do
 
if(key=='2')PlaySound("C:\\Users\\Administrator\\Desktop\\do_re_mi\\re.wav",NULL,SND_ASYNC);///絕對位置要兩個斜線
    
if(key=='3')PlaySound("C:\\Users\\Administrator\\Desktop\\do_re_mi\\mi.wav",NULL,SND_ASYNC);///PlaySound三個參數
}
int main(int argc, char **argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week11");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);

    glutMainLoop();
}
3.手動把winmm加入lib咒語裡:
重新開啟CodeBlocks / 開新Empty file:week11-3_PlaySound.cpp /寫程式只有include windows.h和playsound的程式 / setting / compiler / Linker settings / 加上winmm咒語
※OpenCV要3個咒語,GLUT要5咒語,PlaySound要1咒語



#include <windows.h>
int main()
{
    PlaySound("C:\\Users\\Administrator\\Desktop\\do_re_mi\\do.wav",NULL,SND_SYNC);//第三個參數SYNC等待同步,以免瞬間結束
    PlaySound("C:\\Users\\Administrator\\Desktop\\do_re_mi\\re.wav",NULL,SND_SYNC);
    PlaySound("C:\\Users\\Administrator\\Desktop\\do_re_mi\\mi.wav",NULL,SND_SYNC);
}

4.MP3檔: 

開新專案week11-4_glut_mp3 / 在標頭20行位置加入#include "CMP3_MCI.h"插入外掛 / 將CMP3_MCI.h檔放到week11-4_glut_mp3目錄下 / 加入程式碼

...
#include "CMP3_MCI.h"///插入外掛
CMP3_MCI myMP3;///宣告變數

int main(int argc, char *argv[])
{
    char filename[]="C:\\Users\\Administrator\\Desktop\\do_re_mi\\suzumi.mp3";
    myMP3.Load(filename);
    myMP3.Play();///播放
...

將絕對路徑簡化成相對路徑:存檔後用notepad++開啟 week11-4_glut_mp3.cbp / 更改11行working_dir變成="."

 

將mp3檔和freeglut / bin內的freeglut.dll加入執行檔

C:\Users\Administrator\Desktop\week11-4_glut_mp3 / 把絕對入徑改成相對入徑



git上傳時,gitignore由於.dll 檔不備分,但我們需要libfreeglut.dll 及opencv.dll / 因此先將2023graphicsac clone下來 / 用notepad++開啟.gitignore/在.dll前加#


git指令
cd desktop
git clone https://github.com/你的帳號/2023graphicsa
cd 2023graphicsa
加入檔案
git add . 
git status

git config --global user.email "jsyeh@mail.mcu.edu.tw"

git config --global user.name "jsyeh"

git commit -m "week10"

git push


沒有留言:

張貼留言