File: Audio.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (302 lines) | stat: -rw-r--r-- 6,021 bytes parent folder | download | duplicates (3)
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "stdafx.h"
#include "Audio.h"
#include "Player.h"
#include "LibData.h"
#include "Exception.h"
#include "mpg123.h"

namespace sound {

	AudioMgr::AudioMgr() : wait(null), soundDevice() {
		players = new (this) WeakSet<Player>();

		try {
			init();
		} catch (const storm::Exception *e) {
			std::wcout << e->toS() << endl;
			soundDevice = SoundDevice();
		} catch (const ::Exception &e) {
			std::wcout << e.what() << endl;
			soundDevice = SoundDevice();
		}

		// We need to initialize the MP3 backend.
		mpg123_init();
	}

	void AudioMgr::terminate() {
		if (wait) {
			wait->terminate();
			wait = null;
		}

		// We're likely running in a different thread that the thread that set up OpenAL. We need to
		// activate the OpenAL context once more to make 'close' work as expected.
		activate();

		WeakSet<Player>::Iter i = players->iter();
		while (Player *p = i.next()) {
			p->close();
		}

		destroy();
	}

	void AudioMgr::addPlayer(Player *player) {
		players->put(player);
	}

	void AudioMgr::removePlayer(Player *player) {
		players->remove(player);
	}

	AudioMgr *audioMgr(EnginePtr e) {
		AudioMgr *&d = audioMgrData(e.v);
		if (!d)
			d = new (e.v) AudioMgr();
		return d;
	}

	/**
	 * Waiting logic.
	 */

	AudioWait::AudioWait(Engine &e) : e(e), exit(false), working(false), notifyExit(null) {
		AudioMgr *m = audioMgr(e);
		m->wait = this;
	}

	AudioWait::~AudioWait() {
		destroy();
	}

	void AudioWait::doExit() {
		if (exit && !working && notifyExit) {
			notifyExit->up();
			notifyExit = null;
		}
	}

	void AudioWait::terminate() {
		os::Sema sync(0);
		notifyExit = &sync;

		exit = true;

		// Allow the thread to exit properly...
		signal();
		sync.down();
	}

	os::Thread spawnAudioThread(Engine &e) {
		return os::Thread::spawn(new AudioWait(e), runtime::threadGroup(e));
	}

	/**
	 * Backend specific code.
	 */
#ifdef SOUND_DX

	void AudioMgr::init() {
		IDirectSound8 *dsound = null;
		if (FAILED(DirectSoundCreate8(NULL, &dsound, NULL))) {
			throw new (this) SoundInitError();
		} else {
			// This is fine since we do not do anything that depends on focus:
			// https://groups.google.com/forum/#!msg/microsoft.public.win32.programmer.directx.audio/a7QhlgNFBpg/w8lTd8_NRSEJ
			dsound->SetCooperativeLevel(GetDesktopWindow(), DSSCL_PRIORITY);
			soundDevice = dsound;
		}
	}

	void AudioMgr::destroy() {
		soundDevice.release();
	}

	void AudioMgr::activate() {}

	static bool signaled(HANDLE event) {
		return WaitForSingleObjectEx(event, 0, FALSE) != WAIT_TIMEOUT;
	}

	void AudioMgr::notifyEvents() {
		WeakSet<Player>::Iter i = players->iter();
		while (Player *p = i.next()) {
			if (p->waitEvent() == Handle())
				continue;

			HANDLE event = p->waitEvent().v();
			if (signaled(event)) {
				ResetEvent(event);
				p->onNotify();
			}
		}
	}

	void AudioMgr::allEvents(vector<HANDLE> &all) {
		all.resize(2);
		WeakSet<Player>::Iter i = players->iter();
		while (Player *p = i.next()) {
			Handle event = p->waitEvent();
			if (event != Handle())
				all.push_back(event.v());
		}
	}

	void AudioWait::init() {
		events.push_back(NULL);
		events.push_back(CreateEvent(NULL, TRUE, FALSE, NULL));
	}

	void AudioWait::destroy() {
		CloseHandle(events[1]);
	}

	bool AudioWait::wait(os::IOHandle &io) {
		return wait(io, INFINITE);
	}

	bool AudioWait::wait(os::IOHandle &io, nat ms) {
		if (exit) {
			doExit();
			return false;
		}

		events[0] = io.v();
		Nat first = events[0] ? 0 : 1;
		Nat count = 2;
		if (!working) {
			AudioMgr *m = audioMgr(e);
			m->allEvents(events);
			count = nat(events.size());
		}

		DWORD z = WaitForMultipleObjectsEx(count - first, &events[first], FALSE, ms, FALSE);
		ResetEvent(events[1]);

		if (exit)
			doExit();
		return !exit;
	}

	void AudioWait::signal() {
		SetEvent(events[1]);
	}

	void AudioWait::work() {
		AudioMgr *m = audioMgr(e);
		working = true;
		m->notifyEvents();
		working = false;
		doExit();
	}

#endif
#ifdef SOUND_AL

	/**
	 * Note: There does not seem to be any sophisticated mechanism to get notified when sound
	 * buffers in OpenAL are finished playing. So we simply make sure that we do not sleep longer
	 * than a couple of hundred ms each time, so that we can poll OpenAL often enough.
	 */
	static const nat MAX_WAIT_MS = 100;

	void AudioMgr::init() {
		soundDevice = alcOpenDevice(NULL);
		if (!soundDevice)
			throw new (this) SoundInitError();

		soundContext = alcCreateContext(soundDevice, NULL);
		if (!soundContext)
			throw new (this) SoundInitError();

		alcMakeContextCurrent(soundContext);

		// Set up the listener.
		alListener3f(AL_POSITION, 0, 0, 1);
		alListener3f(AL_VELOCITY, 0, 0, 0);
		ALfloat orientation[] = { 0, 0, 1, 0, 1, 0 };
		alListenerfv(AL_ORIENTATION, orientation);
	}

	void AudioMgr::destroy() {
		alcDestroyContext(soundContext);
		alcCloseDevice(soundDevice);
	}

	void AudioMgr::activate() {
		if (soundContext)
			alcMakeContextCurrent(soundContext);
	}

	void AudioMgr::notifyEvents() {
		WeakSet<Player>::Iter i = players->iter();
		while (Player *p = i.next()) {
			if (p->playing())
				p->onNotify();
		}
	}

	bool AudioMgr::anyPlayback() {
		WeakSet<Player>::Iter i = players->iter();
		while (Player *p = i.next()) {
			if (p->playing())
				return true;
		}

		return false;
	}

	void AudioWait::init() {}

	void AudioWait::destroy() {}

	bool AudioWait::wait(os::IOHandle &io) {
		if (exit) {
			doExit();
			return false;
		}

		if (!working && audioMgr(e)->anyPlayback()) {
			ioWait.wait(io, MAX_WAIT_MS);
		} else {
			ioWait.wait(io);
		}

		if (exit)
			doExit();
		return !exit;
	}

	bool AudioWait::wait(os::IOHandle &io, nat ms) {
		if (exit) {
			doExit();
			return false;
		}

		if (!working && audioMgr(e)->anyPlayback()) {
			ioWait.wait(io, min(MAX_WAIT_MS, ms));
		} else {
			ioWait.wait(io, ms);
		}

		if (exit)
			doExit();
		return !exit;
	}

	void AudioWait::signal() {
		ioWait.signal();
	}

	void AudioWait::work() {
		AudioMgr *m = audioMgr(e);
		working = true;
		m->notifyEvents();
		working = false;
		doExit();
	}

#endif

}