File: soundemitter.cpp

package info (click to toggle)
fife 0.3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,012 kB
  • ctags: 26,660
  • sloc: cpp: 84,903; sh: 10,269; python: 8,753; xml: 1,213; makefile: 265; objc: 245; ansic: 229
file content (272 lines) | stat: -rw-r--r-- 7,441 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/***************************************************************************
 *   Copyright (C) 2005-2013 by the FIFE team                              *
 *   http://www.fifengine.net                                              *
 *   This file is part of FIFE.                                            *
 *                                                                         *
 *   FIFE is free software; you can redistribute it and/or                 *
 *   modify it under the terms of the GNU Lesser General Public            *
 *   License as published by the Free Software Foundation; either          *
 *   version 2.1 of the License, or (at your option) any later version.    *
 *                                                                         *
 *   This library is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   Lesser General Public License for more details.                       *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this library; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
 ***************************************************************************/

// Standard C++ library includes

// Platform specific includes

// 3rd party library includes

// FIFE includes
// These includes are split up in two parts, separated by one empty line
// First block: files included from the FIFE root src directory
// Second block: files included from the same folder
#include "util/log/logger.h"
#include "util/time/timemanager.h"
#include "util/base/exception.h"
#include "soundemitter.h"
#include "soundmanager.h"
#include "soundclipmanager.h"

namespace FIFE {
	static Logger _log(LM_AUDIO);

	SoundEmitter::SoundEmitter(SoundManager* manager, uint32_t uid) : m_manager(manager), m_source(0), m_soundclip(), m_soundclipid(0), m_streamid(0),
															m_emitterid(uid), m_loop(false) {
		if (!m_manager->isActive()) {
			return;
		}

		TimeManager::instance()->registerEvent(this);
		setPeriod(-1);
		alGenSources(1, &m_source);
		CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error creating source")
	}

	SoundEmitter::~SoundEmitter() {
		if (!m_manager->isActive()) {
			return;
		}

		setPeriod(-1);
		TimeManager::instance()->unregisterEvent(this);
		reset();
		alDeleteSources(1, &m_source);
	}

	void SoundEmitter::reset(bool defaultall) {
		if (m_soundclip) {

			setPeriod(-1);
			alSourceStop(m_source);

			// Release all buffers
			alSourcei(m_source, AL_BUFFER, AL_NONE);
			alGetError();

			if (m_soundclip->isStream()) {
				m_soundclip->quitStreaming(m_streamid);
			}

			// release the soundclip
			//SoundClipManager::instance()->free(m_soundclipid);
			m_soundclip.reset();

			// default source properties
			if (defaultall) {
				setPosition(0.0f, 0.0f, 0.0f);
				setVelocity(0.0f, 0.0f, 0.0f);
				setGain(1.0f);
				setPositioning(false);
				alSourcei(m_source, AL_LOOPING, AL_FALSE);
			}
		}
	}

	void SoundEmitter::release() {
		m_manager->releaseEmitter(m_emitterid);
	}

	void SoundEmitter::setSoundClip(SoundClipPtr soundclip) {
		m_soundclipid = soundclip->getHandle();
		m_soundclip = soundclip;

		attachSoundClip();
	}

	void SoundEmitter::setCallback(const type_callback& cb) {
		m_callback = cb;
	}

	void SoundEmitter::attachSoundClip() {
		if (!m_soundclip->isStream()) {
			// non-streaming
			alSourceQueueBuffers(m_source, m_soundclip->countBuffers(), m_soundclip->getBuffers());
			alSourcei(m_source, AL_LOOPING, m_loop ? AL_TRUE : AL_FALSE);

		} else {
			// streaming
			m_streamid = m_soundclip->beginStreaming();
			m_soundclip->acquireStream(m_streamid);

			// queue initial buffers
			alSourceQueueBuffers(m_source, BUFFER_NUM, m_soundclip->getBuffers(m_streamid));
			alSourcei(m_source, AL_LOOPING, AL_FALSE);
		}

		CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error attaching sound clip")
	}

