Sound::Sound(Sounds id){ FMOD_RESULT result; result = FMOD::System_Create(&fmodSystem); FmodErrorCheck(result); result = fmodSystem->init(32, FMOD_INIT_NORMAL, 0); FmodErrorCheck(result); this->id = id; switch(id){ case Sounds::ROCKET_LAUCHED: result = fmodSystem->createSound("../data/sounds/launch_rocket.wav", FMOD_SOFTWARE, 0, &actionSound); FmodErrorCheck(result); break; case Sounds::MUSIC: result = fmodSystem->createStream("../data/sounds/music.mp3", FMOD_SOFTWARE , 0, &actionSound); FmodErrorCheck(result); break; case Sounds::MUSIC2: result = fmodSystem->createStream("../data/sounds/music2.mp3", FMOD_SOFTWARE , 0, &actionSound); FmodErrorCheck(result); break; case Sounds::MUSIC3: result = fmodSystem->createStream("../data/sounds/music3.mp3", FMOD_SOFTWARE, 0, &actionSound); FmodErrorCheck(result); break; case Sounds::MUSIC4: result = fmodSystem->createStream("../data/sounds/music4.mp3", FMOD_SOFTWARE, 0, &actionSound); FmodErrorCheck(result); break; case Sounds::MUSIC5: result = fmodSystem->createStream("../data/sounds/music5.mp3", FMOD_SOFTWARE, 0, &actionSound); FmodErrorCheck(result); break; case Sounds::MUSIC6: result = fmodSystem->createStream("../data/sounds/music6.mp3", FMOD_SOFTWARE, 0, &actionSound); FmodErrorCheck(result); break; case Sounds::EXPLOSION: result = fmodSystem->createSound("../data/sounds/explosion.wav", FMOD_SOFTWARE, 0, &actionSound); FmodErrorCheck(result); break; } }
// Play an event sound bool CAudio::PlayEventSound() { result = m_FmodSystem->playSound(m_eventSound, NULL, false, NULL); FmodErrorCheck(result); if (result != FMOD_OK) return false; return true; }
bool CAudio::Initialise() { // Create an FMOD system result = FMOD::System_Create(&m_FmodSystem); FmodErrorCheck(result); if (result != FMOD_OK) return false; // Initialise the system result = m_FmodSystem->init(32, FMOD_INIT_NORMAL, 0); FmodErrorCheck(result); if (result != FMOD_OK) return false; return true; }
// Play a music stream bool CAudio::PlayMusicStream() { result = m_FmodSystem->playSound(m_music, NULL, false, &m_musicChannel); FmodErrorCheck(result); if (result != FMOD_OK) return false; return true; }
// trackManager serves as cache for sound clips void createSound(char *filePath, FMOD::Sound **sound) { if (trackManager.find(filePath) == trackManager.end()) { FmodErrorCheck(lowLevelSystem->createSound(filePath, FMOD_3D|FMOD_LOOP_NORMAL, 0, sound)); trackManager.insert({ filePath, *sound }); } else *sound = trackManager.find(filePath)->second; }
// Load an event sound bool CAudio::LoadEventSound(char *filename) { result = m_FmodSystem->createSound(filename, NULL, 0, &m_eventSound); FmodErrorCheck(result); if (result != FMOD_OK) return false; return true; }
// Load a music stream bool CAudio::LoadMusicStream(char *filename) { result = m_FmodSystem->createStream(filename, NULL | FMOD_LOOP_NORMAL, 0, &m_music); FmodErrorCheck(result); if (result != FMOD_OK) return false; return true; }
void Sound::Play(){ FMOD_RESULT result; result = fmodSystem->playSound(FMOD_CHANNEL_FREE, actionSound, false, &channel ); if (id==Sounds::ROCKET_LAUCHED){ channel->setVolume(0.1f); }else if (id==Sounds::EXPLOSION){ channel->setVolume(0.2f); } else if (id==Sounds::MUSIC || id==Sounds::MUSIC2 || id==Sounds::MUSIC3){ //music_channel->setVolume(0.4); } else if (id==Sounds::MUSIC6){ channel->setVolume(0.5); } FmodErrorCheck(result); }
bool init() { // init studio system FmodErrorCheck(FMOD::Studio::System::create(&system)); // init lowlevel system FmodErrorCheck(system->getLowLevelSystem(&lowLevelSystem)); int sampleRate = 0; FmodErrorCheck(lowLevelSystem->getSoftwareFormat(&sampleRate, NULL, NULL)); unsigned int bufferSize = 0; FmodErrorCheck(lowLevelSystem->getDSPBufferSize(&bufferSize, NULL)); FmodErrorCheck(lowLevelSystem->setSoftwareFormat(sampleRate, FMOD_SPEAKERMODE_SURROUND, 2)); // 3D FmodErrorCheck(system->initialize(1000, FMOD_STUDIO_INIT_NORMAL, FMOD_INIT_NORMAL | FMOD_INIT_3D_RIGHTHANDED, NULL)); // set distance units const float DISTANCEFACTOR = 1.0f; // Units per meter. I.e feet would = 3.28. centimeters would = 100. lowLevelSystem->set3DSettings(1.0f, DISTANCEFACTOR, 1.0f); return true; }
bool destroy() { FmodErrorCheck(lowLevelSystem->close()); FmodErrorCheck(lowLevelSystem->release()); return true; }
bool update() { FmodErrorCheck(lowLevelSystem->update()); return true; }
void setListenerPosition(FMOD_VECTOR position, FMOD_VECTOR velocity, FMOD_VECTOR foward, FMOD_VECTOR up) { FmodErrorCheck(lowLevelSystem->set3DListenerAttributes(0, &position, &velocity, &foward, &up)); }
void setChannelPosition(FMOD_VECTOR position, FMOD_VECTOR velocity, FMOD::Channel *channel) { FmodErrorCheck(channel->set3DAttributes(&position, &velocity)); }
void playSound(FMOD::Sound *sound, FMOD::Channel **channel) { FmodErrorCheck(lowLevelSystem->playSound(sound, 0, false, channel)); }