TORCS  1.3.9
The Open Racing Car Simulator
SoundInterface.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 
3  file : PlibSoundInterface.cpp
4  created : Thu Apr 7 04:21 CEST 2005
5  copyright : (C) 2005-2024 Christos Dimitrakakis, Bernhard Wymann
6  email : berniw@bluewin.ch
7 
8 ***************************************************************************/
9 
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 #include "SoundInterface.h"
20 #include "CarSoundData.h"
21 
22 SoundInterface::SoundInterface(float sampling_rate, int n_channels)
23 {
24  this->sampling_rate = sampling_rate;
25  this->n_channels = n_channels;
26  int i;
27  for (i = 0; i<4; i++) {
28  skid_sound[i]=NULL;
29  }
30  road_ride_sound=NULL;
31  grass_ride_sound=NULL;
32  grass_skid_sound=NULL;
33  curb_ride_sound =NULL;
34  metal_skid_sound=NULL;
35  axle_sound=NULL;
36  turbo_sound=NULL;
38  for (i = 0; i<NB_CRASH_SOUND; i++) {
39  crash_sound[i]=NULL;
40  }
41  curCrashSnd = 0;
42  bang_sound=NULL;
43  bottom_crash_sound=NULL;
44  gear_change_sound=NULL;
45 
47 
48  int MAX_N_ENGINE_SOUNDS = 8;
49  if (n_engine_sounds<1) {
50  n_engine_sounds = 1;
51  fprintf (stderr, "Warning: maybe insufficient channels\n");
52  } else if (n_engine_sounds > MAX_N_ENGINE_SOUNDS) {
53  n_engine_sounds = MAX_N_ENGINE_SOUNDS;
54  }
55 }
56 
58 {
59  float max_vol = 0.0f;
60  int max_id = 0;
61  for (int id=0; id<n_cars; id++) {
62  CarSoundData* sound_data = car_sound_data[id];
63  QSoundChar CarSoundData::*p2schar = smap->schar;
64  QSoundChar* schar = &(sound_data->*p2schar);
65  float vol = sound_data->attenuation * schar->a;
66  if (vol > max_vol) {
67  max_vol = vol;
68  max_id = id;
69  }
70  }
71  smap->id = max_id;
72  smap->max_vol = max_vol;
73 }
74 
76 {
77  int id = smap->id;
78  float max_vol = smap->max_vol;
79  QSoundChar CarSoundData::*p2schar = smap->schar;
80  QSoundChar* schar = &(car_sound_data[id]->*p2schar);
81  TorcsSound* snd = smap->snd;
82 
83  sgVec3 p;
84  sgVec3 u;
85 
88  snd->setSource (p, u);
89  snd->setVolume (schar->a);
90  snd->setPitch (schar->f);
91  snd->update();
92  if (max_vol > VOLUME_CUTOFF) {
93  snd->start();
94  } else {
95  snd->stop();
96  }
97 }
static CarSoundData ** car_sound_data
Definition: grsound.cpp:38
TorcsSound * bang_sound
sounds when suspension fully compressed
int curCrashSnd
holds current crash sound used - the sound cycles
virtual void setPitch(float pitch)
Set the pitch.
Definition: TorcsSound.cpp:30
A queue containing mappings between sounds and sound sources.
virtual void stop()=0
TorcsSound * bottom_crash_sound
bang when crashing from a great height
float f
Definition: QSoundChar.h:7
QSoundChar CarSoundData::* schar
The calculated sound characteristic.
TorcsSound * curb_ride_sound
rolling on curb
Manages the source sound of each individual car.
Definition: CarSoundData.h:25
TorcsSound * backfire_loop_sound
exhaust backfire sound
void SetMaxSoundCar(CarSoundData **car_sound_data, QueueSoundMap *smap)
Using the smap->id, get the appropriate entry in car_sound_data and call apprioriate methods for smap...
TorcsSound * grass_skid_sound
skidding on dirt/grass
TorcsSound * turbo_sound
turbo spinning sound
TorcsSound * road_ride_sound
rolling on normal road
float a
Definition: QSoundChar.h:6
virtual void setSource(sgVec3 p, sgVec3 u)
Definition: TorcsSound.h:76
float attenuation
global distance attenuation
Definition: CarSoundData.h:85
int n_channels
number of channels
TorcsSound * snd
The raw sound.
virtual void setVolume(float vol)
Set the volume.
Definition: TorcsSound.cpp:25
virtual void update()=0
static Point p[4]
Definition: Convex.cpp:54
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.
void getCarPosition(sgVec3 p)
Definition: CarSoundData.h:121
float sampling_rate
sampling rate
TorcsSound * gear_change_sound
sound when changing gears
#define NB_CRASH_SOUND
Definition: sound_defines.h:11
int n_engine_sounds
number of simultaneous engines
virtual void start()=0
void getCarSpeed(sgVec3 u)
Definition: CarSoundData.h:127
SoundInterface(float sampling_rate, int n_channels)
Make a new sound interface.
TorcsSound * crash_sound[NB_CRASH_SOUND]
list of crash sounds
TorcsSound * metal_skid_sound
metal skidding on metal
TorcsSound * grass_ride_sound
rolling on dirt/grass
#define VOLUME_CUTOFF
float max_vol
Max.
TorcsSound * axle_sound
axle/gear spinning sound
TorcsSound * skid_sound[4]
set of skid sounds, one per tyre
A generic TORCS sound.
Definition: TorcsSound.h:53
int id
The id of the car producing the sound, used for retrieving doppler effects etc.