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 00013 void ADR_CALL play() { 00014 if (m_stream->isPlaying()) { 00015 m_stream->reset(); 00016 m_stream->play(); 00017 } else { 00018 m_stream->reset(); 00019 m_stream->play(); 00020 } 00021 } 00022 00023 void ADR_CALL stop() { 00024 m_stream->stop(); 00025 m_stream->reset(); 00026 } 00027 00028 private: 00029 RefPtr<OutputStream> m_stream; 00030 }; 00031 00032 00033 class MultipleSoundEffect : public RefImplementation<SoundEffect> { 00034 public: 00035 MultipleSoundEffect(AudioDevice* device, SampleBuffer* sb) { 00036 m_device = device; 00037 m_buffer = sb; 00038 } 00039 00040 void ADR_CALL play() { 00041 // open the stream and play it 00042 OutputStream* stream = m_device->openStream(m_buffer->openStream()); 00043 if (!stream) { 00044 return; 00045 } 00046 stream->play(); 00047 00048 // go through the list and see if any streams are done playing 00049 // so we can reuse them 00050 for (unsigned i = 0; i < m_streams.size(); ++i) { 00051 if (!m_streams[i]->isPlaying()) { 00052 m_streams[i] = stream; 00053 return; 00054 } 00055 } 00056 00057 m_streams.push_back(stream); 00058 } 00059 00060 void ADR_CALL stop() { 00061 m_streams.clear(); 00062 } 00063 00064 private: 00065 RefPtr<AudioDevice> m_device; 00066 RefPtr<SampleBuffer> m_buffer; 00067 std::vector<RefPtr<OutputStream> > m_streams; 00068 }; 00069 00070 00071 ADR_EXPORT(SoundEffect*, AdrOpenSoundEffect)( 00072 AudioDevice* device, 00073 SampleSource* source, 00074 SoundEffectType type) 00075 { 00076 if (!device || !source) { 00077 return 0; 00078 } 00079 00080 switch (type) { 00081 case SINGLE: { 00082 OutputStream* os = OpenSound(device, source, true); 00083 return (os ? new SingleSoundEffect(os) : 0); 00084 } 00085 00086 case MULTIPLE: { 00087 SampleBuffer* sb = CreateSampleBuffer(source); 00088 return (sb ? new MultipleSoundEffect(device, sb) : 0); 00089 } 00090 00091 default: 00092 return 0; 00093 } 00094 } 00095 00096 }
1.2.17