Colobot
alsound.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2016, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
25 #pragma once
26 
27 #include "sound/sound.h"
28 
29 #include "sound/oalsound/buffer.h"
30 #include "sound/oalsound/channel.h"
31 #include "sound/oalsound/check.h"
32 
33 #include <map>
34 #include <memory>
35 #include <string>
36 #include <list>
37 
38 #include <al.h>
39 
40 
41 struct OldMusic
42 {
43  OldMusic() = default;
44 
45  OldMusic(const OldMusic&) = delete;
46  OldMusic& operator=(const OldMusic&) = delete;
47 
48  // Workaround for MSVC2013
49  OldMusic(OldMusic&& other)
50  : music(std::move(other.music)),
51  fadeTime(std::move(other.fadeTime)),
52  currentTime(std::move(other.currentTime))
53  {}
54 
55  OldMusic& operator=(OldMusic&& other)
56  {
57  music = std::move(other.music);
58  fadeTime = std::move(other.fadeTime);
59  currentTime = std::move(other.currentTime);
60  return *this;
61  }
62 
63  std::unique_ptr<CChannel> music;
64  float fadeTime = 0.0f;
65  float currentTime = 0.0f;
66 
67  inline friend bool operator<(const OldMusic & l, const OldMusic & r)
68  {
69  return l.currentTime < r.currentTime;
70  }
71 
72  inline friend bool operator==(const OldMusic & l, const OldMusic & r)
73  {
74  return l.currentTime == r.currentTime;
75  }
76 };
77 
78 class CALSound : public CSoundInterface
79 {
80 public:
81  CALSound();
82  ~CALSound();
83 
84  bool Create() override;
85  bool Cache(SoundType, const std::string &) override;
86  bool CacheMusic(const std::string &) override;
87  bool IsCached(SoundType) override;
88  bool IsCachedMusic(const std::string &) override;
89 
90  bool GetEnable() override;
91  void SetAudioVolume(int volume) override;
92  int GetAudioVolume() override;
93  void SetMusicVolume(int volume) override;
94  int GetMusicVolume() override;
95 
96  void SetListener(const Math::Vector &eye, const Math::Vector &lookat) override;
97  void FrameMove(float rTime) override;
98 
99  int Play(SoundType sound, float amplitude=1.0f, float frequency=1.0f, bool loop = false) override;
100  int Play(SoundType sound, const Math::Vector &pos, float amplitude=1.0f, float frequency=1.0f, bool loop = false) override;
101  bool FlushEnvelope(int channel) override;
102  bool AddEnvelope(int channel, float amplitude, float frequency, float time, SoundNext oper) override;
103  bool Position(int channel, const Math::Vector &pos) override;
104  bool Frequency(int channel, float frequency) override;
105  bool Stop(int channel) override;
106  bool StopAll() override;
107  bool MuteAll(bool mute) override;
108 
109  bool PlayMusic(const std::string &filename, bool repeat, float fadeTime=2.0f) override;
110  bool RestartMusic() override;
111  void SuspendMusic() override;
112  void StopMusic(float fadeTime=2.0f) override;
113  bool IsPlayingMusic() override;
114  bool PlayPauseMusic(const std::string &filename, bool repeat) override;
115  void StopPauseMusic() override;
116 
117 private:
118  void CleanUp();
119  int GetPriority(SoundType);
120  bool SearchFreeBuffer(SoundType sound, int &channel, bool &alreadyLoaded);
121  bool CheckChannel(int &channel);
122 
123  bool m_enabled;
124  float m_audioVolume;
125  float m_musicVolume;
126  unsigned int m_channelsLimit;
127  ALCdevice* m_device;
128  ALCcontext* m_context;
129  std::map<SoundType, std::unique_ptr<CBuffer>> m_sounds;
130  std::map<std::string, std::unique_ptr<CBuffer>> m_music;
131  std::map<int, std::unique_ptr<CChannel>> m_channels;
132  std::unique_ptr<CChannel> m_currentMusic;
133  std::list<OldMusic> m_oldMusic;
134  OldMusic m_previousMusic;
135  Math::Vector m_eye;
136  Math::Vector m_lookat;
137 };
Sound plugin interface.
OpenAL channel.
SoundNext
Enum representing operation that will be performend on a sound at given time.
Definition: sound.h:46
OpenAL buffer.
SoundType
Enum representing sound file.
Definition: sound_type.h:34
Definition: alsound.h:78
Definition: alsound.h:41
3D (3x1) vector
Definition: vector.h:53
Sound plugin interface.
Definition: sound.h:60