TORCS  1.3.10
The Open Racing Car Simulator
Loading...
Searching...
No Matches
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
34static int soundInitialized = 0;
35static double lastUpdated;
36
39
41
43
44
45void 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:
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);
156 sound_interface->setCrashSound(buf, i);
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);
166 // Must happen after all static non-shared have been allocated.
167 sound_interface->initSharedSourcePool();
168}
169
170
171void
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 }
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
203float
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->getWorldPosv();
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
251{
252 if (sound_interface != 0 && sound_mode != DISABLED) {
253 sound_interface->muteForMenu();
254 }
255
256}
#define PRM_TURBO_RPM
Definition car.h:600
#define PRM_TURBO
Definition car.h:599
struct CarElt tCarElt
Car structure (tCarElt).
#define PRM_TURBO_LAG
Definition car.h:602
#define SECT_ENGINE
Definition car.h:504
Manages the source sound of each individual car.
PLIB interface.
Sound interface.
A generic TORCS sound.
Definition TorcsSound.h:53
sgVec3 * getUpv(void)
Definition grcam.h:136
sgVec3 * getSpeedv(void)
Definition grcam.h:115
sgVec3 * getCenterv(void)
Definition grcam.h:125
sgVec3 * getPosv(void)
Definition grcam.h:104
sgVec3 * getWorldPosv(void)
Definition grcam.h:107
#define GR_ATT_SOUND_STATE_DISABLED
Definition graphic.h:47
#define GR_ATT_SOUND_VOLUME
Definition graphic.h:48
#define GR_SCT_SOUND
Definition graphic.h:43
#define GR_ATT_SOUND_STATE_OPENAL
Definition graphic.h:46
#define GR_SOUND_PARM_CFG
Definition graphic.h:42
#define GR_ATT_SOUND_STATE_PLIB
Definition graphic.h:45
#define GR_ATT_SOUND_STATE
Definition graphic.h:44
float tdble
Floating point type used in TORCS.
Definition tgf.h:47
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:2453
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:2534
void GfParmReleaseHandle(void *parmHandle)
Release given parameter set handle parmHandle.
Definition params.cpp:1963
void * GfParmReadFile(const char *file, int mode)
Read parameter set from file and return handle to parameter set.
Definition params.cpp:1173
SoundMode
Definition grsound.cpp:40
@ DISABLED
Definition grsound.cpp:40
@ PLIB_MODE
Definition grsound.cpp:40
@ OPENAL_MODE
Definition grsound.cpp:40
static enum SoundMode sound_mode
Definition grsound.cpp:42
static SoundInterface * sound_interface
Definition grsound.cpp:37
float grRefreshSound(tSituation *s, cGrCamera *camera)
Definition grsound.cpp:204
void grInitSound(tSituation *s, int ncars)
Definition grsound.cpp:45
static int soundInitialized
Definition grsound.cpp:34
static CarSoundData ** car_sound_data
Definition grsound.cpp:38
void grMuteForMenu(void)
Definition grsound.cpp:250
void grShutdownSound(int ncars)
Definition grsound.cpp:172
static double lastUpdated
Definition grsound.cpp:35
struct Situation tSituation
cars situation used to inform the GUI and the drivers
#define ACTIVE_LP_FILTER
#define ACTIVE_PITCH
#define NB_CRASH_SOUND
#define ACTIVE_VOLUME
int index
car index
Definition car.h:459
tCarElt ** cars
list of cars
Definition raceman.h:89
double currentTime
current time in sec since the beginning of the simulation
Definition raceman.h:87
Parameter header structure, a parameter can either carry a numeric or a string value,...
Definition params.cpp:63
char * GetLocalDir(void)
Definition tgf.cpp:230
#define GfOut
Definition tgf.h:372
#define GFPARM_RMODE_CREAT
Allow an empty in-memory set on file-open failure; does not write a file.
Definition tgf.h:266
#define GFPARM_RMODE_REREAD
Clear shared cached data and reread the file.
Definition tgf.h:265
#define GfError
Definition tgf.h:350