Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
SoundBuffer.hpp
1 // Copyright (C) 2017 Jérôme Leclercq
2 // This file is part of the "Nazara Engine - Audio module"
3 // For conditions of distribution and use, see copyright notice in Config.hpp
4 
5 #pragma once
6 
7 #ifndef NAZARA_SOUNDBUFFER_HPP
8 #define NAZARA_SOUNDBUFFER_HPP
9 
10 #include <Nazara/Prerequesites.hpp>
11 #include <Nazara/Audio/Config.hpp>
12 #include <Nazara/Audio/Enums.hpp>
13 #include <Nazara/Core/MovablePtr.hpp>
14 #include <Nazara/Core/ObjectRef.hpp>
15 #include <Nazara/Core/ObjectLibrary.hpp>
16 #include <Nazara/Core/RefCounted.hpp>
17 #include <Nazara/Core/Resource.hpp>
18 #include <Nazara/Core/ResourceLoader.hpp>
19 #include <Nazara/Core/ResourceManager.hpp>
20 #include <Nazara/Core/ResourceParameters.hpp>
21 #include <Nazara/Core/Signal.hpp>
22 
23 namespace Nz
24 {
25  struct SoundBufferParams : ResourceParameters
26  {
27  bool forceMono = false;
28 
29  bool IsValid() const;
30  };
31 
32  class Sound;
33  class SoundBuffer;
34 
35  using SoundBufferConstRef = ObjectRef<const SoundBuffer>;
36  using SoundBufferLibrary = ObjectLibrary<SoundBuffer>;
37  using SoundBufferLoader = ResourceLoader<SoundBuffer, SoundBufferParams>;
38  using SoundBufferManager = ResourceManager<SoundBuffer, SoundBufferParams>;
39  using SoundBufferRef = ObjectRef<SoundBuffer>;
40 
41  struct SoundBufferImpl;
42 
43  class NAZARA_AUDIO_API SoundBuffer : public RefCounted, public Resource
44  {
45  friend Sound;
46  friend SoundBufferLibrary;
47  friend SoundBufferLoader;
48  friend SoundBufferManager;
49  friend class Audio;
50 
51  public:
52  SoundBuffer() = default;
53  SoundBuffer(AudioFormat format, UInt64 sampleCount, UInt32 sampleRate, const Int16* samples);
54  SoundBuffer(const SoundBuffer&) = delete;
55  SoundBuffer(SoundBuffer&&) = delete;
56  ~SoundBuffer();
57 
58  bool Create(AudioFormat format, UInt64 sampleCount, UInt32 sampleRate, const Int16* samples);
59  void Destroy();
60 
61  UInt32 GetDuration() const;
62  AudioFormat GetFormat() const;
63  const Int16* GetSamples() const;
64  UInt64 GetSampleCount() const;
65  UInt32 GetSampleRate() const;
66 
67  bool IsValid() const;
68 
69  bool LoadFromFile(const String& filePath, const SoundBufferParams& params = SoundBufferParams());
70  bool LoadFromMemory(const void* data, std::size_t size, const SoundBufferParams& params = SoundBufferParams());
71  bool LoadFromStream(Stream& stream, const SoundBufferParams& params = SoundBufferParams());
72 
73  static bool IsFormatSupported(AudioFormat format);
74  template<typename... Args> static SoundBufferRef New(Args&&... args);
75 
76  SoundBuffer& operator=(const SoundBuffer&) = delete;
77  SoundBuffer& operator=(SoundBuffer&&) = delete;
78 
79  // Signals:
80  NazaraSignal(OnSoundBufferDestroy, const SoundBuffer* /*soundBuffer*/);
81  NazaraSignal(OnSoundBufferRelease, const SoundBuffer* /*soundBuffer*/);
82 
83  private:
84  unsigned int GetOpenALBuffer() const;
85 
86  static bool Initialize();
87  static void Uninitialize();
88 
89  MovablePtr<SoundBufferImpl> m_impl = nullptr;
90 
91  static SoundBufferLibrary::LibraryMap s_library;
92  static SoundBufferLoader::LoaderList s_loaders;
93  static SoundBufferManager::ManagerMap s_managerMap;
94  static SoundBufferManager::ManagerParams s_managerParameters;
95  };
96 }
97 
98 #include <Nazara/Audio/SoundBuffer.inl>
99 
100 #endif // NAZARA_SOUNDBUFFER_HPP
TODO: Inherit SoundEmitter from Node.
Definition: Algorithm.hpp:12
AudioFormat
WARNING: The integer value is the number of canals owned by the format.
Definition: Enums.hpp:12
Core class that represents a string.
Definition: String.hpp:22
Audio class that represents the module initializer of Audio.
Definition: Audio.hpp:18
Core class that represents a loader of resources.
Definition: ResourceLoader.hpp:23
Core class that represents a resource.
Definition: Resource.hpp:15
Core class that represents a reference with a counter.
Definition: RefCounted.hpp:21
Core class that represents a reference to an object.
Definition: ObjectRef.hpp:18
Audio class that represents a sound.
Definition: Sound.hpp:17
Core class that represents a resource manager.
Definition: ResourceManager.hpp:18
Audio class that represents a buffer for sound.
Definition: SoundBuffer.hpp:43
Core class that represents a stream.
Definition: Stream.hpp:19