~ubuntu-branches/debian/sid/ember/sid

« back to all changes in this revision

Viewing changes to src/services/sound/SoundSample.h

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2009-07-23 07:46:40 UTC
  • Revision ID: james.westby@ubuntu.com-20090723074640-wh0ukzis0kda36qv
Tags: upstream-0.5.6
ImportĀ upstreamĀ versionĀ 0.5.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2008 Romulo Fernandes Machado (nightz)
 
3
 
 
4
    This program is free software; you can redistribute it and/or modify
 
5
    it under the terms of the GNU General Public License as published by
 
6
    the Free Software Foundation; either version 2 of the License, or
 
7
    (at your option) any later version.
 
8
 
 
9
    This program is distributed in the hope that it will be useful,
 
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
    GNU General Public License for more details.
 
13
 
 
14
    You should have received a copy of the GNU General Public License
 
15
    along with this program; if not, write to the Free Software
 
16
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
17
*/
 
18
 
 
19
#ifndef SOUND_SAMPLE_H
 
20
#define SOUND_SAMPLE_H
 
21
 
 
22
#include "SoundGeneral.h"
 
23
#include "SoundBinding.h"
 
24
#include "framework/IResourceProvider.h"
 
25
#include <vector>
 
26
#ifdef __APPLE__
 
27
#include <OpenAL/al.h>
 
28
#else
 
29
#include <AL/al.h>
 
30
#endif
 
31
 
 
32
namespace Ember
 
