2020年5月12日 星期二

Week11 聲音 /*CMP3_MCI

用openGL叫出聲音
基本:
#include <stdio.h>
#include <windows.h>
#include <mmsystem.h>

int main()
{
    PlaySoundA("Shoutgun198.wav",NULL,SND_SYNC);
    PlaySoundA("ak47.wav",NULL,SND_SYNC);
    printf("ak47\n");
    PlaySoundA("launch_dnmenu1.wav",NULL,SND_SYNC);
}
加入滑鼠點擊:
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <mmsystem.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{
    if(state==GLUT_DOWN){
        PlaySoundA("Shoutgun198.wav",NULL,SND_SYNC);
        PlaySoundA("ak47.wav",NULL,SND_SYNC);
        printf("ak47\n");
        PlaySoundA("launch_dnmenu1.wav",NULL,SND_SYNC);}
}
int main(int argc, char**argv)
{///只能用WAV檔,放 freeglut/bin 裡
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week11 sound");

    glutMouseFunc(mouse);
    glutDisplayFunc(display);
    glutMainLoop();
}
播放MP3檔:
下載CMP3_MCI.h ,放入GLUT專案內
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <mmsystem.h>
#include "CMP3_MCI.h"///雙引號,表示要在同一目錄找
CMP3_MCI myMP3;///宣告一變數 myMP3

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{
    if(state==GLUT_DOWN){
        PlaySoundA("Shoutgun198.wav",NULL,SND_SYNC);
        PlaySoundA("ak47.wav",NULL,SND_SYNC);
        printf("ak47\n");
        PlaySoundA("launch_dnmenu1.wav",NULL,SND_SYNC);}
}
int main(int argc, char**argv)
{///只能用WAV檔,放 freeglut/bin 裡
    myMP3.Load("bgm1.mp3");///讀入檔案
    myMP3.Play();///播放
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week11 sound");

    glutMouseFunc(mouse);
    glutDisplayFunc(display);
    glutMainLoop();
}
補充:PlaySoundA("ak47.wav",NULL,SND_SYNC); 
SND_SYNC 改成 SND_ASYNC
           PlaySoundA("ak47.wav",NULL,SND_ASYNC); 
              聲音就會不等結束,直接再播一次(不同步)


用notepad++開啟 專案.cbp
就可以將音效檔放入專案內,而不是 freeglut/bin 內了!!

用keyboard按出聲音:
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <mmsystem.h>
#include "CMP3_MCI.h"
CMP3_MCI myMP3;
CMP3_MCI Do,Re,Mi,Fa,So;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glutSwapBuffers();
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='1') Do.Play();
    if(key=='2') Re.Play();
    if(key=='3') Mi.Play();
    if(key=='4') Fa.Play();
    if(key=='5') So.Play();

}
void mouse(int button,int state,int x,int y)
{
    if(state==GLUT_DOWN){
        PlaySoundA("ak47.wav",NULL,SND_SYNC);
        printf("ak47\n");
        PlaySoundA("launch_dnmenu1.wav",NULL,SND_ASYNC);}
}
int main(int argc, char**argv)
{
    Do.Load("do.wav");
    Re.Load("re.wav");
    Mi.Load("mi.wav");
    Fa.Load("fa.wav");
    So.Load("so.wav");

    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week11 sound");

    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);
    glutMainLoop();
}

沒有留言:

張貼留言