File: track.cpp

package info (click to toggle)
warzone2100 4.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 660,320 kB
  • sloc: cpp: 676,209; ansic: 391,201; javascript: 78,238; python: 16,632; php: 4,294; sh: 4,094; makefile: 2,629; lisp: 1,492; cs: 489; xml: 404; perl: 224; ruby: 156; java: 89
file content (474 lines) | stat: -rw-r--r-- 14,696 bytes parent folder | download | duplicates (2)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
	This file is part of Warzone 2100.
	Copyright (C) 1999-2004  Eidos Interactive
	Copyright (C) 2005-2020  Warzone 2100 Project

	Warzone 2100 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.

	Warzone 2100 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 Warzone 2100; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "tracklib.h"
#include "lib/framework/frame.h"
#include "lib/framework/frameresource.h"
#include "audio_id.h"
#include "src/droid.h"

#include <algorithm>
#include <vector>

//*
//
// defines
#define MAX_TRACKS	( 65536 )

//*
//
// static global variables
// array of pointers to sound effects
static std::vector<TRACK *> g_apTrack;

// number of tracks loaded
static SDWORD			g_iCurTracks = 0;

// flag set when system is active (for callbacks etc)
static bool				g_bSystemActive = false;
static AUDIO_CALLBACK	g_pStopTrackCallback = nullptr;

//*
// =======================================================================================================================
// =======================================================================================================================
//
bool sound_Init(HRTFMode hrtf)
{
	g_iCurTracks = 0;
	if (!sound_InitLibrary(hrtf))
	{
		debug(LOG_ERROR, "Cannot init sound library");
		return false;
	}

	g_apTrack.resize(std::max<size_t>(ID_MAX_SOUND, 512));

	// set system active flag for callbacks
	g_bSystemActive = true;
	return true;
}

//*
// =======================================================================================================================
// =======================================================================================================================
//
bool sound_Shutdown(void)
{
	// set inactive flag to prevent callbacks coming after shutdown
	g_bSystemActive = false;
	sound_ShutdownLibrary();
	return true;
}

//*
// =======================================================================================================================
// =======================================================================================================================
//
bool sound_GetSystemActive(void)
{
	return g_bSystemActive;
}

/** Retrieves loaded audio files and retrieves a TRACK from them on which some values are set and returns their respective ID numbers
 *  \param fileName the filename of the track
 *  \param loop whether the track should be looped until explicitly stopped
 *  \param volume the volume this track should be played on (range is 0-100)
 *  \param audibleRadius the radius from the source of sound where it can be heard
 *  \return a non-zero value representing the track id number when successful, zero when the file is not found or no more tracks can be loaded (i.e. the limit is reached)
 */
unsigned int sound_SetTrackVals(const char *fileName, bool loop, unsigned int volume, unsigned int audibleRadius)
{
	unsigned int trackID;
	TRACK *psTrack;

	if (fileName == nullptr || strlen(fileName) == 0) // check for empty filename.  This is a non fatal error.
	{
		debug(LOG_WARNING, "fileName is %s", (fileName == nullptr) ? "a NULL pointer" : "empty");
		return 0;
	}

	psTrack = (TRACK *)resGetData("WAV", fileName);
	if (psTrack == nullptr)
	{
		debug(LOG_WARNING, "track %s resource not found", fileName);
		return 0;
	}

	// get pre-assigned ID or produce one
	trackID = audio_GetIDFromStr(fileName);
	if (trackID == NO_SOUND)
	{
		// No pre-assigned ID available, produce one
		trackID = sound_GetAvailableID();
		if (trackID >= MAX_TRACKS)
		{
			ASSERT(trackID < MAX_TRACKS, "sound_GetTrackID: unused track not found / available!");
			return 0;
		}
	}

	ASSERT_OR_RETURN(0, trackID < g_apTrack.size(), "Invalid trackID: %u", trackID);
	if (g_apTrack[trackID] != nullptr)
	{
		debug(LOG_SOUND, "sound_SetTrackVals: track %i already set (filename: \"%s\") - overriding", trackID, g_apTrack[trackID]->fileName);
		ASSERT_OR_RETURN(0, psTrack == g_apTrack[trackID], "Data pointer not equal?");
	}

	// set track members
	psTrack->bLoop = loop;
	psTrack->iVol = volume;
	psTrack->iAudibleRadius = audibleRadius;

	// RAII: Make sure to initialize all values (we don't want undefined values)!
	psTrack->iTime = 0;
	psTrack->iTimeLastFinished = 0;
	psTrack->iNumPlaying = 0;

	// set global
	g_apTrack[trackID] = psTrack;

	// increment counter for amount of loaded tracks
	++g_iCurTracks;

	return trackID;
}

