void RaceController::createSounds() { kartChannel = 0; effectChannel = 0; musicChannel = 0; FMOD::System_Create(&system); system->setSpeakerMode(FMOD_SPEAKERMODE_STEREO); system->init(100, FMOD_INIT_NORMAL, 0); system->set3DSettings(1.0, 1.0f, 1.0f); //Initiate each sound effect here and tie it to each of the appropriate sound objects system->createSound("assets/sounds/mk64.wav", FMOD_SOFTWARE, 0, &startSound); system->createSound("assets/sounds/kartIdleFinal.wav", FMOD_SOFTWARE, 0, &idleSound); system->createSound("assets/sounds/kartAccelFinal.wav", FMOD_SOFTWARE, 0, &accelSound); system->createSound("assets/sounds/kartNewBrakeFinal.wav", FMOD_SOFTWARE, 0, &brakeSound); system->createSound("assets/sounds/kartHit.wav", FMOD_3D, 0, &bumpSound); system->createSound("assets/sounds/booster.wav", FMOD_SOFTWARE, 0, &boostSelfSound); system->createSound("assets/sounds/booster.wav", FMOD_3D, 0, &boostOthersSound); system->createSound("assets/sounds/shotsFired.wav", FMOD_3D, 0, &missileSound); system->createSound("assets/sounds/gotItem.wav", FMOD_3D, 0, &itemSound); idleSound->set3DMinMaxDistance(15.0f, 5000.0f); accelSound->set3DMinMaxDistance(15.0f, 5000.0f); bumpSound->set3DMinMaxDistance(15.0f, 5000.0f); boostOthersSound->set3DMinMaxDistance(15.0f, 5000.0f); missileSound->set3DMinMaxDistance(15.0f, 5000.0f); itemSound->set3DMinMaxDistance(15.0f, 5000.0f); } void RaceController::checkSounds() { //Iterate through each kart and determine speed and distance from camera //Speed determines sound played, closer to camera = louder sound std::vector >::const_iterator kart = scene.getKarts().begin(); for(; kart != scene.getKarts().end(); kart++) { //Item found! if ((*kart)->gotItem) { (*kart)->gotItem = false; playSound(SoundEffects::GOTITEM, (*kart)->getSpeed(), (*kart)->getPosition()); } //Boost used! if ((*kart)->boostUsed) { (*kart)->boostUsed = false; playSound(SoundEffects::BOOST, (*kart)->getSpeed(), (*kart)->getPosition()); } //Missile fired! if ((*kart)->missileFired) { (*kart)->missileFired = false; playSound(SoundEffects::MISSILEFIRED, (*kart)->getSpeed(), (*kart)->getPosition()); } //Missile hit! if ((*kart)->missileHit) { (*kart)->missileHit = false; playSound(SoundEffects::CARBUMP, (*kart)->getSpeed(), (*kart)->getPosition()); } //Only do kart sounds for our kart if (!((*kart)->getPosition() == scene.getMainPlayer()->getPosition())) { continue; } //If we aren't moving backwards we are braking if (!(*kart)->checkReverse() && brakeOn && (*kart)->getSpeed() > 3.0) { playSound(SoundEffects::BRAKE, (*kart)->getSpeed(), (*kart)->getPosition()); } //If accel, play accel sound else if ((*kart)->getAccelerating()) { playSound(SoundEffects::ENGINEACCEL, (*kart)->getSpeed(), (*kart)->getPosition()); } //Else, play idle sound else { playSound(SoundEffects::ENGINEIDLE, (*kart)->getSpeed(), (*kart)->getPosition()); } } } //Play the appropriate sound effect void RaceController::playSound(SoundEffects::soundEffect effect, float speed, glm::vec3 kartPos) { FMOD::Sound *curSound; bool isPlaying; glm::vec3 player = scene.getMainPlayer()->getPosition(); FMOD_VECTOR pos = {kartPos.x, kartPos.y, kartPos.z}; FMOD_VECTOR vel = { 0.0f, 0.0f, 0.0f }; switch (effect) { case SoundEffects::STARTRACE: musicChannel->getCurrentSound(&curSound); if (curSound != startSound) { musicChannel->stop(); Sound::SetPause(true); system->playSound(FMOD_CHANNEL_FREE, startSound, false, &musicChannel); } break; case SoundEffects::ENGINEIDLE: kartChannel->getCurrentSound(&curSound); if (curSound != idleSound) kartChannel->stop(); kartChannel->isPlaying(&isPlaying); if (!isPlaying) { kartChannel->setPaused(false); system->playSound(FMOD_CHANNEL_FREE, idleSound, false, &kartChannel); } kartChannel->set3DAttributes(&pos, &vel); break; case SoundEffects::ENGINEACCEL: kartChannel->getCurrentSound(&curSound); if (curSound != accelSound) kartChannel->stop(); kartChannel->isPlaying(&isPlaying); if (!isPlaying) { kartChannel->setPaused(false); system->playSound(FMOD_CHANNEL_FREE, accelSound, false, &kartChannel); } kartChannel->setVolume((speed + 5) / 35); kartChannel->setMode(FMOD_LOOP_NORMAL); kartChannel->set3DAttributes(&pos, &vel); break; case SoundEffects::BRAKE: kartChannel->getCurrentSound(&curSound); if (curSound != brakeSound) kartChannel->stop(); kartChannel->isPlaying(&isPlaying); if (!isPlaying) { kartChannel->setPaused(false); system->playSound(FMOD_CHANNEL_FREE, brakeSound, false, &kartChannel); kartChannel->setVolume(.7); } kartChannel->setMode(FMOD_LOOP_NORMAL); break; case SoundEffects::CARBUMP: system->playSound(FMOD_CHANNEL_FREE, bumpSound, false, &effectChannel); effectChannel->set3DAttributes(&pos, &vel); effectChannel->setPaused(false); break; case SoundEffects::BOOST: if (kartPos == player) { system->playSound(FMOD_CHANNEL_FREE, boostSelfSound, false, &effectChannel); effectChannel->setPaused(false); } else { system->playSound(FMOD_CHANNEL_FREE, boostOthersSound, false, &effectChannel); effectChannel->set3DAttributes(&pos, &vel); effectChannel->setPaused(false); } break; case SoundEffects::MISSILEFIRED: system->playSound(FMOD_CHANNEL_FREE, missileSound, false, &effectChannel); effectChannel->set3DAttributes(&pos, &vel); effectChannel->setPaused(false); break; case SoundEffects::GOTITEM: system->playSound(FMOD_CHANNEL_FREE, itemSound, false, &effectChannel); effectChannel->set3DAttributes(&pos, &vel); effectChannel->setPaused(false); break; } FMOD_VECTOR listenerpos = { player.x, player.y, player.z}; FMOD_VECTOR forward = { 0.0f, 0.0f, 1.0f }; FMOD_VECTOR up = { 0.0f, 1.0f, 0.0f }; FMOD_RESULT result = system->set3DListenerAttributes(0, &listenerpos, &vel, &forward, &up); system->update(); }