Audio.cpp
Go to the documentation of this file.00001 //*** Audio.cpp *** 00002 00003 #include "Audio.h" 00004 #include "Debug.h" 00005 #include "Platform_Sound_SoundStream.h" 00006 #include "Platform_Sound.h" 00007 #include "Music.h" 00008 #include "Asset.h" 00009 #include "SoundInstance.h" 00010 #include "Sound.h" 00011 #include "ArrayIterator.h" 00012 #include "StandardLibrary.h" 00013 00014 00015 //*** Constructor *** 00016 00017 Audio::Audio(): 00018 streamBuffer_(0), 00019 streamBufferSize_(0), 00020 currentSoundInstanceHandle_(0) 00021 { 00022 } 00023 00024 00025 //*** Destructor *** 00026 00027 Audio::~Audio() 00028 { 00029 for (int i=0; i<freeSoundStreams_.GetItemCount(); i++) 00030 { 00031 delete freeSoundStreams_.Get(i); 00032 } 00033 00034 for (int i=0; i<assignedSoundStreams_.GetItemCount(); i++) 00035 { 00036 delete assignedSoundStreams_.Get(i).stream; 00037 } 00038 00039 if (streamBuffer_) 00040 { 00041 Free(streamBuffer_); 00042 streamBuffer_=0; 00043 streamBufferSize_=0; 00044 } 00045 } 00046 00047 00048 //*** Update *** 00049 00050 void Audio::Update() 00051 { 00052 // Update all music 00053 for (int i=0; i<music_.GetItemCount(); i++) 00054 { 00055 Music* music=music_.Get(i); 00056 music->Update(); 00057 } 00058 00059 for (int i=0; i<soundInstances_.GetItemCount(); i++) 00060 { 00061 SoundInstance* instance=soundInstances_.Get(i).instance; 00062 instance->Update(); 00063 } 00064 } 00065 00066 00067 //*** Register *** 00068 00069 void Audio::Register(Music* music) 00070 { 00071 music_.Add(music); 00072 } 00073 00074 00075 //*** Unregister *** 00076 00077 void Audio::Unregister(Music* music) 00078 { 00079 ArrayIterator<Music*> it(music_); 00080 if (it.Find(music)) 00081 { 00082 music_.Remove(it); 00083 } 00084 } 00085 00086 00087 //*** PlaySound *** 00088 00089 unsigned int Audio::PlaySound(const Sound& sound, float priority, bool looping) 00090 { 00091 SoundInstanceEntry entry; 00092 entry.instance=new SoundInstance(sound,priority,looping); 00093 entry.handle=currentSoundInstanceHandle_; 00094 currentSoundInstanceHandle_++; 00095 soundInstances_.Add(entry); 00096 entry.instance->Play(); 00097 return entry.handle; 00098 } 00099 00100 00101 //*** GetSoundInstance *** 00102 00103 SoundInstance* Audio::GetSoundInstance(unsigned int handle) 00104 { 00105 for (int i=0; i<soundInstances_.GetItemCount(); i++) 00106 { 00107 if (soundInstances_.Get(i).handle==handle) 00108 { 00109 return soundInstances_.Get(i).instance; 00110 } 00111 } 00112 00113 return 0; 00114 } 00115 00116 00117 //*** GetStreamBuffer *** 00118 00119 void* Audio::GetStreamBuffer(int minSize) 00120 { 00121 if (!streamBuffer_ || streamBufferSize_<minSize) 00122 { 00123 if (streamBuffer_) 00124 { 00125 Free(streamBuffer_); 00126 streamBuffer_=0; 00127 } 00128 streamBufferSize_=minSize; 00129 streamBuffer_=Malloc(streamBufferSize_); 00130 } 00131 00132 return streamBuffer_; 00133 } 00134 00135 00136 //*** AcquireSoundStream *** 00137 00138 Platform_Sound_SoundStream* Audio::AcquireSoundStream(SoundInstance* instance) 00139 { 00140 const Sound* definition=instance->GetSound(); 00141 int channels=definition->GetChannels(); 00142 int frequency=definition->GetFrequency(); 00143 int bitsPerSample=definition->GetBitsPerSample(); 00144 00145 // If there's a free stream, just use that 00146 if (freeSoundStreams_.GetItemCount()>0) 00147 { 00148 Platform_Sound_SoundStream* stream=freeSoundStreams_.Get(freeSoundStreams_.GetItemCount()-1); 00149 freeSoundStreams_.RemoveLast(); 00150 00151 if (stream->GetChannels()!=channels || stream->GetFrequency()!=frequency || stream->GetBitsPerSample()!=bitsPerSample) 00152 { 00153 delete stream; 00154 stream=Platform::GetPlatform_Sound()->CreateSoundStream(channels,frequency,bitsPerSample,Sound_BufferSize); 00155 } 00156 00157 SoundStreamEntry entry; 00158 entry.instanceAssignedTo=instance; 00159 entry.stream=stream; 00160 assignedSoundStreams_.Add(entry); 00161 return stream; 00162 } 00163 00164 // If we haven't reached the maximum yet, create a new one and use it 00165 if (assignedSoundStreams_.GetItemCount()<MaxSimultaneousSounds) 00166 { 00167 Platform_Sound_SoundStream* stream=Platform::GetPlatform_Sound()->CreateSoundStream(channels,frequency,bitsPerSample,Sound_BufferSize); 00168 00169 SoundStreamEntry entry; 00170 entry.instanceAssignedTo=instance; 00171 entry.stream=stream; 00172 assignedSoundStreams_.Add(entry); 00173 return stream; 00174 } 00175 00176 // Kill off a sound with lower or equal priority 00177 for (int i=0; i<assignedSoundStreams_.GetItemCount(); i++) 00178 { 00179 if (assignedSoundStreams_.Get(i).instanceAssignedTo->GetPriority()<=instance->GetPriority()) 00180 { 00181 // Stop the sound, and release the sound stream it was using 00182 instance->Stop(); 00183 00184 // And return that stream 00185 Platform_Sound_SoundStream* stream=freeSoundStreams_.Get(freeSoundStreams_.GetItemCount()-1); 00186 freeSoundStreams_.RemoveLast(); 00187 00188 if (stream->GetChannels()!=channels || stream->GetFrequency()!=frequency || stream->GetBitsPerSample()!=bitsPerSample) 00189 { 00190 delete stream; 00191 stream=Platform::GetPlatform_Sound()->CreateSoundStream(channels,frequency,bitsPerSample,Sound_BufferSize); 00192 } 00193 00194 SoundStreamEntry entry; 00195 entry.instanceAssignedTo=instance; 00196 entry.stream=stream; 00197 assignedSoundStreams_.Add(entry); 00198 return stream; 00199 } 00200 } 00201 00202 // No stream available - that's fine too, just means the sound won't play 00203 return 0; 00204 } 00205 00206 00207 //*** ReleaseSoundStream *** 00208 00209 void Audio::ReleaseSoundStream(SoundInstance* instance) 00210 { 00211 for (int i=0; i<assignedSoundStreams_.GetItemCount(); i++) 00212 { 00213 if (assignedSoundStreams_.Get(i).instanceAssignedTo==instance) 00214 { 00215 Platform_Sound_SoundStream* stream=assignedSoundStreams_.Get(i).stream; 00216 stream->Stop(); 00217 assignedSoundStreams_.Remove(i); 00218 freeSoundStreams_.Add(stream); 00219 return; 00220 } 00221 } 00222 }
Reproduction/republishing of any material on this site without permission is strictly prohibited.
