Cafu Engine
Buffer.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_SOUNDSYS_BUFFER_HPP_INCLUDED
8 #define CAFU_SOUNDSYS_BUFFER_HPP_INCLUDED
9 
10 #include "OpenALIncl.hpp"
11 #include "Templates/Array.hpp"
12 
13 #include <string>
14 
15 
16 class MixerTrackT;
17 
18 
19 /// A BufferT encapsulates an audio resource such as a file or a capture device.
20 /// It is responsible for managing the OpenAL buffer(s) in an OpenAL source,
21 /// such as creating and filling the buffer(s), assigning or queuing them to the source, etc.
22 class BufferT
23 {
24  public:
25 
26  /// The constructor.
27  /// @param ResName The name of the resource (file or capture device) that this buffer is created from.
28  /// @param ForceMono Whether the data from the resource should be reduced to a single channel before use (mono output).
29  BufferT(const std::string& ResName, bool ForceMono);
30 
31  /// The virtual destructor, so that derived classes can safely be deleted via a BufferT (base class) pointer.
32  virtual ~BufferT() { }
33 
34  /// Returns the name of the resource (file or capture device) that this buffer was created from.
35  const std::string& GetName() const { return m_ResName; }
36 
37  /// Returns whether the data from the resource is reduced to a single channel before use (mono output).
38  bool ForcesMono() const { return m_ForceMono; }
39 
40  /// Returns the number of audio channels in this buffer (1 is mono, 2 is stereo).
41  virtual unsigned int GetChannels() const=0;
42 
43  /// Returns whether this buffer can be attached to multiple mixer tracks (resource sharing).
44  virtual bool CanShare() const=0;
45 
46  /// Updates the buffer.
47  virtual void Update()=0;
48 
49  /// Attaches the buffer to a mixer track, so the mixer track can play this buffer.
50  /// Note that depending on the underlying buffer it is possible to attach one buffer to multiple mixer tracks.
51  /// As for streaming buffers this is not possible since they are unique and can only be attached to one mixer
52  /// track.
53  /// @param MixerTrack The mixer track this buffer should be attached to.
54  /// @return Whether the buffer could be attached to the mixer track.
55  virtual bool AttachToMixerTrack(MixerTrackT* MixerTrack)=0;
56 
57  /// Detaches the buffer from a mixer track.
58  /// This method informs the mixer track as well as the buffer of the change.
59  /// @param MixerTrack The mixer track this buffer should be attached from.
60  /// @return Whether the buffer could be attached to the mixer track. False means usually that this buffer wasn't
61  /// attached to the passed mixer track at all.
62  virtual bool DetachFromMixerTrack(MixerTrackT* MixerTrack)=0;
63 
64 
65  unsigned int References; ///< Number of references to this buffer (e.g. how many sound objects use this buffer).
66 
67 
68  protected:
69 
70  ArrayT<MixerTrackT*> m_MixerTracks; ///< Mixer tracks this buffer is currently attached to.
71 
72  /// Converts signed 16 bit raw PCM data from stereo to mono.
73  /// @param Buffer The PCM data to convert to mono.
74  /// @param Size Size of the data in bytes.
75  /// @return The resulting buffer size.
76  unsigned int ConvertToMono(unsigned char* Buffer, unsigned int Size);
77 
78 
79  private:
80 
81  BufferT(const BufferT&); ///< Use of the Copy Constructor is not allowed.
82  void operator = (const BufferT&); ///< Use of the Assignment Operator is not allowed.
83 
84  const std::string m_ResName; ///< Name of the resource (file or capture device) that this buffer was created from.
85  const bool m_ForceMono; ///< Whether the data from the resource is reduced to a single channel before use (mono output).
86 };
87 
88 #endif
virtual ~BufferT()
The virtual destructor, so that derived classes can safely be deleted via a BufferT (base class) poin...
Definition: Buffer.hpp:32
A mixer track represents/encapsulates/abstracs an OpenAL sound source.
Definition: MixerTrack.hpp:22
ArrayT< MixerTrackT * > m_MixerTracks
Mixer tracks this buffer is currently attached to.
Definition: Buffer.hpp:70
virtual void Update()=0
Updates the buffer.
virtual bool CanShare() const =0
Returns whether this buffer can be attached to multiple mixer tracks (resource sharing).
unsigned int ConvertToMono(unsigned char *Buffer, unsigned int Size)
Converts signed 16 bit raw PCM data from stereo to mono.
Definition: Buffer.cpp:18
A BufferT encapsulates an audio resource such as a file or a capture device.
Definition: Buffer.hpp:22
BufferT(const std::string &ResName, bool ForceMono)
The constructor.
Definition: Buffer.cpp:10
const std::string & GetName() const
Returns the name of the resource (file or capture device) that this buffer was created from...
Definition: Buffer.hpp:35
virtual bool DetachFromMixerTrack(MixerTrackT *MixerTrack)=0
Detaches the buffer from a mixer track.
unsigned int References
Number of references to this buffer (e.g. how many sound objects use this buffer).
Definition: Buffer.hpp:65
bool ForcesMono() const
Returns whether the data from the resource is reduced to a single channel before use (mono output)...
Definition: Buffer.hpp:38
virtual bool AttachToMixerTrack(MixerTrackT *MixerTrack)=0
Attaches the buffer to a mixer track, so the mixer track can play this buffer.
virtual unsigned int GetChannels() const =0
Returns the number of audio channels in this buffer (1 is mono, 2 is stereo).