FIFE  2008.0
soundemitter.h
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 #ifndef FIFE_SOUNDEMITTER_H_
00023 #define FIFE_SOUNDEMITTER_H_
00024 
00025 // Standard C++ library includes
00026 
00027 // Platform specific includes
00028 
00029 // 3rd party library includes
00030 #include <boost/function.hpp>
00031 
00032 // FIFE includes
00033 // These includes are split up in two parts, separated by one empty line
00034 // First block: files included from the FIFE root src directory
00035 // Second block: files included from the same folder
00036 #include "vfs/raw/rawdata.h"
00037 #include "util/time/timeevent.h"
00038 
00039 #include "soundclip.h"
00040 
00041 namespace FIFE {
00042 
00043     class SoundManager;
00044     class SoundClipPool;
00045 
00048     class SoundEmitter : private TimeEvent {
00049     public:
00050         typedef boost::function0<void> type_callback;
00051 
00052         SoundEmitter(SoundManager* manager, SoundClipPool* pool, unsigned int uid);
00053         ~SoundEmitter();
00054 
00057         unsigned int getId() const{
00058             return m_emitterid;
00059         }
00060 
00067         void setPositioning(bool relative) {
00068             alSourcei(m_source, AL_SOURCE_RELATIVE, relative ? AL_TRUE : AL_FALSE);
00069         }
00070 
00075         void setRolloff(float rolloff) {
00076             alSourcef (m_source, AL_ROLLOFF_FACTOR,  rolloff);
00077         }
00078 
00082         void setSoundClip(unsigned int sound_id);
00083 
00089         void setCallback(const type_callback& cb);
00090 
00095         void reset(bool defaultall = false);
00096 
00099         void release();
00100 
00103         void setLooping(bool loop);
00104 
00107         void play();
00108 
00111         void stop();
00112 
00115         void pause() {
00116             if (m_soundclip) {
00117                 alSourcePause(m_source);
00118             }
00119         }
00120 
00125         void setGain(float gain) {
00126             alSourcef(m_source, AL_GAIN, gain);
00127         }
00128 
00133         float getGain() {
00134             float tmp;
00135             alGetSourcef(m_source, AL_GAIN, &tmp);
00136             return tmp;
00137         }
00138 
00143         bool isStereo() const{
00144             if (m_soundclip) {
00145                 return m_soundclip->getDecoder()->isStereo();
00146             }
00147             return false;
00148         }
00149 
00152         short getBitResolution() const {
00153             if (m_soundclip) {
00154                 return m_soundclip->getDecoder()->getBitResolution();
00155             }
00156             return 0;
00157         }
00158 
00161         unsigned long getSampleRate() const{
00162             if (m_soundclip) {
00163                 return m_soundclip->getDecoder()->getSampleRate();
00164             }
00165             return 0;
00166         }
00167 
00170         unsigned long getDecodedLength() const{
00171             if (m_soundclip) {
00172                 return m_soundclip->getDecoder()->getDecodedLength();
00173 
00174             }
00175             return 0;
00176         }
00177 
00180         unsigned long getDuration() const{
00181             if (m_soundclip) {
00182                 double samplerate = static_cast<double>(getSampleRate()) / 1000.0;  //convert to milliseconds
00183                 double bitres = static_cast<double>(getBitResolution());
00184                 double size = static_cast<double>(getDecodedLength()) * 8.0;  //convert to bits
00185                 double stereo = (isStereo() ? 2.0 : 1.0);
00186                 double time = ( size / (samplerate * bitres) ) / stereo;
00187 
00188                 return static_cast<unsigned long>(time);
00189             }
00190             return 0;
00191          }
00192 
00195         void setCursor(SoundPositionType type, float value);
00196 
00199         float getCursor(SoundPositionType type);
00200 
00203         void setPosition(float x, float y, float z) {
00204             alSource3f(m_source, AL_POSITION, x, y, z);
00205         }
00206 
00209         void setVelocity(float x, float y, float z) {
00210             alSource3f(m_source, AL_VELOCITY, x, y, z);
00211         }
00212 
00213     private:
00216         virtual void updateEvent(unsigned long time);
00217 
00220         void attachSoundClip();
00221 
00222         SoundManager*   m_manager;
00223         SoundClipPool*  m_pool;
00224         ALuint          m_source;           // The openAL-source
00225         SoundClip*      m_soundclip;    // the attached soundclip
00226         unsigned int    m_soundclipid;// id of the attached soundclip
00227         unsigned int    m_streamid;     // the id of the stream
00228         unsigned int    m_emitterid;    // the emitter-id
00229         bool            m_loop;             // loop?
00230         type_callback   m_callback;
00231     };
00232 }
00233 
00234 #endif