File: skyrocket_sound.cpp

package info (click to toggle)
rss-glx 0.7.5-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 10,184 kB
  • ctags: 1,561
  • sloc: cpp: 11,577; ansic: 4,233; sh: 3,315; asm: 1,312; xml: 552; makefile: 194; perl: 49
file content (197 lines) | stat: -rw-r--r-- 6,527 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
/*
 * Copyright (C) 2002  Terence M. Welsh
 * Ported to Linux by Tugrul Galatali <tugrul@galatali.com>
 *
 * Skyrocket is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as 
 * published by the Free Software Foundation.
 *
 * Skyrocket 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
 */

#include "skyrocket_sound.h"

#ifdef HAVE_OPENAL

#include <math.h>
#include <AL/al.h>
#include <AL/alut.h>

#include <list>

#include "loadTexture.h"

// reference distance, max distance
// reference distance = sound is partly attenuated
// maximum distance = sound is completely attenuated
float sound_distances[NUM_BUFFERS][2] = { 
	{ 300.0f, 600.0f },	// launch sounds
	{ 300.0f, 600.0f },
	{ 4000.0f, 8000.0f },	// booms
	{ 4000.0f, 8000.0f },
	{ 4000.0f, 8000.0f },
	{ 6000.0f, 12000.0f },
	{ 2500.0f, 5000.0f },	// poppers
	{ 10000.0f, 20000.0f },	// suck
	{ 20000.0f, 40000.0f },	// nuke
	{ 2500.0f, 5000.0f }	// whistle
};

class soundnode;

ALuint g_Buffers[NUM_BUFFERS];
ALuint g_Sources[NUM_SOURCES];
extern float elapsedTime;
extern int dSound;
extern int kSlowMotion;

void swap_bytes (unsigned char *data, int bytes)
{
	int i = 0;

	for (i = 0; i < bytes - 1; i += 2) {
		int temp = data[i];

		data[i] = data[i + 1];
		data[i + 1] = temp;
	}
}

void initSound ()
{
	int i = 1;
	unsigned char *j;
	unsigned char *l_snd;

	j = (unsigned char *)&i;

	alutInit (NULL, 0);

	alDistanceModel (AL_INVERSE_DISTANCE);
	// As of October, 2001, AL_ROLLOFF_FACTOR isn't implemented in the Windows 
	// version of OpenAL, so the distance attenuation models won't work.  You 
	// have to set the gains yourself.

	//alDistanceModel(AL_NONE);

	alDopplerVelocity (1130.0f);	// Sound travels at 1130 feet/sec
	alListenerf (AL_GAIN, float (dSound) * 0.01f);	// Volume

	// Initialize sound data
	alGenBuffers (NUM_BUFFERS, g_Buffers);
	alGenSources (NUM_SOURCES, g_Sources);

#define BUFFER_SOUND(index, data, compressedsize, size) \
	LOAD_TEXTURE(l_snd, data, compressedsize, size) \
	if (!*j) swap_bytes(l_snd, size); \
	alBufferData(g_Buffers[index], AL_FORMAT_MONO16, l_snd, size, 44100); \
	FREE_TEXTURE(l_snd)

	BUFFER_SOUND (BOOM1SOUND, launch1SoundData, launch1SoundData_compressedsize, launch1SoundData_size)
	BUFFER_SOUND (BOOM2SOUND, launch2SoundData, launch2SoundData_compressedsize, launch2SoundData_size)
	BUFFER_SOUND (BOOM1SOUND, boom1SoundData, boom1SoundData_compressedsize, boom1SoundData_size)
	BUFFER_SOUND (BOOM2SOUND, boom2SoundData, boom2SoundData_compressedsize, boom2SoundData_size)
	BUFFER_SOUND (BOOM3SOUND, boom3SoundData, boom3SoundData_compressedsize, boom3SoundData_size)
	BUFFER_SOUND (BOOM4SOUND, boom4SoundData, boom4SoundData_compressedsize, boom4SoundData_size)
	BUFFER_SOUND (POPPERSOUND, popperSoundData, popperSoundData_compressedsize, popperSoundData_size)
	BUFFER_SOUND (POPPERSOUND, suckSoundData, suckSoundData_compressedsize, suckSoundData_size)
	BUFFER_SOUND (POPPERSOUND, nukeSoundData, nukeSoundData_compressedsize, nukeSoundData_size)
	BUFFER_SOUND (POPPERSOUND, whistleSoundData, whistleSoundData_compressedsize, whistleSoundData_size)
}

