1 #ifndef GAME_SOUND_OPENAL_OUTPUT_H
2 #define GAME_SOUND_OPENAL_OUTPUT_H
3 
4 #include <string>
5 #include <vector>
6 #include <map>
7 #include <deque>
8 
9 #include "alc.h"
10 #include "al.h"
11 #include "alext.h"
12 
13 #include "sound_output.hpp"
14 
15 namespace MWSound
16 {
17     class SoundManager;
18     class Sound;
19     class Stream;
20 
21     class OpenAL_Output : public Sound_Output
22     {
23         ALCdevice *mDevice;
24         ALCcontext *mContext;
25 
26         struct {
27             bool EXT_EFX : 1;
28             bool SOFT_HRTF : 1;
29         } ALC = {false, false};
30         struct {
31             bool SOFT_source_spatialize : 1;
32         } AL = {false};
33 
34         typedef std::deque<ALuint> IDDq;
35         IDDq mFreeSources;
36 
37         typedef std::vector<Sound*> SoundVec;
38         SoundVec mActiveSounds;
39         typedef std::vector<Stream*> StreamVec;
40         StreamVec mActiveStreams;
41 
42         osg::Vec3f mListenerPos;
43         Environment mListenerEnv;
44 
45         ALuint mWaterFilter;
46         ALuint mWaterEffect;
47         ALuint mDefaultEffect;
48         ALuint mEffectSlot;
49 
50         struct StreamThread;
51         std::unique_ptr<StreamThread> mStreamThread;
52 
53         void initCommon2D(ALuint source, const osg::Vec3f &pos, ALfloat gain, ALfloat pitch, bool loop, bool useenv);
54         void initCommon3D(ALuint source, const osg::Vec3f &pos, ALfloat mindist, ALfloat maxdist, ALfloat gain, ALfloat pitch, bool loop, bool useenv);
55 
56         void updateCommon(ALuint source, const osg::Vec3f &pos, ALfloat maxdist, ALfloat gain, ALfloat pitch, bool useenv, bool is3d);
57 
58         OpenAL_Output& operator=(const OpenAL_Output &rhs);
59         OpenAL_Output(const OpenAL_Output &rhs);
60 
61     public:
62         std::vector<std::string> enumerate() override;
63         bool init(const std::string &devname, const std::string &hrtfname, HrtfMode hrtfmode) override;
64         void deinit() override;
65 
66         std::vector<std::string> enumerateHrtf() override;
67         void setHrtf(const std::string &hrtfname, HrtfMode hrtfmode) override;
68 
69         std::pair<Sound_Handle,size_t> loadSound(const std::string &fname) override;
70         size_t unloadSound(Sound_Handle data) override;
71 
72         bool playSound(Sound *sound, Sound_Handle data, float offset) override;
73         bool playSound3D(Sound *sound, Sound_Handle data, float offset) override;
74         void finishSound(Sound *sound) override;
75         bool isSoundPlaying(Sound *sound) override;
76         void updateSound(Sound *sound) override;
77 
78         bool streamSound(DecoderPtr decoder, Stream *sound, bool getLoudnessData=false) override;
79         bool streamSound3D(DecoderPtr decoder, Stream *sound, bool getLoudnessData) override;
80         void finishStream(Stream *sound) override;
81         double getStreamDelay(Stream *sound) override;
82         double getStreamOffset(Stream *sound) override;
83         float getStreamLoudness(Stream *sound) override;
84         bool isStreamPlaying(Stream *sound) override;
85         void updateStream(Stream *sound) override;
86 
87         void startUpdate() override;
88         void finishUpdate() override;
89 
90         void updateListener(const osg::Vec3f &pos, const osg::Vec3f &atdir, const osg::Vec3f &updir, Environment env) override;
91 
92         void pauseSounds(int types) override;
93         void resumeSounds(int types) override;
94 
95         void pauseActiveDevice() override;
96         void resumeActiveDevice() override;
97 
98         OpenAL_Output(SoundManager &mgr);
99         virtual ~OpenAL_Output();
100     };
101 }
102 
103 #endif
104