//*
// =======================================================================================================================
// =======================================================================================================================
//
void sound_ReleaseTrack(TRACK *psTrack)
{
	// This is here to save CPU wasted by the loop below;
	// Calling this function with psTrack = NULL is perfectly legal,
	// with or without this check
	if (!psTrack)
	{
		return;
	}

	// Run through the list of tracks and set the pointer to the track which is to be released to NULL
	for (auto it = g_apTrack.begin(), end = g_apTrack.end(); it != end; ++it)
	{
		if (*it == psTrack)
		{
			*it = nullptr;
		}
	}

	sound_FreeTrack(psTrack);
	free(psTrack);
}

//*
// =======================================================================================================================
// =======================================================================================================================
//
void sound_CheckAllUnloaded(void)
{
	for (auto pTrack : g_apTrack)
	{
		ASSERT(pTrack == nullptr, "A track is not unloaded yet (%s); check audio.json for duplicate IDs", pTrack->fileName);
	}
}

//*
// =======================================================================================================================
// =======================================================================================================================
//
bool sound_TrackLooped(SDWORD iTrack)
{
	if (!sound_CheckTrack(iTrack))
	{
		return false;
	}
	return g_apTrack[iTrack]->bLoop;
}

//*
// =======================================================================================================================
// =======================================================================================================================
//
SDWORD sound_GetNumPlaying(SDWORD iTrack)
{
	if (!sound_CheckTrack(iTrack))
	{
		return 0;
	}
	return g_apTrack[iTrack]->iNumPlaying;
}

//*
// =======================================================================================================================
// =======================================================================================================================
//
bool sound_CheckTrack(SDWORD iTrack)
{
	if (iTrack < 0 || iTrack > g_iCurTracks - 1 || static_cast<size_t>(iTrack) >= g_apTrack.size())
	{
		debug(LOG_SOUND, "Track number %i outside max %i (size: %zu)\n", iTrack, g_iCurTracks, g_apTrack.size());
		return false;
	}

	if (g_apTrack[iTrack] == nullptr)
	{
		debug(LOG_SOUND, "Track %i NULL\n", iTrack);
		return false;
	}

	return true;
}

//*
// =======================================================================================================================
// =======================================================================================================================
//
SDWORD sound_GetTrackTime(SDWORD iTrack)
{
	if (!sound_CheckTrack(iTrack))
	{
		return 0;
	}
	return g_apTrack[iTrack]->iTime;
}

//*
// =======================================================================================================================
// =======================================================================================================================
//
SDWORD sound_GetTrackVolume(SDWORD iTrack)
{
	if (!sound_CheckTrack(iTrack))
	{
		return 0;
	}
	return g_apTrack[iTrack]->iVol;
}

//*
// =======================================================================================================================
// =======================================================================================================================
//
SDWORD sound_GetTrackAudibleRadius(SDWORD iTrack)
{
	if (!sound_CheckTrack(iTrack))
	{
		return 0;
	}
	return g_apTrack[iTrack]->iAudibleRadius;
}

//*
// =======================================================================================================================
// =======================================================================================================================
//
const char *sound_GetTrackName(SDWORD iTrack)
{
	// If we get an invalid track ID or there are
	// currently no tracks loaded then return NULL
	if (iTrack <= 0
	    || static_cast<size_t>(iTrack) >= g_apTrack.size()
	    || iTrack == SAMPLE_NOT_FOUND
	    || g_apTrack[iTrack] == nullptr)
	{
		return nullptr;
	}

	return g_apTrack[iTrack]->fileName;
}

//*
// =======================================================================================================================
// =======================================================================================================================
//
bool sound_Play2DTrack(AUDIO_SAMPLE *psSample, bool bQueued)
{
	TRACK	*psTrack;

	// Check to make sure the requested track is loaded
	if (!sound_CheckTrack(psSample->iTrack))
	{
		return false;
	}

	psTrack = g_apTrack[psSample->iTrack];
	return sound_Play2DSample(psTrack, psSample, bQueued);
}

