File: objsound.c

package info (click to toggle)
searchandrescue 0.8.2-10
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,656 kB
  • ctags: 6,111
  • sloc: ansic: 89,072; cpp: 7,691; sh: 90; makefile: 80
file content (628 lines) | stat: -rw-r--r-- 14,388 bytes parent folder | download | duplicates (6)
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <math.h>

#include "matrixmath.h"
#include "sound.h"
#include "obj.h"
#include "objsound.h"
#include "sar.h"
#include "config.h"


#ifdef __MSW__
static double rint(double x);
#endif	/* __MSW__ */


sar_sound_source_struct *SARSoundSourceNew(
	const char *name,
	const char *filename,
	const char *filename_far,
	float range,                    /* In meters */
	float range_far,                /* In meters */
	const sar_position_struct *pos,
	float cutoff,                   /* In radians */
	const sar_direction_struct *dir,
	int sample_rate_limit           /* In Hz */
);
void SARSoundSourceDelete(sar_sound_source_struct *sndsrc);
int SARSoundSourceMatchFromList(
	sar_sound_source_struct **list, int total,
	const char *name
);
void SARSoundSourcePlayFromList(
	snd_recorder_struct *recorder,
	sar_sound_source_struct **list, int total,
	const char *name,
	const sar_position_struct *pos,		/* Object position */
	const sar_direction_struct *dir,	/* Object direction */
	const sar_position_struct *ear_pos
);
snd_play_struct *SARSoundSourcePlayFromListRepeating(
	snd_recorder_struct *recorder,
	sar_sound_source_struct **list, int total,
	const char *name, int *sndsrc_num
);

void SARSoundEngineMute(
	snd_recorder_struct *recorder,
	snd_play_struct *inside_snd_play,
	snd_play_struct *outside_snd_play 
);
void SARSoundEngineUpdate(
	snd_recorder_struct *recorder,
	sar_sound_source_struct **list, int total,
	snd_play_struct *inside_snd_play,
	int inside_sndsrc_num,
	snd_play_struct *outside_snd_play,
	int outside_sndsrc_num,
	char ear_in_cockpit,
	sar_engine_state engine_state,
	float throttle,
	float distance_to_camera
);
int SARSoundStallUpdate(
	snd_recorder_struct *recorder,
	sar_sound_source_struct **list, int total,
	void **stall_snd_play,
	int stall_sndsrc_num,
	char ear_in_cockpit,
	sar_flight_model_type flight_model_type,
	char is_landed,
	float speed, float speed_stall
);
void SARSoundOverSpeedUpdate(
	snd_recorder_struct *recorder,
	sar_sound_source_struct **list, int total,
	void **overspeed_snd_play,
	int overspeed_sndsrc_num,
	char ear_in_cockpit, char is_overspeed
);


#define POW(x,y)        (((x) > 0.0f) ? pow(x,y) : 0.0f)

#define MAX(a,b)	(((a) > (b)) ? (a) : (b))
#define MIN(a,b)	(((a) < (b)) ? (a) : (b))
#define CLIP(a,l,h)	(MIN(MAX((a),(l)),(h)))
#define STRDUP(s)	(((s) != NULL) ? strdup(s) : NULL)


#ifdef __MSW__
static double rint(double x)
{
	if((double)((double)x - (int)x) > (double)0.5)
	    return((double)((int)x + (int)1));
	else
	    return((double)((int)x));
}
#endif	/* __MSW__ */



/*
 *      Creates a new sound source structure.
 */
sar_sound_source_struct *SARSoundSourceNew(
	const char *name,
	const char *filename,
	const char *filename_far,
	float range,			/* In meters */
	float range_far,		/* In meters */
	const sar_position_struct *pos,
	float cutoff,			/* In radians */
	const sar_direction_struct *dir,
	int sample_rate_limit		/* In Hz */
)
{
	sar_sound_source_struct *sndsrc = SAR_SOUND_SOURCE(
	    calloc(1, sizeof(sar_sound_source_struct))
	);
	if(sndsrc == NULL)
	    return(NULL);

	sndsrc->name = STRDUP(name);

	sndsrc->filename = STRDUP(filename);
	sndsrc->filename_far = STRDUP(filename_far);

	sndsrc->range = range;
	sndsrc->range_far = range_far;

	if(pos != NULL)
	    memcpy(&sndsrc->pos, pos, sizeof(sar_position_struct));

	sndsrc->cutoff = cutoff;

	if(dir != NULL)
	    memcpy(&sndsrc->dir, dir, sizeof(sar_direction_struct));

	sndsrc->sample_rate_limit = sample_rate_limit;

	return(sndsrc);
}

