6 * ***** BEGIN GPL LICENSE BLOCK *****
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23 * All rights reserved.
25 * The Original Code is: all of this file.
27 * Contributor(s): none yet.
29 * ***** END GPL LICENSE BLOCK *****
33 /** \file gameengine/Ketsji/KX_SoundActuator.cpp
38 #include "KX_SoundActuator.h"
41 # include "AUD_C-API.h"
42 # include "AUD_PingPongFactory.h"
43 # include "AUD_IDevice.h"
44 # include "AUD_I3DHandle.h"
47 #include "KX_GameObject.h"
48 #include "KX_PyMath.h" // needed for PyObjectFrom()
49 #include "KX_PythonInit.h"
50 #include "KX_Camera.h"
53 /* ------------------------------------------------------------------------- */
54 /* Native functions */
55 /* ------------------------------------------------------------------------- */
56 KX_SoundActuator::KX_SoundActuator(SCA_IObject* gameobj,
57 AUD_Reference<AUD_IFactory> sound,
61 KX_3DSoundSettings settings,
62 KX_SOUNDACT_TYPE type)//,
63 : SCA_IActuator(gameobj, KX_ACT_SOUND)
76 KX_SoundActuator::~KX_SoundActuator()
78 if(!m_handle.isNull())
82 void KX_SoundActuator::play()
84 if(!m_handle.isNull())
90 // this is the sound that will be played and not deleted afterwards
91 AUD_Reference<AUD_IFactory> sound = m_sound;
97 case KX_SOUNDACT_LOOPBIDIRECTIONAL:
98 case KX_SOUNDACT_LOOPBIDIRECTIONAL_STOP:
99 sound = new AUD_PingPongFactory(sound);
101 case KX_SOUNDACT_LOOPEND:
102 case KX_SOUNDACT_LOOPSTOP:
105 case KX_SOUNDACT_PLAYSTOP:
106 case KX_SOUNDACT_PLAYEND:
113 m_handle = AUD_getDevice()->play(sound, 0);
115 catch(AUD_Exception&)
117 // cannot play back, ignore
121 AUD_Reference<AUD_I3DHandle> handle3d = AUD_Reference<AUD_I3DHandle>(m_handle);
123 if(m_is3d && !handle3d.isNull())
125 handle3d->setRelative(true);
126 handle3d->setVolumeMaximum(m_3d.max_gain);
127 handle3d->setVolumeMinimum(m_3d.min_gain);
128 handle3d->setDistanceReference(m_3d.reference_distance);
129 handle3d->setDistanceMaximum(m_3d.max_distance);
130 handle3d->setAttenuation(m_3d.rolloff_factor);
131 handle3d->setConeAngleInner(m_3d.cone_inner_angle);
132 handle3d->setConeAngleOuter(m_3d.cone_outer_angle);
133 handle3d->setConeVolumeOuter(m_3d.cone_outer_gain);
137 m_handle->setLoopCount(-1);
138 m_handle->setPitch(m_pitch);
139 m_handle->setVolume(m_volume);
143 CValue* KX_SoundActuator::GetReplica()
145 KX_SoundActuator* replica = new KX_SoundActuator(*this);
146 replica->ProcessReplica();
150 void KX_SoundActuator::ProcessReplica()
152 SCA_IActuator::ProcessReplica();
153 m_handle = AUD_Reference<AUD_IHandle>();
156 bool KX_SoundActuator::Update(double curtime, bool frame)
162 // do nothing on negative events, otherwise sounds are played twice!
163 bool bNegativeEvent = IsNegativeEvent();
164 bool bPositiveEvent = m_posevent;
171 // actual audio device playing state
172 bool isplaying = m_handle.isNull() ? false : (m_handle->getStatus() == AUD_STATUS_PLAYING);
176 // here must be a check if it is still playing
177 if (m_isplaying && isplaying)
181 case KX_SOUNDACT_PLAYSTOP:
182 case KX_SOUNDACT_LOOPSTOP:
183 case KX_SOUNDACT_LOOPBIDIRECTIONAL_STOP:
186 if(!m_handle.isNull())
188 m_handle = AUD_Reference<AUD_IHandle>();
191 case KX_SOUNDACT_PLAYEND:
193 // do nothing, sound will stop anyway when it's finished
196 case KX_SOUNDACT_LOOPEND:
197 case KX_SOUNDACT_LOOPBIDIRECTIONAL:
199 // stop the looping so that the sound stops when it finished
200 if(!m_handle.isNull())
201 m_handle->setLoopCount(0);
209 // remember that we tried to stop the actuator
214 // Warning: when de-activating the actuator, after a single negative event this runs again with...
215 // m_posevent==false && m_posevent==false, in this case IsNegativeEvent() returns false
216 // and assumes this is a positive event.
217 // check that we actually have a positive event so as not to play sounds when being disabled.
218 else if(bPositiveEvent) { // <- added since 2.49
220 else { // <- works in most cases except a loop-end sound will never stop unless
221 // the negative pulse is done continuesly
226 // verify that the sound is still playing
227 isplaying = m_handle.isNull() ? false : (m_handle->getStatus() == AUD_STATUS_PLAYING);
231 AUD_Reference<AUD_I3DHandle> handle3d = AUD_Reference<AUD_I3DHandle>(m_handle);
233 if(m_is3d && !handle3d.isNull())
235 KX_Camera* cam = KX_GetActiveScene()->GetActiveCamera();
238 KX_GameObject* obj = (KX_GameObject*)this->GetParent();
244 Mo = cam->NodeGetWorldOrientation().inverse();
245 p = (obj->NodeGetWorldPosition() - cam->NodeGetWorldPosition());
248 handle3d->setSourceLocation(v);
249 p = (obj->GetLinearVelocity() - cam->GetLinearVelocity());
252 handle3d->setSourceVelocity(v);
253 (Mo * obj->NodeGetWorldOrientation()).getRotation().getValue(q);
254 handle3d->setSourceOrientation(AUD_Quaternion(q[3], q[0], q[1], q[2]));
269 /* ------------------------------------------------------------------------- */
270 /* Python functions */
271 /* ------------------------------------------------------------------------- */
275 /* Integration hooks ------------------------------------------------------- */
276 PyTypeObject KX_SoundActuator::Type = {
277 PyVarObject_HEAD_INIT(NULL, 0)
279 sizeof(PyObjectPlus_Proxy),
288 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
293 &SCA_IActuator::Type,
298 PyMethodDef KX_SoundActuator::Methods[] = {
299 KX_PYMETHODTABLE_NOARGS(KX_SoundActuator, startSound),
300 KX_PYMETHODTABLE_NOARGS(KX_SoundActuator, pauseSound),
301 KX_PYMETHODTABLE_NOARGS(KX_SoundActuator, stopSound),
302 {NULL, NULL} //Sentinel
305 PyAttributeDef KX_SoundActuator::Attributes[] = {
306 KX_PYATTRIBUTE_BOOL_RO("is3D", KX_SoundActuator, m_is3d),
307 KX_PYATTRIBUTE_RW_FUNCTION("volume_maximum", KX_SoundActuator, pyattr_get_3d_property, pyattr_set_3d_property),
308 KX_PYATTRIBUTE_RW_FUNCTION("volume_minimum", KX_SoundActuator, pyattr_get_3d_property, pyattr_set_3d_property),
309 KX_PYATTRIBUTE_RW_FUNCTION("distance_reference", KX_SoundActuator, pyattr_get_3d_property, pyattr_set_3d_property),
310 KX_PYATTRIBUTE_RW_FUNCTION("distance_maximum", KX_SoundActuator, pyattr_get_3d_property, pyattr_set_3d_property),
311 KX_PYATTRIBUTE_RW_FUNCTION("attenuation", KX_SoundActuator, pyattr_get_3d_property, pyattr_set_3d_property),
312 KX_PYATTRIBUTE_RW_FUNCTION("cone_angle_inner", KX_SoundActuator, pyattr_get_3d_property, pyattr_set_3d_property),
313 KX_PYATTRIBUTE_RW_FUNCTION("cone_angle_outer", KX_SoundActuator, pyattr_get_3d_property, pyattr_set_3d_property),
314 KX_PYATTRIBUTE_RW_FUNCTION("cone_volume_outer", KX_SoundActuator, pyattr_get_3d_property, pyattr_set_3d_property),
315 KX_PYATTRIBUTE_RW_FUNCTION("sound", KX_SoundActuator, pyattr_get_sound, pyattr_set_sound),
317 KX_PYATTRIBUTE_RW_FUNCTION("time", KX_SoundActuator, pyattr_get_audposition, pyattr_set_audposition),
318 KX_PYATTRIBUTE_RW_FUNCTION("volume", KX_SoundActuator, pyattr_get_gain, pyattr_set_gain),
319 KX_PYATTRIBUTE_RW_FUNCTION("pitch", KX_SoundActuator, pyattr_get_pitch, pyattr_set_pitch),
320 KX_PYATTRIBUTE_ENUM_RW("mode",KX_SoundActuator::KX_SOUNDACT_NODEF+1,KX_SoundActuator::KX_SOUNDACT_MAX-1,false,KX_SoundActuator,m_type),
324 /* Methods ----------------------------------------------------------------- */
325 KX_PYMETHODDEF_DOC_NOARGS(KX_SoundActuator, startSound,
327 "\tStarts the sound.\n")
329 if(!m_handle.isNull())
331 switch(m_handle->getStatus())
333 case AUD_STATUS_PLAYING:
335 case AUD_STATUS_PAUSED:
345 KX_PYMETHODDEF_DOC_NOARGS(KX_SoundActuator, pauseSound,
347 "\tPauses the sound.\n")
349 if(!m_handle.isNull())
354 KX_PYMETHODDEF_DOC_NOARGS(KX_SoundActuator, stopSound,
356 "\tStops the sound.\n")
358 if(!m_handle.isNull())
360 m_handle = AUD_Reference<AUD_IHandle>();
364 /* Atribute setting and getting -------------------------------------------- */
365 PyObject* KX_SoundActuator::pyattr_get_3d_property(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
367 KX_SoundActuator * actuator = static_cast<KX_SoundActuator *> (self);
368 const char* prop = attrdef->m_name;
369 float result_value = 0.0;
371 if(!strcmp(prop, "volume_maximum")) {
372 result_value = actuator->m_3d.max_gain;
374 } else if (!strcmp(prop, "volume_minimum")) {
375 result_value = actuator->m_3d.min_gain;
377 } else if (!strcmp(prop, "distance_reference")) {
378 result_value = actuator->m_3d.reference_distance;
380 } else if (!strcmp(prop, "distance_maximum")) {
381 result_value = actuator->m_3d.max_distance;
383 } else if (!strcmp(prop, "attenuation")) {
384 result_value = actuator->m_3d.rolloff_factor;
386 } else if (!strcmp(prop, "cone_angle_inner")) {
387 result_value = actuator->m_3d.cone_inner_angle;
389 } else if (!strcmp(prop, "cone_angle_outer")) {
390 result_value = actuator->m_3d.cone_outer_angle;
392 } else if (!strcmp(prop, "cone_volume_outer")) {
393 result_value = actuator->m_3d.cone_outer_gain;
399 PyObject* result = PyFloat_FromDouble(result_value);
403 PyObject* KX_SoundActuator::pyattr_get_audposition(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
405 KX_SoundActuator * actuator = static_cast<KX_SoundActuator *> (self);
406 float position = 0.0;
408 if(!actuator->m_handle.isNull())
409 position = actuator->m_handle->getPosition();
411 PyObject* result = PyFloat_FromDouble(position);
416 PyObject* KX_SoundActuator::pyattr_get_gain(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
418 KX_SoundActuator * actuator = static_cast<KX_SoundActuator *> (self);
419 float gain = actuator->m_volume;
421 PyObject* result = PyFloat_FromDouble(gain);
426 PyObject* KX_SoundActuator::pyattr_get_pitch(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
428 KX_SoundActuator * actuator = static_cast<KX_SoundActuator *> (self);
429 float pitch = actuator->m_pitch;
431 PyObject* result = PyFloat_FromDouble(pitch);
436 PyObject* KX_SoundActuator::pyattr_get_sound(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
438 KX_SoundActuator * actuator = static_cast<KX_SoundActuator *> (self);
439 if(!actuator->m_sound.isNull())
440 return AUD_getPythonFactory(&actuator->m_sound);
445 int KX_SoundActuator::pyattr_set_3d_property(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
447 KX_SoundActuator * actuator = static_cast<KX_SoundActuator *> (self);
448 const char* prop = attrdef->m_name;
449 float prop_value = 0.0;
451 if (!PyArg_Parse(value, "f", &prop_value))
452 return PY_SET_ATTR_FAIL;
454 AUD_Reference<AUD_I3DHandle> handle3d = AUD_Reference<AUD_I3DHandle>(actuator->m_handle);
455 // if sound is working and 3D, set the new setting
456 if(!actuator->m_is3d)
457 return PY_SET_ATTR_FAIL;
459 if(!strcmp(prop, "volume_maximum")) {
460 actuator->m_3d.max_gain = prop_value;
461 if(!handle3d.isNull())
462 handle3d->setVolumeMaximum(prop_value);
464 } else if (!strcmp(prop, "volume_minimum")) {
465 actuator->m_3d.min_gain = prop_value;
466 if(!handle3d.isNull())
467 handle3d->setVolumeMinimum(prop_value);
469 } else if (!strcmp(prop, "distance_reference")) {
470 actuator->m_3d.reference_distance = prop_value;
471 if(!handle3d.isNull())
472 handle3d->setDistanceReference(prop_value);
474 } else if (!strcmp(prop, "distance_maximum")) {
475 actuator->m_3d.max_distance = prop_value;
476 if(!handle3d.isNull())
477 handle3d->setDistanceMaximum(prop_value);
479 } else if (!strcmp(prop, "attenuation")) {
480 actuator->m_3d.rolloff_factor = prop_value;
481 if(!handle3d.isNull())
482 handle3d->setAttenuation(prop_value);
484 } else if (!!strcmp(prop, "cone_angle_inner")) {
485 actuator->m_3d.cone_inner_angle = prop_value;
486 if(!handle3d.isNull())
487 handle3d->setConeAngleInner(prop_value);
489 } else if (!strcmp(prop, "cone_angle_outer")) {
490 actuator->m_3d.cone_outer_angle = prop_value;
491 if(!handle3d.isNull())
492 handle3d->setConeAngleOuter(prop_value);
494 } else if (!strcmp(prop, "cone_volume_outer")) {
495 actuator->m_3d.cone_outer_gain = prop_value;
496 if(!handle3d.isNull())
497 handle3d->setConeVolumeOuter(prop_value);
500 return PY_SET_ATTR_FAIL;
503 return PY_SET_ATTR_SUCCESS;
506 int KX_SoundActuator::pyattr_set_audposition(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
508 KX_SoundActuator * actuator = static_cast<KX_SoundActuator *> (self);
510 float position = 1.0;
511 if (!PyArg_Parse(value, "f", &position))
512 return PY_SET_ATTR_FAIL;
514 if(!actuator->m_handle.isNull())
515 actuator->m_handle->seek(position);
516 return PY_SET_ATTR_SUCCESS;
519 int KX_SoundActuator::pyattr_set_gain(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
522 KX_SoundActuator * actuator = static_cast<KX_SoundActuator *> (self);
523 if (!PyArg_Parse(value, "f", &gain))
524 return PY_SET_ATTR_FAIL;
526 actuator->m_volume = gain;
527 if(!actuator->m_handle.isNull())
528 actuator->m_handle->setVolume(gain);
530 return PY_SET_ATTR_SUCCESS;
533 int KX_SoundActuator::pyattr_set_pitch(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
536 KX_SoundActuator * actuator = static_cast<KX_SoundActuator *> (self);
537 if (!PyArg_Parse(value, "f", &pitch))
538 return PY_SET_ATTR_FAIL;
540 actuator->m_pitch = pitch;
541 if(!actuator->m_handle.isNull())
542 actuator->m_handle->setPitch(pitch);
544 return PY_SET_ATTR_SUCCESS;
547 int KX_SoundActuator::pyattr_set_sound(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
549 PyObject* sound = NULL;
550 KX_SoundActuator * actuator = static_cast<KX_SoundActuator *> (self);
551 if (!PyArg_Parse(value, "O", &sound))
552 return PY_SET_ATTR_FAIL;
554 AUD_Reference<AUD_IFactory>* snd = reinterpret_cast<AUD_Reference<AUD_IFactory>*>(AUD_getPythonSound(sound));
557 actuator->m_sound = *snd;
559 return PY_SET_ATTR_SUCCESS;
562 return PY_SET_ATTR_FAIL;
565 #endif // WITH_PYTHON