Bunch of gcc 4.0 warning fixes.
[blender.git] / intern / SoundSystem / intern / SND_AudioDevice.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version. The Blender
9  * Foundation also sells licenses for use in proprietary software under
10  * the Blender License.  See http://www.blender.org/BL/ for information
11  * about this.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL/BL DUAL LICENSE BLOCK *****
30  */
31
32 #include "SND_AudioDevice.h"
33 #include "SND_SoundObject.h"
34
35 #ifdef WIN32
36 // This warning tells us about truncation of __long__ stl-generated names.
37 // It can occasionally cause DevStudio to have internal compiler warnings.
38 #pragma warning( disable : 4786 )     
39 #endif
40
41
42 SND_AudioDevice::SND_AudioDevice()
43 {
44         m_wavecache = NULL;
45         m_audio = false;
46
47         for (int i = 0; i < NUM_SOURCES; i++)
48         {
49                 m_idObjectArray[i] = new SND_IdObject();
50                 m_idObjectArray[i]->SetId(i);
51                 m_idObjectArray[i]->SetSoundObject(NULL);
52                 m_idObjectList.addTail(m_idObjectArray[i]);
53         }
54 }
55
56
57
58 SND_AudioDevice::~SND_AudioDevice()
59 {
60         for (int i = 0; i < NUM_SOURCES; i++)
61         {
62                 delete m_idObjectArray[i];
63                 m_idObjectArray[i] = NULL;
64         }
65
66         if (m_wavecache)
67         {
68                 delete m_wavecache;
69                 m_wavecache = NULL;
70         }
71 }
72
73
74
75 bool SND_AudioDevice::IsInitialized()
76 {
77         return m_audio;
78 }
79
80
81
82 SND_WaveCache* SND_AudioDevice::GetWaveCache() const
83 {
84         return m_wavecache;
85 }
86
87
88
89 /* seeks an unused id and returns it */
90 bool SND_AudioDevice::GetNewId(SND_SoundObject* pObject)
91 {
92 #ifdef ONTKEVER
93         printf("SND_AudioDevice::GetNewId\n");
94 #endif
95
96         bool result = false;
97
98         // first, get the oldest (the first) idobject
99         SND_IdObject* pIdObject = (SND_IdObject*)m_idObjectList.getHead();
100
101         if (pIdObject->isTail())
102         {
103         }
104         else
105         {
106                 // find the first id object which doesn't have a high priority soundobject
107                 bool ThisSoundMustStay = false;
108                 bool OutOfIds = false;
109
110                 do
111                 {
112                         // if no soundobject present, it's seat may be taken
113                         if (pIdObject->GetSoundObject())
114                         {
115                                 // and also if it ain't highprio
116                                 if (pIdObject->GetSoundObject()->IsHighPriority())
117                                 {
118                                         ThisSoundMustStay = true;
119                                         pIdObject = (SND_IdObject*)pIdObject->getNext();
120                                         
121                                         // if the last one is a priority sound too, then there are no id's left
122                                         // and we won't add any new sounds
123                                         if (pIdObject->isTail())
124                                                 OutOfIds = true;
125                                 }
126                                 else
127                                 {
128                                         ThisSoundMustStay = false;
129                                 }
130                         }
131                         else
132                         {
133                                 ThisSoundMustStay = false;
134                         }
135
136                 } while (ThisSoundMustStay && !OutOfIds);
137
138                 if (!OutOfIds)
139                 {
140                         SND_SoundObject* oldobject = pIdObject->GetSoundObject();
141                         
142                         // revoke the old object if present
143                         if (oldobject)
144                         {
145 #ifdef ONTKEVER
146                                 printf("oldobject: %x\n", oldobject);
147 #endif
148                                 RevokeSoundObject(oldobject);
149                         }
150                         
151                         // set the new soundobject into the idobject
152                         pIdObject->SetSoundObject(pObject);
153                         
154                         // set the id into the soundobject
155                         int id = pIdObject->GetId();
156                         pObject->SetId(id);
157                         
158                         // connect the new id to the buffer the sample is stored in
159                         SetObjectBuffer(id, pObject->GetBuffer());
160                         
161                         // remove the idobject from the list and add it in the back again
162                         pIdObject->remove();
163                         m_idObjectList.addTail(pIdObject);
164                         
165                         result = true;
166                 }
167         }
168
169         return result;
170 }
171
172
173
174 void SND_AudioDevice::ClearId(SND_SoundObject* pObject)
175 {
176 #ifdef ONTKEVER
177         printf("SND_AudioDevice::ClearId\n");
178 #endif
179
180         if (pObject)
181         {
182                 int id = pObject->GetId();
183                 
184                 if (id != -1)
185                 {
186                         // lets get the idobject belonging to the soundobject
187                         SND_IdObject* pIdObject = m_idObjectArray[id];
188                         SND_SoundObject* oldobject = pIdObject->GetSoundObject();
189                         
190                         if (oldobject)
191                         {
192                                 RevokeSoundObject(oldobject);
193
194                                 // clear the idobject from the soundobject
195                                 pIdObject->SetSoundObject(NULL);
196                         }
197
198                         // remove the idobject and place it in front
199                         pIdObject->remove();
200                         m_idObjectList.addHead(pIdObject);
201                 }
202         }
203 }
204
205
206
207 void SND_AudioDevice::RevokeSoundObject(SND_SoundObject* pObject)
208 {
209 #ifdef ONTKEVER
210         printf("SND_AudioDevice::RevokeSoundObject\n");
211 #endif
212
213         // stop the soundobject
214         int id = pObject->GetId();
215
216         if (id >= 0 && id < NUM_SOURCES)
217         {
218                 StopObject(id);
219
220                 // remove the object from the 'activelist'
221                 pObject->SetActive(false);
222
223 #ifdef ONTKEVER
224                 printf("pObject->remove();\n");
225 #endif
226         }
227
228         // make sure its id is invalid
229         pObject->SetId(-1);
230 }
231
232 /*
233 void SND_AudioDevice::RemoveSample(const char* filename)
234 {
235         if (m_wavecache)
236                 m_wavecache->RemoveSample(filename);
237 }
238 */
239
240 void SND_AudioDevice::RemoveAllSamples()
241 {
242         if (m_wavecache)
243                 m_wavecache->RemoveAllSamples();
244 }
245