//*
// =======================================================================================================================
// =======================================================================================================================
//
bool sound_Play3DTrack(AUDIO_SAMPLE *psSample)
{
	TRACK	*psTrack;

	// Check to make sure the requested track is loaded
	if (!sound_CheckTrack(psSample->iTrack))
	{
		return false;
	}

	psTrack = g_apTrack[psSample->iTrack];
	return sound_Play3DSample(psTrack, psSample);
}

//*
// =======================================================================================================================
// =======================================================================================================================
//
void sound_StopTrack(AUDIO_SAMPLE *psSample)
{
	ASSERT_OR_RETURN(, psSample != nullptr, "Sample pointer invalid");

	sound_StopSample(psSample);

	// do stopped callback
	if (g_pStopTrackCallback != nullptr && psSample->psObj != nullptr)
	{
		g_pStopTrackCallback(psSample->psObj);
	}
}

//*
// =======================================================================================================================
// =======================================================================================================================
//
void sound_PauseTrack(AUDIO_SAMPLE *psSample)
{
	sound_StopSample(psSample);
}

//*
// =======================================================================================================================
// =======================================================================================================================
//
void sound_FinishedCallback(AUDIO_SAMPLE *psSample)
{
	ASSERT_OR_RETURN(, psSample != nullptr, "sound_FinishedCallback: sample pointer invalid\n");

	// Check to make sure the requested track is loaded
	if (!sound_CheckTrack(psSample->iTrack))
	{
		return;
	}

	if (g_apTrack[psSample->iTrack] != nullptr)
	{
		g_apTrack[psSample->iTrack]->iTimeLastFinished = sound_GetGameTime();
	}

	// call user callback if specified
	if (psSample->pCallback != nullptr)
	{
		psSample->pCallback(psSample->psObj);
		// NOTE: we must invalidate the iAudioID (iTrack) so the game knows to add the
		// sample back into the queue.  This is a bit of a hacky way to handle "looped" samples,
		// since currently, the game don't have a decent way to handle this.
		if (psSample->psObj)
		{
			droidAudioTrackStopped((void *)psSample->psObj);
		}
	}

	// set finished flag
	psSample->bFinishedPlaying = true;
}

//*
// =======================================================================================================================
// =======================================================================================================================
//
SDWORD sound_GetTrackID(TRACK *psTrack)
{
	if (psTrack == nullptr)
	{
		return SAMPLE_NOT_FOUND;
	}

	// find matching track
	for (unsigned int i = 0; i < static_cast<unsigned int>(g_apTrack.size()); ++i)
	{
		if (g_apTrack[i] == psTrack)
		{
			return i;
		}
	}

	// no matching track found
	return SAMPLE_NOT_FOUND;
}

//*
// =======================================================================================================================
// =======================================================================================================================
//
UDWORD sound_GetAvailableID()
{
	unsigned int i;

	// Iterate through the list of tracks until we find an unused ID slot
	for (i = ID_SOUND_NEXT; i < static_cast<unsigned int>(g_apTrack.size()); ++i)
	{
		if (g_apTrack[i] == nullptr)
		{
			return i;
		}
	}

	// add a new entry
	g_apTrack.push_back(nullptr);
	return i;
}

//*
// =======================================================================================================================
// =======================================================================================================================
//
void sound_SetStoppedCallback(AUDIO_CALLBACK pStopTrackCallback)
{
	g_pStopTrackCallback = pStopTrackCallback;
}

//*
// =======================================================================================================================
// =======================================================================================================================
//
UDWORD sound_GetTrackTimeLastFinished(SDWORD iTrack)
{
	if (!sound_CheckTrack(iTrack))
	{
		return 0;
	}
	return g_apTrack[iTrack]->iTimeLastFinished;
}

//*
// =======================================================================================================================
// =======================================================================================================================
//
void sound_SetTrackTimeLastFinished(SDWORD iTrack, UDWORD iTime)
{
	if (!sound_CheckTrack(iTrack))
	{
		return;
	}
	g_apTrack[iTrack]->iTimeLastFinished = iTime;
}