/*
 *      Deletes the given sound source structure.
 */
void SARSoundSourceDelete(sar_sound_source_struct *sndsrc)
{
	if(sndsrc == NULL)
	    return;

	free(sndsrc->name);
	free(sndsrc->filename);
	free(sndsrc->filename_far);
	free(sndsrc);
}

/*
 *      Returns the index of the sound source found in the given list
 *      that matches the given name.
 *
 *      Can return -1 on failed match.
 */
int SARSoundSourceMatchFromList(
	sar_sound_source_struct **list, int total,
	const char *name
)
{
	int i;
	sar_sound_source_struct *sndsrc;


	if((list == NULL) || (total <= 0) || (name == NULL))
	    return(-1);

	for(i = 0; i < total; i++)
	{
	    sndsrc = list[i];
	    if(sndsrc == NULL)
		continue;

	    if(sndsrc->name == NULL)
		continue;

	    if(!strcmp(sndsrc->name, name))
		return(i);
	}

	return(-1);
}

/*
 *      Searches the given list of sound sources for a sound source
 *      who's name matches the given name.
 *
 *      If it is matched then it will be played once.
 */
void SARSoundSourcePlayFromList(
	snd_recorder_struct *recorder,
	sar_sound_source_struct **list, int total,
	const char *name,
	const sar_position_struct *pos,		/* Object position */
	const sar_direction_struct *dir,	/* Object direction */
	const sar_position_struct *ear_pos
)
{
	int i;
	double a[3 * 1], b[3 * 1];
	float r;
	sar_sound_source_struct *sndsrc;
	sar_position_struct snd_pos;


	if(recorder == NULL)
	    return;

	i = SARSoundSourceMatchFromList(list, total, name);
	sndsrc = (i > -1) ? list[i] : NULL;
	if(sndsrc == NULL)
	    return;

	if(sndsrc->filename == NULL)
	    return;


	/* Found sound source to play */

	/* Calculate world coordinates of sound as snd_pos */
	if((pos != NULL) && (dir != NULL))
	{
	    a[0] = sndsrc->pos.x;
	    a[1] = sndsrc->pos.y;
	    a[2] = sndsrc->pos.z;

	    /* Rotate matrix a into b */
	    MatrixRotateBank3(a, -dir->bank, b);        /* Our bank is negative,
							 * so pass as flipped
							 */
	    MatrixRotatePitch3(b, dir->pitch, a);
	    MatrixRotateHeading3(a, dir->heading, b);

	    snd_pos.x = (float)(pos->x + b[0]);
	    snd_pos.y = (float)(pos->y + b[1]);
	    snd_pos.z = (float)(pos->z + b[2]);
	}
	else if(pos != NULL)
	{
	    snd_pos.x = pos->x;
	    snd_pos.y = pos->y;
	    snd_pos.z = pos->z;
	}
	else
	{
	    memset(&snd_pos, 0x00, sizeof(sar_position_struct));
	}


	/* Calculate 3d distance r of source to ear */
	if(ear_pos != NULL)
	    r = (float)SFMHypot3(
		snd_pos.x - ear_pos->x,
		snd_pos.y - ear_pos->y,
		snd_pos.z - ear_pos->z
	    );
	else
	    r = 0.0f;


	/* Calculate volume due to distance r relative to the
	 * maximum range of the sound source.
	 */
	if(sndsrc->range > 0.0)
	{
	    if(r < sndsrc->range)
	    {
		float vol = (float)MAX(
		    1.0 - (r / sndsrc->range), 0.0
		);
		SoundStartPlayVoid(
		    recorder,
		    sndsrc->filename,
		    vol, vol,
		    0, 0
		);
	    }
	}
	else
	{
	    float vol = 1.0f;
	    SoundStartPlayVoid(
		recorder,
		sndsrc->filename,
		vol, vol,
		0, 0
	    );
	}
}

/*
 *      Searches the given list of sound sources for a sound source
 *      who's name matches the given name.
 *
 *      If it is matched then it will be played repeatedly.
 *
 *      Returns the pointer to the new sound object play structure
 *      or NULL on failure.
 */
