FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
soundemitter.cpp
1 /***************************************************************************
2  * Copyright (C) 2005-2008 by the FIFE team *
3  * http://www.fifengine.de *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library 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. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 
24 // Platform specific includes
25 
26 // 3rd party library includes
27 
28 // FIFE includes
29 // These includes are split up in two parts, separated by one empty line
30 // First block: files included from the FIFE root src directory
31 // Second block: files included from the same folder
32 #include "util/log/logger.h"
33 #include "util/time/timemanager.h"
34 #include "util/base/exception.h"
35 #include "soundemitter.h"
36 #include "soundmanager.h"
37 #include "soundclippool.h"
38 
39 namespace FIFE {
40  static Logger _log(LM_AUDIO);
41 
42  SoundEmitter::SoundEmitter(SoundManager* manager, SoundClipPool* pool, unsigned int uid) : m_manager(manager), m_pool(pool), m_source(0), m_soundclip(NULL), m_soundclipid(0), m_streamid(0),
43  m_emitterid(uid), m_loop(false) {
44  if (!m_manager->isActive()) {
45  return;
46  }
47 
48  TimeManager::instance()->registerEvent(this);
49  setPeriod(-1);
50  alGenSources(1, &m_source);
51  CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error creating source")
52  }
53 
54  SoundEmitter::~SoundEmitter() {
55  if (!m_manager->isActive()) {
56  return;
57  }
58 
59  setPeriod(-1);
60  TimeManager::instance()->unregisterEvent(this);
61  reset();
62  alDeleteSources(1, &m_source);
63  }
64 
65  void SoundEmitter::reset(bool defaultall) {
66  if (m_soundclip != NULL) {
67 
68  setPeriod(-1);
69  alSourceStop(m_source);
70 
71  // Release all buffers
72  alSourcei(m_source, AL_BUFFER, AL_NONE);
73  alGetError();
74 
75  if (m_soundclip->isStream()) {
76  m_soundclip->quitStreaming(m_streamid);
77  }
78 
79  // release the soundclip
80  m_pool->release(m_soundclipid, true);
81  m_soundclip = NULL;
82 
83  // default source properties
84  if (defaultall) {
85  setPosition(0.0f, 0.0f, 0.0f);
86  setVelocity(0.0f, 0.0f, 0.0f);
87  setGain(1.0f);
88  setPositioning(false);
89  alSourcei(m_source, AL_LOOPING, AL_FALSE);
90  }
91  }
92  }
93 
95  m_manager->releaseEmitter(m_emitterid);
96  }
97 
98  void SoundEmitter::setSoundClip(unsigned int sound_id) {
99  m_soundclipid = sound_id;
100  m_soundclip = &(m_pool->getSoundClip(m_soundclipid));
101  m_soundclip->addRef();
102 
103  attachSoundClip();
104  }
105 
106  void SoundEmitter::setCallback(const type_callback& cb) {
107  m_callback = cb;
108  }
109 
110  void SoundEmitter::attachSoundClip() {
111  if (!m_soundclip->isStream()) {
112  // non-streaming
113  alSourceQueueBuffers(m_source, m_soundclip->countBuffers(), m_soundclip->getBuffers());
114  alSourcei(m_source, AL_LOOPING, m_loop ? AL_TRUE : AL_FALSE);
115 
116  } else {
117  // streaming
118  m_streamid = m_soundclip->beginStreaming();
119  m_soundclip->acquireStream(m_streamid);
120 
121  // queue initial buffers
122  alSourceQueueBuffers(m_source, BUFFER_NUM, m_soundclip->getBuffers(m_streamid));
123  alSourcei(m_source, AL_LOOPING, AL_FALSE);
124  }
125 
126  CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error attaching sound clip")
127  }
128 
129  void SoundEmitter::updateEvent(unsigned long time) {
130  ALint procs;
131  ALint bufs;
132  ALuint buffer;
133 
134  alGetSourcei(m_source, AL_BUFFERS_PROCESSED, &procs);
135 
136  while (procs--) {
137  alSourceUnqueueBuffers(m_source, 1, &buffer);
138 
139  if (m_soundclip->getStream(m_streamid, buffer)) {
140  // EOF!
141  if (m_loop) {
142  // play again from the beginning
143  m_soundclip->setStreamPos(m_streamid, SD_BYTE_POS, 0);
144  m_soundclip->getStream(m_streamid, buffer);
145  } else {
146 
147  // check if the playback has been finished
148  alGetSourcei(m_source, AL_BUFFERS_QUEUED, &bufs);
149  if (bufs == 0) {
150  setPeriod(-1);
151  alSourceStop(m_source);
152  if(m_callback) {
153  m_callback();
154  }
155  }
156  continue;
157  }
158  }
159  alSourceQueueBuffers(m_source, 1, &buffer);
160  }
161 
162  CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error while streaming")
163  }
164 
165  void SoundEmitter::setLooping(bool loop) {
166  if (m_soundclip) {
167  if (!m_soundclip->isStream()) {
168  alSourcei(m_source, AL_LOOPING, loop ? AL_TRUE : AL_FALSE);
169  } else {
170  alSourcei(m_source, AL_LOOPING, AL_FALSE);
171  }
172  }
173  m_loop = loop;
174  }
175 
177  if (m_soundclip) {
178  alSourcePlay(m_source);
179  if (m_soundclip->isStream()) {
180  setPeriod(5000);
181  }
182  }
183  }
184 
186  if (m_soundclip) {
187  alSourceStop(m_source);
188 
189  if (m_soundclip->isStream()) {
190  setPeriod(-1);
191  setCursor(SD_BYTE_POS, 0);
192  } else {
193  alSourceRewind(m_source);
194  }
195  }
196  }
197 
198  void SoundEmitter::setCursor(SoundPositionType type, float value) {
199  if (!m_soundclip) {
200  return;
201  }
202 
203  ALint state = 0;
204 
205  if (!m_soundclip->isStream()) {
206  switch(type) {
207  case SD_BYTE_POS:
208  alSourcef(m_source, AL_BYTE_OFFSET, value);
209  break;
210  case SD_SAMPLE_POS:
211  alSourcef(m_source, AL_SAMPLE_OFFSET, value);
212  break;
213  case SD_TIME_POS:
214  alSourcef(m_source, AL_SEC_OFFSET, value);
215  break;
216  }
217 
218  CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error setting cursor position")
219  }
220  else {
221  alGetSourcei(m_source, AL_SOURCE_STATE, &state);
222 
223  if (state == AL_PLAYING || AL_PAUSED) {
224  setPeriod(-1);
225  alSourceStop(m_source);
226  }
227 
228  m_soundclip->setStreamPos(m_streamid, type, value);
229 
230  // detach all buffers
231  alSourcei(m_source, AL_BUFFER, 0);
232 
233  // queue the buffers with new data
234  m_soundclip->acquireStream(m_streamid);
235  alSourceQueueBuffers(m_source, BUFFER_NUM, m_soundclip->getBuffers(m_streamid));
236 
237  if (state == AL_PLAYING) {
238  setPeriod(5000);
239  alSourcePlay(m_source);
240  }
241 
242  CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error setting stream cursor position")
243  }
244  }
245 
247  if (!m_soundclip) {
248  return 0.0f;
249  }
250 
251  ALfloat pos = 0.0f;
252 
253  switch(type) {
254  case SD_BYTE_POS:
255  alGetSourcef(m_source, AL_BYTE_OFFSET, &pos);
256  break;
257  case SD_SAMPLE_POS:
258  alGetSourcef(m_source, AL_SAMPLE_OFFSET, &pos);
259  break;
260  case SD_TIME_POS:
261  alGetSourcef(m_source, AL_SEC_OFFSET, &pos);
262  break;
263  }
264 
265  if (m_soundclip->isStream()) {
266  pos += m_soundclip->getStreamPos(m_streamid, type);
267  }
268 
269  CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error getting cursor")
270 
271  return pos;
272  }
273 }