Revision: 2510 http://rigsofrods.svn.sourceforge.net/rigsofrods/?rev=2510&view=rev Author: ulteq Date: 2012-05-06 06:26:27 +0000 (Sun, 06 May 2012) Log Message: ----------- style++
Modified Paths: -------------- trunk/source/main/audio/Sound.cpp trunk/source/main/audio/Sound.h trunk/source/main/audio/SoundManager.cpp trunk/source/main/audio/SoundManager.h trunk/source/main/audio/SoundScriptManager.cpp trunk/source/main/audio/SoundScriptManager.h Modified: trunk/source/main/audio/Sound.cpp =================================================================== --- trunk/source/main/audio/Sound.cpp 2012-05-06 03:58:36 UTC (rev 2509) +++ trunk/source/main/audio/Sound.cpp 2012-05-06 06:26:27 UTC (rev 2510) @@ -26,8 +26,8 @@ Sound::Sound(ALuint buffer, SoundManager *soundManager, int sourceIndex) : buffer(buffer) - , soundManager(soundManager) - , sourceIndex(sourceIndex) + , sound_manager(soundManager) + , source_index(sourceIndex) , audibility(0.0f) , gain(0.0f) , pitch(1.0f) @@ -53,7 +53,7 @@ if (!loop && should_play && hardware_index != -1) { int value = 0; - alGetSourcei((ALuint)soundManager->getHardwareSource(hardware_index), AL_SOURCE_STATE, &value); + alGetSourcei((ALuint)sound_manager->getHardwareSource(hardware_index), AL_SOURCE_STATE, &value); if (value != AL_PLAYING) { should_play = false; @@ -69,15 +69,15 @@ float distance = (pos - position).length(); - if (distance > soundManager->MAX_DISTANCE) + if (distance > sound_manager->MAX_DISTANCE) { audibility = 0.0f; - } else if (distance < soundManager->REFERENCE_DISTANCE) + } else if (distance < sound_manager->REFERENCE_DISTANCE) { audibility = gain; } else { - audibility = gain * (soundManager->REFERENCE_DISTANCE / (soundManager->REFERENCE_DISTANCE + (soundManager->ROLLOFF_FACTOR * (distance - soundManager->REFERENCE_DISTANCE)))); + audibility = gain * (sound_manager->REFERENCE_DISTANCE / (sound_manager->REFERENCE_DISTANCE + (sound_manager->ROLLOFF_FACTOR * (distance - sound_manager->REFERENCE_DISTANCE)))); } } @@ -86,7 +86,7 @@ if (hardware_index != -1) { int value = 0; - alGetSourcei((ALuint)soundManager->getHardwareSource(hardware_index), AL_SOURCE_STATE, &value); + alGetSourcei((ALuint)sound_manager->getHardwareSource(hardware_index), AL_SOURCE_STATE, &value); return (value == AL_PLAYING); } return false; @@ -95,7 +95,7 @@ void Sound::setEnabled(bool e) { enabled = e; - soundManager->recomputeSource(sourceIndex, REASON_PLAY, 0.0f, NULL); + sound_manager->recomputeSource(source_index, REASON_PLAY, 0.0f, NULL); } bool Sound::getEnabled() @@ -106,43 +106,43 @@ void Sound::play() { should_play = true; - soundManager->recomputeSource(sourceIndex, REASON_PLAY, 0.0f, NULL); + sound_manager->recomputeSource(source_index, REASON_PLAY, 0.0f, NULL); } void Sound::stop() { should_play = false; - soundManager->recomputeSource(sourceIndex, REASON_STOP, 0.0f, NULL); + sound_manager->recomputeSource(source_index, REASON_STOP, 0.0f, NULL); } void Sound::setGain(float gain) { this->gain = gain; - soundManager->recomputeSource(sourceIndex, REASON_GAIN, gain, NULL); + sound_manager->recomputeSource(source_index, REASON_GAIN, gain, NULL); } void Sound::setLoop(bool loop) { this->loop = loop; - soundManager->recomputeSource(sourceIndex, REASON_LOOP, (loop) ? 1.0f : 0.0f, NULL); + sound_manager->recomputeSource(source_index, REASON_LOOP, (loop) ? 1.0f : 0.0f, NULL); } void Sound::setPitch(float pitch) { this->pitch = pitch; - soundManager->recomputeSource(sourceIndex, REASON_PTCH, pitch, NULL); + sound_manager->recomputeSource(source_index, REASON_PTCH, pitch, NULL); } void Sound::setPosition(Ogre::Vector3 pos) { this->position = pos; - soundManager->recomputeSource(sourceIndex, REASON_POSN, 0.0f, &pos); + sound_manager->recomputeSource(source_index, REASON_POSN, 0.0f, &pos); } void Sound::setVelocity(Ogre::Vector3 vel) { this->velocity = vel; - soundManager->recomputeSource(sourceIndex, REASON_VLCT, 0.0f, &vel); + sound_manager->recomputeSource(source_index, REASON_VLCT, 0.0f, &vel); } #endif // USE_OPENAL Modified: trunk/source/main/audio/Sound.h =================================================================== --- trunk/source/main/audio/Sound.h 2012-05-06 03:58:36 UTC (rev 2509) +++ trunk/source/main/audio/Sound.h 2012-05-06 06:26:27 UTC (rev 2510) @@ -63,9 +63,9 @@ Ogre::Vector3 position; Ogre::Vector3 velocity; - SoundManager* soundManager; + SoundManager* sound_manager; // must not be changed during the lifetime of this object - int sourceIndex; + int source_index; }; #endif // __Sound_H_ Modified: trunk/source/main/audio/SoundManager.cpp =================================================================== --- trunk/source/main/audio/SoundManager.cpp 2012-05-06 03:58:36 UTC (rev 2509) +++ trunk/source/main/audio/SoundManager.cpp 2012-05-06 06:26:27 UTC (rev 2510) @@ -28,14 +28,13 @@ #pragma GCC diagnostic ignored "-Wfloat-equal" #endif // OGRE_PLATFORM_LINUX - bool _checkALErrors(const char *filename, int linenum) { int err = alGetError(); - if(err != AL_NO_ERROR) + if (err != AL_NO_ERROR) { - char buf[4096]=""; - sprintf(buf, "OpenAL Error: %s (0x%x), @ %s:%d\n", alGetString(err), err, filename, linenum); + char buf[4096] = {}; + sprintf(buf, "OpenAL Error: %s (0x%x), @ %s:%d", alGetString(err), err, filename, linenum); LOG(buf); return true; } @@ -50,69 +49,54 @@ const float SoundManager::REFERENCE_DISTANCE = 7.5f; SoundManager::SoundManager() : - m_audio_buffers_in_use_count(0) - , m_audio_sources_in_use_count(0) - , m_hardware_sources_in_use_count(0) - , m_hardware_sources_num(0) - , m_sound_context(NULL) - , m_sound_device(NULL) + audio_buffers_in_use_count(0) + , audio_sources_in_use_count(0) + , hardware_sources_in_use_count(0) + , hardware_sources_num(0) + , sound_context(NULL) + , audio_device(NULL) { - String audio_device = SSETTING("AudioDevice", "Default"); + String device = SSETTING("AudioDevice", ""); - if (audio_device == "No Output") return; + if (device == "") + audio_device = alcOpenDevice(NULL); + else + audio_device = alcOpenDevice(device.c_str()); - LOG("Opening Device: '" + audio_device + "'"); - - const char *alDeviceString = audio_device.c_str(); - if(audio_device == "Default") alDeviceString = NULL; - m_sound_device = alcOpenDevice(alDeviceString); - if(!m_sound_device) + if (!audio_device) { hasALErrors(); return; } - m_sound_context = alcCreateContext(m_sound_device, NULL); + sound_context = alcCreateContext(audio_device, NULL); - if (!m_sound_context) + if (!sound_context) { - ALint error= alGetError(); - LOG("SoundManager: Could not create OpenAL context, error code: " + TOSTRING(error)); - alcCloseDevice(m_sound_device); - m_sound_device = NULL; + alcCloseDevice(audio_device); + audio_device = NULL; + hasALErrors(); return; } - const char *tmp = alGetString(AL_VENDOR); - if(tmp) LOG("OpenAL vendor is: " + String(tmp)); + alcMakeContextCurrent(sound_context); - tmp = alGetString(AL_VERSION); - if(tmp) LOG("OpenAL version is: " + String(tmp)); + if (alGetString(AL_VENDOR)) LOG("SoundManager: OpenAL vendor is: " + String(alGetString(AL_VENDOR))); + if (alGetString(AL_VERSION)) LOG("SoundManager: OpenAL version is: " + String(alGetString(AL_VERSION))); + if (alGetString(AL_RENDERER)) LOG("SoundManager: OpenAL renderer is: " + String(alGetString(AL_RENDERER))); + if (alGetString(AL_EXTENSIONS)) LOG("SoundManager: OpenAL extensions are: " + String(alGetString(AL_EXTENSIONS))); + if (alcGetString(audio_device, ALC_DEVICE_SPECIFIER)) LOG("SoundManager: OpenAL device is: " + String(alcGetString(audio_device, ALC_DEVICE_SPECIFIER))); + if (alcGetString(audio_device, ALC_EXTENSIONS)) LOG("SoundManager: OpenAL ALC extensions are: " + String(alcGetString(audio_device, ALC_EXTENSIONS))); - tmp = alGetString(AL_RENDERER); - if(tmp) LOG("OpenAL renderer is: " + String(tmp)); - - tmp = alGetString(AL_EXTENSIONS); - if(tmp) LOG("OpenAL extensions are: " + String(tmp)); - - tmp = alcGetString(m_sound_device, ALC_DEVICE_SPECIFIER); - if(tmp) LOG("OpenAL device is: " + String(tmp)); - - tmp = alcGetString(m_sound_device, ALC_EXTENSIONS); - if(tmp) LOG("OpenAL ALC extensions are: " + String(tmp)); - - - alcMakeContextCurrent(m_sound_context); - // generate the AL sources - for (m_hardware_sources_num=0; m_hardware_sources_num < MAX_HARDWARE_SOURCES; m_hardware_sources_num++) + for (hardware_sources_num=0; hardware_sources_num < MAX_HARDWARE_SOURCES; hardware_sources_num++) { alGetError(); - alGenSources(1, &m_hardware_sources[m_hardware_sources_num]); + alGenSources(1, &hardware_sources[hardware_sources_num]); if (alGetError() != AL_NO_ERROR) break; - alSourcef(m_hardware_sources[m_hardware_sources_num], AL_REFERENCE_DISTANCE, REFERENCE_DISTANCE); - alSourcef(m_hardware_sources[m_hardware_sources_num], AL_ROLLOFF_FACTOR, ROLLOFF_FACTOR); - alSourcef(m_hardware_sources[m_hardware_sources_num], AL_MAX_DISTANCE, MAX_DISTANCE); + alSourcef(hardware_sources[hardware_sources_num], AL_REFERENCE_DISTANCE, REFERENCE_DISTANCE); + alSourcef(hardware_sources[hardware_sources_num], AL_ROLLOFF_FACTOR, ROLLOFF_FACTOR); + alSourcef(hardware_sources[hardware_sources_num], AL_MAX_DISTANCE, MAX_DISTANCE); } alDopplerFactor(1.0f); @@ -120,36 +104,36 @@ for (int i=0; i < MAX_HARDWARE_SOURCES; i++) { - m_hardware_sources_map[i] = -1; + hardware_sources_map[i] = -1; } - m_master_volume = FSETTING("Sound Volume", 100.0f) / 100.0f; + master_volume = FSETTING("Sound Volume", 100.0f) / 100.0f; } SoundManager::~SoundManager() { // delete the sources and buffers - alDeleteSources(MAX_HARDWARE_SOURCES, m_hardware_sources); - alDeleteBuffers(MAX_AUDIO_BUFFERS, m_audio_buffers); + alDeleteSources(MAX_HARDWARE_SOURCES, hardware_sources); + alDeleteBuffers(MAX_AUDIO_BUFFERS, audio_buffers); // destroy the sound context and device - m_sound_context = alcGetCurrentContext(); - m_sound_device = alcGetContextsDevice(m_sound_context); - alcMakeContextCurrent(NULL); - alcDestroyContext(m_sound_context); - if (m_sound_device) + sound_context = alcGetCurrentContext(); + audio_device = alcGetContextsDevice(sound_context); + alcMakeContextCurrent(NULL); + alcDestroyContext(sound_context); + if (audio_device) { - alcCloseDevice(m_sound_device); + alcCloseDevice(audio_device); } LOG("SoundManager destroyed."); } void SoundManager::setCamera(Ogre::Vector3 position, Ogre::Vector3 direction, Ogre::Vector3 up, Ogre::Vector3 velocity) { - if (!m_sound_device) return; + if (!audio_device) return; camera_position = position; recomputeAllSources(); - + float orientation[6]; // direction orientation[0] = direction.x; @@ -173,36 +157,36 @@ // called when the camera moves void SoundManager::recomputeAllSources() { - if (!m_sound_device) return; + if (!audio_device) return; - for (int i=0; i < m_audio_sources_in_use_count; i++) + for (int i=0; i < audio_sources_in_use_count; i++) { - m_audio_sources[i]->computeAudibility(camera_position); - m_audio_sources_most_audible[i].first = i; - m_audio_sources_most_audible[i].second = m_audio_sources[i]->audibility; + audio_sources[i]->computeAudibility(camera_position); + audio_sources_most_audible[i].first = i; + audio_sources_most_audible[i].second = audio_sources[i]->audibility; } // sort first 'num_hardware_sources' sources by audibility // see: https://en.wikipedia.org/wiki/Selection_algorithm - if ((m_audio_sources_in_use_count - 1) > m_hardware_sources_num) + if ((audio_sources_in_use_count - 1) > hardware_sources_num) { - std::nth_element(m_audio_sources_most_audible, m_audio_sources_most_audible+m_hardware_sources_num, m_audio_sources_most_audible+m_audio_sources_in_use_count-1, compareByAudibility); + std::nth_element(audio_sources_most_audible, audio_sources_most_audible+hardware_sources_num, audio_sources_most_audible+audio_sources_in_use_count-1, compareByAudibility); } // retire out of range sources first - for (int i=0; i < m_audio_sources_in_use_count; i++) + for (int i=0; i < audio_sources_in_use_count; i++) { - if (m_audio_sources[m_audio_sources_most_audible[i].first]->hardware_index != -1 && (i >= m_hardware_sources_num || m_audio_sources_most_audible[i].second == 0)) - retire(m_audio_sources_most_audible[i].first); + if (audio_sources[audio_sources_most_audible[i].first]->hardware_index != -1 && (i >= hardware_sources_num || audio_sources_most_audible[i].second == 0)) + retire(audio_sources_most_audible[i].first); } // assign new sources - for (int i=0; i < std::min(m_audio_sources_in_use_count, m_hardware_sources_num); i++) + for (int i=0; i < std::min(audio_sources_in_use_count, hardware_sources_num); i++) { - if (m_audio_sources[m_audio_sources_most_audible[i].first]->hardware_index == -1 && m_audio_sources_most_audible[i].second > 0) + if (audio_sources[audio_sources_most_audible[i].first]->hardware_index == -1 && audio_sources_most_audible[i].second > 0) { - for (int j=0; j < m_hardware_sources_num; j++) + for (int j=0; j < hardware_sources_num; j++) { - if (m_hardware_sources_map[j] == -1) + if (hardware_sources_map[j] == -1) { - assign(m_audio_sources_most_audible[i].first, j); + assign(audio_sources_most_audible[i].first, j); break; } } @@ -212,12 +196,12 @@ void SoundManager::recomputeSource(int source_index, int reason, float vfl, Vector3 *vvec) { - if (!m_sound_device) return; - m_audio_sources[source_index]->computeAudibility(camera_position); + if (!audio_device) return; + audio_sources[source_index]->computeAudibility(camera_position); - if (m_audio_sources[source_index]->audibility == 0.0f) + if (audio_sources[source_index]->audibility == 0.0f) { - if (m_audio_sources[source_index]->hardware_index != -1) + if (audio_sources[source_index]->hardware_index != -1) { // retire the source if it is currently assigned retire(source_index); @@ -225,30 +209,30 @@ } else { // this is a potentially audible m_audio_sources[source_index] - if (m_audio_sources[source_index]->hardware_index != -1) + if (audio_sources[source_index]->hardware_index != -1) { // m_audio_sources[source_index] already playing // update the AL settings switch (reason) { - case Sound::REASON_PLAY: alSourcePlay(m_hardware_sources[m_audio_sources[source_index]->hardware_index]); break; - case Sound::REASON_STOP: alSourceStop(m_hardware_sources[m_audio_sources[source_index]->hardware_index]); break; - case Sound::REASON_GAIN: alSourcef(m_hardware_sources[m_audio_sources[source_index]->hardware_index], AL_GAIN, vfl * m_master_volume); break; - case Sound::REASON_LOOP: alSourcei(m_hardware_sources[m_audio_sources[source_index]->hardware_index], AL_LOOPING, (vfl > 0.5) ? AL_TRUE : AL_FALSE); break; - case Sound::REASON_PTCH: alSourcef(m_hardware_sources[m_audio_sources[source_index]->hardware_index], AL_PITCH, vfl); break; - case Sound::REASON_POSN: alSource3f(m_hardware_sources[m_audio_sources[source_index]->hardware_index], AL_POSITION, vvec->x, vvec->y, vvec->z); break; - case Sound::REASON_VLCT: alSource3f(m_hardware_sources[m_audio_sources[source_index]->hardware_index], AL_VELOCITY, vvec->x, vvec->y, vvec->z); break; - default: break; + case Sound::REASON_PLAY: alSourcePlay(hardware_sources[audio_sources[source_index]->hardware_index]); break; + case Sound::REASON_STOP: alSourceStop(hardware_sources[audio_sources[source_index]->hardware_index]); break; + case Sound::REASON_GAIN: alSourcef(hardware_sources[audio_sources[source_index]->hardware_index], AL_GAIN, vfl * master_volume); break; + case Sound::REASON_LOOP: alSourcei(hardware_sources[audio_sources[source_index]->hardware_index], AL_LOOPING, (vfl > 0.5) ? AL_TRUE : AL_FALSE); break; + case Sound::REASON_PTCH: alSourcef(hardware_sources[audio_sources[source_index]->hardware_index], AL_PITCH, vfl); break; + case Sound::REASON_POSN: alSource3f(hardware_sources[audio_sources[source_index]->hardware_index], AL_POSITION, vvec->x, vvec->y, vvec->z); break; + case Sound::REASON_VLCT: alSource3f(hardware_sources[audio_sources[source_index]->hardware_index], AL_VELOCITY, vvec->x, vvec->y, vvec->z); break; + default: break; } } else { // try to make it play by the hardware // check if there is one free m_audio_sources[source_index] in the pool - if (m_hardware_sources_in_use_count < m_hardware_sources_num) + if (hardware_sources_in_use_count < hardware_sources_num) { - for (int i=0; i < m_hardware_sources_num; i++) + for (int i=0; i < hardware_sources_num; i++) { - if (m_hardware_sources_map[i] == -1) + if (hardware_sources_map[i] == -1) { assign(source_index, i); break; @@ -260,19 +244,19 @@ // note: we know the table m_hardware_sources_map is full! float fv = 1.0f; int al_faintest = 0; - for (int i=0; i < m_hardware_sources_num; i++) + for (int i=0; i < hardware_sources_num; i++) { - if (m_hardware_sources_map[i] >= 0 && m_audio_sources[m_hardware_sources_map[i]]->audibility < fv) + if (hardware_sources_map[i] >= 0 && audio_sources[hardware_sources_map[i]]->audibility < fv) { - fv = m_audio_sources[m_hardware_sources_map[i]]->audibility; + fv = audio_sources[hardware_sources_map[i]]->audibility; al_faintest = i; } } // check to ensure that the sound is louder than the faintest sound currently playing - if (fv < m_audio_sources[source_index]->audibility) + if (fv < audio_sources[source_index]->audibility) { // this new m_audio_sources[source_index] is louder than the faintest! - retire(m_hardware_sources_map[al_faintest]); + retire(hardware_sources_map[al_faintest]); assign(source_index, al_faintest); } // else this m_audio_sources[source_index] is too faint, we don't play it! @@ -280,64 +264,64 @@ } } } - + void SoundManager::assign(int source_index, int hardware_index) { - if (!m_sound_device) return; - m_audio_sources[source_index]->hardware_index = hardware_index; - m_hardware_sources_map[hardware_index] = source_index; + if (!audio_device) return; + audio_sources[source_index]->hardware_index = hardware_index; + hardware_sources_map[hardware_index] = source_index; // the hardware source is supposed to be stopped! - alSourcei(m_hardware_sources[hardware_index], AL_BUFFER, m_audio_sources[source_index]->buffer); - alSourcef(m_hardware_sources[hardware_index], AL_GAIN, m_audio_sources[source_index]->gain*m_master_volume); - alSourcei(m_hardware_sources[hardware_index], AL_LOOPING, (m_audio_sources[source_index]->loop)?AL_TRUE:AL_FALSE); - alSourcef(m_hardware_sources[hardware_index], AL_PITCH, m_audio_sources[source_index]->pitch); - alSource3f(m_hardware_sources[hardware_index], AL_POSITION, m_audio_sources[source_index]->position.x,m_audio_sources[source_index]->position.y,m_audio_sources[source_index]->position.z); - alSource3f(m_hardware_sources[hardware_index], AL_VELOCITY, m_audio_sources[source_index]->velocity.x,m_audio_sources[source_index]->velocity.y,m_audio_sources[source_index]->velocity.z); - if (m_audio_sources[source_index]->should_play) + alSourcei(hardware_sources[hardware_index], AL_BUFFER, audio_sources[source_index]->buffer); + alSourcef(hardware_sources[hardware_index], AL_GAIN, audio_sources[source_index]->gain*master_volume); + alSourcei(hardware_sources[hardware_index], AL_LOOPING, (audio_sources[source_index]->loop)?AL_TRUE:AL_FALSE); + alSourcef(hardware_sources[hardware_index], AL_PITCH, audio_sources[source_index]->pitch); + alSource3f(hardware_sources[hardware_index], AL_POSITION, audio_sources[source_index]->position.x,audio_sources[source_index]->position.y,audio_sources[source_index]->position.z); + alSource3f(hardware_sources[hardware_index], AL_VELOCITY, audio_sources[source_index]->velocity.x,audio_sources[source_index]->velocity.y,audio_sources[source_index]->velocity.z); + if (audio_sources[source_index]->should_play) { - alSourcePlay(m_hardware_sources[hardware_index]); + alSourcePlay(hardware_sources[hardware_index]); } - m_hardware_sources_in_use_count++; + hardware_sources_in_use_count++; } void SoundManager::retire(int source_index) { - if (!m_sound_device) return; - if (m_audio_sources[source_index]->hardware_index == -1) return; - alSourceStop(m_hardware_sources[m_audio_sources[source_index]->hardware_index]); - m_hardware_sources_map[m_audio_sources[source_index]->hardware_index] = -1; - m_audio_sources[source_index]->hardware_index = -1; - m_hardware_sources_in_use_count--; + if (!audio_device) return; + if (audio_sources[source_index]->hardware_index == -1) return; + alSourceStop(hardware_sources[audio_sources[source_index]->hardware_index]); + hardware_sources_map[audio_sources[source_index]->hardware_index] = -1; + audio_sources[source_index]->hardware_index = -1; + hardware_sources_in_use_count--; } void SoundManager::pauseAllSounds() { - if (!m_sound_device) return; + if (!audio_device) return; // no mutex needed alListenerf(AL_GAIN, 0.0f); } void SoundManager::resumeAllSounds() { - if (!m_sound_device) return; + if (!audio_device) return; // no mutex needed - alListenerf(AL_GAIN, m_master_volume); + alListenerf(AL_GAIN, master_volume); } void SoundManager::setMasterVolume(float v) { - if (!m_sound_device) return; + if (!audio_device) return; // no mutex needed - m_master_volume = v; - alListenerf(AL_GAIN, m_master_volume); + master_volume = v; + alListenerf(AL_GAIN, master_volume); } Sound* SoundManager::createSound(String filename) { - if (!m_sound_device) return NULL; + if (!audio_device) return NULL; - if (m_audio_buffers_in_use_count >= MAX_AUDIO_BUFFERS) + if (audio_buffers_in_use_count >= MAX_AUDIO_BUFFERS) { LOG("SoundManager: Reached MAX_AUDIO_BUFFERS limit (" + TOSTRING(MAX_AUDIO_BUFFERS) + ")"); return NULL; @@ -346,11 +330,11 @@ ALuint buffer = 0; // is the file already loaded? - for (int i=0; i < m_audio_buffers_in_use_count; i++) + for (int i=0; i < audio_buffers_in_use_count; i++) { - if (filename == m_audio_buffer_file_name[i]) + if (filename == audio_buffer_file_name[i]) { - buffer = m_audio_buffers[i]; + buffer = audio_buffers[i]; break; } } @@ -358,28 +342,28 @@ if (!buffer) { // load the file - alGenBuffers(1, &m_audio_buffers[m_audio_buffers_in_use_count]); - if (loadWAVFile(filename, m_audio_buffers[m_audio_buffers_in_use_count])) + alGenBuffers(1, &audio_buffers[audio_buffers_in_use_count]); + if (loadWAVFile(filename, audio_buffers[audio_buffers_in_use_count])) { // there was an error! - alDeleteBuffers(1, &m_audio_buffers[m_audio_buffers_in_use_count]); - m_audio_buffer_file_name[m_audio_buffers_in_use_count] = ""; + alDeleteBuffers(1, &audio_buffers[audio_buffers_in_use_count]); + audio_buffer_file_name[audio_buffers_in_use_count] = ""; return NULL; } - buffer = m_audio_buffers[m_audio_buffers_in_use_count]; - m_audio_buffer_file_name[m_audio_buffers_in_use_count] = filename; + buffer = audio_buffers[audio_buffers_in_use_count]; + audio_buffer_file_name[audio_buffers_in_use_count] = filename; } - m_audio_sources[m_audio_buffers_in_use_count] = new Sound(buffer, this, m_audio_buffers_in_use_count); + audio_sources[audio_buffers_in_use_count] = new Sound(buffer, this, audio_buffers_in_use_count); - return m_audio_sources[m_audio_buffers_in_use_count++]; + return audio_sources[audio_buffers_in_use_count++]; } bool SoundManager::loadWAVFile(String filename, ALuint buffer) { - if (!m_sound_device) return true; + if (!audio_device) return true; LOG("Loading WAV file "+filename); - + // create the Stream ResourceGroupManager *rgm=ResourceGroupManager::getSingletonPtr(); String group=rgm->findGroupContainingResource(filename); @@ -434,7 +418,7 @@ } // the next four bytes are the remaining size of the file if (stream->read(&lbuf, 4) != 4) {LOG("Could not read file "+filename);return true;} - + unsigned long dataSize=lbuf; int format=0; Modified: trunk/source/main/audio/SoundManager.h =================================================================== --- trunk/source/main/audio/SoundManager.h 2012-05-06 03:58:36 UTC (rev 2509) +++ trunk/source/main/audio/SoundManager.h 2012-05-06 06:26:27 UTC (rev 2510) @@ -32,8 +32,6 @@ friend class Sound; public: - static SoundManager* mSoundManager; - SoundManager(); ~SoundManager(); @@ -44,8 +42,10 @@ void resumeAllSounds(); void setMasterVolume(float v); - int getNumHardwareSources() { return m_hardware_sources_num; } + bool isDisabled() { return audio_device == 0; } + int getNumHardwareSources() { return hardware_sources_num; } + static const float MAX_DISTANCE; static const float ROLLOFF_FACTOR; static const float REFERENCE_DISTANCE; @@ -55,7 +55,7 @@ private: void recomputeAllSources(); void recomputeSource(int source_index, int reason, float vfl, Ogre::Vector3 *vvec); - ALuint getHardwareSource(int hardware_index) { return m_hardware_sources[hardware_index]; }; + ALuint getHardwareSource(int hardware_index) { return hardware_sources[hardware_index]; }; void assign(int source_index, int hardware_index); void retire(int source_index); @@ -63,27 +63,27 @@ bool loadWAVFile(Ogre::String filename, ALuint buffer); // active audio sources (hardware sources) - int m_hardware_sources_num; // total number of available hardware sources < MAX_HARDWARE_SOURCES - int m_hardware_sources_in_use_count; - int m_hardware_sources_map[MAX_HARDWARE_SOURCES]; // stores the hardware index for each source. -1 = unmapped - ALuint m_hardware_sources[MAX_HARDWARE_SOURCES]; // this buffer contains valid AL handles up to m_hardware_sources_num + int hardware_sources_num; // total number of available hardware sources < MAX_HARDWARE_SOURCES + int hardware_sources_in_use_count; + int hardware_sources_map[MAX_HARDWARE_SOURCES]; // stores the hardware index for each source. -1 = unmapped + ALuint hardware_sources[MAX_HARDWARE_SOURCES]; // this buffer contains valid AL handles up to m_hardware_sources_num // audio sources - int m_audio_sources_in_use_count; - Sound* m_audio_sources[MAX_AUDIO_BUFFERS]; + int audio_sources_in_use_count; + Sound* audio_sources[MAX_AUDIO_BUFFERS]; // helper for calculating the most audible sources - std::pair<int, float> m_audio_sources_most_audible[MAX_AUDIO_BUFFERS]; + std::pair<int, float> audio_sources_most_audible[MAX_AUDIO_BUFFERS]; // audio buffers: Array of AL buffers and filenames - int m_audio_buffers_in_use_count; - ALuint m_audio_buffers[MAX_AUDIO_BUFFERS]; - Ogre::String m_audio_buffer_file_name[MAX_AUDIO_BUFFERS]; + int audio_buffers_in_use_count; + ALuint audio_buffers[MAX_AUDIO_BUFFERS]; + Ogre::String audio_buffer_file_name[MAX_AUDIO_BUFFERS]; Ogre::Vector3 camera_position; - ALCdevice* m_sound_device; - ALCcontext* m_sound_context; + ALCdevice* audio_device; + ALCcontext* sound_context; - float m_master_volume; + float master_volume; }; #endif // __SoundManager_H_ Modified: trunk/source/main/audio/SoundScriptManager.cpp =================================================================== --- trunk/source/main/audio/SoundScriptManager.cpp 2012-05-06 03:58:36 UTC (rev 2509) +++ trunk/source/main/audio/SoundScriptManager.cpp 2012-05-06 06:26:27 UTC (rev 2510) @@ -37,12 +37,13 @@ const float SoundScriptInstance::PITCHDOWN_CUTOFF_FACTOR = 5.0f; SoundScriptManager::SoundScriptManager() : - soundsDisabled(false) - , loadingBase(false) + disabled(true) + , loading_base(false) , instance_counter(0) - , maxDistance(500.0f) - , rolloffFactor(1.0f) - , referenceDistance(7.5f) + , max_distance(500.0f) + , rolloff_factor(1.0f) + , reference_distance(7.5f) + , sound_manager(0) { for (int i=0; i < SS_MAX_TRIG; i++) { @@ -68,19 +69,32 @@ } // reset all states - statemap.clear(); + state_map.clear(); - soundManager = new SoundManager(); // we can give a device name if we want here - LOG("SoundScriptManager: Sound Manager started with " + TOSTRING(soundManager->getNumHardwareSources())+" sources"); - mScriptPatterns.push_back("*.soundscript"); + sound_manager = new SoundManager(); + + if (!sound_manager) + { + LOG("SoundScriptManager: Failed to create the Sound Manager"); + return; + } + + disabled = sound_manager->isDisabled(); + + if (disabled) + { + LOG("SoundScriptManager: Sound Manager is disabled"); + return; + } + + LOG("SoundScriptManager: Sound Manager started with " + TOSTRING(sound_manager->getNumHardwareSources())+" sources"); + script_patterns.push_back("*.soundscript"); ResourceGroupManager::getSingleton()._registerScriptLoader(this); - - soundsDisabled = (SSETTING("AudioDevice", "Default") == "No Output"); } void SoundScriptManager::trigOnce(Beam *truck, int trig, int linkType, int linkItemID) { - if (soundsDisabled) return; + if (disabled) return; if (truck) { @@ -90,14 +104,14 @@ void SoundScriptManager::trigOnce(int truck, int trig, int linkType, int linkItemID) { - if (soundsDisabled) return; + if (disabled) return; for (int i=0; i < free_trigs[trig]; i++) { // cycle through all instance groups SoundScriptInstance* inst = trigs[trig+i*SS_MAX_TRIG]; - if (inst->truck == truck && inst->soundLinkType == linkType && inst->soundLinkItemId == linkItemID) + if (inst->truck == truck && inst->sound_link_type == linkType && inst->sound_link_item_id == linkItemID) { inst->runOnce(); } @@ -106,7 +120,7 @@ void SoundScriptManager::trigStart(Beam *truck, int trig, int linkType, int linkItemID) { - if (soundsDisabled) return; + if (disabled) return; if (truck) { @@ -116,16 +130,16 @@ void SoundScriptManager::trigStart(int truck, int trig, int linkType, int linkItemID) { - if (soundsDisabled) return; + if (disabled) return; if (getTrigState(truck, trig, linkType, linkItemID)) return; - statemap[linkType][linkItemID][truck][trig] = true; + state_map[linkType][linkItemID][truck][trig] = true; for (int i=0; i < free_trigs[trig]; i++) { SoundScriptInstance* inst=trigs[trig+i*SS_MAX_TRIG]; - if (inst->truck == truck && inst->soundLinkType == linkType && inst->soundLinkItemId == linkItemID) + if (inst->truck == truck && inst->sound_link_type == linkType && inst->sound_link_item_id == linkItemID) { inst->start(); } @@ -134,7 +148,7 @@ void SoundScriptManager::trigStop(Beam *truck, int trig, int linkType, int linkItemID) { - if (soundsDisabled) return; + if (disabled) return; if (truck) { @@ -144,16 +158,16 @@ void SoundScriptManager::trigStop(int truck, int trig, int linkType, int linkItemID) { - if (soundsDisabled) return; + if (disabled) return; if (!getTrigState(truck, trig, linkType, linkItemID)) return; - statemap[linkType][linkItemID][truck][trig] = false; + state_map[linkType][linkItemID][truck][trig] = false; for (int i=0; i < free_trigs[trig]; i++) { SoundScriptInstance* inst=trigs[trig+i*SS_MAX_TRIG]; - if (inst->truck == truck && inst->soundLinkType == linkType && inst->soundLinkItemId == linkItemID) + if (inst->truck == truck && inst->sound_link_type == linkType && inst->sound_link_item_id == linkItemID) { inst->stop(); } @@ -162,7 +176,7 @@ void SoundScriptManager::trigToggle(Beam *truck, int trig, int linkType, int linkItemID) { - if (soundsDisabled) return; + if (disabled) return; if (truck) { @@ -172,7 +186,7 @@ void SoundScriptManager::trigToggle(int truck, int trig, int linkType, int linkItemID) { - if (soundsDisabled) return; + if (disabled) return; if (getTrigState(truck, trig, linkType, linkItemID)) trigStop(truck, trig, linkType, linkItemID); @@ -182,7 +196,7 @@ bool SoundScriptManager::getTrigState(Beam *truck, int trig, int linkType, int linkItemID) { - if (soundsDisabled) return false; + if (disabled) return false; if (truck) return getTrigState(truck->trucknum, trig, linkType, linkItemID); @@ -192,14 +206,14 @@ bool SoundScriptManager::getTrigState(int truck, int trig, int linkType, int linkItemID) { - if (soundsDisabled) return false; + if (disabled) return false; - return statemap[linkType][linkItemID][truck][trig]; + return state_map[linkType][linkItemID][truck][trig]; } void SoundScriptManager::modulate(Beam *truck, int mod, float value, int linkType, int linkItemID) { - if (soundsDisabled) return; + if (disabled) return; if (truck) { @@ -209,7 +223,7 @@ void SoundScriptManager::modulate(int truck, int mod, float value, int linkType, int linkItemID) { - if (soundsDisabled) return; + if (disabled) return; if (mod >= SS_MAX_MOD) return; @@ -217,7 +231,7 @@ { SoundScriptInstance* inst = gains[mod + i * SS_MAX_MOD]; if (!inst) continue; - if (inst->truck == truck && inst->soundLinkType == linkType && inst->soundLinkItemId == linkItemID) + if (inst->truck == truck && inst->sound_link_type == linkType && inst->sound_link_item_id == linkItemID) { // this one requires modulation float gain=value*value*inst->templ->gain_square+value*inst->templ->gain_multiplier+inst->templ->gain_offset; @@ -231,7 +245,7 @@ { SoundScriptInstance* inst = pitches[mod + i * SS_MAX_MOD]; if (!inst) continue; - if (inst->truck == truck && inst->soundLinkType == linkType && inst->soundLinkItemId == linkItemID) + if (inst->truck == truck && inst->sound_link_type == linkType && inst->sound_link_item_id == linkItemID) { // this one requires modulation float pitch=value*value*inst->templ->pitch_square+value*inst->templ->pitch_multiplier+inst->templ->pitch_offset; @@ -243,13 +257,13 @@ void SoundScriptManager::setCamera(Vector3 position, Vector3 direction, Vector3 up, Vector3 velocity) { - if (soundsDisabled) return; - soundManager->setCamera(position, direction, up, velocity); + if (disabled) return; + sound_manager->setCamera(position, direction, up, velocity); } const StringVector& SoundScriptManager::getScriptPatterns(void) const { - return mScriptPatterns; + return script_patterns; } Real SoundScriptManager::getLoadingOrder(void) const @@ -267,7 +281,7 @@ return NULL; } - SoundScriptTemplate *ssi = new SoundScriptTemplate(name, groupname, filename, loadingBase); + SoundScriptTemplate *ssi = new SoundScriptTemplate(name, groupname, filename, loading_base); templates[name] = ssi; return ssi; } @@ -277,7 +291,7 @@ // first, search if there is a template name collision for(std::map<Ogre::String, SoundScriptTemplate*>::iterator it = templates.begin(); it!=templates.end();) { - if (it->second && it->second->groupname == groupname) + if (it->second && it->second->group_name == groupname) { templates.erase(it++); } else @@ -293,7 +307,7 @@ for(std::map<Ogre::String, SoundScriptTemplate*>::iterator it = templates.begin(); it != templates.end();) { - if (it->second && !it->second->baseTemplate) + if (it->second && !it->second->base_template) { delete(it->second); it->second = 0; @@ -336,7 +350,7 @@ return NULL; // reached limit! } - SoundScriptInstance* inst = new SoundScriptInstance(truck, templ, soundManager, templ->filename+"-"+TOSTRING(truck)+"-"+TOSTRING(instance_counter), soundLinkType, soundLinkItemId); + SoundScriptInstance* inst = new SoundScriptInstance(truck, templ, sound_manager, templ->file_name+"-"+TOSTRING(truck)+"-"+TOSTRING(instance_counter), soundLinkType, soundLinkItemId); instance_counter++; // register to lookup tables @@ -436,25 +450,25 @@ } } -void SoundScriptManager::soundEnable(bool state) +void SoundScriptManager::setEnabled(bool state) { if (state) - soundManager->resumeAllSounds(); + sound_manager->resumeAllSounds(); else - soundManager->pauseAllSounds(); + sound_manager->pauseAllSounds(); } //===================================================================== SoundScriptTemplate::SoundScriptTemplate(String name, String groupname, String filename, bool baseTemplate) : - baseTemplate(baseTemplate) - , filename(filename) + base_template(baseTemplate) + , file_name(filename) , free_sound(0) , gain_multiplier(1.0f) , gain_offset(0.0f) , gain_source(SS_MOD_NONE) , gain_square(0.0f) - , groupname(groupname) + , group_name(groupname) , has_start_sound(false) , has_stop_sound(false) , name(name) @@ -671,32 +685,32 @@ //==================================================================== -SoundScriptInstance::SoundScriptInstance(int truck, SoundScriptTemplate *templ, SoundManager* sm, String instancename, int soundLinkType, int soundLinkItemId) : +SoundScriptInstance::SoundScriptInstance(int truck, SoundScriptTemplate *templ, SoundManager* sound_manager, String instancename, int soundLinkType, int soundLinkItemId) : truck(truck) , templ(templ) - , sm(sm) - , soundLinkType(soundLinkType) - , soundLinkItemId(soundLinkItemId) - , startSound(NULL) - , startSound_pitchgain(0.0f) - , stopSound(NULL) - , stopSound_pitchgain(0.0f) + , sound_manager(sound_manager) + , sound_link_type(soundLinkType) + , sound_link_item_id(soundLinkItemId) + , start_sound(NULL) + , start_sound_pitchgain(0.0f) + , stop_sound(NULL) + , stop_sound_pitchgain(0.0f) , lastgain(1.0f) { // create sounds if (templ->has_start_sound) { - startSound = sm->createSound(templ->start_sound_name); + start_sound = sound_manager->createSound(templ->start_sound_name); } if (templ->has_stop_sound) { - stopSound = sm->createSound(templ->stop_sound_name); + stop_sound = sound_manager->createSound(templ->stop_sound_name); } for (int i=0; i < templ->free_sound; i++) { - sounds[i] = sm->createSound(templ->sound_names[i]); + sounds[i] = sound_manager->createSound(templ->sound_names[i]); } setPitch(0.0f); @@ -707,13 +721,13 @@ void SoundScriptInstance::setPitch(float value) { - if (startSound) + if (start_sound) { - startSound_pitchgain = pitchgain_cutoff(templ->start_sound_pitch, value); + start_sound_pitchgain = pitchgain_cutoff(templ->start_sound_pitch, value); - if (startSound_pitchgain != 0.0f && templ->start_sound_pitch != 0.0f) + if (start_sound_pitchgain != 0.0f && templ->start_sound_pitch != 0.0f) { - startSound->setPitch(value / templ->start_sound_pitch); + start_sound->setPitch(value / templ->start_sound_pitch); } } @@ -821,13 +835,13 @@ } } - if (stopSound) + if (stop_sound) { - stopSound_pitchgain = pitchgain_cutoff(templ->stop_sound_pitch, value); + stop_sound_pitchgain = pitchgain_cutoff(templ->stop_sound_pitch, value); - if (stopSound_pitchgain != 0.0f && templ->stop_sound_pitch != 0.0f) + if (stop_sound_pitchgain != 0.0f && templ->stop_sound_pitch != 0.0f) { - stopSound->setPitch(value / templ->stop_sound_pitch); + stop_sound->setPitch(value / templ->stop_sound_pitch); } } @@ -858,9 +872,9 @@ void SoundScriptInstance::setGain(float value) { - if (startSound) + if (start_sound) { - startSound->setGain(value * startSound_pitchgain); + start_sound->setGain(value * start_sound_pitchgain); } for (int i=0; i < templ->free_sound; i++) @@ -871,9 +885,9 @@ } } - if (stopSound) + if (stop_sound) { - stopSound->setGain(value * stopSound_pitchgain); + stop_sound->setGain(value * stop_sound_pitchgain); } lastgain = value; @@ -881,10 +895,10 @@ void SoundScriptInstance::setPosition(Vector3 pos, Vector3 velocity) { - if (startSound) + if (start_sound) { - startSound->setPosition(pos); - startSound->setVelocity(velocity); + start_sound->setPosition(pos); + start_sound->setVelocity(velocity); } for (int i=0; i<templ->free_sound; i++) @@ -896,22 +910,22 @@ } } - if (stopSound) + if (stop_sound) { - stopSound->setPosition(pos); - stopSound->setVelocity(velocity); + stop_sound->setPosition(pos); + stop_sound->setVelocity(velocity); } } void SoundScriptInstance::runOnce() { - if (startSound) + if (start_sound) { - if (startSound->isPlaying()) + if (start_sound->isPlaying()) { return; } - startSound->play(); + start_sound->play(); } for (int i=0; i<templ->free_sound; i++) @@ -927,22 +941,22 @@ } } - if (stopSound) + if (stop_sound) { - if (stopSound->isPlaying()) + if (stop_sound->isPlaying()) { return; } - stopSound->play(); + stop_sound->play(); } } void SoundScriptInstance::start() { - if (startSound) + if (start_sound) { - startSound->stop(); - startSound->play(); + start_sound->stop(); + start_sound->play(); } for (int i=0; i<templ->free_sound; i++) @@ -962,23 +976,23 @@ if (sounds[i]) sounds[i]->stop(); } - if (stopSound) + if (stop_sound) { - stopSound->stop(); - stopSound->play(); + stop_sound->stop(); + stop_sound->play(); } } void SoundScriptInstance::setEnabled(bool e) { - if (startSound) + if (start_sound) { - startSound->setEnabled(e); + start_sound->setEnabled(e); } - if (stopSound) + if (stop_sound) { - stopSound->setEnabled(e); + stop_sound->setEnabled(e); } for (int i=0; i < templ->free_sound; i++) Modified: trunk/source/main/audio/SoundScriptManager.h =================================================================== --- trunk/source/main/audio/SoundScriptManager.h 2012-05-06 03:58:36 UTC (rev 2509) +++ trunk/source/main/audio/SoundScriptManager.h 2012-05-06 06:26:27 UTC (rev 2510) @@ -23,8 +23,10 @@ #include "RoRPrerequisites.h" +#include <OgreScriptLoader.h> + +// deprecated #include "BeamData.h" // for MAX_TRUCKS -#include "OgreScriptLoader.h" #include "Singleton.h" enum { @@ -169,10 +171,10 @@ bool setParameter(Ogre::StringVector vec); Ogre::String name; - Ogre::String filename; - Ogre::String groupname; + Ogre::String file_name; + Ogre::String group_name; - bool baseTemplate; + bool base_template; bool has_start_sound; bool has_stop_sound; bool unpitchable; @@ -221,18 +223,18 @@ float pitchgain_cutoff(float sourcepitch, float targetpitch); SoundScriptTemplate* templ; - SoundManager* sm; - Sound *startSound; - Sound *stopSound; + SoundManager* sound_manager; + Sound *start_sound; + Sound *stop_sound; Sound *sounds[MAX_SOUNDS_PER_SCRIPT]; - float startSound_pitchgain; - float stopSound_pitchgain; + float start_sound_pitchgain; + float stop_sound_pitchgain; float sounds_pitchgain[MAX_SOUNDS_PER_SCRIPT]; float lastgain; int truck; // holds the number of the truck this is for. important - int soundLinkType; // holds the SL_ type this is bound to - int soundLinkItemId; // holds the item number this is for + int sound_link_type; // holds the SL_ type this is bound to + int sound_link_item_id; // holds the item number this is for }; class SoundScriptManager : public Ogre::ScriptLoader, public RoRSingleton<SoundScriptManager> @@ -264,13 +266,18 @@ void modulate (int truck, int mod, float value, int linkType = SL_DEFAULT, int linkItemID=-1); void modulate (Beam *b, int mod, float value, int linkType = SL_DEFAULT, int linkItemID=-1); - void soundEnable(bool state); + void setEnabled(bool state); + // deprecated + void soundEnable(bool state) { setEnabled(state); }; void setCamera(Ogre::Vector3 position, Ogre::Vector3 direction, Ogre::Vector3 up, Ogre::Vector3 velocity); - void setLoadingBaseSounds(bool v) { loadingBase = v; }; + void setLoadingBaseSounds(bool value) { loading_base = value; }; - inline bool working() { return !soundsDisabled; } + bool isDisabled() { return disabled; } + // deprecated + inline bool working() { return !disabled; } + // deprecated static const unsigned int TERRAINSOUND = MAX_TRUCKS+1; private: @@ -279,13 +286,13 @@ void skipToNextCloseBrace(Ogre::DataStreamPtr& chunk); void skipToNextOpenBrace(Ogre::DataStreamPtr& chunk); - Ogre::StringVector mScriptPatterns; + bool disabled; + bool loading_base; + float max_distance; + float reference_distance; + float rolloff_factor; int instance_counter; - bool loadingBase; - bool soundsDisabled; - float maxDistance; - float rolloffFactor; - float referenceDistance; + Ogre::StringVector script_patterns; std::map <Ogre::String, SoundScriptTemplate*> templates; @@ -301,9 +308,9 @@ // state map // soundLinks, soundItems, trucks, triggers - std::map <int, std::map <int, std::map <int, std::map <int, bool > > > > statemap; + std::map <int, std::map <int, std::map <int, std::map <int, bool > > > > state_map; - SoundManager* soundManager; + SoundManager* sound_manager; }; #endif // __SoundScriptManager_H_ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Live Security Virtual Conference Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ _______________________________________________ Rigsofrods-devel mailing list Rigsofrods-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rigsofrods-devel