snd_play_struct *SARSoundSourcePlayFromListRepeating(
	snd_recorder_struct *recorder,
	sar_sound_source_struct **list, int total,
	const char *name, int *sndsrc_num
)
{
	int i;
	sar_sound_source_struct *sndsrc;

	if(sndsrc_num != NULL)
	    *sndsrc_num = -1;

	if(recorder == NULL)
	    return(NULL);

	i = SARSoundSourceMatchFromList(list, total, name);
	sndsrc = (i > -1) ? list[i] : NULL;
	if(sndsrc == NULL)
	    return(NULL);

	if(sndsrc->filename == NULL)
	    return(NULL);

	if(sndsrc_num != NULL)
	    *sndsrc_num = i;

	/* Start playing the sound object repeatedly, the volume
	 * should be initially 0.0 (muted). The calling function
	 * is responsible for increasing the volume of the returned
	 * sound play object.
	 */
	return(SoundStartPlay(
	    recorder,
	    sndsrc->filename,   /* Full path to object */
	    0.0, 0.0,           /* Volume, from 0.0 to 1.0 */
	    0,                  /* Applied sample rate, can be 0 */
	    SND_PLAY_OPTION_REPEATING   /* Options */
	));
}


/*
 *	Calls SARSoundEngineUpdate() with throttle set to 0.0
 *	and the engine_state set to SAR_ENGINE_OFF.
 *
 *	This effectively mutes the engine sound.
 */
void SARSoundEngineMute(
	snd_recorder_struct *recorder,
	snd_play_struct *inside_snd_play,
	snd_play_struct *outside_snd_play 
)
{
	SARSoundEngineUpdate(
	    recorder,
	    NULL, 0,			/* No sound sources needed */
	    inside_snd_play,
	    -1,
	    outside_snd_play,
	    -1,
	    0,				/* In cockpit? */
	    SAR_ENGINE_OFF,	/* Engine state */
	    0.0f,			/* Throttle */
	    0.0f			/* Distance to camera */
	);
}

/* 
 *      Adjusts the engine volume and sample rate.
 */
void SARSoundEngineUpdate(
	snd_recorder_struct *recorder,
	sar_sound_source_struct **list, int total,
	snd_play_struct *inside_snd_play,
	int inside_sndsrc_num,
	snd_play_struct *outside_snd_play,
	int outside_sndsrc_num,
	char ear_in_cockpit,
	sar_engine_state engine_state,	/* One of SAR_ENGINE_* */
	float throttle,			/* Throttle coeff 0.0 to 1.0 */
	float distance_to_camera	/* In meters */
)
{
	int sample_rate, new_sample_rate, sample_rate_limit;	/* In Hz */
	float volume;				/* 0.0 to 1.0 */
	float audiable_radius = 2000.0f;
	sar_sound_source_struct *inside_sndsrc = NULL,
				*outside_sndsrc = NULL;

	/* Not connected to sound server? */
	if(recorder == NULL)
	    return;

	/* If the engine is off then mute all sound volumes, this
	 * improves efficiency since no sound would be mixed by
	 * the sound server if volume is 0.0 but the sound object will
	 * still remain valid.
	 */
	if(engine_state != SAR_ENGINE_ON)
	{
	    SoundChangePlayVolume(
		recorder, inside_snd_play,
		0.0f, 0.0f
	    );
	    SoundChangePlayVolume(
		recorder, outside_snd_play,
		0.0f, 0.0f
	    );
	    return;
	}


	/* Get pointers to sound source structures (if possible) */
	if(list != NULL)
	{
	    if((inside_sndsrc_num >= 0) &&
	       (inside_sndsrc_num < total)
	    )
		inside_sndsrc = list[inside_sndsrc_num];

	    if((outside_sndsrc_num >= 0) &&
	       (outside_sndsrc_num < total)
	    )
		outside_sndsrc = list[outside_sndsrc_num];
	}


	/* Begin calculating sample rate based on the throttle position,
	 * increase to a faster sample rate as throttle increases.
	 */

	/* Get lower and upper sample rate bounds and ranges */
	sample_rate_limit = sample_rate = recorder->sample_rate;
	if(ear_in_cockpit && inside_sndsrc != NULL)
	{
	    sample_rate_limit = inside_sndsrc->sample_rate_limit;
	    audiable_radius = inside_sndsrc->range;
	}
	else if(outside_sndsrc != NULL)
	{
	    sample_rate_limit = outside_sndsrc->sample_rate_limit;
	    audiable_radius = outside_sndsrc->range;
	}

	/* Calculate new sample rate based on throttle position */
	new_sample_rate = (int)rint(
	    ((MAX(sample_rate_limit - sample_rate, 0) * throttle) +
	    sample_rate) / 1000
	);
	new_sample_rate = (int)(new_sample_rate * 1000);


	/* Calculate volume based on (1 - (x^0.5)) curve */
	volume = (float)CLIP(1.0 -
	    POW(distance_to_camera / audiable_radius, 0.5),
	    0.0, 1.0
	);

	/* Is ear inside cockpit? */
	if(ear_in_cockpit)
	{
	    /* Inside cockpit */
	    SoundChangePlayVolume(
		recorder, inside_snd_play,
		volume, volume
	    );
	    SoundChangePlaySampleRate(
		recorder, inside_snd_play,
		new_sample_rate
	    );
	    SoundChangePlayVolume(
		recorder, outside_snd_play,
		0.0f, 0.0f
	    );
	    SoundChangePlaySampleRate(
		recorder, outside_snd_play,
		new_sample_rate
	    );
	}
	else
	{
	    /* Outside */
	    SoundChangePlayVolume(
		recorder, inside_snd_play,
		0.0f, 0.0f
	    );
	    SoundChangePlaySampleRate(
		recorder, inside_snd_play,
		new_sample_rate
	    );
	    SoundChangePlayVolume(
		recorder, outside_snd_play,
		volume, volume
	    );
	    SoundChangePlaySampleRate(
		recorder, outside_snd_play,
		new_sample_rate
	    );
	}
}

