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 "common/thread/worker_thread.h"
30 
31 #include "sound/oalsound/buffer.h"
32 #include "sound/oalsound/channel.h"
33 #include "sound/oalsound/check.h"
34 
35 #include <map>
36 #include <memory>
37 #include <string>
38 #include <list>
39 
40 #include <al.h>
41 
42 
43 struct OldMusic
44 {
45  OldMusic() = default;
46 
47  OldMusic(const OldMusic&) = delete;
48  OldMusic& operator=(const OldMusic&) = delete;
49 
50  // Workaround for MSVC2013
51  OldMusic(OldMusic&& other)
52  : music(std::move(other.music)),
53  fadeTime(std::move(other.fadeTime)),
54  currentTime(std::move(other.currentTime))
55  {}
56 
57  OldMusic& operator=(OldMusic&& other)
58  {
59  music = std::move(other.music);
60  fadeTime = std::move(other.fadeTime);
61  currentTime = std::move(other.currentTime);
62  return *this;
63  }
64 
65  std::unique_ptr<CChannel> music;
66  float fadeTime = 0.0f;
67  float currentTime = 0.0f;
68 
69  inline friend bool operator<(const OldMusic & l, const OldMusic & r)
70  {
71  return l.currentTime < r.currentTime;
72  }
73 
74  inline friend bool operator==(const OldMusic & l, const OldMusic & r)
75  {
76  return l.currentTime == r.currentTime;
77  }
78 };
79 
80 class CALSound : public CSoundInterface
81 {
82 public:
83  CALSound();
84  ~CALSound();
85 
86  bool Create() override;
87  bool Cache(SoundType, const std::string &) override;
88  void CacheMusic(const std::string &) override;
89  bool IsCached(SoundType) override;
90  bool IsCachedMusic(const std::string &) override;
91 
92  bool GetEnable() override;
93  void SetAudioVolume(int volume) override;
94  int GetAudioVolume() override;
95  void SetMusicVolume(int volume) override;
96  int GetMusicVolume() override;
97 
98  void SetListener(const Math::Vector &eye, const Math::Vector &lookat) override;
99  void FrameMove(float rTime) override;
100 
101  int Play(SoundType sound, float amplitude=1.0f, float frequency=1.0f, bool loop = false) override;
102  int Play(SoundType sound, const Math::Vector &pos, float amplitude=1.0f, float frequency=1.0f, bool loop = false) override;
103  bool FlushEnvelope(int channel) override;
104  bool AddEnvelope(int channel, float amplitude, float frequency, float time, SoundNext oper) override;
105  bool Position(int channel, const Math::Vector &pos) override;
106  bool Frequency(int channel, float frequency) override;
107  bool Stop(int channel) override;
108  bool StopAll() override;
109  bool MuteAll(bool mute) override;
110 
111  void PlayMusic(const std::string &filename, bool repeat, float fadeTime = 2.0f) override;
112  void StopMusic(float fadeTime=2.0f) override;
113  bool IsPlayingMusic() override;
114  void 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  CWorkerThread m_thread;
138 };
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:80
Definition: alsound.h:43
3D (3x1) vector
Definition: vector.h:53
Sound plugin interface.
Definition: sound.h:60
Thread that runs functions, one at a time.
Definition: worker_thread.h:36