	void SoundEmitter::updateEvent(uint32_t time) {
		ALint procs;
		ALint bufs;
		ALuint buffer;

		alGetSourcei(m_source, AL_BUFFERS_PROCESSED, &procs);

		while (procs--) {
			alSourceUnqueueBuffers(m_source, 1, &buffer);

			if (m_soundclip->getStream(m_streamid, buffer)) {
				// EOF!
				if (m_loop) {
					// play again from the beginning
					m_soundclip->setStreamPos(m_streamid, SD_BYTE_POS, 0);
					m_soundclip->getStream(m_streamid, buffer);
				} else {

					// check if the playback has been finished
					alGetSourcei(m_source, AL_BUFFERS_QUEUED, &bufs);
					if (bufs == 0) {
						setPeriod(-1);
						alSourceStop(m_source);
						if(m_callback) {
							m_callback();
						}
					}
					continue;
				}
			}
			alSourceQueueBuffers(m_source, 1, &buffer);
		}

		CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error while streaming")
	}

	void SoundEmitter::setLooping(bool loop) {
		if (m_soundclip) {
			if (!m_soundclip->isStream()) {
				alSourcei(m_source, AL_LOOPING, loop ? AL_TRUE : AL_FALSE);
			} else {
				alSourcei(m_source, AL_LOOPING, AL_FALSE);
			}
		}
		m_loop = loop;
	}

	void SoundEmitter::play() {
		if (m_soundclip) {
			alSourcePlay(m_source);
			if (m_soundclip->isStream()) {
				setPeriod(5000);
			}
		}
	}

	void SoundEmitter::stop() {
		if (m_soundclip) {
			alSourceStop(m_source);

			if (m_soundclip->isStream()) {
				setPeriod(-1);
				setCursor(SD_BYTE_POS, 0);
			} else {
				alSourceRewind(m_source);
			}
		}
	}

	void SoundEmitter::setCursor(SoundPositionType type, float value) {
		if (!m_soundclip) {
			return;
		}

		ALint state = 0;

		if (!m_soundclip->isStream()) {
			switch(type) {
			case SD_BYTE_POS:
				alSourcef(m_source, AL_BYTE_OFFSET, value);
				break;
			case SD_SAMPLE_POS:
				alSourcef(m_source, AL_SAMPLE_OFFSET, value);
				break;
			case SD_TIME_POS:
				alSourcef(m_source, AL_SEC_OFFSET, value);
				break;
			}

			CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error setting cursor position")
		}
		else {
			alGetSourcei(m_source, AL_SOURCE_STATE, &state);

			if (state == AL_PLAYING || AL_PAUSED) {
				setPeriod(-1);
				alSourceStop(m_source);
			}

			m_soundclip->setStreamPos(m_streamid, type, value);

			// detach all buffers
			alSourcei(m_source, AL_BUFFER, 0);

			// queue the buffers with new data
			m_soundclip->acquireStream(m_streamid);
			alSourceQueueBuffers(m_source, BUFFER_NUM, m_soundclip->getBuffers(m_streamid));

			if (state == AL_PLAYING) {
				setPeriod(5000);
				alSourcePlay(m_source);
			}

			CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error setting stream cursor position")
		}
	}

	float SoundEmitter::getCursor(SoundPositionType type) {
		if (!m_soundclip) {
			return 0.0f;
		}

		ALfloat pos = 0.0f;

		switch(type) {
			case SD_BYTE_POS:
				alGetSourcef(m_source, AL_BYTE_OFFSET, &pos);
				break;
			case SD_SAMPLE_POS:
				alGetSourcef(m_source, AL_SAMPLE_OFFSET, &pos);
				break;
			case SD_TIME_POS:
				alGetSourcef(m_source, AL_SEC_OFFSET, &pos);
				break;
		}

		if (m_soundclip->isStream()) {
			pos += m_soundclip->getStreamPos(m_streamid, type);
		}

		CHECK_OPENAL_LOG(_log, LogManager::LEVEL_ERROR, "error getting cursor")

		return pos;
	}
}