/*
 *	Plays or stops the repeating stall warning sound
 *
 *	The speed_stall should have modifications (ie flaps) already
 *	applied to it.
 *
 *	Returns 1 if sound was turned on or 0 if it was not.
 */
int SARSoundStallUpdate(
	snd_recorder_struct *recorder,
	sar_sound_source_struct **list, int total,
	void **stall_snd_play,
	int stall_sndsrc_num,
	char ear_in_cockpit,
	sar_flight_model_type flight_model_type,
	char is_landed,
	float speed, float speed_stall
)
{
	Boolean stalling = False;
	float speed_threshold;

	if((recorder == NULL) || (stall_snd_play == NULL))
	    return(0);

	/* Calculate the speed at which the sound should be turned on
	 * if the speed is lower than this value
	 */
	speed_threshold = speed_stall * 0.78f;

	/* Stall warning sound should be turned on? */
	if(!is_landed && (flight_model_type == SAR_FLIGHT_MODEL_AIRPLANE))
	{
	    if((speed < speed_threshold) && (speed_threshold > 0.0f))
		stalling = True;
	}

	/* Check if the sound needs to be played or stopped? */
	if(stalling && (*stall_snd_play == NULL))
	{
	    /* Need to start playing stall sound */
	    int i = stall_sndsrc_num;
	    sar_sound_source_struct *sndsrc;

	    if((i >= 0) && (i < total))
		sndsrc = list[i];
	    else
		sndsrc = NULL;

	    if(sndsrc != NULL)
		*stall_snd_play = (void *)SoundStartPlay(
		    recorder,
		    sndsrc->filename,
		    1.0f, 1.0f,
		    0,
		    SND_PLAY_OPTION_REPEATING
		);
	}
	else if(!stalling && (*stall_snd_play != NULL))
	{
	    /* Need to stop playing stall sound */
	    SoundStopPlay(
		recorder,
		(snd_play_struct *)(*stall_snd_play)
	    );
	    *stall_snd_play = NULL;
	}

	return(stalling ? 1 : 0);
}

/*
 *      Plays or stops the repeating overspeed warning sound.
 */
void SARSoundOverSpeedUpdate(
	snd_recorder_struct *recorder,
	sar_sound_source_struct **list, int total,
	void **overspeed_snd_play,
	int overspeed_sndsrc_num,
	char ear_in_cockpit, char is_overspeed
)
{
	if((recorder == NULL) || (overspeed_snd_play == NULL))
	    return;

	/* Check if the sound needs to be played or stopped? */
	if(is_overspeed && (*overspeed_snd_play == NULL))
	{
	    /* Need to start playing overspeed sound */
	    int i = overspeed_sndsrc_num;
	    sar_sound_source_struct *sndsrc;

	    if((i >= 0) && (i < total))
		sndsrc = list[i];
	    else
		sndsrc = NULL;

	    if(sndsrc != NULL)
		*overspeed_snd_play = (void *)SoundStartPlay(
		    recorder,
		    sndsrc->filename,
		    1.0f, 1.0f,
		    0,
		    SND_PLAY_OPTION_REPEATING
		);
	}
	else if(!is_overspeed && (*overspeed_snd_play != NULL))
	{
	    /* Need to stop playing overspeed sound */
	    SoundStopPlay(
		recorder,
		(snd_play_struct *)(*overspeed_snd_play)
	    );
	    *overspeed_snd_play = NULL;
	}
}