soundemitter.h

00001 /***************************************************************************
00002  *   Copyright (C) 2005-2011 by the FIFE team                              *
00003  *   http://www.fifengine.net                                              *
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 "util/time/timeevent.h"
00037 
00038 #include "soundclip.h"
00039 
00040 namespace FIFE {
00041 
00042     class SoundManager;
00043 
00046     class SoundEmitter : private TimeEvent {
00047     public:
00048         typedef boost::function0<void> type_callback;
00049 
00050         SoundEmitter(SoundManager* manager, uint32_t uid);
00051         ~SoundEmitter();
00052 
00055         uint32_t getId() const{
00056             return m_emitterid;
00057         }
00058 
00065         void setPositioning(bool relative) {
00066             alSourcei(m_source, AL_SOURCE_RELATIVE, relative ? AL_TRUE : AL_FALSE);
00067         }
00068 
00073         void setRolloff(float rolloff) {
00074             alSourcef (m_source, AL_ROLLOFF_FACTOR,  rolloff);
00075         }
00076 
00080         void setSoundClip(SoundClipPtr soundclip);
00081 
00085         SoundClipPtr getSoundClip() { return m_soundclip; };
00086 
00092         void setCallback(const type_callback& cb);
00093 
00098         void reset(bool defaultall = false);
00099 
00102         void release();
00103 
00106         void setLooping(bool loop);
00107 
00110         void play();
00111 
00114         void stop();
00115 
00118         void pause() {
00119             if (m_soundclip) {
00120                 alSourcePause(m_source);
00121             }
00122         }
00123 
00128         void setGain(float gain) {
00129             alSourcef(m_source, AL_GAIN, gain);
00130         }
00131 
00136         float getGain() {
00137             float tmp;
00138             alGetSourcef(m_source, AL_GAIN, &tmp);
00139             return tmp;
00140         }
00141 
00146         bool isStereo() {
00147             if (m_soundclip) {
00148                 return m_soundclip->getDecoder()->isStereo();
00149             }
00150             return false;
00151         }
00152 
00155         int16_t getBitResolution() {
00156             if (m_soundclip) {
00157                 return m_soundclip->getDecoder()->getBitResolution();
00158             }
00159             return 0;
00160         }
00161 
00164         uint64_t getSampleRate() {
00165             if (m_soundclip) {
00166                 return m_soundclip->getDecoder()->getSampleRate();
00167             }
00168             return 0;
00169         }
00170 
00173         uint64_t getDecodedLength() {
00174             if (m_soundclip) {
00175                 return m_soundclip->getDecoder()->getDecodedLength();
00176 
00177             }
00178             return 0;
00179         }
00180 
00183         uint64_t getDuration() {
00184             if (m_soundclip) {
00185                 double samplerate = static_cast<double>(getSampleRate()) / 1000.0;  //convert to milliseconds
00186                 double bitres = static_cast<double>(getBitResolution());
00187                 double size = static_cast<double>(getDecodedLength()) * 8.0;  //convert to bits
00188                 double stereo = (isStereo() ? 2.0 : 1.0);
00189                 double time = ( size / (samplerate * bitres) ) / stereo;
00190 
00191                 return static_cast<uint64_t>(time);
00192             }
00193             return 0;
00194          }
00195 
00198         void setCursor(SoundPositionType type, float value);
00199 
00202         float getCursor(SoundPositionType type);
00203 
00206         void setPosition(float x, float y, float z) {
00207             alSource3f(m_source, AL_POSITION, x, y, z);
00208         }
00209 
00212         void setVelocity(float x, float y, float z) {
00213             alSource3f(m_source, AL_VELOCITY, x, y, z);
00214         }
00215 
00216     private:
00219         virtual void updateEvent(uint32_t time);
00220 
00223         void attachSoundClip();
00224 
00225         SoundManager*   m_manager;
00226         ALuint          m_source;           // The openAL-source
00227         SoundClipPtr    m_soundclip;    // the attached soundclip
00228         uint32_t    m_soundclipid;// id of the attached soundclip
00229         uint32_t    m_streamid;     // the id of the stream
00230         uint32_t    m_emitterid;    // the emitter-id
00231         bool            m_loop;             // loop?
00232         type_callback   m_callback;
00233     };
00234 }
00235 
00236 #endif