33
{
 
34
class SoundSource;
 
35
class StaticSoundSample;
 
36
 
 
37
 
 
38
/**
 
39
@brief A binding to a "static" sound source, i.e. a sound source which doesn't have to be updated.
 
40
 
 
41
A "static" sound is one that is small enough to fit into one continous buffer, and thus doesn't need to be dynamically updated as is the case with "streaming" sounds. As a result, this binding is very simple and will just bind the sound data to the source in the constructor, without having to provide any functionality in the update() method.
 
42
 
 
43
@author Erik Hjortsberg <erik.hjortsberg@gmail.com>
 
44
*/
 
45
class StaticSoundBinding : public SoundBinding
 
46
{
 
47
public:
 
48
 
 
49
/**
 
50
 * @brief Ctor. All bindings between the buffer and the sound source will occur here.
 
51
 * @param source The sound source.
 
52
 * @param sample The static sound sample to bind to the source.
 
53
 */
 
54
StaticSoundBinding(SoundSource& source, StaticSoundSample& sample);
 
55
 
 
56
/**
 
57
 * @copydoc SoundBinding::update()
 
58
 */
 
59
virtual void update()
 
60
{
 
61
        ///Since it's a static sound we don't need to update anything.
 
62
}
 
63
 
 
64
protected:
 
65
 
 
66
/**
 
67
 * @brief The static sound samle used for binding.
 
68
 * There's really no need to keep this around here, since the binding will occur in the constructor, but hey, someday we might provide some kind of dynamic unloading/reloading...
 
69
 */
 
70
StaticSoundSample& mSample;
 
71
};
 
72
 
 
73
/**
 
74
 * Sound Sample 
 
75
 *
 
76
 * Defines general properties of sound data
 
77
 */
 
78
class BaseSoundSample
 
79
{
 
80
public:
 
81
        
 
82
        typedef std::vector<ALuint> BufferStore;
 
83
 
 
84
        virtual ~BaseSoundSample() {}
 
85
        
 
86
        /**
 
87
        * Returns entity type
 
88
        */
 
89
        SoundGeneral::SoundSampleType getType();
 
90
 
 
91
        virtual unsigned int getNumberOfBuffers() = 0;
 
92
        
 
93
        /**
 
94
        * @brief Returns a store of the sound data buffers stored by this sample.
 
95
        * The buffers will be returned as ALuint which is the internal buffer reference within OpenAL. Any further operation on the buffer must therefore go through OpenAL (i.e. the values returned are _not_ memory pointers).
 
96
        * @return A store of OpenAL buffer identifiers.
 
97
        */
 
98
        virtual BufferStore getBuffers() = 0;
 
99
        
 
100
        /**
 
101
        * @brief Creates a new binding to this buffer, to be used together with an instance of SoundInstance.
 
102
        * If you want the sound held by this buffer to be played, one way would be to call this to create a binding which you then feed to an instance of SoundInstance.
 
103
        * Note that ownership of the created binding is transferred to the caller, and thus it's the caller's responsibility to make sure it's properly deleted. Under normal circumstances that will be taken care of by SoundInstance however.
 
104
        * @see SoundInstance::bind()
 
105
        * @param source The sound source to which we should bind this sound sample.
 
106
        * @return A new sound binding instance.
 
107
        */
 
108
        virtual SoundBinding* createBinding(SoundSource& source) = 0;
 
109
protected:
 
110
 
 
111
        /**
 
112
        * @brief Ctor. This is protected to disallow direct creation of this class except by subclasses.
 
113
        */
 
114
        BaseSoundSample() {}
 
115
        /**
 
116
        * Type of the sample
 
117
        */
 
118
        SoundGeneral::SoundSampleType   mType;
 
119
};
 
120
 
 
121
/**
 
122
 * The class StaticSoundSample is responsible
 
123
 * to keep track of samples that doesnt need
 
124
 * often updates and only have one buffer
 
125
 */
 
126
class StaticSoundSample : public BaseSoundSample
 
127
{
 
128
public:
 
129
        StaticSoundSample(const ResourceWrapper& resource, bool playsLocal, float volume);
 
130
        ~StaticSoundSample();
 
131
 
 
132
        /**
 
133
        * Returns the unique buffer this sample has.
 
134
        */
 
135
        ALuint  getBuffer();
 
136
 
 
137
        /**
 
138
        * Within this class, this is always 1.
 
139
        */
 
140
        unsigned int getNumberOfBuffers();
 
141
 
 
142
        /**
 
143
        * @copydoc BaseSoundSample::createBinding()
 
144
        */
 
145
        virtual SoundBinding* createBinding(SoundSource& source);
 
146
 
 
147
        /**
 
148
        * @copydoc BaseSoundSample::getBuffers()
 
149
        */
 
150
        virtual BaseSoundSample::BufferStore getBuffers();
 
151
private:
 
152
        /**
 
153
        * Sample buffer
 
154
        */
 
155
        ALuint mBuffer;
 
156
        
 
157
        /**
 
158
        * @brief The resource wrapper instance which holds the actual data.
 
159
        */
 
160
        ResourceWrapper mResource;
 
161
 
 
162
};
 
163
 
 
164
/**
 
165
 * The class StreamedSoundSample is responsible
 
166
 * to keep track of samples that often need
 
167
 * updates and requires more than a buffer to stream
 
168
 * data.
 
169
 */
 
170
// class StreamedSoundSample : public BaseSoundSample
 
171
// {
 
172
//      private:
 
173
//              /**
 
174
//               * Filename with full path to the data.
 
175
//               */
 
176
//              std::string             mFilename;
 
177
// 
 
178
//              /**
 
179
//               * A pointer to the file specified in mFilename
 
180
//               */
 
181
//              FILE*                           mFile;
 
182
// 
 
183
//              /**
 
184
//               * VORBIS Internal Stream 
 
185
//               */
 
186
//              OggVorbis_File mStream;
 
187
// 
 
188
//              /**
 
189
//               * Front and back buffers for openAl
 
190
//               */
 
191
//              ALuint                  mBuffers[2];
 
192
// 
 
193
//              /**
 
194
//               * Format of the stream (checked from ogg/vorbis)
 
195
//               */
 
196
//              ALenum                  mFormat;
 
197
// 
 
198
//              /**
 
199
//               * Rate of the stream (checked from ogg/vorbis)
 
200
//               */
 
201
//              ALuint                  mRate;
 
202
// 
 
203
//              /**
 
204
//               * If this stream is playing
 
205
//               */
 
206
//              bool                            mPlaying;
 
207
// 
 
208
//              /**
 
209
//               * This function is responsible to fill
 
210
//               * buffers from stream data
 
211
//               *
 
212
//               * @param buffer The destination openAl buffer
 
213
//               * @return Status of the streaming
 
214
//               */
 
215
//              bool stream(ALuint buffer);
 
216
// 
 
217
//      public:
 
218
//              StreamedSoundSample(const std::string& filename, bool playsLocal, float volume);
 
219
//              ~StreamedSoundSample();
 
220
// 
 
221
//              /**
 
222
//               * Set the file to be used in stream proccess.
 
223
//               *
 
224
//               * @param ptr A pointer to the file.
 
225
//               * @param filename The file name with full path.
 
226
//               */
 
227
//              void setFile(FILE* ptr, const std::string& filename);
 
228
// 
 
229
//              /**
 
230
//               * Set stream format
 
231
//               */
 
232
//              void setFormat(ALenum fmt);
 
233
// 
 
234
//              /**
 
235
//               * Set stream rate
 
236
//               */
 
237
//              void setRate(ALuint rate);
 
238
// 
 
239
//              /**
 
240
//               * Set the stream status (if playing or not)
 
241
//               */
 
242
//              void setPlaying(bool play);
 
243
// 
 
244
//              /**
 
245
//               * Returns a pointer to the buffers array
 
246
//               */
 
247
//              ALuint*                         getBufferPtr();
 
248
// 
 
249
//              /**
 
250
//               * Returns a pointer to the stream information (vorbis internals).
 
251
//               */
 
252
//              OggVorbis_File* getStreamPtr();
 
253
// 
 
254
//              /**
 
255
//               * Return the state of the stream.
 
256
//               */
 
257
//              bool                                    isPlaying();
 
258
// 
 
259
//              /**
 
260
//               * Return the number of buffers in this stream. In this case this is 2.
 
261
//               */
 
262
//              unsigned int            getNumberOfBuffers();
 
263
// 
 
264
//              /**
 
265
//               * Return the full filename of the stream file.
 
266
//               */
 
267
//              const std::string& getFilename();
 
268
// 
 
269
//              // Common methods
 
270
//              void play();    
 
271
//              void stop();
 
272
//              void cycle();                           
 
273
// };
 
274
 
 
275
} // namespace Ember
 
276
 
 
277
#endif
 
278