手机版
你好,游客 登录 注册
背景:
阅读新闻

在Linux下使用Openal来播放声音类

[日期:2012-06-12] 来源:Linux公社  作者:ouyangshima [字体: ]

头文件

  1. /* 
  2.  * SoundPlay.h 
  3.  *  1:需要下载开发openal开发包(Software implementation of the OpenAL API(devlopment files))和alut开发包 
  4.  *  2:添加头文件路径:/usr/include/AL 
  5.  *  3:添加库:openal和alut 
  6.  */  
  7. #ifndef SOUNDPLAY_H_   
  8. #define SOUNDPLAY_H_   
  9.   
  10. #include <al.h>   
  11. #include <alc.h>   
  12. #include <alut.h>   
  13.   
  14. #include <iostream>   
  15. #include <stdlib.h>   
  16. #include <stdio.h>   
  17.   
  18. #define numBuffer 4   //可以同时播放4个声音   
  19. #define numSource 4   //可以同时播放4个声音   
  20.   
  21. using namespace std;  
  22. class SoundPlay {  
  23. public:  
  24.     SoundPlay();  
  25.     virtual ~SoundPlay();  
  26.     void PlaySound(string fileName,int index,float volume);//当index=0:循环播放;index!=0:播放1次   
  27.     void SetVolume(int index,float volume);  
  28.     void ReleaseSound(int index);  
  29.     void MusicPause(int index);  
  30.     void MusicContinue(int index);  
  31.     bool CheckPlayState(int i);  
  32. private:  
  33.     void PlayLongSound(const char* fileName,int index);  
  34.     void OpenDevice();  
  35.     void CloseDevice();  
  36.     string GetALCErrorString(ALenum err);  
  37. private:  
  38.     ALuint buffers[numBuffer];  
  39.     ALuint sources[numSource];  
  40.     ALCcontext* cc;  
  41.     ALCdevice* dev;  
  42.     bool checkstate;  
  43.     ALenum result;  
  44. };  
  45. #endif /* SOUNDPLAY_H_ */  

cpp文件

  1. /* 
  2.  *  SoundPlay.cpp 
  3.  */  
  4. #include "SoundPlay.h"   
  5.   
  6. SoundPlay::SoundPlay()  
  7. {  
  8.     alutInit(NULL,0);  
  9.     if ((result=alGetError()) != AL_NO_ERROR)  
  10.         cout<<"alutInit--"<<result<<endl;  
  11.     OpenDevice();  
  12.     //PlaySound("Media/Music/BGSound1.wav",0,100);   
  13. }  
  14. SoundPlay::~SoundPlay()  
  15. {  
  16.     CloseDevice();  
  17. }  
  18. void SoundPlay::OpenDevice()  
  19. {  
  20.     ALCchar DeviceName[] = "ALSA Software";//ALSA Software,DirectSound3D   
  21.     dev=alcOpenDevice(DeviceName);  
  22.     cc=alcCreateContext(dev,NULL);  
  23.     alcMakeContextCurrent(cc);  
  24. }  
  25. void SoundPlay::CloseDevice()  
  26. {  
  27.     ALCcontext* context = alcGetCurrentContext();  
  28.     ALCdevice* device = alcGetContextsDevice( context );  
  29.     alcMakeContextCurrent( NULL );  
  30.     alcDestroyContext( context );  
  31.     alcCloseDevice( device );  
  32. }  
  33. void SoundPlay::ReleaseSound(int index)  
  34. {  
  35.     alDeleteSources(1,&sources[index]);  
  36.     alDeleteBuffers(1,&buffers[index]);  
  37. }  
  38. void SoundPlay::PlayLongSound(const char* fileName,int index)  
  39. {  
  40.     alGenSources(1,&sources[index]);  
  41.     alGenBuffers(1,&buffers[index]);  
  42.     ALvoid *data;  
  43.     ALsizei size=0,freq=0;  
  44.     ALenum format;  
  45.     ALboolean loop;  
  46.     alutLoadWAVFile((ALbyte *)fileName,&format,&data,&size,&freq,&loop);  
  47.     alBufferData(buffers[index],format,data,size,freq);  
  48.     if ((result=alGetError()) != AL_NO_ERROR)  
  49.         cout<<"PlayLongSound alBufferData errno"<<result<<endl;  
  50.     alutUnloadWAV(format,data,size,freq);  
  51.     alSourcei(sources[index],AL_BUFFER,buffers[index]);//用音源关联缓冲器   
  52.     if(index==0)  
  53.         alSourcei(sources[index],AL_LOOPING,AL_TRUE);  
  54.     alSourcePlay(sources[index]);  
  55. }  
  56. void SoundPlay::PlaySound(string fileName,int index,float volume)  
  57. {  
  58.     alGenSources(1,&sources[index]);  
  59.     alGenBuffers(1,&buffers[index]);  
  60.     ReleaseSound(index);  
  61.     PlayLongSound(fileName.data(),index);  
  62.     alSourcef(sources[index],AL_GAIN,volume);  
  63. }  
  64. void SoundPlay::SetVolume(int index,float volume)//volume取值范围(0~1)   
  65. {  
  66.     alSourcef(sources[index],AL_GAIN,volume);  
  67. }  
  68. void SoundPlay::MusicPause(int index)  
  69. {  
  70.     alSourcePause(sources[index]);  
  71.     alSourceStop(sources[index]);  
  72. }  
  73. void SoundPlay::MusicContinue(int index)  
  74. {  
  75.     alSourcePlay(sources[index]);  
  76. }  
  77. bool SoundPlay::CheckPlayState(int i)  
  78. {  
  79.     ALint state;  
  80.     alGetSourcei(sources[i], AL_SOURCE_STATE, &state);  
  81.     if(state != AL_PLAYING)  
  82.     {  
  83.         checkstate=false;  
  84.         return true;  
  85.     }  
  86.     return false;  
  87. }  
  88. int main()  
  89. {  
  90.     SoundPlay sp;  
  91.     sp.PlaySound("Media/Music/BGSound1.wav",0,100);  
  92.     while(1){}  
  93.     return 0;  
  94. }  
linux
相关资讯       Linux编程  Openal 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款