sound_effect.cpp

Go to the documentation of this file.
00001 #include <vector>
00002 #include "internal.h"
00003 
00004 
00005 namespace audiere {
00006 
00007   class SingleSoundEffect : public RefImplementation<SoundEffect> {
00008   public:
00009     SingleSoundEffect(OutputStream* os) {
00010       m_stream = os;
00011 
00012       m_volume = 1;
00013       m_pan    = 0;
00014       m_shift  = 1;
00015     }
00016 
00017     void ADR_CALL play() {
00018       m_stream->reset();
00019       m_stream->setVolume(m_volume);
00020       m_stream->setPan(m_pan);
00021       m_stream->setPitchShift(m_shift);
00022       m_stream->play();
00023     }
00024 
00025     void ADR_CALL stop() {
00026       m_stream->stop();
00027       m_stream->reset();
00028     }
00029 
00030     void ADR_CALL setVolume(float volume) {
00031       m_volume = volume;
00032     }
00033 
00034     float ADR_CALL getVolume() {
00035       return m_volume;
00036     }
00037 
00038     void ADR_CALL setPan(float pan) {
00039       m_pan = pan;
00040     }
00041 
00042     float ADR_CALL getPan() {
00043       return m_pan;
00044     }
00045 
00046     void ADR_CALL setPitchShift(float shift) {
00047       m_shift = shift;
00048     }
00049 
00050     float ADR_CALL getPitchShift() {
00051       return m_shift;
00052     }
00053 
00054   private:
00055     OutputStreamPtr m_stream;
00056 
00057     float m_volume;
00058     float m_pan;
00059     float m_shift;
00060   };
00061 
00062 
00063   class MultipleSoundEffect : public RefImplementation<SoundEffect> {
00064   public:
00065     MultipleSoundEffect(AudioDevice* device, SampleBuffer* sb) {
00066       m_device = device;
00067       m_buffer = sb;
00068 
00069       m_volume = 1;
00070       m_pan = 0;
00071       m_shift = 1;
00072     }
00073 
00074     void ADR_CALL play() {
00075       // go through the list and see if any streams are done playing
00076       // so we can reuse them
00077       for (unsigned i = 0; i < m_streams.size(); ++i) {
00078         if (!m_streams[i]->isPlaying()) {
00079           m_streams[i]->reset();
00080           m_streams[i]->setVolume(m_volume);
00081           m_streams[i]->setPan(m_pan);
00082           m_streams[i]->setPitchShift(m_shift);
00083           m_streams[i]->play();
00084           return;
00085         }
00086       }
00087 
00088       // open a new stream and play it
00089       OutputStream* stream = m_device->openStream(m_buffer->openStream());
00090       if (!stream) {
00091         return;
00092       }
00093       stream->setVolume(m_volume);
00094       stream->setPan(m_pan);
00095       stream->setPitchShift(m_shift);
00096       stream->play();
00097 
00098       m_streams.push_back(stream);
00099     }
00100 
00101     void ADR_CALL stop() {
00102       m_streams.clear();
00103     }
00104 
00105     void ADR_CALL setVolume(float volume) {
00106       m_volume = volume;
00107     }
00108 
00109     float ADR_CALL getVolume() {
00110       return m_volume;
00111     }
00112 
00113     void ADR_CALL setPan(float pan) {
00114       m_pan = pan;
00115     }
00116 
00117     float ADR_CALL getPan() {
00118       return m_pan;
00119     }
00120 
00121     void ADR_CALL setPitchShift(float shift) {
00122       m_shift = shift;
00123     }
00124 
00125     float ADR_CALL getPitchShift() {
00126       return m_shift;
00127     }
00128     
00129   private:
00130     AudioDevicePtr m_device;
00131     SampleBufferPtr m_buffer;
00132     std::vector<OutputStreamPtr> m_streams;
00133 
00134     float m_volume;
00135     float m_pan;
00136     float m_shift;
00137   };
00138 
00139 
00140   ADR_EXPORT(SoundEffect*) AdrOpenSoundEffect(
00141     AudioDevice* device,
00142     SampleSource* source,
00143     SoundEffectType type)
00144   {
00145     if (!device || !source) {
00146       return 0;
00147     }
00148 
00149     switch (type) {
00150       case SINGLE: {
00151         OutputStream* os = OpenSound(device, source, false);
00152         return (os ? new SingleSoundEffect(os) : 0);
00153       }
00154         
00155       case MULTIPLE: {
00156         SampleBuffer* sb = CreateSampleBuffer(source);
00157         return (sb ? new MultipleSoundEffect(device, sb) : 0);
00158       }
00159 
00160       default:
00161         return 0;
00162     }
00163   }
00164 
00165 }

Generated on Mon Feb 13 23:07:47 2006 for audiere by  doxygen 1.4.6