aboutsummaryrefslogtreecommitdiff
path: root/engines/sword25/sfx/fmodexchannel.cpp
blob: 88ad9640c53715c28c4bfb4733fefe59cf6e6533 (plain)
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
// -----------------------------------------------------------------------------
// This file is part of Broken Sword 2.5
// Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsd�rfer
//
// Broken Sword 2.5 is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Broken Sword 2.5 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Broken Sword 2.5; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
// Logging
// -----------------------------------------------------------------------------

#define BS_LOG_PREFIX "FMODEXCHANNEL"

// -----------------------------------------------------------------------------
// Includes
// -----------------------------------------------------------------------------

#include "fmod.h"
#include "fmodexexception.h"
#include "fmodexchannel.h"

// -----------------------------------------------------------------------------
// Konstruktion / Destruktion
// -----------------------------------------------------------------------------

BS_FMODExChannel::BS_FMODExChannel(FMOD_CHANNEL * ChannelPtr, FMOD_SOUND * SoundPtr) :
	m_ChannelPtr(ChannelPtr),
	m_SoundPtr(SoundPtr)
{
}

// -----------------------------------------------------------------------------

BS_FMODExChannel::~BS_FMODExChannel()
{
	if (m_ChannelPtr) FMOD_Channel_Stop(m_ChannelPtr);
	if (m_SoundPtr) FMOD_Sound_Release(m_SoundPtr);
}

// -----------------------------------------------------------------------------
// FMOD Ex macht alle Kan�le ung�ltig, sobald sie nicht mehr abgespielt werden,
// oder wenn der Kanal in der zwischenzeit neu vergeben wurde.
// Dann f�hren alle Aufrufe von Funktionen dieser Kan�le zu dem Fehlern
// FMOD_ERR_INVALID_HANDLE oder FMOD_ERR_CHANNEL_STOLEN
// Dieses Soundsystem entfernt aber nur jeden Frame alle toten Kan�le. Daher
// kann es vorkommen, dass an einem bereits toten Kanal Aufrufe get�tigt werden.
// Diese Fehler werden daher von den folgenden Methoden ignoriert.
// -----------------------------------------------------------------------------

namespace
{
	bool IsImportantError(FMOD_RESULT Result)
	{
		return Result != FMOD_OK && Result != FMOD_ERR_INVALID_HANDLE && Result != FMOD_ERR_CHANNEL_STOLEN;
	}
}

// -----------------------------------------------------------------------------
// Setter
// -----------------------------------------------------------------------------

bool BS_FMODExChannel::SetPaused(bool Paused)
{
	BS_ASSERT(m_ChannelPtr);

	FMOD_RESULT Result = FMOD_Channel_SetPaused(m_ChannelPtr, Paused ? 1 : 0);
	if (IsImportantError(Result))
	{
		BS_FMODExException("FMOD_Channel_SetPaused()", Result).Log();
		return false;
	}
	else
		return true;
}

// -----------------------------------------------------------------------------

bool BS_FMODExChannel::SetVolume(float Volume)
{
	BS_ASSERT(m_ChannelPtr);

	FMOD_RESULT Result = FMOD_Channel_SetVolume(m_ChannelPtr, Volume);
	if (IsImportantError(Result))
	{
		BS_FMODExException("FMOD_Channel_SetVolume()", Result).Log();
		return false;
	}
	else
		return true;
}

// -----------------------------------------------------------------------------

bool BS_FMODExChannel::SetPanning(float Panning)
{
	BS_ASSERT(m_ChannelPtr);

	FMOD_RESULT Result = FMOD_Channel_SetPan(m_ChannelPtr, Panning);
	if (IsImportantError(Result))
	{
		BS_FMODExException("FMOD_Channel_SetPan()", Result).Log();
		return false;
	}
	else
		return true;
}

// -----------------------------------------------------------------------------

bool BS_FMODExChannel::SetLoop(bool Loop)
{
	BS_ASSERT(m_ChannelPtr);

	FMOD_RESULT Result = FMOD_Channel_SetLoopCount(m_ChannelPtr, Loop ? -1 : 0);
	if (IsImportantError(Result))
	{
		BS_FMODExException("FMOD_Channel_SetLoopCount()", Result).Log();
		return false;
	}
	else
		return true;
}

// -----------------------------------------------------------------------------

bool BS_FMODExChannel::SetLoopPoints(unsigned int LoopStart, unsigned int LoopEnd)
{
	BS_ASSERT(m_ChannelPtr);

	FMOD_RESULT Result = FMOD_Channel_SetLoopPoints(m_ChannelPtr, LoopStart, FMOD_TIMEUNIT_PCM, LoopEnd, FMOD_TIMEUNIT_PCM);
	if (IsImportantError(Result))
	{
		BS_FMODExException("FMOD_Channel_SetLoopPoints()", Result).Log();
		return false;
	}
	else
		return true;
}

// -----------------------------------------------------------------------------

