Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SND_AudioDevice.cpp « intern « SoundSystem « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2080015312869b088140d860a983695daa5a7d84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/**
 * $Id$
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version. The Blender
 * Foundation also sells licenses for use in proprietary software under
 * the Blender License.  See http://www.blender.org/BL/ for information
 * about this.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
 */

#include "SND_AudioDevice.h"

#include "SND_SoundObject.h"

#ifdef WIN32
// This warning tells us about truncation of __long__ stl-generated names.
// It can occasionally cause DevStudio to have internal compiler warnings.
#pragma warning( disable : 4786 )     
#endif


SND_AudioDevice::SND_AudioDevice()
{
	m_wavecache = NULL;
	m_audio = false;

	for (int i = 0; i < NUM_SOURCES; i++)
	{
		m_idObjectArray[i] = new SND_IdObject();
		m_idObjectArray[i]->SetId(i);
		m_idObjectArray[i]->SetSoundObject(NULL);
		m_idObjectList.addTail(m_idObjectArray[i]);
	}
}



SND_AudioDevice::~SND_AudioDevice()
{
	for (int i = 0; i < NUM_SOURCES; i++)
	{
		delete m_idObjectArray[i];
		m_idObjectArray[i] = NULL;
	}

	if (m_wavecache)
	{
		delete m_wavecache;
		m_wavecache = NULL;
	}
}



bool SND_AudioDevice::IsInitialized()
{
	return m_audio;
}



SND_WaveCache* SND_AudioDevice::GetWaveCache() const
{
	return m_wavecache;
}



/* seeks an unused id and returns it */
bool SND_AudioDevice::GetNewId(SND_SoundObject* pObject)
{
#ifdef ONTKEVER
	printf("SND_AudioDevice::GetNewId\n");
#endif

	bool result;

	// first, get the oldest (the first) idobject
	SND_IdObject* pIdObject = (SND_IdObject*)m_idObjectList.getHead();

	if (pIdObject->isTail())
	{
		result = false;
	}
	else
	{
		// find the first id object which doesn't have a high priority soundobject
		bool ThisSoundMustStay = false;
		bool OutOfIds = false;

		do
		{
			// if no soundobject present, it's seat may be taken
			if (pIdObject->GetSoundObject())
			{
				// and also if it ain't highprio
				if (pIdObject->GetSoundObject()->IsHighPriority())
				{
					ThisSoundMustStay = true;
					pIdObject = (SND_IdObject*)pIdObject->getNext();
					
					// if the last one is a priority sound too, then there are no id's left
					// and we won't add any new sounds
					if (pIdObject->isTail())
						OutOfIds = true;
				}
				else
				{
					ThisSoundMustStay = false;
				}
			}
			else
			{
				ThisSoundMustStay = false;
			}

		} while (ThisSoundMustStay && !OutOfIds);

		if (!OutOfIds)
		{
			SND_SoundObject* oldobject = oldobject = pIdObject->GetSoundObject();
			
			// revoke the old object if present
			if (oldobject)
			{
#ifdef ONTKEVER
				printf("oldobject: %x\n", oldobject);
#endif
				RevokeSoundObject(oldobject);
			}
			
			// set the new soundobject into the idobject
			pIdObject->SetSoundObject(pObject);
			
			// set the id into the soundobject
			int id = pIdObject->GetId();
			pObject->SetId(id);
			
			// connect the new id to the buffer the sample is stored in
			SetObjectBuffer(id, pObject->GetBuffer());
			
			// remove the idobject from the list and add it in the back again
			pIdObject->remove();
			m_idObjectList.addTail(pIdObject);
			
			result = true;
		}
	}

	return result;
}



void SND_AudioDevice::ClearId(SND_SoundObject* pObject)
{
#ifdef ONTKEVER
	printf("SND_AudioDevice::ClearId\n");
#endif

	if (pObject)
	{
		int id = pObject->GetId();
		
		if (id != -1)
		{
			// lets get the idobject belonging to the soundobject
			SND_IdObject* pIdObject = m_idObjectArray[id];
			SND_SoundObject* oldobject = pIdObject->GetSoundObject();
			
			if (oldobject)
			{
				RevokeSoundObject(oldobject);

				// clear the idobject from the soundobject
				pIdObject->SetSoundObject(NULL);
			}

			// remove the idobject and place it in front
			pIdObject->remove();
			m_idObjectList.addHead(pIdObject);
		}
	}
}



void SND_AudioDevice::RevokeSoundObject(SND_SoundObject* pObject)
{
#ifdef ONTKEVER
	printf("SND_AudioDevice::RevokeSoundObject\n");
#endif

	// stop the soundobject
	int id = pObject->GetId();

	if (id >= 0 && id < NUM_SOURCES)
	{
		StopObject(id);

		// remove the object from the 'activelist'
		pObject->SetActive(false);

#ifdef ONTKEVER
		printf("pObject->remove();\n");
#endif
	}

	// make sure its id is invalid
	pObject->SetId(-1);
}

/*
void SND_AudioDevice::RemoveSample(const char* filename)
{
	if (m_wavecache)
		m_wavecache->RemoveSample(filename);
}
*/

void SND_AudioDevice::RemoveAllSamples()
{
	if (m_wavecache)
		m_wavecache->RemoveAllSamples();
}