00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "soundemittercomponent.h"
00022
00023 #include <core/debughelper.h>
00024 #include <audio/engine.h>
00025 #include <audio/sound.h>
00026 #include <engine/assets/audio/sound/soundasset.h>
00027 #include <engine/gameobject.h>
00028 #include <core/metainfo.h>
00029
00030 #include <QtCore/QDebug>
00031 #include <QMimeData>
00032
00033 REGISTER_OBJECTTYPE( GluonEngine, SoundEmitterComponent )
00034
00035 using namespace GluonEngine;
00036
00037 class SoundEmitterComponent::SoundEmitterComponentPrivate
00038 {
00039 public:
00040 SoundEmitterComponentPrivate()
00041 : asset( 0 )
00042 , sound( 0 )
00043 , buffer( 0 )
00044 , radius( 10000.0f )
00045 , volume( 1.0f )
00046 , pitch( 1.0f )
00047 , loop( false )
00048 , autoPlay( false )
00049 {}
00050
00051 Asset* asset;
00052 GluonAudio::Sound* sound;
00053 GluonAudio::Buffer* buffer;
00054 float radius;
00055 float volume;
00056 float pitch;
00057 bool loop;
00058 bool autoPlay;
00059 };
00060
00061 SoundEmitterComponent::SoundEmitterComponent( QObject* parent )
00062 : Component( parent )
00063 , d( new SoundEmitterComponentPrivate )
00064 {
00065 metaInfo()->setPropertyRange( "pitch", 0.5, 2.0 );
00066 }
00067
00068 SoundEmitterComponent::SoundEmitterComponent( const GluonEngine::SoundEmitterComponent& other )
00069 : Component( other )
00070 , d( other.d )
00071 {
00072 }
00073
00074 SoundEmitterComponent::~SoundEmitterComponent()
00075 {
00076 stop();
00077 delete d;
00078 }
00079
00080 QString SoundEmitterComponent::category() const
00081 {
00082 return QString( "Audio" );
00083 }
00084
00085 void SoundEmitterComponent::play()
00086 {
00087 d->sound->play();
00088 }
00089
00090 Asset* SoundEmitterComponent::sound()
00091 {
00092 return d->asset;
00093 }
00094
00095 void SoundEmitterComponent::setSound( Asset* asset )
00096 {
00097 d->asset = asset;
00098
00099 if( d->sound && asset->data()->hasFormat( "application/gluon-audio-buffer" ) )
00100 {
00101 d->sound->stop();
00102 d->buffer->setBuffer( asset->data()->data( "application/gluon-audio-buffer" ).toUInt(), true );
00103 d->sound->load( d->buffer );
00104 }
00105 }
00106
00107 void SoundEmitterComponent::initialize()
00108 {
00109 if( !d->sound )
00110 d->sound = new GluonAudio::Sound();
00111
00112 if( !d->buffer )
00113 d->buffer = new GluonAudio::Buffer();
00114 }
00115
00116 void SoundEmitterComponent::start()
00117 {
00118 if( d->asset )
00119 {
00120 if( !d->asset->isLoaded() ) d->asset->load();
00121
00122 if( d->asset->data()->hasFormat( "application/gluon-audio-buffer" ) )
00123 {
00124 d->buffer->setBuffer( d->asset->data()->data( "application/gluon-audio-buffer" ).toUInt(), true );
00125 }
00126 }
00127
00128 d->sound->load( d->buffer );
00129 d->sound->setPosition( gameObject()->position() );
00130 d->sound->setRadius( d->radius );
00131 d->sound->setVolume( d->volume );
00132 d->sound->setPitch( d->pitch );
00133 d->sound->setLoop( d->loop );
00134
00135 if( d->autoPlay )
00136 d->sound->play();
00137 }
00138
00139 void SoundEmitterComponent::draw( int timeLapse )
00140 {
00141 Q_UNUSED( timeLapse );
00142 if( d->sound )
00143 d->sound->setPosition( gameObject()->position() );
00144 }
00145
00146 void SoundEmitterComponent::stop()
00147 {
00148 if( d->sound )
00149 {
00150 d->sound->stop();
00151 }
00152 }
00153
00154 void SoundEmitterComponent::cleanup()
00155 {
00156 delete d->sound;
00157
00158 d->sound = 0;
00159 d->buffer = 0;
00160 }
00161
00162 float SoundEmitterComponent::radius() const
00163 {
00164 return d->radius;
00165 }
00166
00167 void SoundEmitterComponent::setRadius( float radius )
00168 {
00169 if( d->sound )
00170 d->sound->setRadius( radius );
00171 d->radius = radius;
00172 }
00173
00174 float SoundEmitterComponent::volume() const
00175 {
00176 return d->volume;
00177 }
00178
00179 void SoundEmitterComponent::setVolume( float volume )
00180 {
00181 d->volume = volume;
00182 if( d->sound )
00183 d->sound->setVolume( volume );
00184 }
00185
00186 float SoundEmitterComponent::pitch() const
00187 {
00188 return d->pitch;
00189 }
00190
00191 void SoundEmitterComponent::setPitch( float pitch )
00192 {
00193 d->pitch = pitch;
00194 if( d->sound )
00195 d->sound->setPitch( pitch );
00196 }
00197
00198 bool SoundEmitterComponent::isLooping() const
00199 {
00200 return d->loop;
00201 }
00202
00203 bool SoundEmitterComponent::isPlaying() const
00204 {
00205 if( d->sound )
00206 {
00207 return d->sound->isPlaying();
00208 }
00209 return false;
00210 }
00211
00212 void SoundEmitterComponent::setLoop( bool loop )
00213 {
00214 d->loop = loop;
00215
00216 if( d->sound )
00217 {
00218 d->sound->setLoop( loop );
00219 if( loop )
00220 {
00221 d->sound->play();
00222 }
00223 else if( d->sound->isPlaying() )
00224 {
00225 d->sound->stop();
00226 }
00227 }
00228 }
00229
00230 bool SoundEmitterComponent::autoPlay() const
00231 {
00232 return d->autoPlay;
00233 }
00234
00235 void SoundEmitterComponent::setAutoPlay( bool autoPlay )
00236 {
00237 d->autoPlay = autoPlay;
00238 }
00239
00240 Q_EXPORT_PLUGIN2( gluon_component_soundemitter, GluonEngine::SoundEmitterComponent )
00241
00242 #include "soundemittercomponent.moc"