class soundnode {
      public:
	int sound;
	float pos[3];
	float dist;
	float time;		// time until sound plays

	  soundnode () {
	};

	~soundnode () {
	};
};

std::list < soundnode > soundlist;

void insertSoundNode (int sound, rsVec source, rsVec observer)
{
	soundnode *node;
	rsVec dir = observer - source;

	soundlist.push_back (soundnode ());
	node = &soundlist.back ();
	node->sound = sound;
	node->pos[0] = source[0];
	node->pos[1] = source[1];
	node->pos[2] = source[2];
	// distance to sound
	node->dist = sqrt (dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]);
	// Sound travels at 1130 feet/sec
	node->time = node->dist * 0.000885f;
	if (node->sound == POPPERSOUND)	// poppers have a little delay
		node->time += 2.5f;
}

void updateSound (float *listenerPos, float *listenerVel, float *listenerOri)
{
	static int source = 0;

	// Set current listener attributes
	alListenerfv (AL_POSITION, listenerPos);
	alListenerfv (AL_VELOCITY, listenerVel);
	alListenerfv (AL_ORIENTATION, listenerOri);

	std::list < soundnode >::iterator cursound = soundlist.begin ();
	while (cursound != soundlist.end ()) {
		cursound->time -= elapsedTime;
		if (cursound->time <= 0.0f) {
			if (cursound->dist < sound_distances[cursound->sound][1]) {
				alSourceStop (g_Sources[source]);
				alSourcei (g_Sources[source], AL_BUFFER, g_Buffers[cursound->sound]);
				//alSourcef(g_Sources[source], AL_REFERENCE_DISTANCE, sound_distances[cursound->sound][0]);
				//alSourcef(g_Sources[source], AL_ROLLOFF_FACTOR, 0.013557606f);
				// HACK:  AL_REFERENCE_DISTANCE must be set or OpenAL won't make any sound at all.
				// I assume this will be fixed in future implementations of OpenAL.
				alSourcef (g_Sources[source], AL_REFERENCE_DISTANCE, 1000000.0f);
				// As of October, 2001, AL_ROLLOFF_FACTOR isn't implemented in the Windows 
				// version of OpenAL, so the distance attenuation models won't work.  You 
				// have to set the gains yourself.
				// Here I implement the AL_INVERSE_DISTANCE model formula from the OpenAL spec.
				//float gain = 1.0f - 20.0f * log10(1.0f + 0.013557606f 
				//      * (cursound->dist - sound_distances[cursound->sound][0]) 
				//      / sound_distances[cursound->sound][0]);
				// But I don't like the way it sounds, so I'll just fudge everything
				float gain = 1.0f - (cursound->dist / sound_distances[cursound->sound][1]);

				if (gain > 1.0f)
					gain = 1.0f;
				if (gain < 0.0f)
					gain = 0.0f;
				alSourcef (g_Sources[source], AL_GAIN, gain);
				alSourcefv (g_Sources[source], AL_POSITION, cursound->pos);
				alSourcei (g_Sources[source], AL_LOOPING, AL_FALSE);
				if (kSlowMotion)	// Slow down the sound
					alSourcef (g_Sources[source], AL_PITCH, 0.5f);
				else	// Sound at regular speed
					alSourcef (g_Sources[source], AL_PITCH, 1.0f);
				alSourcePlay (g_Sources[source]);
				source++;
				if (source >= NUM_SOURCES)
					source = 0;
			}
			cursound = soundlist.erase (cursound);
		} else
			cursound++;
	}
}

#endif