25#undef USE_OPENAL_DOPPLER
35 ALfloat far_away[] = { 0.0f, 0.0f, 1000.0f };
36 ALfloat zeroes[] = { 0.0f, 0.0f, 0.0f };
37 ALfloat front[] = { 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f };
38 dev = alcOpenDevice( NULL );
40 throw (
"Could not open device");
45 cc = alcCreateContext(
dev, NULL);
47 alcCloseDevice(
dev );
48 throw (
"Could not create context.");
51 alcMakeContextCurrent(
cc );
58 const int MAX_SOURCES = 1024;
60 ALuint sourcelist[MAX_SOURCES];
61 for (sources = 0; sources < MAX_SOURCES; sources++) {
62 alGenSources(1, &sourcelist[sources]);
63 if (alGetError() != AL_NO_ERROR) {
69 for (clear = 0; clear < sources; clear++) {
70 if (alIsSource(sourcelist[clear])) {
71 alDeleteSources(1, &sourcelist[clear]);
72 if (alGetError() != AL_NO_ERROR) {
73 printf(
"Error in probing OpenAL sources.\n");
76 printf(
"Error in probing OpenAL sources.\n");
85 ALuint bufferlist[MAX_SOURCES];
86 for (buffers = 0; buffers < MAX_SOURCES; buffers++) {
87 alGenBuffers(1, &bufferlist[buffers]);
88 if (alGetError() != AL_NO_ERROR) {
93 for (clear = 0; clear < buffers; clear++) {
94 if (alIsBuffer(bufferlist[clear])) {
95 alDeleteBuffers(1, &bufferlist[clear]);
96 if (alGetError() != AL_NO_ERROR) {
97 printf(
"Error in probing OpenAL buffers.\n");
100 printf(
"Error in probing OpenAL buffers.\n");
106 printf(
"OpenAL backend info:\n Vendor: %s\n Renderer: %s\n Version: %s\n", alGetString(AL_VENDOR), alGetString(AL_RENDERER), alGetString(AL_VERSION));
107 printf(
" Available sources: %d%s\n",
OSI_MAX_SOURCES, (sources >= MAX_SOURCES) ?
" or more" :
"");
108 printf(
" Available buffers: %d%s\n",
OSI_MAX_BUFFERS, (buffers >= MAX_SOURCES) ?
" or more" :
"");
110 alDistanceModel ( AL_INVERSE_DISTANCE );
111 int error = alGetError();
112 if (error != AL_NO_ERROR) {
113 printf(
"OpenAL Error: %d alDistanceModel\n", error);
116 alDopplerFactor (1.0f);
119 error = alGetError();
120 if (error != AL_NO_ERROR) {
121 printf(
"OpenAL Error: %d alDopplerX\n", error);
125 alListenerfv(AL_POSITION, far_away );
126 alListenerfv(AL_VELOCITY, zeroes );
127 alListenerfv(AL_ORIENTATION, front );
128 error = alGetError();
129 if (error != AL_NO_ERROR) {
130 printf(
"OpenAL Error: %d alListenerfv\n", error);
152 for (
unsigned int i=0; i<
sound_list.size(); i++) {
156 alcDestroyContext (
cc);
157 alcCloseDevice (
dev);
181 ALfloat listener_pos[3];
183 ALfloat listener_orientation[6];
184 ALfloat zeros[] = {0.0f, 0.0f, 0.0f};
187 for (i = 0; i<3; i++) {
188 listener_pos[i] = p_obs[i];
190 listener_orientation[i] = c_obs[i];
191 listener_orientation[i+3] = a_obs[i];
194 alListenerfv(AL_POSITION, listener_pos );
195#ifdef USE_OPENAL_DOPPLER
196 alListenerfv(AL_VELOCITY, listener_speed );
198 alListenerfv(AL_VELOCITY, zeros);
200 alListenerfv(AL_ORIENTATION, listener_orientation );
203 for (i = 0; i<n_cars; i++) {
211 car_src[id].setListener (p_obs, u_obs);
224 for (i = n_cars - 1; i >= 0; i--) {
233#ifdef USE_OPENAL_DOPPLER
249 float max_skid_vol[4] = {0.0f, 0.0f, 0.0f, 0.0f};
250 int max_skid_id[4] = {0,0,0,0};
252 for (
id = 0;
id<n_cars;
id++) {
254 for (
int j=0; j<4; j++) {
256 if (skvol > max_skid_vol[j]) {
257 max_skid_vol[j] = skvol;
263 for (i = 0; i<4; i++) {
264 int id = max_skid_id[i];
266 skid_sound[i]->setSource (sound_data[i].
p, sound_data[i].u);
267 skid_sound[i]->setVolume (sound_data[i].skid.a);
268#ifdef USE_OPENAL_DOPPLER
273 skid_sound[i]->setPitch (sound_data[i].skid.f * mod_f);
322 for (
id = 0;
id<n_cars;
id++) {
325 sgVec3 u = {0, 0, 0};
326 if (sound_data->
crash) {
339 if (sound_data->
bang) {
375 alListenerf(AL_GAIN, 0.0f);
384 printf(
" #dyn sources : %d\n",
sourcepool->getNbSources());
392 alGenSources (1, source);
393 int error = alGetError();
394 if (error != AL_NO_ERROR) {
414 sgVec3 u = {0.0f, 0.0f, 0.0f};
417#ifdef USE_OPENAL_DOPPLER
422#ifdef USE_OPENAL_DOPPLER
struct WheelSoundData_ WheelSoundData
struct QSoundChar_ QSoundChar
struct QueueSoundMap_ QueueSoundMap
A queue containing mappings between sounds and sound sources.
int sortSndPriority(const void *a, const void *b)
struct SoundPri_ SoundPri
Sound priority, used to sort cars according to amplitude attenuation.
Manages the source sound of each individual car.
QSoundChar engine_backfire
float attenuation
global distance attenuation
void getCarSpeed(sgVec3 u)
TorcsSound * getEngineSound()
QSoundChar drag_collision
void getCarPosition(sgVec3 p)
virtual void muteForMenu()
virtual TorcsSound * addSample(const char *filename, int flags=(ACTIVE_VOLUME|ACTIVE_PITCH), bool loop=false, bool static_pool=true)
int OSI_MAX_STATIC_SOURCES
int n_static_sources_in_use
virtual void setNCars(int n_cars)
static const int OSI_MIN_DYNAMIC_SOURCES
Define this to use the OpenAL Doppler.
virtual float getGlobalGain()
OpenalSoundInterface(float sampling_rate, int n_channels)
virtual ~OpenalSoundInterface()
virtual void SetMaxSoundCar(CarSoundData **car_sound_data, QueueSoundMap *smap)
virtual bool getStaticSource(ALuint *source)
SharedSourcePool * sourcepool
virtual void initSharedSourcePool()
virtual void update(CarSoundData **car_sound_data, int n_cars, sgVec3 p_obs, sgVec3 u_obs, sgVec3 c_obs, sgVec3 a_obs)
SoundInterface(float sampling_rate, int n_channels)
Make a new sound interface.
TorcsSound * turbo_sound
turbo spinning sound
QueueSoundMap backfire_loop
void SortSingleQueue(CarSoundData **car_sound_data, QueueSoundMap *smap, int n_cars)
Find the max amplitude sound in car_sound_data and put it in smap.
TorcsSound * metal_skid_sound
metal skidding on metal
TorcsSound * backfire_loop_sound
exhaust backfire sound
TorcsSound * grass_skid_sound
skidding on dirt/grass
TorcsSound * axle_sound
axle/gear spinning sound
TorcsSound * skid_sound[4]
set of skid sounds, one per tyre
TorcsSound * grass_ride_sound
rolling on dirt/grass
QueueSoundMap road
The following are mappings for sound prioritisation.
TorcsSound * curb_ride_sound
rolling on curb
float sampling_rate
sampling rate
TorcsSound * crash_sound[NB_CRASH_SOUND]
list of crash sounds
TorcsSound * road_ride_sound
rolling on normal road
int n_channels
number of channels
std::vector< TorcsSound * > sound_list
keeps track of sounds used
TorcsSound * gear_change_sound
sound when changing gears
TorcsSound * bottom_crash_sound
bang when crashing from a great height
int n_engine_sounds
number of simultaneous engines
int curCrashSnd
holds current crash sound used - the sound cycles
SoundPri * engpri
the engine priority, used for sorting
TorcsSound * bang_sound
sounds when suspension fully compressed
virtual void setSource(sgVec3 p, sgVec3 u)
virtual void setPitch(float pitch)
Set the pitch.
virtual void setVolume(float vol)
Set the volume.
static CarSoundData ** car_sound_data
int id
The id of the car producing the sound, used for retrieving doppler effects etc.
QSoundChar CarSoundData::* schar
The calculated sound characteristic.
TorcsSound * snd
The raw sound.