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:
111 m_handle = AUD_getDevice()->play(sound, 0);
113 AUD_Reference<AUD_I3DHandle> handle3d = AUD_Reference<AUD_I3DHandle>(m_handle);
115 if(m_is3d && !handle3d.isNull())
117 handle3d->setRelative(true);
118 handle3d->setVolumeMaximum(m_3d.max_gain);
119 handle3d->setVolumeMinimum(m_3d.min_gain);
120 handle3d->setDistanceReference(m_3d.reference_distance);
121 handle3d->setDistanceMaximum(m_3d.max_distance);
122 handle3d->setAttenuation(m_3d.rolloff_factor);
123 handle3d->setConeAngleInner(m_3d.cone_inner_angle);
124 handle3d->setConeAngleOuter(m_3d.cone_outer_angle);
125 handle3d->setConeVolumeOuter(m_3d.cone_outer_gain);
129 m_handle->setLoopCount(-1);
130 m_handle->setPitch(m_pitch);
131 m_handle->setVolume(m_volume);
135 CValue* KX_SoundActuator::GetReplica()
137 KX_SoundActuator* replica = new KX_SoundActuator(*this);
138 replica->ProcessReplica();
142 void KX_SoundActuator::ProcessReplica()
144 SCA_IActuator::ProcessReplica();
145 m_handle = AUD_Reference<AUD_IHandle>();
148 bool KX_SoundActuator::Update(double curtime, bool frame)
154 // do nothing on negative events, otherwise sounds are played twice!
155 bool bNegativeEvent = IsNegativeEvent();
156 bool bPositiveEvent = m_posevent;
163 // actual audio device playing state
164 bool isplaying = m_handle.isNull() ? false : (m_handle->getStatus() == AUD_STATUS_PLAYING);
168 // here must be a check if it is still playing
169 if (m_isplaying && isplaying)
173 case KX_SOUNDACT_PLAYSTOP:
174 case KX_SOUNDACT_LOOPSTOP:
175 case KX_SOUNDACT_LOOPBIDIRECTIONAL_STOP:
178 if(!m_handle.isNull())
180 m_handle = AUD_Reference<AUD_IHandle>();
183 case KX_SOUNDACT_PLAYEND:
185 // do nothing, sound will stop anyway when it's finished
188 case KX_SOUNDACT_LOOPEND:
189 case KX_SOUNDACT_LOOPBIDIRECTIONAL:
191 // stop the looping so that the sound stops when it finished
192 if(!m_handle.isNull())
193 m_handle->setLoopCount(0);
201 // remember that we tried to stop the actuator
206 // Warning: when de-activating the actuator, after a single negative event this runs again with...
207 // m_posevent==false && m_posevent==false, in this case IsNegativeEvent() returns false
208 // and assumes this is a positive event.
209 // check that we actually have a positive event so as not to play sounds when being disabled.
210 else if(bPositiveEvent) { // <- added since 2.49
212 else { // <- works in most cases except a loop-end sound will never stop unless
213 // the negative pulse is done continuesly
218 // verify that the sound is still playing
219 isplaying = m_handle.isNull() ? false : (m_handle->getStatus() == AUD_STATUS_PLAYING);
223 AUD_Reference<AUD_I3DHandle> handle3d = AUD_Reference<AUD_I3DHandle>(m_handle);
225 if(m_is3d && !handle3d.isNull())
227 KX_Camera* cam = KX_GetActiveScene()->GetActiveCamera();
230 KX_GameObject* obj = (KX_GameObject*)this->GetParent();
236 Mo = cam->NodeGetWorldOrientation().inverse();
237 p = (obj->NodeGetWorldPosition() - cam->NodeGetWorldPosition());
240 handle3d->setSourceLocation(v);
241 p = (obj->GetLinearVelocity() - cam->GetLinearVelocity());
244 handle3d->setSourceVelocity(v);
245 (Mo * obj->NodeGetWorldOrientation()).getRotation().getValue(q);
246 handle3d->setSourceOrientation(AUD_Quaternion(q[3], q[0], q[1], q[2]));
261 /* ------------------------------------------------------------------------- */
262 /* Python functions */
263 /* ------------------------------------------------------------------------- */
267 /* Integration hooks ------------------------------------------------------- */
268 PyTypeObject KX_SoundActuator::Type = {
269 PyVarObject_HEAD_INIT(NULL, 0)
271 sizeof(PyObjectPlus_Proxy),
280 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
285 &SCA_IActuator::Type,
290 PyMethodDef KX_SoundActuator::Methods[] = {
291 KX_PYMETHODTABLE_NOARGS(KX_SoundActuator, startSound),
292 KX_PYMETHODTABLE_NOARGS(KX_SoundActuator, pauseSound),
293 KX_PYMETHODTABLE_NOARGS(KX_SoundActuator, stopSound),
294 {NULL, NULL} //Sentinel
297 PyAttributeDef KX_SoundActuator::Attributes[] = {
298 KX_PYATTRIBUTE_BOOL_RO("is3D", KX_SoundActuator, m_is3d),
299 KX_PYATTRIBUTE_RW_FUNCTION("volume_maximum", KX_SoundActuator, pyattr_get_3d_property, pyattr_set_3d_property),
300 KX_PYATTRIBUTE_RW_FUNCTION("volume_minimum", KX_SoundActuator, pyattr_get_3d_property, pyattr_set_3d_property),
301 KX_PYATTRIBUTE_RW_FUNCTION("distance_reference", KX_SoundActuator, pyattr_get_3d_property, pyattr_set_3d_property),
302 KX_PYATTRIBUTE_RW_FUNCTION("distance_maximum", KX_SoundActuator, pyattr_get_3d_property, pyattr_set_3d_property),
303 KX_PYATTRIBUTE_RW_FUNCTION("attenuation", KX_SoundActuator, pyattr_get_3d_property, pyattr_set_3d_property),
304 KX_PYATTRIBUTE_RW_FUNCTION("cone_angle_inner", KX_SoundActuator, pyattr_get_3d_property, pyattr_set_3d_property),
305 KX_PYATTRIBUTE_RW_FUNCTION("cone_angle_outer", KX_SoundActuator, pyattr_get_3d_property, pyattr_set_3d_property),
306 KX_PYATTRIBUTE_RW_FUNCTION("cone_volume_outer", KX_SoundActuator, pyattr_get_3d_property, pyattr_set_3d_property),
307 KX_PYATTRIBUTE_RW_FUNCTION("sound", KX_SoundActuator, pyattr_get_sound, pyattr_set_sound),
309 KX_PYATTRIBUTE_RW_FUNCTION("time", KX_SoundActuator, pyattr_get_audposition, pyattr_set_audposition),
310 KX_PYATTRIBUTE_RW_FUNCTION("volume", KX_SoundActuator, pyattr_get_gain, pyattr_set_gain),
311 KX_PYATTRIBUTE_RW_FUNCTION("pitch", KX_SoundActuator, pyattr_get_pitch, pyattr_set_pitch),
312 KX_PYATTRIBUTE_ENUM_RW("mode",KX_SoundActuator::KX_SOUNDACT_NODEF+1,KX_SoundActuator::KX_SOUNDACT_MAX-1,false,KX_SoundActuator,m_type),
316 /* Methods ----------------------------------------------------------------- */
317 KX_PYMETHODDEF_DOC_NOARGS(KX_SoundActuator, startSound,
319 "\tStarts the sound.\n")
321 if(!m_handle.isNull())
323 switch(m_handle->getStatus())
325 case AUD_STATUS_PLAYING:
327 case AUD_STATUS_PAUSED:
337 KX_PYMETHODDEF_DOC_NOARGS(KX_SoundActuator, pauseSound,
339 "\tPauses the sound.\n")
341 if(!m_handle.isNull())
346 KX_PYMETHODDEF_DOC_NOARGS(KX_SoundActuator, stopSound,
348 "\tStops the sound.\n")
350 if(!m_handle.isNull())
352 m_handle = AUD_Reference<AUD_IHandle>();
356 /* Atribute setting and getting -------------------------------------------- */
357 PyObject* KX_SoundActuator::pyattr_get_3d_property(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
359 KX_SoundActuator * actuator = static_cast<KX_SoundActuator *> (self);
360 const char* prop = attrdef->m_name;
361 float result_value = 0.0;
363 if(!strcmp(prop, "volume_maximum")) {
364 result_value = actuator->m_3d.max_gain;
366 } else if (!strcmp(prop, "volume_minimum")) {
367 result_value = actuator->m_3d.min_gain;
369 } else if (!strcmp(prop, "distance_reference")) {
370 result_value = actuator->m_3d.reference_distance;
372 } else if (!strcmp(prop, "distance_maximum")) {
373 result_value = actuator->m_3d.max_distance;
375 } else if (!strcmp(prop, "attenuation")) {
376 result_value = actuator->m_3d.rolloff_factor;
378 } else if (!strcmp(prop, "cone_angle_inner")) {
379 result_value = actuator->m_3d.cone_inner_angle;
381 } else if (!strcmp(prop, "cone_angle_outer")) {
382 result_value = actuator->m_3d.cone_outer_angle;
384 } else if (!strcmp(prop, "cone_volume_outer")) {
385 result_value = actuator->m_3d.cone_outer_gain;
391 PyObject* result = PyFloat_FromDouble(result_value);
395 PyObject* KX_SoundActuator::pyattr_get_audposition(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
397 KX_SoundActuator * actuator = static_cast<KX_SoundActuator *> (self);
398 float position = 0.0;
400 if(!actuator->m_handle.isNull())
401 position = actuator->m_handle->getPosition();
403 PyObject* result = PyFloat_FromDouble(position);
408 PyObject* KX_SoundActuator::pyattr_get_gain(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
410 KX_SoundActuator * actuator = static_cast<KX_SoundActuator *> (self);
411 float gain = actuator->m_volume;
413 PyObject* result = PyFloat_FromDouble(gain);
418 PyObject* KX_SoundActuator::pyattr_get_pitch(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
420 KX_SoundActuator * actuator = static_cast<KX_SoundActuator *> (self);
421 float pitch = actuator->m_pitch;
423 PyObject* result = PyFloat_FromDouble(pitch);
428 PyObject* KX_SoundActuator::pyattr_get_sound(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
430 KX_SoundActuator * actuator = static_cast<KX_SoundActuator *> (self);
431 if(!actuator->m_sound.isNull())
432 return AUD_getPythonFactory(&actuator->m_sound);
437 int KX_SoundActuator::pyattr_set_3d_property(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
439 KX_SoundActuator * actuator = static_cast<KX_SoundActuator *> (self);
440 const char* prop = attrdef->m_name;
441 float prop_value = 0.0;
443 if (!PyArg_Parse(value, "f", &prop_value))
444 return PY_SET_ATTR_FAIL;
446 AUD_Reference<AUD_I3DHandle> handle3d = AUD_Reference<AUD_I3DHandle>(actuator->m_handle);
447 // if sound is working and 3D, set the new setting
448 if(!actuator->m_is3d)
449 return PY_SET_ATTR_FAIL;
451 if(!strcmp(prop, "volume_maximum")) {
452 actuator->m_3d.max_gain = prop_value;
453 if(!handle3d.isNull())
454 handle3d->setVolumeMaximum(prop_value);
456 } else if (!strcmp(prop, "volume_minimum")) {
457 actuator->m_3d.min_gain = prop_value;
458 if(!handle3d.isNull())
459 handle3d->setVolumeMinimum(prop_value);
461 } else if (!strcmp(prop, "distance_reference")) {
462 actuator->m_3d.reference_distance = prop_value;
463 if(!handle3d.isNull())
464 handle3d->setDistanceReference(prop_value);
466 } else if (!strcmp(prop, "distance_maximum")) {
467 actuator->m_3d.max_distance = prop_value;
468 if(!handle3d.isNull())
469 handle3d->setDistanceMaximum(prop_value);
471 } else if (!strcmp(prop, "attenuation")) {
472 actuator->m_3d.rolloff_factor = prop_value;
473 if(!handle3d.isNull())
474 handle3d->setAttenuation(prop_value);
476 } else if (!!strcmp(prop, "cone_angle_inner")) {
477 actuator->m_3d.cone_inner_angle = prop_value;
478 if(!handle3d.isNull())
479 handle3d->setConeAngleInner(prop_value);
481 } else if (!strcmp(prop, "cone_angle_outer")) {
482 actuator->m_3d.cone_outer_angle = prop_value;
483 if(!handle3d.isNull())
484 handle3d->setConeAngleOuter(prop_value);
486 } else if (!strcmp(prop, "cone_volume_outer")) {
487 actuator->m_3d.cone_outer_gain = prop_value;
488 if(!handle3d.isNull())
489 handle3d->setConeVolumeOuter(prop_value);
492 return PY_SET_ATTR_FAIL;
495 return PY_SET_ATTR_SUCCESS;
498 int KX_SoundActuator::pyattr_set_audposition(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
500 KX_SoundActuator * actuator = static_cast<KX_SoundActuator *> (self);
502 float position = 1.0;
503 if (!PyArg_Parse(value, "f", &position))
504 return PY_SET_ATTR_FAIL;
506 if(!actuator->m_handle.isNull())
507 actuator->m_handle->seek(position);
508 return PY_SET_ATTR_SUCCESS;
511 int KX_SoundActuator::pyattr_set_gain(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
514 KX_SoundActuator * actuator = static_cast<KX_SoundActuator *> (self);
515 if (!PyArg_Parse(value, "f", &gain))
516 return PY_SET_ATTR_FAIL;
518 actuator->m_volume = gain;
519 if(!actuator->m_handle.isNull())
520 actuator->m_handle->setVolume(gain);
522 return PY_SET_ATTR_SUCCESS;
525 int KX_SoundActuator::pyattr_set_pitch(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
528 KX_SoundActuator * actuator = static_cast<KX_SoundActuator *> (self);
529 if (!PyArg_Parse(value, "f", &pitch))
530 return PY_SET_ATTR_FAIL;
532 actuator->m_pitch = pitch;
533 if(!actuator->m_handle.isNull())
534 actuator->m_handle->setPitch(pitch);
536 return PY_SET_ATTR_SUCCESS;
539 int KX_SoundActuator::pyattr_set_sound(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
541 PyObject* sound = NULL;
542 KX_SoundActuator * actuator = static_cast<KX_SoundActuator *> (self);
543 if (!PyArg_Parse(value, "O", &sound))
544 return PY_SET_ATTR_FAIL;
546 AUD_Reference<AUD_IFactory>* snd = reinterpret_cast<AUD_Reference<AUD_IFactory>*>(AUD_getPythonSound(sound));
549 actuator->m_sound = *snd;
551 return PY_SET_ATTR_SUCCESS;
554 return PY_SET_ATTR_FAIL;
557 #endif // WITH_PYTHON