OpenAL.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef _GAZEBO_UTIL_OPENAL_HH_
18 #define _GAZEBO_UTIL_OPENAL_HH_
19 
20 #include <set>
21 #include <string>
22 #include <vector>
23 #include <sdf/sdf.hh>
24 
25 #include <ignition/math/Vector3.hh>
26 #include <ignition/math/Pose3.hh>
27 
29 #include "gazebo/util/UtilTypes.hh"
30 
31 #include "gazebo/gazebo_config.h"
32 #include "gazebo/util/system.hh"
33 
34 #ifdef HAVE_OPENAL
35 
36 namespace gazebo
37 {
38  namespace util
39  {
40  // Forward declare private openal data class
41  class OpenALPrivate;
42 
43  // Forward declare private openal sourcedata class
44  class OpenALSourcePrivate;
45 
48 
51  class GZ_UTIL_VISIBLE OpenAL : public SingletonT<OpenAL>
52  {
54  private: OpenAL();
55 
57  private: virtual ~OpenAL();
58 
61  public: bool Load(sdf::ElementPtr _sdf = sdf::ElementPtr());
62 
64  public: void Fini();
65 
69  public: OpenALSourcePtr CreateSource(sdf::ElementPtr _sdf);
70 
75  public: OpenALSinkPtr CreateSink(sdf::ElementPtr _sdf);
76 
79  public: std::set<std::string> DeviceList() const;
80 
83  private: std::unique_ptr<OpenALPrivate> dataPtr;
84 
86  private: friend class SingletonT<OpenAL>;
87  };
88 
91  class GZ_UTIL_VISIBLE OpenALSink
92  {
94  public: OpenALSink();
95 
97  public: virtual ~OpenALSink();
98 
102  public: bool SetPose(const ignition::math::Pose3d &_pose);
103 
107  public: bool SetVelocity(const ignition::math::Vector3d &_vel);
108  };
109 
112  class GZ_UTIL_VISIBLE OpenALSource
113  {
115  public: OpenALSource();
116 
118  public: virtual ~OpenALSource();
119 
123  public: bool Load(sdf::ElementPtr _sdf);
124 
128  public: bool SetPose(const ignition::math::Pose3d &_pose);
129 
133  public: bool SetVelocity(const ignition::math::Vector3d &_vel);
134 
138  public: bool SetPitch(float _p);
139 
143  public: bool SetGain(float _g);
144 
148  public: bool SetLoop(bool _state);
149 
155  public: bool OnContact() const;
156 
160  public: std::vector<std::string> CollisionNames() const;
161 
165  public: bool HasCollisionName(const std::string &_name) const;
166 
168  public: void Play();
169 
171  public: void Pause();
172 
174  public: void Stop();
175 
177  public: void Rewind();
178 
180  public: bool IsPlaying();
181 
187  public: bool FillBufferFromPCM(uint8_t *_pcmData, unsigned int _dataCount,
188  int _sampleRate);
189 
192  public: void FillBufferFromFile(const std::string &_audioFile);
193 
196  private: std::unique_ptr<OpenALSourcePrivate> dataPtr;
197  };
199  }
200 }
201 #endif
202 #endif
OpenALSink()
Constructor.
bool SetLoop(bool _state)
Set whether the source loops the audio.
bool IsPlaying()
Is the audio playing.
Forward declarations for the common classes.
Definition: Animation.hh:26
virtual ~OpenALSource()
Destructor.
3D audio setup and playback.
Definition: OpenAL.hh:51
bool HasCollisionName(const std::string &_name) const
Get whether the source has a collision name set.
bool SetVelocity(const ignition::math::Vector3d &_vel)
Set the velocity of the source.
std::set< std::string > DeviceList() const
Get a list of available audio devices.
void Fini()
Finalize.
Singleton template class.
Definition: SingletonT.hh:33
std::shared_ptr< OpenALSource > OpenALSourcePtr
Definition: UtilTypes.hh:48
bool SetPose(const ignition::math::Pose3d &_pose)
Set the position of the source.
bool OnContact() const
Return true if the audio source is played on contact with another object.
OpenAL Listener. This can be thought of as a microphone.
Definition: OpenAL.hh:91
void Pause()
Pause a sound.
bool SetVelocity(const ignition::math::Vector3d &_vel)
Set the velocity of the sink.
bool FillBufferFromPCM(uint8_t *_pcmData, unsigned int _dataCount, int _sampleRate)
Fill the OpenAL audio buffer from PCM data.
virtual ~OpenALSink()
Destructor.
bool SetGain(float _g)
Set the pitch of the source.
void FillBufferFromFile(const std::string &_audioFile)
Fill the OpenAL audio buffer with data from a sound file.
void Stop()
Stop a sound.
void Play()
Play a sound.
std::shared_ptr< OpenALSink > OpenALSinkPtr
Definition: UtilTypes.hh:44
bool SetPitch(float _p)
Set the pitch of the source.
bool Load(sdf::ElementPtr _sdf=sdf::ElementPtr())
Load the OpenAL server.
OpenALSource()
Constructor.
OpenALSourcePtr CreateSource(sdf::ElementPtr _sdf)
Create an OpenALSource object.
void Rewind()
Rewind the sound to the beginning.
std::vector< std::string > CollisionNames() const
Get a vector of all the collision names.
bool SetPose(const ignition::math::Pose3d &_pose)
Set the position of the sink.
OpenAL Source. This can be thought of as a speaker.
Definition: OpenAL.hh:112
bool Load(sdf::ElementPtr _sdf)
Load the source from sdf.
OpenALSinkPtr CreateSink(sdf::ElementPtr _sdf)
Create an audio listener.