Sound* PlibSoundInterface::addSample(const char* filename, int flags, bool loop, bool static_pool)
{
	PlibSound* sound = new PlibSound(sched, filename, flags, loop);
    sound->setVolume(getGlobalGain());
	sound_list.push_back ((Sound*) sound);
	return sound;
}
Beispiel #2
0
float Camera::getGlobalGain(int whichSide)
{
	float globalGain = -1.0;
#ifdef _WIN32
	eSPAEAWB_GetGlobalGain(whichSide, &globalGain);
#elif __APPLE__
    globalGain = getGlobalGain(whichSide);
#endif
    return globalGain;
}
void PlibSoundInterface::setMaxSoundCar(CarSoundData** car_sound_data, QueueSoundMap* smap)
{
	int id = smap->id;
	//float max_vol = smap->max_vol;
	QSoundChar CarSoundData::*p2schar = smap->schar;
	QSoundChar* schar = &(car_sound_data[id]->*p2schar);
	Sound* snd = smap->snd;

	snd->setVolume(0.5f*getGlobalGain() * schar->a * car_src[id].a);
	snd->setPitch(schar->f * car_src[id].f);
	snd->update();
#if 0
	if (max_vol > VOLUME_CUTOFF) {
		snd->start();
	} else {
		snd->stop();
	}
#endif
}
void PlibSoundInterface::update(CarSoundData** car_sound_data, int n_cars, sgVec3 p_obs, sgVec3 u_obs, sgVec3 c_obs, sgVec3 a_obs)
{
	// Copy car ID basically.
	int i;
	for (i = 0; i<n_cars; i++) {
		car_sound_data[i]->copyEngPri(engpri[i]);
	}

	for (i = 0; i<n_cars; i++) {
		int id = engpri[i].id;
		sgVec3 p;
		sgVec3 u;
		car_sound_data[id]->getCarPosition(p);
		car_sound_data[id]->getCarSpeed(u);
		car_src[id].setSource (p, u);
		car_src[id].setListener (p_obs, u_obs);
		car_src[id].update();
		engpri[id].a = car_src[id].a;
	}

	qsort((void*) engpri, n_cars, sizeof(SoundPri), &sortSndPriority);

	for (i = 0; i<n_cars; i++) {
		int id = engpri[i].id;
		Sound* engine = car_sound_data[id]->getEngineSound();
		if (i>=NB_ENGINE_SOUND) {
			engine->setVolume (0.0f);
			engine->pause();
		} else {
			engine->resume();
			engine->setLPFilter(car_src[id].lp*car_sound_data[id]->engine.lp);
			engine->setPitch(car_src[id].f*car_sound_data[id]->engine.f);
			engine->setVolume(0.5f*getGlobalGain()*car_src[id].a*car_sound_data[id]->engine.a);
			engine->update();
		}
	}

	float max_skid_vol[4] = {0.0f, 0.0f, 0.0f, 0.0f};
	int max_skid_id[4] = {0,0,0,0};
	int id;
	for (id = 0; id<n_cars; id++) {
		CarSoundData* sound_data = car_sound_data[id];
		for (int j=0; j<4; j++) {
			float skvol=sound_data->attenuation*sound_data->wheel[j].skid.a;
			if (skvol > max_skid_vol[j]) {
				max_skid_vol[j] = skvol;
				max_skid_id[j] = id;
			}
		}
	}

	for (i = 0; i<4; i++) {
		int id = max_skid_id[i];
		WheelSoundData* sound_data = car_sound_data[id]->wheel;
		float mod_a = car_src[id].a;
		float mod_f = car_src[id].f;
		skid_sound[i]->setVolume(0.5f*getGlobalGain()*sound_data[i].skid.a * mod_a);
		skid_sound[i]->setPitch(sound_data[i].skid.f * mod_f);
		skid_sound[i]->update();
#if 0
		if (sound_data[i].skid.a > VOLUME_CUTOFF) {
			skid_sound[i]->start();
		} else {
			skid_sound[i]->stop();
		}
#endif
	}

	
	// other looping sounds
	road.snd = road_ride_sound;
	sortSingleQueue (car_sound_data, &road, n_cars);
	setMaxSoundCar (car_sound_data, &road);

	grass.snd = grass_ride_sound;
	sortSingleQueue (car_sound_data, &grass, n_cars);
	setMaxSoundCar (car_sound_data, &grass);

	grass_skid.snd = grass_skid_sound;
	sortSingleQueue (car_sound_data, &grass_skid, n_cars);
	setMaxSoundCar (car_sound_data, &grass_skid);

	metal_skid.snd = metal_skid_sound;
	sortSingleQueue (car_sound_data, &metal_skid, n_cars);
	setMaxSoundCar (car_sound_data, &metal_skid);

	backfire_loop.snd = backfire_loop_sound;
	sortSingleQueue (car_sound_data, &backfire_loop, n_cars);
	setMaxSoundCar (car_sound_data, &backfire_loop);

	turbo.snd = turbo_sound;
	sortSingleQueue (car_sound_data, &turbo, n_cars);
	setMaxSoundCar (car_sound_data, &turbo);

	axle.snd = axle_sound;
	sortSingleQueue (car_sound_data, &axle, n_cars);
	setMaxSoundCar (car_sound_data, &axle);


	// One-off sounds
	for (id = 0; id<n_cars; id++) {
		float crash_threshold = 0.5f;
		float gear_threshold = 0.75;

		CarSoundData* sound_data = car_sound_data[id];

		if (sound_data->crash) {
			if (++curCrashSnd>=NB_CRASH_SOUND) {
				
				curCrashSnd = 0;
			}
			if (car_src[id].a > crash_threshold) {
				crash_sound[curCrashSnd]->start();
			}
		}

		if (sound_data->bang) {
			if (car_src[id].a > crash_threshold) {
				bang_sound->start();
			}
		}

		if (sound_data->bottom_crash) {
			if (car_src[id].a > crash_threshold) {
				bottom_crash_sound->start();
			}
		}

		if (sound_data->gear_changing) {
			if (car_src[id].a > gear_threshold) {
				gear_change_sound->start();
			}
		}
	}


	sched->update();
}
void OpenalSoundInterface::update(CarSoundData** car_sound_data, int n_cars, sgVec3 p_obs, sgVec3 u_obs, sgVec3 c_obs, sgVec3 a_obs)
{
	
	ALfloat listener_pos[3];
	//ALfloat listener_speed[3];
	ALfloat listener_orientation[6];
    ALfloat zeros[] = {0.0f, 0.0f, 0.0f};
	
	int i;
	for (i = 0; i<3; i++) {
		listener_pos[i] = p_obs[i];
		//listener_speed[i] = 0;// u_obs[i];
		listener_orientation[i] = c_obs[i];
		listener_orientation[i+3] = a_obs[i];
	}
	
	alListenerfv(AL_POSITION, listener_pos );
#ifdef USE_OPENAL_DOPPLER
	alListenerfv(AL_VELOCITY, listener_speed );
#else 
    alListenerfv(AL_VELOCITY, zeros);
#endif
	alListenerfv(AL_ORIENTATION, listener_orientation );
	alListenerf(AL_GAIN, getGlobalGain());

	for (i = 0; i<n_cars; i++) {
		car_sound_data[i]->copyEngPri(engpri[i]);
		int id = engpri[i].id;
		sgVec3 p;
		sgVec3 u;
		car_sound_data[id]->getCarPosition(p);
		car_sound_data[id]->getCarSpeed(u);
		car_src[id].setSource (p, u);
		car_src[id].setListener (p_obs, u_obs);
		car_src[id].update();
		engpri[id].a = car_src[id].a;
	}


	qsort ((void*) engpri, n_cars, sizeof(SoundPri), &sortSndPriority);


	int nsrc = MIN(sourcepool->getNbSources(), n_engine_sounds);

	// Reverse order is important to gain free sources from stopped engine sounds
	// before attempting to start new ones.
	for (i = n_cars - 1; i >= 0; i--) {
		int id = engpri[i].id;
		sgVec3 p;
		sgVec3 u;
		CarSoundData* sound_data = car_sound_data[id];
		sound_data->getCarPosition(p);
		sound_data->getCarSpeed(u);
		TorcsSound* engine = sound_data->getEngineSound();
		engine->setSource(p, u);
#ifdef USE_OPENAL_DOPPLER
		engine->setPitch (sound_data->engine.f);
#else
		engine->setPitch (car_src[id].f*sound_data->engine.f);
#endif
		engine->setVolume (sound_data->engine.a * 1.5f * exp(sound_data->engine.lp-1.0));
		//engine->setLPFilter(sound_data->engine.lp);

		engine->update();
		if (i < nsrc) {
			engine->start();
		} else {
			engine->stop();
		}
	}
	
	float max_skid_vol[4] = {0.0f, 0.0f, 0.0f, 0.0f};
	int max_skid_id[4] = {0,0,0,0};
	int id;
	for (id = 0; id<n_cars; id++) {
		CarSoundData* sound_data = car_sound_data[id];
		for (int j=0; j<4; j++) {
			float skvol=sound_data->attenuation*sound_data->wheel[j].skid.a;
			if (skvol > max_skid_vol[j]) {
				max_skid_vol[j] = skvol;
				max_skid_id[j] = id;
			}
		}
	}

	for (i = 0; i<4; i++) {
		int id = max_skid_id[i];
		WheelSoundData* sound_data = car_sound_data[id]->wheel;
		skid_sound[i]->setSource (sound_data[i].p, sound_data[i].u);
		skid_sound[i]->setVolume (sound_data[i].skid.a);
#ifdef USE_OPENAL_DOPPLER
        /// \note Why MIN() here?
		skid_sound[i]->setPitch (MIN(sound_data[i].skid.f, 1.0f));
#else
		float mod_f = car_src[id].f;
		skid_sound[i]->setPitch (sound_data[i].skid.f * mod_f);
#endif
		skid_sound[i]->update();
		if (sound_data[i].skid.a > VOLUME_CUTOFF) {
			skid_sound[i]->start();
		} else {
			skid_sound[i]->stop();
		}
	}

	
	// other looping sounds
	road.snd = road_ride_sound;
	SortSingleQueue (car_sound_data, &road, n_cars);
	SetMaxSoundCar (car_sound_data, &road);

	grass.snd = grass_ride_sound;
	SortSingleQueue (car_sound_data, &grass, n_cars);
	SetMaxSoundCar (car_sound_data, &grass);

	grass_skid.snd = grass_skid_sound;
	SortSingleQueue (car_sound_data, &grass_skid, n_cars);
	SetMaxSoundCar (car_sound_data, &grass_skid);

	metal_skid.snd = metal_skid_sound;
	SortSingleQueue (car_sound_data, &metal_skid, n_cars);
	SetMaxSoundCar (car_sound_data, &metal_skid);

	backfire_loop.snd = backfire_loop_sound;
	SortSingleQueue (car_sound_data, &backfire_loop, n_cars);
	SetMaxSoundCar (car_sound_data, &backfire_loop);

	backfire_loop.snd = backfire_loop_sound;
	SortSingleQueue (car_sound_data, &backfire_loop, n_cars);
	SetMaxSoundCar (car_sound_data, &backfire_loop);

	turbo.snd = turbo_sound;
	SortSingleQueue (car_sound_data, &turbo, n_cars);
	SetMaxSoundCar (car_sound_data, &turbo);

	axle.snd = axle_sound;
	SortSingleQueue (car_sound_data, &axle, n_cars);
	SetMaxSoundCar (car_sound_data, &axle);

	// One-off sounds
	for (id = 0; id<n_cars; id++) {
		CarSoundData* sound_data = car_sound_data[id];
		sgVec3 p;
		sgVec3 u = {0, 0, 0};
		if (sound_data->crash) {
			if (++curCrashSnd>=NB_CRASH_SOUND) {
				curCrashSnd = 0;
			}
			sound_data->getCarPosition(p);
			sound_data->getCarSpeed(u);
			crash_sound[curCrashSnd]->setSource (p, u);
			crash_sound[curCrashSnd]->setVolume (1.0f);
			crash_sound[curCrashSnd]->setPitch (1.0f);
			crash_sound[curCrashSnd]->update();
			crash_sound[curCrashSnd]->start();
		}

		if (sound_data->bang) {
			sound_data->getCarPosition(p);
			sound_data->getCarSpeed(u);
			bang_sound->setSource (p, u);
			bang_sound->setVolume (1.0f);
			bang_sound->setPitch (1.0f);
			bang_sound->update();
			bang_sound->start();
		}

		if (sound_data->bottom_crash) {
			sound_data->getCarPosition(p);
			sound_data->getCarSpeed(u);
			bottom_crash_sound->setSource (p, u);
			bottom_crash_sound->setVolume (1.0f);
			bottom_crash_sound->setPitch (1.0f);
			bottom_crash_sound->update();
			bottom_crash_sound->start();
		}

		if (sound_data->gear_changing) {
			sound_data->getCarPosition(p);
			sound_data->getCarSpeed(u);
			gear_change_sound->setSource (p, u);
			gear_change_sound->setReferenceDistance (1.0f);
			gear_change_sound->setVolume (1.0f);
			gear_change_sound->setPitch (1.0f);
			gear_change_sound->update();
			gear_change_sound->start();
		}
	}

}