歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 在Linux下使用Openal來播放聲音類

在Linux下使用Openal來播放聲音類

日期:2017/3/1 10:20:00   编辑:Linux編程

頭文件

  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. #include <al.h>
  10. #include <alc.h>
  11. #include <alut.h>
  12. #include <iostream>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #define numBuffer 4 //可以同時播放4個聲音
  16. #define numSource 4 //可以同時播放4個聲音
  17. using namespace std;
  18. class SoundPlay {
  19. public:
  20. SoundPlay();
  21. virtual ~SoundPlay();
  22. void PlaySound(string fileName,int index,float volume);//當index=0:循環播放;index!=0:播放1次
  23. void SetVolume(int index,float volume);
  24. void ReleaseSound(int index);
  25. void MusicPause(int index);
  26. void MusicContinue(int index);
  27. bool CheckPlayState(int i);
  28. private:
  29. void PlayLongSound(const char* fileName,int index);
  30. void OpenDevice();
  31. void CloseDevice();
  32. string GetALCErrorString(ALenum err);
  33. private:
  34. ALuint buffers[numBuffer];
  35. ALuint sources[numSource];
  36. ALCcontext* cc;
  37. ALCdevice* dev;
  38. bool checkstate;
  39. ALenum result;
  40. };
  41. #endif /* SOUNDPLAY_H_ */

cpp文件

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