TORCS  1.3.9
The Open Racing Car Simulator
grsound.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 
3  file : grsound.cpp
4  created : Thu Aug 17 23:57:10 CEST 2000
5  copyright : (C) 2000-2024 by Eric Espie, 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 under1 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 
20 #include <math.h>
21 
22 #include <tgfclient.h>
23 #include <graphic.h>
24 #include <car.h>
25 #include <portability.h>
26 
27 #include "grsound.h"
28 #include "grmain.h"
29 #include "sound_defines.h"
30 #include "SoundInterface.h"
31 #include "CarSoundData.h"
32 
33 
34 static int soundInitialized = 0;
35 static double lastUpdated;
36 
38 static CarSoundData** car_sound_data = NULL;
39 
41 
43 
44 
45 void grInitSound(tSituation* s, int ncars)
46 {
47  const int BUFSIZE = 1024;
48  char buf[BUFSIZE];
49 
50  GfOut("-- grInitSound\n");
51 
52  // Check if we want sound (sound.xml).
53  const char *soundDisabledStr = GR_ATT_SOUND_STATE_DISABLED;
54  const char *soundOpenALStr = GR_ATT_SOUND_STATE_OPENAL;
55  const char *soundPlibStr = GR_ATT_SOUND_STATE_PLIB;
56  snprintf(buf, BUFSIZE, "%s%s", GetLocalDir(), GR_SOUND_PARM_CFG);
57  void *paramHandle = GfParmReadFile(buf, GFPARM_RMODE_REREAD | GFPARM_RMODE_CREAT);
58  const char *optionName = GfParmGetStr(paramHandle, GR_SCT_SOUND, GR_ATT_SOUND_STATE, soundOpenALStr);
59  float global_volume = GfParmGetNum(paramHandle, GR_SCT_SOUND, GR_ATT_SOUND_VOLUME, "%", 100.0f);
60  if (!strcmp(optionName, soundDisabledStr)) {
62  } else if (!strcmp(optionName, soundOpenALStr)) {
64  } else if (!strcmp(optionName, soundPlibStr)) {
66  }
67  //printf ("vol:%f\n", global_volume);
68  GfParmReleaseHandle(paramHandle);
69  paramHandle = NULL;
70 
71  lastUpdated = -1000.0;
72 
73  switch (sound_mode) {
74  case OPENAL_MODE:
75  try {
76  sound_interface = new OpenalSoundInterface (44100, 32);
77  } catch (const char* err) {
78  GfError("Disabling Sound: OpenAL initialisation failed: %s\n", err ? err : "");
80  return;
81  }
82  break;
83  case PLIB_MODE:
84  sound_interface = new PlibSoundInterface(44100, 32);
85  break;
86  case DISABLED:
87  sound_interface = 0;
88  return;
89  default:
90  GfOut (" -- Unknown sound mode %d\n", sound_mode);
91  exit(-1);
92  }
93 
94  sound_interface->setGlobalGain(global_volume/100.0f);
95 
96  car_sound_data = new CarSoundData* [ncars];
97 
98  int i;
99  for (i = 0; i<ncars; i++) {
100  void* handle = s->cars[i]->_carHandle;
101  tCarElt *car = s->cars[i];
102  const char* param;
103  FILE *file = NULL;
104 
105  // ENGINE PARAMS
106  tdble rpm_scale;
107  param = GfParmGetStr(handle, "Sound", "engine sample", "engine-1.wav");
108  rpm_scale = GfParmGetNum(handle, "Sound", "rpm scale", NULL, 1.0);
109  snprintf (buf, BUFSIZE, "cars/%s/%s", car->_carName, param);
110  file = fopen(buf, "r");
111  if (!file)
112  {
113  snprintf (buf, BUFSIZE, "data/sound/%s", param);
114  }
115  else
116  {
117  fclose(file);
118  }
119 
121  TorcsSound* engine_sound = sound_interface->addSample(buf, ACTIVE_VOLUME | ACTIVE_PITCH | ACTIVE_LP_FILTER, true, false);
122  car_sound_data[i]->setEngineSound (engine_sound, rpm_scale);
123 
124  // TURBO PARAMS
125  float default_turbo_rpm = 100.0f;//0.5f*car->_enginerpmMaxTq;
126  bool turbo_on;
127  param = GfParmGetStr(handle, SECT_ENGINE, PRM_TURBO, "false");
128  if (!strcmp(param, "true")) {
129  turbo_on = true;
130  } else {
131  if (strcmp(param, "false")) {
132  fprintf (stderr, "expected true or false, found %s\n", param);
133  }
134  turbo_on = false;
135  }
136 
137  float turbo_rpm = GfParmGetNum(handle, SECT_ENGINE, PRM_TURBO_RPM, NULL, default_turbo_rpm);
138  float turbo_lag = GfParmGetNum(handle, SECT_ENGINE, PRM_TURBO_LAG, NULL, 1.0f);
139  car_sound_data[i]->setTurboParameters (turbo_on, turbo_rpm, turbo_lag);
140  }
141 
142 
143 
144  sound_interface->setSkidSound("data/sound/skid_tyres.wav");
145  sound_interface->setRoadRideSound("data/sound/road-ride.wav");
146  sound_interface->setGrassRideSound("data/sound/out_of_road.wav");
147  sound_interface->setCurbRideSound("data/sound/curb_ride.wav");
148  sound_interface->setGrassSkidSound("data/sound/out_of_road-3.wav");
149  sound_interface->setMetalSkidSound("data/sound/skid_metal.wav");
150  sound_interface->setAxleSound("data/sound/axle.wav");
151  sound_interface->setTurboSound("data/sound/turbo1.wav");
152  sound_interface->setBackfireLoopSound("data/sound/backfire_loop.wav");
153 
154  for (i = 0; i < NB_CRASH_SOUND; i++) {
155  snprintf(buf, BUFSIZE, "data/sound/crash%d.wav", i+1);
157  }
158 
159  sound_interface->setBangSound("data/sound/boom.wav");
160  sound_interface->setBottomCrashSound("data/sound/bottom_crash.wav");
161  sound_interface->setBackfireSound("data/sound/backfire.wav");
162  sound_interface->setGearChangeSound("data/sound/gear_change1.wav");
163 
164  sound_interface->setNCars(ncars);
165  soundInitialized = 1;
166  // Must happen after all static non-shared have been allocated.
168 }
169 
170 
171 void
172 grShutdownSound(int ncars)
173 {
174  GfOut("-- grShutdownSound\n");
175 
176  if (sound_mode == DISABLED) {
177  return;
178  }
179 
180  int i;
181  for (i = 0; i < ncars; i++) {
182  delete car_sound_data[i];
183  }
184 
185  delete [] car_sound_data;
186 
187  if (!soundInitialized) {
188  return;
189  }
190  soundInitialized = 0;
191 
192  delete sound_interface;
193  sound_interface = 0;
194 
195  if (__slPendingError) {
196  GfOut("!!! error ignored: %s\n", __slPendingError);
197  __slPendingError = 0; /* ARG!!! ugly ugly bad thing... but should not occur anymore now */
198  }
199 }
200 
201 
202 
203 float
205 {
206  if (sound_mode == DISABLED) {
207  return 0.0f;
208  }
209 
210  // Update sound at most 100 times a second.
211  const double UPDATE_DT = 0.01;
212  if (s->currentTime - lastUpdated < UPDATE_DT) {
213  return 0.0f;
214  }
216 
217  tCarElt *car;//= s->cars[s->current];
218 
219  // TODO: Fix for a lot of cars. I guess in this implementation we can change the Update() call to have _ncars = 1?
220 
221  // TODO: Just consider cars near the camera, doing computations just for them?
222 
223  if (camera) {
224  sgVec3* p_camera = camera->getPosv();
225  sgVec3* u_camera = camera->getSpeedv();
226  sgVec3 c_camera;
227  sgVec3* a_camera = camera->getUpv();
228  int i;
229  for (i = 0; i<3; i++) {
230  c_camera[i] = (*camera->getCenterv())[i] - (*camera->getPosv())[i];
231  }
232 
233  //sgNormaliseVec3 (c_camera);
234 
235 
236  for (i = 0; i < s->_ncars; i++) {
237  car = s->cars[i];
238  car_sound_data[car->index]->setListenerPosition(*p_camera);
239  car_sound_data[car->index]->update(car);
240  }
241 
242  sound_interface->update (car_sound_data, s->_ncars, *p_camera, *u_camera, c_camera, *a_camera);
243 
244 
245  }
246  return 0.0f;
247 }
248 
249 
250 void grMuteForMenu(void)
251 {
252  if (sound_interface != 0 && sound_mode != DISABLED) {
254  }
255 
256 }
void setBackfireSound(const char *sound_name)
#define PRM_TURBO
Definition: car.h:595
#define PRM_TURBO_RPM
Definition: car.h:596
#define GfError
Definition: tgf.h:351
void setRoadRideSound(const char *sound_name)
void setListenerPosition(sgVec3 p)
Definition: CarSoundData.h:133
double currentTime
current time in sec since the beginning of the simulation
Definition: raceman.h:88
void * GfParmReadFile(const char *file, int mode)
Read parameter set from file and return handle to parameter set.
Definition: params.cpp:1157
cars situation used to inform the GUI and the drivers
Definition: raceman.h:85
void setSkidSound(const char *sound_name)
static CarSoundData ** car_sound_data
Definition: grsound.cpp:38
virtual void update(CarSoundData **car_sound_data, int n_cars, sgVec3 p_obs, sgVec3 u_obs, sgVec3 c_obs=NULL, sgVec3 a_obs=NULL)
Update sound for a given observer.
tCarElt ** cars
list of cars
Definition: raceman.h:90
void setAxleSound(const char *sound_name)
void grMuteForMenu(void)
Definition: grsound.cpp:250
sgVec3 * getSpeedv(void)
Definition: grcam.h:101
Car structure (tCarElt).
Definition: car.h:455
char * GetLocalDir(void)
Definition: tgf.cpp:231
void GfParmReleaseHandle(void *parmHandle)
Release given parameter set handle parmHandle.
Definition: params.cpp:1834
virtual TorcsSound * addSample(const char *filename, int flags=(ACTIVE_VOLUME|ACTIVE_PITCH), bool loop=false, bool static_pool=true)=0
Add a new sample - must be defined in children classes.
void grInitSound(tSituation *s, int ncars)
Definition: grsound.cpp:45
#define GR_SCT_SOUND
Definition: graphic.h:45
void grShutdownSound(int ncars)
Definition: grsound.cpp:172
void setEngineSound(TorcsSound *engine_sound, float rpm_scale)
void setBangSound(const char *sound_name)
#define ACTIVE_LP_FILTER
Definition: sound_defines.h:16
Manages the source sound of each individual car.
Definition: CarSoundData.h:25
void setGrassSkidSound(const char *sound_name)
void setTurboSound(const char *sound_name)
void setBackfireLoopSound(const char *sound_name)
void setCurbRideSound(const char *sound_name)
#define GR_ATT_SOUND_STATE_DISABLED
Definition: graphic.h:49
const char * GfParmGetStr(void *parmHandle, const char *path, const char *key, const char *deflt)
Get a string parameter from the parameter set handle.
Definition: params.cpp:2311
static double lastUpdated
Definition: grsound.cpp:35
The Gaming Framework API (client part).
void setMetalSkidSound(const char *sound_name)
float tdble
Floating point type used in TORCS.
Definition: tgf.h:48
#define GR_SOUND_PARM_CFG
Definition: graphic.h:44
#define GfOut
Definition: tgf.h:373
#define GFPARM_RMODE_REREAD
reread the parameters from file and release the previous ones
Definition: tgf.h:266
#define ACTIVE_VOLUME
Definition: sound_defines.h:14
Graphic Module Interface Definition.
float grRefreshSound(tSituation *s, cGrCamera *camera)
Definition: grsound.cpp:204
#define GR_ATT_SOUND_VOLUME
Definition: graphic.h:50
Parameter header structure, a parameter can either carry a numeric or a string value, numeric value is constraint by min and max, string value by options in within.
Definition: params.cpp:63
void setBottomCrashSound(const char *sound_name)
This is the car structure.
int index
car index
Definition: car.h:457
virtual void setGlobalGain(float g)
void setGearChangeSound(const char *sound_name)
#define NB_CRASH_SOUND
Definition: sound_defines.h:11
virtual void initSharedSourcePool()
initialised the pool of shared sources
#define SECT_ENGINE
Definition: car.h:500
void setCrashSound(const char *sound_name, int index)
static SoundInterface * sound_interface
Definition: grsound.cpp:37
sgVec3 * getPosv(void)
Definition: grcam.h:98
tdble GfParmGetNum(void *handle, const char *path, const char *key, const char *unit, tdble deflt)
Get a numerical parameter from the parameter set handle.
Definition: params.cpp:2392
void setTurboParameters(bool turbo_on, float turbo_rpm, float turbo_lag)
Sound interface.
#define GR_ATT_SOUND_STATE_OPENAL
Definition: graphic.h:48
#define GR_ATT_SOUND_STATE
Definition: graphic.h:46
SoundMode
Definition: grsound.cpp:40
void setGrassRideSound(const char *sound_name)
void update(tCarElt *car)
#define ACTIVE_PITCH
Definition: sound_defines.h:15
virtual void muteForMenu()
static int soundInitialized
Definition: grsound.cpp:34
sgVec3 * getUpv(void)
Definition: grcam.h:122
virtual void setNCars(int n_cars)=0
Set the number of cars - must be defined in children classes.
#define GR_ATT_SOUND_STATE_PLIB
Definition: graphic.h:47
#define GFPARM_RMODE_CREAT
Create the file if doesn&#39;t exist.
Definition: tgf.h:267
A generic TORCS sound.
Definition: TorcsSound.h:53
sgVec3 * getCenterv(void)
Definition: grcam.h:111
static enum SoundMode sound_mode
Definition: grsound.cpp:42
#define PRM_TURBO_LAG
Definition: car.h:598
PLIB interface.