]> git.sesse.net Git - casparcg/blob - SFML-1.6/include/SFML/Audio/SoundBuffer.hpp
(no commit message)
[casparcg] / SFML-1.6 / include / SFML / Audio / SoundBuffer.hpp
1 ////////////////////////////////////////////////////////////\r
2 //\r
3 // SFML - Simple and Fast Multimedia Library\r
4 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)\r
5 //\r
6 // This software is provided 'as-is', without any express or implied warranty.\r
7 // In no event will the authors be held liable for any damages arising from the use of this software.\r
8 //\r
9 // Permission is granted to anyone to use this software for any purpose,\r
10 // including commercial applications, and to alter it and redistribute it freely,\r
11 // subject to the following restrictions:\r
12 //\r
13 // 1. The origin of this software must not be misrepresented;\r
14 //    you must not claim that you wrote the original software.\r
15 //    If you use this software in a product, an acknowledgment\r
16 //    in the product documentation would be appreciated but is not required.\r
17 //\r
18 // 2. Altered source versions must be plainly marked as such,\r
19 //    and must not be misrepresented as being the original software.\r
20 //\r
21 // 3. This notice may not be removed or altered from any source distribution.\r
22 //\r
23 ////////////////////////////////////////////////////////////\r
24 \r
25 #ifndef SFML_SOUNDBUFFER_HPP\r
26 #define SFML_SOUNDBUFFER_HPP\r
27 \r
28 ////////////////////////////////////////////////////////////\r
29 // Headers\r
30 ////////////////////////////////////////////////////////////\r
31 #include <SFML/System/Resource.hpp>\r
32 #include <SFML/Audio/AudioResource.hpp>\r
33 #include <string>\r
34 #include <vector>\r
35 #include <set>\r
36 \r
37 \r
38 namespace sf\r
39 {\r
40 class Sound;\r
41 \r
42 ////////////////////////////////////////////////////////////\r
43 /// SoundBuffer is the low-level for loading and manipulating\r
44 /// sound buffers\r
45 ////////////////////////////////////////////////////////////\r
46 class SFML_API SoundBuffer : public AudioResource, public Resource<SoundBuffer>\r
47 {\r
48 public :\r
49 \r
50     ////////////////////////////////////////////////////////////\r
51     /// Default constructor\r
52     ///\r
53     ////////////////////////////////////////////////////////////\r
54     SoundBuffer();\r
55 \r
56     ////////////////////////////////////////////////////////////\r
57     /// Copy constructor\r
58     ///\r
59     /// \param Copy : Instance to copy\r
60     ///\r
61     ////////////////////////////////////////////////////////////\r
62     SoundBuffer(const SoundBuffer& Copy);\r
63 \r
64     ////////////////////////////////////////////////////////////\r
65     /// Destructor\r
66     ///\r
67     ////////////////////////////////////////////////////////////\r
68     ~SoundBuffer();\r
69 \r
70     ////////////////////////////////////////////////////////////\r
71     /// Load the sound buffer from a file\r
72     ///\r
73     /// \param Filename : Path of the sound file to load\r
74     ///\r
75     /// \return True if loading has been successful\r
76     ///\r
77     ////////////////////////////////////////////////////////////\r
78     bool LoadFromFile(const std::string& Filename);\r
79 \r
80     ////////////////////////////////////////////////////////////\r
81     /// Load the sound buffer from a file in memory\r
82     ///\r
83     /// \param Data :        Pointer to the file data in memory\r
84     /// \param SizeInBytes : Size of the data to load, in bytes\r
85     ///\r
86     /// \return True if loading has been successful\r
87     ///\r
88     ////////////////////////////////////////////////////////////\r
89     bool LoadFromMemory(const char* Data, std::size_t SizeInBytes);\r
90 \r
91     ////////////////////////////////////////////////////////////\r
92     /// Load the sound buffer from an array of samples - assumed format for\r
93     /// samples is 16 bits signed integer\r
94     ///\r
95     /// \param Samples :       Pointer to the samples in memory\r
96     /// \param SamplesCount :  Number of samples pointed by Samples\r
97     /// \param ChannelsCount : Number of channels (1 = mono, 2 = stereo, ...)\r
98     /// \param SampleRate :    Frequency (number of samples to play per second)\r
99     ///\r
100     /// \return True if loading has been successful\r
101     ///\r
102     ////////////////////////////////////////////////////////////\r
103     bool LoadFromSamples(const Int16* Samples, std::size_t SamplesCount, unsigned int ChannelsCount, unsigned int SampleRate);\r
104 \r
105     ////////////////////////////////////////////////////////////\r
106     /// Save the sound buffer to a file\r
107     ///\r
108     /// \param Filename : Path of the sound file to write\r
109     ///\r
110     /// \return True if saving has been successful\r
111     ///\r
112     ////////////////////////////////////////////////////////////\r
113     bool SaveToFile(const std::string& Filename) const;\r
114 \r
115     ////////////////////////////////////////////////////////////\r
116     /// Return the sound samples\r
117     ///\r
118     /// \return Pointer to the array of sound samples, in 16 bits signed integer format\r
119     ///\r
120     ////////////////////////////////////////////////////////////\r
121     const Int16* GetSamples() const;\r
122 \r
123     ////////////////////////////////////////////////////////////\r
124     /// Return the samples count\r
125     ///\r
126     /// \return Number of samples\r
127     ///\r
128     ////////////////////////////////////////////////////////////\r
129     std::size_t GetSamplesCount() const;\r
130 \r
131     ////////////////////////////////////////////////////////////\r
132     /// Get the sample rate\r
133     ///\r
134     /// \return Sound frequency (number of samples per second)\r
135     ///\r
136     ////////////////////////////////////////////////////////////\r
137     unsigned int GetSampleRate() const;\r
138 \r
139     ////////////////////////////////////////////////////////////\r
140     /// Return the number of channels (1 = mono, 2 = stereo, ...)\r
141     ///\r
142     /// \return Number of channels\r
143     ///\r
144     ////////////////////////////////////////////////////////////\r
145     unsigned int GetChannelsCount() const;\r
146 \r
147     ////////////////////////////////////////////////////////////\r
148     /// Get the sound duration\r
149     ///\r
150     /// \return Sound duration, in seconds\r
151     ///\r
152     ////////////////////////////////////////////////////////////\r
153     float GetDuration() const;\r
154 \r
155     ////////////////////////////////////////////////////////////\r
156     /// Assignment operator\r
157     ///\r
158     /// \param Other : Instance to assign\r
159     ///\r
160     /// \return Reference to the sound buffer\r
161     ///\r
162     ////////////////////////////////////////////////////////////\r
163     SoundBuffer& operator =(const SoundBuffer& Other);\r
164 \r
165 private :\r
166 \r
167     friend class Sound;\r
168 \r
169     ////////////////////////////////////////////////////////////\r
170     /// Update the internal buffer with the audio samples\r
171     ///\r
172     /// \param ChannelsCount : Number of channels\r
173     /// \param SampleRate :    Sample rate\r
174     ///\r
175     /// \return True on success\r
176     ///\r
177     ////////////////////////////////////////////////////////////\r
178     bool Update(unsigned int ChannelsCount, unsigned int SampleRate);\r
179 \r
180     ////////////////////////////////////////////////////////////\r
181     /// Add a sound to the list of sounds that use this buffer\r
182     ///\r
183     /// \param Instance : Sound object to attach\r
184     ///\r
185     ////////////////////////////////////////////////////////////\r
186     void AttachSound(Sound* Instance) const;\r
187 \r
188     ////////////////////////////////////////////////////////////\r
189     /// Remove a sound from the list of sounds that use this buffer\r
190     ///\r
191     /// \param Instance : Sound object to detach\r
192     ///\r
193     ////////////////////////////////////////////////////////////\r
194     void DetachSound(Sound* Instance) const;\r
195 \r
196     ////////////////////////////////////////////////////////////\r
197     // Types\r
198     ////////////////////////////////////////////////////////////\r
199     typedef std::set<Sound*> SoundList; ///< Set of unique sound instances\r
200 \r
201     ////////////////////////////////////////////////////////////\r
202     // Member data\r
203     ////////////////////////////////////////////////////////////\r
204     unsigned int       myBuffer;   ///< OpenAL buffer identifier\r
205     std::vector<Int16> mySamples;  ///< Samples buffer\r
206     float              myDuration; ///< Sound duration, in seconds\r
207     mutable SoundList  mySounds;   ///< List of sounds that are using this buffer\r
208 };\r
209 \r
210 } // namespace sf\r
211 \r
212 \r
213 #endif // SFML_SOUNDBUFFER_HPP\r