AudioManager.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #ifndef AUDIO_MANAGER_H
  2. #define AUDIO_MANAGER_H
  3. #include <OgreSingleton.h>
  4. #include <boost/thread.hpp>
  5. #include <vorbis/vorbisfile.h>
  6. // Include OpenAL
  7. #if defined(__WIN32__) || defined(_WIN32)
  8. #include <al.h>
  9. #include <alc.h>
  10. #else
  11. #include <AL/al.h>
  12. #include <AL/alc.h>
  13. #endif
  14. class AudioManager : public Ogre::Singleton< AudioManager >
  15. {
  16. public:
  17. AudioManager();
  18. virtual ~AudioManager();
  19. // boost uses this
  20. void operator()();
  21. void Update();
  22. void MusicPause();
  23. void MusicPlay( const Ogre::String& name );
  24. void MusicStop();
  25. struct Music
  26. {
  27. Ogre::String name;
  28. Ogre::String file;
  29. float loop;
  30. };
  31. void AddMusic( const AudioManager::Music& music );
  32. AudioManager::Music* GetMusic( const Ogre::String& name );
  33. private:
  34. const bool Init();
  35. const char* ALError();
  36. const char* ALCError( const ALCdevice* device );
  37. class Player
  38. {
  39. public:
  40. Player( boost::recursive_mutex* mutex );
  41. ~Player();
  42. void Pause();
  43. void Play( const Ogre::String& file );
  44. void Stop();
  45. void SetLoop( const float loop );
  46. void Update();
  47. float GetPosition();
  48. private:
  49. boost::recursive_mutex* m_UpdateMutex;
  50. float m_Loop;
  51. OggVorbis_File m_VorbisFile;
  52. vorbis_info* m_VorbisInfo;
  53. int m_VorbisSection;
  54. bool m_StreamFinished;
  55. ALuint m_Source;
  56. ALsizei FillBuffer();
  57. };
  58. private:
  59. bool m_Initialized;
  60. ALCdevice* m_ALDevice;
  61. ALCcontext* m_ALContext;
  62. char* m_Buffer;
  63. boost::recursive_mutex m_UpdateMutex;
  64. boost::thread* m_UpdateThread;
  65. bool m_ThreadContinue;
  66. AudioManager::Player m_Music;
  67. std::list< AudioManager::Music > m_MusicList;
  68. // allocate for every new player two 96Kb buffer chunks
  69. // every chunk would buffer ~0.5 seconds of 44100Hz stereo 16-bit data
  70. // in that case we can sleep updating buffers for 250ms
  71. static ALsizei m_ChannelBufferSize;
  72. static int m_ChannelBufferNumber;
  73. };
  74. #endif // AUDIO_MANAGER_H