Angel 3.2
A 2D Game Prototyping Engine
SoundDevice.h
1 
2 // Copyright (C) 2008-2013, Shane Liesegang
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of the copyright holder nor the names of any
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
29 
30 #pragma once
31 
32 #include "../AngelConfig.h"
33 #include "../Infrastructure/Callback.h"
34 #include "../Util/StringUtil.h"
35 
36 #if !ANGEL_DISABLE_FMOD
37  #include <map>
38  #include <assert.h>
39 
40  #include "fmod.hpp"
41  #include "fmod_errors.h"
42 #else
43  #if defined(WIN32) || defined(__linux__)
44  #include <AL/al.h>
45  #include <AL/alc.h>
46  #else
47  #include <OpenAL/al.h>
48  #include <OpenAL/alc.h>
49  #define OV_EXCLUDE_STATIC_CALLBACKS 1
50  #endif
51  #include <vorbis/vorbisfile.h>
52 #endif
53 
54 //typedefs so our declarations can look more sensible
55 #if !ANGEL_DISABLE_FMOD
56  typedef void* AngelSoundHandle;
57  typedef void* AngelSampleHandle;
58 #else
59  typedef ALuint AngelSoundHandle;
60  typedef ALuint AngelSampleHandle;
61 #endif
62 
63 //singleton shortcut
64 #define theSound SoundDevice::GetInstance()
65 
66 //forward declaration
67 class GameManager;
68 
70 
99 {
100 public:
107  static SoundDevice& GetInstance();
108 
121  AngelSampleHandle LoadSample(const String& filename, bool isStream);
122 
135  AngelSoundHandle PlaySound(AngelSampleHandle sample, float volume=1.0f, bool looping=false, int flags=0);
136 
142  void StopSound(AngelSoundHandle sound);
143 
152  void PauseSound(AngelSoundHandle sound, bool paused);
153 
160  bool IsPlaying(AngelSoundHandle sound);
161 
168  bool IsPaused(AngelSoundHandle sound);
169 
180  void SetPan(AngelSoundHandle sound, float newPan);
181 
189  void SetVolume(AngelSoundHandle sound, float newVolume);
190 
202  void SetSoundCallback(GameManager* instance, void (GameManager::*function)(AngelSoundHandle param))
203  {
204  soundCallback.SetCallback(instance, function);
205  }
206 
215  void Update();
216 
223  void Shutdown();
224 
225 private:
226  static SoundDevice* s_soundDevice;
227 
228  SoundDevice() : _system(NULL)
229  {
230  Initialize();
231  }
232 
233  void Initialize();
234 
236 
237  #if !ANGEL_DISABLE_FMOD
238  FMOD::System* _system;
239  std::vector<FMOD::Sound*> _samples;
240  static FMOD_RESULT F_CALLBACK FMOD_SoundCallback(FMOD_CHANNEL *channel, FMOD_CHANNEL_CALLBACKTYPE type, void *commanddata1, void *commanddata2);
241  #else
242  struct StreamingAudio
243  {
244  ALuint source;
245  ALuint buffers[2];
246  OggVorbis_File file;
247  vorbis_info* vorbisInfo;
248  bool looped;
249  ALenum format;
250  };
251  ALCcontext * _system;
252  std::map<ALuint, StreamingAudio> _streams;
253  std::vector<ALuint> _buffers;
254  std::vector<ALuint> _sources;
255 
256  bool _isSampleStreamed(AngelSampleHandle sample);
257  bool _streamAudio(ALuint buffer, StreamingAudio &sa);
258  #endif
259 
260 };