TODO: Use std::optional for pose argument.
| 166 | |
| 167 | // TODO: Use std::optional for pose argument. |
| 168 | bool SoundEffect(int soundID, Pose* pose, SoundEnvironment soundEnv, float pitchMult, float gainMult) |
| 169 | { |
| 170 | if (!g_Configuration.EnableSound) |
| 171 | return false; |
| 172 | |
| 173 | if (soundID >= g_Level.SoundMap.size()) |
| 174 | return false; |
| 175 | |
| 176 | if (BASS_GetDevice() == -1) |
| 177 | return false; |
| 178 | |
| 179 | // Test if sound effect environment matches camera environment. |
| 180 | if (soundEnv != SoundEnvironment::Always) |
| 181 | { |
| 182 | bool isCameraUnderwater = TestEnvironment(ENV_FLAG_WATER, Camera.pos.RoomNumber); |
| 183 | if ((soundEnv == SoundEnvironment::Underwater && !isCameraUnderwater) || |
| 184 | (soundEnv != SoundEnvironment::Underwater && isCameraUnderwater)) |
| 185 | { |
| 186 | return false; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // Get actual sample index from sound map. |
| 191 | int sampleIndex = g_Level.SoundMap[soundID]; |
| 192 | |
| 193 | // -1 means no such effect exists in level file.Set to -2 afterwards to prevent further debug message firings. |
| 194 | if (sampleIndex == -1) |
| 195 | { |
| 196 | TENLog("Missing sound effect " + std::to_string(soundID), LogLevel::Warning); |
| 197 | g_Level.SoundMap[soundID] = -2; |
| 198 | return false; |
| 199 | } |
| 200 | else if (sampleIndex == -2) |
| 201 | { |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | const auto& sample = g_Level.SoundDetails[sampleIndex]; |
| 206 | if (sample.Number < 0) |
| 207 | { |
| 208 | TENLog("No valid samples count for effect " + std::to_string(sampleIndex), LogLevel::Warning); |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | // Assign common sample flags. |
| 213 | DWORD sampleFlags = SOUND_SAMPLE_FLAGS; |
| 214 | |
| 215 | // Test play chance. |
| 216 | if (sample.Randomness && ((GetRandomControl() & UCHAR_MAX) > sample.Randomness)) |
| 217 | return false; |
| 218 | |
| 219 | // Apply 3D attribute only to sound with position property. |
| 220 | if (pose != nullptr) |
| 221 | sampleFlags |= BASS_SAMPLE_3D; |
| 222 | |
| 223 | // Set and randomize volume (if needed). |
| 224 | float gain = ((float)sample.Volume / UCHAR_MAX) * std::clamp(gainMult, SOUND_MIN_PARAM_MULTIPLIER, SOUND_MAX_PARAM_MULTIPLIER); |
| 225 | if ((sample.Flags & SOUND_FLAG_RND_GAIN)) |