bool BS_FMODExChannel::SetPosition(unsigned int Position)
{
	BS_ASSERT(m_ChannelPtr);

	FMOD_RESULT Result = FMOD_Channel_SetPosition(m_ChannelPtr, Position, FMOD_TIMEUNIT_PCM);
	if (IsImportantError(Result))
	{
		BS_FMODExException("FMOD_Channel_SetPosition()", Result).Log();
		return false;
	}
	else
		return true;
}

// -----------------------------------------------------------------------------

bool BS_FMODExChannel::Stop()
{
	BS_ASSERT(m_ChannelPtr);

	FMOD_RESULT Result = FMOD_Channel_Stop(m_ChannelPtr);
	if (IsImportantError(Result))
	{
		BS_FMODExException("FMOD_Channel_Stop()", Result).Log();
		return false;
	}
	else
		return true;
}

// -----------------------------------------------------------------------------
// Getter
// -----------------------------------------------------------------------------

float BS_FMODExChannel::GetVolume()
{
	BS_ASSERT(m_ChannelPtr);

	float Volume = 0;
	FMOD_RESULT Result = FMOD_Channel_GetVolume(m_ChannelPtr, &Volume);
	if (IsImportantError(Result)) BS_FMODExException("FMOD_Channel_GetVolume()", Result).Log();
	
	return Volume;
}

// -----------------------------------------------------------------------------

float BS_FMODExChannel::GetPanning()
{
	BS_ASSERT(m_ChannelPtr);

	float Panning = 0;
	FMOD_RESULT Result = FMOD_Channel_GetPan(m_ChannelPtr, &Panning);
	if (IsImportantError(Result)) BS_FMODExException("FMOD_Channel_GetPan()", Result).Log();
	
	return Panning;
}

// -----------------------------------------------------------------------------

unsigned int BS_FMODExChannel::GetPosition()
{
	BS_ASSERT(m_ChannelPtr);

	unsigned int Position = 0;
	FMOD_RESULT Result = FMOD_Channel_GetPosition(m_ChannelPtr, &Position, FMOD_TIMEUNIT_PCM);
	if (IsImportantError(Result)) BS_FMODExException("FMOD_Channel_GetPosition()", Result).Log();

	return Position;
}

// -----------------------------------------------------------------------------

unsigned int BS_FMODExChannel::GetTime()
{
	BS_ASSERT(m_ChannelPtr);

	unsigned int Time = 0;
	FMOD_RESULT Result = FMOD_Channel_GetPosition(m_ChannelPtr, &Time, FMOD_TIMEUNIT_MS);
	if (IsImportantError(Result)) BS_FMODExException("FMOD_Channel_GetPosition()", Result).Log();

	return Time;
}

// -----------------------------------------------------------------------------

unsigned int BS_FMODExChannel::GetLoopStart()
{
	BS_ASSERT(m_ChannelPtr);
	unsigned int LoopStart = 0;
	FMOD_RESULT Result = FMOD_Channel_GetLoopPoints(m_ChannelPtr, &LoopStart, FMOD_TIMEUNIT_PCM, 0, FMOD_TIMEUNIT_PCM);
	if (IsImportantError(Result)) BS_FMODExException("FMOD_Channel_GetLoopPoints()", Result).Log();

	return LoopStart;
}

// -----------------------------------------------------------------------------

unsigned int BS_FMODExChannel::GetLoopEnd()
{
	BS_ASSERT(m_ChannelPtr);
	unsigned int LoopEnd = 0;
	FMOD_RESULT Result = FMOD_Channel_GetLoopPoints(m_ChannelPtr, 0, FMOD_TIMEUNIT_PCM, &LoopEnd, FMOD_TIMEUNIT_PCM);
	if (IsImportantError(Result)) BS_FMODExException("FMOD_Channel_GetLoopPoints()", Result).Log();

	return LoopEnd;
}

// -----------------------------------------------------------------------------

bool BS_FMODExChannel::IsLooping()
{
	BS_ASSERT(m_ChannelPtr);

	int LoopCount = 0;
	FMOD_RESULT Result = FMOD_Channel_GetLoopCount(m_ChannelPtr, &LoopCount);
	if (IsImportantError(Result)) BS_FMODExException("FMOD_Channel_GetLoopCount()", Result).Log();
	
	return LoopCount == -1;
}

// -----------------------------------------------------------------------------

bool BS_FMODExChannel::IsPaused()
{
	BS_ASSERT(m_ChannelPtr);

	FMOD_BOOL Paused = 0;
	FMOD_RESULT Result = FMOD_Channel_GetPaused(m_ChannelPtr, &Paused);
	if (IsImportantError(Result)) BS_FMODExException("FMOD_Channel_GetPaused()", Result).Log();
	
	return Paused != 0;
}

// -----------------------------------------------------------------------------

bool BS_FMODExChannel::IsPlaying()
{
	BS_ASSERT(m_ChannelPtr);

	FMOD_BOOL Playing = 0;
	FMOD_RESULT Result = FMOD_Channel_IsPlaying(m_ChannelPtr, &Playing);
	if (IsImportantError(Result))	BS_FMODExException("FMOD_Channel_IsPlaying()", Result).Log();

	return Playing != 0;
}